Get time NetCDF attributes. More...
#include <io.h>
Go to the source code of this file.
Functions | |
int | get_time_attributes (char **time_units, char **cal_type, char *filename, char *varname) |
Get main time attributes in a NetCDF file. |
Get time NetCDF attributes.
Definition in file get_time_attributes.c.
int get_time_attributes | ( | char ** | time_units, | |
char ** | cal_type, | |||
char * | filename, | |||
char * | varname | |||
) |
Get main time attributes in a NetCDF file.
[out] | time_units | Time units (udunits) |
[out] | cal_type | Calendar type (udunits) |
[in] | filename | NetCDF input filename |
[in] | varname | NetCDF time variable name |
Definition at line 66 of file get_time_attributes.c.
References alloc_error(), and handle_netcdf_error().
00066 { 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 }