NcBool NcAtt::is_valid( void ) const { int num; return the_file->is_valid() && (the_variable->id() == NC_GLOBAL || the_variable->is_valid()) && NcError::set_err( nc_inq_attid(the_file->id(), the_variable->id(), the_name, &num) ) == NC_NOERR; }
/* Given an ncid, check the file to make sure it has all the objects I * expect. */ int check_classic_file(int ncid) { int varid, dimid, attnum; size_t att_len; nc_type att_type; char name_in[sizeof(name_utf8) + 1], strings_in[sizeof(name_utf8) + 1]; /* Check the variable. */ if (nc_inq_varid(ncid, name_utf8, &varid)) ERR; if (nc_inq_varname(ncid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_varid(ncid, norm_utf8, &varid)) ERR; name_in[0] = 0; if (nc_inq_varname(ncid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_get_var_text(ncid, varid, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; strings_in[0] = '\0'; /* Reset my string buffer. */ /* Check the dimension. */ if (nc_inq_dimid(ncid, name_utf8, &dimid)) ERR; if (nc_inq_dimname(ncid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_dimid(ncid, norm_utf8, &dimid)) ERR; if (nc_inq_dimname(ncid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; /* Check the attribute. We don't normalize data or attribute * values, so get exactly what was put for the value, but * normalized values for names. */ if (nc_inq_attid(ncid, varid, norm_utf8, &attnum)) ERR; if (attnum) ERR; attnum = 99; /* Reset. */ if (nc_inq_attid(ncid, varid, name_utf8, &attnum)) ERR; if (attnum) ERR; if (nc_inq_att(ncid, varid, norm_utf8, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != sizeof(name_utf8)) ERR; if (nc_get_att_text(ncid, varid, norm_utf8, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; return NC_NOERR; }
void FileNetcdf::prependGlobalAttribute(std::string iName, std::string iValue) { int id; int status = nc_inq_attid(mFile, NC_GLOBAL, iName.c_str(), &id); if(status == NC_NOERR) { char value[10000]; int status = nc_get_att_text(mFile, NC_GLOBAL, iName.c_str(), value); handleNetcdfError(status, "could not get attribute when prepending a new value"); std::stringstream ss; ss << iValue << "\n" << value; setGlobalAttribute(iName, ss.str()); } else { setGlobalAttribute(iName, iValue); } }
NCstate NCdataCopyAttributes(int inNCid, int inVarid, int outNCid, int outVarid, bool overwrite) { bool redef; int status, att = 0, attId; char attName[NC_MAX_NAME]; redef = nc_redef(outNCid) == NC_NOERR; while ((status = nc_inq_attname(inNCid, inVarid, att++, attName)) == NC_NOERR) { if (overwrite || (nc_inq_attid(outNCid, outVarid, attName, &attId) != NC_NOERR)) { if ((status = nc_copy_att(inNCid, inVarid, attName, outNCid, outVarid)) != NC_NOERR) { NCprintNCError (status, "_NCdataCopyAttributes"); return (NCfailed); } } } if (redef) nc_enddef(outNCid); return (NCsucceeded); }
int main(int argc, char* argv[]) { int c; int ncid; // id of netcdf file int dimid; // dimension id int varid; // variable id int attid; // attribute id int ndims; // number of dimensions int verbose=0; // level of verbosity char name[NC_MAX_NAME+1]; if(argc<2) { usage(argv[0]); return 1; } // parse command-line arguments while ((c = getopt(argc, argv, "dh")) != -1) switch (c) { case 'd': verbose++; break; case 'h': default: usage(argv[0]); return 1; } __NC_ASRT__(nc_open(argv[optind],NC_NOWRITE,&ncid)); __NC_ASRT__(nc_inq_ndims(ncid,&ndims)); if (verbose) printf("Found %d dimensions\n",ndims); for (dimid=0; dimid<ndims; dimid++) { __NC_ASRT__(nc_inq_dimname(ncid,dimid,name)); if (verbose) printf("Examining \"%s\"\n",name); if (nc_inq_varid(ncid,name,&varid)==NC_NOERR && nc_inq_attid(ncid,varid,"compress",&attid)==NC_NOERR) { return 0; } } __NC_ASRT__(nc_close(ncid)); return -1; }
int main(int argc, char **argv) { printf("\n*** Testing 'Fileinfo attributes.\n"); { hid_t fileid; hid_t fcplid; hid_t scalar_spaceid; printf("*** creating test file using HDF5 directly %s...", HDFFILE); /* Create scalar dataspace */ if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR; /* Set creation ordering for file, so we can revise its contents later */ if((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; if(H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR; if(H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR; /* Create new file, using default properties */ if((fileid = H5Fcreate(HDFFILE, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR; /* Close file creation property list */ if(H5Pclose(fcplid) < 0) ERR; /* Add attributes to root group */ { hid_t scalar_spaceid = -1; hid_t attid = -1; /* Create scalar dataspace */ if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR; /* Create attribute with native integer datatype on object */ if((attid = H5Acreate2(fileid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if(H5Aclose(attid) < 0) ERR; /* Clean up objects created */ if(H5Sclose(scalar_spaceid) < 0) ERR; } /* Close rest */ if(H5Sclose(scalar_spaceid) < 0) ERR; if(H5Fclose(fileid) < 0) ERR; } { int root, grpid, varid, stat, natts, id; int data = 17; const char* sdata = "text"; char ncprops[8192]; size_t len; int dimid; nc_type xtype; char name[NC_MAX_NAME]; printf("\n*** creating netcdf-4 test file using netCDF %s...", NC4FILE); if(nc_create(NC4FILE,NC_WRITE|NC_CLOBBER|NC_NETCDF4,&root)!=0) ERR; /* Create global attribute */ if(nc_put_att_int(root,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR; /* Create global variable */ if(nc_def_var(root,INT_VAR_NAME,NC_INT,0,NULL,&varid)!=0) ERR; /* Create attribute on var */ if(nc_put_att_int(root,varid,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR; /* Create global subgroup */ if(nc_def_grp(root,GROUPNAME,&grpid)!=0) ERR; /* Create global attribute in the group */ if(nc_put_att_int(grpid,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR; /* Create _NCProperties as var attr and as subgroup attribute */ if(nc_put_att_text(grpid,NC_GLOBAL,NCPROPS,strlen(sdata),sdata)!=0) ERR; if(nc_put_att_text(root,varid,NCPROPS,strlen(sdata),sdata)!=0) ERR; /* Create var + dimension to cause e.g. dimscales to appear */ if(nc_def_dim(root,DIMNAME,(size_t)4,&dimid)!=0) ERR; if(nc_def_var(root,DIMNAME,NC_INT,1,&dimid,&varid)!=0) ERR; /* same name */ /* Close, then re-open */ if(nc_close(root)) ERR; if(nc_open(NC4FILE,NC_WRITE|NC_NETCDF4,&root)!=0) ERR; /* Is all invisible attributes actually invisible vis-a-vis nc_inq? */ if(nc_inq(root,NULL,NULL,&natts,NULL)!=0) ERR; if(natts != 1) ERR; /* Now, fiddle with the NCPROPS attribute */ /* Get its metadata */ if(nc_inq_att(root,NC_GLOBAL,NCPROPS,&xtype,&len)!=0) ERR; if(xtype != NC_CHAR) ERR; /* Read in two ways */ if(nc_get_att_text(root,NC_GLOBAL,NCPROPS,ncprops)!=0) ERR; if(strlen(ncprops) != len) ERR; /* Attempt to get attribute metadata piecemeal; some will fail */ id = -1; stat = nc_inq_attid(root,NC_GLOBAL,NCPROPS,&id); if(stat == NC_NOERR) ERR; stat = nc_inq_attname(root,NC_GLOBAL,id,name); if(stat == NC_NOERR) ERR; if(nc_inq_atttype(root,NC_GLOBAL,NCPROPS,&xtype)!=0) ERR; if(xtype != NC_CHAR) ERR; if(nc_inq_attlen(root,NC_GLOBAL,NCPROPS,&len)!=0) ERR; if(len != strlen(ncprops)) ERR; /*Overwrite _NCProperties root attribute; should fail */ stat = nc_put_att_text(root,NC_GLOBAL,NCPROPS,strlen(sdata),sdata); if(stat == NC_NOERR) ERR; /* Delete; should fail */ stat = nc_del_att(root,NC_GLOBAL,NCPROPS); if(stat != NC_ENOTATT) ERR; /* Ditto _SuperblockVersion */ /* Get its metadata */ if(nc_inq_att(root,NC_GLOBAL,SUPERBLOCKATT,&xtype,&len)!=0) ERR; if(xtype != NC_INT) ERR; if(len != 1) ERR; if(nc_get_att_int(root,NC_GLOBAL,SUPERBLOCKATT,&data)!=0) ERR; /* Attempt to get attribute metadata piecemeal */ stat = nc_inq_attid(root,NC_GLOBAL,SUPERBLOCKATT,&id); if(stat == NC_NOERR) ERR; stat = nc_inq_attname(root,NC_GLOBAL,id,name); if(stat == NC_NOERR) ERR; if(nc_inq_atttype(root,NC_GLOBAL,SUPERBLOCKATT,&xtype)!=0) ERR; if(xtype != NC_INT) ERR; if(nc_inq_attlen(root,NC_GLOBAL,SUPERBLOCKATT,&len)!=0) ERR; if(len != 1) ERR; /*Overwrite; should fail */ stat = nc_put_att_int(root,NC_GLOBAL,NCPROPS,NC_INT,1,&data); if(stat == NC_NOERR) ERR; /* Delete; should fail */ stat = nc_del_att(root,NC_GLOBAL,SUPERBLOCKATT); if(stat == NC_NOERR) ERR; /* Ditto _IsNetcdf4 */ /* Get its metadata */ if(nc_inq_att(root,NC_GLOBAL,ISNETCDF4ATT,&xtype,&len)!=0) ERR; if(xtype != NC_INT) ERR; if(len != 1) ERR; if(nc_get_att_int(root,NC_GLOBAL,ISNETCDF4ATT,&data)!=0) ERR; /* Attempt to get attribute metadata piecemeal */ stat = nc_inq_attid(root,NC_GLOBAL,ISNETCDF4ATT,&id); if(stat == NC_NOERR) ERR; stat = nc_inq_attname(root,NC_GLOBAL,id,name); if(stat == NC_NOERR) ERR; if(nc_inq_atttype(root,NC_GLOBAL,ISNETCDF4ATT,&xtype)!=0) ERR; if(xtype != NC_INT) ERR; if(nc_inq_attlen(root,NC_GLOBAL,ISNETCDF4ATT,&len)!=0) ERR; if(len != 1) ERR; /*Overwrite; should fail */ stat = nc_put_att_int(root,NC_GLOBAL,ISNETCDF4ATT,NC_INT,1,&data); if(stat == NC_NOERR) ERR; /* Delete; should fail */ stat = nc_del_att(root,NC_GLOBAL,ISNETCDF4ATT); if(stat == NC_NOERR) ERR; if(nc_close(root)!=0) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
/* Given an ncid, check the file to make sure it has all the objects I * expect. */ int check_nc4_file(int ncid) { int varid, dimid, attnum, grpid, grpid2, grpid3, numgrps; int numtypes, enum_typeid, comp_typeid; int class_in; size_t att_len, size_in, num_mem, nfields_in; nc_type att_type, base_type_in; char name_in[NC_MAX_NAME + 1], strings_in[NC_MAX_NAME + 1], value; /* Check the group. */ if (nc_inq_grps(ncid, &numgrps, &grpid)) ERR; if (numgrps != 1) ERR; name_in[0] = 0; if (nc_inq_grpname(grpid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; /* Check the variable. */ if (nc_inq_varid(grpid, name_utf8, &varid)) ERR; if (nc_inq_varname(grpid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_varid(grpid, norm_utf8, &varid)) ERR; name_in[0] = 0; if (nc_inq_varname(grpid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_get_var(grpid, varid, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; strings_in[0] = '\0'; /* Reset my string buffer. */ /* Check the dimension. */ if (nc_inq_dimid(grpid, name_utf8, &dimid)) ERR; if (nc_inq_dimname(grpid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_dimid(grpid, norm_utf8, &dimid)) ERR; if (nc_inq_dimname(grpid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; /* Check the attribute. We don't normalize data or attribute * values, so get exactly what was put for the value, but * normalized values for names. */ if (nc_inq_attid(grpid, varid, norm_utf8, &attnum)) ERR; if (attnum) ERR; attnum = 99; /* Reset. */ if (nc_inq_attid(grpid, varid, name_utf8, &attnum)) ERR; if (attnum) ERR; if (nc_inq_att(grpid, varid, norm_utf8, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != sizeof(name_utf8)) ERR; if (nc_get_att_text(grpid, varid, norm_utf8, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; /* Check the enum type. */ if (nc_inq_grps(grpid, &numgrps, &grpid2)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(grpid2, &numtypes, &enum_typeid)) ERR; if (numtypes != 1) ERR; if (nc_inq_user_type(grpid2, enum_typeid, name_in, &size_in, &base_type_in, &nfields_in, &class_in)) ERR; if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 || base_type_in != NC_BYTE || nfields_in != 1 || class_in != NC_ENUM) ERR; name_in[0] = size_in = base_type_in = 0; if (nc_inq_enum(grpid2, enum_typeid, name_in, &base_type_in, &size_in, &num_mem)) ERR; if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 || base_type_in != NC_BYTE || num_mem != 1) ERR; if (nc_inq_enum_member(grpid2, enum_typeid, 0, name_in, &value)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || value != ENUM_VALUE) ERR; /* Check the compound type. */ if (nc_inq_grps(grpid2, &numgrps, &grpid3)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(grpid3, &numtypes, &comp_typeid)) ERR; if (numtypes != 1) ERR; name_in[0] = 0; if (nc_inq_user_type(grpid3, comp_typeid, name_in, &size_in, &base_type_in, &nfields_in, &class_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) || base_type_in != NC_NAT || nfields_in != 1 || class_in != NC_COMPOUND) ERR; size_in = nfields_in = 999; if (nc_inq_compound(grpid3, comp_typeid, name_in, &size_in, &nfields_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) || nfields_in != 1) ERR; name_in[0] = 0; if (nc_inq_compound_fieldname(grpid3, comp_typeid, 0, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; return NC_NOERR; }
int main(int argc, char **argv) { int ncid, dimids[NUM_DIMS]; int varid; int nvars_in, varids_in[NUM_DIMS]; signed char fill_value = 42, fill_value_in; nc_type xtype_in; size_t len_in; char name_in[NC_MAX_NAME + 1]; int attnum_in; int cnum; #ifdef USE_PARALLEL MPI_Init(&argc, &argv); #endif printf("\n*** Testing netcdf-4 variable functions, even more.\n"); for (cnum = 0; cnum < MAX_CNUM; cnum++) { int cmode; switch(cnum) { case 0: printf("*** Testing with classic format:\n"); cmode = 0; break; case 1: printf("*** Testing with 64-bit offset format:\n"); cmode = NC_64BIT_OFFSET; break; case 2: printf("*** Testing with HDF5:\n"); cmode = NC_NETCDF4|NC_CLOBBER; break; case 3: printf("*** Testing with HDF5, netCDF Classic Model:\n"); cmode = NC_CLASSIC_MODEL | NC_NETCDF4; } printf("**** testing simple fill value attribute creation..."); { /* Create a netcdf-4 file with one scalar var. Add fill * value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, 0, NULL, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, VAR_NAME)) ERR; if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing simple fill value with data read..."); { size_t start[NUM_DIMS], count[NUM_DIMS]; signed char data = 99, data_in; /* Create a netcdf-4 file with one unlimited dim and one * var. Add fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_enddef(ncid)) ERR; /* Write the second record. */ start[0] = 1; count[0] = 1; if (nc_put_vara_schar(ncid, varid, start, count, &data)) ERR; /* Read the first record, it should be the fill value. */ start[0] = 0; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != fill_value) ERR; /* Read the second record, it should be the value we just wrote * there. */ start[0] = 1; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != data) ERR; /* Close up. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check metadata. */ if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, VAR_NAME)) ERR; /* Check fill value att. */ if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; /* Read the first record, it should be the fill value. */ start[0] = 0; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != fill_value) ERR; /* Read the second record, it should be the value we just wrote * there. */ start[0] = 1; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with one other attribute..."); { int losses_value = 192, losses_value_in; /* Create a netcdf-4 file with one dim and one var. Add another * attribute, then fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, LOSSES_NAME, NC_INT, 1, &losses_value)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_att(ncid, 0, LOSSES_NAME, &xtype_in, &len_in)) ERR; if (xtype_in != NC_INT || len_in != 1) ERR; if (nc_get_att(ncid, 0, LOSSES_NAME, &losses_value_in)) ERR; if (losses_value_in != losses_value) ERR; if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_inq_attid(ncid, 0, LOSSES_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with three other attributes..."); { #define NUM_LEADERS 3 char leader[NUM_LEADERS][NC_MAX_NAME + 1] = {"hair_length_of_strategoi", "hair_length_of_Miltiades", "hair_length_of_Darius_I"}; short hair_length[NUM_LEADERS] = {3, 11, 4}; short short_in; int a; /* Create a netcdf file with one dim and one var. Add 3 * attributes, then fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; for (a = 0; a < NUM_LEADERS; a++) if (nc_put_att_short(ncid, varid, leader[a], NC_SHORT, 1, &hair_length[a])) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check our three hair-related attributes. */ for (a = 0; a < NUM_LEADERS; a++) { if (nc_inq_att(ncid, 0, leader[a], &xtype_in, &len_in)) ERR; if (xtype_in != NC_SHORT || len_in != 1) ERR; if (nc_get_att(ncid, 0, leader[a], &short_in)) ERR; if (short_in != hair_length[a]) ERR; if (nc_inq_attid(ncid, 0, leader[a], &attnum_in)) ERR; if (attnum_in != a) ERR; } /* Check our fill value attribute. */ if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with simple example..."); { /* Dims stuff. */ #define NDIMS 3 #define VAR_DIMS 3 #define DIM_A "dim1" #define DIM_A_LEN 4 #define DIM_B "dim2" #define DIM_B_LEN 3 #define DIM_C "dim3" #define DIM_C_LEN NC_UNLIMITED /* Var stuff. */ #define CXX_VAR_NAME "P" /* Att stuff. */ #define NUM_ATTS 4 #define LONG_NAME "long_name" #define PRES_MAX_WIND "pressure at maximum wind" #define UNITS "units" #define HECTOPASCALS "hectopascals" int dimid[NDIMS], var_dimids[VAR_DIMS] = {2, 1, 0}; float fill_value = -9999.0f; char long_name[] = PRES_MAX_WIND; if (nc_create(FILE_NAME, cmode, &ncid)) ERR; /* Create dims. */ if (nc_def_dim(ncid, DIM_A, DIM_A_LEN, &dimid[0])) ERR; if (nc_def_dim (ncid, DIM_B, DIM_B_LEN, &dimid[1])) ERR; if (nc_def_dim(ncid, DIM_C, DIM_C_LEN, &dimid[2])) ERR; /* Create var. */ if (nc_def_var(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids, &varid)) ERR; if (varid) ERR; if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1, long_name)) ERR; if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1, UNITS)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; /* Now add a fill value. This will acutually cause HDF5 to * destroy the dataset and recreate it, recreating also the * three attributes that are attached to it. */ if (nc_put_att(ncid, varid, _FillValue, NC_FLOAT, 1, &fill_value)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 2) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #ifndef NO_NETCDF_2 /* The following test is an attempt to recreate a problem occuring in the cxx tests. The file is created in c++ in nctsts.cpp. */ printf("**** testing fill value with example from cxx tests in v2 api..."); { /* Dims stuff. */ #define NDIMS_1 4 #define VAR_DIMS 3 #define LAT "lat" #define LAT_LEN 4 #define LON "lon" #define LON_LEN 3 #define FRTIMED "frtimed" #define FRTIMED_LEN NC_UNLIMITED #define TIMELEN "timelen" #define TIMELEN_LEN 20 /* Var stuff. */ #define CXX_VAR_NAME "P" /* Att stuff. */ #define NUM_ATTS 4 #define LONG_NAME "long_name" #define UNITS "units" int dimid[NDIMS_1], var_dimids[VAR_DIMS] = {2, 0, 1}; float fill_value = -9999.0f; char long_name[] = PRES_MAX_WIND; int i, attid[NUM_ATTS]; ncid = nccreate(FILE_NAME, NC_NETCDF4); /* Create dims. */ dimid[0] = ncdimdef(ncid, LAT, LAT_LEN); dimid[1] = ncdimdef(ncid, LON, LON_LEN); dimid[2] = ncdimdef(ncid, FRTIMED, FRTIMED_LEN); dimid[3] = ncdimdef(ncid, TIMELEN, TIMELEN_LEN); /* Just check our dimids to see that they are correct. */ for (i = 0; i < NDIMS_1; i++) if (dimid[i] != i) ERR; /* Create var. */ varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids); if (varid) ERR; /* Add three atts to the var, long_name, units, and * valid_range. */ if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1, long_name)) ERR; if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1, UNITS)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; /* Now add a fill value. This will acutually cause HDF5 to * destroy the dataset and recreate it, recreating also the * three attributes that are attached to it. */ attid[3] = ncattput(ncid, varid, _FillValue, NC_FLOAT, 1, &fill_value); /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; ncclose(ncid); /* Open the file and check. */ ncid = ncopen(FILE_NAME, 0); if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 2) ERR; ncclose(ncid); } SUMMARIZE_ERR; #endif /* NO_NETCDF_2 */ } printf("**** testing create order varids..."); #define UNITS "units" #define DIMNAME "x" #define VARNAME "data" { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, dimid, varid, xvarid; char units[] = "zlotys"; if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIMNAME, 1, &dimid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_def_var(ncid, DIMNAME, NC_INT, 1, &dimid, &xvarid)) ERR; if (nc_put_att_text(ncid, xvarid, UNITS, strlen(units), units)) ERR; if (nc_def_var(ncid, VARNAME, NC_INT, 1, &dimid, &varid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #define RANK_wind 1 printf("**** testing simple variable renaming..."); { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, lat_dim, time_dim, lon_dim, wind_id; size_t lat_len = 73, time_len = 10, lon_len = 145; int cdf_goober[1]; /* if (nc_set_default_format(NC_FORMAT_NETCDF4, NULL)) ERR;*/ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "a", lon_len, &lon_dim)) ERR; if (nc_def_dim(ncid, "b", lat_len, &lat_dim)) ERR; if (nc_def_dim(ncid, "c", time_len, &time_dim)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, "a", 3, "bar")) ERR; cdf_goober[0] = 2; if (nc_put_att_int(ncid, NC_GLOBAL, "b", NC_INT, 1, cdf_goober)) ERR; /* define variables */ if (nc_def_var(ncid, "aa", NC_FLOAT, RANK_wind, &lon_dim, &wind_id)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_rename_var(ncid, 0, "az")) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing dimension and variable renaming..."); { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, lat_dim, time_dim, lon_dim, wind_id; size_t lat_len = 73, time_len = 10, lon_len = 145; int wind_dims[RANK_wind], wind_slobber[1], cdf_goober[1]; if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "lon", lon_len, &lon_dim)) ERR; if (nc_def_dim(ncid, "lat", lat_len, &lat_dim)) ERR; if (nc_def_dim(ncid, "time", time_len, &time_dim)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, "foo", 3, "bar")) ERR; cdf_goober[0] = 2; if (nc_put_att_int(ncid, NC_GLOBAL, "goober", NC_INT, 1, cdf_goober)) ERR; /* define variables */ wind_dims[0] = lon_dim; if (nc_def_var(ncid, "temp", NC_FLOAT, RANK_wind, wind_dims, &wind_id)) ERR; if (nc_put_att_text(ncid, wind_id, "bar", 3, "foo")) ERR; wind_slobber[0] = 3; if (nc_put_att_int(ncid, wind_id, "slobber", NC_INT, 1, wind_slobber)) ERR; if (nc_close(ncid)) ERR; /* re-open dataset*/ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_dimid(ncid, "lon", &lon_dim)) ERR; /* rename dimension */ if (nc_rename_dim(ncid, lon_dim, "longitude")) ERR; if (nc_inq_varid(ncid, "temp", &wind_id)) ERR; /* rename variable */ if (nc_rename_var(ncid, wind_id, "wind")) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; /* printf("*** testing 2D array of NC_CHAR..."); */ /* { */ /* int dimid[NDIMS_1], var_dimids[VAR_DIMS] = {2, 0, 1}; */ /* float fill_value = -9999.0f; */ /* char long_name[] = PRES_MAX_WIND; */ /* int i, attid[NUM_ATTS]; */ /* ncid = nccreate(FILE_NAME, NC_NETCDF4); */ /* /\* Create dims. *\/ */ /* dimid[0] = ncdimdef(ncid, LAT, LAT_LEN); */ /* dimid[1] = ncdimdef(ncid, LON, LON_LEN); */ /* /\* Create var. *\/ */ /* varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids); */ /* if (varid) ERR; */ /* ncclose(ncid); */ /* /\* Open the file and check. *\/ */ /* ncid = ncopen(FILE_NAME, 0); */ /* ncclose(ncid); */ /* } */ /* SUMMARIZE_ERR; */ #define NDIMS 3 #define NNAMES 4 #define NLINES 13 /* printf("**** testing funny names for netCDF-4..."); */ /* { */ /* int ncid, wind_id; */ /* size_t len[NDIMS] = {7, 3, 1}; */ /* int dimids[NDIMS], dimids_in[NDIMS], ndims_in; */ /* char funny_name[NNAMES][NC_MAX_NAME] = {"\a\t", "\f\n", "\r\v", "\b"}; */ /* char name_in[NC_MAX_NAME + 1]; */ /* char *speech[NLINES] = {"who would fardels bear, ", */ /* "To grunt and sweat under a weary life, ", */ /* "But that the dread of something after death, ", */ /* "The undiscover'd country from whose bourn ", */ /* "No traveller returns, puzzles the will ", */ /* "And makes us rather bear those ills we have ", */ /* "Than fly to others that we know not of? ", */ /* "Thus conscience does make cowards of us all; ", */ /* "And thus the native hue of resolution ", */ /* "Is sicklied o'er with the pale cast of thought, ", */ /* "And enterprises of great pith and moment ", */ /* "With this regard their currents turn awry, ", */ /* "And lose the name of action."}; */ /* char *speech_in[NLINES]; */ /* int i; */ /* unsigned short nlines = NLINES; */ /* unsigned int nlines_in; */ /* if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */ /* /\* Define dimensions. *\/ */ /* for (i = 0; i < NDIMS; i++) */ /* if (nc_def_dim(ncid, funny_name[i], len[i], &dimids[i])) ERR; */ /* /\* Write some global atts. *\/ */ /* if (nc_put_att_string(ncid, NC_GLOBAL, funny_name[0], NLINES, */ /* (const char **)speech)) ERR; */ /* if (nc_put_att_ushort(ncid, NC_GLOBAL, funny_name[1], NC_UINT, 1, &nlines)) ERR; */ /* /\* Define variables. *\/ */ /* if (nc_def_var(ncid, funny_name[3], NC_INT64, NDIMS, dimids, &wind_id)) ERR; */ /* if (nc_close(ncid)) ERR; */ /* /\* Open the file and check. *\/ */ /* if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; */ /* if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; */ /* if (ndims_in != NDIMS) ERR; */ /* for (i = 0; i < NDIMS; i++) */ /* { */ /* if (dimids_in[i] != i) ERR; */ /* if (nc_inq_dimname(ncid, i, name_in)) ERR; */ /* if (strcmp(name_in, funny_name[i])) ERR; */ /* } */ /* if (nc_get_att_string(ncid, NC_GLOBAL, funny_name[0], (char **)speech_in)) ERR; */ /* for (i = 0; i < NLINES; i++) */ /* if (strcmp(speech_in[i], speech[i])) ERR; */ /* if (nc_get_att_uint(ncid, NC_GLOBAL, funny_name[1], &nlines_in)) ERR; */ /* if (nlines_in != NLINES) ERR; */ /* if (nc_free_string(NLINES, (char **)speech_in)) ERR; */ /* if (nc_inq_varname(ncid, 0, name_in)) ERR; */ /* if (strcmp(name_in, funny_name[3])) ERR; */ /* if (nc_close(ncid)) ERR; */ /* } */ /* SUMMARIZE_ERR; */ printf("**** testing endianness..."); #define NDIMS4 1 #define DIM4_NAME "Joe" #define VAR_NAME4 "Ed" #define DIM4_LEN 10 { int dimids[NDIMS4], dimids_in[NDIMS4]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM4_LEN], data_in[DIM4_LEN]; int endian_in; int i; for (i = 0; i < DIM4_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM4_NAME, DIM4_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME4, NC_INT, NDIMS4, dimids, &varid)) ERR; if (nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG)) ERR; if (varid != 0) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS4 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR; if (endian_in != NC_ENDIAN_BIG) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS4 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR; if (endian_in != NC_ENDIAN_BIG) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM4_LEN; i++) if (data[i] != data_in[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing chunking..."); { #define NDIMS5 1 #define DIM5_NAME "D5" #define VAR_NAME5 "V5" #define DIM5_LEN 1000 int dimids[NDIMS5], dimids_in[NDIMS5]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM5_LEN], data_in[DIM5_LEN]; int chunksize[NDIMS5] = {5}; int chunksize_in[NDIMS5]; int contiguous_in; int i, d; for (i = 0; i < DIM5_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM5_NAME, DIM5_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME5, NC_INT, NDIMS5, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 0, chunksize)) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS5 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS5; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM5_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS5 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS5; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM5_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing contiguous storage..."); { #define NDIMS6 1 #define DIM6_NAME "D5" #define VAR_NAME6 "V5" #define DIM6_LEN 100 int dimids[NDIMS6], dimids_in[NDIMS6]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM6_LEN], data_in[DIM6_LEN]; int chunksize_in[NDIMS6]; int contiguous_in; int i, d; for (i = 0; i < DIM6_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM6_NAME, DIM6_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME6, NC_INT, NDIMS6, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 1, NULL)) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS6 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS6; d++) if (chunksize_in[d] != 0) ERR; if (!contiguous_in) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM6_LEN; i++) if (data_in[i] != data[i]) ERR_RET; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS6 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS6; d++) if (chunksize_in[d] != 0) ERR; if (!contiguous_in) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM6_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing extreme numbers dude..."); { #define VAR_NAME7 "V5" #define DIM6_LEN 100 int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; /* unsigned long long data = 9223372036854775807ull, data_in;*/ unsigned long long data = 9223372036854775817ull, data_in; /* Create a netcdf-4 file with scalar var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME7, NC_UINT64, 0, NULL, &varid)) ERR; if (nc_put_var_ulonglong(ncid, varid, &data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR; if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR; if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR; if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR; if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, GENERIC_NAME)) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes some more..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes even more..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one group. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, GENERIC_NAME, NULL)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error code for too-large chunks..."); { #define NDIMS17 2 #define DIM17_NAME "personality" #define DIM17_NAME_2 "good_looks" #define VAR_NAME17 "ed" #define DIM17_LEN 2147483644 /* max dimension size - 2GB - 4. */ #define DIM17_2_LEN 1000 int dimids[NDIMS17], dimids_in[NDIMS17]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int chunksize[NDIMS17] = {5, 5}; int bad_chunksize[NDIMS17] = {5, DIM17_LEN}; int chunksize_in[NDIMS17]; int contiguous_in; int d; /* Create a netcdf-4 file with two dims and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM17_NAME, DIM17_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM17_NAME_2, DIM17_2_LEN, &dimids[1])) ERR; if (dimids[0] != 0 || dimids[1] != 1) ERR; if (nc_def_var(ncid, VAR_NAME17, NC_UINT64, NDIMS17, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 0, bad_chunksize) != NC_EBADCHUNK) ERR; if (nc_def_var_chunking(ncid, varid, 0, chunksize)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS17 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS17; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS17 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS17; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; #ifdef USE_PARALLEL MPI_Finalize(); #endif }
int check_attrs(int ncid, int obj) { int attid; int natts = 0; size_t len; nc_type type; char *vlstr; char fixstr[10]; int x; /* Check the object's attributes are OK */ if (nc_inq_varnatts(ncid, obj, &natts )) ERR_GOTO; if (natts != 6) ERR_GOTO; if (nc_inq_attid(ncid, obj, VSTR_ATT1_NAME, &attid)) ERR_GOTO; if (attid != 0) ERR_GOTO; if (nc_inq_atttype(ncid, obj, VSTR_ATT1_NAME, &type)) ERR_GOTO; if (type != NC_STRING) ERR_GOTO; if (nc_inq_attlen(ncid, obj, VSTR_ATT1_NAME, &len)) ERR_GOTO; if (len != 1) ERR_GOTO; vlstr = NULL; if (nc_get_att(ncid, obj, VSTR_ATT1_NAME, &vlstr)) ERR_GOTO; if (NULL != vlstr) ERR_GOTO; if (nc_inq_attid(ncid, obj, VSTR_ATT2_NAME, &attid)) ERR_GOTO; if (attid != 1) ERR_GOTO; if (nc_inq_atttype(ncid, obj, VSTR_ATT2_NAME, &type)) ERR_GOTO; if (type != NC_STRING) ERR_GOTO; if (nc_inq_attlen(ncid, obj, VSTR_ATT2_NAME, &len)) ERR_GOTO; if (len != 1) ERR_GOTO; vlstr = NULL; if (nc_get_att(ncid, obj, VSTR_ATT2_NAME, &vlstr)) ERR_GOTO; if (NULL != vlstr) ERR_GOTO; if (nc_inq_attid(ncid, obj, VSTR_ATT3_NAME, &attid)) ERR_GOTO; if (attid != 2) ERR_GOTO; if (nc_inq_atttype(ncid, obj, VSTR_ATT3_NAME, &type)) ERR_GOTO; if (type != NC_STRING) ERR_GOTO; if (nc_inq_attlen(ncid, obj, VSTR_ATT3_NAME, &len)) ERR_GOTO; if (len != 1) ERR_GOTO; vlstr = NULL; if (nc_get_att(ncid, obj, VSTR_ATT3_NAME, &vlstr)) ERR_GOTO; if (strcmp(vlstr, "")) ERR_GOTO; free(vlstr); if (nc_inq_attid(ncid, obj, VSTR_ATT4_NAME, &attid)) ERR_GOTO; if (attid != 3) ERR_GOTO; if (nc_inq_atttype(ncid, obj, VSTR_ATT4_NAME, &type)) ERR_GOTO; if (type != NC_STRING) ERR_GOTO; if (nc_inq_attlen(ncid, obj, VSTR_ATT4_NAME, &len)) ERR_GOTO; if (len != 1) ERR_GOTO; vlstr = NULL; if (nc_get_att(ncid, obj, VSTR_ATT4_NAME, &vlstr)) ERR_GOTO; if (strcmp(vlstr, "foo")) ERR_GOTO; free(vlstr); if (nc_inq_attid(ncid, obj, FSTR_ATT_NAME, &attid)) ERR_GOTO; if (attid != 4) ERR_GOTO; if (nc_inq_atttype(ncid, obj, FSTR_ATT_NAME, &type)) ERR_GOTO; if (type != NC_CHAR) ERR_GOTO; if (nc_inq_attlen(ncid, obj, FSTR_ATT_NAME, &len)) ERR_GOTO; if (len != 10) ERR_GOTO; memset(fixstr, 1, sizeof(fixstr)); if (nc_get_att(ncid, obj, FSTR_ATT_NAME, fixstr)) ERR_GOTO; if ('\0' != fixstr[0]) ERR_GOTO; if (nc_inq_attid(ncid, obj, INT_ATT_NAME, &attid)) ERR_GOTO; if (attid != 5) ERR_GOTO; if (nc_inq_atttype(ncid, obj, INT_ATT_NAME, &type)) ERR_GOTO; if (type != NC_INT) ERR_GOTO; if (nc_inq_attlen(ncid, obj, INT_ATT_NAME, &len)) ERR_GOTO; if (len != 1) ERR_GOTO; x = -1; if (nc_get_att(ncid, obj, INT_ATT_NAME, &x)) ERR_GOTO; if (0 != x) ERR_GOTO; return(0); error: return(-1); }
int main(int argc, char **argv) { (void) signal(SIGFPE, SIG_IGN); printf("\n*** Testing netcdf-4 attribute functions.\n"); printf("*** testing really simple global atts..."); #define NUM_SIMPLE_ATTS 9 { int ncid; char name[NUM_SIMPLE_ATTS][ATT_MAX_NAME + 1] = {"Gc", "Gb", "Gs", "Gi", "Gf", "Gd", "G7", "G8", "G9"}; char name_in[NC_MAX_NAME]; int j; /* Create a file with some global atts. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; for (j = 0; j < NUM_SIMPLE_ATTS; j++) if (nc_put_att_int(ncid, NC_GLOBAL, name[j], NC_INT, 0, NULL)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check the order. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; for (j = 0; j < NUM_SIMPLE_ATTS; j++) { if (nc_inq_attname(ncid, NC_GLOBAL, j, name_in)) ERR; if (strcmp(name_in, name[j])) ERR; } /* Close up shop. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing simple global atts..."); { int ncid; nc_type att_type; size_t att_len; int i; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; unsigned char uchar_in[ATT_LEN], uchar_out[ATT_LEN] = {0, 128, NC_MAX_CHAR}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN], ushort_out[ATT_LEN] = {0, 128, NC_MAX_USHORT}; unsigned int uint_in[ATT_LEN], uint_out[ATT_LEN] = {0, 128, NC_MAX_UINT}; unsigned long long uint64_in[ATT_LEN], uint64_out[ATT_LEN] = {0, 128, 18446744073709551612ULL}; long long int64_in[ATT_LEN], int64_out[ATT_LEN] = {NC_MIN_INT64, 128, NC_MAX_INT64}; /* This won't work, because classic files can't create these types. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out) != NC_ESTRICTNC3) ERR; if (nc_close(ncid)) ERR; /* Create a file with a global attribute of each type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UBYTE, ATT_LEN, uchar_out)) ERR; if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, ATT_LEN, short_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, ATT_LEN, int_out)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, ATT_LEN, float_out)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, ATT_LEN, double_out)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out)) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out)) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out)) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; /* Check text. */ if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); /* Check numeric values. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, uchar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uchar_in[i] != uchar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, ushort_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (ushort_in[i] != ushort_out[i]) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, uint_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint_in[i] != uint_out[i]) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, int64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int64_in[i] != int64_out[i]) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, uint64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint64_in[i] != uint64_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute data type conversions..."); { int ncid; int i; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN]; unsigned int uint_in[ATT_LEN]; unsigned long long uint64_in[ATT_LEN]; long long int64_in[ATT_LEN]; /* Reopen the file and try different type conversions. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; /* No text conversions are allowed, and people who try them shold * be locked up, away from decent folk! */ if (nc_get_att_short(ncid, NC_GLOBAL, ATT_TEXT_NAME, short_in) != NC_ECHAR) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_TEXT_NAME, int_in) != NC_ECHAR) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_TEXT_NAME, float_in) != NC_ECHAR) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_TEXT_NAME, double_in) != NC_ECHAR) ERR; /* if (nc_get_att_ubyte(ncid, NC_GLOBAL, ATT_TEXT_NAME, uchar_in) != NC_ECHAR) ERR;*/ if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_TEXT_NAME, ushort_in) != NC_ECHAR) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_TEXT_NAME, uint_in) != NC_ECHAR) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_TEXT_NAME, int64_in) != NC_ECHAR) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_TEXT_NAME, uint64_in) != NC_ECHAR) ERR; /* Read all atts (except text) as double. */ if (nc_get_att_double(ncid, NC_GLOBAL, ATT_SCHAR_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != schar_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_SHORT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != short_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_INT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != int_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_FLOAT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != float_out[i]) ERR; /* Read all atts (except text) as float. */ if (nc_get_att_float(ncid, NC_GLOBAL, ATT_SCHAR_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != schar_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_SHORT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != short_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_INT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != (float)int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != (float)double_out[i]) ERR; /* Read all atts (except text) as int. */ if (nc_get_att_int(ncid, NC_GLOBAL, ATT_SCHAR_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != schar_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_SHORT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_FLOAT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != (int)float_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != (int)double_out[i]) ERR; /* Read all atts (except text) as short. */ if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SCHAR_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != schar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_INT_NAME, short_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)int_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_FLOAT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)float_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)double_out[i]) ERR; /* Read all atts (except text) as schar. Some range errors will * result converting to schar. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)short_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)int_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)float_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)double_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing simple variable atts..."); { int ncid, varid, dimids[2]; nc_type att_type; size_t att_len; int i, v; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; /* Create a file with two vars, attaching to each an attribute of * each type. */ 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, 2, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, varid, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_short(ncid, varid, ATT_SHORT_NAME, NC_SHORT, 3, short_out)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_put_att_float(ncid, varid, ATT_FLOAT_NAME, NC_FLOAT, 3, float_out)) ERR; if (nc_put_att_double(ncid, varid, ATT_DOUBLE_NAME, NC_DOUBLE, 3, double_out)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, varid, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_short(ncid, varid, ATT_SHORT_NAME, NC_SHORT, 3, short_out)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_put_att_float(ncid, varid, ATT_FLOAT_NAME, NC_FLOAT, 3, float_out)) ERR; if (nc_put_att_double(ncid, varid, ATT_DOUBLE_NAME, NC_DOUBLE, 3, double_out)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; for (v=0; v<2; v++) { if (nc_inq_att(ncid, v, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, v, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); if (nc_get_att_schar(ncid, v, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_short(ncid, v, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, v, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, v, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, v, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; } if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing zero-length attributes..."); { int ncid; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ /* Create a file with a global attribute of each type of zero length. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, 0, NULL)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, 0, NULL)) ERR; /* if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UCHAR, ATT_LEN, uchar_out)) ERR;*/ if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, 0, NULL)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, 0, NULL)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, 0, NULL)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, 0, NULL)) ERR; if (nc_close(ncid)) ERR; } /* Make sure we can read all these zero-length atts. */ { int ncid; signed char schar_in[ATT_LEN]; short short_in[ATT_LEN]; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN]; float float_in[ATT_LEN]; double double_in[ATT_LEN]; size_t len; nc_type xtype; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, NULL)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_CHAR) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_SCHAR_NAME, &xtype, &len)) ERR; if (len || xtype != NC_BYTE) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_SHORT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_SHORT) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_INT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_INT) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_FLOAT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_FLOAT) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, &xtype, &len)) ERR; if (len || xtype != NC_DOUBLE) ERR; /* Conversions no longer result in range errors, since there's no data. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing zero-length attributes and redef...(this test skipped for HDF5-1.8.0 beta1"); { int ncid; signed char schar_in[ATT_LEN]; short short_in[ATT_LEN]; int int_in[ATT_LEN]; float float_in[ATT_LEN]; double double_in[ATT_LEN]; /* Create a file with a global attribute of each type of zero length. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, 0, NULL)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, 0, NULL)) ERR; /* if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UCHAR, ATT_LEN, uchar_out)) ERR;*/ if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, 0, NULL)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, 0, NULL)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, 0, NULL)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, 0, NULL)) ERR; if (nc_close(ncid)) ERR; /* Make sure we can read all these zero-length atts added during a * redef. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, NULL)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; /* Conversions no longer result in range errors, since there's no data. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute deletes and renames..."); { int ncid, varid, dimids[2]; nc_type att_type; size_t att_len; char *speech_in; char name_in[NC_MAX_NAME + 1]; int attid_in, natts_in; int int_out[ATT_LEN] = {-100000, 128, 100000}; /* Create a file with a global attribute. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_close(ncid)) ERR; /* Rename it. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT_TEXT_NAME, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attname(ncid, NC_GLOBAL, attid_in, name_in)) ERR; if (strcmp(name_in, ATT_TEXT_NAME)) ERR; if (nc_rename_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, ATT_TEXT_NAME2)) ERR; if (nc_inq_attname(ncid, NC_GLOBAL, attid_in, name_in)) ERR; if (strcmp(name_in, ATT_TEXT_NAME2)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME2, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME2, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in) != NC_ENOTATT) ERR; if (nc_close(ncid)) ERR; /* Now delete the att. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_del_att(ncid, NC_GLOBAL, ATT_TEXT_NAME2)) ERR; if (nc_close(ncid)) ERR; /* Now create a file with a variable, which has an att. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) 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_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and delete it. Make sure it's gone. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_del_att(ncid, 0, ATT_INT_NAME)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and readd the attribute. Enddef and redef, * and delete it, then check to make sure it's gone. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_del_att(ncid, 0, ATT_INT_NAME)) ERR; if (nc_inq_varnatts(ncid, 0, &natts_in)) ERR; if (natts_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute create order..."); #define ATT0 "Maturin" #define ATT1 "Aubery" { int ncid, varid, dimids[2]; int attid_in; const int number = 42; /* Create a file with several global attributes. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT0, NC_INT, 1, &number)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT1, NC_INT, 1, &number)) ERR; if (nc_close(ncid)) ERR; /* Open it and check the order. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT0, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT1, &attid_in)) ERR; if (attid_in != 1) ERR; if (nc_close(ncid)) ERR; /* Now create a file with a variable, which has two atts. */ 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, 2, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, ATT0, NC_INT, 1, &number)) ERR; if (nc_put_att_int(ncid, varid, ATT1, NC_INT, 1, &number)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check the order of the attributes on the var. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, 0, ATT0, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attid(ncid, 0, ATT1, &attid_in)) ERR; if (attid_in != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute ordering some more..."); #define VAR_NAME "i" #define A1_NAME "i" #define A2_NAME "f" #define A3_NAME "d" #define A1_LEN 3 #define A2_LEN 4 #define A3_LEN 5 { int ncid; int varid, natts, nvars; double dvalue[] = {999.99, 999.99, 999.99, 999.99, 999.99}; int varids[1]; char name_in[NC_MAX_NAME + 1]; /* Create a file with one var, and attach three atts to it. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_INT, 0, NULL, &varid)) ERR; if (nc_put_att_double(ncid, varid, A1_NAME, NC_INT, A1_LEN, dvalue)) ERR; if (nc_put_att_double(ncid, varid, A2_NAME, NC_INT, A2_LEN, dvalue)) ERR; if (nc_put_att_double(ncid, varid, A3_NAME, NC_INT, A3_LEN, dvalue)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars, varids)) ERR; if (nvars != 1 || varids[0] != 0) ERR; if (nc_inq_varnatts(ncid, 0, &natts)) ERR; if (natts != 3) ERR; if (nc_inq_attname(ncid, 0, 0, name_in)) ERR; if (strcmp(name_in, A1_NAME)) ERR; if (nc_inq_attname(ncid, 0, 1, name_in)) ERR; if (strcmp(name_in, A2_NAME)) ERR; if (nc_inq_attname(ncid, 0, 2, name_in)) ERR; if (strcmp(name_in, A3_NAME)) ERR; /* Close up shop. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute ordering even more..."); /* Test the ordering of atts for each cmode. */ if (tst_att_ordering(NC_CLOBBER)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_64BIT_OFFSET)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_NETCDF4)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL)) ERR; SUMMARIZE_ERR; printf("*** testing attributes and enddef/redef..."); #define ATT_1 "a" #define ATT_2 "b" #define ATT_3 "c" { int ncid, att = 1; if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL|NC_CLOBBER, &ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_1, NC_INT, 1, &att)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_2, NC_INT, 1, &att)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_3, NC_INT, 1, &att)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing copy of simple global atts..."); { int ncid, ncid2; nc_type att_type; size_t att_len; int i; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; unsigned char uchar_in[ATT_LEN], uchar_out[ATT_LEN] = {0, 128, NC_MAX_CHAR}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN], ushort_out[ATT_LEN] = {0, 128, NC_MAX_USHORT}; unsigned int uint_in[ATT_LEN], uint_out[ATT_LEN] = {0, 128, NC_MAX_UINT}; unsigned long long uint64_in[ATT_LEN], uint64_out[ATT_LEN] = {0, 128, 18446744073709551612ULL}; long long int64_in[ATT_LEN], int64_out[ATT_LEN] = {NC_MIN_INT64, 128, NC_MAX_INT64}; /* Create a file with a global attribute of each type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UBYTE, ATT_LEN, uchar_out)) ERR; if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, ATT_LEN, short_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, ATT_LEN, int_out)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, ATT_LEN, float_out)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, ATT_LEN, double_out)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out)) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out)) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out)) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out)) ERR; /* Create another file and copy all the attributes. */ if (nc_create(FILE_NAME2, NC_NETCDF4, &ncid2)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_SCHAR_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UCHAR_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_SHORT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_INT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_FLOAT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_USHORT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UINT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_INT64_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UINT64_NAME, ncid2, NC_GLOBAL)) ERR; /* Close both files. */ if (nc_close(ncid)) ERR; if (nc_close(ncid2)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME2, 0, &ncid)) ERR; /* Check text. */ if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); /* Check numeric values. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, uchar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uchar_in[i] != uchar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, ushort_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (ushort_in[i] != ushort_out[i]) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, uint_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint_in[i] != uint_out[i]) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, int64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int64_in[i] != int64_out[i]) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, uint64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint64_in[i] != uint64_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int main(int argc, char **argv) { int ncid, dimid, varid; int dimids[NDIMS]; /* unnormalized UTF-8 encoding for Unicode 8-character "Hello" in Greek: */ unsigned char uname_utf8[] = { 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x83, /* COMBINING TILDE */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x8A, /* COMBINING RING ABOVE */ 0x43, /* LATIN CAPITAL LETTER C */ 0xCC, 0xA7, /* COMBINING CEDILLA */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x4E, /* LATIN CAPITAL LETTER N */ 0xCC, 0x83, /* COMBINING TILDE */ 0x00 }; /* NFC normalized UTF-8 encoding for same Unicode string: */ unsigned char nname_utf8[] = { 0xC3, 0x80, /* LATIN CAPITAL LETTER A WITH GRAVE */ 0xC3, 0x81, /* LATIN CAPITAL LETTER A WITH ACUTE */ 0xC3, 0x82, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */ 0xC3, 0x83, /* LATIN CAPITAL LETTER A WITH TILDE */ 0xC3, 0x84, /* LATIN CAPITAL LETTER A WITH DIAERESIS */ 0xC3, 0x85, /* LATIN CAPITAL LETTER A WITH RING ABOVE */ 0xC3, 0x87, /* LATIN CAPITAL LETTER C WITH CEDILLA */ 0xC3, 0x88, /* LATIN CAPITAL LETTER E WITH GRAVE */ 0xC3, 0x89, /* LATIN CAPITAL LETTER E WITH ACUTE */ 0xC3, 0x8A, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */ 0xC3, 0x8B, /* LATIN CAPITAL LETTER E WITH DIAERESIS */ 0xC3, 0x8C, /* LATIN CAPITAL LETTER I WITH GRAVE */ 0xC3, 0x8D, /* LATIN CAPITAL LETTER I WITH ACUTE */ 0xC3, 0x8E, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */ 0xC3, 0x8F, /* LATIN CAPITAL LETTER I WITH DIAERESIS */ 0xC3, 0x91, /* LATIN CAPITAL LETTER N WITH TILDE */ 0x00 }; /* Unnormalized name used for dimension, variable, and attribute value */ #define UNAME ((char *) uname_utf8) #define UNAMELEN (sizeof uname_utf8) /* Normalized name */ #define NNAME ((char *) nname_utf8) #define NNAMELEN (sizeof nname_utf8) char name_in[UNAMELEN + 1], strings_in[UNAMELEN + 1]; nc_type att_type; size_t att_len; int res; int dimid_in, varid_in, attnum_in; int attvals[] = {42}; #define ATTNUM ((sizeof attvals)/(sizeof attvals[0])) printf("\n*** testing UTF-8 normalization..."); if((res = nc_create(FILE7_NAME, NC_CLOBBER, &ncid))) ERR; /* Define dimension with unnormalized Unicode UTF-8 encoded name */ if ((res = nc_def_dim(ncid, UNAME, NX, &dimid))) ERR; dimids[0] = dimid; /* Define variable with same name */ if ((res = nc_def_var(ncid, UNAME, NC_CHAR, NDIMS, dimids, &varid))) ERR; /* Create string attribute with same value */ if ((res = nc_put_att_text(ncid, varid, UNITS, UNAMELEN, UNAME))) ERR; /* Create int attribute with same name */ if ((res = nc_put_att_int(ncid, varid, UNAME, NC_INT, ATTNUM, attvals))) ERR; /* Try to create dimension and variable with NFC-normalized * version of same name. These should fail, as unnormalized name * should have been normalized in library, so these are attempts to * create duplicate netCDF objects. */ if ((res = nc_def_dim(ncid, NNAME, NX, &dimid)) != NC_ENAMEINUSE) ERR; if ((res = nc_def_var(ncid, NNAME, NC_CHAR, NDIMS, dimids, &varid)) != NC_ENAMEINUSE) ERR; if ((res = nc_enddef(ncid))) ERR; /* Write string data, UTF-8 encoded, to the file */ if ((res = nc_put_var_text(ncid, varid, UNAME))) ERR; if ((res = nc_close(ncid))) ERR; /* Check it out. */ if ((res = nc_open(FILE7_NAME, NC_NOWRITE, &ncid))) ERR; if ((res = nc_inq_varid(ncid, UNAME, &varid))) ERR; if ((res = nc_inq_varname(ncid, varid, name_in))) ERR; if ((res = strncmp(NNAME, name_in, NNAMELEN))) ERR; if ((res = nc_inq_varid(ncid, NNAME, &varid_in)) || varid != varid_in) ERR; if ((res = nc_inq_dimid(ncid, UNAME, &dimid_in)) || dimid != dimid_in) ERR; if ((res = nc_inq_dimid(ncid, NNAME, &dimid_in)) || dimid != dimid_in) ERR; if ((res = nc_inq_att(ncid, varid, UNITS, &att_type, &att_len))) ERR; if ((att_type != NC_CHAR || att_len != UNAMELEN)) ERR; if ((res = nc_get_att_text(ncid, varid, UNITS, strings_in))) ERR; strings_in[UNAMELEN] = '\0'; if ((res = strncmp(UNAME, strings_in, UNAMELEN))) ERR; if ((res = nc_inq_attid(ncid, varid, UNAME, &attnum_in)) || ATTNUM != attnum_in) ERR; if ((res = nc_inq_attid(ncid, varid, NNAME, &attnum_in)) || ATTNUM != attnum_in) ERR; if ((res = nc_close(ncid))) ERR; SUMMARIZE_ERR; FINAL_RESULTS; return 0; }