/* Create a test file with one var of type NC_BYTE. */ int create_file(int format, unsigned char *uchar_out) { int ncid, varid, cflags=0, dimids[1]; int retval; if (format == NC_FORMAT_64BIT_OFFSET) cflags |= NC_64BIT_OFFSET; else if (format == NC_FORMAT_CDF5) cflags |= NC_CDF5; else if (format == NC_FORMAT_NETCDF4_CLASSIC) { cflags |= (NC_NETCDF4|NC_CLASSIC_MODEL); } else if (format == NC_FORMAT_NETCDF4) cflags |= NC_NETCDF4; if (nc_create(FILE_NAME, cflags, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; retval = nc_put_var_uchar(ncid, varid, uchar_out); if (format == NC_FORMAT_NETCDF4 || format == NC_FORMAT_64BIT_DATA) { if (retval != NC_ERANGE) ERR; } else if (retval != NC_NOERR) ERR; if (nc_close(ncid)) ERR; return NC_NOERR; }
void nc_put_var(TYPE type,int ncid,int varid,const void *dp){ switch(type){ case(BYTE): nc_put_var_uchar(ncid,varid,(const unsigned char *)dp); break; case(SHORT): nc_put_var_short(ncid,varid,(const short *)dp); break; case(INT): nc_put_var_int(ncid,varid,(const int *)dp); break; case(LONG): nc_put_var_long(ncid,varid,(const long *)dp); break; case(FLOAT): nc_put_var_float(ncid,varid,(const float *)dp); break; case(DOUBLE): nc_put_var_double(ncid,varid,(const double *)dp); break; default: printf("unkown types nc_put_var()!\n"); } }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 file functions.\n"); { char str[NC_MAX_NAME+1]; /* Actually we never make any promises about the length of the * version string, but it is always smaller than NC_MAX_NAME. */ if (strlen(nc_inq_libvers()) > NC_MAX_NAME) ERR; strcpy(str, nc_inq_libvers()); printf("*** testing version %s...", str); } SUMMARIZE_ERR; printf("*** testing with bad inputs..."); { int ncid; /* Make sure bad create mode causes failure. */ /*if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;*/ /* Create an empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_NETCDF4, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; } SUMMARIZE_ERR; printf("*** testing simple opens and creates..."); { int ncid, ncid2, ncid3, varid, dimids[2]; int ndims, nvars, natts, unlimdimid; int dimids_var[1], var_type; size_t dim_len; char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1]; unsigned char uchar_out[DIM1_LEN] = {0, 128, 255}; /* Open and close empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; /* Recreate it again. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Then define a new variable. Since it's * netcdf-4, nc_enddef isn't required - it's called * automatically. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR; if (nc_redef(ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Open three copies of the same file. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid2)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid3)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq(ncid2, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq(ncid3, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; if (nc_close(ncid2)) ERR; if (nc_close(ncid3)) ERR; /* Open and close empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; /* Create a file with one dimension and nothing else. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_enddef(ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_close(ncid)) ERR; /* Create a simple file, and write some data to it. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_uchar(ncid, varid, uchar_out) != NC_ERANGE) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_BYTE || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_close(ncid)) ERR; /* Recreate the file. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_uchar(ncid, varid, uchar_out)) ERR; if (nc_close(ncid)) ERR; /* Recreate it, then make sure NOCLOBBER works. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; if (nc_create(FILE_NAME, NC_NETCDF4|NC_NOCLOBBER, &ncid) != NC_EEXIST) ERR; /* Recreate it again. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Then define a new variable. Since it's * netcdf-4, nc_enddef isn't required - it's called * automatically. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR; if (nc_redef(ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Recreate it again with netcdf-3 rules turned on. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Check that netcdf-3 rules are in effect. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid) != NC_ENOTINDEFINE) ERR; if (nc_close(ncid)) ERR; /* Check some other stuff about it. Closing and reopening the * file forces HDF5 to tell us if we forgot to free some HDF5 * resource associated with the file. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing more complex opens and creates..."); { int ncid, varid, dimids[2]; int ndims, nvars, natts, unlimdimid; int dimids_var[2], var_type; size_t dim_len; char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1]; float float_in, float_out = 99.99; int int_in, int_out = -9999; /* Create a file, this time with attributes. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT1_NAME, NC_FLOAT, 1, &float_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT2_NAME, NC_INT, 1, &int_out)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 2 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; /* Reopen it and check each dim, var, and att. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_dim(ncid, 1, dim_name, &dim_len)) ERR; if (dim_len != DIM2_LEN || strcmp(dim_name, DIM2_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 2 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_inq_var(ncid, 1, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 2 || strcmp(var_name, VAR2_NAME) || var_type != NC_UINT || dimids_var[1] != dimids[1] || natts != 0) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT1_NAME, &float_in)) ERR; if (float_in != float_out) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT2_NAME, &int_in)) ERR; if (int_in != int_out) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing redef for netCDF classic..."); test_redef(NC_FORMAT_CLASSIC); SUMMARIZE_ERR; printf("*** testing redef for netCDF 64-bit offset..."); test_redef(NC_FORMAT_64BIT); SUMMARIZE_ERR; printf("*** testing redef for netCDF-4 ..."); test_redef(NC_FORMAT_NETCDF4); SUMMARIZE_ERR; printf("*** testing redef for netCDF-4, with strict netCDF-3 rules..."); test_redef(NC_FORMAT_NETCDF4_CLASSIC); SUMMARIZE_ERR; printf("*** testing different formats..."); { int ncid; int format; /* Create a netcdf-3 file. */ if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_CLASSIC) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-3 64-bit offset file. */ if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_64BIT) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-4 file. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_NETCDF4) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing CLASSIC_MODEL flag with classic formats..."); { int ncid; int format; /* Create a netcdf-3 file. */ if (nc_create(FILE_NAME, NC_CLOBBER|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_CLASSIC) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-3 64-bit offset file. */ if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLOBBER|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_64BIT) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing multiple open files..."); { #define VAR_NAME "Captain_Kirk" #define NDIMS 1 #define NUM_FILES 30 #define TEXT_LEN 15 #define D1_NAME "tl" int ncid[NUM_FILES], varid; int dimid; size_t count[NDIMS], index[NDIMS] = {0}; const char ttext[TEXT_LEN + 1] = "20051224.150000"; char ttext_in[TEXT_LEN + 1]; char file_name[NC_MAX_NAME + 1]; size_t chunks[NDIMS] = {TEXT_LEN + 1}; int f; /* Create a bunch of files. */ for (f = 0; f < NUM_FILES; f++) { sprintf(file_name, "tst_files2_%d.nc", f); if (nc_create(file_name, NC_NETCDF4, &ncid[f])) ERR; if (nc_def_dim(ncid[f], D1_NAME, TEXT_LEN + 1, &dimid)) ERR; if (nc_def_var(ncid[f], VAR_NAME, NC_CHAR, NDIMS, &dimid, &varid)) ERR; if (f % 2 == 0) if (nc_def_var_chunking(ncid[f], varid, 0, chunks)) ERR; /* Write one time to the coordinate variable. */ count[0] = TEXT_LEN + 1; if (nc_put_vara_text(ncid[f], varid, index, count, ttext)) ERR; } /* Read something from each file. */ for (f = 0; f < NUM_FILES; f++) { if (nc_get_vara_text(ncid[f], varid, index, count, (char *)ttext_in)) ERR; if (strcmp(ttext_in, ttext)) ERR; } /* Close all open files. */ for (f = 0; f < NUM_FILES; f++) if (nc_close(ncid[f])) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }