int main(int argc, char **argv) { int ncid, dimid, varid; int var_dims[VAR_RANK]; int g1id, g2id, g3id; float vals[] = {1.0, 2.0, 3.0, 4.0}; printf("\n*** Testing groups.\n"); printf("*** creating nested group file %s...", FILE_NAME); /* Create a file with nested groups. */ if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR; /* At root level define dim, var, atts */ if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(ncid, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_NAME, strlen(ATT_VAL), ATT_VAL)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL), GATT_VAL)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_float(ncid, varid, vals)) ERR; /* put dim, var, atts with same names in a group */ if (nc_def_grp(ncid, G1_NAME, &g1id)) ERR; if (nc_def_dim(g1id, DIM_NAME, DIM_LEN1, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g1id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g1id, varid, ATT_NAME, strlen(ATT_VAL1), ATT_VAL1)) ERR; if (nc_put_att_text(g1id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL1), GATT_VAL1)) ERR; if (nc_enddef(g1id)) ERR; if (nc_put_var_float(g1id, varid, vals)) ERR; /* put dim, var, atts with same names in a second group */ if (nc_def_grp(ncid, G2_NAME, &g2id)) ERR; if (nc_def_dim(g2id, DIM_NAME, DIM_LEN2, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g2id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g2id, varid, ATT_NAME, strlen(ATT_VAL2), ATT_VAL2)) ERR; if (nc_put_att_text(g2id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL2), GATT_VAL2)) ERR; if (nc_enddef(g2id)) ERR; if (nc_put_var_float(g2id, varid, vals)) ERR; /* put dim, var, atts with same names in a subgroup of second group */ if (nc_def_grp(g2id, G3_NAME, &g3id)) ERR; if (nc_def_dim(g3id, DIM_NAME, DIM_LEN3, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g3id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g3id, varid, ATT_NAME, strlen(ATT_VAL3), ATT_VAL3)) ERR; if (nc_put_att_text(g3id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL3), GATT_VAL3)) ERR; if (nc_enddef(g3id)) ERR; if (nc_put_var_float(g3id, varid, vals)) ERR; if (nc_close(ncid)) ERR; SUMMARIZE_ERR; FINAL_RESULTS; }
int CONetCDF4::addGroup(const std::string & _name) { int retvalue = 0; int grpid = this->getCurrentGroup(); CheckError(nc_def_grp(grpid, const_cast<char*>(_name.c_str()), &retvalue)); return (retvalue); }
void R_nc4_def_grp( int *parent_ncid, char **grpname, int *new_ncid, int *retval ) { *retval = nc_def_grp(*parent_ncid, grpname[0], new_ncid ); if( *retval != NC_NOERR ) { Rprintf( "Error in R_nc4_def_grp: %s\n", nc_strerror(*retval) ); Rprintf( "Name of group that the error occurred on: \"%s\"\n", grpname[0] ); } }
int main(int argc, char **argv) { struct timeval start_time, end_time, diff_time; double sec; int nitem = 1000; /* default number of objects of each type */ int i; int ncid; int data[] = {42}; int g, grp, numgrp; char gname[16]; int a, numatt, an, aleft, natts; if(argc > 2) { /* Usage */ printf("NetCDF performance test, writing many groups, variables, and attributes.\n"); printf("Usage:\t%s [N]\n", argv[0]); printf("\tN: number of objects\n"); return(0); } for(i = 1; i < argc; i++) { nitem = atoi(argv[i]); } /* create new file */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* create N group/global attributes, printing time after every 100. * Because only NC_MAX_ATTRS are permitted per group, create the * necessary number of groups to hold nitem attributes. */ numatt = nitem; a = 1; numgrp = (numatt - 1) / NC_MAX_ATTRS + 1; aleft = numatt - (NC_MAX_ATTRS * (numgrp - 1)); if (gettimeofday(&start_time, NULL)) ERR; for(g = 1; g < numgrp + 1; g++) { sprintf(gname, "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; natts = g < numgrp ? NC_MAX_ATTRS : aleft; /* leftovers on last time through */ for(an = 1; an < natts + 1; an++) { char aname[20]; sprintf(aname, "attribute%d", a); if (nc_put_att_int(grp, NC_GLOBAL, aname, NC_INT, 1, data)) ERR; if(a%100 == 0) { /* only print every 100th attribute name */ if (gettimeofday(&end_time, NULL)) ERR; if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR; sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec; printf("%s/%s\t%.3g sec\n", gname, aname, sec); } a++; } } nc_close(ncid); return(0); }
/*! This function makes a request to netcdf with a id of a prent groupd and then return id of the created group, given its name \param [in] parentNcid Id of parent groupd(or File Id) \param [in] grpName Name of the desired group \param [in/out] grpId Group id if this group is created sucessfully \return Status code */ int CNetCdfInterface::defGrp(int parentNcid, const StdString& grpName, int& grpId) { int status = nc_def_grp(parentNcid, grpName.c_str(), &grpId); if (NC_NOERR != status) { StdString errormsg(nc_strerror(status)); StdStringStream sstr; sstr << "Error when calling function nc_def_grp(parentNcid, grpName.c_str(), &grpId)" << std::endl; sstr << errormsg << std::endl; sstr << "Unable to create group Id, given its name: " << grpName << std::endl; StdString e = sstr.str(); throw CNetCdfException(e); } return status; }
int ex_create_group (int parent_id, const char *group_name) { int exoid = -1; int status; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ #if defined(NOT_NETCDF4) exerrval = NC_ENOTNC4; sprintf(errmsg, "Error: Group capabilities are not available in this netcdf version--not netcdf4"); ex_err("ex_create_group",errmsg,exerrval); return (EX_FATAL); #else if ((status = nc_redef (parent_id)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to put file id %d into define mode", parent_id); ex_err("ex_create_group",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_def_grp (parent_id, group_name, &exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: group create failed for %s in file id %d", group_name, parent_id); ex_err("ex_create_group",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_enddef (parent_id)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } #endif return (exoid); }
int main(int argc, char **argv) { nc_set_log_level(0); printf("\n*** Testing netcdf-4 dimensions even more.\n"); printf("*** testing netcdf-4 dimension inheritance..."); { #define FILE_NAME "tst_dims3.nc" #define RANK_time 1 #define GRP_NAME "G" #define TIME_NAME "time" #define VAR2_NAME "z" #define TIME_RANK 1 #define NUM_TIMES 2 int ncid, grpid; int time_dim, time_dim_in; int time_var, z_var; size_t len; int time_data[NUM_TIMES] = {1, 2} ; size_t time_startset[TIME_RANK] = {0} ; size_t time_countset[TIME_RANK] = {NUM_TIMES} ; /* Create file with unlimited dim and associated coordinate * variable in root group, another variable that uses unlimited * dim in subgroup. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, GRP_NAME, &grpid)) ERR; if (nc_def_dim(ncid, TIME_NAME, NC_UNLIMITED, &time_dim)) ERR; if (nc_def_var(ncid, TIME_NAME, NC_INT, TIME_RANK, &time_dim, &time_var)) ERR; if (nc_def_var(grpid, VAR2_NAME, NC_INT, TIME_RANK, &time_dim, &z_var)) ERR; if (nc_enddef(ncid)) ERR; /* Assign data to time variable, creating two times */ if (nc_put_vara(ncid, time_dim, time_startset, time_countset, time_data)) ERR; /* Check the dim len from the root group */ if (nc_inq_dimlen(ncid, time_dim, &len)) ERR; if (len != NUM_TIMES) ERR; /* Check the dim len from the sub group */ if (nc_inq_dimlen(grpid, time_dim, &len)) ERR; if (len != NUM_TIMES) ERR; if (nc_close(ncid)) ERR; /* Now check how many times there are from the subgroup */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_ncid(ncid, GRP_NAME, &grpid)) ERR; if (nc_inq_dimid(ncid, TIME_NAME, &time_dim)) ERR; /* Check the dim len from the root group */ if (nc_inq_dimlen(ncid, time_dim, &len)) ERR; if (len != NUM_TIMES) ERR; /* Check the dim len from the sub group */ if (nc_inq_dimlen(grpid, time_dim, &len)) ERR; if (len != NUM_TIMES) ERR; /* Find the dimension by name. */ if (nc_inq_dimid(grpid, TIME_NAME, &time_dim_in)) ERR; if (time_dim_in != time_dim) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing a scalar coordinate dimension..."); { int ncid, dimid, varid, stat; float data = 42.5; /* Create a scalar coordinate dimension. The only reason that * the user can ever possibly have for doing this is just * because they like to make life difficult for poor, poor * netCDF programmers, trapped in this horrible place, in a * Rocky Mountain valley, drenched in sunlight, with a stream * quietly gurgling, deer feeding on the grasses, and all those * damn birds chirping! */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR_RET; if (nc_def_dim(ncid, "scalar", 0, &dimid)) ERR_RET; if (nc_def_var(ncid, "scalar", NC_FLOAT, 0, &dimid, &varid)) ERR_RET; if (nc_put_var_float(ncid, varid, &data)) ERR_RET; if (nc_close(ncid)) ERR_RET; } SUMMARIZE_ERR; FINAL_RESULTS; }
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; }
/* * Generate C code for creating netCDF from in-memory structure. */ void gen_netcdf(const char *filename) { int stat, ncid; int idim, ivar, iatt; int ndims, nvars, natts, ngatts; #ifdef USE_NETCDF4 int ntyps, ngrps, igrp; #endif Bytebuffer* databuf = bbNew(); ndims = listlength(dimdefs); nvars = listlength(vardefs); natts = listlength(attdefs); ngatts = listlength(gattdefs); #ifdef USE_NETCDF4 ntyps = listlength(typdefs); ngrps = listlength(grpdefs); #endif /*USE_NETCDF4*/ /* create netCDF file, uses NC_CLOBBER mode */ cmode_modifier |= NC_CLOBBER; #ifdef USE_NETCDF4 if(!usingclassic) cmode_modifier |= NC_NETCDF4; #endif stat = nc_create(filename, cmode_modifier, &ncid); check_err(stat,__LINE__,__FILE__); /* ncid created above is also root group*/ rootgroup->ncid = ncid; #ifdef USE_NETCDF4 /* Define the group structure */ /* walking grdefs list will do a preorder walk of all defined groups*/ for(igrp=0;igrp<ngrps;igrp++) { Symbol* gsym = (Symbol*)listget(grpdefs,igrp); if(gsym == rootgroup) continue; /* ignore root group*/ stat = nc_def_grp(gsym->container->ncid,gsym->name,&gsym->ncid); check_err(stat,__LINE__,__FILE__); } #endif #ifdef USE_NETCDF4 /* Define the types*/ if (ntyps > 0) { int ityp; for(ityp = 0; ityp < ntyps; ityp++) { Symbol* tsym = (Symbol*)listget(typdefs,ityp); genbin_deftype(tsym); } } #endif /* define dimensions from info in dims array */ if (ndims > 0) { for(idim = 0; idim < ndims; idim++) { Symbol* dsym = (Symbol*)listget(dimdefs,idim); stat = nc_def_dim(dsym->container->ncid, dsym->name, (dsym->dim.isunlimited?NC_UNLIMITED:dsym->dim.declsize), &dsym->ncid); check_err(stat,__LINE__,__FILE__); } } /* define variables from info in vars array */ if (nvars > 0) { for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if (vsym->typ.dimset.ndims > 0) { /* a dimensioned variable */ /* construct a vector of dimension ids*/ int dimids[NC_MAX_VAR_DIMS]; for(idim=0;idim<vsym->typ.dimset.ndims;idim++) dimids[idim] = vsym->typ.dimset.dimsyms[idim]->ncid; stat = nc_def_var(vsym->container->ncid, vsym->name, vsym->typ.basetype->ncid, vsym->typ.dimset.ndims, dimids, &vsym->ncid); } else { /* a scalar */ stat = nc_def_var(vsym->container->ncid, vsym->name, vsym->typ.basetype->ncid, vsym->typ.dimset.ndims, NULL, &vsym->ncid); } check_err(stat,__LINE__,__FILE__); } } #ifdef USE_NETCDF4 /* define special variable properties */ if(nvars > 0) { for(ivar = 0; ivar < nvars; ivar++) { Symbol* var = (Symbol*)listget(vardefs,ivar); genbin_definespecialattributes(var); } } #endif /*USE_NETCDF4*/ /* define global attributes */ if(ngatts > 0) { for(iatt = 0; iatt < ngatts; iatt++) { Symbol* gasym = (Symbol*)listget(gattdefs,iatt); genbin_defineattr(gasym); } } /* define per-variable attributes */ if(natts > 0) { for(iatt = 0; iatt < natts; iatt++) { Symbol* asym = (Symbol*)listget(attdefs,iatt); genbin_defineattr(asym); } } if (nofill_flag) { stat = nc_set_fill(rootgroup->ncid, NC_NOFILL, 0); check_err(stat,__LINE__,__FILE__); } /* leave define mode */ stat = nc_enddef(rootgroup->ncid); check_err(stat,__LINE__,__FILE__); if(!header_only) { /* Load values into those variables with defined data */ if(nvars > 0) { for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if(vsym->data != NULL) { bbClear(databuf); genbin_definevardata(vsym); } } } } bbFree(databuf); }
int main(int argc, char **argv) { struct timeval start_time, end_time, diff_time; double sec; int nitem = 10000; /* default number of objects of each type */ int i; int ncid; int data[] = {42}; int g, grp, numgrp; char gname[16]; int v, var, numvar, vn, vleft, nvars; if(argc > 2) { /* Usage */ printf("NetCDF performance test, writing many groups and variables.\n"); printf("Usage:\t%s [N]\n", argv[0]); printf("\tN: number of objects\n"); return(0); } for(i = 1; i < argc; i++) { nitem = atoi(argv[i]); } /* create new file */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (gettimeofday(&start_time, NULL)) ERR; /* create N groups, printing time after every 1000 */ numgrp = nitem; for(g = 1; g < numgrp + 1; g++) { sprintf(gname, "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; if (nc_def_var(grp, "var", NC_INT, 0, NULL, &var)) ERR; if(nc_enddef (grp)) ERR; if(nc_put_var(grp, var, data)) ERR; if(g%1000 == 0) { /* only print every 1000th group name */ if (gettimeofday(&end_time, NULL)) ERR; if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR; sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec; printf("%s\t%.3g sec\n", gname, sec); } } nc_close(ncid); /* create new file */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* create N variables, printing time after every 1000. Because only * NC_MAX_VARS are permitted per group, create the necessary number * of groups to hold nitem variables. */ numvar = nitem; v = 1; numgrp = (numvar - 1) / NC_MAX_VARS + 1; vleft = numvar - (NC_MAX_VARS * (numgrp - 1)); if (gettimeofday(&start_time, NULL)) ERR; for(g = 1; g < numgrp + 1; g++) { sprintf(gname, "group%d", g); if (nc_def_grp(ncid, gname, &grp)) ERR; nvars = g < numgrp ? NC_MAX_VARS : vleft; /* leftovers on last time through */ for(vn = 1; vn < nvars + 1; vn++) { int var; char vname[20]; sprintf(vname, "variable%d", v); if(nc_def_var(grp, vname, NC_INT, 0, NULL, &var)) ERR; if(nc_put_var(grp, var, data)) ERR; if(v%1000 == 0) { /* only print every 1000th variable name */ if (gettimeofday(&end_time, NULL)) ERR; if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR; sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec; printf("%s/%s\t%.3g sec\n", gname, vname, sec); } v++; } } nc_close(ncid); return(0); }
int main() {/* create tst_diskless2.nc */ int stat; /* return status */ int ncid; /* netCDF id */ /* group ids */ int root_grp; int g_grp; int h_grp; /* type ids */ int enum_t_typ; int opaque_t_typ; int vlen_t_typ; int g_cmpd_t_typ; /* dimension ids */ int lat_dim; int lon_dim; int time_dim; /* dimension lengths */ size_t lat_len = 10; size_t lon_len = 5; size_t time_len = NC_UNLIMITED; /* variable ids */ int lat_id; int lon_id; int time_id; int Z_id; int t_id; int p_id; int rh_id; int country_id; int tag_id; int h_compoundvar_id; /* rank (number of dimensions) for each variable */ # define RANK_lat 1 # define RANK_lon 1 # define RANK_time 1 # define RANK_Z 3 # define RANK_t 3 # define RANK_p 3 # define RANK_rh 3 # define RANK_country 3 # define RANK_tag 0 # define RANK_h_compoundvar 0 /* variable shapes */ int lat_dims[RANK_lat]; int lon_dims[RANK_lon]; int time_dims[RANK_time]; int Z_dims[RANK_Z]; int t_dims[RANK_t]; int p_dims[RANK_p]; int rh_dims[RANK_rh]; int country_dims[RANK_country]; /* enter define mode */ stat = nc_create("tst_diskless2.nc", NC_DISKLESS|NC_WRITE|NC_CLOBBER|NC_NETCDF4, &ncid); check_err(stat,__LINE__,__FILE__); root_grp = ncid; stat = nc_def_grp(root_grp, "g", &g_grp); check_err(stat,__LINE__,__FILE__); stat = nc_def_grp(root_grp, "h", &h_grp); check_err(stat,__LINE__,__FILE__); { unsigned char econst; stat = nc_def_enum(root_grp, NC_UBYTE, "enum_t", &enum_t_typ); check_err(stat,__LINE__,__FILE__); econst = 0; stat = nc_insert_enum(root_grp, enum_t_typ, "Clear", &econst); check_err(stat,__LINE__,__FILE__); econst = 1; stat = nc_insert_enum(root_grp, enum_t_typ, "Cumulonimbus", &econst); check_err(stat,__LINE__,__FILE__); econst = 2; stat = nc_insert_enum(root_grp, enum_t_typ, "Stratus", &econst); check_err(stat,__LINE__,__FILE__); } stat = nc_def_opaque(root_grp, 11, "opaque_t", &opaque_t_typ); check_err(stat,__LINE__,__FILE__); stat = nc_def_vlen(root_grp, "vlen_t", NC_INT, &vlen_t_typ); check_err(stat,__LINE__,__FILE__); stat = nc_def_compound(g_grp, sizeof(g_cmpd_t), "cmpd_t", &g_cmpd_t_typ); check_err(stat,__LINE__,__FILE__); { stat = nc_insert_compound(g_grp, g_cmpd_t_typ, "f1", NC_COMPOUND_OFFSET(g_cmpd_t,f1), vlen_t_typ); check_err(stat,__LINE__,__FILE__); stat = nc_insert_compound(g_grp, g_cmpd_t_typ, "f2", NC_COMPOUND_OFFSET(g_cmpd_t,f2), enum_t_typ); check_err(stat,__LINE__,__FILE__); } /* define dimensions */ stat = nc_def_dim(root_grp, "lat", lat_len, &lat_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(root_grp, "lon", lon_len, &lon_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(root_grp, "time", time_len, &time_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ lat_dims[0] = lat_dim; stat = nc_def_var(root_grp, "lat", NC_INT, RANK_lat, lat_dims, &lat_id); check_err(stat,__LINE__,__FILE__); lon_dims[0] = lon_dim; stat = nc_def_var(root_grp, "lon", NC_INT, RANK_lon, lon_dims, &lon_id); check_err(stat,__LINE__,__FILE__); time_dims[0] = time_dim; stat = nc_def_var(root_grp, "time", NC_INT, RANK_time, time_dims, &time_id); check_err(stat,__LINE__,__FILE__); Z_dims[0] = time_dim; Z_dims[1] = lat_dim; Z_dims[2] = lon_dim; stat = nc_def_var(root_grp, "Z", NC_FLOAT, RANK_Z, Z_dims, &Z_id); check_err(stat,__LINE__,__FILE__); t_dims[0] = time_dim; t_dims[1] = lat_dim; t_dims[2] = lon_dim; stat = nc_def_var(root_grp, "t", NC_FLOAT, RANK_t, t_dims, &t_id); check_err(stat,__LINE__,__FILE__); p_dims[0] = time_dim; p_dims[1] = lat_dim; p_dims[2] = lon_dim; stat = nc_def_var(root_grp, "p", NC_DOUBLE, RANK_p, p_dims, &p_id); check_err(stat,__LINE__,__FILE__); rh_dims[0] = time_dim; rh_dims[1] = lat_dim; rh_dims[2] = lon_dim; stat = nc_def_var(root_grp, "rh", NC_INT, RANK_rh, rh_dims, &rh_id); check_err(stat,__LINE__,__FILE__); country_dims[0] = time_dim; country_dims[1] = lat_dim; country_dims[2] = lon_dim; stat = nc_def_var(root_grp, "country", NC_STRING, RANK_country, country_dims, &country_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(root_grp, "tag", NC_UBYTE, RANK_tag, 0, &tag_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(h_grp, "compoundvar", g_cmpd_t_typ, RANK_h_compoundvar, 0, &h_compoundvar_id); check_err(stat,__LINE__,__FILE__); /* assign global attributes */ { static const int vlen_2[] = {17, 18, 19} ; static const vlen_t globalatt_att[1] = {{3, (void*)vlen_2}} ; stat = nc_put_att(root_grp, NC_GLOBAL, "globalatt", vlen_t_typ, 1, globalatt_att); check_err(stat,__LINE__,__FILE__); } /* assign per-variable attributes */ { stat = nc_put_att_text(root_grp, lat_id, "long_name", 8, "latitude"); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(root_grp, lat_id, "units", 13, "degrees_north"); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(root_grp, lon_id, "long_name", 9, "longitude"); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(root_grp, lon_id, "units", 12, "degrees_east"); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(root_grp, time_id, "units", 31, "seconds since 1992-1-1 00:00:00"); check_err(stat,__LINE__,__FILE__); } { static const char* Z_units_att[1] = {"geopotential meters"} ; stat = nc_put_att_string(root_grp, Z_id, "units", 1, Z_units_att); check_err(stat,__LINE__,__FILE__); } { static const float Z_valid_range_att[2] = {((float)0), ((float)5000)} ; stat = nc_put_att_float(root_grp, Z_id, "valid_range", NC_FLOAT, 2, Z_valid_range_att); check_err(stat,__LINE__,__FILE__); } { static const double p_FillValue_att[1] = {((double)-9999)} ; stat = nc_put_att_double(root_grp, p_id, "_FillValue", NC_DOUBLE, 1, p_FillValue_att); check_err(stat,__LINE__,__FILE__); } { static const int rh_FillValue_att[1] = {-1} ; stat = nc_put_att_int(root_grp, rh_id, "_FillValue", NC_INT, 1, rh_FillValue_att); check_err(stat,__LINE__,__FILE__); } /* leave define mode */ stat = nc_enddef (root_grp); check_err(stat,__LINE__,__FILE__); /* assign variable data */ { int lat_data[10] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} ; size_t lat_startset[1] = {0} ; size_t lat_countset[1] = {10}; stat = nc_put_vara(root_grp, lat_id, lat_startset, lat_countset, lat_data); check_err(stat,__LINE__,__FILE__); } { int lon_data[5] = {-140, -118, -96, -84, -52} ; size_t lon_startset[1] = {0} ; size_t lon_countset[1] = {5}; stat = nc_put_vara(root_grp, lon_id, lon_startset, lon_countset, lon_data); check_err(stat,__LINE__,__FILE__); } { static const int vlen_10[] = {3, 4, 5} ; size_t zero = 0; static g_cmpd_t h_compoundvar_data[1] = {{{3, (void*)vlen_10}, 2}}; stat = nc_put_var1(h_grp, h_compoundvar_id, &zero, h_compoundvar_data); check_err(stat,__LINE__,__FILE__); } stat = nc_close(root_grp); check_err(stat,__LINE__,__FILE__); return 0; }
static void populateVars(Context ctx,const char *key,void *value,int type,int elems){ int ret,ncgid,cnt; DimRef dr; double *pdDepth=NULL; char *pstrSource=NULL; size_t dep,*basei=NULL,*endi=NULL; size_t i=0; if(ctx.ncid<=0)return; if(type==LUA_TTABLE){//any other type at this level will be disgarded if(NC_NOERR!=(ret=nc_def_grp(ctx.ncid,key,&ncgid))) nc_error(ret,__LINE__); ctx.ncid=ncgid; ctx.pfunCallback=populateAttrs; loopTable(&ctx,NULL,0); if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_FEATURE_TYPE_INSTANCE,1,&(dr.dim1[0])))) nc_error(ret,__LINE__); if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_TIME,NC_UNLIMITED,&(dr.dim2[1])))) nc_error(ret,__LINE__); pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_Z,DEPTH_LEVEL); if(pdDepth){ dep=(size_t)*pdDepth; if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_Z,dep,&(dr.dim3[2])))) nc_error(ret,__LINE__); free(pdDepth); } dr.dim2[0]=dr.dim3[0]=dr.dim1[0]; dr.dim1t[0]=dr.dim3[1]=dr.dim2[1]; dr.dim1z[0]=dr.dim3[2]; ctx.pfunCallback=defineVars; ctx.param=(void*)&dr; loopTable(&ctx,LUA_VARS_ROOT,0); pstrSource=(char*)queryTableValue(ctx.L,LUA_VARS_ROOT,NULL,DATA_SOURCE); if(pstrSource&&dep>0){ pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,OBS_COUNT); if(dep>1){ basei=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MIN); endi=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MAX); } if(pdDepth){//dpDepth represents tscount here cnt=(int)(*pdDepth)*dep; free(pdDepth); ctx.obs=(double*)malloc(sizeof(double)*cnt); ctx.time=(double*)malloc(sizeof(double)*cnt); FILE *f=fopen(pstrSource,"r"); if(f){ double f1,f2,f3; int ret=2; if(1==dep){ while(!feof(f)&&ret==2&&cnt--){ ret=fscanf(f,"%lf,%lf",&f1,&f2); ctx.time[i]=f1; ctx.obs[i++]=f2; } }else{ if(basei&&endi&&*endi>=*basei){ double *pdKey=(double*)malloc(sizeof(double)*(*pdDepth)); ctx.height=(double*)malloc(sizeof(double)*cnt); if(populateArray(ctx.L,VAR_Z,DEPTHS,LUA_TNUMBER,ctx.height,cnt)){ qsort(ctx.height,dep,sizeof(double),compareDoubles); for(;i<cnt;++i) *(ctx.obs+i)=ctx.missingval; while(!feof(f)&&ret==2&&cnt--){ ret=fscanf(f,"%lf,%lf,%lf",&f1,&f2,&f3); bsearch(f3,ctx.height,dep,sizeof(double),compareDoubles); ctx.time[i]=f1; ctx.obs[i++]=f2; } } else{ free(ctx.height,dep,sizeof(double),); ctx.height=NULL; } if(pdKey)free(pdKey); } else{ free(ctx.obs); free(ctx.time); ctx.obs=ctx.time=NULL; } } fclose(f); ctx.pfunCallback=putVars; ctx.len=i; loopTable(&ctx,LUA_VARS_ROOT,0); } if(basei)free(basei); if(endi)free(endi); if(ctx.height)free(ctx.height); if(ctx.time)free(ctx.time); if(ctx.obs)free(ctx.obs); free(pstrSource); }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 group functions.\n"); printf("*** testing use of unlimited dim in parent group..."); { #define NDIMS_IN_VAR 1 #define NDIMS_IN_FILE 2 #define BABE_LIMIT 3 #define DIM_NAME1 "Influence" #define DIM_NAME2 "Babe_Factor" #define VAR_NAME1 "Court_of_Star_Chamber" #define VAR_NAME2 "Justice_of_the_Peace" #define VAR_NAME3 "Bosworth_Field" int ncid, dimid1, dimid2, varid1, varid2, varid3, henry_vii_id; int grpid_in, varid_in1, varid_in2, varid_in3; nc_type xtype_in; int ndims_in, dimids_in[NDIMS_IN_FILE], dimid1_in, natts; char name_in[NC_MAX_NAME + 1]; size_t len_in, index[NDIMS_IN_VAR] = {0}; long long value = NC_FILL_INT64 + 1, value_in; /* Create a file with an unlimited dim and a limited, used by * variables in child groups. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM_NAME1, NC_UNLIMITED, &dimid1)) ERR; if (nc_def_dim(ncid, DIM_NAME2, BABE_LIMIT, &dimid2)) ERR; if (nc_def_grp(ncid, HENRY_VII, &henry_vii_id)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME1, NC_INT64, NDIMS_IN_VAR, &dimid1, &varid1)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME2, NC_INT64, NDIMS_IN_VAR, &dimid1, &varid2)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME3, NC_INT64, NDIMS_IN_VAR, &dimid2, &varid3)) ERR; /* Check it out. Find the group by name. */ if (nc_inq_ncid(ncid, HENRY_VII, &grpid_in)) ERR; /* Ensure that dimensions in parent are visible and correct. */ if (nc_inq_dimids(grpid_in, &ndims_in, dimids_in, 1)) ERR; if (ndims_in != NDIMS_IN_FILE || dimids_in[0] != dimid1 || dimids_in[1] != dimid2) ERR; if (nc_inq_dim(grpid_in, dimids_in[0], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME1) || len_in != 0) ERR; if (nc_inq_dim(grpid_in, dimids_in[1], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME2) || len_in != BABE_LIMIT) ERR; /* Check the vars in the group. */ if (nc_inq_varid(grpid_in, VAR_NAME1, &varid_in1)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME2, &varid_in2)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME3, &varid_in3)) ERR; if (varid_in1 != varid1 || varid_in2 != varid2 || varid_in3 != varid3) ERR; if (nc_inq_var(grpid_in, varid1, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME1) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid2, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME2) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid3, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME3) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid2 || natts != 0) ERR; /* Write one value to one variable. */ if (nc_put_var1_longlong(grpid_in, varid_in1, index, &value)) ERR; /* Read one value from the second unlim dim variable. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Read one value from the variable with limited dim. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Attempt to read beyond end of dimensions to generate error. */ index[0] = BABE_LIMIT; if (nc_get_var1_longlong(grpid_in, varid_in1, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_close(ncid)) ERR; /* Check it out again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Find the group by name. */ if (nc_inq_ncid(ncid, HENRY_VII, &grpid_in)) ERR; /* Ensure that dimensions in parent are visible and correct. */ if (nc_inq_dimids(grpid_in, &ndims_in, dimids_in, 1)) ERR; if (ndims_in != NDIMS_IN_FILE || dimids_in[0] != dimid1 || dimids_in[1] != dimid2) ERR; if (nc_inq_dim(grpid_in, dimids_in[0], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME1) || len_in != 1) ERR; if (nc_inq_dim(grpid_in, dimids_in[1], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME2) || len_in != BABE_LIMIT) ERR; /* Check the vars in the group. */ if (nc_inq_varid(grpid_in, VAR_NAME1, &varid_in1)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME2, &varid_in2)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME3, &varid_in3)) ERR; if (varid_in1 != varid1 || varid_in2 != varid2 || varid_in3 != varid3) ERR; if (nc_inq_var(grpid_in, varid1, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME1) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid2, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME2) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid3, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME3) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid2 || natts != 0) ERR; /* Read one value from the second unlim dim variable. It should * be the fill value. */ index[0] = 0; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Read one value from the variable with limited dim. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Attempt to read beyond end of dimensions to generate error. */ index[0] = BABE_LIMIT; if (nc_get_var1_longlong(grpid_in, varid_in1, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing groups and unlimited dimensions..."); { int ncid; int henry_vii_id; int tudor_id; int dimids_in[MAX_SIBLING_GROUPS], ndims_in; int num_grps; int dimid, dynasty, varid; size_t len_in; int natts_in; int grpids_in[10]; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data_out[DIM1_LEN] = {0, 2, 6}, data_in[DIM1_LEN]; size_t start[1] = {0}, count[1] = {3}; int j; /* Create one group, with one var, which has one dimension, which is unlimited. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, DYNASTY, &tudor_id)) ERR; if (nc_def_dim(tudor_id, DIM1_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_grp(tudor_id, HENRY_VII, &henry_vii_id)) ERR; if (nc_def_var(henry_vii_id, VAR1_NAME, NC_INT, 1, &dimid, &varid)) ERR; if (nc_put_vara_int(henry_vii_id, varid, start, count, data_out)) ERR; if (nc_close(ncid)) ERR; /* Now check the file to see if the dimension and variable are * there. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_grps(ncid, &num_grps, &dynasty)) ERR; if (num_grps != 1) ERR; if (nc_inq_grps(dynasty, &num_grps, grpids_in)) ERR; if (num_grps != 1) ERR; if (nc_inq_dim(grpids_in[0], 0, name_in, &len_in)) ERR; if (strcmp(name_in, DIM1_NAME) || len_in != DIM1_LEN) ERR; if (nc_inq_var(grpids_in[0], 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(name_in, VAR1_NAME) || xtype_in != NC_INT || ndims_in != 1 || dimids_in[0] != 0 || natts_in != 0) ERR; if (nc_get_vara_int(grpids_in[0], 0, start, count, data_in)) ERR; for (j=0; j<DIM1_LEN; j++) if (data_in[j] != data_out[j]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int main(int argc, char **argv) { printf("\n*** Testing UTF-8 names.\n"); printf("*** creating UTF-8 names in classic model netcdf files..."); { int ncid, varid, dimids[NDIMS]; int f; for (f = NC_FORMAT_CLASSIC; f < NC_FORMAT_NETCDF4_CLASSIC; f++) { if (nc_set_default_format(f, NULL)) ERR; if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; /* Define various netcdf objects with a Unicode UTF-8 encoded name * that must be normalized. Where possible, also use the utf8 * string as the value. The name will be normalized, but not the * value. */ if (nc_def_dim(ncid, name_utf8, NX, &dimids[0])) ERR; if (nc_def_var(ncid, name_utf8, NC_CHAR, NDIMS, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, name_utf8, sizeof(name_utf8), name_utf8)) ERR; if (nc_enddef(ncid)) ERR; /* Write var data. */ if (nc_put_var_text(ncid, varid, name_utf8)) ERR; /* Check the file. */ check_classic_file(ncid); if (nc_close(ncid)) ERR; /* Reopen the file and check again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; check_classic_file(ncid); if (nc_close(ncid)) ERR; } /* next format */ } SUMMARIZE_ERR; #define DIM1_NAME "d1" #define VAR1_NAME "v1" #define ATT1_NAME "a1" printf("*** renaming to UTF-8 names in classic model netcdf files..."); { int ncid, varid, dimids[NDIMS]; int f; for (f = NC_FORMAT_CLASSIC; f < NC_FORMAT_NETCDF4_CLASSIC; f++) { if (nc_set_default_format(f, NULL)) ERR; if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; /* Create objects. */ if (nc_def_dim(ncid, DIM1_NAME, NX, &dimids[0])) ERR; if (nc_rename_dim(ncid, 0, name_utf8)) ERR; if (nc_def_var(ncid, name_utf8, NC_CHAR, NDIMS, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT1_NAME, sizeof(name_utf8), name_utf8)) ERR; if (nc_rename_att(ncid, 0, ATT1_NAME, name_utf8)) ERR; if (nc_enddef(ncid)) ERR; /* Write var data. */ if (nc_put_var_text(ncid, varid, name_utf8)) ERR; /* Check the file. */ check_classic_file(ncid); if (nc_close(ncid)) ERR; /* Reopen the file and check again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; check_classic_file(ncid); if (nc_close(ncid)) ERR; } /* next format */ } SUMMARIZE_ERR; printf("*** creating UTF-8 names in netcdf-4 file..."); { int ncid, varid, grpid, comp_typeid, enum_typeid, grpid2, grpid3; int dimids[NDIMS]; char my_int = ENUM_VALUE; if (nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid)) ERR; /* Define various netcdf objects with a Unicode UTF-8 encoded name * that must be normalized. Where possible, also use the utf8 * string as the value. The name will be normalized, but not the * value. */ if (nc_def_grp(ncid, name_utf8, &grpid)) ERR; if (nc_def_dim(grpid, name_utf8, NX, &dimids[0])) ERR; if (nc_def_var(grpid, name_utf8, NC_CHAR, NDIMS, dimids, &varid)) ERR; if (nc_put_att_text(grpid, varid, name_utf8, sizeof(name_utf8), name_utf8)) ERR; if (nc_def_grp(grpid, "tmp", &grpid2)) ERR; if (nc_def_enum(grpid2, NC_BYTE, name_utf8, &enum_typeid)) ERR; if (nc_insert_enum(grpid2, enum_typeid, name_utf8, &my_int)) ERR; if (nc_def_grp(grpid2, "tmp", &grpid3)) ERR; if (nc_def_compound(grpid3, sizeof(struct comp), name_utf8, &comp_typeid)) ERR; if (nc_insert_compound(grpid3, comp_typeid, name_utf8, offsetof(struct comp, i), NC_INT)) ERR; /* Write var data. */ if (nc_put_var_text(grpid, varid, name_utf8)) ERR; /* Check the file. */ check_nc4_file(ncid); if (nc_close(ncid)) ERR; /* Reopen the file and check again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; check_nc4_file(ncid); if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** ensuring UTF-8 normaization is applied in rename..."); { int ncid, varid; char name_in[NC_MAX_NAME + 1]; if (nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid)) ERR; if (nc_def_var(ncid, BORING_NAME, NC_CHAR, 0, NULL, &varid)) ERR; if (nc_rename_var(ncid, varid, name_utf8)) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (!strcmp(name_in, norm_utf8)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (!strcmp(name_in, norm_utf8)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 coordinate variables and dimensions.\n"); printf("**** testing Jeff Whitakers reported 4.1 bug..."); { #define NDIMS 2 #define NLAT 10 #define NLON 20 #define LAT_NAME "lat" #define LON_NAME "lon" #define NVARS 2 #define START_LAT 25.0 #define START_LON -125.0 { int ncid, lon_dimid, lat_dimid; int lat_varid, lon_varid; float lats[NLAT], lons[NLON]; int lat, lon; int nvars, ndims, ngatts, unlimdimid, nvars_in, varids_in[NVARS]; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1]; nc_type xtype_in; /* Initialize coord data. */ for (lat = 0; lat < NLAT; lat++) lats[lat] = START_LAT + 5. * lat; for (lon = 0; lon < NLON; lon++) lons[lon] = START_LON + 5. * lon; /* Create file with two dimensions. */ if (nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)) ERR; if (nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)) ERR; if (nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid, &lon_varid)) ERR; /* Define and write longitude coord variable. */ if (nc_put_var_float(ncid, lon_varid, &lons[0])) ERR; /* Define and write latitude coord variable. */ if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid, &lat_varid)) ERR; if (nc_put_var_float(ncid, lat_varid, &lats[0])) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 2 || ndims != 2 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 2 || varids_in[0] != 0 || varids_in[1] != 1) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 || dimids_in[0] != 1 || natts_in != 0) ERR; /* Close the file. */ if (nc_close(ncid)) ERR; /* Re-open and check the file. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 2 || ndims != 2 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 2 || varids_in[0] != 0 || varids_in[1] != 1) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 || dimids_in[0] != 1 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } } SUMMARIZE_ERR; printf("**** testing setting cache values for coordinate variables..."); { #define RANK_1 1 #define DIM0_NAME "d0" #define CACHE_SIZE 1000000 #define CACHE_NELEMS 1009 #define CACHE_PREEMPTION .90 int ncid, dimid, varid; char name_in[NC_MAX_NAME + 1]; /* Create a test file with one dim and coord variable. */ if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM0_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_var(ncid, DIM0_NAME, NC_DOUBLE, 1, &dimid, &varid)) ERR; if (nc_set_var_chunk_cache(ncid, varid, CACHE_SIZE, CACHE_NELEMS, CACHE_PREEMPTION)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, DIM0_NAME)) ERR; if (nc_set_var_chunk_cache(ncid, 0, CACHE_SIZE, CACHE_NELEMS, CACHE_PREEMPTION)) ERR; if (nc_inq_dimname(ncid, 0, name_in)) ERR; if (strcmp(name_in, DIM0_NAME)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing multi-line strings in attributes..."); { int ncid_classic, ncid_nc4; /*int attid;*/ char att_in_classic[NC_MAX_NAME + 1], att_in_nc4[NC_MAX_NAME + 1]; #define FILE_CLASSIC "tst_coords_classic_att.nc" #define FILE_NC4 "tst_coords_nc4_att.nc" /* Create a classic and a netcdf-4 file. */ if (nc_create(FILE_CLASSIC, 0, &ncid_classic)) ERR; if (nc_create(FILE_NC4, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid_nc4)) ERR; /* Write an att to both. */ if (nc_put_att_text(ncid_classic, NC_GLOBAL, INST_NAME, strlen(INSTITUTION) + 1, INSTITUTION)) ERR; if (nc_put_att_text(ncid_nc4, NC_GLOBAL, INST_NAME, strlen(INSTITUTION) + 1, INSTITUTION)) ERR; /* Close them. */ if (nc_close(ncid_classic)) ERR; if (nc_close(ncid_nc4)) ERR; /* Reopen the files. */ if (nc_open(FILE_CLASSIC, 0, &ncid_classic)) ERR; if (nc_open(FILE_NC4, 0, &ncid_nc4)) ERR; /* I'll show you mine, if you show me yours... */ if (nc_get_att_text(ncid_classic, NC_GLOBAL, INST_NAME, att_in_classic)) ERR; if (nc_get_att_text(ncid_nc4, NC_GLOBAL, INST_NAME, att_in_nc4)) ERR; if (strcmp(att_in_classic, INSTITUTION) || strcmp(att_in_nc4, INSTITUTION) || strcmp(att_in_classic, att_in_nc4)) ERR; if (memcmp(att_in_classic, att_in_nc4, strlen(INSTITUTION) + 1)) ERR; /* Close them. */ if (nc_close(ncid_classic)) ERR; if (nc_close(ncid_nc4)) ERR; } SUMMARIZE_ERR; printf("**** testing ar-4 example metadata with ncgen-generated code..."); { int stat; /* return status */ int ncid; /* netCDF id */ /* group ids */ int root_grp; /* dimension ids */ int lon_dim; int lat_dim; int bnds_dim; int time_dim; /* dimension lengths */ size_t lon_len = 256; size_t lat_len = 128; size_t bnds_len = 2; size_t time_len = NC_UNLIMITED; /* variable ids */ int lon_bnds_id; int lat_bnds_id; int time_bnds_id; int time_id; int lat_id; int lon_id; int pr_id; /* rank (number of dimensions) for each variable */ # define RANK_lon_bnds 2 # define RANK_lat_bnds 2 # define RANK_time_bnds 2 # define RANK_time 1 # define RANK_lat 1 # define RANK_lon 1 # define RANK_pr 3 /* variable shapes */ int lon_bnds_dims[RANK_lon_bnds]; int lat_bnds_dims[RANK_lat_bnds]; int time_bnds_dims[RANK_time_bnds]; int time_dims[RANK_time]; int lat_dims[RANK_lat]; int lon_dims[RANK_lon]; int pr_dims[RANK_pr]; /* enter define mode */ stat = nc_create(FILE_NAME, NC_NETCDF4 | NC_CLASSIC_MODEL, &ncid); check_err(stat,__LINE__,__FILE__); root_grp = ncid; /* define dimensions */ stat = nc_def_dim(root_grp, "lon", lon_len, &lon_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(root_grp, "lat", lat_len, &lat_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(root_grp, "bnds", bnds_len, &bnds_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(root_grp, "time", time_len, &time_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ lon_bnds_dims[0] = lon_dim; lon_bnds_dims[1] = bnds_dim; stat = nc_def_var(root_grp, "lon_bnds", NC_DOUBLE, RANK_lon_bnds, lon_bnds_dims, &lon_bnds_id); check_err(stat,__LINE__,__FILE__); lat_bnds_dims[0] = lat_dim; lat_bnds_dims[1] = bnds_dim; stat = nc_def_var(root_grp, "lat_bnds", NC_DOUBLE, RANK_lat_bnds, lat_bnds_dims, &lat_bnds_id); check_err(stat,__LINE__,__FILE__); time_bnds_dims[0] = time_dim; time_bnds_dims[1] = bnds_dim; stat = nc_def_var(root_grp, "time_bnds", NC_DOUBLE, RANK_time_bnds, time_bnds_dims, &time_bnds_id); check_err(stat,__LINE__,__FILE__); time_dims[0] = time_dim; stat = nc_def_var(root_grp, "time", NC_DOUBLE, RANK_time, time_dims, &time_id); check_err(stat,__LINE__,__FILE__); lat_dims[0] = lat_dim; stat = nc_def_var(root_grp, "lat", NC_DOUBLE, RANK_lat, lat_dims, &lat_id); check_err(stat,__LINE__,__FILE__); lon_dims[0] = lon_dim; stat = nc_def_var(root_grp, "lon", NC_DOUBLE, RANK_lon, lon_dims, &lon_id); check_err(stat,__LINE__,__FILE__); pr_dims[0] = time_dim; pr_dims[1] = lat_dim; pr_dims[2] = lon_dim; stat = nc_def_var(root_grp, "pr", NC_FLOAT, RANK_pr, pr_dims, &pr_id); check_err(stat,__LINE__,__FILE__); /* assign global attributes */ { /* table_id */ static const char table_id_att[8] = {"Table A1"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "table_id", 8, table_id_att); check_err(stat,__LINE__,__FILE__); } { /* title */ static const char title_att[34] = {"model output prepared for IPCC AR4"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "title", 34, title_att); check_err(stat,__LINE__,__FILE__); } { /* institution */ static const char institution_att[66] = {INSTITUTION} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, INST_NAME, 66, institution_att); check_err(stat,__LINE__,__FILE__); } { /* source */ static const char source_att[153] = {"CCSM3.0, version vector05 (2004): \natmosphere: CAM3.0, T85L26;\nocean : POP1.4.3 (modified), gx1v3\nsea ice : CSIM5.0, gx1v3;\nland : CLM3.0, T85"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "source", 153, source_att); check_err(stat,__LINE__,__FILE__); } { /* contact */ static const char contact_att[13] = {"*****@*****.**"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "contact", 13, contact_att); check_err(stat,__LINE__,__FILE__); } { /* project_id */ static const char project_id_att[22] = {"IPCC Fourth Assessment"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "project_id", 22, project_id_att); check_err(stat,__LINE__,__FILE__); } { /* Conventions */ static const char Conventions_att[6] = {"CF-1.0"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "Conventions", 6, Conventions_att); check_err(stat,__LINE__,__FILE__); } { /* references */ static const char references_att[137] = {"Collins, W.D., et al., 2005:\n The Community Climate System Model, Version 3\n Journal of Climate\n \n Main website: http://www.ccsm.ucar.edu"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "references", 137, references_att); check_err(stat,__LINE__,__FILE__); } { /* acknowledgment */ static const char acknowledgment_att[847] = {" Any use of CCSM data should acknowledge the contribution\n of the CCSM project and CCSM sponsor agencies with the \n following citation:\n 'This research uses data provided by the Community Climate\n System Model project (www.ccsm.ucar.edu), supported by the\n Directorate for Geosciences of the National Science Foundation\n and the Office of Biological and Environmental Research of\n the U.S. Department of Energy.'\nIn addition, the words 'Community Climate System Model' and\n 'CCSM' should be included as metadata for webpages referencing\n work using CCSM data or as keywords provided to journal or book\npublishers of your manuscripts.\nUsers of CCSM data accept the responsibility of emailing\n citations of publications of research using CCSM data to\n [email protected].\nAny redistribution of CCSM data must include this data\n acknowledgement statement."} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "acknowledgment", 847, acknowledgment_att); check_err(stat,__LINE__,__FILE__); } { /* realization */ static const int realization_att[1] = {8} ; stat = nc_put_att_int(root_grp, NC_GLOBAL, "realization", NC_INT, 1, realization_att); check_err(stat,__LINE__,__FILE__); } { /* experiment_id */ static const char experiment_id_att[46] = {"climate of the 20th Century experiment (20C3M)"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "experiment_id", 46, experiment_id_att); check_err(stat,__LINE__,__FILE__); } { /* history */ static const char history_att[139] = {"Created from CCSM3 case b30.030g.ES01\n by [email protected]\n on Wed Sep 10 12:22:06 MDT 2008\n \n For all data, added IPCC requested metadata"} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "history", 139, history_att); check_err(stat,__LINE__,__FILE__); } { /* comment */ static const char comment_att[1105] = {"This simulation was initiated from year 460 of \n CCSM3 model run b30.020.ES01 and executed on \n hardware Earth Simulator Center, JAMSTEC. The input external forcings are\nozone forcing : mozart.o3.128x64_L18_1870-2000_c040515.nc\naerosol optics : AerosolOptics_c040105.nc\naerosol MMR : AerosolMass_V_128x256_clim_c031022.nc\ncarbon scaling : carbonscaling_1870-2000_c040225.nc\nsolar forcing : scon_lean_1870-2100_c040123.nc\nGHGs : ghg_1870_2100_c040122.nc\nGHG loss rates : noaamisc.r8.nc\nvolcanic forcing : VolcanicMass_1870-1999_64x1_L18_c040123.nc\nDMS emissions : DMS_emissions_128x256_clim_c040122.nc\noxidants : oxid_128x256_L26_clim_c040112.nc\nSOx emissions : SOx_emissions_128x256_L2_1850-2000_c040321.nc\n Physical constants used for derived data:\n Lv (latent heat of evaporation): 2.501e6 J kg-1\n Lf (latent heat of fusion ): 3.337e5 J kg-1\n r[h2o] (density of water ): 1000 kg m-3\n g2kg (grams to kilograms ): 1000 g kg-1\n \n Integrations were performed by NCAR and CRIEPI with support\n and facilities provided by NSF, DOE, MEXT and ESC/JAMSTEC."} ; stat = nc_put_att_text(root_grp, NC_GLOBAL, "comment", 1105, comment_att); check_err(stat,__LINE__,__FILE__); } /* assign per-variable attributes */ { /* calendar */ static const char time_calendar_att[6] = {"noleap"} ; stat = nc_put_att_text(root_grp, time_id, "calendar", 6, time_calendar_att); check_err(stat,__LINE__,__FILE__); } { /* standard_name */ static const char time_standard_name_att[4] = {"time"} ; stat = nc_put_att_text(root_grp, time_id, "standard_name", 4, time_standard_name_att); check_err(stat,__LINE__,__FILE__); } { /* axis */ static const char time_axis_att[1] = {"T"} ; stat = nc_put_att_text(root_grp, time_id, "axis", 1, time_axis_att); check_err(stat,__LINE__,__FILE__); } { /* units */ static const char time_units_att[19] = {"days since 0000-1-1"} ; stat = nc_put_att_text(root_grp, time_id, "units", 19, time_units_att); check_err(stat,__LINE__,__FILE__); } { /* bounds */ static const char time_bounds_att[9] = {"time_bnds"} ; stat = nc_put_att_text(root_grp, time_id, "bounds", 9, time_bounds_att); check_err(stat,__LINE__,__FILE__); } { /* long_name */ static const char time_long_name_att[4] = {"time"} ; stat = nc_put_att_text(root_grp, time_id, "long_name", 4, time_long_name_att); check_err(stat,__LINE__,__FILE__); } { /* axis */ static const char lat_axis_att[1] = {"Y"} ; stat = nc_put_att_text(root_grp, lat_id, "axis", 1, lat_axis_att); check_err(stat,__LINE__,__FILE__); } { /* standard_name */ static const char lat_standard_name_att[8] = {"latitude"} ; stat = nc_put_att_text(root_grp, lat_id, "standard_name", 8, lat_standard_name_att); check_err(stat,__LINE__,__FILE__); } { /* bounds */ static const char lat_bounds_att[8] = {"lat_bnds"} ; stat = nc_put_att_text(root_grp, lat_id, "bounds", 8, lat_bounds_att); check_err(stat,__LINE__,__FILE__); } { /* long_name */ static const char lat_long_name_att[8] = {"latitude"} ; stat = nc_put_att_text(root_grp, lat_id, "long_name", 8, lat_long_name_att); check_err(stat,__LINE__,__FILE__); } { /* units */ static const char lat_units_att[13] = {"degrees_north"} ; stat = nc_put_att_text(root_grp, lat_id, "units", 13, lat_units_att); check_err(stat,__LINE__,__FILE__); } { /* axis */ static const char lon_axis_att[1] = {"X"} ; stat = nc_put_att_text(root_grp, lon_id, "axis", 1, lon_axis_att); check_err(stat,__LINE__,__FILE__); } { /* standard_name */ static const char lon_standard_name_att[9] = {"longitude"} ; stat = nc_put_att_text(root_grp, lon_id, "standard_name", 9, lon_standard_name_att); check_err(stat,__LINE__,__FILE__); } { /* bounds */ static const char lon_bounds_att[8] = {"lon_bnds"} ; stat = nc_put_att_text(root_grp, lon_id, "bounds", 8, lon_bounds_att); check_err(stat,__LINE__,__FILE__); } { /* long_name */ static const char lon_long_name_att[9] = {"longitude"} ; stat = nc_put_att_text(root_grp, lon_id, "long_name", 9, lon_long_name_att); check_err(stat,__LINE__,__FILE__); } { /* units */ static const char lon_units_att[12] = {"degrees_east"} ; stat = nc_put_att_text(root_grp, lon_id, "units", 12, lon_units_att); check_err(stat,__LINE__,__FILE__); } { /* comment */ static const char pr_comment_att[60] = {"Created using NCL code CCSM_atmm_2cf.ncl on\n machine mineral"} ; stat = nc_put_att_text(root_grp, pr_id, "comment", 60, pr_comment_att); check_err(stat,__LINE__,__FILE__); } { /* missing_value */ static const float pr_missing_value_att[1] = {1e+20} ; stat = nc_put_att_float(root_grp, pr_id, "missing_value", NC_FLOAT, 1, pr_missing_value_att); check_err(stat,__LINE__,__FILE__); } { /* _FillValue */ static const float pr_FillValue_att[1] = {1e+20} ; stat = nc_put_att_float(root_grp, pr_id, "_FillValue", NC_FLOAT, 1, pr_FillValue_att); check_err(stat,__LINE__,__FILE__); } { /* cell_methods */ static const char pr_cell_methods_att[30] = {"time: mean (interval: 1 month)"} ; stat = nc_put_att_text(root_grp, pr_id, "cell_methods", 30, pr_cell_methods_att); check_err(stat,__LINE__,__FILE__); } { /* history */ static const char pr_history_att[20] = {"(PRECC+PRECL)*r[h2o]"} ; stat = nc_put_att_text(root_grp, pr_id, "history", 20, pr_history_att); check_err(stat,__LINE__,__FILE__); } { /* original_units */ static const char pr_original_units_att[7] = {"m-1 s-1"} ; stat = nc_put_att_text(root_grp, pr_id, "original_units", 7, pr_original_units_att); check_err(stat,__LINE__,__FILE__); } { /* original_name */ static const char pr_original_name_att[12] = {"PRECC, PRECL"} ; stat = nc_put_att_text(root_grp, pr_id, "original_name", 12, pr_original_name_att); check_err(stat,__LINE__,__FILE__); } { /* standard_name */ static const char pr_standard_name_att[18] = {"precipitation_flux"} ; stat = nc_put_att_text(root_grp, pr_id, "standard_name", 18, pr_standard_name_att); check_err(stat,__LINE__,__FILE__); } { /* units */ static const char pr_units_att[10] = {"kg m-2 s-1"} ; stat = nc_put_att_text(root_grp, pr_id, "units", 10, pr_units_att); check_err(stat,__LINE__,__FILE__); } { /* long_name */ static const char pr_long_name_att[18] = {"precipitation_flux"} ; stat = nc_put_att_text(root_grp, pr_id, "long_name", 18, pr_long_name_att); check_err(stat,__LINE__,__FILE__); } { /* cell_method */ static const char pr_cell_method_att[10] = {"time: mean"} ; stat = nc_put_att_text(root_grp, pr_id, "cell_method", 10, pr_cell_method_att); check_err(stat,__LINE__,__FILE__); } /* don't initialize variables with fill values */ stat = nc_set_fill(root_grp, NC_NOFILL, 0); check_err(stat,__LINE__,__FILE__); /* leave define mode */ stat = nc_enddef (root_grp); check_err(stat,__LINE__,__FILE__); stat = nc_close(root_grp); check_err(stat,__LINE__,__FILE__); { #define NDIMS4 4 int ndims, dimids_in[NDIMS4]; char name_in[NC_MAX_NAME + 1]; char institution_att_in[NC_MAX_NAME + 1]; /* Now open this file and check order of dimensions. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check dimids. */ if (nc_inq_dimname(ncid, 0, name_in)) ERR; if (strcmp(name_in, "lon")) ERR; if (nc_inq_dimname(ncid, 1, name_in)) ERR; if (strcmp(name_in, "lat")) ERR; if (nc_inq_dimname(ncid, 2, name_in)) ERR; if (strcmp(name_in, "bnds")) ERR; if (nc_inq_dimname(ncid, 3, name_in)) ERR; if (strcmp(name_in, "time")) ERR; /* Check inq_dimids function. */ if (nc_inq_dimids(ncid, &ndims, dimids_in, 0)) ERR; if (ndims != NDIMS4 || dimids_in[0] != 0 || dimids_in[1] != 1 || dimids_in[2] != 2 || dimids_in[3] != 3) ERR; /* Check attribute with line breaks. */ if (nc_get_att_text(ncid, NC_GLOBAL, INST_NAME, institution_att_in)) ERR; if (strncmp(institution_att_in, INSTITUTION, strlen(INSTITUTION))) ERR; if (nc_close(ncid)) ERR; } } SUMMARIZE_ERR; printf("**** testing dim order when coord vars are defined in the wrong order..."); { #define RANK_2 2 #define DIM0 "d0" #define DIM1 "d1" int ncid, dimids[RANK_2], varid[RANK_2]; char name_in[NC_MAX_NAME + 1]; /* Create a test file. */ if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_NETCDF4, &ncid)) ERR; /* Define dimensions in order. */ if (nc_def_dim(ncid, DIM0, NC_UNLIMITED, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM1, 4, &dimids[1])) ERR; /* Define coordinate variables in a different order. */ if (nc_def_var(ncid, DIM1, NC_DOUBLE, 1, &dimids[1], &varid[1])) ERR; if (nc_def_var(ncid, DIM0, NC_DOUBLE, 1, &dimids[0], &varid[0])) ERR; /* That's it! */ if (nc_close(ncid)) ERR; /* Reopen the file and check dimension order. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_dimname(ncid, 0, name_in)) ERR; if (strcmp(name_in, DIM0)) ERR; if (nc_inq_dimname(ncid, 1, name_in)) ERR; if (strcmp(name_in, DIM1)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing order of coordinate dims..."); { #define RANK_3 3 #define DIM1_NAME "d1" #define DIM2_NAME "d2" #define DIM3_NAME "d3" int ncid, dimids[RANK_3], varid[RANK_3]; char name_in[NC_MAX_NAME + 1]; /* Create a 3D test file. */ if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_NETCDF4, &ncid)) ERR; /* Define dimensions in order. */ if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, 4, &dimids[1])) ERR; if (nc_def_dim(ncid, DIM3_NAME, 3, &dimids[2])) ERR; /* Define coordinate variables in a different order. */ if (nc_def_var(ncid, DIM1_NAME, NC_DOUBLE, 1, &dimids[0], &varid[0])) ERR; if (nc_def_var(ncid, DIM2_NAME, NC_DOUBLE, 1, &dimids[1], &varid[1])) ERR; if (nc_def_var(ncid, DIM3_NAME, NC_DOUBLE, 1, &dimids[2], &varid[2])) ERR; /* That's it! */ if (nc_close(ncid)) ERR; /* Reopen the file and check dimension order. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_dimname(ncid, 0, name_in)) ERR; if (strcmp(name_in, DIM1_NAME)) ERR; if (nc_inq_dimname(ncid, 1, name_in)) ERR; if (strcmp(name_in, DIM2_NAME)) ERR; if (nc_inq_dimname(ncid, 2, name_in)) ERR; if (strcmp(name_in, DIM3_NAME)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing coordinate vars..."); { # define RANK_Coordinates_lat 1 int ncid; int Coordinates_grp; int lat_dim; size_t lat_len = 3; int Coordinates_lat_id; if(nc_create(FILE_NAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, "Coordinates", &Coordinates_grp)) ERR; /* Define dimensions in root group. */ if (nc_def_dim(ncid, "lat", lat_len, &lat_dim)) ERR; /* Define coordinate variable in Coordinates group. */ if (nc_def_var(Coordinates_grp, "lat", NC_FLOAT, 1, &lat_dim, &Coordinates_lat_id)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing 2D non-coordinate variable..."); { #define VAR_NAME "Britany" #define NDIMS 2 #define TEXT_LEN 15 #define D0_NAME "time" #define D1_NAME "tl" int ncid, nvars_in, varids_in[1]; int time_dimids[NDIMS], time_id; size_t time_count[NDIMS], time_index[NDIMS] = {0, 0}; const char ttext[TEXT_LEN]="20051224.150000"; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1]; nc_type xtype_in; /* Create a netcdf-4 file with 2D coordinate var. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, D0_NAME, NC_UNLIMITED, &time_dimids[0])) ERR; if (nc_def_dim(ncid, D1_NAME, TEXT_LEN, &time_dimids[1])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_USHORT, NDIMS, time_dimids, &time_id) != NC_ESTRICTNC3) ERR; if (nc_def_var(ncid, VAR_NAME, NC_CHAR, NDIMS, time_dimids, &time_id)) ERR; if (nc_enddef(ncid)) ERR; /* Write one time to the coordinate variable. */ time_count[0] = 1; time_count[1] = TEXT_LEN; if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 1 || ndims != 2 || ngatts != 0 || unlimdimid != 0) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, VAR_NAME) || xtype_in != NC_CHAR || ndims_in != 2 || dimids_in[0] != 0 || dimids_in[1] != 1 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing 2D coordinate variable..."); { #define NDIMS 2 #define TEXT_LEN 15 #define D0_NAME "time" #define D1_NAME "tl" int ncid, nvars_in, varids_in[1]; int time_dimids[NDIMS], time_id; size_t time_count[NDIMS], time_index[NDIMS] = {0, 0}; const char ttext[TEXT_LEN + 1]="20051224.150000"; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1]; nc_type xtype_in; /* Create a netcdf-4 file with 2D coordinate var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, D0_NAME, NC_UNLIMITED, &time_dimids[0])) ERR; if (nc_def_dim(ncid, D1_NAME, TEXT_LEN, &time_dimids[1])) ERR; if (nc_def_var(ncid, D0_NAME, NC_CHAR, NDIMS, time_dimids, &time_id)) ERR; /* Write one time to the coordinate variable. */ time_count[0] = 1; time_count[1] = TEXT_LEN; if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 1 || ndims != 2 || ngatts != 0 || unlimdimid != 0) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, D0_NAME) || xtype_in != NC_CHAR || ndims_in != 2 || dimids_in[0] != 0 || dimids_in[1] != 1 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing new order of doing things with coordinate variable..."); { /* In this test: define a dimension define a variable that uses that dimension put values in the variable define coordinate values for the dimension */ #define VAR_NAME_BB "The_Birth_of_Britain" #define NDIMS_1 1 #define TEXT_LEN 15 #define WINSTON_CHURCHILL "Winston_S_Churchill" #define D0_LEN 2 #define NUM_VARS_2 2 int ncid, nvars_in, varids_in[NUM_VARS_2]; int dimid, varid, varid2; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1]; nc_type xtype_in; int data[D0_LEN] = {42, -42}; /* Create a netcdf-4 file with 2D coordinate var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, WINSTON_CHURCHILL, NC_UNLIMITED, &dimid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS_1, &dimid, &varid)) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; if (nc_def_var(ncid, WINSTON_CHURCHILL, NC_INT, NDIMS_1, &dimid, &varid2)) ERR; if (nc_put_var_int(ncid, varid2, data)) ERR; /* Check things. */ if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 2 || ndims != 1 || ngatts != 0 || unlimdimid != 0) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 2 || varids_in[0] != 0 || varids_in[1] != 1) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, VAR_NAME) || xtype_in != NC_INT || ndims_in != 1 || dimids_in[0] != 0 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != 2 || ndims != 1 || ngatts != 0 || unlimdimid != 0) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 2 || varids_in[0] != 0 || varids_in[1] != 1) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, VAR_NAME) || xtype_in != NC_INT || ndims_in != 1 || dimids_in[0] != 0 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing 2D coordinate variable with dimensions defined in different order..."); { #define NDIMS 2 #define TEXT_LEN 15 #define D0_NAME "time" #define D1_NAME "tl" #define NUM_VARS 2 int ncid, nvars_in, varids_in[NUM_VARS]; int time_dimids[NDIMS], time_id, tl_id; size_t time_count[NDIMS], time_index[NDIMS] = {0, 0}; const char ttext[TEXT_LEN + 1]="20051224.150000"; char ttext_in[TEXT_LEN + 1]; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1], dim_name_in[NC_MAX_NAME + 1]; size_t len_in; nc_type xtype_in; /* Create a netcdf-4 file with 2D coordinate var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, D1_NAME, TEXT_LEN, &time_dimids[1])) ERR; if (nc_def_dim(ncid, D0_NAME, NC_UNLIMITED, &time_dimids[0])) ERR; if (nc_def_var(ncid, D0_NAME, NC_CHAR, NDIMS, time_dimids, &time_id)) ERR; if (nc_def_var(ncid, D1_NAME, NC_CHAR, 1, &time_dimids[0], &tl_id)) ERR; /* Write one time to the coordinate variable. */ time_count[0] = 1; time_count[1] = TEXT_LEN; if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Close up. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != NUM_VARS || ndims != NDIMS || ngatts != 0 || unlimdimid != 1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != NUM_VARS || varids_in[0] != 0 || varids_in[1] != 1) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, D0_NAME) || xtype_in != NC_CHAR || ndims_in != NDIMS || dimids_in[0] != 1 || dimids_in[1] != 0 || natts_in != 0) ERR; if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; if (ndims_in != NDIMS || dimids_in[0] != 0 || dimids_in[1] != 1) ERR; if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, D1_NAME) || len_in != TEXT_LEN) ERR; if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, D0_NAME) || len_in != 1) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Close up. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
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 main(int argc, char **argv) { printf("\n*** Testing netcdf-4 coordinate dimensions and variables, some more.\n"); printf("**** testing more complex 2D coordinate variable with dimensions defined in different order..."); { #define NDIMS 5 #define NVARS 4 #define LON_NAME "lon" #define LAT_NAME "lat" #define LVL_NAME "lvl" #define TIME_NAME "time" #define TEXT_LEN_NAME "text_len" #define TEMP_NAME "temp" #define PRES_NAME "pres" #define LON_LEN 5 #define LAT_LEN 3 #define LVL_LEN 7 #define TIME_LEN 2 #define TEXT_LEN 15 #define TIME_NDIMS 2 #define DATA_NDIMS 4 int ncid, nvars_in, varids_in[NVARS]; int time_dimids[TIME_NDIMS]; int dimids[NDIMS], time_id, lon_id, pres_id, temp_id; size_t time_count[NDIMS], time_index[NDIMS] = {0, 0}; const char ttext[TEXT_LEN + 1]="20051224.150000"; char ttext_in[TEXT_LEN + 1]; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1], dim_name_in[NC_MAX_NAME + 1]; size_t len_in; nc_type xtype_in; int d; /* Create a netcdf-4 file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* Define dimensions. */ if (nc_def_dim(ncid, LON_NAME, LON_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimids[1])) ERR; if (nc_def_dim(ncid, LVL_NAME, LVL_LEN, &dimids[2])) ERR; if (nc_def_dim(ncid, TIME_NAME, TIME_LEN, &dimids[3])) ERR; if (nc_def_dim(ncid, TEXT_LEN_NAME, TEXT_LEN, &dimids[4])) ERR; /* Define two coordinate variables out of order. */ time_dimids[0] = dimids[3]; time_dimids[1] = dimids[4]; if (nc_def_var(ncid, TIME_NAME, NC_CHAR, TIME_NDIMS, time_dimids, &time_id)) ERR; if (nc_def_var(ncid, LON_NAME, NC_CHAR, 1, &dimids[0], &lon_id)) ERR; /* Write one time to the coordinate variable. */ time_count[0] = 1; time_count[1] = TEXT_LEN; if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR; /* Define two data variable. */ if (nc_def_var(ncid, PRES_NAME, NC_CHAR, DATA_NDIMS, dimids, &pres_id)) ERR; if (nc_def_var(ncid, TEMP_NAME, NC_CHAR, DATA_NDIMS, dimids, &temp_id)) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Check the metadata. */ if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != NVARS || ndims != NDIMS || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != NVARS || varids_in[0] != 0 || varids_in[1] != 1 || varids_in[2] != 2 || varids_in[3] != 3) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, TIME_NAME) || xtype_in != NC_CHAR || ndims_in != TIME_NDIMS || dimids_in[0] != time_dimids[0] || dimids_in[1] != time_dimids[1] || natts_in != 0) ERR; if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; if (ndims_in != NDIMS) ERR; for (d = 0; d < NDIMS; d++) if (dimids_in[d] != dimids[d]) ERR; if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LON_NAME) || len_in != LON_LEN) ERR; if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LAT_NAME) || len_in != LAT_LEN) ERR; if (nc_inq_dim(ncid, 2, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LVL_NAME) || len_in != LVL_LEN) ERR; if (nc_inq_dim(ncid, 3, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TIME_NAME) || len_in != TIME_LEN) ERR; if (nc_inq_dim(ncid, 4, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TEXT_LEN_NAME) || len_in != TEXT_LEN) ERR; /* Close up. */ if (nc_close(ncid)) ERR; /* Open the file and check the order of variables and dimensions. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != NVARS || ndims != NDIMS || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != NVARS || varids_in[0] != 0 || varids_in[1] != 1 || varids_in[2] != 2 || varids_in[3] != 3) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, TIME_NAME) || xtype_in != NC_CHAR || ndims_in != TIME_NDIMS || dimids_in[0] != time_dimids[0] || dimids_in[1] != time_dimids[1] || natts_in != 0) ERR; if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; if (ndims_in != NDIMS) ERR; for (d = 0; d < NDIMS; d++) if (dimids_in[d] != dimids[d]) ERR; if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LON_NAME) || len_in != LON_LEN) ERR; if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LAT_NAME) || len_in != LAT_LEN) ERR; if (nc_inq_dim(ncid, 2, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LVL_NAME) || len_in != LVL_LEN) ERR; if (nc_inq_dim(ncid, 3, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TIME_NAME) || len_in != TIME_LEN) ERR; if (nc_inq_dim(ncid, 4, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TEXT_LEN_NAME) || len_in != TEXT_LEN) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Close up. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing example from bug NCF-247, multidimensional coord variable in subgroup ..."); { #define GRPNAME "g1" #define DIM0NAME "dim0" #define DIM1NAME "dim1" #define DIM2NAME "coord" #define DIM3NAME "dim3" #define VARNAME DIM2NAME #define VARRANK 2 int ncid, grpid, varid, var_dims[VARRANK], var_dims_in[VARRANK]; char name2[NC_MAX_NAME], name3[NC_MAX_NAME]; int dim0_dim, dim1_dim, dim2_dim, dim3_dim; size_t dim0_len = 3, dim1_len = 2, dim2_len = 5, dim3_len = 7; if (nc_create(FILE_NAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, GRPNAME, &grpid)) ERR; if (nc_def_dim(ncid, DIM0NAME, dim0_len, &dim0_dim)) ERR; if (nc_def_dim(ncid, DIM1NAME, dim1_len, &dim1_dim)) ERR; if (nc_def_dim(grpid, DIM2NAME, dim2_len, &dim2_dim)) ERR; if (nc_def_dim(grpid, DIM3NAME, dim3_len, &dim3_dim)) ERR; var_dims[0] = dim2_dim; var_dims[1] = dim3_dim; if (nc_def_var(grpid, VARNAME, NC_INT, VARRANK, var_dims, &varid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_grp_ncid(ncid, GRPNAME, &grpid)) ERR; if (nc_inq_varid(grpid, VARNAME, &varid)) ERR; if (nc_inq_vardimid(grpid, varid, var_dims_in)) ERR; if (nc_inq_dimname(grpid, var_dims_in[0], name2)) ERR; if (nc_inq_dimname(grpid, var_dims_in[1], name3)) ERR; if (strcmp(name2, DIM2NAME) != 0 || strcmp(name3, DIM3NAME) != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }