/* Determine whether a group named formatting_specs.lgrps[igrp] exists * in a netCDF file or group with id ncid. If so, return the count of * how many matching groups were found, else return a count of 0. If * the name begins with "/", it is interpreted as an absolute group * name, in which case only 0 or 1 is returned. Otherwise, interpret * it as a relative name, and the total number of occurrences within * the file/group identified by ncid is returned. * * Also has side effect of updating the ngrpids and the associate * grpids array that represent the group list specified by the -g * option. TODO: put this in its own function instead. */ static size_t nc_inq_grpname_count(int ncid, int igrp, char **lgrps, idnode_t *grpids) { size_t count = 0; #ifdef USE_NETCDF4 int numgrps; int *ncids; int g; int grpid; int status; #endif char *grpname = lgrps[igrp]; /* permit empty string to also designate root group */ if(grpname[0] == '\0' || STREQ(grpname,"/")) { count = 1; idadd(grpids, ncid); return count; } #ifdef USE_NETCDF4 /* Handle absolute group names */ if(grpname[0] == '/') { int grpid; status = nc_inq_grp_full_ncid(ncid, grpname, &grpid); if(status == NC_NOERR) { count = 1; idadd(grpids, grpid); } else if(status == NC_ENOGRP) { count = 0; } else { error("when looking up group %s: %s ", grpname, nc_strerror(status)); } return count; } /* look in this group */ status = nc_inq_grp_ncid(ncid, grpname, &grpid); if (status == NC_NOERR) { count++; idadd(grpids, grpid); } /* if this group has subgroups, call recursively on each of them */ NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); if(numgrps > 0) { /* Allocate memory to hold the list of group ids. */ ncids = emalloc(numgrps * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); /* Call this function recursively for each group. */ for (g = 0; g < numgrps; g++) { count += nc_inq_grpname_count(ncids[g], igrp, lgrps, grpids); } free(ncids); } #endif /* USE_NETCDF4 */ return count; }
/** * Given a file or group 'parent' id, return the number of child groups and the ids * of the child groups below the parent. If num_groups is NULL, do not return * count; if group_ids is NULL, do not return ids. */ int ex_get_group_ids (int parent_id, int *num_groups, int *group_ids) { 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_get_group_ids",errmsg,exerrval); return (EX_FATAL); #else status = nc_inq_grps(parent_id, num_groups, group_ids); if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: Failed to get child group ids in file id %d", parent_id); ex_err("ex_get_group_ids",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); #endif }
/* Inputs: root_id: netcdfID of the root group Outputs: gids: IDs of the groups below the root group, must already be allocated! ierr: 0 on success, otherwise an error was encountered */ void R_nc4_inq_groupids( int *root_id, int *gids, int *ierr ) { *ierr = nc_inq_grps( *root_id, NULL, gids ); if( *ierr != NC_NOERR ) { Rprintf( "Error in R_nc4_inq_groupids: %s\n", nc_strerror(*ierr) ); } }
/* Inputs: root_id: netcdfID of the root group Outputs: ngroups: How many groups are BELOW the specified root group ierr: 0 on success, otherwise an error was encountered */ void R_nc4_inq_ngroups( int *root_id, int *ngroups, int *ierr ) { *ierr = nc_inq_grps( *root_id, ngroups, NULL ); if( *ierr != NC_NOERR ) { Rprintf( "Error in R_nc4_inq_ngroups: %s\n", nc_strerror(*ierr) ); } }
/* Determine whether a variable named varname exists in any group in an open netCDF file with id ncid. If so, return the count of how many matching variables were found, else return a count of 0. The variable name can be absolute such as "/foo" or "/GRP1/GRP1A/foo", in which case there is only one group to look in, given by the path from the root group. Alternatively, the variable name can be relative, such as "foo" or "GRPA/GRPB/foo", in which case every group is examined for a variable with that relative name. */ size_t nc_inq_varname_count(int ncid, char *varname) { /* count = 0; status = nc_inq_gvarid(ncid, varname, varid); if (status == NC_NOERR) count++; for each subgroup gid { count += nc_inq_varname_count(gid, varname); } return count; */ size_t count = 0; int varid; /* look in this group */ int status = nc_inq_gvarid(ncid, varname, &varid); #ifdef USE_NETCDF4 int numgrps; int *ncids; int g; #endif if (status == NC_NOERR) count++; #ifdef USE_NETCDF4 /* if this group has subgroups, call recursively on each of them */ NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ); /* Allocate memory to hold the list of group ids. */ ncids = emalloc((numgrps + 1) * sizeof(int)); /* Get the list of group ids. */ NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ); /* Call this function for each group. */ for (g = 0; g < numgrps; g++) { count += nc_inq_varname_count(ncids[g], varname); } free(ncids); #endif /* USE_NETCDF4 */ return count; }
/* return number of user-defined types in a group and all its subgroups */ static int count_udtypes(int ncid) { int ntypes = 0; int numgrps; int *ncids; int i; int format; if( nc_inq_format(ncid, &format) ) ERR; if (format == NC_FORMAT_NETCDF4) { /* Get number of types in this group */ if( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR; if( nc_inq_grps(ncid, &numgrps, NULL) ) ERR; ncids = (int *) malloc(sizeof(int) * numgrps); if( nc_inq_grps(ncid, NULL, ncids) ) ERR; /* Add number of types in each subgroup, if any */ for (i=0; i < numgrps; i++) { ntypes += count_udtypes(ncids[i]); } } return ntypes; }
/* Like netCDF-4 function nc_inq_grps(), but can be called from * netCDF-3 only code as well. Maybe this is what nc_inq_grps() * should do if built without netCDF-4 data model support. */ static int nc_inq_grps2(int ncid, int *numgrps, int *grpids) { int stat = NC_NOERR; /* just check if ncid is valid id of open netCDF file */ NC_CHECK(nc_inq(ncid, NULL, NULL, NULL, NULL)); #ifdef USE_NETCDF4 NC_CHECK(nc_inq_grps(ncid, numgrps, grpids)); #else *numgrps = 0; #endif return stat; }
/*! This function queries the list of group ids given a location id. \param [in] ncid Groupd id (or File Id) \param [in/out] numgrps number of groups \param [in/out] ncids list of group ids \return Status code */ int CNetCdfInterface::inqGrpIds(int ncid, int& numgrps, int* ncids) { int status = nc_inq_grps(ncid, &numgrps, ncids); if (NC_NOERR != status) { StdString errormsg(nc_strerror(status)); StdStringStream sstr; sstr << "Error when calling function nc_inq_grps(ncid, &numgrps, ncids)" << std::endl; sstr << errormsg << std::endl; sstr << "Unable to retrieve the list of groups for location id: " << ncid << std::endl; StdString e = sstr.str(); throw CNetCdfException(e); } return status; }
int main(int argc, char **argv) {/* create file that caused seg fault in ncdump */ int ncid; /* netCDF id */ /* dimension ids */ int Time_dim; int X_dim; int Y_dim; /* dimension lengths */ size_t Time_len = NC_UNLIMITED; size_t X_len = 4; size_t Y_len = 3; /* variable ids */ int Time_id; int P_id; /* rank (number of dimensions) for each variable */ # define RANK_Time 1 # define RANK_P 3 /* variable shapes */ int Time_dims[RANK_Time]; int P_dims[RANK_P]; printf("\n*** Testing preparation of fillbug test.\n"); printf("*** creating fillbug test file %s...", FILENAME); /* enter define mode */ if (nc_create(FILENAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "Time", Time_len, &Time_dim)) ERR; if (nc_def_dim(ncid, "X", X_len, &X_dim)) ERR; if (nc_def_dim(ncid, "Y", Y_len, &Y_dim)) ERR; /* define variables */ Time_dims[0] = Time_dim; if (nc_def_var(ncid, "Time", NC_DOUBLE, RANK_Time, Time_dims, &Time_id)) ERR; P_dims[0] = Time_dim; P_dims[1] = Y_dim; P_dims[2] = X_dim; if (nc_def_var(ncid, "P", NC_FLOAT, RANK_P, P_dims, &P_id)) ERR; /* leave define mode */ if (nc_enddef (ncid)) ERR; {/* assign variable data */ static double Time_data[1]={3.14159}; static size_t Time_startset[1] = {0}; static size_t Time_countset[1] = {1}; if (nc_put_vara(ncid, Time_id, Time_startset, Time_countset, Time_data)) ERR; } if (nc_close(ncid)) ERR; /* Try to duplicate segfault ncdump gets by making the same calls * to the netCDF-4 library, in the same order. This doesn't * result in the same segfault, so either we have missed a call * made by ncdump, or an earlier ncdump bug masks the real problem * until a call is made into the netCDF-4 library ... */ if (nc_open(FILENAME, NC_NOWRITE, &ncid)) ERR; { /* We declare local arrays with small constant sizes to avoid * all the mallocs and frees used in ncdump. For the example * above, the fixed-size arrays are ample. */ int format, ndims, nvars, ngatts, xdimid, ndims_grp, dimids_grp[3], unlimids[1], d_grp, nunlim, nvars_grp, varids_grp[3], v_grp, varid, varndims, vardims[3], varnatts, vartype, dimids[3], is_recvar, vdims[3], id, ntypes, numgrps, atttype, nc_status; size_t dimsize, len, attlen; char dimname[20], varname[20]; if ( nc_inq_format(ncid, &format)) ERR; ntypes = count_udtypes(ncid); if ( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR; if ( nc_inq_format(ncid, &format)) ERR; if ( nc_inq_grps(ncid, &numgrps, NULL) ) ERR; if ( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR; if ( nc_inq(ncid, &ndims, &nvars, &ngatts, &xdimid) ) ERR; if ( nc_inq_ndims(ncid, &ndims_grp) ) ERR; if ( nc_inq_dimids(ncid, 0, dimids_grp, 0) ) ERR; if ( nc_inq_unlimdims(ncid, &nunlim, NULL) ) ERR; if ( nc_inq_unlimdims(ncid, &nunlim, unlimids) ) ERR; for (d_grp = 0; d_grp < ndims_grp; d_grp++) { int dimid = dimids_grp[d_grp]; if ( nc_inq_dim(ncid, dimid, dimname, &dimsize) ) ERR; } if ( nc_inq_format(ncid, &format) ) ERR; if ( nc_inq_varids(ncid, &nvars_grp, varids_grp) ) ERR; for (v_grp = 0; v_grp < nvars_grp; v_grp++) { varid = varids_grp[v_grp]; if ( nc_inq_varndims(ncid, varid, &varndims) ) ERR; if ( nc_inq_var(ncid, varid, varname, &vartype, 0, vardims, &varnatts) ) ERR; for (id = 0; id < varndims; id++) { if ( nc_inq_dimname(ncid, vardims[id], dimname) ) ERR; } } for (v_grp = 0; v_grp < nvars_grp; v_grp++) { varid = varids_grp[v_grp]; if( nc_inq_varndims(ncid, varid, &varndims) ) ERR; if( nc_inq_var(ncid, varid, varname, &vartype, 0, vardims, &varnatts) ) ERR; { is_recvar = 0; if ( nc_inq_varndims(ncid, varid, &ndims) ) ERR; if (ndims > 0) { int nunlimdims; int recdimids[3]; int dim, recdim; if ( nc_inq_vardimid(ncid, varid, dimids) ) ERR; if ( nc_inq_unlimdims(ncid, &nunlimdims, NULL) ) ERR; if ( nc_inq_unlimdims(ncid, NULL, recdimids) ) ERR; for (dim = 0; dim < ndims && is_recvar == 0; dim++) { for(recdim = 0; recdim < nunlimdims; recdim++) { if(dimids[dim] == recdimids[recdim]) { is_recvar = 1; break; } } } } } for (id = 0; id < varndims; id++) { if( nc_inq_dimlen(ncid, vardims[id], &len) ) ERR; vdims[id] = len; } nc_status = nc_inq_att(ncid,varid,_FillValue,&atttype,&attlen); nc_status = nc_inq_att(ncid, varid, "units", &atttype, &attlen); nc_status = nc_inq_att(ncid, varid, "C_format", &atttype, &attlen); if (varid == 0) { /* read Time variable */ static double Time_data; static size_t cor[RANK_Time] = {0}; static size_t edg[RANK_Time] = {1}; if (nc_get_vara(ncid, varid, cor, edg, &Time_data)) ERR; } else { /* read data slices from P variable, should get fill values */ static float P_data[4]; static size_t cor[RANK_P] = {0, 0, 0}; static size_t edg[RANK_P] = {1, 1, 4}; /* first slice retrieved OK */ if (nc_get_vara(ncid, varid, cor, edg, P_data)) ERR; /* In ncdump, reading second slice gets seg fault in * nc4_open_var_grp(), but this attempt to do all the * same netCDF calls as ncdump can't duplicate the * error, which would seem to implicate ncdump rather * than HDF5 or netCDF-4 library ... */ cor[1] = 1; if (nc_get_vara(ncid, varid, cor, edg, P_data)) ERR; } } } if (nc_close(ncid)) ERR; SUMMARIZE_ERR; FINAL_RESULTS; }
int main(int argc, char **argv) { printf("\n*** Testing HDF5/NetCDF-4 interoperability...\n"); printf("*** testing HDF5 compatibility..."); { #define GRPA_NAME "grpa" #define VAR_NAME "vara" #define NDIMS 2 int nrowCur = 7; /* current size */ int ncolCur = 3; int nrowMax = nrowCur + 0; /* maximum size */ int ncolMax = ncolCur + 0; hid_t xdimId; hid_t ydimId; hsize_t xscaleDims[1]; hsize_t yscaleDims[1]; hid_t xdimSpaceId, spaceId; hid_t fileId; hid_t fapl; hsize_t curDims[2]; hsize_t maxDims[2]; hid_t dataTypeId, dsPropertyId, grpaId, grpaPropId, dsId; hid_t ydimSpaceId; const char * dimNameBase = "This is a netCDF dimension but not a netCDF variable."; char dimNameBuf[1000]; char *varaName = "/grpa/vara"; short amat[nrowCur][ncolCur]; int ii, jj; xscaleDims[0] = nrowCur; yscaleDims[0] = ncolCur; if ((xdimSpaceId = H5Screate_simple(1, xscaleDims, NULL)) < 0) ERR; /* With the SEMI close degree, the HDF5 file close will fail if * anything is left open. */ if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if (H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI)) ERR; /* Create file */ if((fileId = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5Pcreate(H5P_FILE_CREATE), fapl)) < 0) ERR; if (H5Pclose(fapl) < 0) ERR; /* Create data space */ curDims[0] = nrowCur; curDims[1] = ncolCur; maxDims[0] = nrowMax; maxDims[1] = ncolMax; if ((spaceId = H5Screate_simple(2, curDims, maxDims)) < 0) ERR; if ((dataTypeId = H5Tcopy(H5T_NATIVE_SHORT)) < 0) ERR; if ((dsPropertyId = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if ((grpaPropId = H5Pcreate(H5P_GROUP_CREATE)) < 0) ERR; if ((grpaId = H5Gcreate2(fileId, GRPA_NAME, H5P_DEFAULT, grpaPropId, H5P_DEFAULT)) < 0) ERR; if (H5Pclose(grpaPropId) < 0) ERR; /* Create vara dataset */ if ((dsId = H5Dcreate2(fileId, varaName, dataTypeId, spaceId, H5P_DEFAULT, dsPropertyId, H5P_DEFAULT)) < 0) ERR; if (H5Pclose(dsPropertyId) < 0) ERR; if (H5Tclose(dataTypeId) < 0) ERR; if ((ydimSpaceId = H5Screate_simple(1, yscaleDims, NULL)) < 0) ERR; /* Create xdim dimension dataset */ if ((xdimId = H5Dcreate2(fileId, "/xdim", H5T_IEEE_F32BE, xdimSpaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if (H5Sclose(xdimSpaceId) < 0) ERR; /* Create ydim dimension dataset */ if ((ydimId = H5Dcreate2(fileId, "/ydim", H5T_IEEE_F32BE, ydimSpaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if (H5Sclose(ydimSpaceId) < 0) ERR; /* Create xdim scale */ sprintf(dimNameBuf, "%s%10d", dimNameBase, nrowCur); if (H5DSset_scale(xdimId, dimNameBuf) < 0) ERR; /* Create ydim scale */ sprintf(dimNameBuf, "%s%10d", dimNameBase, ncolCur); if (H5DSset_scale(ydimId, dimNameBuf) < 0) ERR; /* Attach dimension scales to the dataset */ if (H5DSattach_scale(dsId, xdimId, 0) < 0) ERR; if (H5DSattach_scale(dsId, ydimId, 1) < 0) ERR; /* Close stuff. */ if (H5Dclose(xdimId) < 0) ERR; if (H5Dclose(ydimId) < 0) ERR; if (H5Dclose(dsId) < 0) ERR; if (H5Gclose(grpaId) < 0) ERR; if (H5Sclose(spaceId) < 0) ERR; if (H5Fclose(fileId) < 0) ERR; /* Create some data */ for (ii = 0; ii < nrowCur; ii++) for (jj = 0; jj < ncolCur; jj++) amat[ii][jj] = 100 * ii + jj; /* Re-open file */ if ((fileId = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpaId = H5Gopen2(fileId, GRPA_NAME, H5P_DEFAULT)) < 0) ERR; if ((dsId = H5Dopen2(grpaId, varaName, H5P_DEFAULT)) < 0) ERR; /* Write dataset */ if (H5Dwrite(dsId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, amat) < 0) ERR; /* Write dimension values for both xdim, ydim */ { short xydimMat[ nrowCur >= ncolCur ? nrowCur : ncolCur]; for (ii = 0; ii < nrowCur; ii++) xydimMat[ii] = 0; /*#### 100 * ii; */ /* Write xdim */ if ((xdimId = H5Dopen2(fileId, "/xdim", H5P_DEFAULT)) < 0) ERR; if (H5Dwrite(xdimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, xydimMat) < 0) ERR; if (H5Dclose(xdimId) < 0) ERR; /* Write ydim */ if ((ydimId = H5Dopen2(fileId, "/ydim", H5P_DEFAULT)) < 0) ERR; if (H5Dwrite(ydimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, xydimMat) < 0) ERR; if (H5Dclose(ydimId) < 0) ERR; } if (H5Dclose(dsId) < 0) ERR; if (H5Gclose(grpaId) < 0) ERR; if (H5Fclose(fileId) < 0) ERR; { int ncid, grpid, nvars, ngatts, ndims, unlimdimid, ngrps; char name_in[NC_MAX_NAME + 1]; nc_type xtype_in; int ndims_in, natts_in, dimid_in[NDIMS]; /* nc_set_log_level(5);*/ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 0 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_grps(ncid, &ngrps, &grpid)) ERR; if (ngrps != 1) ERR; if (nc_inq(grpid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_var(grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in, &natts_in)) ERR; if (strcmp(name_in, VAR_NAME) || xtype_in != NC_SHORT || ndims_in != NDIMS || dimid_in[0] != 0 || dimid_in[1] != 1 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } } SUMMARIZE_ERR; #ifdef USE_SZIP printf("*** testing HDF5 compatibility with szip..."); { #define DEFLATE_LEVEL 9 #define MAX_NAME 100 #define NUM_CD_ELEM 10 /* HDF5 defines this... */ #define DEFLATE_NAME "deflate" #define DIM1_LEN 3000 #define GRP_NAME "George_Washington" #define BATTLE_RECORD "Battle_Record" hid_t fileid, grpid, spaceid, datasetid; int data_out[DIM1_LEN], data_in[DIM1_LEN]; hsize_t dims[1] = {DIM1_LEN}; hid_t propid; char name_in[MAX_NAME + 1]; int ncid, ndims_in, nvars_in, ngatts_in, unlimdimid_in, ngrps_in; int nc_grpid; int dimid_in[1], natts_in; nc_type xtype_in; int i; for (i = 0; i < DIM1_LEN; i++) data_out[i] = i; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Write an array of bools, with szip compression. */ if ((propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_layout(propid, H5D_CHUNKED)) ERR; if (H5Pset_chunk(propid, 1, dims)) ERR; if (H5Pset_szip(propid, H5_SZIP_EC_OPTION_MASK, 32)) ERR; if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; if ((datasetid = H5Dcreate(grpid, BATTLE_RECORD, H5T_NATIVE_INT, spaceid, propid)) < 0) ERR; if (H5Dwrite(datasetid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_out) < 0) ERR; if (H5Dclose(datasetid) < 0 || H5Pclose(propid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file with netCDF and check it. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR; if (ndims_in != 0 || nvars_in != 0 || ngatts_in != 0 || unlimdimid_in != -1) ERR; if (nc_inq_grps(ncid, &ngrps_in, &nc_grpid)) ERR; if (ngrps_in != 1) ERR; if (nc_inq(nc_grpid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR; if (ndims_in != 1 || nvars_in != 1 || ngatts_in != 0 || unlimdimid_in != -1) ERR; /* Check the variable. */ if (nc_inq_var(nc_grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in, &natts_in)) ERR; if (strcmp(name_in, BATTLE_RECORD) || xtype_in != NC_INT || ndims_in != 1 || dimid_in[0] != 0 || natts_in != 0) ERR; /* Check the data. */ if (nc_get_var(nc_grpid, 0, data_in)) ERR; for (i = 0; i < DIM1_LEN; i++) if (data_in[i] != data_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #endif /* USE_SZIP */ FINAL_RESULTS; }
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; }
/* Given an ncid, check the file to make sure it has all the objects I * expect. */ int check_nc4_file(int ncid) { int varid, dimid, attnum, grpid, grpid2, grpid3, numgrps; int numtypes, enum_typeid, comp_typeid; int class_in; size_t att_len, size_in, num_mem, nfields_in; nc_type att_type, base_type_in; char name_in[NC_MAX_NAME + 1], strings_in[NC_MAX_NAME + 1], value; /* Check the group. */ if (nc_inq_grps(ncid, &numgrps, &grpid)) ERR; if (numgrps != 1) ERR; name_in[0] = 0; if (nc_inq_grpname(grpid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; /* Check the variable. */ if (nc_inq_varid(grpid, name_utf8, &varid)) ERR; if (nc_inq_varname(grpid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_varid(grpid, norm_utf8, &varid)) ERR; name_in[0] = 0; if (nc_inq_varname(grpid, varid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_get_var(grpid, varid, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; strings_in[0] = '\0'; /* Reset my string buffer. */ /* Check the dimension. */ if (nc_inq_dimid(grpid, name_utf8, &dimid)) ERR; if (nc_inq_dimname(grpid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; if (nc_inq_dimid(grpid, norm_utf8, &dimid)) ERR; if (nc_inq_dimname(grpid, dimid, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; /* Check the attribute. We don't normalize data or attribute * values, so get exactly what was put for the value, but * normalized values for names. */ if (nc_inq_attid(grpid, varid, norm_utf8, &attnum)) ERR; if (attnum) ERR; attnum = 99; /* Reset. */ if (nc_inq_attid(grpid, varid, name_utf8, &attnum)) ERR; if (attnum) ERR; if (nc_inq_att(grpid, varid, norm_utf8, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != sizeof(name_utf8)) ERR; if (nc_get_att_text(grpid, varid, norm_utf8, strings_in)) ERR; if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR; /* Check the enum type. */ if (nc_inq_grps(grpid, &numgrps, &grpid2)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(grpid2, &numtypes, &enum_typeid)) ERR; if (numtypes != 1) ERR; if (nc_inq_user_type(grpid2, enum_typeid, name_in, &size_in, &base_type_in, &nfields_in, &class_in)) ERR; if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 || base_type_in != NC_BYTE || nfields_in != 1 || class_in != NC_ENUM) ERR; name_in[0] = size_in = base_type_in = 0; if (nc_inq_enum(grpid2, enum_typeid, name_in, &base_type_in, &size_in, &num_mem)) ERR; if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 || base_type_in != NC_BYTE || num_mem != 1) ERR; if (nc_inq_enum_member(grpid2, enum_typeid, 0, name_in, &value)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || value != ENUM_VALUE) ERR; /* Check the compound type. */ if (nc_inq_grps(grpid2, &numgrps, &grpid3)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(grpid3, &numtypes, &comp_typeid)) ERR; if (numtypes != 1) ERR; name_in[0] = 0; if (nc_inq_user_type(grpid3, comp_typeid, name_in, &size_in, &base_type_in, &nfields_in, &class_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) || base_type_in != NC_NAT || nfields_in != 1 || class_in != NC_COMPOUND) ERR; size_in = nfields_in = 999; if (nc_inq_compound(grpid3, comp_typeid, name_in, &size_in, &nfields_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) || nfields_in != 1) ERR; name_in[0] = 0; if (nc_inq_compound_fieldname(grpid3, comp_typeid, 0, name_in)) ERR; if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR; return NC_NOERR; }
int main(int argc, char **argv) { int ncid, dimids[NUM_DIMS]; int varid; int nvars_in, varids_in[NUM_DIMS]; signed char fill_value = 42, fill_value_in; nc_type xtype_in; size_t len_in; char name_in[NC_MAX_NAME + 1]; int attnum_in; int cnum; #ifdef USE_PARALLEL MPI_Init(&argc, &argv); #endif printf("\n*** Testing netcdf-4 variable functions, even more.\n"); for (cnum = 0; cnum < MAX_CNUM; cnum++) { int cmode; switch(cnum) { case 0: printf("*** Testing with classic format:\n"); cmode = 0; break; case 1: printf("*** Testing with 64-bit offset format:\n"); cmode = NC_64BIT_OFFSET; break; case 2: printf("*** Testing with HDF5:\n"); cmode = NC_NETCDF4|NC_CLOBBER; break; case 3: printf("*** Testing with HDF5, netCDF Classic Model:\n"); cmode = NC_CLASSIC_MODEL | NC_NETCDF4; } printf("**** testing simple fill value attribute creation..."); { /* Create a netcdf-4 file with one scalar var. Add fill * value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, 0, NULL, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, VAR_NAME)) ERR; if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing simple fill value with data read..."); { size_t start[NUM_DIMS], count[NUM_DIMS]; signed char data = 99, data_in; /* Create a netcdf-4 file with one unlimited dim and one * var. Add fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_enddef(ncid)) ERR; /* Write the second record. */ start[0] = 1; count[0] = 1; if (nc_put_vara_schar(ncid, varid, start, count, &data)) ERR; /* Read the first record, it should be the fill value. */ start[0] = 0; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != fill_value) ERR; /* Read the second record, it should be the value we just wrote * there. */ start[0] = 1; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != data) ERR; /* Close up. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check metadata. */ if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, VAR_NAME)) ERR; /* Check fill value att. */ if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; /* Read the first record, it should be the fill value. */ start[0] = 0; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != fill_value) ERR; /* Read the second record, it should be the value we just wrote * there. */ start[0] = 1; if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with one other attribute..."); { int losses_value = 192, losses_value_in; /* Create a netcdf-4 file with one dim and one var. Add another * attribute, then fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, LOSSES_NAME, NC_INT, 1, &losses_value)) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_att(ncid, 0, LOSSES_NAME, &xtype_in, &len_in)) ERR; if (xtype_in != NC_INT || len_in != 1) ERR; if (nc_get_att(ncid, 0, LOSSES_NAME, &losses_value_in)) ERR; if (losses_value_in != losses_value) ERR; if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_inq_attid(ncid, 0, LOSSES_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with three other attributes..."); { #define NUM_LEADERS 3 char leader[NUM_LEADERS][NC_MAX_NAME + 1] = {"hair_length_of_strategoi", "hair_length_of_Miltiades", "hair_length_of_Darius_I"}; short hair_length[NUM_LEADERS] = {3, 11, 4}; short short_in; int a; /* Create a netcdf file with one dim and one var. Add 3 * attributes, then fill value. */ if (nc_create(FILE_NAME, cmode, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR; for (a = 0; a < NUM_LEADERS; a++) if (nc_put_att_short(ncid, varid, leader[a], NC_SHORT, 1, &hair_length[a])) ERR; if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR; if (nc_close(ncid)) ERR; /* Open the file. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check our three hair-related attributes. */ for (a = 0; a < NUM_LEADERS; a++) { if (nc_inq_att(ncid, 0, leader[a], &xtype_in, &len_in)) ERR; if (xtype_in != NC_SHORT || len_in != 1) ERR; if (nc_get_att(ncid, 0, leader[a], &short_in)) ERR; if (short_in != hair_length[a]) ERR; if (nc_inq_attid(ncid, 0, leader[a], &attnum_in)) ERR; if (attnum_in != a) ERR; } /* Check our fill value attribute. */ if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR; if (xtype_in != NC_BYTE || len_in != 1) ERR; if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR; if (fill_value_in != fill_value) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing fill value with simple example..."); { /* Dims stuff. */ #define NDIMS 3 #define VAR_DIMS 3 #define DIM_A "dim1" #define DIM_A_LEN 4 #define DIM_B "dim2" #define DIM_B_LEN 3 #define DIM_C "dim3" #define DIM_C_LEN NC_UNLIMITED /* Var stuff. */ #define CXX_VAR_NAME "P" /* Att stuff. */ #define NUM_ATTS 4 #define LONG_NAME "long_name" #define PRES_MAX_WIND "pressure at maximum wind" #define UNITS "units" #define HECTOPASCALS "hectopascals" int dimid[NDIMS], var_dimids[VAR_DIMS] = {2, 1, 0}; float fill_value = -9999.0f; char long_name[] = PRES_MAX_WIND; if (nc_create(FILE_NAME, cmode, &ncid)) ERR; /* Create dims. */ if (nc_def_dim(ncid, DIM_A, DIM_A_LEN, &dimid[0])) ERR; if (nc_def_dim (ncid, DIM_B, DIM_B_LEN, &dimid[1])) ERR; if (nc_def_dim(ncid, DIM_C, DIM_C_LEN, &dimid[2])) ERR; /* Create var. */ if (nc_def_var(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids, &varid)) ERR; if (varid) ERR; if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1, long_name)) ERR; if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1, UNITS)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; /* Now add a fill value. This will acutually cause HDF5 to * destroy the dataset and recreate it, recreating also the * three attributes that are attached to it. */ if (nc_put_att(ncid, varid, _FillValue, NC_FLOAT, 1, &fill_value)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 2) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #ifndef NO_NETCDF_2 /* The following test is an attempt to recreate a problem occuring in the cxx tests. The file is created in c++ in nctsts.cpp. */ printf("**** testing fill value with example from cxx tests in v2 api..."); { /* Dims stuff. */ #define NDIMS_1 4 #define VAR_DIMS 3 #define LAT "lat" #define LAT_LEN 4 #define LON "lon" #define LON_LEN 3 #define FRTIMED "frtimed" #define FRTIMED_LEN NC_UNLIMITED #define TIMELEN "timelen" #define TIMELEN_LEN 20 /* Var stuff. */ #define CXX_VAR_NAME "P" /* Att stuff. */ #define NUM_ATTS 4 #define LONG_NAME "long_name" #define UNITS "units" int dimid[NDIMS_1], var_dimids[VAR_DIMS] = {2, 0, 1}; float fill_value = -9999.0f; char long_name[] = PRES_MAX_WIND; int i, attid[NUM_ATTS]; ncid = nccreate(FILE_NAME, NC_NETCDF4); /* Create dims. */ dimid[0] = ncdimdef(ncid, LAT, LAT_LEN); dimid[1] = ncdimdef(ncid, LON, LON_LEN); dimid[2] = ncdimdef(ncid, FRTIMED, FRTIMED_LEN); dimid[3] = ncdimdef(ncid, TIMELEN, TIMELEN_LEN); /* Just check our dimids to see that they are correct. */ for (i = 0; i < NDIMS_1; i++) if (dimid[i] != i) ERR; /* Create var. */ varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids); if (varid) ERR; /* Add three atts to the var, long_name, units, and * valid_range. */ if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1, long_name)) ERR; if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1, UNITS)) ERR; /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; /* Now add a fill value. This will acutually cause HDF5 to * destroy the dataset and recreate it, recreating also the * three attributes that are attached to it. */ attid[3] = ncattput(ncid, varid, _FillValue, NC_FLOAT, 1, &fill_value); /* Check to ensure the atts have their expected attnums. */ if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; ncclose(ncid); /* Open the file and check. */ ncid = ncopen(FILE_NAME, 0); if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR; if (attnum_in != 0) ERR; if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR; if (attnum_in != 1) ERR; if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR; if (attnum_in != 2) ERR; ncclose(ncid); } SUMMARIZE_ERR; #endif /* NO_NETCDF_2 */ } printf("**** testing create order varids..."); #define UNITS "units" #define DIMNAME "x" #define VARNAME "data" { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, dimid, varid, xvarid; char units[] = "zlotys"; if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIMNAME, 1, &dimid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_def_var(ncid, DIMNAME, NC_INT, 1, &dimid, &xvarid)) ERR; if (nc_put_att_text(ncid, xvarid, UNITS, strlen(units), units)) ERR; if (nc_def_var(ncid, VARNAME, NC_INT, 1, &dimid, &varid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #define RANK_wind 1 printf("**** testing simple variable renaming..."); { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, lat_dim, time_dim, lon_dim, wind_id; size_t lat_len = 73, time_len = 10, lon_len = 145; int cdf_goober[1]; /* if (nc_set_default_format(NC_FORMAT_NETCDF4, NULL)) ERR;*/ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "a", lon_len, &lon_dim)) ERR; if (nc_def_dim(ncid, "b", lat_len, &lat_dim)) ERR; if (nc_def_dim(ncid, "c", time_len, &time_dim)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, "a", 3, "bar")) ERR; cdf_goober[0] = 2; if (nc_put_att_int(ncid, NC_GLOBAL, "b", NC_INT, 1, cdf_goober)) ERR; /* define variables */ if (nc_def_var(ncid, "aa", NC_FLOAT, RANK_wind, &lon_dim, &wind_id)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_rename_var(ncid, 0, "az")) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing dimension and variable renaming..."); { /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */ int ncid, lat_dim, time_dim, lon_dim, wind_id; size_t lat_len = 73, time_len = 10, lon_len = 145; int wind_dims[RANK_wind], wind_slobber[1], cdf_goober[1]; if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "lon", lon_len, &lon_dim)) ERR; if (nc_def_dim(ncid, "lat", lat_len, &lat_dim)) ERR; if (nc_def_dim(ncid, "time", time_len, &time_dim)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, "foo", 3, "bar")) ERR; cdf_goober[0] = 2; if (nc_put_att_int(ncid, NC_GLOBAL, "goober", NC_INT, 1, cdf_goober)) ERR; /* define variables */ wind_dims[0] = lon_dim; if (nc_def_var(ncid, "temp", NC_FLOAT, RANK_wind, wind_dims, &wind_id)) ERR; if (nc_put_att_text(ncid, wind_id, "bar", 3, "foo")) ERR; wind_slobber[0] = 3; if (nc_put_att_int(ncid, wind_id, "slobber", NC_INT, 1, wind_slobber)) ERR; if (nc_close(ncid)) ERR; /* re-open dataset*/ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_dimid(ncid, "lon", &lon_dim)) ERR; /* rename dimension */ if (nc_rename_dim(ncid, lon_dim, "longitude")) ERR; if (nc_inq_varid(ncid, "temp", &wind_id)) ERR; /* rename variable */ if (nc_rename_var(ncid, wind_id, "wind")) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; /* printf("*** testing 2D array of NC_CHAR..."); */ /* { */ /* int dimid[NDIMS_1], var_dimids[VAR_DIMS] = {2, 0, 1}; */ /* float fill_value = -9999.0f; */ /* char long_name[] = PRES_MAX_WIND; */ /* int i, attid[NUM_ATTS]; */ /* ncid = nccreate(FILE_NAME, NC_NETCDF4); */ /* /\* Create dims. *\/ */ /* dimid[0] = ncdimdef(ncid, LAT, LAT_LEN); */ /* dimid[1] = ncdimdef(ncid, LON, LON_LEN); */ /* /\* Create var. *\/ */ /* varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids); */ /* if (varid) ERR; */ /* ncclose(ncid); */ /* /\* Open the file and check. *\/ */ /* ncid = ncopen(FILE_NAME, 0); */ /* ncclose(ncid); */ /* } */ /* SUMMARIZE_ERR; */ #define NDIMS 3 #define NNAMES 4 #define NLINES 13 /* printf("**** testing funny names for netCDF-4..."); */ /* { */ /* int ncid, wind_id; */ /* size_t len[NDIMS] = {7, 3, 1}; */ /* int dimids[NDIMS], dimids_in[NDIMS], ndims_in; */ /* char funny_name[NNAMES][NC_MAX_NAME] = {"\a\t", "\f\n", "\r\v", "\b"}; */ /* char name_in[NC_MAX_NAME + 1]; */ /* char *speech[NLINES] = {"who would fardels bear, ", */ /* "To grunt and sweat under a weary life, ", */ /* "But that the dread of something after death, ", */ /* "The undiscover'd country from whose bourn ", */ /* "No traveller returns, puzzles the will ", */ /* "And makes us rather bear those ills we have ", */ /* "Than fly to others that we know not of? ", */ /* "Thus conscience does make cowards of us all; ", */ /* "And thus the native hue of resolution ", */ /* "Is sicklied o'er with the pale cast of thought, ", */ /* "And enterprises of great pith and moment ", */ /* "With this regard their currents turn awry, ", */ /* "And lose the name of action."}; */ /* char *speech_in[NLINES]; */ /* int i; */ /* unsigned short nlines = NLINES; */ /* unsigned int nlines_in; */ /* if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */ /* /\* Define dimensions. *\/ */ /* for (i = 0; i < NDIMS; i++) */ /* if (nc_def_dim(ncid, funny_name[i], len[i], &dimids[i])) ERR; */ /* /\* Write some global atts. *\/ */ /* if (nc_put_att_string(ncid, NC_GLOBAL, funny_name[0], NLINES, */ /* (const char **)speech)) ERR; */ /* if (nc_put_att_ushort(ncid, NC_GLOBAL, funny_name[1], NC_UINT, 1, &nlines)) ERR; */ /* /\* Define variables. *\/ */ /* if (nc_def_var(ncid, funny_name[3], NC_INT64, NDIMS, dimids, &wind_id)) ERR; */ /* if (nc_close(ncid)) ERR; */ /* /\* Open the file and check. *\/ */ /* if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; */ /* if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; */ /* if (ndims_in != NDIMS) ERR; */ /* for (i = 0; i < NDIMS; i++) */ /* { */ /* if (dimids_in[i] != i) ERR; */ /* if (nc_inq_dimname(ncid, i, name_in)) ERR; */ /* if (strcmp(name_in, funny_name[i])) ERR; */ /* } */ /* if (nc_get_att_string(ncid, NC_GLOBAL, funny_name[0], (char **)speech_in)) ERR; */ /* for (i = 0; i < NLINES; i++) */ /* if (strcmp(speech_in[i], speech[i])) ERR; */ /* if (nc_get_att_uint(ncid, NC_GLOBAL, funny_name[1], &nlines_in)) ERR; */ /* if (nlines_in != NLINES) ERR; */ /* if (nc_free_string(NLINES, (char **)speech_in)) ERR; */ /* if (nc_inq_varname(ncid, 0, name_in)) ERR; */ /* if (strcmp(name_in, funny_name[3])) ERR; */ /* if (nc_close(ncid)) ERR; */ /* } */ /* SUMMARIZE_ERR; */ printf("**** testing endianness..."); #define NDIMS4 1 #define DIM4_NAME "Joe" #define VAR_NAME4 "Ed" #define DIM4_LEN 10 { int dimids[NDIMS4], dimids_in[NDIMS4]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM4_LEN], data_in[DIM4_LEN]; int endian_in; int i; for (i = 0; i < DIM4_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM4_NAME, DIM4_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME4, NC_INT, NDIMS4, dimids, &varid)) ERR; if (nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG)) ERR; if (varid != 0) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS4 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR; if (endian_in != NC_ENDIAN_BIG) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS4 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR; if (endian_in != NC_ENDIAN_BIG) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM4_LEN; i++) if (data[i] != data_in[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing chunking..."); { #define NDIMS5 1 #define DIM5_NAME "D5" #define VAR_NAME5 "V5" #define DIM5_LEN 1000 int dimids[NDIMS5], dimids_in[NDIMS5]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM5_LEN], data_in[DIM5_LEN]; int chunksize[NDIMS5] = {5}; int chunksize_in[NDIMS5]; int contiguous_in; int i, d; for (i = 0; i < DIM5_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM5_NAME, DIM5_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME5, NC_INT, NDIMS5, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 0, chunksize)) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS5 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS5; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM5_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS5 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS5; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM5_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing contiguous storage..."); { #define NDIMS6 1 #define DIM6_NAME "D5" #define VAR_NAME6 "V5" #define DIM6_LEN 100 int dimids[NDIMS6], dimids_in[NDIMS6]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data[DIM6_LEN], data_in[DIM6_LEN]; int chunksize_in[NDIMS6]; int contiguous_in; int i, d; for (i = 0; i < DIM6_LEN; i++) data[i] = i; /* Create a netcdf-4 file with one dim and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM6_NAME, DIM6_LEN, &dimids[0])) ERR; if (dimids[0] != 0) ERR; if (nc_def_var(ncid, VAR_NAME6, NC_INT, NDIMS6, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 1, NULL)) ERR; if (nc_put_var_int(ncid, varid, data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS6 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS6; d++) if (chunksize_in[d] != 0) ERR; if (!contiguous_in) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM6_LEN; i++) if (data_in[i] != data[i]) ERR_RET; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS6 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS6; d++) if (chunksize_in[d] != 0) ERR; if (!contiguous_in) ERR; if (nc_get_var_int(ncid, varid, data_in)) ERR; for (i = 0; i < DIM6_LEN; i++) if (data[i] != data_in[i]) ERR_RET; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing extreme numbers dude..."); { #define VAR_NAME7 "V5" #define DIM6_LEN 100 int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; /* unsigned long long data = 9223372036854775807ull, data_in;*/ unsigned long long data = 9223372036854775817ull, data_in; /* Create a netcdf-4 file with scalar var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME7, NC_UINT64, 0, NULL, &varid)) ERR; if (nc_put_var_ulonglong(ncid, varid, &data)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR; if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR; if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1 || varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR; if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR; if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != 1 || varids_in[0] != 0) ERR; if (nc_inq_varname(ncid, 0, name_in)) ERR; if (strcmp(name_in, GENERIC_NAME)) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes some more..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error codes for name clashes even more..."); { #define GENERIC_NAME "bob" int ncid, varid, numgrps, ntypes; /* Create a netcdf-4 file with one group. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, GENERIC_NAME, NULL)) ERR; /* These don'e work, becuase the name is already in use. Make * sure the correct error is returned. */ if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR; if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR; /* Close it. */ if (nc_close(ncid)) ERR; /* Open the file and check. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in) ERR; if (nc_inq_grps(ncid, &numgrps, NULL)) ERR; if (numgrps != 1) ERR; if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR; if (ntypes) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing error code for too-large chunks..."); { #define NDIMS17 2 #define DIM17_NAME "personality" #define DIM17_NAME_2 "good_looks" #define VAR_NAME17 "ed" #define DIM17_LEN 2147483644 /* max dimension size - 2GB - 4. */ #define DIM17_2_LEN 1000 int dimids[NDIMS17], dimids_in[NDIMS17]; int varid; int ndims, nvars, natts, unlimdimid; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int chunksize[NDIMS17] = {5, 5}; int bad_chunksize[NDIMS17] = {5, DIM17_LEN}; int chunksize_in[NDIMS17]; int contiguous_in; int d; /* Create a netcdf-4 file with two dims and one var. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM17_NAME, DIM17_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM17_NAME_2, DIM17_2_LEN, &dimids[1])) ERR; if (dimids[0] != 0 || dimids[1] != 1) ERR; if (nc_def_var(ncid, VAR_NAME17, NC_UINT64, NDIMS17, dimids, &varid)) ERR; if (nc_def_var_chunking(ncid, varid, 0, bad_chunksize) != NC_EBADCHUNK) ERR; if (nc_def_var_chunking(ncid, varid, 0, chunksize)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS17 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS17; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_close(ncid)) ERR; /* Open the file and check the same stuff. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check stuff. */ if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NDIMS17 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars, varids_in)) ERR; if (nvars != 1) ERR; if (varids_in[0] != 0) ERR; if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR; if (nc_inq_var_chunking(ncid, 0, &contiguous_in, chunksize_in)) ERR; for (d = 0; d < NDIMS17; d++) if (chunksize[d] != chunksize_in[d]) ERR; if (contiguous_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; #ifdef USE_PARALLEL MPI_Finalize(); #endif }