00001 /* ***************************************************** */ 00002 /* get_time_attributes Get time NetCDF attributes. */ 00003 /* get_time_attributes.c */ 00004 /* ***************************************************** */ 00005 /* Author: Christian Page, CERFACS, Toulouse, France. */ 00006 /* ***************************************************** */ 00007 /* Date of creation: oct 2008 */ 00008 /* Last date of modification: oct 2008 */ 00009 /* ***************************************************** */ 00010 /* Original version: 1.0 */ 00011 /* Current revision: */ 00012 /* ***************************************************** */ 00013 /* Revisions */ 00014 /* ***************************************************** */ 00019 /* LICENSE BEGIN 00020 00021 Copyright Cerfacs (Christian Page) (2015) 00022 00023 christian.page@cerfacs.fr 00024 00025 This software is a computer program whose purpose is to downscale climate 00026 scenarios using a statistical methodology based on weather regimes. 00027 00028 This software is governed by the CeCILL license under French law and 00029 abiding by the rules of distribution of free software. You can use, 00030 modify and/ or redistribute the software under the terms of the CeCILL 00031 license as circulated by CEA, CNRS and INRIA at the following URL 00032 "http://www.cecill.info". 00033 00034 As a counterpart to the access to the source code and rights to copy, 00035 modify and redistribute granted by the license, users are provided only 00036 with a limited warranty and the software's author, the holder of the 00037 economic rights, and the successive licensors have only limited 00038 liability. 00039 00040 In this respect, the user's attention is drawn to the risks associated 00041 with loading, using, modifying and/or developing or reproducing the 00042 software by the user in light of its specific status of free software, 00043 that may mean that it is complicated to manipulate, and that also 00044 therefore means that it is reserved for developers and experienced 00045 professionals having in-depth computer knowledge. Users are therefore 00046 encouraged to load and test the software's suitability as regards their 00047 requirements in conditions enabling the security of their systems and/or 00048 data to be ensured and, more generally, to use and operate it in the 00049 same conditions as regards security. 00050 00051 The fact that you are presently reading this means that you have had 00052 knowledge of the CeCILL license and that you accept its terms. 00053 00054 LICENSE END */ 00055 00056 00057 00058 00059 00060 00061 00062 #include <io.h> 00063 00065 int 00066 get_time_attributes(char **time_units, char **cal_type, char *filename, char *varname) { 00067 00077 int istat; /* Diagnostic status */ 00078 00079 int ncinid; /* NetCDF input file handle ID */ 00080 int timeinid; /* NetCDF time variable ID */ 00081 00082 size_t t_len; /* Length of time units string */ 00083 00084 /* Read data in NetCDF file */ 00085 00086 /* Open NetCDF file for reading */ 00087 printf("%s: Opening for reading time attributes in NetCDF input file %s\n", __FILE__, filename); 00088 istat = nc_open(filename, NC_NOWRITE, &ncinid); /* open for reading */ 00089 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00090 00091 /* Get ID for time variable */ 00092 istat = nc_inq_varid(ncinid, varname, &timeinid); 00093 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00094 00095 /* Get time units attribute length */ 00096 istat = nc_inq_attlen(ncinid, timeinid, "units", &t_len); 00097 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00098 00099 /* Allocate required space before retrieving values */ 00100 (*time_units) = (char *) malloc((t_len+1) * sizeof(char)); 00101 if ((*time_units) == NULL) alloc_error(__FILE__, __LINE__); 00102 00103 /* Get time units attribute value */ 00104 istat = nc_get_att_text(ncinid, timeinid, "units", *time_units); 00105 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00106 if ((*time_units)[t_len-2] == 'Z') 00107 (*time_units)[t_len-2] = '\0'; /* null terminate */ 00108 else if ((*time_units)[t_len-1] == 'Z') 00109 (*time_units)[t_len-1] = '\0'; /* null terminate */ 00110 else 00111 (*time_units)[t_len] = '\0'; 00112 00113 /* Get calendar type attribute length */ 00114 istat = nc_inq_attlen(ncinid, timeinid, "calendar", &t_len); 00115 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00116 00117 /* Allocate required space before retrieving values */ 00118 (*cal_type) = (char *) malloc(t_len + 1); 00119 if ((*cal_type) == NULL) alloc_error(__FILE__, __LINE__); 00120 00121 /* Get calendar type attribute value */ 00122 istat = nc_get_att_text(ncinid, timeinid, "calendar", *cal_type); 00123 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00124 (*cal_type)[t_len] = '\0'; /* null terminate */ 00125 00126 /* Close the intput netCDF file. */ 00127 istat = ncclose(ncinid); 00128 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__); 00129 00130 /* Success status */ 00131 return 0; 00132 }