00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include <io.h>
00063
00065 int
00066 read_netcdf_latlon(double **lon, double **lat, int *nlon, int *nlat, char *dimcoords, char *coords, char *gridname,
00067 char *lonname, char *latname, char *dimxname, char *dimyname, char *filename) {
00085 int istat;
00086
00087 size_t dimval;
00088
00089 int ncinid;
00090 int latinid;
00091 int loninid;
00092 nc_type vartype;
00093 int varndims;
00094 int vardimids[NC_MAX_VAR_DIMS];
00095 int londiminid;
00096 int latdiminid;
00097
00098 size_t start[3];
00099 size_t count[3];
00100
00101 double *tmpd = NULL;
00102
00103 int i;
00104 int j;
00105 int ndims;
00106 int ndims_xy;
00107 int npts;
00108
00109
00110
00111
00112 printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00113 istat = nc_open(filename, NC_NOWRITE, &ncinid);
00114 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00115
00116 if ( !strcmp(coords, "1D") )
00118 ndims = 1;
00119 else if ( !strcmp(coords, "2D") )
00121 ndims = 2;
00122 else
00124 ndims = 2;
00125
00126 if ( !strcmp(dimcoords, "1D") ) {
00128 ndims_xy = 1;
00129
00130
00131 istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);
00132 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00133 istat = nc_inq_dimlen(ncinid, latdiminid, &dimval);
00134 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00135 *nlat = (int) dimval;
00136
00137 istat = nc_inq_dimid(ncinid, dimxname, &londiminid);
00138 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00139 istat = nc_inq_dimlen(ncinid, londiminid, &dimval);
00140 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00141 *nlon = (int) dimval;
00142 }
00143 else if ( !strcmp(dimcoords, "2D") && !strcmp(gridname, "Latitude_Longitude") ) {
00145 ndims_xy = 2;
00146
00147
00148 istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);
00149 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150 istat = nc_inq_dimlen(ncinid, latdiminid, &dimval);
00151 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00152 *nlat = (int) dimval;
00153
00154 istat = nc_inq_dimid(ncinid, dimxname, &londiminid);
00155 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00156 istat = nc_inq_dimlen(ncinid, londiminid, &dimval);
00157 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00158 *nlon = (int) dimval;
00159 }
00160 else {
00162 ndims_xy = 2;
00163
00164
00165 istat = nc_inq_dimid(ncinid, "y", &latdiminid);
00166 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00167 istat = nc_inq_dimlen(ncinid, latdiminid, &dimval);
00168 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00169 *nlat = (int) dimval;
00170
00171 istat = nc_inq_dimid(ncinid, "x", &londiminid);
00172 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00173 istat = nc_inq_dimlen(ncinid, londiminid, &dimval);
00174 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00175 *nlon = (int) dimval;
00176 }
00177
00178
00179 istat = nc_inq_varid(ncinid, latname, &latinid);
00180 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00181
00182 istat = nc_inq_var(ncinid, latinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL);
00183 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00184 if (varndims != ndims) {
00185 (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00186 istat = ncclose(ncinid);
00187 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00188 return -1;
00189 }
00190
00191
00192 istat = nc_inq_varid(ncinid, lonname, &loninid);
00193 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00194
00195 istat = nc_inq_var(ncinid, loninid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL);
00196 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00197 if (varndims != ndims) {
00198 (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00199 istat = ncclose(ncinid);
00200 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00201 return -1;
00202 }
00203
00205 if ( !strcmp(gridname, "list") ) {
00206
00207 npts = *nlon;
00208 *nlat = 0;
00209
00210 start[0] = 0;
00211 start[1] = 0;
00212 start[2] = 0;
00213 count[0] = (size_t) npts;
00214 count[1] = 0;
00215 count[2] = 0;
00216 (*lat) = (double *) malloc(npts * sizeof(double));
00217 if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00218
00219
00220 istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00221 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00222
00223
00224 start[0] = 0;
00225 start[1] = 0;
00226 start[2] = 0;
00227 count[0] = (size_t) npts;
00228 count[1] = 0;
00229 count[2] = 0;
00230 (*lon) = (double *) malloc(npts * sizeof(double));
00231 if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00232
00233
00234 istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00235 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00236 }
00237 else if ( !strcmp(coords, "1D") ) {
00238
00239 start[0] = 0;
00240 start[1] = 0;
00241 start[2] = 0;
00242 count[0] = (size_t) (*nlat);
00243 count[1] = 0;
00244 count[2] = 0;
00245 (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00246 if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00247 tmpd = (double *) malloc((*nlat) * sizeof(double));
00248 if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00249
00250
00251 istat = nc_get_vara_double(ncinid, latinid, start, count, tmpd);
00252 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00253 for (j=0; j<(*nlat); j++)
00254 for (i=0; i<(*nlon); i++)
00255 (*lat)[i+j*(*nlon)] = tmpd[j];
00256
00257
00258 start[0] = 0;
00259 start[1] = 0;
00260 start[2] = 0;
00261 count[0] = (size_t) (*nlon);
00262 count[1] = 0;
00263 count[2] = 0;
00264 (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00265 if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00266 tmpd = (double *) realloc(tmpd, (*nlon) * sizeof(double));
00267 if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00268
00269
00270 istat = nc_get_vara_double(ncinid, loninid, start, count, tmpd);
00271 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00272 for (j=0; j<(*nlat); j++)
00273 for (i=0; i<(*nlon); i++)
00274 (*lon)[i+j*(*nlon)] = tmpd[i];
00275
00276 (void) free(tmpd);
00277
00278 }
00279 else {
00280
00281 start[0] = 0;
00282 start[1] = 0;
00283 start[2] = 0;
00284 count[0] = (size_t) (*nlat);
00285 count[1] = (size_t) (*nlon);
00286 count[2] = 0;
00287 (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00288 if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00289
00290
00291 istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00292 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00293
00294
00295 start[0] = 0;
00296 start[1] = 0;
00297 start[2] = 0;
00298 count[0] = (size_t) (*nlat);
00299 count[1] = (size_t) (*nlon);
00300 count[2] = 0;
00301 (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00302 if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00303
00304
00305 istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00306 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00307 }
00308
00309 istat = ncclose(ncinid);
00310 if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00311
00312
00313 return 0;
00314 }