static int write_numeric_attribute(int ncid, int varid, const char *name, harp_data_type data_type, harp_scalar data) { int result; result = NC_NOERR; switch (data_type) { case harp_type_int8: result = nc_put_att_schar(ncid, varid, name, NC_BYTE, 1, &data.int8_data); break; case harp_type_int16: result = nc_put_att_short(ncid, varid, name, NC_SHORT, 1, &data.int16_data); break; case harp_type_int32: result = nc_put_att_int(ncid, varid, name, NC_INT, 1, &data.int32_data); break; case harp_type_float: result = nc_put_att_float(ncid, varid, name, NC_FLOAT, 1, &data.float_data); break; case harp_type_double: result = nc_put_att_double(ncid, varid, name, NC_DOUBLE, 1, &data.double_data); break; default: assert(0); exit(1); } if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } return 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); }
/** * Writes meta data about the simulation into the netCDF-file in order to be able to restart the simulation * using only the checkpoint file * * @param i_numberOfCheckpoints The total number of checkpoints to be written * @param i_endSimulation The total time to be simulated * @param i_boundaryTypes The type of left, right, bottom top boundary (e.g. OUTFLOW or WALL) */ void io::NetCdfWriter::writeSimulationInfo( int i_numberOfCheckpoints, float i_endSimulation, BoundaryType* i_boundaryTypes) { nc_put_att_int(dataFile, NC_GLOBAL, "numberOfCheckpoints", NC_INT, 1, &i_numberOfCheckpoints); nc_put_att_float(dataFile, NC_GLOBAL, "endSimulation", NC_FLOAT, 1, &i_endSimulation); ncPutBoundaryTypeAtt(NC_GLOBAL, "boundaryTypeLeft", i_boundaryTypes[BND_LEFT]); ncPutBoundaryTypeAtt(NC_GLOBAL, "boundaryTypeRight", i_boundaryTypes[BND_RIGHT]); ncPutBoundaryTypeAtt(NC_GLOBAL, "boundaryTypeBottom", i_boundaryTypes[BND_BOTTOM]); ncPutBoundaryTypeAtt(NC_GLOBAL, "boundaryTypeTop", i_boundaryTypes[BND_TOP]); }
void R_nc_put_att_int( int *ncid, int *varid, char **attname, int *type_to_create, int *natts, int *attribute, int *retval ) { nc_type ttc; ttc = R_nc_ttc_to_nctype( *type_to_create ); *retval = nc_put_att_int(*ncid, *varid, attname[0], ttc, *natts, attribute ); if( *retval != NC_NOERR ) REprintf( "Error in R_nc_put_att_int: %s\n", nc_strerror(*retval) ); }
/* NOTE that NA's are handled through this vector. According to the * docs as of 2010-11-02, NA's have the value MIN_INT */ void R_nc4_put_att_logical( int *ncid, int *varid, char **attname, int *type_to_create, int *natts, int *attribute, int *retval ) { int R_NA_val; float C_NA_val_f; double C_NA_val_d; /* Rprintf( "in R_nc4_put_att_logical with val=%d\n", *attribute ); */ nc_type ttc; ttc = R_nc4_ttc_to_nctype( *type_to_create ); R_NA_val = -2147483648; /* From R docs */ if( *attribute == R_NA_val ) { /* Rprintf( "PUTTING a NA -- float \n" ); */ /* Put a NA */ if( ttc == NC_FLOAT ) { C_NA_val_f = 0./0.; *retval = nc_put_att_float(*ncid, *varid, attname[0], ttc, *natts, &C_NA_val_f ); if( *retval != NC_NOERR ) Rprintf( "Error in R_nc4_put_att_logical: %s\n", nc_strerror(*retval) ); } else if( ttc == NC_DOUBLE ) { /* Rprintf( "PUTTING a NA -- double \n" ); */ C_NA_val_d = 0./0.; *retval = nc_put_att_double(*ncid, *varid, attname[0], ttc, *natts, &C_NA_val_d ); if( *retval != NC_NOERR ) Rprintf( "Error in R_nc4_put_att_logical: %s\n", nc_strerror(*retval) ); } else { Rprintf( "Error in R_nc4_put_att_logical: asked to put a NA value, but the variable's type is not a double or float, which are the only two types that have a defined NaN value\n" ); *retval = -1; return; } } else *retval = nc_put_att_int(*ncid, *varid, attname[0], ttc, *natts, attribute ); if( *retval != NC_NOERR ) Rprintf( "Error in R_nc4_put_att_logical: %s\n", nc_strerror(*retval) ); }
int put_nc_global_attr(int ncid, int dim_latlon, int grid_template) { char *str; str=""; if (dim_latlon == 1) str = "COARDS"; else if (dim_latlon == 2) str = "CF-1.0"; else fatal_error("netcdf:create_nc_dims: %s","unsupported lat-lon dimension"); netcdf_func( nc_put_att_text(ncid, NC_GLOBAL, "Conventions", strlen(str), str) ); netcdf_func( nc_put_att_int (ncid, NC_GLOBAL, "GRIB2_grid_template", NC_INT, 1, &grid_template) ); }
void ex_update_max_name_length(int exoid, int length) { int status; int db_length = 0; /* Get current value of the maximum_name_length attribute... */ if ((status = nc_get_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &db_length)) != NC_NOERR) { char errmsg[MAX_ERR_LENGTH]; exerrval = status; sprintf(errmsg, "Error: failed to update 'max_name_length' attribute in file id %d", exoid); ex_err("ex_update_max_name_length",errmsg,exerrval); } if (length > db_length) { /* Update with new value... */ nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &length); } }
void ex_update_max_name_length(int exoid, int length) { int status; int db_length = 0; int rootid = exoid & EX_FILE_ID_MASK; ex_check_valid_file_id(exoid); /* Get current value of the maximum_name_length attribute... */ if ((status = nc_get_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &db_length)) != NC_NOERR) { char errmsg[MAX_ERR_LENGTH]; exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to update 'max_name_length' attribute in file id %d", exoid); ex_err("ex_update_max_name_length", errmsg, exerrval); } if (length > db_length) { /* Update with new value... */ nc_put_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &length); nc_sync(rootid); } }
// NetcdfFile::NC_createReservoir() int NetcdfFile::NC_createReservoir(bool hasBins, double reservoirT, int iseed, int& eptotVID, int& binsVID) { int dimensionID[1]; dimensionID[0] = frameDID_; if (ncid_ == -1 || dimensionID[0] == -1) return 1; // Place file back in define mode if ( checkNCerr( nc_redef( ncid_ ) ) ) return 1; // Define eptot, bins, temp0 if ( checkNCerr( nc_def_var(ncid_, NCEPTOT, NC_DOUBLE, 1, dimensionID, &eptotVID)) ) { mprinterr("Error: defining eptot variable ID.\n"); return 1; } if (hasBins) { if ( checkNCerr( nc_def_var(ncid_, NCBINS, NC_INT, 1, dimensionID, &binsVID)) ) { mprinterr("Error: defining bins variable ID.\n"); return 1; } } else binsVID = -1; if (NC_defineTemperature(dimensionID, 0)) return 1; // Random seed, make global if ( checkNCerr( nc_put_att_int(ncid_, NC_GLOBAL, "iseed", NC_INT, 1, &iseed) ) ) { mprinterr("Error: setting random seed attribute.\n"); return 1; } // End definitions if (checkNCerr(nc_enddef(ncid_))) { mprinterr("NetCDF error on ending definitions."); return 1; } // Write temperature if (checkNCerr(nc_put_var_double(ncid_,TempVID_,&reservoirT)) ) { mprinterr("Error: Writing reservoir temperature.\n"); return 1; } return 0; }
int main(int argc, char **argv) { /* MPI stuff. */ int mpi_namelen; char mpi_name[MPI_MAX_PROCESSOR_NAME]; int mpi_size, mpi_rank; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; double start_time = 0, total_time; /* Netcdf-4 stuff. */ int ncid, varid, dimids[NDIMS]; size_t start[NDIMS] = {0, 0, 0}; size_t count[NDIMS] = {1, DIMSIZE, DIMSIZE}; int data[DIMSIZE * DIMSIZE], data_in[DIMSIZE * DIMSIZE]; int j, i; char file_name[NC_MAX_NAME + 1]; int ndims_in, nvars_in, natts_in, unlimdimid_in; #ifdef USE_MPE int s_init, e_init, s_define, e_define, s_write, e_write, s_close, e_close; #endif /* USE_MPE */ /* Initialize MPI. */ MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Get_processor_name(mpi_name, &mpi_namelen); /*printf("mpi_name: %s size: %d rank: %d\n", mpi_name, mpi_size, mpi_rank);*/ /* Must be able to evenly divide my slabs between processors. */ if (NUM_SLABS % mpi_size != 0) { if (!mpi_rank) printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n", NUM_SLABS, mpi_size); ERR; } #ifdef USE_MPE MPE_Init_log(); s_init = MPE_Log_get_event_number(); e_init = MPE_Log_get_event_number(); s_define = MPE_Log_get_event_number(); e_define = MPE_Log_get_event_number(); s_write = MPE_Log_get_event_number(); e_write = MPE_Log_get_event_number(); s_close = MPE_Log_get_event_number(); e_close = MPE_Log_get_event_number(); s_open = MPE_Log_get_event_number(); e_open = MPE_Log_get_event_number(); MPE_Describe_state(s_init, e_init, "Init", "red"); MPE_Describe_state(s_define, e_define, "Define", "yellow"); MPE_Describe_state(s_write, e_write, "Write", "green"); MPE_Describe_state(s_close, e_close, "Close", "purple"); MPE_Describe_state(s_open, e_open, "Open", "blue"); MPE_Start_log(); MPE_Log_event(s_init, 0, "start init"); #endif /* USE_MPE */ /* if (!mpi_rank) */ /* { */ /* printf("\n*** Testing parallel I/O some more.\n"); */ /* printf("*** writing a %d x %d x %d file from %d processors...\n", */ /* NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size); */ /* } */ /* We will write the same slab over and over. */ for (i = 0; i < DIMSIZE * DIMSIZE; i++) data[i] = mpi_rank; #ifdef USE_MPE MPE_Log_event(e_init, 0, "end init"); MPE_Log_event(s_define, 0, "start define file"); #endif /* USE_MPE */ /* Create a parallel netcdf-4 file. */ sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create_par(file_name, NC_PNETCDF, comm, info, &ncid)) ERR; /* A global attribute holds the number of processors that created * the file. */ if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR; /* Create three dimensions. */ if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR; if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR; /* Create one var. */ if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS, dimids, &varid)) ERR; /* Write metadata to file. */ if (nc_enddef(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_define, 0, "end define file"); if (mpi_rank) sleep(mpi_rank); #endif /* USE_MPE */ /* if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR;*/ if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR; if (!mpi_rank) start_time = MPI_Wtime(); /* Write all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { start[0] = NUM_SLABS / mpi_size * mpi_rank + i; #ifdef USE_MPE MPE_Log_event(s_write, 0, "start write slab"); #endif /* USE_MPE */ /* Write one slab of data. */ if (nc_put_vara_int(ncid, varid, start, count, data)) ERR; #ifdef USE_MPE MPE_Log_event(e_write, 0, "end write file"); #endif /* USE_MPE */ } if (!mpi_rank) { total_time = MPI_Wtime() - start_time; /* printf("num_proc\ttime(s)\n");*/ printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS * sizeof(int) / total_time); } #ifdef USE_MPE MPE_Log_event(s_close, 0, "start close file"); #endif /* USE_MPE */ /* Close the netcdf file. */ if (nc_close(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_close, 0, "end close file"); #endif /* USE_MPE */ /* Reopen the file and check it. */ if (nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR; if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 || unlimdimid_in != -1) ERR; /* Read all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { start[0] = NUM_SLABS / mpi_size * mpi_rank + i; #ifdef USE_MPE MPE_Log_event(s_read, 0, "start read slab"); #endif /* USE_MPE */ /* Read one slab of data. */ if (nc_get_vara_int(ncid, varid, start, count, data_in)) ERR; /* Check data. */ for (j = 0; j < DIMSIZE * DIMSIZE; j++) if (data_in[j] != mpi_rank) { ERR; break; } #ifdef USE_MPE MPE_Log_event(e_read, 0, "end read file"); #endif /* USE_MPE */ } #ifdef USE_MPE MPE_Log_event(s_close, 0, "start close file"); #endif /* USE_MPE */ /* Close the netcdf file. */ if (nc_close(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_close, 0, "end close file"); #endif /* USE_MPE */ /* Delete this large file. */ remove(file_name); /* Shut down MPI. */ MPI_Finalize(); /* if (!mpi_rank) */ /* { */ /* SUMMARIZE_ERR; */ /* FINAL_RESULTS; */ /* } */ return total_err; }
int ex_open_int (const char *path, int mode, int *comp_ws, int *io_ws, float *version, int run_version) { int exoid; int status, stat_att, stat_dim; nc_type att_type = NC_NAT; size_t att_len = 0; int old_fill; int file_wordsize; int dim_str_name; int int64_status = 0; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* set error handling mode to no messages, non-fatal errors */ ex_opts(exoptval); /* call required to set ncopts first time through */ if (run_version != EX_API_VERS_NODOT && warning_output == 0) { int run_version_major = run_version / 100; int run_version_minor = run_version % 100; int lib_version_major = EX_API_VERS_NODOT / 100; int lib_version_minor = EX_API_VERS_NODOT % 100; fprintf(stderr, "EXODUS: Warning: This code was compiled with exodus version %d.%02d,\n but was linked with exodus library version %d.%02d\n This is probably an error in the build process of this code.\n", run_version_major, run_version_minor, lib_version_major, lib_version_minor); warning_output = 1; } if ((mode & EX_READ) && (mode & EX_WRITE)) { exerrval = EX_BADFILEMODE; sprintf(errmsg,"Error: Cannot specify both EX_READ and EX_WRITE"); ex_err("ex_open",errmsg,exerrval); return (EX_FATAL); } /* The EX_READ mode is the default if EX_WRITE is not specified... */ if (!(mode & EX_WRITE)) { /* READ ONLY */ #if defined(__LIBCATAMOUNT__) if ((status = nc_open (path, NC_NOWRITE, &exoid)) != NC_NOERR) #else if ((status = nc_open (path, NC_NOWRITE|NC_SHARE, &exoid)) != NC_NOERR) #endif { /* NOTE: netCDF returns an id of -1 on an error - but no error code! */ if (status == 0) { exerrval = EX_FATAL; } else { /* It is possible that the user is trying to open a netcdf4 file, but the netcdf4 capabilities aren't available in the netcdf linked to this library. Note that we can't just use a compile-time define since we could be using a shareable netcdf library, so the netcdf4 capabilities aren't known until runtime... Netcdf-4.X does not (yet?) have a function that can be queried to determine whether the library being used was compiled with --enable-netcdf4, so that isn't very helpful.. At this time, query the beginning of the file and see if it is an HDF-5 file and if it is assume that the open failure is due to the netcdf library not enabling netcdf4 features... */ int type = 0; ex_check_file_type(path, &type); if (type == 5) { /* This is an hdf5 (netcdf4) file. Since the nc_open failed, the assumption is that the netcdf doesn't have netcdf4 capabilities enabled. Tell the user... */ fprintf(stderr, "EXODUS: Error: Attempting to open the netcdf-4 file:\n\t'%s'\n\twith a netcdf library that does not support netcdf-4\n", path); } exerrval = status; } sprintf(errmsg,"Error: failed to open %s read only",path); ex_err("ex_open",errmsg,exerrval); return(EX_FATAL); } } else /* (mode & EX_WRITE) READ/WRITE */ { #if defined(__LIBCATAMOUNT__) if ((status = nc_open (path, NC_WRITE, &exoid)) != NC_NOERR) #else if ((status = nc_open (path, NC_WRITE|NC_SHARE, &exoid)) != NC_NOERR) #endif { /* NOTE: netCDF returns an id of -1 on an error - but no error code! */ if (status == 0) exerrval = EX_FATAL; else exerrval = status; sprintf(errmsg,"Error: failed to open %s write only",path); ex_err("ex_open",errmsg,exerrval); return(EX_FATAL); } /* turn off automatic filling of netCDF variables */ if ((status = nc_set_fill (exoid, NC_NOFILL, &old_fill)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to set nofill mode in file id %d", exoid); ex_err("ex_open", errmsg, exerrval); return (EX_FATAL); } stat_att = nc_inq_att(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len); stat_dim = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name); if(stat_att != NC_NOERR || stat_dim != NC_NOERR) { nc_redef(exoid); if (stat_att != NC_NOERR) { int max_so_far = 32; nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far); } /* If the DIM_STR_NAME variable does not exist on the database, we need to add it now. */ if(stat_dim != NC_NOERR) { /* Not found; set to default value of 32+1. */ int max_name = ex_default_max_name_length < 32 ? 32 : ex_default_max_name_length; nc_def_dim(exoid, DIM_STR_NAME, max_name+1, &dim_str_name); } nc_enddef (exoid); } } /* determine version of EXODUS II file, and the word size of * floating point and integer values stored in the file */ if ((status = nc_get_att_float(exoid, NC_GLOBAL, ATT_VERSION, version)) != NC_NOERR) { exerrval = status; sprintf(errmsg,"Error: failed to get database version for file id: %d", exoid); ex_err("ex_open",errmsg,exerrval); return(EX_FATAL); } /* check ExodusII file version - old version 1.x files are not supported */ if (*version < 2.0) { exerrval = EX_FATAL; sprintf(errmsg,"Error: Unsupported file version %.2f in file id: %d", *version, exoid); ex_err("ex_open",errmsg,exerrval); return(EX_FATAL); } if (nc_get_att_int (exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, &file_wordsize) != NC_NOERR) { /* try old (prior to db version 2.02) attribute name */ if (nc_get_att_int (exoid,NC_GLOBAL,ATT_FLT_WORDSIZE_BLANK,&file_wordsize) != NC_NOERR) { exerrval = EX_FATAL; sprintf(errmsg,"Error: failed to get file wordsize from file id: %d", exoid); ex_err("ex_open",errmsg,exerrval); return(exerrval); } } /* See if int64 status attribute exists and if so, what data is stored as int64 * Older files don't have the attribute, so it is not an error if it is missing */ if (nc_get_att_int (exoid, NC_GLOBAL, ATT_INT64_STATUS, &int64_status) != NC_NOERR) { int64_status = 0; /* Just in case it gets munged by a failed get_att_int call */ } /* Merge in API int64 status flags as specified by caller of function... */ int64_status |= (mode & EX_ALL_INT64_API); /* initialize floating point and integer size conversion. */ if (ex_conv_ini( exoid, comp_ws, io_ws, file_wordsize, int64_status ) != EX_NOERR ) { exerrval = EX_FATAL; sprintf(errmsg, "Error: failed to initialize conversion routines in file id %d", exoid); ex_err("ex_open", errmsg, exerrval); return (EX_FATAL); } return (exoid); }
int ex_copy (int in_exoid, int out_exoid) { int status; int ndims; /* number of dimensions */ int nvars; /* number of variables */ int ngatts; /* number of global attributes */ int recdimid; /* id of unlimited dimension */ int dimid; /* dimension id */ int dim_out_id; /* dimension id */ int varid; /* variable id */ int var_out_id; /* variable id */ struct ncvar var; /* variable */ struct ncatt att; /* attribute */ nc_type att_type = NC_NAT; size_t att_len = 0; size_t i; size_t numrec; size_t dim_sz; char dim_nm[NC_MAX_NAME]; int in_large, out_large; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* * Get exodus_large_model setting on both input and output * databases so know how to handle coordinates. */ in_large = ex_large_model(in_exoid); out_large = ex_large_model(out_exoid); /* * get number of dimensions, number of variables, number of global * atts, and dimension id of unlimited dimension, if any */ (void)nc_inq(in_exoid, &ndims, &nvars, &ngatts, &recdimid); (void)nc_inq_dimlen(in_exoid, recdimid, &numrec); /* put output file into define mode */ (void)nc_redef(out_exoid); /* copy global attributes */ for (i = 0; i < (size_t)ngatts; i++) { (void)nc_inq_attname(in_exoid, NC_GLOBAL, i, att.name); /* if attribute exists in output file, don't overwrite it; compute * word size, I/O word size etc. are global attributes stored when * file is created with ex_create; we don't want to overwrite those */ if ((status = nc_inq_att(out_exoid, NC_GLOBAL, att.name, &att.type, &att.len)) != NC_NOERR) { /* The "last_written_time" attribute is a special attribute used by the Sierra IO system to determine whether a timestep has been fully written to the database in order to try to detect a database crash that happens in the middle of a database output step. Don't want to copy that attribute. */ if (strcmp(att.name,"last_written_time") != 0) { /* attribute doesn't exist in new file so OK to create it */ nc_copy_att(in_exoid,NC_GLOBAL,att.name,out_exoid,NC_GLOBAL); } } } /* copy dimensions */ /* Get the dimension sizes and names */ for(dimid = 0; dimid < ndims; dimid++){ (void)nc_inq_dim(in_exoid,dimid,dim_nm,&dim_sz); /* If the dimension isn't one we specifically don't want * to copy (ie, number of QA or INFO records) and it * hasn't been defined, copy it */ if ( ( strcmp(dim_nm,DIM_NUM_QA) != 0) && ( strcmp(dim_nm,DIM_NUM_INFO) != 0) && ( strcmp(dim_nm,DIM_NUM_NOD_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_EDG_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_FAC_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ELE_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_NSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ESET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_FSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_SSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ELSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_GLO_VAR) != 0) ) { /* See if the dimension has already been defined */ status = nc_inq_dimid(out_exoid, dim_nm, &dim_out_id); if(status != NC_NOERR) { if(dimid != recdimid) { status = nc_def_dim(out_exoid, dim_nm, dim_sz, &dim_out_id); } else { status = nc_def_dim(out_exoid, dim_nm, NC_UNLIMITED, &dim_out_id); } /* end else */ if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define %s dimension in file id %d", dim_nm, out_exoid); ex_err("ex_copy",errmsg,exerrval); return (EX_FATAL); } } /* end if */ } /* end if */ } /* end loop over dim */ /* DIM_STR_NAME is a newly added dimension required by current API. * If it doesn't exist on the source database, we need to add it to * the target... */ status = nc_inq_dimid(in_exoid, DIM_STR_NAME, &dim_out_id); if (status != NC_NOERR) { /* Not found; set to default value of 32+1. */ if ((status = nc_def_dim(out_exoid, DIM_STR_NAME, 33, &dim_out_id)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define string name dimension in file id %d", out_exoid); ex_err("ex_copy",errmsg,exerrval); return (EX_FATAL); } } status = nc_inq_att(in_exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len); if (status != NC_NOERR) { int max_so_far = 32; nc_put_att_int(out_exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far); } /* copy variable definitions and variable attributes */ for (varid = 0; varid < nvars; varid++) { (void)nc_inq_var(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts); /* we don't want to copy some variables because there is not a * simple way to add to them; * QA records, info records and all results variables (nodal * element, and global results) are examples */ if ( ( strcmp(var.name,VAR_QA_TITLE) != 0) && ( strcmp(var.name,VAR_INFO) != 0) && ( strcmp(var.name,VAR_EBLK_TAB) != 0) && ( strcmp(var.name,VAR_FBLK_TAB) != 0) && ( strcmp(var.name,VAR_ELEM_TAB) != 0) && ( strcmp(var.name,VAR_ELSET_TAB) != 0) && ( strcmp(var.name,VAR_SSET_TAB) != 0) && ( strcmp(var.name,VAR_FSET_TAB) != 0) && ( strcmp(var.name,VAR_ESET_TAB) != 0) && ( strcmp(var.name,VAR_NSET_TAB) != 0) && ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0) && ( strcmp(var.name,VAR_GLO_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0)&& ( strncmp(var.name,"vals_elset_var", 14) != 0) && ( strncmp(var.name,"vals_sset_var", 13) != 0) && ( strncmp(var.name,"vals_fset_var", 13) != 0) && ( strncmp(var.name,"vals_eset_var", 13) != 0) && ( strncmp(var.name,"vals_nset_var", 13) != 0) && ( strncmp(var.name,"vals_nod_var", 12) != 0) && ( strncmp(var.name,"vals_edge_var", 13) != 0) && ( strncmp(var.name,"vals_face_var", 13) != 0) && ( strncmp(var.name,"vals_elem_var", 13) != 0) ) { if (strncmp(var.name,VAR_COORD,5) == 0) { var_out_id = cpy_coord_def (in_exoid, out_exoid, recdimid, var.name, in_large, out_large); } else { var_out_id = cpy_var_def (in_exoid, out_exoid, recdimid, var.name); } /* copy the variable's attributes */ (void) cpy_att (in_exoid, out_exoid, varid, var_out_id); } } /* take the output file out of define mode */ if ((exerrval=nc_enddef (out_exoid)) != NC_NOERR) { sprintf(errmsg, "Error: failed to complete definition in file id %d", out_exoid); ex_err("ex_copy",errmsg,exerrval); return (EX_FATAL); } /* output variable data */ for (varid = 0; varid < nvars; varid++) { (void)nc_inq_var(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts); /* we don't want to copy some variable values; * QA records and info records shouldn't be copied because there * isn't an easy way to add to them; * the time value array ("time_whole") and any results variables * (nodal, elemental, or global) shouldn't be copied */ if ( ( strcmp(var.name,VAR_QA_TITLE) != 0) && ( strcmp(var.name,VAR_INFO) != 0) && ( strcmp(var.name,VAR_EBLK_TAB) != 0) && ( strcmp(var.name,VAR_FBLK_TAB) != 0) && ( strcmp(var.name,VAR_ELEM_TAB) != 0) && ( strcmp(var.name,VAR_ELSET_TAB) != 0) && ( strcmp(var.name,VAR_SSET_TAB) != 0) && ( strcmp(var.name,VAR_FSET_TAB) != 0) && ( strcmp(var.name,VAR_ESET_TAB) != 0) && ( strcmp(var.name,VAR_NSET_TAB) != 0) && ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0) && ( strcmp(var.name,VAR_GLO_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0) && ( strncmp(var.name,"vals_elset_var", 14) != 0)&& ( strncmp(var.name,"vals_sset_var", 13) != 0)&& ( strncmp(var.name,"vals_fset_var", 13) != 0)&& ( strncmp(var.name,"vals_eset_var", 13) != 0)&& ( strncmp(var.name,"vals_nset_var", 13) != 0)&& ( strncmp(var.name,"vals_nod_var", 12) != 0) && ( strncmp(var.name,"vals_edge_var",13) != 0) && ( strncmp(var.name,"vals_face_var",13) != 0) && ( strncmp(var.name,"vals_elem_var",13) != 0) && ( strcmp(var.name,VAR_WHOLE_TIME) != 0) ) { if (strncmp(var.name,VAR_COORD,5) == 0) { (void) cpy_coord_val (in_exoid, out_exoid, var.name, in_large, out_large); } else { (void) cpy_var_val (in_exoid, out_exoid, var.name); } } } /* ensure internal data structures are updated */ /* if number of blocks > 0 */ update_internal_structs( out_exoid, EX_INQ_EDGE_BLK, ex_get_counter_list(EX_EDGE_BLOCK)); update_internal_structs( out_exoid, EX_INQ_FACE_BLK, ex_get_counter_list(EX_FACE_BLOCK)); update_internal_structs( out_exoid, EX_INQ_ELEM_BLK, ex_get_counter_list(EX_ELEM_BLOCK)); /* if number of sets > 0 */ update_internal_structs( out_exoid, EX_INQ_NODE_SETS, ex_get_counter_list(EX_NODE_SET)); update_internal_structs( out_exoid, EX_INQ_EDGE_SETS, ex_get_counter_list(EX_EDGE_SET)); update_internal_structs( out_exoid, EX_INQ_FACE_SETS, ex_get_counter_list(EX_FACE_SET)); update_internal_structs( out_exoid, EX_INQ_SIDE_SETS, ex_get_counter_list(EX_SIDE_SET)); update_internal_structs( out_exoid, EX_INQ_ELEM_SETS, ex_get_counter_list(EX_ELEM_SET)); /* if number of maps > 0 */ update_internal_structs( out_exoid, EX_INQ_NODE_MAP, ex_get_counter_list(EX_NODE_MAP)); update_internal_structs( out_exoid, EX_INQ_EDGE_MAP, ex_get_counter_list(EX_EDGE_MAP)); update_internal_structs( out_exoid, EX_INQ_FACE_MAP, ex_get_counter_list(EX_FACE_MAP)); update_internal_structs( out_exoid, EX_INQ_ELEM_MAP, ex_get_counter_list(EX_ELEM_MAP)); return(EX_NOERR); }
int main(int argc, char **argv) { (void) signal(SIGFPE, SIG_IGN); printf("\n*** Testing netcdf-4 attribute functions.\n"); printf("*** testing really simple global atts..."); #define NUM_SIMPLE_ATTS 9 { int ncid; char name[NUM_SIMPLE_ATTS][ATT_MAX_NAME + 1] = {"Gc", "Gb", "Gs", "Gi", "Gf", "Gd", "G7", "G8", "G9"}; char name_in[NC_MAX_NAME]; int j; /* Create a file with some global atts. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; for (j = 0; j < NUM_SIMPLE_ATTS; j++) if (nc_put_att_int(ncid, NC_GLOBAL, name[j], NC_INT, 0, NULL)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check the order. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; for (j = 0; j < NUM_SIMPLE_ATTS; j++) { if (nc_inq_attname(ncid, NC_GLOBAL, j, name_in)) ERR; if (strcmp(name_in, name[j])) ERR; } /* Close up shop. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing simple global atts..."); { int ncid; nc_type att_type; size_t att_len; int i; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; unsigned char uchar_in[ATT_LEN], uchar_out[ATT_LEN] = {0, 128, NC_MAX_CHAR}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN], ushort_out[ATT_LEN] = {0, 128, NC_MAX_USHORT}; unsigned int uint_in[ATT_LEN], uint_out[ATT_LEN] = {0, 128, NC_MAX_UINT}; unsigned long long uint64_in[ATT_LEN], uint64_out[ATT_LEN] = {0, 128, 18446744073709551612ULL}; long long int64_in[ATT_LEN], int64_out[ATT_LEN] = {NC_MIN_INT64, 128, NC_MAX_INT64}; /* This won't work, because classic files can't create these types. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out) != NC_ESTRICTNC3) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out) != NC_ESTRICTNC3) ERR; if (nc_close(ncid)) ERR; /* Create a file with a global attribute of each type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UBYTE, ATT_LEN, uchar_out)) ERR; if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, ATT_LEN, short_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, ATT_LEN, int_out)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, ATT_LEN, float_out)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, ATT_LEN, double_out)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out)) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out)) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out)) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; /* Check text. */ if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); /* Check numeric values. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, uchar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uchar_in[i] != uchar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, ushort_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (ushort_in[i] != ushort_out[i]) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, uint_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint_in[i] != uint_out[i]) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, int64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int64_in[i] != int64_out[i]) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, uint64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint64_in[i] != uint64_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute data type conversions..."); { int ncid; int i; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN]; unsigned int uint_in[ATT_LEN]; unsigned long long uint64_in[ATT_LEN]; long long int64_in[ATT_LEN]; /* Reopen the file and try different type conversions. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; /* No text conversions are allowed, and people who try them shold * be locked up, away from decent folk! */ if (nc_get_att_short(ncid, NC_GLOBAL, ATT_TEXT_NAME, short_in) != NC_ECHAR) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_TEXT_NAME, int_in) != NC_ECHAR) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_TEXT_NAME, float_in) != NC_ECHAR) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_TEXT_NAME, double_in) != NC_ECHAR) ERR; /* if (nc_get_att_ubyte(ncid, NC_GLOBAL, ATT_TEXT_NAME, uchar_in) != NC_ECHAR) ERR;*/ if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_TEXT_NAME, ushort_in) != NC_ECHAR) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_TEXT_NAME, uint_in) != NC_ECHAR) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_TEXT_NAME, int64_in) != NC_ECHAR) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_TEXT_NAME, uint64_in) != NC_ECHAR) ERR; /* Read all atts (except text) as double. */ if (nc_get_att_double(ncid, NC_GLOBAL, ATT_SCHAR_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != schar_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_SHORT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != short_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_INT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != int_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_FLOAT_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != float_out[i]) ERR; /* Read all atts (except text) as float. */ if (nc_get_att_float(ncid, NC_GLOBAL, ATT_SCHAR_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != schar_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_SHORT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != short_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_INT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != (float)int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != (float)double_out[i]) ERR; /* Read all atts (except text) as int. */ if (nc_get_att_int(ncid, NC_GLOBAL, ATT_SCHAR_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != schar_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_SHORT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_FLOAT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != (int)float_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != (int)double_out[i]) ERR; /* Read all atts (except text) as short. */ if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SCHAR_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != schar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_INT_NAME, short_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)int_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_FLOAT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)float_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != (short)double_out[i]) ERR; /* Read all atts (except text) as schar. Some range errors will * result converting to schar. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)short_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in) != NC_ERANGE) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)int_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)float_out[i]) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != (signed char)double_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing simple variable atts..."); { int ncid, varid, dimids[2]; nc_type att_type; size_t att_len; int i, v; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; /* Create a file with two vars, attaching to each an attribute of * each type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, varid, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_short(ncid, varid, ATT_SHORT_NAME, NC_SHORT, 3, short_out)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_put_att_float(ncid, varid, ATT_FLOAT_NAME, NC_FLOAT, 3, float_out)) ERR; if (nc_put_att_double(ncid, varid, ATT_DOUBLE_NAME, NC_DOUBLE, 3, double_out)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, varid, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_short(ncid, varid, ATT_SHORT_NAME, NC_SHORT, 3, short_out)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_put_att_float(ncid, varid, ATT_FLOAT_NAME, NC_FLOAT, 3, float_out)) ERR; if (nc_put_att_double(ncid, varid, ATT_DOUBLE_NAME, NC_DOUBLE, 3, double_out)) ERR; if (nc_close(ncid)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; for (v=0; v<2; v++) { if (nc_inq_att(ncid, v, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, v, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); if (nc_get_att_schar(ncid, v, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_short(ncid, v, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, v, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, v, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, v, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; } if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing zero-length attributes..."); { int ncid; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ /* Create a file with a global attribute of each type of zero length. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, 0, NULL)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, 0, NULL)) ERR; /* if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UCHAR, ATT_LEN, uchar_out)) ERR;*/ if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, 0, NULL)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, 0, NULL)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, 0, NULL)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, 0, NULL)) ERR; if (nc_close(ncid)) ERR; } /* Make sure we can read all these zero-length atts. */ { int ncid; signed char schar_in[ATT_LEN]; short short_in[ATT_LEN]; /*int int_in[ATT_LEN], int_out[ATT_LEN] = {NC_MIN_INT, 128, NC_MAX_INT};*/ int int_in[ATT_LEN]; float float_in[ATT_LEN]; double double_in[ATT_LEN]; size_t len; nc_type xtype; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, NULL)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_CHAR) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_SCHAR_NAME, &xtype, &len)) ERR; if (len || xtype != NC_BYTE) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_SHORT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_SHORT) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_INT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_INT) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_FLOAT_NAME, &xtype, &len)) ERR; if (len || xtype != NC_FLOAT) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, &xtype, &len)) ERR; if (len || xtype != NC_DOUBLE) ERR; /* Conversions no longer result in range errors, since there's no data. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing zero-length attributes and redef...(this test skipped for HDF5-1.8.0 beta1"); { int ncid; signed char schar_in[ATT_LEN]; short short_in[ATT_LEN]; int int_in[ATT_LEN]; float float_in[ATT_LEN]; double double_in[ATT_LEN]; /* Create a file with a global attribute of each type of zero length. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, 0, NULL)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, 0, NULL)) ERR; /* if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UCHAR, ATT_LEN, uchar_out)) ERR;*/ if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, 0, NULL)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, 0, NULL)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, 0, NULL)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, 0, NULL)) ERR; if (nc_close(ncid)) ERR; /* Make sure we can read all these zero-length atts added during a * redef. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, NULL)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; /* Conversions no longer result in range errors, since there's no data. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_FLOAT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_INT_NAME, schar_in)) ERR; if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SHORT_NAME, schar_in)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute deletes and renames..."); { int ncid, varid, dimids[2]; nc_type att_type; size_t att_len; char *speech_in; char name_in[NC_MAX_NAME + 1]; int attid_in, natts_in; int int_out[ATT_LEN] = {-100000, 128, 100000}; /* Create a file with a global attribute. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_close(ncid)) ERR; /* Rename it. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT_TEXT_NAME, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attname(ncid, NC_GLOBAL, attid_in, name_in)) ERR; if (strcmp(name_in, ATT_TEXT_NAME)) ERR; if (nc_rename_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, ATT_TEXT_NAME2)) ERR; if (nc_inq_attname(ncid, NC_GLOBAL, attid_in, name_in)) ERR; if (strcmp(name_in, ATT_TEXT_NAME2)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME2, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME2, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in) != NC_ENOTATT) ERR; if (nc_close(ncid)) ERR; /* Now delete the att. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_del_att(ncid, NC_GLOBAL, ATT_TEXT_NAME2)) ERR; if (nc_close(ncid)) ERR; /* Now create a file with a variable, which has an att. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and delete it. Make sure it's gone. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_del_att(ncid, 0, ATT_INT_NAME)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and readd the attribute. Enddef and redef, * and delete it, then check to make sure it's gone. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_put_att_int(ncid, varid, ATT_INT_NAME, NC_INT, 3, int_out)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_del_att(ncid, 0, ATT_INT_NAME)) ERR; if (nc_inq_varnatts(ncid, 0, &natts_in)) ERR; if (natts_in != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute create order..."); #define ATT0 "Maturin" #define ATT1 "Aubery" { int ncid, varid, dimids[2]; int attid_in; const int number = 42; /* Create a file with several global attributes. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT0, NC_INT, 1, &number)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT1, NC_INT, 1, &number)) ERR; if (nc_close(ncid)) ERR; /* Open it and check the order. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT0, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attid(ncid, NC_GLOBAL, ATT1, &attid_in)) ERR; if (attid_in != 1) ERR; if (nc_close(ncid)) ERR; /* Now create a file with a variable, which has two atts. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, ATT0, NC_INT, 1, &number)) ERR; if (nc_put_att_int(ncid, varid, ATT1, NC_INT, 1, &number)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check the order of the attributes on the var. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq_attid(ncid, 0, ATT0, &attid_in)) ERR; if (attid_in != 0) ERR; if (nc_inq_attid(ncid, 0, ATT1, &attid_in)) ERR; if (attid_in != 1) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute ordering some more..."); #define VAR_NAME "i" #define A1_NAME "i" #define A2_NAME "f" #define A3_NAME "d" #define A1_LEN 3 #define A2_LEN 4 #define A3_LEN 5 { int ncid; int varid, natts, nvars; double dvalue[] = {999.99, 999.99, 999.99, 999.99, 999.99}; int varids[1]; char name_in[NC_MAX_NAME + 1]; /* Create a file with one var, and attach three atts to it. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_INT, 0, NULL, &varid)) ERR; if (nc_put_att_double(ncid, varid, A1_NAME, NC_INT, A1_LEN, dvalue)) ERR; if (nc_put_att_double(ncid, varid, A2_NAME, NC_INT, A2_LEN, dvalue)) ERR; if (nc_put_att_double(ncid, varid, A3_NAME, NC_INT, A3_LEN, dvalue)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_varids(ncid, &nvars, varids)) ERR; if (nvars != 1 || varids[0] != 0) ERR; if (nc_inq_varnatts(ncid, 0, &natts)) ERR; if (natts != 3) ERR; if (nc_inq_attname(ncid, 0, 0, name_in)) ERR; if (strcmp(name_in, A1_NAME)) ERR; if (nc_inq_attname(ncid, 0, 1, name_in)) ERR; if (strcmp(name_in, A2_NAME)) ERR; if (nc_inq_attname(ncid, 0, 2, name_in)) ERR; if (strcmp(name_in, A3_NAME)) ERR; /* Close up shop. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing attribute ordering even more..."); /* Test the ordering of atts for each cmode. */ if (tst_att_ordering(NC_CLOBBER)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_64BIT_OFFSET)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_NETCDF4)) ERR; if (tst_att_ordering(NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL)) ERR; SUMMARIZE_ERR; printf("*** testing attributes and enddef/redef..."); #define ATT_1 "a" #define ATT_2 "b" #define ATT_3 "c" { int ncid, att = 1; if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL|NC_CLOBBER, &ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_1, NC_INT, 1, &att)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_2, NC_INT, 1, &att)) ERR; if (nc_put_att(ncid, NC_GLOBAL, ATT_3, NC_INT, 1, &att)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing copy of simple global atts..."); { int ncid, ncid2; nc_type att_type; size_t att_len; int i; char *speech_in; signed char schar_in[ATT_LEN], schar_out[ATT_LEN] = {NC_MIN_BYTE, 1, NC_MAX_BYTE}; unsigned char uchar_in[ATT_LEN], uchar_out[ATT_LEN] = {0, 128, NC_MAX_CHAR}; short short_in[ATT_LEN], short_out[ATT_LEN] = {NC_MIN_SHORT, -128, NC_MAX_SHORT}; int int_in[ATT_LEN], int_out[ATT_LEN] = {-100000, 128, 100000}; float float_in[ATT_LEN], float_out[ATT_LEN] = {.5, 0.25, 0.125}; double double_in[ATT_LEN], double_out[ATT_LEN] = {0.25, .5, 0.125}; unsigned short ushort_in[ATT_LEN], ushort_out[ATT_LEN] = {0, 128, NC_MAX_USHORT}; unsigned int uint_in[ATT_LEN], uint_out[ATT_LEN] = {0, 128, NC_MAX_UINT}; unsigned long long uint64_in[ATT_LEN], uint64_out[ATT_LEN] = {0, 128, 18446744073709551612ULL}; long long int64_in[ATT_LEN], int64_out[ATT_LEN] = {NC_MIN_INT64, 128, NC_MAX_INT64}; /* Create a file with a global attribute of each type. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, strlen(speech)+1, speech)) ERR; if (nc_put_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, NC_BYTE, ATT_LEN, schar_out)) ERR; if (nc_put_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, NC_UBYTE, ATT_LEN, uchar_out)) ERR; if (nc_put_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, NC_SHORT, ATT_LEN, short_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, NC_INT, ATT_LEN, int_out)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, NC_FLOAT, ATT_LEN, float_out)) ERR; if (nc_put_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, NC_DOUBLE, ATT_LEN, double_out)) ERR; if (nc_put_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, NC_USHORT, ATT_LEN, ushort_out)) ERR; if (nc_put_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, NC_UINT, ATT_LEN, uint_out)) ERR; if (nc_put_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, NC_INT64, ATT_LEN, int64_out)) ERR; if (nc_put_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, NC_UINT64, ATT_LEN, uint64_out)) ERR; /* Create another file and copy all the attributes. */ if (nc_create(FILE_NAME2, NC_NETCDF4, &ncid2)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_SCHAR_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UCHAR_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_SHORT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_INT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_FLOAT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_USHORT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UINT_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_INT64_NAME, ncid2, NC_GLOBAL)) ERR; if (nc_copy_att(ncid, NC_GLOBAL, ATT_UINT64_NAME, ncid2, NC_GLOBAL)) ERR; /* Close both files. */ if (nc_close(ncid)) ERR; if (nc_close(ncid2)) ERR; /* Open the file and check attributes. */ if (nc_open(FILE_NAME2, 0, &ncid)) ERR; /* Check text. */ if (nc_inq_att(ncid, NC_GLOBAL, ATT_TEXT_NAME, &att_type, &att_len)) ERR; if (att_type != NC_CHAR || att_len != strlen(speech) + 1) ERR; if (!(speech_in = malloc(att_len + 1))) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_TEXT_NAME, speech_in)) ERR; if (strcmp(speech, speech_in)) ERR; free(speech_in); /* Check numeric values. */ if (nc_get_att_schar(ncid, NC_GLOBAL, ATT_SCHAR_NAME, schar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (schar_in[i] != schar_out[i]) ERR; if (nc_get_att_uchar(ncid, NC_GLOBAL, ATT_UCHAR_NAME, uchar_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uchar_in[i] != uchar_out[i]) ERR; if (nc_get_att_short(ncid, NC_GLOBAL, ATT_SHORT_NAME, short_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (short_in[i] != short_out[i]) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT_INT_NAME, int_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int_in[i] != int_out[i]) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT_FLOAT_NAME, float_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (float_in[i] != float_out[i]) ERR; if (nc_get_att_double(ncid, NC_GLOBAL, ATT_DOUBLE_NAME, double_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (double_in[i] != double_out[i]) ERR; if (nc_get_att_ushort(ncid, NC_GLOBAL, ATT_USHORT_NAME, ushort_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (ushort_in[i] != ushort_out[i]) ERR; if (nc_get_att_uint(ncid, NC_GLOBAL, ATT_UINT_NAME, uint_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint_in[i] != uint_out[i]) ERR; if (nc_get_att_longlong(ncid, NC_GLOBAL, ATT_INT64_NAME, int64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (int64_in[i] != int64_out[i]) ERR; if (nc_get_att_ulonglong(ncid, NC_GLOBAL, ATT_UINT64_NAME, uint64_in)) ERR; for (i = 0; i < ATT_LEN; i++) if (uint64_in[i] != uint64_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
static GDALDataset * GMTCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, int bStrict, CPL_UNUSED char ** papszOptions, CPL_UNUSED GDALProgressFunc pfnProgress, CPL_UNUSED void * pProgressData ) { /* -------------------------------------------------------------------- */ /* Figure out general characteristics. */ /* -------------------------------------------------------------------- */ nc_type nc_datatype; GDALRasterBand *poBand; int nXSize, nYSize; CPLMutexHolderD(&hNCMutex); if( poSrcDS->GetRasterCount() != 1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Currently GMT export only supports 1 band datasets." ); return NULL; } poBand = poSrcDS->GetRasterBand(1); nXSize = poSrcDS->GetRasterXSize(); nYSize = poSrcDS->GetRasterYSize(); if( poBand->GetRasterDataType() == GDT_Int16 ) nc_datatype = NC_SHORT; else if( poBand->GetRasterDataType() == GDT_Int32 ) nc_datatype = NC_INT; else if( poBand->GetRasterDataType() == GDT_Float32 ) nc_datatype = NC_FLOAT; else if( poBand->GetRasterDataType() == GDT_Float64 ) nc_datatype = NC_DOUBLE; else if( bStrict ) { CPLError( CE_Failure, CPLE_AppDefined, "Band data type %s not supported in GMT, giving up.", GDALGetDataTypeName( poBand->GetRasterDataType() ) ); return NULL; } else if( poBand->GetRasterDataType() == GDT_Byte ) nc_datatype = NC_SHORT; else if( poBand->GetRasterDataType() == GDT_UInt16 ) nc_datatype = NC_INT; else if( poBand->GetRasterDataType() == GDT_UInt32 ) nc_datatype = NC_INT; else nc_datatype = NC_FLOAT; /* -------------------------------------------------------------------- */ /* Establish bounds from geotransform. */ /* -------------------------------------------------------------------- */ double adfGeoTransform[6]; double dfXMax, dfYMin; poSrcDS->GetGeoTransform( adfGeoTransform ); if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 ) { CPLError( bStrict ? CE_Failure : CE_Warning, CPLE_AppDefined, "Geotransform has rotational coefficients not supported in GMT." ); if( bStrict ) return NULL; } dfXMax = adfGeoTransform[0] + adfGeoTransform[1] * nXSize; dfYMin = adfGeoTransform[3] + adfGeoTransform[5] * nYSize; /* -------------------------------------------------------------------- */ /* Create base file. */ /* -------------------------------------------------------------------- */ int cdfid, err; err = nc_create (pszFilename, NC_CLOBBER,&cdfid); if( err != NC_NOERR ) { CPLError( CE_Failure, CPLE_AppDefined, "nc_create(%s): %s", pszFilename, nc_strerror( err ) ); return NULL; } /* -------------------------------------------------------------------- */ /* Define the dimensions and so forth. */ /* -------------------------------------------------------------------- */ int side_dim, xysize_dim, dims[1]; int x_range_id, y_range_id, z_range_id, inc_id, nm_id, z_id; nc_def_dim(cdfid, "side", 2, &side_dim); nc_def_dim(cdfid, "xysize", (int) (nXSize * nYSize), &xysize_dim); dims[0] = side_dim; nc_def_var (cdfid, "x_range", NC_DOUBLE, 1, dims, &x_range_id); nc_def_var (cdfid, "y_range", NC_DOUBLE, 1, dims, &y_range_id); nc_def_var (cdfid, "z_range", NC_DOUBLE, 1, dims, &z_range_id); nc_def_var (cdfid, "spacing", NC_DOUBLE, 1, dims, &inc_id); nc_def_var (cdfid, "dimension", NC_LONG, 1, dims, &nm_id); dims[0] = xysize_dim; nc_def_var (cdfid, "z", nc_datatype, 1, dims, &z_id); /* -------------------------------------------------------------------- */ /* Assign attributes. */ /* -------------------------------------------------------------------- */ double default_scale = 1.0; double default_offset = 0.0; int default_node_offset = 1; // pixel is area nc_put_att_text (cdfid, x_range_id, "units", 7, "meters"); nc_put_att_text (cdfid, y_range_id, "units", 7, "meters"); nc_put_att_text (cdfid, z_range_id, "units", 7, "meters"); nc_put_att_double (cdfid, z_id, "scale_factor", NC_DOUBLE, 1, &default_scale ); nc_put_att_double (cdfid, z_id, "add_offset", NC_DOUBLE, 1, &default_offset ); nc_put_att_int (cdfid, z_id, "node_offset", NC_LONG, 1, &default_node_offset ); nc_put_att_text (cdfid, NC_GLOBAL, "title", 1, ""); nc_put_att_text (cdfid, NC_GLOBAL, "source", 1, ""); /* leave define mode */ nc_enddef (cdfid); /* -------------------------------------------------------------------- */ /* Get raster min/max. */ /* -------------------------------------------------------------------- */ double adfMinMax[2]; GDALComputeRasterMinMax( (GDALRasterBandH) poBand, FALSE, adfMinMax ); /* -------------------------------------------------------------------- */ /* Set range variables. */ /* -------------------------------------------------------------------- */ size_t start[2], edge[2]; double dummy[2]; int nm[2]; start[0] = 0; edge[0] = 2; dummy[0] = adfGeoTransform[0]; dummy[1] = dfXMax; nc_put_vara_double(cdfid, x_range_id, start, edge, dummy); dummy[0] = dfYMin; dummy[1] = adfGeoTransform[3]; nc_put_vara_double(cdfid, y_range_id, start, edge, dummy); dummy[0] = adfGeoTransform[1]; dummy[1] = -adfGeoTransform[5]; nc_put_vara_double(cdfid, inc_id, start, edge, dummy); nm[0] = nXSize; nm[1] = nYSize; nc_put_vara_int(cdfid, nm_id, start, edge, nm); nc_put_vara_double(cdfid, z_range_id, start, edge, adfMinMax); /* -------------------------------------------------------------------- */ /* Write out the image one scanline at a time. */ /* -------------------------------------------------------------------- */ double *padfData; int iLine; padfData = (double *) CPLMalloc( sizeof(double) * nXSize ); edge[0] = nXSize; for( iLine = 0; iLine < nYSize; iLine++ ) { start[0] = iLine * nXSize; poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, padfData, nXSize, 1, GDT_Float64, 0, 0, NULL ); err = nc_put_vara_double( cdfid, z_id, start, edge, padfData ); if( err != NC_NOERR ) { CPLError( CE_Failure, CPLE_AppDefined, "nc_put_vara_double(%s): %s", pszFilename, nc_strerror( err ) ); nc_close (cdfid); return( NULL ); } } CPLFree( padfData ); /* -------------------------------------------------------------------- */ /* Close file, and reopen. */ /* -------------------------------------------------------------------- */ nc_close (cdfid); /* -------------------------------------------------------------------- */ /* Re-open dataset, and copy any auxiliary pam information. */ /* -------------------------------------------------------------------- */ GDALPamDataset *poDS = (GDALPamDataset *) GDALOpen( pszFilename, GA_ReadOnly ); if( poDS ) poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT ); return poDS; }
/* * Add or changes a numeric variable or global attribute of an open * netCDF file. */ static void c_ncapt ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ nc_type datatype, /* attribute datatype */ size_t attlen, /* attribute length */ const void* value, /* pointer to data values */ int* rcode /* returned error code */ ) { int status; switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_att_schar(ncid, varid, attname, datatype, attlen, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; } if (status == 0) *rcode = 0; else { nc_advise("NCAPT", status, ""); *rcode = ncerr; } }
int ex_put_prop (int exoid, ex_entity_type obj_type, int obj_id, const char *prop_name, int value) { int status; int oldfill, temp; int found = FALSE; int num_props, i, dimid, propid, dims[1]; size_t start[1]; size_t prop_name_len, name_length; int ldum; char name[MAX_VAR_NAME_LENGTH+1]; char tmpstr[MAX_STR_LENGTH+1]; char dim_name[MAX_VAR_NAME_LENGTH+1]; int vals[1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* check if property has already been created */ num_props = ex_get_num_props(exoid, obj_type); if (num_props > 1) { /* any properties other than the default 1? */ for (i=1; i<=num_props; i++) { switch (obj_type) { case EX_ELEM_BLOCK: strcpy (name, VAR_EB_PROP(i)); break; case EX_EDGE_BLOCK: strcpy (name, VAR_ED_PROP(i)); break; case EX_FACE_BLOCK: strcpy (name, VAR_FA_PROP(i)); break; case EX_NODE_SET: strcpy (name, VAR_NS_PROP(i)); break; case EX_EDGE_SET: strcpy (name, VAR_ES_PROP(i)); break; case EX_FACE_SET: strcpy (name, VAR_FS_PROP(i)); break; case EX_ELEM_SET: strcpy (name, VAR_ELS_PROP(i)); break; case EX_SIDE_SET: strcpy (name, VAR_SS_PROP(i)); break; case EX_ELEM_MAP: strcpy (name, VAR_EM_PROP(i)); break; case EX_FACE_MAP: strcpy (name, VAR_FAM_PROP(i)); break; case EX_EDGE_MAP: strcpy (name, VAR_EDM_PROP(i)); break; case EX_NODE_MAP: strcpy (name, VAR_NM_PROP(i)); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_put_prop",errmsg,exerrval); return(EX_FATAL); } if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get property array id in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } /* compare stored attribute name with passed property name */ memset(tmpstr, 0, MAX_STR_LENGTH+1); if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get property name in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } if (strcmp(tmpstr, prop_name) == 0) { found = TRUE; break; } } } /* if property array has not been created, create it */ if (!found) { name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH)+1; /* put netcdf file into define mode */ if ((status = nc_redef (exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } /* create a variable with a name xx_prop#, where # is the new number */ /* of the property */ switch (obj_type){ case EX_ELEM_BLOCK: strcpy (name, VAR_EB_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_EL_BLK); break; case EX_FACE_BLOCK: strcpy (name, VAR_FA_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_FA_BLK); break; case EX_EDGE_BLOCK: strcpy (name, VAR_ED_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_ED_BLK); break; case EX_NODE_SET: strcpy (name, VAR_NS_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_NS); break; case EX_EDGE_SET: strcpy (name, VAR_ES_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_ES); break; case EX_FACE_SET: strcpy (name, VAR_FS_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_FS); break; case EX_ELEM_SET: strcpy (name, VAR_ELS_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_ELS); break; case EX_SIDE_SET: strcpy (name, VAR_SS_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_SS); break; case EX_ELEM_MAP: strcpy (name, VAR_EM_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_EM); break; case EX_FACE_MAP: strcpy (name, VAR_FAM_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_FAM); break; case EX_EDGE_MAP: strcpy (name, VAR_EDM_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_EDM); break; case EX_NODE_MAP: strcpy (name, VAR_NM_PROP(num_props+1)); strcpy (dim_name, DIM_NUM_NM); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_put_prop",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } /* inquire id of previously defined dimension (number of objects) */ if ((status = nc_inq_dimid(exoid, dim_name, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of objects in file id %d", exoid); ex_err("ex_put_prop",errmsg, exerrval); goto error_ret; /* Exit define mode and return */ } dims[0] = dimid; nc_set_fill(exoid, NC_FILL, &oldfill); /* fill with zeros per routine spec */ if ((status = nc_def_var(exoid, name, NC_INT, 1, dims, &propid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create property array variable in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } vals[0] = 0; /* fill value */ /* create attribute to cause variable to fill with zeros per routine spec */ if ((status = nc_put_att_int(exoid, propid, _FillValue, NC_INT, 1, vals)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create property name fill attribute in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } /* Check that the property name length is less than MAX_NAME_LENGTH */ prop_name_len = strlen(prop_name)+1; if (prop_name_len > name_length) { fprintf(stderr, "Warning: The property name '%s' is too long.\n\tIt will be truncated from %d to %d characters\n", prop_name, (int)prop_name_len-1, (int)name_length-1); prop_name_len = name_length; } /* store property name as attribute of property array variable */ if ((status = nc_put_att_text(exoid, propid, ATT_PROP_NAME, prop_name_len, (void*)prop_name)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store property name %s in file id %d", prop_name,exoid); ex_err("ex_put_prop",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } ex_update_max_name_length(exoid, prop_name_len-1); /* leave define mode */ if ((status = nc_enddef (exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to leave define mode in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } nc_set_fill(exoid, oldfill, &temp); /* default: nofill */ } /* find index into property array using obj_id; put value in property */ /* array at proper index; ex_id_lkup returns an index that is 1-based,*/ /* but netcdf expects 0-based arrays so subtract 1 */ /* special case: property name ID - check for duplicate ID assignment */ if (strcmp("ID",prop_name) == 0) { start[0] = ex_id_lkup (exoid, obj_type, value); if (exerrval != EX_LOOKUPFAIL) /* found the id */ { exerrval = EX_BADPARAM; sprintf(errmsg, "Warning: attempt to assign duplicate %s ID %d in file id %d", ex_name_of_object(obj_type), value, exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_WARN); } } start[0] = ex_id_lkup (exoid, obj_type, obj_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: no properties allowed for NULL %s id %d in file id %d", ex_name_of_object(obj_type), obj_id,exoid); ex_err("ex_put_prop",errmsg,EX_MSG); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to find value %d in %s property array in file id %d", obj_id, ex_name_of_object(obj_type), exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } } start[0] = start[0] - 1; ldum = (int)value; if ((status = nc_put_var1_int(exoid, propid, start, &ldum)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store property value in file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: nc_set_fill(exoid, oldfill, &temp); /* default: nofill */ if (nc_enddef (exoid) != NC_NOERR) { /* exit define mode */ sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_prop",errmsg,exerrval); } return (EX_FATAL); }
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() {/* create ctest0_64.nc */ int stat; /* return status */ int ncid; /* netCDF id */ /* dimension ids */ int Dr_dim; int D1_dim; int D2_dim; int D3_dim; int dim_MINUS_name_MINUS_dashes_dim; int dim_PERIOD_name_PERIOD_dots_dim; int dim_PLUS_name_PLUS_plusses_dim; int dim_ATSIGN_name_ATSIGN_ats_dim; /* dimension lengths */ size_t Dr_len = NC_UNLIMITED; size_t D1_len = 1; size_t D2_len = 2; size_t D3_len = 3; size_t dim_MINUS_name_MINUS_dashes_len = 4; size_t dim_PERIOD_name_PERIOD_dots_len = 5; size_t dim_PLUS_name_PLUS_plusses_len = 6; size_t dim_ATSIGN_name_ATSIGN_ats_len = 7; /* variable ids */ int c_id; int b_id; int s_id; int i_id; int f_id; int d_id; int cr_id; int br_id; int sr_id; int ir_id; int fr_id; int dr_id; int c1_id; int b1_id; int s1_id; int i1_id; int f1_id; int d1_id; int c2_id; int b2_id; int s2_id; int i2_id; int f2_id; int d2_id; int c3_id; int b3_id; int s3_id; int i3_id; int f3_id; int d3_id; int cr1_id; int br2_id; int sr3_id; int f11_id; int d12_id; int c13_id; int s21_id; int i22_id; int f23_id; int c31_id; int b32_id; int s33_id; int sr11_id; int ir12_id; int fr13_id; int cr21_id; int br22_id; int sr23_id; int fr31_id; int dr32_id; int cr33_id; int c111_id; int b112_id; int s113_id; int f121_id; int d122_id; int c123_id; int s131_id; int i132_id; int f133_id; int f211_id; int d212_id; int c213_id; int s221_id; int i222_id; int f223_id; int c231_id; int b232_id; int s233_id; int s311_id; int i312_id; int f313_id; int var_MINUS_name_MINUS_dashes_id; int var_PERIOD_name_PERIOD_dots_id; int var_PLUS_name_PLUS_plusses_id; int var_ATSIGN_name_ATSIGN_ats_id; /* rank (number of dimensions) for each variable */ # define RANK_c 0 # define RANK_b 0 # define RANK_s 0 # define RANK_i 0 # define RANK_f 0 # define RANK_d 0 # define RANK_cr 1 # define RANK_br 1 # define RANK_sr 1 # define RANK_ir 1 # define RANK_fr 1 # define RANK_dr 1 # define RANK_c1 1 # define RANK_b1 1 # define RANK_s1 1 # define RANK_i1 1 # define RANK_f1 1 # define RANK_d1 1 # define RANK_c2 1 # define RANK_b2 1 # define RANK_s2 1 # define RANK_i2 1 # define RANK_f2 1 # define RANK_d2 1 # define RANK_c3 1 # define RANK_b3 1 # define RANK_s3 1 # define RANK_i3 1 # define RANK_f3 1 # define RANK_d3 1 # define RANK_cr1 2 # define RANK_br2 2 # define RANK_sr3 2 # define RANK_f11 2 # define RANK_d12 2 # define RANK_c13 2 # define RANK_s21 2 # define RANK_i22 2 # define RANK_f23 2 # define RANK_c31 2 # define RANK_b32 2 # define RANK_s33 2 # define RANK_sr11 3 # define RANK_ir12 3 # define RANK_fr13 3 # define RANK_cr21 3 # define RANK_br22 3 # define RANK_sr23 3 # define RANK_fr31 3 # define RANK_dr32 3 # define RANK_cr33 3 # define RANK_c111 3 # define RANK_b112 3 # define RANK_s113 3 # define RANK_f121 3 # define RANK_d122 3 # define RANK_c123 3 # define RANK_s131 3 # define RANK_i132 3 # define RANK_f133 3 # define RANK_f211 3 # define RANK_d212 3 # define RANK_c213 3 # define RANK_s221 3 # define RANK_i222 3 # define RANK_f223 3 # define RANK_c231 3 # define RANK_b232 3 # define RANK_s233 3 # define RANK_s311 3 # define RANK_i312 3 # define RANK_f313 3 # define RANK_var_MINUS_name_MINUS_dashes 0 # define RANK_var_PERIOD_name_PERIOD_dots 0 # define RANK_var_PLUS_name_PLUS_plusses 0 # define RANK_var_ATSIGN_name_ATSIGN_ats 0 /* variable shapes */ int cr_dims[RANK_cr]; int br_dims[RANK_br]; int sr_dims[RANK_sr]; int ir_dims[RANK_ir]; int fr_dims[RANK_fr]; int dr_dims[RANK_dr]; int c1_dims[RANK_c1]; int b1_dims[RANK_b1]; int s1_dims[RANK_s1]; int i1_dims[RANK_i1]; int f1_dims[RANK_f1]; int d1_dims[RANK_d1]; int c2_dims[RANK_c2]; int b2_dims[RANK_b2]; int s2_dims[RANK_s2]; int i2_dims[RANK_i2]; int f2_dims[RANK_f2]; int d2_dims[RANK_d2]; int c3_dims[RANK_c3]; int b3_dims[RANK_b3]; int s3_dims[RANK_s3]; int i3_dims[RANK_i3]; int f3_dims[RANK_f3]; int d3_dims[RANK_d3]; int cr1_dims[RANK_cr1]; int br2_dims[RANK_br2]; int sr3_dims[RANK_sr3]; int f11_dims[RANK_f11]; int d12_dims[RANK_d12]; int c13_dims[RANK_c13]; int s21_dims[RANK_s21]; int i22_dims[RANK_i22]; int f23_dims[RANK_f23]; int c31_dims[RANK_c31]; int b32_dims[RANK_b32]; int s33_dims[RANK_s33]; int sr11_dims[RANK_sr11]; int ir12_dims[RANK_ir12]; int fr13_dims[RANK_fr13]; int cr21_dims[RANK_cr21]; int br22_dims[RANK_br22]; int sr23_dims[RANK_sr23]; int fr31_dims[RANK_fr31]; int dr32_dims[RANK_dr32]; int cr33_dims[RANK_cr33]; int c111_dims[RANK_c111]; int b112_dims[RANK_b112]; int s113_dims[RANK_s113]; int f121_dims[RANK_f121]; int d122_dims[RANK_d122]; int c123_dims[RANK_c123]; int s131_dims[RANK_s131]; int i132_dims[RANK_i132]; int f133_dims[RANK_f133]; int f211_dims[RANK_f211]; int d212_dims[RANK_d212]; int c213_dims[RANK_c213]; int s221_dims[RANK_s221]; int i222_dims[RANK_i222]; int f223_dims[RANK_f223]; int c231_dims[RANK_c231]; int b232_dims[RANK_b232]; int s233_dims[RANK_s233]; int s311_dims[RANK_s311]; int i312_dims[RANK_i312]; int f313_dims[RANK_f313]; /* enter define mode */ stat = nc_create("ctest0_64.nc", NC_CLOBBER|NC_64BIT_OFFSET, &ncid); check_err(stat,__LINE__,__FILE__); /* define dimensions */ stat = nc_def_dim(ncid, "Dr", Dr_len, &Dr_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "D1", D1_len, &D1_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "D2", D2_len, &D2_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "D3", D3_len, &D3_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "dim-name-dashes", dim_MINUS_name_MINUS_dashes_len, &dim_MINUS_name_MINUS_dashes_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "dim.name.dots", dim_PERIOD_name_PERIOD_dots_len, &dim_PERIOD_name_PERIOD_dots_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "dim+name+plusses", dim_PLUS_name_PLUS_plusses_len, &dim_PLUS_name_PLUS_plusses_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "dim@name@ats", dim_ATSIGN_name_ATSIGN_ats_len, &dim_ATSIGN_name_ATSIGN_ats_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ stat = nc_def_var(ncid, "c", NC_CHAR, RANK_c, 0, &c_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "b", NC_BYTE, RANK_b, 0, &b_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "s", NC_SHORT, RANK_s, 0, &s_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "i", NC_INT, RANK_i, 0, &i_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "f", NC_FLOAT, RANK_f, 0, &f_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "d", NC_DOUBLE, RANK_d, 0, &d_id); check_err(stat,__LINE__,__FILE__); cr_dims[0] = Dr_dim; stat = nc_def_var(ncid, "cr", NC_CHAR, RANK_cr, cr_dims, &cr_id); check_err(stat,__LINE__,__FILE__); br_dims[0] = Dr_dim; stat = nc_def_var(ncid, "br", NC_BYTE, RANK_br, br_dims, &br_id); check_err(stat,__LINE__,__FILE__); sr_dims[0] = Dr_dim; stat = nc_def_var(ncid, "sr", NC_SHORT, RANK_sr, sr_dims, &sr_id); check_err(stat,__LINE__,__FILE__); ir_dims[0] = Dr_dim; stat = nc_def_var(ncid, "ir", NC_INT, RANK_ir, ir_dims, &ir_id); check_err(stat,__LINE__,__FILE__); fr_dims[0] = Dr_dim; stat = nc_def_var(ncid, "fr", NC_FLOAT, RANK_fr, fr_dims, &fr_id); check_err(stat,__LINE__,__FILE__); dr_dims[0] = Dr_dim; stat = nc_def_var(ncid, "dr", NC_DOUBLE, RANK_dr, dr_dims, &dr_id); check_err(stat,__LINE__,__FILE__); c1_dims[0] = D1_dim; stat = nc_def_var(ncid, "c1", NC_CHAR, RANK_c1, c1_dims, &c1_id); check_err(stat,__LINE__,__FILE__); b1_dims[0] = D1_dim; stat = nc_def_var(ncid, "b1", NC_BYTE, RANK_b1, b1_dims, &b1_id); check_err(stat,__LINE__,__FILE__); s1_dims[0] = D1_dim; stat = nc_def_var(ncid, "s1", NC_SHORT, RANK_s1, s1_dims, &s1_id); check_err(stat,__LINE__,__FILE__); i1_dims[0] = D1_dim; stat = nc_def_var(ncid, "i1", NC_INT, RANK_i1, i1_dims, &i1_id); check_err(stat,__LINE__,__FILE__); f1_dims[0] = D1_dim; stat = nc_def_var(ncid, "f1", NC_FLOAT, RANK_f1, f1_dims, &f1_id); check_err(stat,__LINE__,__FILE__); d1_dims[0] = D1_dim; stat = nc_def_var(ncid, "d1", NC_DOUBLE, RANK_d1, d1_dims, &d1_id); check_err(stat,__LINE__,__FILE__); c2_dims[0] = D2_dim; stat = nc_def_var(ncid, "c2", NC_CHAR, RANK_c2, c2_dims, &c2_id); check_err(stat,__LINE__,__FILE__); b2_dims[0] = D2_dim; stat = nc_def_var(ncid, "b2", NC_BYTE, RANK_b2, b2_dims, &b2_id); check_err(stat,__LINE__,__FILE__); s2_dims[0] = D2_dim; stat = nc_def_var(ncid, "s2", NC_SHORT, RANK_s2, s2_dims, &s2_id); check_err(stat,__LINE__,__FILE__); i2_dims[0] = D2_dim; stat = nc_def_var(ncid, "i2", NC_INT, RANK_i2, i2_dims, &i2_id); check_err(stat,__LINE__,__FILE__); f2_dims[0] = D2_dim; stat = nc_def_var(ncid, "f2", NC_FLOAT, RANK_f2, f2_dims, &f2_id); check_err(stat,__LINE__,__FILE__); d2_dims[0] = D2_dim; stat = nc_def_var(ncid, "d2", NC_DOUBLE, RANK_d2, d2_dims, &d2_id); check_err(stat,__LINE__,__FILE__); c3_dims[0] = D3_dim; stat = nc_def_var(ncid, "c3", NC_CHAR, RANK_c3, c3_dims, &c3_id); check_err(stat,__LINE__,__FILE__); b3_dims[0] = D3_dim; stat = nc_def_var(ncid, "b3", NC_BYTE, RANK_b3, b3_dims, &b3_id); check_err(stat,__LINE__,__FILE__); s3_dims[0] = D3_dim; stat = nc_def_var(ncid, "s3", NC_SHORT, RANK_s3, s3_dims, &s3_id); check_err(stat,__LINE__,__FILE__); i3_dims[0] = D3_dim; stat = nc_def_var(ncid, "i3", NC_INT, RANK_i3, i3_dims, &i3_id); check_err(stat,__LINE__,__FILE__); f3_dims[0] = D3_dim; stat = nc_def_var(ncid, "f3", NC_FLOAT, RANK_f3, f3_dims, &f3_id); check_err(stat,__LINE__,__FILE__); d3_dims[0] = D3_dim; stat = nc_def_var(ncid, "d3", NC_DOUBLE, RANK_d3, d3_dims, &d3_id); check_err(stat,__LINE__,__FILE__); cr1_dims[0] = Dr_dim; cr1_dims[1] = D1_dim; stat = nc_def_var(ncid, "cr1", NC_CHAR, RANK_cr1, cr1_dims, &cr1_id); check_err(stat,__LINE__,__FILE__); br2_dims[0] = Dr_dim; br2_dims[1] = D2_dim; stat = nc_def_var(ncid, "br2", NC_BYTE, RANK_br2, br2_dims, &br2_id); check_err(stat,__LINE__,__FILE__); sr3_dims[0] = Dr_dim; sr3_dims[1] = D3_dim; stat = nc_def_var(ncid, "sr3", NC_SHORT, RANK_sr3, sr3_dims, &sr3_id); check_err(stat,__LINE__,__FILE__); f11_dims[0] = D1_dim; f11_dims[1] = D1_dim; stat = nc_def_var(ncid, "f11", NC_FLOAT, RANK_f11, f11_dims, &f11_id); check_err(stat,__LINE__,__FILE__); d12_dims[0] = D1_dim; d12_dims[1] = D2_dim; stat = nc_def_var(ncid, "d12", NC_DOUBLE, RANK_d12, d12_dims, &d12_id); check_err(stat,__LINE__,__FILE__); c13_dims[0] = D1_dim; c13_dims[1] = D3_dim; stat = nc_def_var(ncid, "c13", NC_CHAR, RANK_c13, c13_dims, &c13_id); check_err(stat,__LINE__,__FILE__); s21_dims[0] = D2_dim; s21_dims[1] = D1_dim; stat = nc_def_var(ncid, "s21", NC_SHORT, RANK_s21, s21_dims, &s21_id); check_err(stat,__LINE__,__FILE__); i22_dims[0] = D2_dim; i22_dims[1] = D2_dim; stat = nc_def_var(ncid, "i22", NC_INT, RANK_i22, i22_dims, &i22_id); check_err(stat,__LINE__,__FILE__); f23_dims[0] = D2_dim; f23_dims[1] = D3_dim; stat = nc_def_var(ncid, "f23", NC_FLOAT, RANK_f23, f23_dims, &f23_id); check_err(stat,__LINE__,__FILE__); c31_dims[0] = D3_dim; c31_dims[1] = D1_dim; stat = nc_def_var(ncid, "c31", NC_CHAR, RANK_c31, c31_dims, &c31_id); check_err(stat,__LINE__,__FILE__); b32_dims[0] = D3_dim; b32_dims[1] = D2_dim; stat = nc_def_var(ncid, "b32", NC_BYTE, RANK_b32, b32_dims, &b32_id); check_err(stat,__LINE__,__FILE__); s33_dims[0] = D3_dim; s33_dims[1] = D3_dim; stat = nc_def_var(ncid, "s33", NC_SHORT, RANK_s33, s33_dims, &s33_id); check_err(stat,__LINE__,__FILE__); sr11_dims[0] = Dr_dim; sr11_dims[1] = D1_dim; sr11_dims[2] = D1_dim; stat = nc_def_var(ncid, "sr11", NC_SHORT, RANK_sr11, sr11_dims, &sr11_id); check_err(stat,__LINE__,__FILE__); ir12_dims[0] = Dr_dim; ir12_dims[1] = D1_dim; ir12_dims[2] = D2_dim; stat = nc_def_var(ncid, "ir12", NC_INT, RANK_ir12, ir12_dims, &ir12_id); check_err(stat,__LINE__,__FILE__); fr13_dims[0] = Dr_dim; fr13_dims[1] = D1_dim; fr13_dims[2] = D3_dim; stat = nc_def_var(ncid, "fr13", NC_FLOAT, RANK_fr13, fr13_dims, &fr13_id); check_err(stat,__LINE__,__FILE__); cr21_dims[0] = Dr_dim; cr21_dims[1] = D2_dim; cr21_dims[2] = D1_dim; stat = nc_def_var(ncid, "cr21", NC_CHAR, RANK_cr21, cr21_dims, &cr21_id); check_err(stat,__LINE__,__FILE__); br22_dims[0] = Dr_dim; br22_dims[1] = D2_dim; br22_dims[2] = D2_dim; stat = nc_def_var(ncid, "br22", NC_BYTE, RANK_br22, br22_dims, &br22_id); check_err(stat,__LINE__,__FILE__); sr23_dims[0] = Dr_dim; sr23_dims[1] = D2_dim; sr23_dims[2] = D3_dim; stat = nc_def_var(ncid, "sr23", NC_SHORT, RANK_sr23, sr23_dims, &sr23_id); check_err(stat,__LINE__,__FILE__); fr31_dims[0] = Dr_dim; fr31_dims[1] = D3_dim; fr31_dims[2] = D1_dim; stat = nc_def_var(ncid, "fr31", NC_FLOAT, RANK_fr31, fr31_dims, &fr31_id); check_err(stat,__LINE__,__FILE__); dr32_dims[0] = Dr_dim; dr32_dims[1] = D3_dim; dr32_dims[2] = D2_dim; stat = nc_def_var(ncid, "dr32", NC_DOUBLE, RANK_dr32, dr32_dims, &dr32_id); check_err(stat,__LINE__,__FILE__); cr33_dims[0] = Dr_dim; cr33_dims[1] = D3_dim; cr33_dims[2] = D3_dim; stat = nc_def_var(ncid, "cr33", NC_CHAR, RANK_cr33, cr33_dims, &cr33_id); check_err(stat,__LINE__,__FILE__); c111_dims[0] = D1_dim; c111_dims[1] = D1_dim; c111_dims[2] = D1_dim; stat = nc_def_var(ncid, "c111", NC_CHAR, RANK_c111, c111_dims, &c111_id); check_err(stat,__LINE__,__FILE__); b112_dims[0] = D1_dim; b112_dims[1] = D1_dim; b112_dims[2] = D2_dim; stat = nc_def_var(ncid, "b112", NC_BYTE, RANK_b112, b112_dims, &b112_id); check_err(stat,__LINE__,__FILE__); s113_dims[0] = D1_dim; s113_dims[1] = D1_dim; s113_dims[2] = D3_dim; stat = nc_def_var(ncid, "s113", NC_SHORT, RANK_s113, s113_dims, &s113_id); check_err(stat,__LINE__,__FILE__); f121_dims[0] = D1_dim; f121_dims[1] = D2_dim; f121_dims[2] = D1_dim; stat = nc_def_var(ncid, "f121", NC_FLOAT, RANK_f121, f121_dims, &f121_id); check_err(stat,__LINE__,__FILE__); d122_dims[0] = D1_dim; d122_dims[1] = D2_dim; d122_dims[2] = D2_dim; stat = nc_def_var(ncid, "d122", NC_DOUBLE, RANK_d122, d122_dims, &d122_id); check_err(stat,__LINE__,__FILE__); c123_dims[0] = D1_dim; c123_dims[1] = D2_dim; c123_dims[2] = D3_dim; stat = nc_def_var(ncid, "c123", NC_CHAR, RANK_c123, c123_dims, &c123_id); check_err(stat,__LINE__,__FILE__); s131_dims[0] = D1_dim; s131_dims[1] = D3_dim; s131_dims[2] = D1_dim; stat = nc_def_var(ncid, "s131", NC_SHORT, RANK_s131, s131_dims, &s131_id); check_err(stat,__LINE__,__FILE__); i132_dims[0] = D1_dim; i132_dims[1] = D3_dim; i132_dims[2] = D2_dim; stat = nc_def_var(ncid, "i132", NC_INT, RANK_i132, i132_dims, &i132_id); check_err(stat,__LINE__,__FILE__); f133_dims[0] = D1_dim; f133_dims[1] = D3_dim; f133_dims[2] = D3_dim; stat = nc_def_var(ncid, "f133", NC_FLOAT, RANK_f133, f133_dims, &f133_id); check_err(stat,__LINE__,__FILE__); f211_dims[0] = D2_dim; f211_dims[1] = D1_dim; f211_dims[2] = D1_dim; stat = nc_def_var(ncid, "f211", NC_FLOAT, RANK_f211, f211_dims, &f211_id); check_err(stat,__LINE__,__FILE__); d212_dims[0] = D2_dim; d212_dims[1] = D1_dim; d212_dims[2] = D2_dim; stat = nc_def_var(ncid, "d212", NC_DOUBLE, RANK_d212, d212_dims, &d212_id); check_err(stat,__LINE__,__FILE__); c213_dims[0] = D2_dim; c213_dims[1] = D1_dim; c213_dims[2] = D3_dim; stat = nc_def_var(ncid, "c213", NC_CHAR, RANK_c213, c213_dims, &c213_id); check_err(stat,__LINE__,__FILE__); s221_dims[0] = D2_dim; s221_dims[1] = D2_dim; s221_dims[2] = D1_dim; stat = nc_def_var(ncid, "s221", NC_SHORT, RANK_s221, s221_dims, &s221_id); check_err(stat,__LINE__,__FILE__); i222_dims[0] = D2_dim; i222_dims[1] = D2_dim; i222_dims[2] = D2_dim; stat = nc_def_var(ncid, "i222", NC_INT, RANK_i222, i222_dims, &i222_id); check_err(stat,__LINE__,__FILE__); f223_dims[0] = D2_dim; f223_dims[1] = D2_dim; f223_dims[2] = D3_dim; stat = nc_def_var(ncid, "f223", NC_FLOAT, RANK_f223, f223_dims, &f223_id); check_err(stat,__LINE__,__FILE__); c231_dims[0] = D2_dim; c231_dims[1] = D3_dim; c231_dims[2] = D1_dim; stat = nc_def_var(ncid, "c231", NC_CHAR, RANK_c231, c231_dims, &c231_id); check_err(stat,__LINE__,__FILE__); b232_dims[0] = D2_dim; b232_dims[1] = D3_dim; b232_dims[2] = D2_dim; stat = nc_def_var(ncid, "b232", NC_BYTE, RANK_b232, b232_dims, &b232_id); check_err(stat,__LINE__,__FILE__); s233_dims[0] = D2_dim; s233_dims[1] = D3_dim; s233_dims[2] = D3_dim; stat = nc_def_var(ncid, "s233", NC_SHORT, RANK_s233, s233_dims, &s233_id); check_err(stat,__LINE__,__FILE__); s311_dims[0] = D3_dim; s311_dims[1] = D1_dim; s311_dims[2] = D1_dim; stat = nc_def_var(ncid, "s311", NC_SHORT, RANK_s311, s311_dims, &s311_id); check_err(stat,__LINE__,__FILE__); i312_dims[0] = D3_dim; i312_dims[1] = D1_dim; i312_dims[2] = D2_dim; stat = nc_def_var(ncid, "i312", NC_INT, RANK_i312, i312_dims, &i312_id); check_err(stat,__LINE__,__FILE__); f313_dims[0] = D3_dim; f313_dims[1] = D1_dim; f313_dims[2] = D3_dim; stat = nc_def_var(ncid, "f313", NC_FLOAT, RANK_f313, f313_dims, &f313_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "var-name-dashes", NC_DOUBLE, RANK_var_MINUS_name_MINUS_dashes, 0, &var_MINUS_name_MINUS_dashes_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "var.name.dots", NC_DOUBLE, RANK_var_PERIOD_name_PERIOD_dots, 0, &var_PERIOD_name_PERIOD_dots_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "var+name+plusses", NC_DOUBLE, RANK_var_PLUS_name_PLUS_plusses, 0, &var_PLUS_name_PLUS_plusses_id); check_err(stat,__LINE__,__FILE__); stat = nc_def_var(ncid, "var@name@ats", NC_DOUBLE, RANK_var_ATSIGN_name_ATSIGN_ats, 0, &var_ATSIGN_name_ATSIGN_ats_id); check_err(stat,__LINE__,__FILE__); /* assign global attributes */ { stat = nc_put_att_text(ncid, NC_GLOBAL, "Gc", 1, ""); check_err(stat,__LINE__,__FILE__); } { static const signed char c0_Gb_att[2] = {-128, 127} ; stat = nc_put_att_schar(ncid, NC_GLOBAL, "Gb", NC_BYTE, 2, c0_Gb_att); check_err(stat,__LINE__,__FILE__); } { static const short c0_Gs_att[3] = {-32768, 0, 32767} ; stat = nc_put_att_short(ncid, NC_GLOBAL, "Gs", NC_SHORT, 3, c0_Gs_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_Gi_att[3] = {-2147483647, 0, 2147483647} ; stat = nc_put_att_int(ncid, NC_GLOBAL, "Gi", NC_INT, 3, c0_Gi_att); check_err(stat,__LINE__,__FILE__); } { static const float c0_Gf_att[3] = {((float)-9.9999996e+35), ((float)0), ((float)9.9999996e+35)} ; stat = nc_put_att_float(ncid, NC_GLOBAL, "Gf", NC_FLOAT, 3, c0_Gf_att); check_err(stat,__LINE__,__FILE__); } { static const double c0_Gd_att[3] = {((double)-1e+308), ((double)0), ((double)1e+308)} ; stat = nc_put_att_double(ncid, NC_GLOBAL, "Gd", NC_DOUBLE, 3, c0_Gd_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_Gatt_MINUS_name_MINUS_dashes_att[1] = {-1} ; stat = nc_put_att_int(ncid, NC_GLOBAL, "Gatt-name-dashes", NC_INT, 1, c0_Gatt_MINUS_name_MINUS_dashes_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_Gatt_DOT_name_DOT_dots_att[1] = {-2} ; stat = nc_put_att_int(ncid, NC_GLOBAL, "Gatt.name.dots", NC_INT, 1, c0_Gatt_DOT_name_DOT_dots_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_Gatt_PLUS_name_PLUS_plusses_att[1] = {-3} ; stat = nc_put_att_int(ncid, NC_GLOBAL, "Gatt+name+plusses", NC_INT, 1, c0_Gatt_PLUS_name_PLUS_plusses_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_Gatt_ATSIGN_name_ATSIGN_ats_att[1] = {-4} ; stat = nc_put_att_int(ncid, NC_GLOBAL, "Gatt@name@ats", NC_INT, 1, c0_Gatt_ATSIGN_name_ATSIGN_ats_att); check_err(stat,__LINE__,__FILE__); } /* assign per-variable attributes */ { static const int c0_att_MINUS_name_MINUS_dashes_att[1] = {4} ; stat = nc_put_att_int(ncid, c_id, "att-name-dashes", NC_INT, 1, c0_att_MINUS_name_MINUS_dashes_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_att_DOT_name_DOT_dots_att[1] = {5} ; stat = nc_put_att_int(ncid, c_id, "att.name.dots", NC_INT, 1, c0_att_DOT_name_DOT_dots_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_att_PLUS_name_PLUS_plusses_att[1] = {6} ; stat = nc_put_att_int(ncid, c_id, "att+name+plusses", NC_INT, 1, c0_att_PLUS_name_PLUS_plusses_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_att_ATSIGN_name_ATSIGN_ats_att[1] = {7} ; stat = nc_put_att_int(ncid, c_id, "att@name@ats", NC_INT, 1, c0_att_ATSIGN_name_ATSIGN_ats_att); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(ncid, b_id, "c", 1, ""); check_err(stat,__LINE__,__FILE__); } { static const signed char c0_b_att[4] = {0, 127, -128, -1} ; stat = nc_put_att_schar(ncid, s_id, "b", NC_BYTE, 4, c0_b_att); check_err(stat,__LINE__,__FILE__); } { static const short c0_s_att[3] = {-32768, 0, 32767} ; stat = nc_put_att_short(ncid, s_id, "s", NC_SHORT, 3, c0_s_att); check_err(stat,__LINE__,__FILE__); } { static const int c0_i_att[3] = {-2147483647, 0, 2147483647} ; stat = nc_put_att_int(ncid, i_id, "i", NC_INT, 3, c0_i_att); check_err(stat,__LINE__,__FILE__); } { static const float c0_f_att[3] = {((float)-9.9999996e+35), ((float)0), ((float)9.9999996e+35)} ; stat = nc_put_att_float(ncid, i_id, "f", NC_FLOAT, 3, c0_f_att); check_err(stat,__LINE__,__FILE__); } { static const double c0_d_att[3] = {((double)-1e+308), ((double)0), ((double)1e+308)} ; stat = nc_put_att_double(ncid, i_id, "d", NC_DOUBLE, 3, c0_d_att); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(ncid, f_id, "c", 1, "x"); check_err(stat,__LINE__,__FILE__); } { stat = nc_put_att_text(ncid, d_id, "c", 8, "abcd\tZ$&"); check_err(stat,__LINE__,__FILE__); } /* leave define mode */ stat = nc_enddef (ncid); check_err(stat,__LINE__,__FILE__); /* assign variable data */ { size_t count = 0; static char c_data[1] = {'2'}; stat = nc_put_var1(ncid, c_id, &count, c_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static signed char b_data[1] = {-2}; stat = nc_put_var1(ncid, b_id, &count, b_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static short s_data[1] = {-5}; stat = nc_put_var1(ncid, s_id, &count, s_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static int i_data[1] = {-20}; stat = nc_put_var1(ncid, i_id, &count, i_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static float f_data[1] = {((float)-9)}; stat = nc_put_var1(ncid, f_id, &count, f_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static double d_data[1] = {((double)-10)}; stat = nc_put_var1(ncid, d_id, &count, d_data); check_err(stat,__LINE__,__FILE__); } { char* cr_data = "ab\000\000" ; size_t cr_startset[1] = {0} ; size_t cr_countset[1] = {2}; stat = nc_put_vara(ncid, cr_id, cr_startset, cr_countset, cr_data); check_err(stat,__LINE__,__FILE__); } { signed char br_data[2] = {-128, 127} ; size_t br_startset[1] = {0} ; size_t br_countset[1] = {2}; stat = nc_put_vara(ncid, br_id, br_startset, br_countset, br_data); check_err(stat,__LINE__,__FILE__); } { short sr_data[2] = {-32768, 32767} ; size_t sr_startset[1] = {0} ; size_t sr_countset[1] = {2}; stat = nc_put_vara(ncid, sr_id, sr_startset, sr_countset, sr_data); check_err(stat,__LINE__,__FILE__); } { int ir_data[2] = {-2147483646, 2147483647} ; size_t ir_startset[1] = {0} ; size_t ir_countset[1] = {2}; stat = nc_put_vara(ncid, ir_id, ir_startset, ir_countset, ir_data); check_err(stat,__LINE__,__FILE__); } { float fr_data[2] = {((float)-9.9999996e+35), ((float)9.9999996e+35)} ; size_t fr_startset[1] = {0} ; size_t fr_countset[1] = {2}; stat = nc_put_vara(ncid, fr_id, fr_startset, fr_countset, fr_data); check_err(stat,__LINE__,__FILE__); } { double dr_data[2] = {((double)-1e+308), ((double)1e+308)} ; size_t dr_startset[1] = {0} ; size_t dr_countset[1] = {2}; stat = nc_put_vara(ncid, dr_id, dr_startset, dr_countset, dr_data); check_err(stat,__LINE__,__FILE__); } { char* c1_data = "\000" ; size_t c1_startset[1] = {0} ; size_t c1_countset[1] = {1}; stat = nc_put_vara(ncid, c1_id, c1_startset, c1_countset, c1_data); check_err(stat,__LINE__,__FILE__); } { signed char b1_data[1] = {-128} ; size_t b1_startset[1] = {0} ; size_t b1_countset[1] = {1}; stat = nc_put_vara(ncid, b1_id, b1_startset, b1_countset, b1_data); check_err(stat,__LINE__,__FILE__); } { short s1_data[1] = {-32768} ; size_t s1_startset[1] = {0} ; size_t s1_countset[1] = {1}; stat = nc_put_vara(ncid, s1_id, s1_startset, s1_countset, s1_data); check_err(stat,__LINE__,__FILE__); } { int i1_data[1] = {-2147483646} ; size_t i1_startset[1] = {0} ; size_t i1_countset[1] = {1}; stat = nc_put_vara(ncid, i1_id, i1_startset, i1_countset, i1_data); check_err(stat,__LINE__,__FILE__); } { float f1_data[1] = {((float)-9.9999996e+35)} ; size_t f1_startset[1] = {0} ; size_t f1_countset[1] = {1}; stat = nc_put_vara(ncid, f1_id, f1_startset, f1_countset, f1_data); check_err(stat,__LINE__,__FILE__); } { double d1_data[1] = {((double)-1e+308)} ; size_t d1_startset[1] = {0} ; size_t d1_countset[1] = {1}; stat = nc_put_vara(ncid, d1_id, d1_startset, d1_countset, d1_data); check_err(stat,__LINE__,__FILE__); } { char* c2_data = "ab\000\000" ; size_t c2_startset[1] = {0} ; size_t c2_countset[1] = {2}; stat = nc_put_vara(ncid, c2_id, c2_startset, c2_countset, c2_data); check_err(stat,__LINE__,__FILE__); } { signed char b2_data[2] = {-128, 127} ; size_t b2_startset[1] = {0} ; size_t b2_countset[1] = {2}; stat = nc_put_vara(ncid, b2_id, b2_startset, b2_countset, b2_data); check_err(stat,__LINE__,__FILE__); } { short s2_data[2] = {-32768, 32767} ; size_t s2_startset[1] = {0} ; size_t s2_countset[1] = {2}; stat = nc_put_vara(ncid, s2_id, s2_startset, s2_countset, s2_data); check_err(stat,__LINE__,__FILE__); } { int i2_data[2] = {-2147483646, 2147483647} ; size_t i2_startset[1] = {0} ; size_t i2_countset[1] = {2}; stat = nc_put_vara(ncid, i2_id, i2_startset, i2_countset, i2_data); check_err(stat,__LINE__,__FILE__); } { float f2_data[2] = {((float)-9.9999996e+35), ((float)9.9999996e+35)} ; size_t f2_startset[1] = {0} ; size_t f2_countset[1] = {2}; stat = nc_put_vara(ncid, f2_id, f2_startset, f2_countset, f2_data); check_err(stat,__LINE__,__FILE__); } { double d2_data[2] = {((double)-1e+308), ((double)1e+308)} ; size_t d2_startset[1] = {0} ; size_t d2_countset[1] = {2}; stat = nc_put_vara(ncid, d2_id, d2_startset, d2_countset, d2_data); check_err(stat,__LINE__,__FILE__); } { char* c3_data = "\001À.\000\000\000\000\000\000" ; size_t c3_startset[1] = {0} ; size_t c3_countset[1] = {3}; stat = nc_put_vara(ncid, c3_id, c3_startset, c3_countset, c3_data); check_err(stat,__LINE__,__FILE__); } { signed char b3_data[3] = {-128, 127, -1} ; size_t b3_startset[1] = {0} ; size_t b3_countset[1] = {3}; stat = nc_put_vara(ncid, b3_id, b3_startset, b3_countset, b3_data); check_err(stat,__LINE__,__FILE__); } { short s3_data[3] = {-32768, 0, 32767} ; size_t s3_startset[1] = {0} ; size_t s3_countset[1] = {3}; stat = nc_put_vara(ncid, s3_id, s3_startset, s3_countset, s3_data); check_err(stat,__LINE__,__FILE__); } { int i3_data[3] = {-2147483646, 0, 2147483647} ; size_t i3_startset[1] = {0} ; size_t i3_countset[1] = {3}; stat = nc_put_vara(ncid, i3_id, i3_startset, i3_countset, i3_data); check_err(stat,__LINE__,__FILE__); } { float f3_data[3] = {((float)-9.9999996e+35), ((float)0), ((float)9.9999996e+35)} ; size_t f3_startset[1] = {0} ; size_t f3_countset[1] = {3}; stat = nc_put_vara(ncid, f3_id, f3_startset, f3_countset, f3_data); check_err(stat,__LINE__,__FILE__); } { double d3_data[3] = {((double)-1e+308), ((double)0), ((double)1e+308)} ; size_t d3_startset[1] = {0} ; size_t d3_countset[1] = {3}; stat = nc_put_vara(ncid, d3_id, d3_startset, d3_countset, d3_data); check_err(stat,__LINE__,__FILE__); } { char* cr1_data = "xy" ; size_t cr1_startset[2] = {0, 0} ; size_t cr1_countset[2] = {2, 1}; stat = nc_put_vara(ncid, cr1_id, cr1_startset, cr1_countset, cr1_data); check_err(stat,__LINE__,__FILE__); } { signed char br2_data[4] = {-24, -26, -20, -22} ; size_t br2_startset[2] = {0, 0} ; size_t br2_countset[2] = {2, 2}; stat = nc_put_vara(ncid, br2_id, br2_startset, br2_countset, br2_data); check_err(stat,__LINE__,__FILE__); } { short sr3_data[6] = {-375, -380, -385, -350, -355, -360} ; size_t sr3_startset[2] = {0, 0} ; size_t sr3_countset[2] = {2, 3}; stat = nc_put_vara(ncid, sr3_id, sr3_startset, sr3_countset, sr3_data); check_err(stat,__LINE__,__FILE__); } { float f11_data[1] = {((float)-2187)} ; size_t f11_startset[2] = {0, 0} ; size_t f11_countset[2] = {1, 1}; stat = nc_put_vara(ncid, f11_id, f11_startset, f11_countset, f11_data); check_err(stat,__LINE__,__FILE__); } { double d12_data[2] = {((double)-3000), ((double)-3010)} ; size_t d12_startset[2] = {0, 0} ; size_t d12_countset[2] = {1, 2}; stat = nc_put_vara(ncid, d12_id, d12_startset, d12_countset, d12_data); check_err(stat,__LINE__,__FILE__); } { char* c13_data = "\tb\177" ; size_t c13_startset[2] = {0, 0} ; size_t c13_countset[2] = {1, 3}; stat = nc_put_vara(ncid, c13_id, c13_startset, c13_countset, c13_data); check_err(stat,__LINE__,__FILE__); } { short s21_data[2] = {-375, -350} ; size_t s21_startset[2] = {0, 0} ; size_t s21_countset[2] = {2, 1}; stat = nc_put_vara(ncid, s21_id, s21_startset, s21_countset, s21_data); check_err(stat,__LINE__,__FILE__); } { int i22_data[4] = {-24000, -24020, -23600, -23620} ; size_t i22_startset[2] = {0, 0} ; size_t i22_countset[2] = {2, 2}; stat = nc_put_vara(ncid, i22_id, i22_startset, i22_countset, i22_data); check_err(stat,__LINE__,__FILE__); } { float f23_data[6] = {((float)-2187), ((float)-2196), ((float)-2205), ((float)-2106), ((float)-2115), ((float)-2124)} ; size_t f23_startset[2] = {0, 0} ; size_t f23_countset[2] = {2, 3}; stat = nc_put_vara(ncid, f23_id, f23_startset, f23_countset, f23_data); check_err(stat,__LINE__,__FILE__); } { char* c31_data = "+- " ; size_t c31_startset[2] = {0, 0} ; size_t c31_countset[2] = {3, 1}; stat = nc_put_vara(ncid, c31_id, c31_startset, c31_countset, c31_data); check_err(stat,__LINE__,__FILE__); } { signed char b32_data[6] = {-24, -26, -20, -22, -16, -18} ; size_t b32_startset[2] = {0, 0} ; size_t b32_countset[2] = {3, 2}; stat = nc_put_vara(ncid, b32_id, b32_startset, b32_countset, b32_data); check_err(stat,__LINE__,__FILE__); } { short s33_data[9] = {-375, -380, -385, -350, -355, -360, -325, -330, -335} ; size_t s33_startset[2] = {0, 0} ; size_t s33_countset[2] = {3, 3}; stat = nc_put_vara(ncid, s33_id, s33_startset, s33_countset, s33_data); check_err(stat,__LINE__,__FILE__); } { short sr11_data[2] = {2500, 2375} ; size_t sr11_startset[3] = {0, 0, 0} ; size_t sr11_countset[3] = {2, 1, 1}; stat = nc_put_vara(ncid, sr11_id, sr11_startset, sr11_countset, sr11_data); check_err(stat,__LINE__,__FILE__); } { int ir12_data[4] = {640000, 639980, 632000, 631980} ; size_t ir12_startset[3] = {0, 0, 0} ; size_t ir12_countset[3] = {2, 1, 2}; stat = nc_put_vara(ncid, ir12_id, ir12_startset, ir12_countset, ir12_data); check_err(stat,__LINE__,__FILE__); } { float fr13_data[6] = {((float)26244), ((float)26235), ((float)26226), ((float)25515), ((float)25506), ((float)25497)} ; size_t fr13_startset[3] = {0, 0, 0} ; size_t fr13_countset[3] = {2, 1, 3}; stat = nc_put_vara(ncid, fr13_id, fr13_startset, fr13_countset, fr13_data); check_err(stat,__LINE__,__FILE__); } { char* cr21_data = "@DHL" ; size_t cr21_startset[3] = {0, 0, 0} ; size_t cr21_countset[3] = {2, 2, 1}; stat = nc_put_vara(ncid, cr21_id, cr21_startset, cr21_countset, cr21_data); check_err(stat,__LINE__,__FILE__); } { signed char br22_data[8] = {64, 62, 68, 66, 56, 54, 60, 58} ; size_t br22_startset[3] = {0, 0, 0} ; size_t br22_countset[3] = {2, 2, 2}; stat = nc_put_vara(ncid, br22_id, br22_startset, br22_countset, br22_data); check_err(stat,__LINE__,__FILE__); } { short sr23_data[12] = {2500, 2495, 2490, 2525, 2520, 2515, 2375, 2370, 2365, 2400, 2395, 2390} ; size_t sr23_startset[3] = {0, 0, 0} ; size_t sr23_countset[3] = {2, 2, 3}; stat = nc_put_vara(ncid, sr23_id, sr23_startset, sr23_countset, sr23_data); check_err(stat,__LINE__,__FILE__); } { float fr31_data[6] = {((float)26244), ((float)26325), ((float)26406), ((float)25515), ((float)25596), ((float)25677)} ; size_t fr31_startset[3] = {0, 0, 0} ; size_t fr31_countset[3] = {2, 3, 1}; stat = nc_put_vara(ncid, fr31_id, fr31_startset, fr31_countset, fr31_data); check_err(stat,__LINE__,__FILE__); } { double dr32_data[12] = {((double)40000), ((double)39990), ((double)40100), ((double)40090), ((double)40200), ((double)40190), ((double)39000), ((double)38990), ((double)39100), ((double)39090), ((double)39200), ((double)39190)} ; size_t dr32_startset[3] = {0, 0, 0} ; size_t dr32_countset[3] = {2, 3, 2}; stat = nc_put_vara(ncid, dr32_id, dr32_startset, dr32_countset, dr32_data); check_err(stat,__LINE__,__FILE__); } { char* cr33_data = "1\000\000two3\000\0004\000\0005\000\000six" ; size_t cr33_startset[3] = {0, 0, 0} ; size_t cr33_countset[3] = {2, 3, 3}; stat = nc_put_vara(ncid, cr33_id, cr33_startset, cr33_countset, cr33_data); check_err(stat,__LINE__,__FILE__); } { char* c111_data = "@" ; size_t c111_startset[3] = {0, 0, 0} ; size_t c111_countset[3] = {1, 1, 1}; stat = nc_put_vara(ncid, c111_id, c111_startset, c111_countset, c111_data); check_err(stat,__LINE__,__FILE__); } { signed char b112_data[2] = {64, 62} ; size_t b112_startset[3] = {0, 0, 0} ; size_t b112_countset[3] = {1, 1, 2}; stat = nc_put_vara(ncid, b112_id, b112_startset, b112_countset, b112_data); check_err(stat,__LINE__,__FILE__); } { short s113_data[3] = {2500, 2495, 2490} ; size_t s113_startset[3] = {0, 0, 0} ; size_t s113_countset[3] = {1, 1, 3}; stat = nc_put_vara(ncid, s113_id, s113_startset, s113_countset, s113_data); check_err(stat,__LINE__,__FILE__); } { float f121_data[2] = {((float)26244), ((float)26325)} ; size_t f121_startset[3] = {0, 0, 0} ; size_t f121_countset[3] = {1, 2, 1}; stat = nc_put_vara(ncid, f121_id, f121_startset, f121_countset, f121_data); check_err(stat,__LINE__,__FILE__); } { double d122_data[4] = {((double)40000), ((double)39990), ((double)40100), ((double)40090)} ; size_t d122_startset[3] = {0, 0, 0} ; size_t d122_countset[3] = {1, 2, 2}; stat = nc_put_vara(ncid, d122_id, d122_startset, d122_countset, d122_data); check_err(stat,__LINE__,__FILE__); } { char* c123_data = "one2\000\000" ; size_t c123_startset[3] = {0, 0, 0} ; size_t c123_countset[3] = {1, 2, 3}; stat = nc_put_vara(ncid, c123_id, c123_startset, c123_countset, c123_data); check_err(stat,__LINE__,__FILE__); } { short s131_data[3] = {2500, 2525, 2550} ; size_t s131_startset[3] = {0, 0, 0} ; size_t s131_countset[3] = {1, 3, 1}; stat = nc_put_vara(ncid, s131_id, s131_startset, s131_countset, s131_data); check_err(stat,__LINE__,__FILE__); } { int i132_data[6] = {640000, 639980, 640400, 640380, 640800, 640780} ; size_t i132_startset[3] = {0, 0, 0} ; size_t i132_countset[3] = {1, 3, 2}; stat = nc_put_vara(ncid, i132_id, i132_startset, i132_countset, i132_data); check_err(stat,__LINE__,__FILE__); } { float f133_data[9] = {((float)26244), ((float)26235), ((float)26226), ((float)26325), ((float)26316), ((float)26307), ((float)26406), ((float)26397), ((float)26388)} ; size_t f133_startset[3] = {0, 0, 0} ; size_t f133_countset[3] = {1, 3, 3}; stat = nc_put_vara(ncid, f133_id, f133_startset, f133_countset, f133_data); check_err(stat,__LINE__,__FILE__); } { float f211_data[2] = {((float)26244), ((float)25515)} ; size_t f211_startset[3] = {0, 0, 0} ; size_t f211_countset[3] = {2, 1, 1}; stat = nc_put_vara(ncid, f211_id, f211_startset, f211_countset, f211_data); check_err(stat,__LINE__,__FILE__); } { double d212_data[4] = {((double)40000), ((double)39990), ((double)39000), ((double)38990)} ; size_t d212_startset[3] = {0, 0, 0} ; size_t d212_countset[3] = {2, 1, 2}; stat = nc_put_vara(ncid, d212_id, d212_startset, d212_countset, d212_data); check_err(stat,__LINE__,__FILE__); } { char* c213_data = "\000\000\000\000\000\000" ; size_t c213_startset[3] = {0, 0, 0} ; size_t c213_countset[3] = {2, 1, 3}; stat = nc_put_vara(ncid, c213_id, c213_startset, c213_countset, c213_data); check_err(stat,__LINE__,__FILE__); } { short s221_data[4] = {2500, 2525, 2375, 2400} ; size_t s221_startset[3] = {0, 0, 0} ; size_t s221_countset[3] = {2, 2, 1}; stat = nc_put_vara(ncid, s221_id, s221_startset, s221_countset, s221_data); check_err(stat,__LINE__,__FILE__); } { int i222_data[8] = {640000, 639980, 640400, 640380, 632000, 631980, 632400, 632380} ; size_t i222_startset[3] = {0, 0, 0} ; size_t i222_countset[3] = {2, 2, 2}; stat = nc_put_vara(ncid, i222_id, i222_startset, i222_countset, i222_data); check_err(stat,__LINE__,__FILE__); } { float f223_data[12] = {((float)26244), ((float)26235), ((float)26226), ((float)26325), ((float)26316), ((float)26307), ((float)25515), ((float)25506), ((float)25497), ((float)25596), ((float)25587), ((float)25578)} ; size_t f223_startset[3] = {0, 0, 0} ; size_t f223_countset[3] = {2, 2, 3}; stat = nc_put_vara(ncid, f223_id, f223_startset, f223_countset, f223_data); check_err(stat,__LINE__,__FILE__); } { char* c231_data = "@DHHLP" ; size_t c231_startset[3] = {0, 0, 0} ; size_t c231_countset[3] = {2, 3, 1}; stat = nc_put_vara(ncid, c231_id, c231_startset, c231_countset, c231_data); check_err(stat,__LINE__,__FILE__); } { signed char b232_data[12] = {64, 62, 68, 66, 72, 70, 56, 54, 60, 58, 64, 62} ; size_t b232_startset[3] = {0, 0, 0} ; size_t b232_countset[3] = {2, 3, 2}; stat = nc_put_vara(ncid, b232_id, b232_startset, b232_countset, b232_data); check_err(stat,__LINE__,__FILE__); } { short s233_data[18] = {2500, 2495, 2490, 2525, 2520, 2515, 2550, 2545, 2540, 2375, 2370, 2365, 2400, 2395, 2390, 2425, 2420, 2415} ; size_t s233_startset[3] = {0, 0, 0} ; size_t s233_countset[3] = {2, 3, 3}; stat = nc_put_vara(ncid, s233_id, s233_startset, s233_countset, s233_data); check_err(stat,__LINE__,__FILE__); } { short s311_data[3] = {2500, 2375, 2250} ; size_t s311_startset[3] = {0, 0, 0} ; size_t s311_countset[3] = {3, 1, 1}; stat = nc_put_vara(ncid, s311_id, s311_startset, s311_countset, s311_data); check_err(stat,__LINE__,__FILE__); } { int i312_data[6] = {640000, 639980, 632000, 631980, 624000, 623980} ; size_t i312_startset[3] = {0, 0, 0} ; size_t i312_countset[3] = {3, 1, 2}; stat = nc_put_vara(ncid, i312_id, i312_startset, i312_countset, i312_data); check_err(stat,__LINE__,__FILE__); } { float f313_data[9] = {((float)26244), ((float)26235), ((float)26226), ((float)25515), ((float)25506), ((float)25497), ((float)24786), ((float)24777), ((float)24768)} ; size_t f313_startset[3] = {0, 0, 0} ; size_t f313_countset[3] = {3, 1, 3}; stat = nc_put_vara(ncid, f313_id, f313_startset, f313_countset, f313_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static double var_MINUS_name_MINUS_dashes_data[1] = {((double)-1)}; stat = nc_put_var1(ncid, var_MINUS_name_MINUS_dashes_id, &count, var_MINUS_name_MINUS_dashes_data); check_err(stat,__LINE__,__FILE__); } { size_t count = 0; static double var_PERIOD_name_PERIOD_dots_data[1] = {((double)-2)}; stat = nc_put_var1(ncid, var_PERIOD_name_PERIOD_dots_id, &count, var_PERIOD_name_PERIOD_dots_data); check_err(stat,__LINE__,__FILE__); } stat = nc_close(ncid); check_err(stat,__LINE__,__FILE__); return 0; }
/*ARGSUSED*/ int main(int argc, char *argv[]) { int cmode=NC_CLOBBER, omode, ret; int id; char buf[256]; #ifdef SYNCDEBUG char *str = "one"; #endif int ii; size_t ui; const struct tcdfvar *tvp = testvars; union getret got; const size_t initialsz = 8192; size_t chunksz = 8192; size_t align = 8192/32; MPI_Init(&argc, &argv); /* cmode |= NC_PNETCDF |NC_64BIT_OFFSET; */ cmode != NC_PNETCDF |NC_64BIT_DATA; ret = nc_create_par(fname,cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &id); if(ret != NC_NOERR) { fprintf(stderr,"Error %s in file %s at line %d\n",nc_strerror(ret),__FILE__,__LINE__); exit(ret); } assert( nc_put_att_text(id, NC_GLOBAL, "TITLE", 12, "another name") == NC_NOERR); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf) == NC_NOERR); /* (void) printf("title 1 \"%s\"\n", buf); */ assert( nc_put_att_text(id, NC_GLOBAL, "TITLE", strlen(fname), fname) == NC_NOERR); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf) == NC_NOERR); buf[strlen(fname)] = 0; /* (void) printf("title 2 \"%s\"\n", buf); */ assert( strcmp(fname, buf) == 0); createtestdims(id, NUM_DIMS, sizes, dim_names); testdims(id, NUM_DIMS, sizes, dim_names); createtestvars(id, testvars, NUM_TESTVARS); { int ifill = -1; double dfill = -9999; assert( nc_put_att_int(id, Long_id, _FillValue, NC_INT, 1, &ifill) == NC_NOERR); assert( nc_put_att_double(id, Double_id, _FillValue, NC_DOUBLE, 1, &dfill) == NC_NOERR); } #ifdef REDEF assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR ); assert( nc_put_var1_int(id, Long_id, indices[3], &birthday) == NC_NOERR ); fill_seq(id); assert( nc_redef(id) == NC_NOERR ); /* assert( nc_rename_dim(id,2, "a long dim name") == NC_NOERR); */ #endif assert( nc_rename_dim(id,1, "IXX") == NC_NOERR); assert( nc_inq_dim(id, 1, buf, &ui) == NC_NOERR); /* (void) printf("dimrename: %s\n", buf); */ assert( nc_rename_dim(id,1, dim_names[1]) == NC_NOERR); #ifdef ATTRX assert( nc_rename_att(id, 1, "UNITS", "units") == NC_NOERR); assert( nc_del_att(id, 4, "FIELDNAM")== NC_NOERR); assert( nc_del_att(id, 2, "SCALEMIN")== NC_NOERR); assert( nc_del_att(id, 2, "SCALEMAX")== NC_NOERR); #endif /* ATTRX */ assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR ); #ifndef REDEF fill_seq(id); assert( nc_put_var1_int(id, Long_id, indices[3], &birthday)== NC_NOERR ); #endif assert( nc_put_vara_schar(id, Byte_id, s_start, s_edges, (signed char *)sentence) == NC_NOERR); assert( nc_put_var1_schar(id, Byte_id, indices[6], (signed char *)(chs+1)) == NC_NOERR); assert( nc_put_var1_schar(id, Byte_id, indices[5], (signed char *)chs) == NC_NOERR); assert( nc_put_vara_text(id, Char_id, s_start, s_edges, sentence) == NC_NOERR); assert( nc_put_var1_text(id, Char_id, indices[6], (chs+1)) == NC_NOERR) ; assert( nc_put_var1_text(id, Char_id, indices[5], chs) == NC_NOERR); assert( nc_put_var1_short(id, Short_id, indices[4], shs) == NC_NOERR); assert( nc_put_var1_float(id, Float_id, indices[2], &e) == NC_NOERR); assert( nc_put_var1_double(id, Double_id, indices[1], &zed) == NC_NOERR); assert( nc_put_var1_double(id, Double_id, indices[0], &pinot) == NC_NOERR); #ifdef SYNCDEBUG (void) printf("Hit Return to sync\n"); gets(str); nc_sync(id,0); (void) printf("Sync done. Hit Return to continue\n"); gets(str); #endif /* SYNCDEBUG */ ret = nc_close(id); /* (void) printf("nc_close ret = %d\n\n", ret); */ /* * read it */ omode = NC_NOWRITE; omode = NC_NOWRITE | NC_PNETCDF; if(ret != NC_NOERR) { (void) printf("Could not open %s: %s\n", fname, nc_strerror(ret)); exit(1); } /* (void) printf("reopen id = %d for filename %s\n", */ /* id, fname); */ /* NC */ /* (void) printf("NC "); */ assert( nc_inq(id, &(cdesc->num_dims), &(cdesc->num_vars), &(cdesc->num_attrs), &(cdesc->xtendim) ) == NC_NOERR); assert((size_t) cdesc->num_dims == num_dims); assert(cdesc->num_attrs == 1); assert(cdesc->num_vars == NUM_TESTVARS); /* (void) printf("done\n"); */ /* GATTR */ /* (void) printf("GATTR "); */ assert( nc_inq_attname(id, NC_GLOBAL, 0, adesc->mnem) == 0); assert(strcmp("TITLE",adesc->mnem) == 0); assert( nc_inq_att(id, NC_GLOBAL, adesc->mnem, &(adesc->type), &(adesc->len))== NC_NOERR); assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(fname) ); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(fname, buf) == 0); /* VAR */ /* (void) printf("VAR "); */ assert( cdesc->num_vars == NUM_TESTVARS ); for(ii = 0; ii < cdesc->num_vars; ii++, tvp++ ) { int jj; assert( nc_inq_var(id, ii, vdesc->mnem, &(vdesc->type), &(vdesc->ndims), vdesc->dims, &(vdesc->num_attrs)) == NC_NOERR); if(strcmp(tvp->mnem , vdesc->mnem) != 0) { (void) printf("attr %d mnem mismatch %s, %s\n", ii, tvp->mnem, vdesc->mnem); continue; } if(tvp->type != vdesc->type) { (void) printf("attr %d type mismatch %d, %d\n", ii, (int)tvp->type, (int)vdesc->type); continue; } for(jj = 0; jj < vdesc->ndims; jj++ ) { if(tvp->dims[jj] != vdesc->dims[jj] ) { (void) printf( "inconsistent dim[%d] for variable %d: %d != %d\n", jj, ii, tvp->dims[jj], vdesc->dims[jj] ); continue; } } /* VATTR */ /* (void) printf("VATTR\n"); */ for(jj=0; jj<vdesc->num_attrs; jj++ ) { assert( nc_inq_attname(id, ii, jj, adesc->mnem) == NC_NOERR); if( strcmp(adesc->mnem, reqattr[jj]) != 0 ) { (void) printf("var %d attr %d mismatch %s != %s\n", ii, jj, adesc->mnem, reqattr[jj] ); break; } } if( nc_inq_att(id, ii, reqattr[0], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(tvp->units) ); assert( nc_get_att_text(id,ii,reqattr[0],buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(tvp->units, buf) == 0); } if( nc_inq_att(id, ii, reqattr[1], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[1], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->validmin); } if( nc_inq_att(id, ii, reqattr[2], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[2], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->validmax); } if( nc_inq_att(id, ii, reqattr[3], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len ==1 ); assert( nc_get_att_double(id, ii, reqattr[3], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->scalemin); } if( nc_inq_att(id, ii, reqattr[4], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[4], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->scalemax); } if( nc_inq_att(id, ii, reqattr[5], &(adesc->type), &(adesc->len))== NC_NOERR) { assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(tvp->fieldnam) ); assert( nc_get_att_text(id,ii,reqattr[5],buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(tvp->fieldnam, buf) == 0); } } /* (void) printf("fill_seq "); */ check_fill_seq(id); /* (void) printf("Done\n"); */ assert( nc_get_var1_double(id, Double_id, indices[0], &got.dbl)== NC_NOERR); /* (void) printf("got val = %f\n", got.dbl ); */ assert( nc_get_var1_double(id, Double_id, indices[1], &got.dbl)== NC_NOERR); /* (void) printf("got val = %f\n", got.dbl ); */ assert( nc_get_var1_float(id, Float_id, indices[2], &got.fl[0])== NC_NOERR); /* (void) printf("got val = %f\n", got.fl[0] ); */ assert( nc_get_var1_int(id, Long_id, indices[3], &got.in[0])== NC_NOERR); /* (void) printf("got val = %d\n", got.in[0] ); */ assert( nc_get_var1_short(id, Short_id, indices[4], &got.sh[0])== NC_NOERR); /* (void) printf("got val = %d\n", got.sh[0] ); */ assert( nc_get_var1_text(id, Char_id, indices[5], &got.by[0]) == NC_NOERR); /* (void) printf("got NC_CHAR val = %c (0x%02x) \n", */ /* got.by[0] , got.by[0]); */ assert( nc_get_var1_text(id, Char_id, indices[6], &got.by[0]) == NC_NOERR); /* (void) printf("got NC_CHAR val = %c (0x%02x) \n", */ /* got.by[0], got.by[0] ); */ (void) memset(buf,0,sizeof(buf)); assert( nc_get_vara_text(id, Char_id, s_start, s_edges, buf) == NC_NOERR); /* (void) printf("got NC_CHAR val = \"%s\"\n", buf); */ assert( nc_get_var1_schar(id, Byte_id, indices[5], (signed char *)&got.by[0])== NC_NOERR); /* (void) printf("got val = %c (0x%02x) \n", got.by[0] , got.by[0]); */ assert( nc_get_var1_schar(id, Byte_id, indices[6], (signed char *)&got.by[0])== NC_NOERR); /* (void) printf("got val = %c (0x%02x) \n", got.by[0], got.by[0] ); */ (void) memset(buf,0,sizeof(buf)); assert( nc_get_vara_schar(id, Byte_id, s_start, s_edges, (signed char *)buf)== NC_NOERR ); /* (void) printf("got val = \"%s\"\n", buf); */ { double dbuf[NUM_RECS * SIZE_1 * SIZE_2]; assert(nc_get_var_double(id, Float_id, dbuf) == NC_NOERR); /* (void) printf("got vals = %f ... %f\n", dbuf[0], */ /* dbuf[NUM_RECS * SIZE_1 * SIZE_2 -1] ); */ } ret = nc_close(id); /* (void) printf("re nc_close ret = %d\n", ret); */ MPI_Finalize(); return 0; }
int ex_create_int (const char *path, int cmode, int *comp_ws, int *io_ws, int run_version) { int exoid, time_dim, dims[1]; int status; int dimid; int old_fill; int lio_ws; int filesiz; float vers; char errmsg[MAX_ERR_LENGTH]; char *mode_name; int mode = 0; #if defined(NC_NETCDF4) static int netcdf4_mode = -1; char *option; #endif /* NC_NETCDF4 */ exerrval = 0; /* clear error code */ if (run_version != EX_API_VERS_NODOT) { int run_version_major = run_version / 100; int run_version_minor = run_version % 100; int lib_version_major = EX_API_VERS_NODOT / 100; int lib_version_minor = EX_API_VERS_NODOT % 100; fprintf(stderr, "EXODUSII: Warning: This code was compiled with exodusII version %d.%02d,\n but was linked with exodusII library version %d.%02d\n This is probably an error in the build process of this code.\n", run_version_major, run_version_minor, lib_version_major, lib_version_minor); } #if defined(NC_NETCDF4) if (cmode & EX_NETCDF4) { mode |= (NC_NETCDF4|NC_CLASSIC_MODEL); } else { if (netcdf4_mode == -1) { option = getenv("EXODUS_NETCDF4"); if (option != NULL) { fprintf(stderr, "EXODUSII: Using netcdf version 4 selected via EXODUS_NETCDF4 environment variable\n"); netcdf4_mode = NC_NETCDF4|NC_CLASSIC_MODEL; } else { netcdf4_mode = 0; } } mode |= netcdf4_mode; } #endif /* * See if "large file" mode was specified in a ex_create cmode. If * so, then pass the NC_64BIT_OFFSET flag down to netcdf. * If netcdf4 mode specified, don't use NC_64BIT_OFFSET mode. */ if ( (cmode & EX_LARGE_MODEL) && (cmode & EX_NORMAL_MODEL)) { exerrval = EX_BADPARAM; sprintf(errmsg, "Warning: conflicting mode specification for file %s, mode %d. Using normal", path, cmode); ex_err("ex_create",errmsg,exerrval); } if ((cmode & EX_NORMAL_MODEL) != 0) filesiz = 0; #if defined(NC_NETCDF4) else if ((mode & NC_NETCDF4) != 0) filesiz = 1; #endif else filesiz = (int)(((cmode & EX_LARGE_MODEL) != 0) || (ex_large_model(-1) == 1)); if ( #if defined(NC_NETCDF4) !(mode & NC_NETCDF4) && #endif filesiz == 1) { mode |= NC_64BIT_OFFSET; } if (cmode & EX_SHARE) { mode |= NC_SHARE; } /* * set error handling mode to no messages, non-fatal errors */ ex_opts(exoptval); /* call required to set ncopts first time through */ if (cmode & EX_CLOBBER) { mode |= NC_CLOBBER; mode_name = "CLOBBER"; } else { mode |= NC_NOCLOBBER; mode_name = "NOCLOBBER"; } if ((status = nc_create (path, mode, &exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: file create failed for %s, mode: %s", path, mode_name); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } /* turn off automatic filling of netCDF variables */ if ((status = nc_set_fill (exoid, NC_NOFILL, &old_fill)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to set nofill mode in file id %d", exoid); ex_err("ex_create", errmsg, exerrval); return (EX_FATAL); } /* initialize floating point size conversion. since creating new file, * i/o wordsize attribute from file is zero. */ if (ex_conv_ini( exoid, comp_ws, io_ws, 0 ) != EX_NOERR) { exerrval = EX_FATAL; sprintf(errmsg, "Error: failed to init conversion routines in file id %d", exoid); ex_err("ex_create", errmsg, exerrval); return (EX_FATAL); } /* put the EXODUS version number, and i/o floating point word size as * netcdf global attributes */ /* store Exodus API version # as an attribute */ vers = EX_API_VERS; if ((status=nc_put_att_float(exoid, NC_GLOBAL, ATT_API_VERSION, NC_FLOAT, 1, &vers)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store Exodus II API version attribute in file id %d", exoid); ex_err("ex_create",errmsg, exerrval); return (EX_FATAL); } /* store Exodus file version # as an attribute */ vers = EX_VERS; if ((status=nc_put_att_float(exoid, NC_GLOBAL, ATT_VERSION, NC_FLOAT, 1, &vers)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store Exodus II file version attribute in file id %d", exoid); ex_err("ex_create",errmsg, exerrval); return (EX_FATAL); } /* store Exodus file float word size as an attribute */ lio_ws = (int)(*io_ws); if ((status=nc_put_att_int (exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, NC_INT, 1, &lio_ws)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store Exodus II file float word size attribute in file id %d", exoid); ex_err("ex_create",errmsg, exerrval); return (EX_FATAL); } /* store Exodus file size (1=large, 0=normal) as an attribute */ if ((status = nc_put_att_int (exoid, NC_GLOBAL, ATT_FILESIZE, NC_INT, 1, &filesiz)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store Exodus II file size attribute in file id %d", exoid); ex_err("ex_create",errmsg, exerrval); return (EX_FATAL); } /* define some dimensions and variables */ /* create string length dimension */ if ((status=nc_def_dim (exoid, DIM_STR, (MAX_STR_LENGTH+1), &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define string length in file id %d",exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } /* create line length dimension */ if ((status = nc_def_dim(exoid, DIM_LIN, (MAX_LINE_LENGTH+1), &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define line length in file id %d",exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } /* create number "4" dimension; must be of type long */ if ((status = nc_def_dim(exoid, DIM_N4, 4L, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number \"4\" dimension in file id %d",exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_def_dim(exoid, DIM_TIME, NC_UNLIMITED, &time_dim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define time dimension in file id %d", exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } dims[0] = time_dim; if ((status = nc_def_var(exoid, VAR_WHOLE_TIME, nc_flt_code(exoid), 1, dims, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define whole time step variable in file id %d", exoid); ex_err("ex_create",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_enddef (exoid)) != 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); } return (exoid); }
/*! Main function for tst_fill_attr_vanish.c * */ int main() { int ncid, dimids[RANK_P], time_id, p_id, test_id; int ndims, dimids_in[RANK_P]; int test_data[1] = {1}; size_t test_start[1] = {0}, test_count[1] = {1}; int test_fill_val[] = {5}; double data[1] = {3.14159}; size_t start[1] = {0}, count[1] = {1}; float ddata[1][4][3]; static float P_data[LEN]; size_t cor[RANK_P] = {0, 1, 0}; size_t edg[RANK_P] = {1, 1, LEN}; float pfills[] = {3}; printf("\n*** Testing for a netCDF-4 fill-value bug.\n"); printf("*** Creating a file with no _FillValue defined. ***\n"); /* Create a 3D test file. */ if (nc_create(FILENAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR; if (nc_set_fill(ncid, NC_NOFILL, NULL)) ERR; /* define dimensions */ if (nc_def_dim(ncid, "Time", NC_UNLIMITED, &dimids[0])) ERR; if (nc_def_dim(ncid, "X", 4, &dimids[2])) ERR; if (nc_def_dim(ncid, "Y", 3, &dimids[1])) ERR; /* define variables */ if (nc_def_var(ncid, "Time", NC_DOUBLE, 1, dimids, &time_id)) ERR; if (nc_def_var(ncid, "P", NC_FLOAT, RANK_P, dimids, &p_id)) ERR; if (nc_def_var(ncid, "Test", NC_INT, 1, &dimids[1], &test_id)) ERR; /* Add a _FillValue attribute */ if (nc_put_att_text(ncid, test_id, ATTNAME, strlen(ATTVAL), ATTVAL)) ERR; /* Add a value to the test variable */ if (nc_put_vara(ncid, test_id, test_start, test_count, test_data)) ERR; /* Add one record in coordinate variable. */ if (nc_put_vara(ncid, time_id, start, count, data)) ERR; /* That's it! */ if (nc_close(ncid)) ERR; /********************************************/ /* Reopen the file, add a fillvalue attribute. */ if (nc_open(FILENAME, NC_NOCLOBBER|NC_WRITE, &ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_inq_varid(ncid, "Test", &test_id)) ERR; /* Query existing attribute. */ { printf("**** Checking that attribute still exists:\t"); char *attval = malloc(sizeof(char) * strlen(ATTVAL)); if(nc_get_att_text(ncid,test_id,ATTNAME,attval)) {printf("Fail\n"); ERR;} else {printf("%s\n",attval);} free(attval); } printf("**** Adding _FillValue attribute.\n"); if (nc_put_att_int(ncid, test_id, "_FillValue", NC_INT, 1, test_fill_val)) ERR; /* Query existing attribute. */ { printf("**** Checking that attribute still exists, pre-write:\t"); char *attval = malloc(sizeof(char) * strlen(ATTVAL)); if(nc_get_att_text(ncid,test_id,ATTNAME,attval)) {printf("Fail\n"); ERR;} else {printf("%s\n",attval);} free(attval); } /* Close file again. */ printf( "**** Saving, closing file.\n"); if (nc_close(ncid)) ERR; /********************************************/ printf( "*** Reopening file.\n"); /* Reopen the file, checking that all attributes are preserved. */ if (nc_open(FILENAME, NC_NOCLOBBER|NC_WRITE, &ncid)) ERR; if (nc_redef(ncid)) ERR; if (nc_inq_varid(ncid, "Test", &test_id)) ERR; /* Query existing attribute. */ { printf("**** Checking that attribute still exists:\t"); char *attval = malloc(sizeof(char) * strlen(ATTVAL)); if(nc_get_att_text(ncid,test_id,ATTNAME,attval)) {printf("Fail\n"); ERR;} else {printf("%s\n",attval);} free(attval); } if (nc_close(ncid)) ERR; /********************************************/ SUMMARIZE_ERR; FINAL_RESULTS; return 0; }
int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, StdSize numVal, const int* data) { return nc_put_att_int(ncid, varid, attrName, NC_INT, numVal, data); }
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; }
void define_nc_dim_var(int ncid, int dim_latlon, int *time_dim, int *time_var, int *x_dim, int *y_dim, double dx, double dy, double verf_utime, double ref_utime, double time_step, int time_step_type, double date0, int time_ind, double *lat_data, double *lon_data) { char *name, *str, *lname, *units; char ref_date[50]; int dimids[2]; int lat_var, lon_var, y_var, x_var; int ref_time_type; double fill_value = FILL_VALUE_DOUBLE; float f_fill_value = FILL_VALUE_FLOAT; /* the variable need by put_nc_dim_var */ double ttime; int i, j; double *test_ll; size_t start[2], count[2]; size_t x_len, y_len; size_t dimlens[2]; // create coordinate variables... if (dim_latlon == 1) { dimids[0] = *y_dim; netcdf_func( nc_def_var (ncid, "latitude", NC_DOUBLE, 1, dimids, &lat_var) ); dimids[0] = *x_dim; netcdf_func( nc_def_var (ncid, "longitude", NC_DOUBLE, 1, dimids, &lon_var) ); /*#ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: latitude(dim,var,ny)=%d %d %d\n",*y_dim,lat_var,ny); fprintf(stderr,"netcdf:create_nc_dims: longitude(dim,var,nx)=%d %d %d\n",*x_dim,lon_var,nx); #endif*/ } else if(dim_latlon == 2) { dimids[0] = *y_dim; netcdf_func( nc_def_var (ncid, "y", NC_DOUBLE, 1, dimids, &y_var) ); str = "y coordinate of projection"; // str = "projection_y_coordinate"; //it is standard_name, //but could require grid_mapping definition that is //projection-dependant; could do it later; //do not need lat-lon values in this case as these would be //computed in processing software (CF convention) netcdf_func( nc_put_att_text(ncid, y_var, "long_name", strlen(str), str) ); str = "projection_y_coordinate"; netcdf_func( nc_put_att_text(ncid, y_var, "standard_name", strlen(str), str) ); dimids[0] = *x_dim; netcdf_func( nc_def_var (ncid, "x", NC_DOUBLE, 1, dimids, &x_var) ); str = "x coordinate of projection"; netcdf_func( nc_put_att_text(ncid, x_var, "long_name", strlen(str), str) ); str = "projection_x_coordinate"; netcdf_func( nc_put_att_text(ncid, x_var, "standard_name", strlen(str), str) ); str = "m"; netcdf_func( nc_put_att_text(ncid, y_var, "units", strlen(str), str) ); netcdf_func( nc_put_att_text(ncid, x_var, "units", strlen(str), str) ); netcdf_func( nc_put_att_double(ncid, x_var, "grid_spacing", NC_DOUBLE, 1, &dx) ); netcdf_func( nc_put_att_double(ncid, y_var, "grid_spacing", NC_DOUBLE, 1, &dy) ); dimids[0] = *y_dim; dimids[1] = *x_dim; netcdf_func( nc_def_var (ncid, "latitude", NC_DOUBLE, 2, dimids, &lat_var) ); netcdf_func( nc_def_var (ncid, "longitude", NC_DOUBLE, 2, dimids, &lon_var) ); /*#ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: y(dim,var,ny)=%d %d %d\n",*y_dim,y_var,ny); fprintf(stderr,"netcdf:create_nc_dims: x(dim,var,nx)=%d %d %d\n",*x_dim,x_var,nx); fprintf(stderr,"netcdf:create_nc_var (2D): latitude(xdim,ydim,var)=%d %d %d\n",*y_dim,*x_dim,lat_var); fprintf(stderr,"netcdf:create_nc_var (2D): longitude(xdim,ydim,var)=%d %d %d\n",*y_dim,*x_dim,lon_var); #endif*/ } else print_error("netcdf:create_nc_dims: %s","unsupported lat-lon dimension"); str = "degrees_north"; netcdf_func( nc_put_att_text(ncid, lat_var, "units", strlen(str), str) ); str = "degrees_east"; netcdf_func( nc_put_att_text(ncid, lon_var, "units", strlen(str), str) ); str = "latitude"; netcdf_func( nc_put_att_text(ncid, lat_var, "long_name", strlen(str), str) ); str = "longitude"; netcdf_func( nc_put_att_text(ncid, lon_var, "long_name", strlen(str), str) ); /* time settings */ dimids[0] = *time_dim; netcdf_func(nc_def_var (ncid, "time", NC_DOUBLE, 1, dimids, time_var)); netcdf_func(nc_put_att_double(ncid, *time_var, "_FillValue", NC_DOUBLE, 1, &fill_value)); str = "seconds since 1970-01-01 00:00:00.0 0:00"; netcdf_func( nc_put_att_text(ncid, *time_var, "units", strlen(str), str) ); //str = "verification time generated by function verftime()"; //netcdf_func( nc_put_att_text(ncid, *time_var, "long_name", strlen(str), str) ); /* nc_ref_time is reference time value, nc_ref_time_type is the reference time type: 0 - undefined 1 - analyses, all for the same reference date, could be succeded by forecasts 2 - analyses, but for different reference date/time (time serie) 3 - forecasts from the same reference date/time For the type 0 or 2 nc_ref_time keeps first field reference date/time */ if (fabs(ref_utime - verf_utime) < TM_TOLERANCE ) { ref_time_type = 1; str = "analyses, reference date is fixed"; } else { ref_time_type = 3; str = "forecast or accumulated, reference date is fixed"; } netcdf_func( nc_put_att_double(ncid, *time_var, "reference_time", NC_DOUBLE, 1, &ref_utime) ); netcdf_func( nc_put_att_int(ncid, *time_var, "reference_time_type", NC_INT, 1, &ref_time_type) ); my_get_unixdate(ref_utime, ref_date); netcdf_func( nc_put_att_text(ncid, *time_var, "reference_date", strlen(ref_date), ref_date) ); netcdf_func( nc_put_att_text(ncid, *time_var, "reference_time_description", strlen(str), str) ); /* write time step attributes, could be -1 (undefined) for first or single time step, then update */ if ( time_step_type ) { str="user"; } else { str="auto"; } netcdf_func( nc_put_att_text(ncid, *time_var, "time_step_setting", strlen(str), str) ); netcdf_func( nc_put_att_double(ncid, *time_var, "time_step", NC_DOUBLE, 1, &time_step) ); #ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: time(dim,var)=%d %d, size unlimited\n",*time_dim,*time_var); fprintf(stderr,"netcdf:create_nc_dims: time_step=%.1lf time_step_type=%d (%s)\n", time_step,time_step_type,str); #endif netcdf_func( nc_enddef(ncid) ); /* * put var */ // populate coordinate variables... start[0] = 0; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &date0) ); if ( time_ind == 1 ) { start[0] = 1; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &verf_utime) ); } else if (time_ind > 1) { for (j=1; j <= time_ind; j++) { start[0] = j; ttime = date0 + (time_step)*j; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &ttime) ); } } else if (time_ind < 0) { fatal_error("netcdf:create_nc_dims: %s","negative time index"); } netcdf_func(nc_inq_dimlen(ncid, *x_dim, &x_len)); netcdf_func(nc_inq_dimlen(ncid, *y_dim, &y_len)); i = MAX(x_len, y_len); test_ll = (double*) malloc(i*sizeof(double)); if (!test_ll) fatal_error("netcdf:create_nc_dims: %s", "error doing malloc of test_ll"); if (dim_latlon == 1) { set_hyperslab(count, start, &y_len, 1); for (i=0; i < count[0] ; i++) test_ll[i] = lat_data[(i+start[0])*x_len]; //start[0] = 0; count[0] = y_len; netcdf_func( nc_put_vara_double(ncid, lat_var, start, count, test_ll) ); set_hyperslab(count, start, &x_len, 1); for (i=0; i<count[0]; i++) test_ll[i] = lon_data[i+start[0]]; //start[0] = 0; count[0] = x_len; netcdf_func( nc_put_vara_double(ncid, lon_var, start, count, test_ll) ); } else { set_hyperslab(count, start, &y_len, 1); for (i=0; i<count[0] ; i++) test_ll[i] = dy*(i+start[0]); //start[0] = 0; count[0] = y_len; netcdf_func( nc_put_vara_double(ncid, y_var, start, count, test_ll) ); set_hyperslab(count, start, &x_len, 1); for (i=0; i<count[0]; i++) test_ll[i] = dx*(i+start[0]); //start[0] = 0; count[0] = x_len; netcdf_func( nc_put_vara_double(ncid, x_var, start, count, test_ll) ); //start[0] = 0; count[0] = y_len; //start[1] = 0; count[1] = x_len; dimlens[0] = y_len; dimlens[1] = x_len; set_hyperslab(count, start, dimlens, 2); netcdf_func( nc_put_vara_double(ncid, lat_var, start, count, &(lat_data[start[0] * x_len + start[1]])) ); netcdf_func( nc_put_vara_double(ncid, lon_var, start, count, &(lon_data[start[0] * x_len + start[1]])) ); } free(test_ll); }
int ex_open_int(const char *path, int mode, int *comp_ws, int *io_ws, float *version, int run_version) { int exoid; int status, stat_att, stat_dim; nc_type att_type = NC_NAT; size_t att_len = 0; int old_fill; int file_wordsize; int dim_str_name; int int64_status = 0; int nc_mode = 0; char errmsg[MAX_ERR_LENGTH]; EX_FUNC_ENTER(); /* set error handling mode to no messages, non-fatal errors */ ex_opts(exoptval); /* call required to set ncopts first time through */ if (run_version != EX_API_VERS_NODOT && warning_output == 0) { int run_version_major = run_version / 100; int run_version_minor = run_version % 100; int lib_version_major = EX_API_VERS_NODOT / 100; int lib_version_minor = EX_API_VERS_NODOT % 100; fprintf(stderr, "EXODUS: Warning: This code was compiled with exodus " "version %d.%02d,\n but was linked with exodus " "library version %d.%02d\n This is probably an " "error in the build process of this code.\n", run_version_major, run_version_minor, lib_version_major, lib_version_minor); warning_output = 1; } if ((mode & EX_READ) && (mode & EX_WRITE)) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Cannot specify both EX_READ and EX_WRITE"); ex_err(__func__, errmsg, EX_BADFILEMODE); EX_FUNC_LEAVE(EX_FATAL); } /* The EX_READ mode is the default if EX_WRITE is not specified... */ if (!(mode & EX_WRITE)) { /* READ ONLY */ nc_mode = NC_NOWRITE | NC_SHARE; #if NC_HAS_DISKLESS if (mode & EX_DISKLESS) { nc_mode |= NC_DISKLESS; } #endif if ((status = nc_open(path, nc_mode, &exoid)) != NC_NOERR) { /* NOTE: netCDF returns an id of -1 on an error - but no error code! */ /* It is possible that the user is trying to open a netcdf4 file, but the netcdf4 capabilities aren't available in the netcdf linked to this library. Note that we can't just use a compile-time define since we could be using a shareable netcdf library, so the netcdf4 capabilities aren't known until runtime... Later versions of netcdf-4.X have a function that can be queried to determine whether the library being used was compiled with --enable-netcdf4, but not everyone is using that version yet, so we may have to do some guessing... At this time, query the beginning of the file and see if it is an HDF-5 file and if it is assume that the open failure is due to the netcdf library not enabling netcdf4 features unless we have the define that shows it is enabled, then assume other error... */ int type = 0; ex_check_file_type(path, &type); if (type == 5) { #if NC_HAS_HDF5 fprintf(stderr, "EXODUS: ERROR: Attempting to open the netcdf-4 " "file:\n\t'%s'\n\t failed. The netcdf library supports " "netcdf-4 so there must be a filesystem or some other " "issue \n", path); #else /* This is an hdf5 (netcdf4) file. If NC_HAS_HDF5 is not defined, then we either don't have hdf5 support in this netcdf version, OR this is an older netcdf version that doesn't provide that define. In either case, we don't have enough information, so we assume that the netcdf doesn't have netcdf4 capabilities enabled. Tell the user... */ fprintf(stderr, "EXODUS: ERROR: Attempting to open the netcdf-4 " "file:\n\t'%s'\n\t. Either the netcdf library does not " "support netcdf-4 or there is a filesystem or some " "other issue \n", path); #endif } else if (type == 4) { #if defined(NC_64BIT_DATA) fprintf(stderr, "EXODUS: ERROR: Attempting to open the CDF5 " "file:\n\t'%s'\n\t failed. The netcdf library supports " "CDF5-type files so there must be a filesystem or some other " "issue \n", path); #else /* This is an cdf5 (64BIT_DATA) file. If NC_64BIT_DATA is not defined, then we either don't have cdf5 support in this netcdf version, OR this is an older netcdf version that doesn't provide that define. In either case, we don't have enough information, so we assume that the netcdf doesn't have cdf5 capabilities enabled. Tell the user... */ fprintf(stderr, "EXODUS: ERROR: Attempting to open the CDF5 " "file:\n\t'%s'\n\t. Either the netcdf library does not " "support CDF5 or there is a filesystem or some " "other issue \n", path); #endif } snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to open %s read only", path); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } } else { /* (mode & EX_WRITE) READ/WRITE */ nc_mode = NC_WRITE | NC_SHARE; #if NC_HAS_DISKLESS if (mode & EX_DISKLESS) { nc_mode |= NC_DISKLESS; } #endif if ((status = nc_open(path, nc_mode, &exoid)) != NC_NOERR) { /* NOTE: netCDF returns an id of -1 on an error - but no error code! */ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to open %s write only", path); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } /* turn off automatic filling of netCDF variables */ if ((status = nc_set_fill(exoid, NC_NOFILL, &old_fill)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to set nofill mode in file id %d", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } stat_att = nc_inq_att(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len); stat_dim = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name); if (stat_att != NC_NOERR || stat_dim != NC_NOERR) { if ((status = nc_redef(exoid)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } if (stat_att != NC_NOERR) { int max_so_far = 32; nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far); } /* If the DIM_STR_NAME variable does not exist on the database, we need to * add it now. */ if (stat_dim != NC_NOERR) { /* Not found; set to default value of 32+1. */ int max_name = ex_default_max_name_length < 32 ? 32 : ex_default_max_name_length; if ((status = nc_def_dim(exoid, DIM_STR_NAME, max_name + 1, &dim_str_name)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define string name dimension in file id %d", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } } if ((status = nc_enddef(exoid)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition in file id %d", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } } } /* determine version of EXODUS file, and the word size of * floating point and integer values stored in the file */ if ((status = nc_get_att_float(exoid, NC_GLOBAL, ATT_VERSION, version)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get database version for file id: %d", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } /* check ExodusII file version - old version 1.x files are not supported */ if (*version < 2.0) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Unsupported file version %.2f in file id: %d", *version, exoid); ex_err(__func__, errmsg, EX_BADPARAM); EX_FUNC_LEAVE(EX_FATAL); } if (nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, &file_wordsize) != NC_NOERR) { /* try old (prior to db version 2.02) attribute name */ if ((status = nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE_BLANK, &file_wordsize)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get file wordsize from file id: %d", exoid); ex_err(__func__, errmsg, status); EX_FUNC_LEAVE(EX_FATAL); } } /* See if int64 status attribute exists and if so, what data is stored as * int64 * Older files don't have the attribute, so it is not an error if it is * missing */ if (nc_get_att_int(exoid, NC_GLOBAL, ATT_INT64_STATUS, &int64_status) != NC_NOERR) { int64_status = 0; /* Just in case it gets munged by a failed get_att_int call */ } /* Merge in API int64 status flags as specified by caller of function... */ int64_status |= (mode & EX_ALL_INT64_API); /* Verify that there is not an existing file_item struct for this exoid This could happen (and has) when application calls ex_open(), but then closes file using nc_close() and then reopens file. NetCDF will possibly reuse the exoid which results in internal corruption in exodus data structures since exodus does not know that file was closed and possibly new file opened for this exoid */ if (ex_find_file_item(exoid) != NULL) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: There is an existing file already using the file " "id %d which was also assigned to file %s.\n\tWas " "nc_close() called instead of ex_close() on an open Exodus " "file?\n", exoid, path); ex_err(__func__, errmsg, EX_BADFILEID); nc_close(exoid); EX_FUNC_LEAVE(EX_FATAL); } /* initialize floating point and integer size conversion. */ if (ex_conv_ini(exoid, comp_ws, io_ws, file_wordsize, int64_status, 0, 0, 0) != EX_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to initialize conversion routines in file id %d", exoid); ex_err(__func__, errmsg, EX_LASTERR); EX_FUNC_LEAVE(EX_FATAL); } EX_FUNC_LEAVE(exoid); }
int ex_create_par_int(const char *path, int cmode, int *comp_ws, int *io_ws, MPI_Comm comm, MPI_Info info, int run_version) { int exoid; int status; int dimid; int old_fill; int lio_ws; int filesiz; float vers; char errmsg[MAX_ERR_LENGTH]; char *mode_name; int nc_mode = 0; int int64_status; const char *routine = "ex_create_par"; int pariomode = 0; int is_mpiio = 0; int is_pnetcdf = 0; unsigned int my_mode = cmode; /* Contains a 1 in all bits corresponding to file modes */ static unsigned int all_modes = EX_NORMAL_MODEL | EX_64BIT_OFFSET | EX_64BIT_DATA | EX_NETCDF4; exerrval = 0; /* clear error code */ #if !NC_HAS_PARALLEL /* Library does NOT support parallel output via netcdf-4 or pnetcdf */ exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: Parallel output requires the netcdf-4 and/or " "pnetcdf library format, but this netcdf library does not " "support either.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); #endif if (run_version != EX_API_VERS_NODOT && warning_output == 0) { int run_version_major = run_version / 100; int run_version_minor = run_version % 100; int lib_version_major = EX_API_VERS_NODOT / 100; int lib_version_minor = EX_API_VERS_NODOT % 100; fprintf(stderr, "EXODUS: Warning: This code was compiled with exodusII " "version %d.%02d,\n but was linked with exodusII " "library version %d.%02d\n This is probably an " "error in the build process of this code.\n", run_version_major, run_version_minor, lib_version_major, lib_version_minor); warning_output = 1; } /* * See if specified mode is supported in the version of netcdf we * are using */ #if !NC_HAS_HDF5 if (my_mode & EX_NETCDF4) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: File format specified as netcdf-4, but the " "NetCDF library being used was not configured to enable " "this format\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } #endif #if !defined(NC_64BIT_DATA) if (my_mode & EX_64BIT_DATA) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: File format specified as 64bit_data, but " "the NetCDF library being used does not support this " "format\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } #endif /* Check that one and only one format mode is specified... */ { unsigned int set_modes = all_modes & my_mode; if (set_modes == 0) { my_mode |= EX_64BIT_OFFSET; /* Default if nothing specified */ } else { /* Checks that only a single bit is set */ set_modes = set_modes && !(set_modes & (set_modes - 1)); if (!set_modes) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: More than 1 file format " "(EX_NORMAL_MODEL, EX_LARGE_MODEL, EX_64BIT_OFFSET, " "EX_64BIT_DATA, or EX_NETCDF4)\nwas specified in the " "mode argument of the ex_create call. Only a single " "format can be specified.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } } } /* * See if any integer data is to be stored as int64 (long long). If * so, then need to set NC_NETCDF4 and unset NC_CLASSIC_MODEL (or * set EX_NOCLASSIC. Output meaningful error message if the library * is not NetCDF-4 enabled... * * As of netcdf-4.4.0, can also use NC_64BIT_DATA (CDF5) mode for this... */ int64_status = my_mode & (EX_ALL_INT64_DB | EX_ALL_INT64_API); if ((int64_status & EX_ALL_INT64_DB) != 0) { #if NC_HAS_HDF5 || defined(NC_64BIT_DATA) /* Library DOES support netcdf4 and/or cdf5 ... See if user * specified either of these and use that one; if not, pick * netcdf4, non-classic as default. */ if (my_mode & EX_NETCDF4) { my_mode |= EX_NOCLASSIC; } #if defined(NC_64BIT_DATA) else if (my_mode & EX_64BIT_DATA) { ; /* Do nothing, already set */ } #endif else { /* Unset the current mode so we don't have multiples specified */ /* ~all_modes sets to 1 all bits not associated with file format */ my_mode &= ~all_modes; #if NC_HAS_HDF5 /* Pick netcdf4 as default mode for 64-bit integers */ my_mode |= EX_NOCLASSIC; my_mode |= EX_NETCDF4; #else /* Pick 64bit_data as default mode for 64-bit integers */ my_mode |= EX_64BIT_DATA; #endif } #else /* Library does NOT support netcdf4 or cdf5 */ exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: 64-bit integer storage requested, but the " "netcdf library does not support the required netcdf-4 or " "64BIT_DATA extensions.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); #endif } /* Check parallel io mode. Valid is NC_MPIPOSIX or NC_MPIIO or NC_PNETCDF * Exodus uses different flag values; map to netcdf values */ { int tmp_mode = 0; if (my_mode & EX_MPIPOSIX) { pariomode = NC_MPIPOSIX; tmp_mode = EX_NETCDF4; #if !NC_HAS_HDF5 exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: EX_MPIPOSIX parallel output requested " "which requires NetCDF-4 support, but the library does " "not have that option enabled.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); #endif } else if (my_mode & EX_MPIIO) { pariomode = NC_MPIIO; is_mpiio = 1; tmp_mode = EX_NETCDF4; #if !NC_HAS_HDF5 exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: EX_MPIIO parallel output requested which " "requires NetCDF-4 support, but the library does not " "have that option enabled.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); #endif } else if (my_mode & EX_PNETCDF) { pariomode = NC_PNETCDF; is_pnetcdf = 1; /* See if client specified 64-bit or not... */ if ((int64_status & EX_ALL_INT64_DB) != 0) { tmp_mode = EX_64BIT_DATA; } else { tmp_mode = EX_64BIT_OFFSET; } #if !NC_HAS_PNETCDF exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "EXODUS: ERROR: EX_PNETCDF parallel output requested " "which requires PNetCDF support, but the library does " "not have that option enabled.\n"); ex_err(routine, errmsg, exerrval); return (EX_FATAL); #endif } /* If tmp_mode was set here, then need to clear any other mode that was potentially already set in my_mode... */ my_mode &= ~all_modes; my_mode |= tmp_mode; } if (my_mode & EX_NETCDF4) { nc_mode |= NC_NETCDF4; } if (!(my_mode & EX_NOCLASSIC)) { nc_mode |= NC_CLASSIC_MODEL; } /* * See if "large file" mode was specified in a ex_create my_mode. If * so, then pass the NC_64BIT_OFFSET flag down to netcdf. * If netcdf4 mode specified, don't use NC_64BIT_OFFSET mode. */ if (my_mode & EX_NORMAL_MODEL) { filesiz = 0; #if NC_HAS_HDF5 } else if (nc_mode & NC_NETCDF4) { filesiz = 1; #endif #if defined(NC_64BIT_DATA) } else if (nc_mode & NC_64BIT_DATA) { filesiz = 1; #endif } else { filesiz = (int)((my_mode & EX_64BIT_OFFSET) || (ex_large_model(-1) == 1)); } if ( #if NC_HAS_HDF5 !(nc_mode & NC_NETCDF4) && #endif #if defined(NC_64BIT_DATA) !(nc_mode & NC_64BIT_DATA) && #endif filesiz == 1) { nc_mode |= NC_64BIT_OFFSET; } if (my_mode & EX_SHARE) { nc_mode |= NC_SHARE; } /* * set error handling mode to no messages, non-fatal errors */ ex_opts(exoptval); /* call required to set ncopts first time through */ if (my_mode & EX_CLOBBER) { nc_mode |= NC_CLOBBER; mode_name = "CLOBBER"; } else { nc_mode |= NC_NOCLOBBER; mode_name = "NOCLOBBER"; } #if defined NC_IGNORE_MAX_DIMS nc_mode |= NC_IGNORE_MAX_DIMS; #endif #if defined NC_IGNORE_MAX_VARS nc_mode |= NC_IGNORE_MAX_VARS; #endif if ((status = nc_create_par(path, nc_mode | pariomode, comm, info, &exoid)) != NC_NOERR) { exerrval = status; #if NC_HAS_HDF5 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s, mode: %s", path, mode_name); #else if (my_mode & EX_NETCDF4) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s in NETCDF4 and %s " "mode.\n\tThis library does not support netcdf-4 files.", path, mode_name); } else { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s, mode: %s", path, mode_name); } #endif ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* turn off automatic filling of netCDF variables */ if ((status = nc_set_fill(exoid, NC_NOFILL, &old_fill)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to set nofill mode in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* Verify that there is not an existing file_item struct for this exoid This could happen (and has) when application calls ex_open(), but then closes file using nc_close() and then reopens file. NetCDF will possibly reuse the exoid which results in internal corruption in exodus data structures since exodus does not know that file was closed and possibly new file opened for this exoid */ if (ex_find_file_item(exoid) != NULL) { char errmsg[MAX_ERR_LENGTH]; exerrval = EX_BADFILEID; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: There is an existing file already using the file " "id %d which was also assigned to file %s.\n\tWas " "nc_close() called instead of ex_close() on an open Exodus " "file?\n", exoid, path); ex_err(routine, errmsg, exerrval); nc_close(exoid); return (EX_FATAL); } /* initialize floating point size conversion. since creating new file, * i/o wordsize attribute from file is zero. */ if (ex_conv_ini(exoid, comp_ws, io_ws, 0, int64_status, 1, is_mpiio, is_pnetcdf) != EX_NOERR) { exerrval = EX_FATAL; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to init conversion routines in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* put the EXODUS version number, and i/o floating point word size as * netcdf global attributes */ /* store Exodus API version # as an attribute */ vers = EX_API_VERS; if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_API_VERSION, NC_FLOAT, 1, &vers)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store Exodus II API version attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* store Exodus file version # as an attribute */ vers = EX_VERS; if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_VERSION, NC_FLOAT, 1, &vers)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store Exodus II file version attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* store Exodus file float word size as an attribute */ lio_ws = (int)(*io_ws); if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, NC_INT, 1, &lio_ws)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store Exodus II file float word size " "attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* store Exodus file size (1=large, 0=normal) as an attribute */ if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_FILESIZE, NC_INT, 1, &filesiz)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store Exodus II file size attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } { int max_so_far = 32; if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add maximum_name_length attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } } /* define some dimensions and variables */ /* create string length dimension */ if ((status = nc_def_dim(exoid, DIM_STR, (MAX_STR_LENGTH + 1), &dimid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define string length in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* The name string length dimension is delayed until the ex_put_init function */ /* create line length dimension */ if ((status = nc_def_dim(exoid, DIM_LIN, (MAX_LINE_LENGTH + 1), &dimid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define line length in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* create number "4" dimension; must be of type long */ if ((status = nc_def_dim(exoid, DIM_N4, 4L, &dimid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define number \"4\" dimension in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } { int int64_db_status = int64_status & EX_ALL_INT64_DB; if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_INT64_STATUS, NC_INT, 1, &int64_db_status)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add int64_status attribute in file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } } if ((status = nc_enddef(exoid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } return (exoid); }
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; }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 file functions.\n"); { char str[NC_MAX_NAME+1]; /* Actually we never make any promises about the length of the * version string, but it is always smaller than NC_MAX_NAME. */ if (strlen(nc_inq_libvers()) > NC_MAX_NAME) ERR; strcpy(str, nc_inq_libvers()); printf("*** testing version %s...", str); } SUMMARIZE_ERR; printf("*** testing with bad inputs..."); { int ncid; /* Make sure bad create mode causes failure. */ /*if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;*/ /* Create an empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_NETCDF4, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; if (nc_create(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR; } SUMMARIZE_ERR; printf("*** testing simple opens and creates..."); { int ncid, ncid2, ncid3, varid, dimids[2]; int ndims, nvars, natts, unlimdimid; int dimids_var[1], var_type; size_t dim_len; char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1]; unsigned char uchar_out[DIM1_LEN] = {0, 128, 255}; /* Open and close empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; /* Recreate it again. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Then define a new variable. Since it's * netcdf-4, nc_enddef isn't required - it's called * automatically. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR; if (nc_redef(ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Open three copies of the same file. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid2)) ERR; if (nc_open(FILE_NAME, NC_WRITE, &ncid3)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq(ncid2, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq(ncid3, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; if (nc_close(ncid2)) ERR; if (nc_close(ncid3)) ERR; /* Open and close empty file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; /* Create a file with one dimension and nothing else. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_enddef(ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_close(ncid)) ERR; /* Create a simple file, and write some data to it. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_uchar(ncid, varid, uchar_out) != NC_ERANGE) ERR; if (nc_close(ncid)) ERR; /* Check the contents. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_BYTE || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_close(ncid)) ERR; /* Recreate the file. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_uchar(ncid, varid, uchar_out)) ERR; if (nc_close(ncid)) ERR; /* Recreate it, then make sure NOCLOBBER works. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_close(ncid)) ERR; if (nc_create(FILE_NAME, NC_NETCDF4|NC_NOCLOBBER, &ncid) != NC_EEXIST) ERR; /* Recreate it again. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Then define a new variable. Since it's * netcdf-4, nc_enddef isn't required - it's called * automatically. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR; if (nc_redef(ncid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_close(ncid)) ERR; /* Recreate it again with netcdf-3 rules turned on. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR; if (nc_enddef(ncid)) ERR; if (nc_close(ncid)) ERR; /* Check the contents. Check that netcdf-3 rules are in effect. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid) != NC_ENOTINDEFINE) ERR; if (nc_close(ncid)) ERR; /* Check some other stuff about it. Closing and reopening the * file forces HDF5 to tell us if we forgot to free some HDF5 * resource associated with the file. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing more complex opens and creates..."); { int ncid, varid, dimids[2]; int ndims, nvars, natts, unlimdimid; int dimids_var[2], var_type; size_t dim_len; char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1]; float float_in, float_out = 99.99; int int_in, int_out = -9999; /* Create a file, this time with attributes. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR; if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR; if (nc_put_att_float(ncid, NC_GLOBAL, ATT1_NAME, NC_FLOAT, 1, &float_out)) ERR; if (nc_put_att_int(ncid, NC_GLOBAL, ATT2_NAME, NC_INT, 1, &int_out)) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 2 || natts != 2 || unlimdimid != -1) ERR; if (nc_close(ncid)) ERR; /* Reopen it and check each dim, var, and att. */ if (nc_open(FILE_NAME, 0, &ncid)) ERR; if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR; if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR; if (nc_inq_dim(ncid, 1, dim_name, &dim_len)) ERR; if (dim_len != DIM2_LEN || strcmp(dim_name, DIM2_NAME)) ERR; if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 2 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT || dimids_var[0] != dimids[0] || natts != 0) ERR; if (nc_inq_var(ncid, 1, var_name, &var_type, &ndims, dimids_var, &natts)) ERR; if (ndims != 2 || strcmp(var_name, VAR2_NAME) || var_type != NC_UINT || dimids_var[1] != dimids[1] || natts != 0) ERR; if (nc_get_att_float(ncid, NC_GLOBAL, ATT1_NAME, &float_in)) ERR; if (float_in != float_out) ERR; if (nc_get_att_int(ncid, NC_GLOBAL, ATT2_NAME, &int_in)) ERR; if (int_in != int_out) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing redef for netCDF classic..."); test_redef(NC_FORMAT_CLASSIC); SUMMARIZE_ERR; printf("*** testing redef for netCDF 64-bit offset..."); test_redef(NC_FORMAT_64BIT); SUMMARIZE_ERR; printf("*** testing redef for netCDF-4 ..."); test_redef(NC_FORMAT_NETCDF4); SUMMARIZE_ERR; printf("*** testing redef for netCDF-4, with strict netCDF-3 rules..."); test_redef(NC_FORMAT_NETCDF4_CLASSIC); SUMMARIZE_ERR; printf("*** testing different formats..."); { int ncid; int format; /* Create a netcdf-3 file. */ if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_CLASSIC) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-3 64-bit offset file. */ if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_64BIT) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-4 file. */ if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_NETCDF4) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing CLASSIC_MODEL flag with classic formats..."); { int ncid; int format; /* Create a netcdf-3 file. */ if (nc_create(FILE_NAME, NC_CLOBBER|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_CLASSIC) ERR; if (nc_close(ncid)) ERR; /* Create a netcdf-3 64-bit offset file. */ if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLOBBER|NC_CLASSIC_MODEL, &ncid)) ERR; if (nc_inq_format(ncid, &format)) ERR; if (format != NC_FORMAT_64BIT) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing multiple open files..."); { #define VAR_NAME "Captain_Kirk" #define NDIMS 1 #define NUM_FILES 30 #define TEXT_LEN 15 #define D1_NAME "tl" int ncid[NUM_FILES], varid; int dimid; size_t count[NDIMS], index[NDIMS] = {0}; const char ttext[TEXT_LEN + 1] = "20051224.150000"; char ttext_in[TEXT_LEN + 1]; char file_name[NC_MAX_NAME + 1]; size_t chunks[NDIMS] = {TEXT_LEN + 1}; int f; /* Create a bunch of files. */ for (f = 0; f < NUM_FILES; f++) { sprintf(file_name, "tst_files2_%d.nc", f); if (nc_create(file_name, NC_NETCDF4, &ncid[f])) ERR; if (nc_def_dim(ncid[f], D1_NAME, TEXT_LEN + 1, &dimid)) ERR; if (nc_def_var(ncid[f], VAR_NAME, NC_CHAR, NDIMS, &dimid, &varid)) ERR; if (f % 2 == 0) if (nc_def_var_chunking(ncid[f], varid, 0, chunks)) ERR; /* Write one time to the coordinate variable. */ count[0] = TEXT_LEN + 1; if (nc_put_vara_text(ncid[f], varid, index, count, ttext)) ERR; } /* Read something from each file. */ for (f = 0; f < NUM_FILES; f++) { if (nc_get_vara_text(ncid[f], varid, index, count, (char *)ttext_in)) ERR; if (strcmp(ttext_in, ttext)) ERR; } /* Close all open files. */ for (f = 0; f < NUM_FILES; f++) if (nc_close(ncid[f])) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int main(int argc, char **argv) { int mpi_namelen; char mpi_name[MPI_MAX_PROCESSOR_NAME]; int mpi_size, mpi_rank; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; double start_time = 0, total_time; int mpi_size_in; #define NUM_TEST_TYPES 11 nc_type test_type[NUM_TEST_TYPES] = {NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE, NC_UBYTE, NC_USHORT, NC_UINT, NC_INT64, NC_UINT64}; int tt, fv; int j, i, k, ret; /* Initialize MPI. */ MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Get_processor_name(mpi_name, &mpi_namelen); /* Must be able to evenly divide my slabs between processors. */ if (NUM_SLABS % mpi_size) { if (!mpi_rank) printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n", NUM_SLABS, mpi_size); ERR; } if (!mpi_rank) printf("\n*** Testing parallel I/O some more.\n"); /* Test for different fill value settings. */ for (fv = 0; fv < NUM_FILL_TEST_RUNS; fv++) { /* Test for different netCDF types. */ for (tt = 0; tt < NUM_TEST_TYPES; tt++) { char file_name[NC_MAX_NAME + 1]; int fill_mode_in; void *data, *data_in; void *fill_value, *fill_value_in; size_t type_size; size_t write_start[NDIMS] = {0, 0, 1}; size_t write_count[NDIMS] = {1, DIMSIZE, DIMSIZE - 1}; size_t read_start[NDIMS] = {0, 0, 0}; size_t read_count[NDIMS] = {1, DIMSIZE, DIMSIZE}; int ncid, varid, dimids[NDIMS]; int ndims_in, nvars_in, natts_in, unlimdimid_in; /* Fill values to be expected. */ signed char byte_expected_fill_value; unsigned char char_expected_fill_value; short short_expected_fill_value; int int_expected_fill_value; float float_expected_fill_value; double double_expected_fill_value; unsigned char ubyte_expected_fill_value; unsigned short ushort_expected_fill_value; unsigned int uint_expected_fill_value; long long int int64_expected_fill_value; unsigned long long int uint64_expected_fill_value; /* Fill values used when writing. */ signed char byte_fill_value = -TEST_VAL_42; unsigned char char_fill_value = 'x'; short short_fill_value = TEST_VAL_42 * 100; int int_fill_value = TEST_VAL_42 * 1000; float float_fill_value = TEST_VAL_42 * 1000; double double_fill_value = TEST_VAL_42 * 1000; unsigned char ubyte_fill_value = TEST_VAL_42; unsigned short ushort_fill_value = TEST_VAL_42 * 100; unsigned int uint_fill_value = TEST_VAL_42 * 1000; long long int int64_fill_value = TEST_VAL_42 * 1000; unsigned long long int uint64_fill_value = TEST_VAL_42 * 1000; /* Fill values read in. */ signed char byte_fill_value_in; unsigned char char_fill_value_in; short short_fill_value_in; int int_fill_value_in; float float_fill_value_in; double double_fill_value_in; unsigned char ubyte_fill_value_in; unsigned short ushort_fill_value_in; unsigned int uint_fill_value_in; long long int int64_fill_value_in; unsigned long long int uint64_fill_value_in; /* Data to write and read. */ signed char byte_data[DIMSIZE * DIMSIZE], byte_data_in[DIMSIZE * DIMSIZE]; unsigned char char_data[DIMSIZE * DIMSIZE], char_data_in[DIMSIZE * DIMSIZE]; short short_data[DIMSIZE * DIMSIZE], short_data_in[DIMSIZE * DIMSIZE]; int int_data[DIMSIZE * DIMSIZE], int_data_in[DIMSIZE * DIMSIZE]; float float_data[DIMSIZE * DIMSIZE], float_data_in[DIMSIZE * DIMSIZE]; double double_data[DIMSIZE * DIMSIZE], double_data_in[DIMSIZE * DIMSIZE]; unsigned char ubyte_data[DIMSIZE * DIMSIZE], ubyte_data_in[DIMSIZE * DIMSIZE]; unsigned short ushort_data[DIMSIZE * DIMSIZE], ushort_data_in[DIMSIZE * DIMSIZE]; unsigned int uint_data[DIMSIZE * DIMSIZE], uint_data_in[DIMSIZE * DIMSIZE]; long long int int64_data[DIMSIZE * DIMSIZE], int64_data_in[DIMSIZE * DIMSIZE]; unsigned long long int uint64_data[DIMSIZE * DIMSIZE], uint64_data_in[DIMSIZE * DIMSIZE]; if (!mpi_rank) printf("*** writing a %d x %d x %d file from %d processors for fill value test %d type %d...\n", NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size, fv, test_type[tt]); /* Initialize test data. */ switch(test_type[tt]) { case NC_BYTE: for (i = 0; i < DIMSIZE * DIMSIZE; i++) byte_data[i] = mpi_rank; data = byte_data; data_in = byte_data_in; byte_expected_fill_value = fv ? byte_fill_value : NC_FILL_BYTE; fill_value = &byte_expected_fill_value; fill_value_in = &byte_fill_value_in; break; case NC_CHAR: for (i = 0; i < DIMSIZE * DIMSIZE; i++) char_data[i] = mpi_rank; data = char_data; data_in = char_data_in; char_expected_fill_value = fv ? char_fill_value : NC_FILL_CHAR; fill_value = &char_expected_fill_value; fill_value_in = &char_fill_value_in; break; case NC_SHORT: for (i = 0; i < DIMSIZE * DIMSIZE; i++) short_data[i] = mpi_rank; data = short_data; data_in = short_data_in; short_expected_fill_value = fv ? short_fill_value : NC_FILL_SHORT; fill_value = &short_expected_fill_value; fill_value_in = &short_fill_value_in; break; case NC_INT: for (i = 0; i < DIMSIZE * DIMSIZE; i++) int_data[i] = mpi_rank; data = int_data; data_in = int_data_in; int_expected_fill_value = fv ? int_fill_value : NC_FILL_INT; fill_value = &int_expected_fill_value; fill_value_in = &int_fill_value_in; break; case NC_FLOAT: for (i = 0; i < DIMSIZE * DIMSIZE; i++) float_data[i] = mpi_rank; data = float_data; data_in = float_data_in; float_expected_fill_value = fv ? float_fill_value : NC_FILL_FLOAT; fill_value = &float_expected_fill_value; fill_value_in = &float_fill_value_in; break; case NC_DOUBLE: for (i = 0; i < DIMSIZE * DIMSIZE; i++) double_data[i] = mpi_rank; data = double_data; data_in = double_data_in; double_expected_fill_value = fv ? double_fill_value : NC_FILL_DOUBLE; fill_value = &double_expected_fill_value; fill_value_in = &double_fill_value_in; break; case NC_UBYTE: for (i = 0; i < DIMSIZE * DIMSIZE; i++) ubyte_data[i] = mpi_rank; data = ubyte_data; data_in = ubyte_data_in; ubyte_expected_fill_value = fv ? ubyte_fill_value : NC_FILL_UBYTE; fill_value = &ubyte_expected_fill_value; fill_value_in = &ubyte_fill_value_in; break; case NC_USHORT: for (i = 0; i < DIMSIZE * DIMSIZE; i++) ushort_data[i] = mpi_rank; data = ushort_data; data_in = ushort_data_in; ushort_expected_fill_value = fv ? ushort_fill_value : NC_FILL_USHORT; fill_value = &ushort_expected_fill_value; fill_value_in = &ushort_fill_value_in; break; case NC_UINT: for (i = 0; i < DIMSIZE * DIMSIZE; i++) uint_data[i] = mpi_rank; data = uint_data; data_in = uint_data_in; uint_expected_fill_value = fv ? uint_fill_value : NC_FILL_UINT; fill_value = &uint_expected_fill_value; fill_value_in = &uint_fill_value_in; break; case NC_INT64: for (i = 0; i < DIMSIZE * DIMSIZE; i++) int64_data[i] = mpi_rank; data = int64_data; data_in = int64_data_in; int64_expected_fill_value = fv ? int64_fill_value : NC_FILL_INT64; fill_value = &int64_expected_fill_value; fill_value_in = &int64_fill_value_in; break; case NC_UINT64: for (i = 0; i < DIMSIZE * DIMSIZE; i++) uint64_data[i] = mpi_rank; data = uint64_data; data_in = uint64_data_in; uint64_expected_fill_value = fv ? uint64_fill_value : NC_FILL_UINT64; fill_value = &uint64_expected_fill_value; fill_value_in = &uint64_fill_value_in; break; } /* Create a file name. */ sprintf(file_name, "%s_type_%d_fv_%d.nc", TEST_NAME, test_type[tt], fv); /* Create a parallel netcdf-4 file. */ if (nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid)) ERR; /* Get the type len. */ if (nc_inq_type(ncid, test_type[tt], NULL, &type_size)) ERR; /* A global attribute holds the number of processors that created * the file. */ if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR; /* Create three dimensions. */ if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR; if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR; /* Create one var. */ if (nc_def_var(ncid, VAR_NAME, test_type[tt], NDIMS, dimids, &varid)) ERR; if (nc_put_att_int(ncid, varid, "var_num_processors", NC_INT, 1, &mpi_size)) ERR; if (fv == 1) { if (nc_def_var_fill(ncid, varid, NC_FILL, fill_value)) ERR; if (nc_inq_var_fill(ncid, varid, &fill_mode_in, fill_value_in)) ERR; if (fill_mode_in != NC_FILL) ERR; if (memcmp(fill_value_in, fill_value, type_size)) ERR; } else if (fv == 2) { if (nc_def_var_fill(ncid, varid, NC_NOFILL, NULL)) ERR; if (nc_inq_var_fill(ncid, varid, &fill_mode_in, NULL)) ERR; if (!fill_mode_in) ERR; /* nofill will be true */ } /* Write metadata to file. */ if (nc_enddef(ncid)) ERR; /* Change access mode to collective, then back to independent. */ if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR; if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR; if (!mpi_rank) start_time = MPI_Wtime(); /* Write all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { write_start[0] = NUM_SLABS / mpi_size * mpi_rank + i; /* Write one slab of data. Due to start/count settings, * every 16th value will be a fill value. */ if (nc_put_vara(ncid, varid, write_start, write_count, data)) ERR; } /* On rank 0, keep track of time. */ if (!mpi_rank) { total_time = MPI_Wtime() - start_time; printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS * sizeof(int) / total_time); } /* Close the netcdf file. */ if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if ((ret = nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid))) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR; if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 || unlimdimid_in != -1) ERR; /* Check the attributes. */ if (nc_get_att_int(ncid, NC_GLOBAL, "num_processors", &mpi_size_in)) ERR; if (mpi_size_in != mpi_size) ERR; if (nc_get_att_int(ncid, 0, "var_num_processors", &mpi_size_in)) ERR; if (mpi_size_in != mpi_size) ERR; if (fv == 1) { if (nc_inq_var_fill(ncid, varid, &fill_mode_in, fill_value_in)) ERR; if (fill_mode_in != NC_FILL) ERR; if (memcmp(fill_value_in, fill_value, type_size)) ERR; } /* Read all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { read_start[0] = NUM_SLABS / mpi_size * mpi_rank + i; /* printf("mpi_rank %d i %d read_start[0] %ld\n", mpi_rank, i, read_start[0]); */ /* Read one slab of data. */ if (nc_get_vara(ncid, varid, read_start, read_count, data_in)) ERR; /* Check data. For the third fill value test, fill is * turned off. So don't bother testing the values where k * is zero. */ /* printf("mpi_rank %d fv %d i %d j %d k %d int_data_in[j * k] %d int_expected_fill_value %d " */ /* "expected_value %d\n", mpi_rank, fv, i, j, k, int_data_in[j * k], */ /* int_expected_fill_value, expected_value); */ switch (test_type[tt]) { case NC_BYTE: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (byte_data_in[j * DIMSIZE + k] != (signed char)(k ? mpi_rank : byte_expected_fill_value)) ERR; break; case NC_SHORT: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (short_data_in[j * DIMSIZE + k] != (short)(k ? mpi_rank : short_expected_fill_value)) ERR; break; case NC_INT: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (int_data_in[j * DIMSIZE + k] != (int)(k ? mpi_rank : int_expected_fill_value)) ERR; break; case NC_FLOAT: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (float_data_in[j * DIMSIZE + k] != (float)(k ? mpi_rank : float_expected_fill_value)) ERR; break; case NC_DOUBLE: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (double_data_in[j * DIMSIZE + k] != (double)(k ? mpi_rank : double_expected_fill_value)) ERR; break; case NC_UBYTE: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (ubyte_data_in[j * DIMSIZE + k] != (unsigned char)(k ? mpi_rank : ubyte_expected_fill_value)) ERR; break; case NC_USHORT: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (ushort_data_in[j * DIMSIZE + k] != (unsigned short)(k ? mpi_rank : ushort_expected_fill_value)) ERR; break; case NC_UINT: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (uint_data_in[j * DIMSIZE + k] != (unsigned int)(k ? mpi_rank : uint_expected_fill_value)) ERR; break; case NC_INT64: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (int64_data_in[j * DIMSIZE + k] != (long long int)(k ? mpi_rank : int64_expected_fill_value)) ERR; break; case NC_UINT64: for (j = 0; j < DIMSIZE; j++) for (k = 0; k < DIMSIZE; k++) if (fv < 2 || k) if (uint64_data_in[j * DIMSIZE + k] != (unsigned long long int)(k ? mpi_rank : uint64_expected_fill_value)) ERR; break; } } /* next slab */ /* Close the netcdf file. */ if (nc_close(ncid)) ERR; if (!mpi_rank) SUMMARIZE_ERR; } /* next test type */ } /* next fill value test run */ /* Shut down MPI. */ MPI_Finalize(); if (!mpi_rank) FINAL_RESULTS; return 0; }
int main(int argc, char **argv) { int ncid, dimid, varid; int dimids[NDIMS]; /* unnormalized UTF-8 encoding for Unicode 8-character "Hello" in Greek: */ unsigned char uname_utf8[] = { 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x83, /* COMBINING TILDE */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x41, /* LATIN CAPITAL LETTER A */ 0xCC, 0x8A, /* COMBINING RING ABOVE */ 0x43, /* LATIN CAPITAL LETTER C */ 0xCC, 0xA7, /* COMBINING CEDILLA */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x45, /* LATIN CAPITAL LETTER E */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x80, /* COMBINING GRAVE ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x81, /* COMBINING ACUTE ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x82, /* COMBINING CIRCUMFLEX ACCENT */ 0x49, /* LATIN CAPITAL LETTER I */ 0xCC, 0x88, /* COMBINING DIAERESIS */ 0x4E, /* LATIN CAPITAL LETTER N */ 0xCC, 0x83, /* COMBINING TILDE */ 0x00 }; /* NFC normalized UTF-8 encoding for same Unicode string: */ unsigned char nname_utf8[] = { 0xC3, 0x80, /* LATIN CAPITAL LETTER A WITH GRAVE */ 0xC3, 0x81, /* LATIN CAPITAL LETTER A WITH ACUTE */ 0xC3, 0x82, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */ 0xC3, 0x83, /* LATIN CAPITAL LETTER A WITH TILDE */ 0xC3, 0x84, /* LATIN CAPITAL LETTER A WITH DIAERESIS */ 0xC3, 0x85, /* LATIN CAPITAL LETTER A WITH RING ABOVE */ 0xC3, 0x87, /* LATIN CAPITAL LETTER C WITH CEDILLA */ 0xC3, 0x88, /* LATIN CAPITAL LETTER E WITH GRAVE */ 0xC3, 0x89, /* LATIN CAPITAL LETTER E WITH ACUTE */ 0xC3, 0x8A, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */ 0xC3, 0x8B, /* LATIN CAPITAL LETTER E WITH DIAERESIS */ 0xC3, 0x8C, /* LATIN CAPITAL LETTER I WITH GRAVE */ 0xC3, 0x8D, /* LATIN CAPITAL LETTER I WITH ACUTE */ 0xC3, 0x8E, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */ 0xC3, 0x8F, /* LATIN CAPITAL LETTER I WITH DIAERESIS */ 0xC3, 0x91, /* LATIN CAPITAL LETTER N WITH TILDE */ 0x00 }; /* Unnormalized name used for dimension, variable, and attribute value */ #define UNAME ((char *) uname_utf8) #define UNAMELEN (sizeof uname_utf8) /* Normalized name */ #define NNAME ((char *) nname_utf8) #define NNAMELEN (sizeof nname_utf8) char name_in[UNAMELEN + 1], strings_in[UNAMELEN + 1]; nc_type att_type; size_t att_len; int res; int dimid_in, varid_in, attnum_in; int attvals[] = {42}; #define ATTNUM ((sizeof attvals)/(sizeof attvals[0])) printf("\n*** testing UTF-8 normalization..."); if((res = nc_create(FILE7_NAME, NC_CLOBBER, &ncid))) ERR; /* Define dimension with unnormalized Unicode UTF-8 encoded name */ if ((res = nc_def_dim(ncid, UNAME, NX, &dimid))) ERR; dimids[0] = dimid; /* Define variable with same name */ if ((res = nc_def_var(ncid, UNAME, NC_CHAR, NDIMS, dimids, &varid))) ERR; /* Create string attribute with same value */ if ((res = nc_put_att_text(ncid, varid, UNITS, UNAMELEN, UNAME))) ERR; /* Create int attribute with same name */ if ((res = nc_put_att_int(ncid, varid, UNAME, NC_INT, ATTNUM, attvals))) ERR; /* Try to create dimension and variable with NFC-normalized * version of same name. These should fail, as unnormalized name * should have been normalized in library, so these are attempts to * create duplicate netCDF objects. */ if ((res = nc_def_dim(ncid, NNAME, NX, &dimid)) != NC_ENAMEINUSE) ERR; if ((res = nc_def_var(ncid, NNAME, NC_CHAR, NDIMS, dimids, &varid)) != NC_ENAMEINUSE) ERR; if ((res = nc_enddef(ncid))) ERR; /* Write string data, UTF-8 encoded, to the file */ if ((res = nc_put_var_text(ncid, varid, UNAME))) ERR; if ((res = nc_close(ncid))) ERR; /* Check it out. */ if ((res = nc_open(FILE7_NAME, NC_NOWRITE, &ncid))) ERR; if ((res = nc_inq_varid(ncid, UNAME, &varid))) ERR; if ((res = nc_inq_varname(ncid, varid, name_in))) ERR; if ((res = strncmp(NNAME, name_in, NNAMELEN))) ERR; if ((res = nc_inq_varid(ncid, NNAME, &varid_in)) || varid != varid_in) ERR; if ((res = nc_inq_dimid(ncid, UNAME, &dimid_in)) || dimid != dimid_in) ERR; if ((res = nc_inq_dimid(ncid, NNAME, &dimid_in)) || dimid != dimid_in) ERR; if ((res = nc_inq_att(ncid, varid, UNITS, &att_type, &att_len))) ERR; if ((att_type != NC_CHAR || att_len != UNAMELEN)) ERR; if ((res = nc_get_att_text(ncid, varid, UNITS, strings_in))) ERR; strings_in[UNAMELEN] = '\0'; if ((res = strncmp(UNAME, strings_in, UNAMELEN))) ERR; if ((res = nc_inq_attid(ncid, varid, UNAME, &attnum_in)) || ATTNUM != attnum_in) ERR; if ((res = nc_inq_attid(ncid, varid, NNAME, &attnum_in)) || ATTNUM != attnum_in) ERR; if ((res = nc_close(ncid))) ERR; SUMMARIZE_ERR; FINAL_RESULTS; return 0; }