00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <nc_tests.h>
00011 #include <netcdf.h>
00012
00013 #define DIM_LEN 4
00014 #define DIM1_NAME "lat"
00015 #define VAR_NAME "earth"
00016 #define COORD_SYSTEM "coord_system"
00017 #define COORD_NAME "coord_name"
00018 #define TRANSFORM_NAME "transform_name"
00019 #define TRANSFORM_TYPE "transform_type"
00020 #define TRANSFORM_NAME "transform_name"
00021
00022 #define COORDINATE_AXES "_CoordinateAxes"
00023 #define COORDINATE_Z_IS_POSITIVE "_CoordinateZisPositive"
00024 #define Z_UP "up"
00025 #define Z_DOWN "down"
00026
00027
00028
00029 #define EARTH "earth"
00030 #define AIR "air"
00031 #define TIME "time"
00032 #define LEVEL "level"
00033 #define LEVEL_LEN 17
00034 #define LAT "lat"
00035 #define LAT_LEN 73
00036 #define LON "lon"
00037 #define LON_LEN 144
00038 #define NDIMS 4
00039 #define LAT_LON_COORDINATE_SYSTEM "LatLonCoordinateSytem"
00040 #define COORDINATE_SYSTEMS "_CoordinateSystems"
00041 #define TRANSFORM_NAME "transform_name"
00042 #define COORDINATE_TRANSFORMS "_CoordinateTransforms"
00043 #define LAMBERT_CONFORMAL_PROJECTION "LambertConformalProjection"
00044 #define PROJECTION "Projection"
00045 #define LAMBERT_CONFORMAL_CONIC "lambert_conformal_conic"
00046
00047
00048
00049 #ifdef USE_NETCDF4
00050 #define NUM_FORMATS (4)
00051 #else
00052 #define NUM_FORMATS (2)
00053 #endif
00054
00055 static void
00056 test_axis(const char *testfile)
00057 {
00058 int ncid, dimid, varid, dimids[1] = {0};
00059 int axis_type;
00060 int nvars, ndims, natts, unlimdimid;
00061 char value[NC_MAX_NAME + 1];
00062
00063
00064 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00065
00066
00067 if (nc_def_dim(ncid, DIM1_NAME, DIM_LEN, &dimid)) ERR;
00068 if (dimid != 0) ERR;
00069 if (nc_def_var(ncid, DIM1_NAME, NC_FLOAT, 1, dimids, &varid)) ERR;
00070 if (varid != 0) ERR;
00071 if (nc_def_axis_type(ncid, varid, NC_LONGITUDE)) ERR;
00072 if (nc_inq_axis_type(ncid, varid, &axis_type)) ERR;
00073 if (axis_type != NC_LONGITUDE) ERR;
00074
00075
00076 if (nc_close(ncid)) ERR;
00077
00078
00079 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00080 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00081 if (ndims != 1 && nvars != 2 && natts != 0 && unlimdimid != -1) ERR;
00082 if (nc_inq_axis_type(ncid, 0, &axis_type)) ERR;
00083 if (axis_type != NC_LONGITUDE) ERR;
00084
00085 if (nc_close(ncid)) ERR;
00086
00087
00088 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00089
00090
00091 if (nc_def_dim(ncid, DIM1_NAME, DIM_LEN, &dimid)) ERR;
00092 if (dimid != 0) ERR;
00093 if (nc_def_var(ncid, DIM1_NAME, NC_FLOAT, 1, dimids, &varid)) ERR;
00094 if (varid != 0) ERR;
00095 if (nc_def_axis_type(ncid, varid, NC_HEIGHT_UP)) ERR;
00096 if (nc_inq_axis_type(ncid, varid, &axis_type)) ERR;
00097 if (axis_type != NC_HEIGHT_UP) ERR;
00098
00099
00100 if (nc_close(ncid)) ERR;
00101
00102
00103 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00104 if (nc_inq_axis_type(ncid, 0, &axis_type)) ERR;
00105 if (axis_type != NC_HEIGHT_UP) ERR;
00106 if (nc_get_att_text(ncid, varid, COORDINATE_Z_IS_POSITIVE,
00107 value)) ERR;
00108 if (strcmp(value, Z_UP)) ERR;
00109
00110 if (nc_close(ncid)) ERR;
00111 }
00112
00113 static void
00114 test_system(const char *testfile)
00115 {
00116 int ncid, dimid, axis_varid, system_varid, dimids[1] = {0};
00117 int axes_varids[1] = {0};
00118 int nvars, ndims, natts, unlimdimid;
00119 char name_in[NC_MAX_NAME + 1];
00120 int naxes_in, axes_varids_in[1];
00121
00122
00123 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00124
00125
00126 if (nc_def_dim(ncid, DIM1_NAME, DIM_LEN, &dimid)) ERR;
00127 if (nc_def_var(ncid, DIM1_NAME, NC_FLOAT, 1, dimids, &axis_varid)) ERR;
00128 if (axis_varid != 0) ERR;
00129 if (nc_def_coord_system(ncid, COORD_SYSTEM, 1, axes_varids, &system_varid)) ERR;
00130 if (system_varid != 1) ERR;
00131 if (nc_inq_coord_system(ncid, 1, name_in, &naxes_in, axes_varids_in)) ERR;
00132 if (strcmp(name_in, COORD_SYSTEM) || naxes_in != 1 ||
00133 axes_varids_in[0] != axes_varids[0]) ERR;
00134
00135
00136
00137 if (nc_close(ncid)) ERR;
00138
00139
00140 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00141 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00142 if (ndims != 1 && nvars != 2 && natts != 0 && unlimdimid != -1) ERR;
00143 if (nc_inq_coord_system(ncid, 1, name_in, &naxes_in,
00144 axes_varids_in)) ERR;
00145 if (strcmp(name_in, COORD_SYSTEM) || naxes_in != 1 ||
00146 axes_varids_in[0] != axes_varids[0]) ERR;
00147
00148 if (nc_close(ncid)) ERR;
00149 }
00150
00151
00152
00153
00154 static void
00155 test_system_assign(const char *testfile)
00156 {
00157 int ncid, axis_varids[NDIMS], system_varid, dimids[NDIMS];
00158 int nvars, ndims, natts, unlimdimid;
00159 int dimids_in[NDIMS], ndims_in;
00160 size_t len_in;
00161 char name_in[NC_MAX_NAME + 1];
00162 char systems_in[NC_MAX_NAME + 1];
00163 int naxes_in, axis_varids_in[NDIMS], natts_in;
00164 char *dim_name[] = {TIME, LEVEL, LAT, LON};
00165 size_t dim_len[NDIMS] = {NC_UNLIMITED, LEVEL_LEN, LAT_LEN, LON_LEN};
00166 int earth_varid, air_varid;
00167 int varid_in;
00168 nc_type type_in;
00169 int d;
00170
00171
00172 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00173
00174
00175 for (d = 0; d < NDIMS; d++)
00176 {
00177 if (nc_def_dim(ncid, dim_name[d], dim_len[d], &dimids[d])) ERR;
00178 if (nc_def_var(ncid, dim_name[d], NC_FLOAT, 1, &dimids[d],
00179 &axis_varids[d])) ERR;
00180 }
00181
00182
00183
00184
00185 if (nc_def_var(ncid, EARTH, NC_FLOAT, NDIMS, dimids,
00186 &earth_varid)) ERR;
00187 if (nc_def_var(ncid, AIR, NC_FLOAT, NDIMS, dimids,
00188 &air_varid)) ERR;
00189
00190
00191 if (nc_def_coord_system(ncid, LAT_LON_COORDINATE_SYSTEM, NDIMS,
00192 axis_varids, &system_varid)) ERR;
00193
00194
00195 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00196 if (ndims != 4 && nvars != 7 && natts != 0 && unlimdimid != dimids[3]) ERR;
00197 if (nc_inq_varid(ncid, LAT_LON_COORDINATE_SYSTEM, &varid_in)) ERR;
00198 if (varid_in != system_varid) ERR;
00199 if (nc_inq_var(ncid, system_varid, name_in, &type_in, &ndims_in,
00200 dimids_in, &natts_in)) ERR;
00201 if (strcmp(name_in, LAT_LON_COORDINATE_SYSTEM) || type_in != NC_CHAR ||
00202 ndims_in != 0 || natts_in != 1);
00203 if (nc_inq_att(ncid, system_varid, COORDINATE_AXES, &type_in, &len_in)) ERR;
00204 if (type_in != NC_CHAR) ERR;
00205 if (nc_inq_coord_system(ncid, system_varid, name_in, &naxes_in, axis_varids_in)) ERR;
00206 if (strcmp(name_in, LAT_LON_COORDINATE_SYSTEM) || naxes_in != NDIMS) ERR;
00207 for (d = 0; d < NDIMS; d++)
00208 if (axis_varids_in[d] != axis_varids[d]) ERR;
00209
00210
00211 if (nc_assign_coord_system(ncid, earth_varid, system_varid)) ERR;
00212 if (nc_assign_coord_system(ncid, air_varid, system_varid)) ERR;
00213
00214
00215 if (nc_inq_varnatts(ncid, earth_varid, &natts_in)) ERR;
00216 if (natts_in != 1) ERR;
00217 if (nc_inq_att(ncid, earth_varid, COORDINATE_SYSTEMS, &type_in, &len_in)) ERR;
00218 if (type_in != NC_CHAR) ERR;
00219 if (nc_inq_varnatts(ncid, air_varid, &natts_in)) ERR;
00220 if (natts_in != 1) ERR;
00221 if (nc_get_att_text(ncid, earth_varid, COORDINATE_SYSTEMS, systems_in)) ERR;
00222 if (strcmp(systems_in, LAT_LON_COORDINATE_SYSTEM)) ERR;
00223 if (nc_inq_att(ncid, air_varid, COORDINATE_SYSTEMS, &type_in, &len_in)) ERR;
00224 if (type_in != NC_CHAR) ERR;
00225 if (nc_get_att_text(ncid, air_varid, COORDINATE_SYSTEMS, systems_in)) ERR;
00226 if (strcmp(systems_in, LAT_LON_COORDINATE_SYSTEM)) ERR;
00227
00228
00229 if (nc_close(ncid)) ERR;
00230
00231
00232 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00233 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00234 if (ndims != 4 && nvars != 7 && natts != 0 && unlimdimid != dimids[3]) ERR;
00235 if (nc_inq_var(ncid, system_varid, name_in, &type_in, &ndims_in,
00236 dimids_in, &natts_in)) ERR;
00237 if (strcmp(name_in, LAT_LON_COORDINATE_SYSTEM) || type_in != NC_CHAR ||
00238 ndims_in != 0 || natts_in != 1);
00239 if (nc_inq_att(ncid, system_varid, COORDINATE_AXES, &type_in, &len_in)) ERR;
00240 if (type_in != NC_CHAR) ERR;
00241 if (nc_inq_coord_system(ncid, system_varid, name_in, &naxes_in, axis_varids_in)) ERR;
00242 if (strcmp(name_in, LAT_LON_COORDINATE_SYSTEM) || naxes_in != NDIMS) ERR;
00243 for (d = 0; d < NDIMS; d++)
00244 if (axis_varids_in[d] != axis_varids[d]) ERR;
00245
00246
00247 if (nc_inq_varnatts(ncid, earth_varid, &natts_in)) ERR;
00248 if (natts_in != 1) ERR;
00249 if (nc_inq_att(ncid, earth_varid, COORDINATE_SYSTEMS, &type_in, &len_in)) ERR;
00250 if (type_in != NC_CHAR) ERR;
00251 if (nc_inq_varnatts(ncid, air_varid, &natts_in)) ERR;
00252 if (natts_in != 1) ERR;
00253 if (nc_get_att_text(ncid, earth_varid, COORDINATE_SYSTEMS, systems_in)) ERR;
00254 if (strcmp(systems_in, LAT_LON_COORDINATE_SYSTEM)) ERR;
00255 if (nc_inq_att(ncid, air_varid, COORDINATE_SYSTEMS, &type_in, &len_in)) ERR;
00256 if (type_in != NC_CHAR) ERR;
00257 if (nc_get_att_text(ncid, air_varid, COORDINATE_SYSTEMS, systems_in)) ERR;
00258 if (strcmp(systems_in, LAT_LON_COORDINATE_SYSTEM)) ERR;
00259
00260 if (nc_close(ncid)) ERR;
00261 }
00262
00263 static void
00264 test_transform(const char *testfile)
00265 {
00266 int ncid, transform_varid;
00267 int nvars, ndims, natts, unlimdimid;
00268 char name_in[NC_MAX_NAME + 1];
00269 char transform_name_in[NC_MAX_NAME + 1], transform_type_in[NC_MAX_NAME + 1];
00270 size_t type_len, name_len;
00271
00272
00273 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00274
00275
00276 if (nc_def_transform(ncid, TRANSFORM_NAME, TRANSFORM_TYPE,
00277 TRANSFORM_NAME, &transform_varid)) ERR;
00278 if (transform_varid != 0) ERR;
00279 if (nc_inq_transform(ncid, transform_varid, name_in, &type_len,
00280 transform_type_in, &name_len,
00281 transform_name_in)) ERR;
00282 if (strcmp(name_in, TRANSFORM_NAME) || type_len != strlen(TRANSFORM_TYPE) + 1 ||
00283 strcmp(transform_type_in, TRANSFORM_TYPE) || name_len != strlen(TRANSFORM_NAME) + 1 ||
00284 strcmp(transform_name_in, TRANSFORM_NAME)) ERR;
00285
00286
00287 if (nc_close(ncid)) ERR;
00288
00289
00290 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00291 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00292 if (ndims != 0 && nvars != 1 && natts != 0 && unlimdimid != -1) ERR;
00293 if (nc_inq_transform(ncid, 0, name_in, &type_len, transform_type_in,
00294 &name_len, transform_name_in)) ERR;
00295 if (strcmp(name_in, TRANSFORM_NAME) || type_len != strlen(TRANSFORM_TYPE) + 1 ||
00296 strcmp(transform_type_in, TRANSFORM_TYPE) || name_len != strlen(TRANSFORM_NAME) + 1 ||
00297 strcmp(transform_name_in, TRANSFORM_NAME)) ERR;
00298
00299 if (nc_close(ncid)) ERR;
00300 }
00301
00302
00303
00304 static void
00305 test_transform_assign(const char *testfile)
00306 {
00307 int ncid, axis_varids[NDIMS], system_varid, dimids[NDIMS];
00308 int transform_varid;
00309 size_t len_in, name_len, type_len;
00310 char name_in[NC_MAX_NAME + 1];
00311 char transform_in[NC_MAX_NAME + 1];
00312 char transform_type_in[NC_MAX_NAME + 1], transform_name_in[NC_MAX_NAME + 1];
00313 char *dim_name[NDIMS] = {TIME, LEVEL, LAT, LON};
00314 size_t dim_len[NDIMS] = {NC_UNLIMITED, LEVEL_LEN, LAT_LEN, LON_LEN};
00315 int axis_type[NDIMS] = {NC_TIME, NC_HEIGHT_DOWN, NC_LATITUDE, NC_LONGITUDE};
00316 int earth_varid, air_varid;
00317 int d;
00318
00319
00320 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
00321
00322
00323
00324 for (d = 0; d < NDIMS; d++)
00325 {
00326 if (nc_def_dim(ncid, dim_name[d], dim_len[d], &dimids[d])) ERR;
00327 if (nc_def_var(ncid, dim_name[d], NC_FLOAT, 1, &dimids[d],
00328 &axis_varids[d])) ERR;
00329 if (nc_def_axis_type(ncid, axis_varids[d], axis_type[d])) ERR;
00330 }
00331
00332
00333 if (nc_def_var(ncid, EARTH, NC_FLOAT, NDIMS, dimids,
00334 &earth_varid)) ERR;
00335 if (nc_def_var(ncid, AIR, NC_FLOAT, NDIMS, dimids,
00336 &air_varid)) ERR;
00337
00338
00339 if (nc_def_coord_system(ncid, LAT_LON_COORDINATE_SYSTEM, NDIMS,
00340 axis_varids, &system_varid)) ERR;
00341
00342
00343 if (nc_assign_coord_system(ncid, earth_varid, system_varid)) ERR;
00344 if (nc_assign_coord_system(ncid, air_varid, system_varid)) ERR;
00345
00346
00347 if (nc_def_transform(ncid, LAMBERT_CONFORMAL_PROJECTION, PROJECTION,
00348 LAMBERT_CONFORMAL_CONIC, &transform_varid)) ERR;
00349 if (nc_inq_transform(ncid, transform_varid, name_in, &len_in,
00350 transform_type_in, &name_len, transform_name_in)) ERR;
00351 if (strcmp(name_in, LAMBERT_CONFORMAL_PROJECTION) || len_in != strlen(PROJECTION) + 1 ||
00352 strcmp(transform_type_in, PROJECTION) || name_len != strlen(LAMBERT_CONFORMAL_CONIC) + 1 ||
00353 strcmp(transform_name_in, LAMBERT_CONFORMAL_CONIC)) ERR;
00354
00355
00356 if (nc_assign_transform(ncid, system_varid, transform_varid)) ERR;
00357
00358
00359 if (nc_get_att_text(ncid, system_varid, COORDINATE_TRANSFORMS, transform_in)) ERR;
00360 if (strcmp(transform_in, LAMBERT_CONFORMAL_PROJECTION)) ERR;
00361
00362
00363 if (nc_close(ncid)) ERR;
00364
00365
00366 if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
00367 if (nc_inq_transform(ncid, transform_varid, name_in, &type_len,
00368 transform_type_in, &name_len, transform_name_in)) ERR;
00369 if (strcmp(name_in, LAMBERT_CONFORMAL_PROJECTION) || type_len != strlen(PROJECTION) + 1 ||
00370 strcmp(transform_type_in, PROJECTION) || name_len != strlen(LAMBERT_CONFORMAL_CONIC) + 1 ||
00371 strcmp(transform_name_in, LAMBERT_CONFORMAL_CONIC)) ERR;
00372 if (nc_get_att_text(ncid, system_varid, COORDINATE_TRANSFORMS, transform_in)) ERR;
00373 if (strcmp(transform_in, LAMBERT_CONFORMAL_PROJECTION)) ERR;
00374
00375 if (nc_close(ncid)) ERR;
00376 }
00377
00378 int
00379 main(int argc, char **argv)
00380 {
00381 int i;
00382 char testfile[NC_MAX_NAME + 1];
00383
00384 printf("\n*** Testing coordinate systems.\n");
00385
00386
00387
00388
00389 for (i = NUM_FORMATS; i >= 1; i--)
00390 {
00391 switch (i)
00392 {
00393 case NC_FORMAT_CLASSIC:
00394 nc_set_default_format(NC_FORMAT_CLASSIC, NULL);
00395 fprintf(stderr, "\n\nSwitching to netCDF classic format.\n");
00396 strcpy(testfile, "tst_coords_classic.nc");
00397 break;
00398 case NC_FORMAT_64BIT:
00399 nc_set_default_format(NC_FORMAT_64BIT, NULL);
00400 fprintf(stderr, "\n\nSwitching to 64-bit offset format.\n");
00401 strcpy(testfile, "tst_coords_64bit.nc");
00402 break;
00403 #ifdef USE_NETCDF4
00404 case NC_FORMAT_NETCDF4_CLASSIC:
00405 nc_set_default_format(NC_FORMAT_NETCDF4_CLASSIC, NULL);
00406 strcpy(testfile, "tst_coords_netcdf4_classic.nc");
00407 fprintf(stderr, "\n\nSwitching to netCDF-4 format (with NC_CLASSIC_MODEL).\n");
00408 break;
00409 case NC_FORMAT_NETCDF4:
00410 nc_set_default_format(NC_FORMAT_NETCDF4, NULL);
00411 strcpy(testfile, "tst_coords_netcdf4.nc");
00412 fprintf(stderr, "\n\nSwitching to netCDF-4 format.\n");
00413 break;
00414 #endif
00415 default:
00416 fprintf(stderr, "Unexpected format!\n");
00417 return 2;
00418 }
00419
00420 printf("*** creating coordinate axis...");
00421 test_axis(testfile);
00422 SUMMARIZE_ERR;
00423
00424 printf("*** creating coordinate system...");
00425 test_system(testfile);
00426 SUMMARIZE_ERR;
00427
00428 printf("*** assigning a coordinate system...");
00429 test_system_assign(testfile);
00430 SUMMARIZE_ERR;
00431
00432 printf("*** creating coordinate transform...");
00433 test_transform(testfile);
00434 SUMMARIZE_ERR;
00435
00436 printf("*** assigning a coordinate transform...");
00437 test_transform_assign(testfile);
00438 SUMMARIZE_ERR;
00439 }
00440
00441 FINAL_RESULTS;
00442 }
00443