int ex_put_coord_names (int exoid, char *coord_names[]) { int status; size_t i; int ndimdim, varid; size_t num_dim, start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_NUM_DIM, &ndimdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of dimensions in file id %d", exoid); ex_err("ex_put_coord_names",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, ndimdim, &num_dim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: inquire failed to get number of dimensions in file id %d", exoid); ex_err("ex_put_coord_names",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_inq_varid(exoid, VAR_NAME_COOR, &varid)) == -1) { exerrval = status; sprintf(errmsg, "Error: failed to locate coordinate names in file id %d", exoid); ex_err("ex_put_coord_names",errmsg,exerrval); return (EX_FATAL); } /* write out coordinate names */ for (i=0; i<num_dim; i++) { start[0] = i; start[1] = 0; count[0] = 1; count[1] = strlen(coord_names[i]) + 1; if ((status = nc_put_vara_text(exoid, varid, start, count, coord_names[i])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store coordinate name %d in file id %d", (int)i,exoid); ex_err("ex_put_coord_names",errmsg,exerrval); return (EX_FATAL); } } return (EX_NOERR); }
NcBool NcVar::put( const char* vals, long edge0, long edge1, long edge2, long edge3, long edge4) { /* no need to check type() vs. TYPE, invoked C function will do that */ if (! the_file->data_mode()) return FALSE; size_t count[5]; count[0] = edge0; count[1] = edge1; count[2] = edge2; count[3] = edge3; count[4] = edge4; for (int i = 0; i < 5; i++) { if (count[i]) { if (num_dims() < i) return FALSE; } else break; } size_t start[5]; for (int j = 0; j < 5; j++) { start[j] = the_cur[j]; } return NcError::set_err( nc_put_vara_text (the_file->id(), the_id, start, count, vals) ) == NC_NOERR; }
/* Test a small file with one var and one att. */ static int test_one_with_att(const char *testfile) { int ncid, dimid, varid; char data = 'h', data_in; int ndims, nvars, natts, unlimdimid; size_t start[NDIMS], count[NDIMS]; /* Create a file with one ulimited dimensions, and one var. */ if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_CHAR, 1, &dimid, &varid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_NAME, 1, &data)) ERR; if (nc_enddef(ncid)) ERR; /* Write one record of var data, a single character. */ count[0] = 1; start[0] = 0; if (nc_put_vara_text(ncid, varid, start, count, &data)) ERR; /* We're done! */ if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 1 && nvars != 1 && natts != 0 && unlimdimid != 0) ERR; if (nc_get_var_text(ncid, varid, &data_in)) ERR; if (data_in != data) ERR; if (nc_get_att_text(ncid, NC_GLOBAL, ATT_NAME, &data_in)) ERR; if (data_in != data) ERR; if (nc_close(ncid)) ERR; return 0; }
int ex_put_names_internal(int exoid, int varid, size_t num_entity, char **names, ex_entity_type obj_type, const char *subtype, const char *routine) { size_t i; int status; size_t start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; int max_name_len = 0; size_t name_length; /* inquire previously defined dimensions */ name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH) + 1; for (i = 0; i < num_entity; i++) { if (names[i] != '\0') { int too_long = 0; start[0] = i; start[1] = 0; count[0] = 1; count[1] = strlen(names[i]) + 1; if (count[1] > name_length) { fprintf(stderr, "Warning: The %s %s name '%s' is too long.\n\tIt will " "be truncated from %d to %d characters\n", ex_name_of_object(obj_type), subtype, names[i], (int)strlen(names[i]), (int)name_length - 1); count[1] = name_length; too_long = 1; } if (count[1] > max_name_len) { max_name_len = count[1]; } if ((status = nc_put_vara_text(exoid, varid, start, count, names[i])) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s names in file id %d", ex_name_of_object(obj_type), exoid); ex_err(routine, errmsg, exerrval); return (EX_FATAL); } /* Add the trailing null if the variable name was too long */ if (too_long) { start[1] = name_length - 1; nc_put_var1_text(exoid, varid, start, "\0"); } } } /* Update the maximum_name_length attribute on the file. */ ex_update_max_name_length(exoid, max_name_len - 1); return (EX_NOERR); }
/* Test a small file with two record vars, which grow, and has * attributes added. */ static int test_two_growing_with_att(const char *testfile) { int ncid, dimid, varid[NUM_VARS]; char data[MAX_RECS], data_in; char att_name[NC_MAX_NAME + 1]; size_t start[ONE_DIM], count[ONE_DIM], index[ONE_DIM], len_in; int v, r; /* Create a file with one ulimited dimensions, and one var. */ if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_CHAR, 1, &dimid, &varid[0])) ERR; if (nc_def_var(ncid, VAR_NAME2, NC_CHAR, 1, &dimid, &varid[1])) ERR; if (nc_close(ncid)) ERR; /* Create some phoney data. */ for (data[0] = 'a', r = 1; r < MAX_RECS; r++) data[r] = data[r - 1] + 1; /* Normally one would not close and reopen the file for each * record, nor add an attribute each time I add a record, but I am * giving the library a little work-out here... */ for (r = 0; r < MAX_RECS; r++) { /* Write one record of var data, a single character. */ if (nc_open(testfile, NC_WRITE, &ncid)) ERR; count[0] = 1; start[0] = r; sprintf(att_name, "a_%d", data[r]); for (v = 0; v < NUM_VARS; v++) { if (nc_put_vara_text(ncid, varid[v], start, count, &data[r])) ERR; if (nc_redef(ncid)) ERR; if (nc_put_att_text(ncid, varid[v], att_name, 1, &data[r])) ERR; if (nc_enddef(ncid)) ERR; } if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR; if (nc_inq_dimlen(ncid, 0, &len_in)) ERR; if (len_in != r + 1) ERR; index[0] = r; for (v = 0; v < NUM_VARS; v++) { if (nc_get_var1_text(ncid, varid[v], index, &data_in)) ERR; if (data_in != data[r]) ERR; } if (nc_close(ncid)) ERR; } /* Next record. */ return 0; }
NcBool NcVar::put( const char* vals, const long* count ) { /* no need to check type() vs. TYPE, invoked C function will do that */ if (! the_file->data_mode()) return FALSE; size_t start[NC_MAX_DIMS]; for (int i = 0; i < num_dims(); i++) start[i] = the_cur[i]; return NcError::set_err( nc_put_vara_text (the_file->id(), the_id, start, (const size_t *)count, vals) ) == NC_NOERR; }
/* Test a diskless file with two record vars, which grow, and has * attributes added. */ static int test_two_growing_with_att(const char *testfile) { int ncid, dimid, varid[NUM_VARS]; char data[MAX_RECS], data_in; char att_name[NC_MAX_NAME + 1]; size_t start[ONE_DIM], count[ONE_DIM], index[ONE_DIM], len_in; int v, r; /* Create a file with one ulimited dimensions, and one var. */ if((status=nc_create(testfile, diskmode|NC_CLOBBER, &ncid))) ERRSTAT(status); if((status=nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimid))) ERRSTAT(status); if((status=nc_def_var(ncid, VAR_NAME, NC_CHAR, 1, &dimid, &varid[0]))) ERRSTAT(status); if((status=nc_def_var(ncid, VAR_NAME2, NC_CHAR, 1, &dimid, &varid[1]))) ERRSTAT(status); if((status=nc_enddef(ncid))) ERRSTAT(status); /* Create some phoney data. */ for (data[0] = 'a', r = 1; r < MAX_RECS; r++) data[r] = data[r - 1] + 1; for (r = 0; r < MAX_RECS; r++) { count[0] = 1; start[0] = r; sprintf(att_name, "a_%d", data[r]); for (v = 0; v < NUM_VARS; v++) { if((status=nc_put_vara_text(ncid, varid[v], start, count, &data[r]))) ERRSTAT(status); if((status=nc_redef(ncid))) ERRSTAT(status); if((status=nc_put_att_text(ncid, varid[v], att_name, 1, &data[r]))) ERRSTAT(status); if((status=nc_enddef(ncid))) ERRSTAT(status); } /* verify */ if((status=nc_inq_dimlen(ncid, 0, &len_in))) ERRSTAT(status); if (len_in != r + 1) ERR; index[0] = r; for (v = 0; v < NUM_VARS; v++) { if((status=nc_get_var1_text(ncid, varid[v], index, &data_in))) ERRSTAT(status); if (data_in != data[r]) ERR; } } /* Next record. */ if((status=nc_close(ncid))) ERRSTAT(status); return 0; }
/* Test a small file with one record var, which grows. */ static int test_one_growing(const char *testfile) { int ncid, dimid, varid; char data[MAX_RECS], data_in; size_t start[ONE_DIM], count[ONE_DIM], index[ONE_DIM], len_in; int r, f; /* Create some phoney data. */ for (data[0] = 'a', r = 1; r < MAX_RECS; r++) data[r] = data[r - 1] + 1; /* Run this with and without fill mode. */ for (f = 0; f < 2; f++) { /* Create a file with one ulimited dimensions, and one var. */ if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_var(ncid, VAR_NAME, NC_CHAR, 1, &dimid, &varid)) ERR; if (nc_close(ncid)) ERR; /* Normally one would not close and reopen the file for each * record, but I am giving the library a little work-out here... */ for (r = 0; r < MAX_RECS; r++) { /* Write one record of var data, a single character. */ if (nc_open(testfile, NC_WRITE, &ncid)) ERR; if (f && nc_set_fill(ncid, NC_NOFILL, NULL)) ERR; count[0] = 1; start[0] = r; if (nc_put_vara_text(ncid, varid, start, count, &data[r])) ERR; if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR; if (nc_inq_dimlen(ncid, 0, &len_in)) ERR; if (len_in != r + 1) ERR; index[0] = r; if (nc_get_var1_text(ncid, 0, index, &data_in)) ERR; if (data_in != data[r]) ERR; if (nc_close(ncid)) ERR; } /* Next record. */ } return 0; }
/* Test a small file with an unlimited dimension. NOTE: Normally I * write a NULL terminator for my attributes and text strings, but * this reproduces a bug that a fortran user sent us. So string data * are written to the file without null terminators. - Ed */ static int test_small_unlim(const char *testfile) { int ncid, dimids[NDIMS], varid; char data[NUM_VALS][STR_LEN + 1], data_in[NUM_VALS][STR_LEN]; int ndims, nvars, natts, unlimdimid; size_t i, start[NDIMS], count[NDIMS]; /* Create null-terminated text strings of correct length. */ /*for (i = 0; i < NUM_VALS; i++) strcpy(data[i], source);*/ strcpy(data[0], "2005-04-11_12:00:00"); strcpy(data[1], "2005-04-11_13:00:00"); /* Create a file with two dimensions, one unlimited, and one * var, and a global att. */ if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR; if (nc_def_dim(ncid, DIM1_NAME, NC_UNLIMITED, dimids)) ERR; if (nc_def_dim(ncid, DIM2_NAME, STR_LEN, &dimids[1])) ERR; if (nc_def_var(ncid, VAR_NAME, NC_CHAR, 2, dimids, &varid)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, ATT_NAME2, strlen(TITLE), TITLE)) ERR; if (nc_enddef(ncid)) ERR; /* Write some records of var data. */ count[0] = 1; count[1] = STR_LEN; start[1] = 0; for (start[0] = 0; start[0] < NUM_VALS; start[0]++) if (nc_put_vara_text(ncid, varid, start, count, data[start[0]])) ERR; /* We're done! */ if (nc_close(ncid)) ERR; /* Reopen the file and check it. */ if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != 2 && nvars != 1 && natts != 0 && unlimdimid != 0) ERR; if (nc_get_var_text(ncid, varid, (char *)data_in)) ERR; for (i = 0; i < NUM_VALS; i++) if (strncmp(data[i], data_in[i], STR_LEN)) ERR; if (nc_close(ncid)) ERR; return 0; }
/* * Write a hypercube of character values into an open netCDF file. */ static void c_ncvptc( int ncid, /* netCDF ID */ int varid, /* variable ID */ const size_t* start, /* multidimensional index of hypercube corner */ const size_t* count, /* multidimensional hypercube edge lengths */ const char* value, /* block of data values to be written */ int lenstr, /* declared length of the data argument */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0) { if (datatype != NC_CHAR) status = NC_ECHAR; else { int rank; status = nc_inq_varndims(ncid, varid, &rank); if (status == 0) { if (dimprod(count, rank) > (size_t)lenstr) status = NC_ESTS; else status = nc_put_vara_text(ncid, varid, start, count, value); } } } if (status == 0) *rcode = 0; else { nc_advise("NCVPTC", status, ""); *rcode = ncerr; } }
/********************************************************************* void mpp_put_var_value_block(int fid, int vid, const size_t *start, const size_t *nread, void *data) read part of var data, the part is defined by start and nread. *********************************************************************/ void mpp_put_var_value_block(int fid, int vid, const size_t *start, const size_t *nwrite, const void *data) { int status; char errmsg[512]; if( mpp_pe() != mpp_root_pe() ) return; if(fid<0 || fid >=nfiles) mpp_error("mpp_io(mpp_put_var_value_block): invalid fid number, fid should be " "a nonnegative integer that less than nfiles"); if(vid<0 || vid >=files[fid].nvar) mpp_error("mpp_io(mpp_put_var_value_block): invalid vid number, vid should be " "a nonnegative integer that less than nvar"); switch(files[fid].var[vid].type) { case NC_DOUBLE:case NC_FLOAT: status = nc_put_vara_double(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_INT: status = nc_put_vara_int(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_SHORT: status = nc_put_vara_short(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_CHAR: status = nc_put_vara_text(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; default: sprintf(errmsg, "mpp_io(mpp_put_var_value_block): field %s in file %s has an invalid type, " "the type should be NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT or NC_CHAR", files[fid].var[vid].name, files[fid].name ); mpp_error(errmsg); } if(status != NC_NOERR) { sprintf(errmsg, "mpp_io(mpp_put_var_value_block): Error in putting value of variable %s from file %s", files[fid].var[vid].name, files[fid].name ); netcdf_error(errmsg, status); } }; /* mpp_put_var_value_block */
static void write_dummy_names(int exoid, ex_entity_type obj_type) { const char *routine = "write_dummy_names"; size_t start[2], count[2]; char *text = ""; int varid; size_t num_entity; size_t i; ex_get_dimension(exoid, ex_dim_num_objects(obj_type), ex_name_of_object(obj_type), &num_entity, &varid, routine); for (i = 0; i < num_entity; i++) { start[0] = i; count[0] = 1; start[1] = 0; count[1] = strlen(text)+1; nc_put_vara_text(exoid, varid, start, count, text); } }
int ex_put_info (int exoid, int num_info, char *info[]) { int status; int i, lindim, num_info_dim, dims[2], varid; size_t start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; int rootid = exoid & EX_FILE_ID_MASK; exerrval = 0; /* clear error code */ /* only do this if there are records */ if (num_info > 0) { /* See if the number of info records has already been defined. Assume that if the DIM_NUM_INFO dimension exists, then the VAR_INFO variable also exists... */ status = nc_inq_dimid(rootid, DIM_NUM_INFO, &num_info_dim); if (status != NC_NOERR) { /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(rootid, DIM_LIN, &lindim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get line string length in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } /* put file into define mode */ if ((status = nc_redef (rootid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed put file id %d into define mode", rootid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } /* define dimensions */ if ((status = nc_def_dim(rootid, DIM_NUM_INFO, num_info, &num_info_dim)) != NC_NOERR) { if (status == NC_ENAMEINUSE) { /* duplicate entry? */ exerrval = status; sprintf(errmsg, "Error: info records already exist in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); } else { exerrval = status; sprintf(errmsg, "Error: failed to define number of info records in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } /* define variable */ dims[0] = num_info_dim; dims[1] = lindim; if ((status = nc_def_var(rootid, VAR_INFO, NC_CHAR, 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define info record in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } ex_compress_variable(rootid, varid, 3); /* leave define mode */ if ((status = nc_enddef (rootid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to complete info record definition in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } } else { if ((status = nc_inq_varid(rootid, VAR_INFO, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find info record variable in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } } if (info != NULL) { /* write out information records */ for (i=0; i<num_info; i++) { int length = strlen(info[i]); start[0] = i; start[1] = 0; count[0] = 1; count[1] = length < MAX_LINE_LENGTH ? length : MAX_LINE_LENGTH; if ((status = nc_put_vara_text(rootid, varid, start, count, info[i])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store info record in file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } } } else if (ex_is_parallel(rootid)) { /* All processors need to call nc_put_vara_text in case in a global collective mode */ char dummy[] = " "; for (i=0; i<num_info; i++) { start[0] = start[1] = 0; count[0] = count[1] = 0; nc_put_vara_text(rootid, varid, start, count, dummy); } } } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (nc_enddef (rootid) != NC_NOERR) { /* exit define mode */ sprintf(errmsg, "Error: failed to complete definition for file id %d", rootid); ex_err("ex_put_info",errmsg,exerrval); } return (EX_FATAL); }
void R_nc_put_vara_text( int *ncid, int *varid, int *start, int *count, char **data, int *retval ) { int ndims, err, idx_string, idx_char; size_t s_start[MAX_NC_DIMS], s_count[MAX_NC_DIMS], slen, slen2use; long i, j, k, stridx, nstrings, nj, nk; /* Get # of dims for this var */ err = nc_inq_varndims( *ncid, *varid, &ndims ); if( err != NC_NOERR ) REprintf("Error on nc_inq_varndims call in R_nc_put_vara_int: %s\n", nc_strerror(*retval) ); /* Copy over from ints to size_t. Remember things are in C style at * this point, so the rightmost dim is the number of characters */ for( i=0; i<ndims; i++ ) { s_start[i] = (size_t)start[i]; s_count[i] = (size_t)count[i]; } /* Chars are an unusually difficult because R seems to store * them as an array of character pointers, while netcdf stores * them as a monolithic block (like any other var type). We * must convert between these representations. */ slen = s_count[ndims-1]; if( ndims == 1 ) { *retval = nc_put_vara_text(*ncid, *varid, s_start, s_count, data[0] ); if( *retval != NC_NOERR ) REprintf("Error in R_nc_put_vara_int: %s\n", nc_strerror(*retval) ); } else if( ndims == 2 ) { idx_string = 0; idx_char = 1; nstrings = s_count[idx_string]; /* number of character strings */ for( i=0L; i<nstrings; i++ ) { slen2use = ((slen < strlen(data[i])) ? slen : strlen(data[i])); s_count[idx_string] = 1L; s_count[idx_char ] = slen2use; s_start[idx_string] = i + start[idx_string]; s_start[idx_char ] = 0L; *retval = nc_put_vara_text(*ncid, *varid, s_start, s_count, data[i] ); if( *retval != NC_NOERR ) { REprintf("Error in R_nc_put_vara_text: %s\n", nc_strerror(*retval) ); return; } } } else if( ndims == 3 ) { stridx = 0L; nj = s_count[0]; nstrings = s_count[1]; for( j=0L; j<nj; j++ ) for( i=0L; i<nstrings; i++ ) { slen2use = ((slen < strlen(data[i])) ? slen : strlen(data[stridx])); s_count[0] = 1L; s_count[1] = 1L; s_count[2] = slen2use; s_start[0] = j; s_start[1] = i; s_start[2] = 0L; *retval = nc_put_vara_text(*ncid, *varid, s_start, s_count, data[stridx++] ); if( *retval != NC_NOERR ) { REprintf("Error in R_nc_put_vara_text: %s\n", nc_strerror(*retval) ); return; } } } else if( ndims == 4 ) { stridx = 0L; nk = s_count[0]; nj = s_count[1]; nstrings = s_count[2]; for( k=0L; k<nk; k++ ) for( j=0L; j<nj; j++ ) for( i=0L; i<nstrings; i++ ) { slen2use = ((slen < strlen(data[i])) ? slen : strlen(data[stridx])); s_count[0] = 1L; s_count[1] = 1L; s_count[2] = 1L; s_count[3] = slen2use; s_start[0] = k; s_start[1] = j; s_start[2] = i; s_start[3] = 0L; *retval = nc_put_vara_text(*ncid, *varid, s_start, s_count, data[stridx++] ); if( *retval != NC_NOERR ) { REprintf("Error in R_nc_put_vara_text: %s\n", nc_strerror(*retval) ); return; } } } else { *retval = -1; REprintf("Error in R_nc_put_vara_text: unhandled case. I only handle char dims with # of dims up to 4. Was passed # dims = %d\n", ndims ); return; } }
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 ex_put_qa(int exoid, int num_qa_records, char *qa_record[][4]) { int status; int i, j, strdim, num_qa_dim, varid, n4dim; int dims[3]; size_t start[3], count[3]; char errmsg[MAX_ERR_LENGTH]; int rootid = exoid & EX_FILE_ID_MASK; ex_check_valid_file_id(exoid); exerrval = 0; /* clear error code */ /* only do this if there are records */ if (num_qa_records > 0) { /* See if the number of qa records has already been defined. Assume that if the DIM_NUM_QA dimension exists, then the VAR_QA_TITLE variable also exists... */ status = nc_inq_dimid(rootid, DIM_NUM_QA, &num_qa_dim); if (status != NC_NOERR) { /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(rootid, DIM_STR, &strdim)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate string length in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimid(rootid, DIM_N4, &n4dim)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate record length in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } /* put file into define mode */ if ((status = nc_redef(rootid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } /* define dimensions */ if ((status = nc_def_dim(rootid, DIM_NUM_QA, num_qa_records, &num_qa_dim)) != NC_NOERR) { if (status == NC_ENAMEINUSE) { /* duplicate entry? */ exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: qa records already exist in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); } else { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define qa record array size in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); } goto error_ret; /* exit define mode and return */ } /* define variable */ dims[0] = num_qa_dim; dims[1] = n4dim; dims[2] = strdim; if ((status = nc_def_var(rootid, VAR_QA_TITLE, NC_CHAR, 3, dims, &varid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define qa record array in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); goto error_ret; /* exit define mode and return */ } /* leave define mode */ if ((status = nc_enddef(rootid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } } else { if ((status = nc_inq_varid(rootid, VAR_QA_TITLE, &varid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find qa records variable in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } } if (qa_record != NULL) { /* write out QA records */ for (i = 0; i < num_qa_records; i++) { for (j = 0; j < 4; j++) { start[0] = i; start[1] = j; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = strlen(qa_record[i][j]) + 1; if ((status = nc_put_vara_text(rootid, varid, start, count, qa_record[i][j])) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store qa record in file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); return (EX_FATAL); } } } } else if (ex_is_parallel(rootid)) { /* In case we are in a collective mode, all processors need to call */ const char dummy[] = " "; for (i = 0; i < num_qa_records; i++) { for (j = 0; j < 4; j++) { start[0] = start[1] = start[2] = 0; count[0] = count[1] = count[2] = 0; nc_put_vara_text(rootid, varid, start, count, dummy); } } } } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (nc_enddef(rootid) != NC_NOERR) { /* exit define mode */ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", rootid); ex_err("ex_put_qa", errmsg, exerrval); } return (EX_FATAL); }
int ex_add_attr(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int64_t num_attr_per_entry) { int status; int dims[2]; int strdim, varid, att_name_varid; size_t num_obj; char errmsg[MAX_ERR_LENGTH]; const char *dnumobjent; const char *dnumobjatt; const char *vobjatt; const char *vattnam; int numobjentdim; int obj_id_ndx; int numattrdim; exerrval = 0; /* clear error code */ if (num_attr_per_entry <= 0) { return (EX_NOERR); } /* Determine index of obj_id in obj_type id array */ if (obj_type == EX_NODAL) { obj_id_ndx = 0; } else { obj_id_ndx = ex_id_lkup(exoid, obj_type, obj_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no attributes found for NULL %s %" PRId64 " in file id %d", ex_name_of_object(obj_type), obj_id, exoid); ex_err("ex_add_attr", errmsg, EX_NULLENTITY); return (EX_WARN); /* no attributes for this object */ } snprintf(errmsg, MAX_ERR_LENGTH, "Warning: failed to locate %s id %" PRId64 " in id array in file id %d", ex_name_of_object(obj_type), obj_id, exoid); ex_err("ex_add_attr", errmsg, exerrval); return (EX_WARN); } } switch (obj_type) { case EX_SIDE_SET: dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx); vobjatt = VAR_SSATTRIB(obj_id_ndx); vattnam = VAR_NAME_SSATTRIB(obj_id_ndx); break; case EX_NODE_SET: dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx); vobjatt = VAR_NSATTRIB(obj_id_ndx); vattnam = VAR_NAME_NSATTRIB(obj_id_ndx); break; case EX_EDGE_SET: dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx); vobjatt = VAR_ESATTRIB(obj_id_ndx); vattnam = VAR_NAME_ESATTRIB(obj_id_ndx); break; case EX_FACE_SET: dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx); vobjatt = VAR_FSATTRIB(obj_id_ndx); vattnam = VAR_NAME_FSATTRIB(obj_id_ndx); break; case EX_ELEM_SET: dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx); vobjatt = VAR_ELSATTRIB(obj_id_ndx); vattnam = VAR_NAME_ELSATTRIB(obj_id_ndx); break; case EX_NODAL: dnumobjent = DIM_NUM_NODES; dnumobjatt = DIM_NUM_ATT_IN_NBLK; vobjatt = VAR_NATTRIB; vattnam = VAR_NAME_NATTRIB; break; case EX_EDGE_BLOCK: dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx); vobjatt = VAR_EATTRIB(obj_id_ndx); vattnam = VAR_NAME_EATTRIB(obj_id_ndx); break; case EX_FACE_BLOCK: dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx); vobjatt = VAR_FATTRIB(obj_id_ndx); vattnam = VAR_NAME_FATTRIB(obj_id_ndx); break; case EX_ELEM_BLOCK: dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx); dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx); vobjatt = VAR_ATTRIB(obj_id_ndx); vattnam = VAR_NAME_ATTRIB(obj_id_ndx); break; default: exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Bad block type (%d) specified for file id %d", obj_type, exoid); ex_err("ex_put_attr_param", errmsg, exerrval); return (EX_FATAL); } /* element attribute array */ /* put netcdf file into define mode */ if ((status = nc_redef(exoid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); ex_err("ex_add_attr", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_def_dim(exoid, dnumobjatt, num_attr_per_entry, &numattrdim)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define number of attributes in %s %" PRId64 " in file id %d", ex_name_of_object(obj_type), obj_id, exoid); ex_err("ex_add_attr", errmsg, exerrval); goto error_ret; /* exit define mode and return */ } ex_get_dimension(exoid, dnumobjent, ex_name_of_object(obj_type), &num_obj, &numobjentdim, "ex_add_attr"); dims[0] = numobjentdim; dims[1] = numattrdim; if ((status = nc_def_var(exoid, vobjatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define attributes for %s %" PRId64 " in file id %d", ex_name_of_object(obj_type), obj_id, exoid); ex_err("ex_add_attr", errmsg, exerrval); goto error_ret; /* exit define mode and return */ } ex_compress_variable(exoid, varid, 2); /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid); ex_err("ex_add_attr", errmsg, exerrval); return (EX_FATAL); } /* Attribute names... */ dims[0] = numattrdim; dims[1] = strdim; if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s attribute name array in file id %d", ex_name_of_object(obj_type), exoid); ex_err("ex_add_attr", errmsg, exerrval); goto error_ret; /* exit define mode and return */ } /* leave define mode */ if ((exerrval = nc_enddef(exoid)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete %s definition in file id %d", ex_name_of_object(obj_type), exoid); ex_err("ex_add_attr", errmsg, exerrval); return (EX_FATAL); } /* Output a dummy empty attribute name in case client code doesn't write anything; avoids corruption in some cases. */ if (att_name_varid >= 0) { size_t count[2]; size_t start[2]; char * text = ""; size_t i; count[0] = 1; start[1] = 0; count[1] = strlen(text) + 1; for (i = 0; i < num_attr_per_entry; i++) { start[0] = i; nc_put_vara_text(exoid, att_name_varid, start, count, text); } } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (nc_enddef(exoid) != NC_NOERR) { /* exit define mode */ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", exoid); ex_err("ex_add_attr", errmsg, exerrval); } return (EX_FATAL); }
bool CNetCDFTraj::WriteHeader(CAmberTopology* p_top) { if( p_top == NULL ){ INVALID_ARGUMENT("p_top == NULL"); } if( NCID < 0 ) { ES_ERROR("file is not opened"); return(false); } int dimensionID[NC_MAX_VAR_DIMS]; NumOfTopologyAtoms = p_top->AtomList.GetNumberOfAtoms(); ActualAtoms = NumOfTopologyAtoms; CurrentSnapshot = 0; TotalSnapshots = 0; if( Coordinates != NULL ) { delete Coordinates; } Coordinates = new float[ActualAtoms*3]; // global dimmensions DefineDimension(AMBER_NETCDF_FRAME, NC_UNLIMITED, &TimeDID); DefineDimension(AMBER_NETCDF_SPATIAL, 3, &SpatialDID); DefineDimension(AMBER_NETCDF_ATOM, ActualAtoms, &CoordinateDID); DefineDimension(AMBER_NETCDF_LABEL, AMBER_NETCDF_LABELLEN, &LabelDID); DefineDimension(AMBER_NETCDF_CELL_SPATIAL, 3, &CellSpatialDID); DefineDimension(AMBER_NETCDF_CELL_ANGULAR, 3, &CellAngularDID); // put global attributes Conventions = "AMBER"; ConventionVersion = "1.0"; PutAttributeText(NC_GLOBAL, "title", Title); PutAttributeText(NC_GLOBAL, "application", Application); PutAttributeText(NC_GLOBAL, "program", Program); PutAttributeText(NC_GLOBAL, "programVersion", Version); PutAttributeText(NC_GLOBAL, "Conventions", Conventions); PutAttributeText(NC_GLOBAL, "ConventionVersion", ConventionVersion); // handle definition of non-optional variables dimensionID[0] = SpatialDID; DefineVariable(AMBER_NETCDF_SPATIAL, NC_CHAR, 1, dimensionID, &SpatialVID); dimensionID[0] = TimeDID; DefineVariable(AMBER_NETCDF_TIME, NC_FLOAT, 1, dimensionID, &TimeVID); PutAttributeText(TimeVID, "units", "picosecond"); dimensionID[0] = TimeDID; dimensionID[1] = CoordinateDID; dimensionID[2] = SpatialDID; DefineVariable(AMBER_NETCDF_COORDS, NC_FLOAT, 3, dimensionID, &CoordinateVID); PutAttributeText(CoordinateVID, "units", "angstrom"); dimensionID[0] = CellSpatialDID; DefineVariable(AMBER_NETCDF_CELL_SPATIAL, NC_CHAR, 1, dimensionID, &CellSpatialVID); dimensionID[0] = CellAngularDID; dimensionID[1] = LabelDID; DefineVariable(AMBER_NETCDF_CELL_ANGULAR, NC_CHAR, 2, dimensionID, &CellAngularVID); // set up box coords HasBox = p_top->BoxInfo.GetType() != AMBER_BOX_NONE; if( HasBox ) { dimensionID[0] = TimeDID; dimensionID[1] = CellSpatialDID; DefineVariable("cell_lengths", NC_DOUBLE, 2, dimensionID, &CellLengthVID); PutAttributeText(CellLengthVID, "units", "angstrom"); dimensionID[1] = CellAngularDID; DefineVariable("cell_angles", NC_DOUBLE, 2, dimensionID, &CellAngleVID); PutAttributeText(CellAngleVID, "units", "degree"); } int err,oldMode; // set fill mode err = nc_set_fill(NCID, NC_NOFILL, &oldMode); if (err != NC_NOERR) { CSmallString error; error << "unable to set fill value (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } // end definition err = nc_enddef(NCID); if (err != NC_NOERR) { CSmallString error; error << "unable to end definitions (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } size_t start[3]; size_t count[3]; char xyz[3]; char abc[15] = { 'a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 'a', ' ', 'g', 'a', 'm', 'm', 'a' }; // setup labels start[0] = 0; count[0] = 3; xyz[0] = 'x'; xyz[1] = 'y'; xyz[2] = 'z'; err = nc_put_vara_text(NCID, SpatialVID, start, count, xyz); if (err != NC_NOERR) { CSmallString error; error << "unable to set spatial VID 'x', 'y' and 'z' (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } start[0] = 0; count[0] = 3; xyz[0] = 'a'; xyz[1] = 'b'; xyz[2] = 'c'; err = nc_put_vara_text(NCID, CellSpatialVID, start, count, xyz); if (err != NC_NOERR) { CSmallString error; error << "unable to set spatial cell VID 'a', 'b' and 'c' (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } start[0] = 0; start[1] = 0; count[0] = 3; count[1] = 5; err = nc_put_vara_text(NCID, CellAngularVID, start, count, abc); if (err != NC_NOERR) { CSmallString error; error << "unable to set angular cell VID 'alpha', 'beta ' and 'gamma' (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } return(true); }
/*! * writes the parameters used to describe an element block * \param exoid exodus file id * \param elem_blk_id element block id * \param elem_type element type string * \param num_elem_this_blk number of elements in the element blk * \param num_nodes_per_elem number of nodes per element block * \param num_attr number of attributes * \param define_maps if != 0, write maps, else don't */ int ex_put_concat_elem_block (int exoid, const int* elem_blk_id, char *elem_type[], const int* num_elem_this_blk, const int* num_nodes_per_elem, const int* num_attr, int define_maps) { int i, varid, dimid, dims[2], strdim, *eb_array; int temp; int iblk; int status; int num_elem_blk; size_t length; int cur_num_elem_blk, nelnoddim, numelbdim, numattrdim, connid, numelemdim, numnodedim; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* first check if any element blocks are specified * OK if zero... */ if (nc_inq_dimid(exoid, DIM_NUM_EL_BLK, &dimid) != NC_NOERR) { return (EX_NOERR); } /* Get number of element blocks defined for this file */ if ((status = nc_inq_dimlen(exoid,dimid,&length)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of element blocks in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } num_elem_blk = length; /* Fill out the element block status array */ if (!(eb_array = malloc(num_elem_blk*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate space for element block status array in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } for (i=0;i<num_elem_blk;i++) { if (num_elem_this_blk[i] == 0) /* Is this a NULL element block? */ eb_array[i] = 0; /* change element block status to NULL */ else eb_array[i] = 1; /* change element block status to TRUE */ } /* Next, get variable id of status array */ if ((status = nc_inq_varid(exoid, VAR_STAT_EL_BLK, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate element block status in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } status = nc_put_var_int(exoid, varid, eb_array); if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store element block status array to file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Next, fill out ids array */ /* first get id of ids array variable */ if ((status = nc_inq_varid(exoid, VAR_ID_EL_BLK, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate element block ids array in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } /* then, write out id list */ status = nc_put_var_int(exoid, varid, elem_blk_id); if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store element block id array in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_STR, &strdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get string length in file id %d",exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } /* 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_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Iterate over element blocks ... */ for (iblk = 0; iblk < num_elem_blk; iblk++) { cur_num_elem_blk=ex_get_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK)); if (cur_num_elem_blk >= num_elem_blk) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of element blocks (%d) defined in file id %d", num_elem_blk,exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; } /* NOTE: ex_inc_file_item is used to find the number of element blocks for a specific file and returns that value incremented. */ cur_num_elem_blk=ex_inc_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK)); if (num_elem_this_blk[iblk] == 0) /* Is this a NULL element block? */ continue; /* define some dimensions and variables*/ if ((status = nc_def_dim(exoid, DIM_NUM_EL_IN_BLK(cur_num_elem_blk+1), num_elem_this_blk[iblk], &numelbdim)) != NC_NOERR) { exerrval = status; if (status == NC_ENAMEINUSE) { /* duplicate entry */ sprintf(errmsg, "Error: element block %d already defined in file id %d", elem_blk_id[iblk],exoid); } else { sprintf(errmsg, "Error: failed to define number of elements/block for block %d file id %d", elem_blk_id[iblk],exoid); } ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if ((status = nc_def_dim(exoid, DIM_NUM_NOD_PER_EL(cur_num_elem_blk+1), num_nodes_per_elem[iblk], &nelnoddim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of nodes/element for block %d in file id %d", elem_blk_id[iblk],exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* element connectivity array */ dims[0] = numelbdim; dims[1] = nelnoddim; if ((status = nc_def_var (exoid, VAR_CONN(cur_num_elem_blk+1), NC_INT, 2, dims, &connid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create connectivity array for block %d in file id %d", elem_blk_id[iblk],exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* store element type as attribute of connectivity variable */ if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(elem_type[iblk])+1, (void*)elem_type[iblk])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store element type name %s in file id %d", elem_type[iblk],exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* element attribute array */ if (num_attr[iblk] > 0) { if ((status = nc_def_dim (exoid, DIM_NUM_ATT_IN_BLK(cur_num_elem_blk+1), num_attr[iblk], &numattrdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of attributes in block %d in file id %d", elem_blk_id[iblk],exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* Attribute names... */ dims[0] = numattrdim; dims[1] = strdim; if ((status = nc_def_var(exoid, VAR_NAME_ATTRIB(cur_num_elem_blk+1), NC_CHAR, 2, dims, &temp)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define element attribute name array in file id %d",exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } eb_array[iblk] = temp; dims[0] = numelbdim; dims[1] = numattrdim; if ((status = nc_def_var(exoid, VAR_ATTRIB(cur_num_elem_blk+1), nc_flt_code(exoid), 2, dims, &temp)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define attributes for element block %d in file id %d", elem_blk_id[iblk],exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } } /* Define the element map here to avoid a later redefine call */ if (define_maps != 0) { if (nc_inq_varid(exoid, VAR_ELEM_NUM_MAP, &temp) != NC_NOERR) { /* Map does not exist */ /* Possible to have zero elements but >0 element blocks. * Only define map if there are nonzero elements */ if (nc_inq_dimid(exoid, DIM_NUM_ELEM, &numelemdim) == NC_NOERR) { dims[0] = numelemdim; if ((status = nc_def_var(exoid, VAR_ELEM_NUM_MAP, NC_INT, 1, dims, &temp)) != NC_NOERR) { exerrval = status; if (status == NC_ENAMEINUSE) { sprintf(errmsg, "Error: element numbering map already exists in file id %d", exoid); } else { sprintf(errmsg, "Error: failed to create element numbering map in file id %d", exoid); } ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } } /* Do the same for the node numbering map */ if (nc_inq_varid(exoid, VAR_NODE_NUM_MAP, &temp) != NC_NOERR) { /* Map does not exist */ if ((nc_inq_dimid(exoid, DIM_NUM_NODES, &numnodedim)) == NC_NOERR) { dims[0] = numnodedim; if ((status = nc_def_var(exoid, VAR_NODE_NUM_MAP, NC_INT, 1, dims, &temp)) != NC_NOERR) { exerrval = status; if (status == NC_ENAMEINUSE) { sprintf(errmsg, "Error: node numbering map already exists in file id %d", exoid); } else { sprintf(errmsg, "Error: failed to create node numbering map array in file id %d", exoid); } ex_err("ex_put_concat_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } } } /* leave define mode */ if ((status = nc_enddef(exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to complete element block definition in file id %d", exoid); ex_err("ex_put_concat_elem_block",errmsg,exerrval); return (EX_FATAL); } { /* Write dummy attribute name. Without this we get corruption in the * attribute name. */ size_t start[2], count[2]; char *text = ""; count[0] = 1; start[1] = 0; count[1] = strlen(text)+1; for (iblk = 0; iblk < num_elem_blk; iblk++) { if (num_elem_this_blk[iblk] == 0) /* Is this a NULL element block? */ continue; for (i = 0; i < num_attr[iblk]; i++) { start[0] = i; nc_put_vara_text(exoid, eb_array[iblk], start, count, text); } } } free(eb_array); return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: 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_concat_elem_block",errmsg,exerrval); } return (EX_FATAL); }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 coordinate dimensions and variables, some more.\n"); printf("**** testing more complex 2D coordinate variable with dimensions defined in different order..."); { #define NDIMS 5 #define NVARS 4 #define LON_NAME "lon" #define LAT_NAME "lat" #define LVL_NAME "lvl" #define TIME_NAME "time" #define TEXT_LEN_NAME "text_len" #define TEMP_NAME "temp" #define PRES_NAME "pres" #define LON_LEN 5 #define LAT_LEN 3 #define LVL_LEN 7 #define TIME_LEN 2 #define TEXT_LEN 15 #define TIME_NDIMS 2 #define DATA_NDIMS 4 int ncid, nvars_in, varids_in[NVARS]; int time_dimids[TIME_NDIMS]; int dimids[NDIMS], time_id, lon_id, pres_id, temp_id; size_t time_count[NDIMS], time_index[NDIMS] = {0, 0}; const char ttext[TEXT_LEN + 1]="20051224.150000"; char ttext_in[TEXT_LEN + 1]; int nvars, ndims, ngatts, unlimdimid; int ndims_in, natts_in, dimids_in[NDIMS]; char var_name_in[NC_MAX_NAME + 1], dim_name_in[NC_MAX_NAME + 1]; size_t len_in; nc_type xtype_in; int d; /* Create a netcdf-4 file. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; /* Define dimensions. */ if (nc_def_dim(ncid, LON_NAME, LON_LEN, &dimids[0])) ERR; if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimids[1])) ERR; if (nc_def_dim(ncid, LVL_NAME, LVL_LEN, &dimids[2])) ERR; if (nc_def_dim(ncid, TIME_NAME, TIME_LEN, &dimids[3])) ERR; if (nc_def_dim(ncid, TEXT_LEN_NAME, TEXT_LEN, &dimids[4])) ERR; /* Define two coordinate variables out of order. */ time_dimids[0] = dimids[3]; time_dimids[1] = dimids[4]; if (nc_def_var(ncid, TIME_NAME, NC_CHAR, TIME_NDIMS, time_dimids, &time_id)) ERR; if (nc_def_var(ncid, LON_NAME, NC_CHAR, 1, &dimids[0], &lon_id)) ERR; /* Write one time to the coordinate variable. */ time_count[0] = 1; time_count[1] = TEXT_LEN; if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR; /* Define two data variable. */ if (nc_def_var(ncid, PRES_NAME, NC_CHAR, DATA_NDIMS, dimids, &pres_id)) ERR; if (nc_def_var(ncid, TEMP_NAME, NC_CHAR, DATA_NDIMS, dimids, &temp_id)) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Check the metadata. */ if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != NVARS || ndims != NDIMS || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != NVARS || varids_in[0] != 0 || varids_in[1] != 1 || varids_in[2] != 2 || varids_in[3] != 3) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, TIME_NAME) || xtype_in != NC_CHAR || ndims_in != TIME_NDIMS || dimids_in[0] != time_dimids[0] || dimids_in[1] != time_dimids[1] || natts_in != 0) ERR; if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; if (ndims_in != NDIMS) ERR; for (d = 0; d < NDIMS; d++) if (dimids_in[d] != dimids[d]) ERR; if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LON_NAME) || len_in != LON_LEN) ERR; if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LAT_NAME) || len_in != LAT_LEN) ERR; if (nc_inq_dim(ncid, 2, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LVL_NAME) || len_in != LVL_LEN) ERR; if (nc_inq_dim(ncid, 3, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TIME_NAME) || len_in != TIME_LEN) ERR; if (nc_inq_dim(ncid, 4, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TEXT_LEN_NAME) || len_in != TEXT_LEN) ERR; /* Close up. */ if (nc_close(ncid)) ERR; /* Open the file and check the order of variables and dimensions. */ if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (nvars != NVARS || ndims != NDIMS || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR; if (nvars_in != NVARS || varids_in[0] != 0 || varids_in[1] != 1 || varids_in[2] != 2 || varids_in[3] != 3) ERR; if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(var_name_in, TIME_NAME) || xtype_in != NC_CHAR || ndims_in != TIME_NDIMS || dimids_in[0] != time_dimids[0] || dimids_in[1] != time_dimids[1] || natts_in != 0) ERR; if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR; if (ndims_in != NDIMS) ERR; for (d = 0; d < NDIMS; d++) if (dimids_in[d] != dimids[d]) ERR; if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LON_NAME) || len_in != LON_LEN) ERR; if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LAT_NAME) || len_in != LAT_LEN) ERR; if (nc_inq_dim(ncid, 2, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, LVL_NAME) || len_in != LVL_LEN) ERR; if (nc_inq_dim(ncid, 3, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TIME_NAME) || len_in != TIME_LEN) ERR; if (nc_inq_dim(ncid, 4, dim_name_in, &len_in)) ERR; if (strcmp(dim_name_in, TEXT_LEN_NAME) || len_in != TEXT_LEN) ERR; /* Check the data. */ if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR; if (strncmp(ttext, ttext_in, TEXT_LEN)) ERR; /* Close up. */ if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("**** testing example from bug NCF-247, multidimensional coord variable in subgroup ..."); { #define GRPNAME "g1" #define DIM0NAME "dim0" #define DIM1NAME "dim1" #define DIM2NAME "coord" #define DIM3NAME "dim3" #define VARNAME DIM2NAME #define VARRANK 2 int ncid, grpid, varid, var_dims[VARRANK], var_dims_in[VARRANK]; char name2[NC_MAX_NAME], name3[NC_MAX_NAME]; int dim0_dim, dim1_dim, dim2_dim, dim3_dim; size_t dim0_len = 3, dim1_len = 2, dim2_len = 5, dim3_len = 7; if (nc_create(FILE_NAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, GRPNAME, &grpid)) ERR; if (nc_def_dim(ncid, DIM0NAME, dim0_len, &dim0_dim)) ERR; if (nc_def_dim(ncid, DIM1NAME, dim1_len, &dim1_dim)) ERR; if (nc_def_dim(grpid, DIM2NAME, dim2_len, &dim2_dim)) ERR; if (nc_def_dim(grpid, DIM3NAME, dim3_len, &dim3_dim)) ERR; var_dims[0] = dim2_dim; var_dims[1] = dim3_dim; if (nc_def_var(grpid, VARNAME, NC_INT, VARRANK, var_dims, &varid)) ERR; if (nc_close(ncid)) ERR; if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_grp_ncid(ncid, GRPNAME, &grpid)) ERR; if (nc_inq_varid(grpid, VARNAME, &varid)) ERR; if (nc_inq_vardimid(grpid, varid, var_dims_in)) ERR; if (nc_inq_dimname(grpid, var_dims_in[0], name2)) ERR; if (nc_inq_dimname(grpid, var_dims_in[1], name3)) ERR; if (strcmp(name2, DIM2NAME) != 0 || strcmp(name3, DIM3NAME) != 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
void NetCDFFileIOHandler::Write( const Teuchos::RCP<const Epetra_MultiVector>& MV, const std::string& filename ) { #ifdef EPETRA_MPI Epetra_MpiComm comm( MPI_COMM_WORLD ); #else Epetra_SerialComm comm; #endif // // Variables for NetCDF // int status; int ncid, len_string_id, num_nodes_id, num_nod_var_id, row_id, col_id, ss_id, name_nod_var_id; int i,j; int ss_dimids[2]; //size_t start[1],count[1]; size_t start2[2],count2[2]; // // Variables for Epetra // int num_vecs = MV->NumVectors(); int dim = MV->GlobalLength(); Epetra_Map Map( dim, 0, comm ); Epetra_Map* Proc0Map; const Epetra_Vector* col_newMV; // // Create map putting all elements of vector on Processor 0. // if ( comm.MyPID() == 0 ) { Proc0Map = new Epetra_Map( dim, dim, 0, comm ); } else { Proc0Map = new Epetra_Map( dim, 0, 0, comm ); } Epetra_Vector Proc0Vector( *Proc0Map ); // // Create an exporter to get the global Epetra_Vector to a local Epetra_Vector. // Epetra_Export exporter( MV->Map(), *Proc0Map ); // if ( comm.MyPID() == 0 ) { // // Open basis output file and define output variables going into the file. // std::string temp_filename = out_path + filename; status=nc_create(temp_filename.c_str(),NC_CLOBBER,&ncid); if (status != NC_NOERR) handle_error(status); status=nc_def_dim(ncid,"len_string",(long)len_string,&len_string_id); if (status != NC_NOERR) handle_error(status); status=nc_def_dim(ncid,"num_nodes",(long)num_nodes,&num_nodes_id); if (status != NC_NOERR) handle_error(status); status=nc_def_dim(ncid,"num_nod_var",(long)num_nod_var,&num_nod_var_id); if (status != NC_NOERR) handle_error(status); status=nc_def_dim(ncid,"row",NC_UNLIMITED,&row_id); if (status != NC_NOERR) handle_error(status); status=nc_def_dim(ncid,"col",(long)dim,&col_id); if (status != NC_NOERR) handle_error(status); ss_dimids[0]=row_id; ss_dimids[1]=col_id; status=nc_def_var(ncid,"snapshot",NC_FLOAT,2,ss_dimids,&ss_id); if (status != NC_NOERR) handle_error(status); ss_dimids[0]=num_nod_var_id; ss_dimids[1]=len_string_id; status=nc_def_var(ncid,"name_nod_var",NC_CHAR,2,ss_dimids,&name_nod_var_id); if (status != NC_NOERR) handle_error(status); status=nc_enddef(ncid); if (status != NC_NOERR) handle_error(status); } // Initialize data pointers for writing out basis to file. float* temp_vec = 0; if ( comm.MyPID() == 0 ) temp_vec = new float[ dim ]; for (i=0; i<num_vecs; ++i) { // // Get column of Epetra_MultiVector in terms of Epetra_Vector. // col_newMV = (*MV)( i ); // Proc0Vector.Export(*col_newMV, exporter, Insert); // if ( comm.MyPID()==0 ) { start2[0] = i; start2[1] = 0; count2[0] = 1; count2[1] = dim; // // Copy double precision vector to single precision and write out. // for (j=0; j<dim; ++j) temp_vec[j] = Proc0Vector[j]; status=nc_put_vara_float(ncid,ss_id,start2,count2,temp_vec); if (status != NC_NOERR) handle_error(status); } } // Write the list of names of the nodal variables to the Netcdf file */ if ( comm.MyPID() == 0 ) { for(i=0; i<num_nod_var; ++i) { start2[0] = i; start2[1] = 0; count2[0] = 1; count2[1] = strlen(var_name[i]); /* printf("start2=%d %d\n",start2[0],start2[1]); printf("count2=%d %d\n",count2[0],count2[1]); */ status=nc_put_vara_text(ncid,name_nod_var_id,start2,count2,var_name[i]); if (status != NC_NOERR) handle_error(status); } status=nc_close(ncid); if (status != NC_NOERR) handle_error(status); } // Clean up. delete Proc0Map; if (temp_vec) delete [] temp_vec; }
/* * NETCDF_setupOutput() * Given a coordinateInfo structure which has been opened for writing NETCDF * (NCInfo is allocated), set up the NCInfo structure. * Return 0 on success, 1 on failure. */ int NETCDF_setupOutput(coordinateInfo *trajInfo, int atoms) { #ifdef BINTRAJ netcdfTrajectoryInfo *NCInfo; int dimensionID[NC_MAX_VAR_DIMS],err,oldMode; # ifdef MPI MPI_Offset start[3], count[3]; # else size_t start[3], count[3]; # endif char xyz[3]; char abc[15] = { 'a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 'a', ' ', 'g', 'a', 'm', 'm', 'a' }; if (trajInfo->NCInfo==NULL) return 1; NCInfo = trajInfo->NCInfo; /* * define the global dimensions of the NetCDF file */ netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_FRAME, NC_UNLIMITED, &NCInfo->frameDID); netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_SPATIAL, 3, &NCInfo->spatialDID); netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_ATOM, atoms, &NCInfo->atomDID); netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_LABEL, AMBER_NETCDF_LABELLEN, &NCInfo->labelDID); netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_CELL_SPATIAL, 3, &NCInfo->cell_spatialDID); netcdfDefineDimension(NCInfo->ncid, AMBER_NETCDF_CELL_ANGULAR, 3, &NCInfo->cell_angularDID); /* * put global attributes */ netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "title", trajInfo->title); netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "application", trajInfo->application); netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "program", trajInfo->program); netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "programVersion", trajInfo->version); netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "Conventions", "AMBER"); netcdfPutAttributeText(NCInfo->ncid, NC_GLOBAL, "ConventionVersion", "1.0"); /* * handle definition of non-optional variables */ dimensionID[0] = NCInfo->spatialDID; netcdfDefineVariable(NCInfo->ncid, AMBER_NETCDF_SPATIAL, NC_CHAR, 1, dimensionID, &NCInfo->spatialVID); dimensionID[0] = NCInfo->frameDID; netcdfDefineVariable(NCInfo->ncid, AMBER_NETCDF_TIME, NC_FLOAT, 1, dimensionID, &NCInfo->timeVID); netcdfPutAttributeText(NCInfo->ncid, NCInfo->timeVID, "units", "picosecond"); dimensionID[0] = NCInfo->frameDID; dimensionID[1] = NCInfo->atomDID; dimensionID[2] = NCInfo->spatialDID; netcdfDefineVariable(NCInfo->ncid, AMBER_NETCDF_COORDS, NC_FLOAT, 3, dimensionID, &NCInfo->coordinateVID); netcdfPutAttributeText(NCInfo->ncid, NCInfo->coordinateVID, "units", "angstrom"); dimensionID[0] = NCInfo->cell_spatialDID; netcdfDefineVariable(NCInfo->ncid, AMBER_NETCDF_CELL_SPATIAL, NC_CHAR, 1, dimensionID, &NCInfo->cell_spatialVID); dimensionID[0] = NCInfo->cell_angularDID; dimensionID[1] = NCInfo->labelDID; netcdfDefineVariable(NCInfo->ncid, AMBER_NETCDF_CELL_ANGULAR, NC_CHAR, 2, dimensionID, &NCInfo->cell_angularVID); // Set up Box coords if (trajInfo->isBox) { dimensionID[0] = NCInfo->frameDID; dimensionID[1] = NCInfo->cell_spatialDID; netcdfDefineVariable(NCInfo->ncid, "cell_lengths", NC_DOUBLE, 2, dimensionID, &NCInfo->cellLengthVID); netcdfPutAttributeText(NCInfo->ncid, NCInfo->cellLengthVID, "units", "angstrom"); dimensionID[1] = NCInfo->cell_angularDID; netcdfDefineVariable(NCInfo->ncid, "cell_angles", NC_DOUBLE, 2, dimensionID, &NCInfo->cellAngleVID); netcdfPutAttributeText(NCInfo->ncid, NCInfo->cellAngleVID, "units", "degree"); } /* DAN ROE: Replica temperature */ if (trajInfo->isREMDTRAJ) { fprintf(stdout,"NETCDF: Defining replica temperature in output trajectory.\n"); dimensionID[0] = NCInfo->frameDID; netcdfDefineVariable(NCInfo->ncid, "temp0", NC_DOUBLE, 1, dimensionID, &NCInfo->TempVarID); netcdfPutAttributeText(NCInfo->ncid, NCInfo->TempVarID,"units","kelvin"); } /* * set fill mode */ err = nc_set_fill(NCInfo->ncid, NC_NOFILL, &oldMode); if (err != NC_NOERR) { printfone("NetCDF setting fill value: %s\n", nc_strerror(err)); return 1; } /* * end of NetCDF definitions */ err = nc_enddef(NCInfo->ncid); if (err != NC_NOERR) { printfone("NetCDF error on ending definitions: %s\n", nc_strerror(err)); return 1; } /* * specify spatial dimension labels */ start[0] = 0; count[0] = 3; xyz[0] = 'x'; xyz[1] = 'y'; xyz[2] = 'z'; err = nc_put_vara_text(NCInfo->ncid, NCInfo->spatialVID, start, count, xyz); if (err != NC_NOERR) { printfone("Error on NetCDF output of spatial VID 'x', 'y' and 'z': %s\n", nc_strerror(err)); return 1; } xyz[0] = 'a'; xyz[1] = 'b'; xyz[2] = 'c'; err = nc_put_vara_text(NCInfo->ncid, NCInfo->cell_spatialVID, start, count, xyz); if (err != NC_NOERR) { printfone("Error on NetCDF output of spatial VID 'x', 'y' and 'z': %s\n", nc_strerror(err)); return 1; } start[0] = 0; start[1] = 0; count[0] = 3; count[1] = 5; err = nc_put_vara_text(NCInfo->ncid, NCInfo->cell_angularVID, start, count, abc); if (err != NC_NOERR) { printfone("Error on NetCDF output of angular VID 'alpha', 'beta ' and 'gamma': %s\n", nc_strerror(err)); return 1; } //outInfo->NCInfo = NCInfo; return 0; #endif return 1; }
int ex_put_names (int exoid, ex_entity_type obj_type, char* names[]) { int status; int varid; size_t i, num_entity; size_t start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; const char *vname = NULL; const char *routine = "ex_put_names"; exerrval = 0; /* clear error code */ switch (obj_type) { /* ======== BLOCKS ========= */ case EX_EDGE_BLOCK: vname = VAR_NAME_ED_BLK; break; case EX_FACE_BLOCK: vname = VAR_NAME_FA_BLK; break; case EX_ELEM_BLOCK: vname = VAR_NAME_EL_BLK; break; /* ======== SETS ========= */ case EX_NODE_SET: vname = VAR_NAME_NS; break; case EX_EDGE_SET: vname = VAR_NAME_ES; break; case EX_FACE_SET: vname = VAR_NAME_FS; break; case EX_SIDE_SET: vname = VAR_NAME_SS; break; case EX_ELEM_SET: vname = VAR_NAME_ELS; break; /* ======== MAPS ========= */ case EX_NODE_MAP: vname = VAR_NAME_NM; break; case EX_EDGE_MAP: vname = VAR_NAME_EDM; break; case EX_FACE_MAP: vname = VAR_NAME_FAM; break; case EX_ELEM_MAP: vname = VAR_NAME_EM; break; /* ======== ERROR (Invalid type) ========= */ default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: Invalid type specified in file id %d", exoid); ex_err(routine,errmsg,exerrval); return(EX_FATAL); } ex_get_dimension(exoid, ex_dim_num_objects(obj_type), ex_name_of_object(obj_type), &num_entity, &varid, routine); if ((status = nc_inq_varid(exoid, vname, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate %s names in file id %d", ex_name_of_object(obj_type), exoid); ex_err(routine,errmsg,exerrval); return (EX_FATAL); } /* write EXODUS entitynames */ for (i=0; i<num_entity; i++) { if (names[i] != '\0') { start[0] = i; start[1] = 0; count[0] = 1; count[1] = strlen(names[i]) + 1; if ((status = nc_put_vara_text(exoid, varid, start, count, names[i])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s names in file id %d", ex_name_of_object(obj_type), exoid); ex_err(routine,errmsg,exerrval); return (EX_FATAL); } } } return(EX_NOERR); }
/*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_put_info (int exoid, int num_info, char *info[]) { int status; int i, lindim, num_info_dim, dims[2], varid; size_t start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* only do this if there are records */ if (num_info > 0) { /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_LIN, &lindim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get line string length in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } /* put file into define mode */ if ((status = nc_redef (exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed put file id %d into define mode", exoid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } /* define dimensions */ if ((status = nc_def_dim(exoid, DIM_NUM_INFO, num_info, &num_info_dim)) != NC_NOERR) { if (status == NC_ENAMEINUSE) { /* duplicate entry? */ exerrval = status; sprintf(errmsg, "Error: info records already exist in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); } else { exerrval = status; sprintf(errmsg, "Error: failed to define number of info records in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } /* define variable */ dims[0] = num_info_dim; dims[1] = lindim; if ((status = nc_def_var(exoid, VAR_INFO, NC_CHAR, 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define info record in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* leave define mode */ if ((status = nc_enddef (exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to complete info record definition in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } /* write out information records */ for (i=0; i<num_info; i++) { int length = strlen(info[i]); start[0] = i; start[1] = 0; count[0] = 1; count[1] = length < MAX_LINE_LENGTH ? length : MAX_LINE_LENGTH; if ((status = nc_put_vara_text(exoid, varid, start, count, info[i])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store info record in file id %d", exoid); ex_err("ex_put_info",errmsg,exerrval); return (EX_FATAL); } } } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: 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_info",errmsg,exerrval); } return (EX_FATAL); }
int ex_put_block_params( int exoid, size_t block_count, const struct ex_block *blocks) { size_t i; int conn_int_type; int status; int arbitrary_polyhedra = 0; /* 1 if block is arbitrary 2d polyhedra type; 2 if 3d polyhedra */ int att_name_varid = -1; int varid, dimid, dims[2], blk_id_ndx, blk_stat, strdim; size_t start[2]; size_t num_blk; int cur_num_blk, numblkdim, numattrdim; int nnodperentdim = -1; int nedgperentdim = -1; int nfacperentdim = -1; int connid; int npeid; char errmsg[MAX_ERR_LENGTH]; char entity_type1[5]; char entity_type2[5]; int* blocks_to_define = NULL; const char* dnumblk = NULL; const char* vblkids = NULL; const char* vblksta = NULL; const char* vnodcon = NULL; const char* vnpecnt = NULL; const char* vedgcon = NULL; const char* vfaccon = NULL; const char* vconn = NULL; const char* vattnam = NULL; const char* vblkatt = NULL; const char* dneblk = NULL; const char* dnape = NULL; const char* dnnpe = NULL; const char* dnepe = NULL; const char* dnfpe = NULL; exerrval = 0; /* clear error code */ blocks_to_define = malloc(block_count*sizeof(int)); for (i=0; i < block_count; i++) { switch (blocks[i].type) { case EX_EDGE_BLOCK: dnumblk = DIM_NUM_ED_BLK; vblkids = VAR_ID_ED_BLK; vblksta = VAR_STAT_ED_BLK; break; case EX_FACE_BLOCK: dnumblk = DIM_NUM_FA_BLK; vblkids = VAR_ID_FA_BLK; vblksta = VAR_STAT_FA_BLK; break; case EX_ELEM_BLOCK: dnumblk = DIM_NUM_EL_BLK; vblkids = VAR_ID_EL_BLK; vblksta = VAR_STAT_EL_BLK; break; default: exerrval = EX_BADPARAM; sprintf( errmsg, "Error: Bad block type (%d) specified for entry %d file id %d", blocks[i].type, (int)i, exoid ); ex_err( "ex_put_block_params", errmsg, exerrval ); free(blocks_to_define); return (EX_FATAL); } /* first check if any blocks of that type are specified */ if ((status = ex_get_dimension(exoid, dnumblk, ex_name_of_object(blocks[i].type), &num_blk, &dimid, "ex_put_block_params")) != NC_NOERR) { sprintf(errmsg, "Error: No %ss defined in file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return EX_FATAL; } /* Next: Make sure that there are not any duplicate block ids by searching the vblkids array. WARNING: This must be done outside of define mode because id_lkup accesses the database to determine the position */ if ((status = nc_inq_varid(exoid, vblkids, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate %s ids in file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return EX_FATAL; } ex_id_lkup(exoid,blocks[i].type,blocks[i].id); /* Error value used, but don't need return value */ if (exerrval != EX_LOOKUPFAIL) { /* found the element block id */ exerrval = EX_FATAL; sprintf(errmsg, "Error: %s id %"PRId64" already exists in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } /* Keep track of the total number of element blocks defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is a function that finds the number of element blocks for a specific file and returns that value. */ cur_num_blk=ex_get_file_item(exoid, ex_get_counter_list(blocks[i].type)); if (cur_num_blk >= (int)num_blk) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of %ss (%d) defined in file id %d", ex_name_of_object(blocks[i].type), (int)num_blk,exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } /* NOTE: ex_inc_file_item is a function that finds the number of element blocks for a specific file and returns that value incremented. */ cur_num_blk=ex_inc_file_item(exoid, ex_get_counter_list(blocks[i].type)); start[0] = cur_num_blk; /* write out block id to previously defined id array variable*/ status = nc_put_var1_longlong(exoid, varid, start, (long long*)&blocks[i].id); if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s id to file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } blocks_to_define[i] = start[0]+1; /* element id index into vblkids array*/ if (blocks[i].num_entry == 0) /* Is this a NULL element block? */ blk_stat = 0; /* change element block status to NULL */ else blk_stat = 1; /* change element block status to EX_EX_TRUE */ if ((status = nc_inq_varid (exoid, vblksta, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate %s status in file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } if ((status = nc_put_var1_int(exoid, varid, start, &blk_stat)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s id %"PRId64" status to file id %d", ex_name_of_object(blocks[i].type), blocks[i].id, exoid); ex_err("ex_put_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } } /* 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_block_params",errmsg,exerrval); free(blocks_to_define); return (EX_FATAL); } for (i=0; i < block_count; i++) { if (blocks[i].num_entry == 0) {/* Is this a NULL element block? */ continue; } blk_id_ndx = blocks_to_define[i]; switch (blocks[i].type) { case EX_EDGE_BLOCK: dneblk = DIM_NUM_ED_IN_EBLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_ED(blk_id_ndx); dnepe = 0; dnfpe = 0; dnape = DIM_NUM_ATT_IN_EBLK(blk_id_ndx); vblkatt = VAR_EATTRIB(blk_id_ndx); vattnam = VAR_NAME_EATTRIB(blk_id_ndx); vnodcon = VAR_EBCONN(blk_id_ndx); vedgcon = 0; vfaccon = 0; break; case EX_FACE_BLOCK: dneblk = DIM_NUM_FA_IN_FBLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_FA(blk_id_ndx); dnepe = 0; dnfpe = 0; dnape = DIM_NUM_ATT_IN_FBLK(blk_id_ndx); vblkatt = VAR_FATTRIB(blk_id_ndx); vattnam = VAR_NAME_FATTRIB(blk_id_ndx); vnodcon = VAR_FBCONN(blk_id_ndx); vnpecnt = VAR_FBEPEC(blk_id_ndx); vedgcon = 0; vfaccon = 0; break; case EX_ELEM_BLOCK: dneblk = DIM_NUM_EL_IN_BLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_EL(blk_id_ndx); dnepe = DIM_NUM_EDG_PER_EL(blk_id_ndx); dnfpe = DIM_NUM_FAC_PER_EL(blk_id_ndx); dnape = DIM_NUM_ATT_IN_BLK(blk_id_ndx); vblkatt = VAR_ATTRIB(blk_id_ndx); vattnam = VAR_NAME_ATTRIB(blk_id_ndx); vnodcon = VAR_CONN(blk_id_ndx); vnpecnt = VAR_EBEPEC(blk_id_ndx); vedgcon = VAR_ECONN(blk_id_ndx); vfaccon = VAR_FCONN(blk_id_ndx); break; default: goto error_ret; } /* define some dimensions and variables*/ if ((status = nc_def_dim(exoid,dneblk,blocks[i].num_entry, &numblkdim )) != NC_NOERR) { if (status == NC_ENAMEINUSE) { /* duplicate entry */ exerrval = status; sprintf(errmsg, "Error: %s %"PRId64" already defined in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); } else { exerrval = status; sprintf(errmsg, "Error: failed to define number of entities/block for %s %"PRId64" file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } if ( dnnpe && blocks[i].num_nodes_per_entry > 0) { /* A nfaced block would not have any nodes defined... */ if ((status = nc_def_dim(exoid,dnnpe,blocks[i].num_nodes_per_entry, &nnodperentdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of nodes/entity for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } if (dnepe && blocks[i].num_edges_per_entry > 0 ) { if ((status = nc_def_dim (exoid,dnepe,blocks[i].num_edges_per_entry, &nedgperentdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of edges/entity for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } if ( dnfpe && blocks[i].num_faces_per_entry > 0 ) { if ((status = nc_def_dim(exoid,dnfpe,blocks[i].num_faces_per_entry, &nfacperentdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of faces/entity for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } /* element attribute array */ if (blocks[i].num_attribute > 0) { if ((status = nc_def_dim(exoid, dnape, blocks[i].num_attribute, &numattrdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of attributes in %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } dims[0] = numblkdim; dims[1] = numattrdim; if ((status = nc_def_var(exoid, vblkatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define attributes for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } ex_compress_variable(exoid, varid, 2); /* inquire previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get string length in file id %d",exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; } /* Attribute names... */ dims[0] = numattrdim; dims[1] = strdim; if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define %s attribute name array in file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } conn_int_type = NC_INT; if (ex_int64_status(exoid) & EX_BULK_INT64_DB) { conn_int_type = NC_INT64; } /* See if storing an 'nsided' element block (arbitrary 2d polyhedra or super element) */ if (strlen(blocks[i].topology) >= 3) { if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && (blocks[i].topology[1] == 's' || blocks[i].topology[1] == 'S') && (blocks[i].topology[2] == 'i' || blocks[i].topology[2] == 'I')) arbitrary_polyhedra = 1; else if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && (blocks[i].topology[1] == 'f' || blocks[i].topology[1] == 'F') && (blocks[i].topology[2] == 'a' || blocks[i].topology[2] == 'A')) /* If a FACE_BLOCK, then we are dealing with the faces of the nfaced blocks[i]. */ arbitrary_polyhedra = blocks[i].type == EX_FACE_BLOCK ? 1 : 2; } /* element connectivity array */ if (arbitrary_polyhedra > 0) { if (blocks[i].type != EX_FACE_BLOCK && blocks[i].type != EX_ELEM_BLOCK) { exerrval = EX_BADPARAM; sprintf( errmsg, "Error: Bad block type (%d) for nsided/nfaced block in file id %d", blocks[i].type, exoid ); ex_err( "ex_put_block_params", errmsg, exerrval ); goto error_ret; } if (arbitrary_polyhedra == 1) { dims[0] = nnodperentdim; vconn = vnodcon; /* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/ strcpy(entity_type1, "NODE"); if (blocks[i].type == EX_ELEM_BLOCK) strcpy(entity_type2, "ELEM"); else strcpy(entity_type2, "FACE"); } else if (arbitrary_polyhedra == 2) { dims[0] = nfacperentdim; vconn = vfaccon; /* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/ strcpy(entity_type1, "FACE"); strcpy(entity_type2, "ELEM"); } if ((status = nc_def_var(exoid, vconn, conn_int_type, 1, dims, &connid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create connectivity array for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* element face-per-element or node-per-element count array */ dims[0] = numblkdim; if ((status = nc_def_var(exoid, vnpecnt, conn_int_type, 1, dims, &npeid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create face- or node- per-entity count array for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id, exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if ((status = nc_put_att_text(exoid, npeid, "entity_type1", strlen(entity_type1)+1, entity_type1)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store entity type attribute text for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id, exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if ((status = nc_put_att_text(exoid, npeid, "entity_type2", strlen(entity_type2)+1, entity_type2)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store entity type attribute text for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id, exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } else { /* "Normal" (non-polyhedra) element block type */ dims[0] = numblkdim; dims[1] = nnodperentdim; if ((status = nc_def_var(exoid, vnodcon, conn_int_type, 2, dims, &connid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create connectivity array for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } ex_compress_variable(exoid, connid, 1); } /* store element type as attribute of connectivity variable */ if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(blocks[i].topology)+1, blocks[i].topology)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s type name %s in file id %d", ex_name_of_object(blocks[i].type), blocks[i].topology,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if (arbitrary_polyhedra == 0) { if (vedgcon && blocks[i].num_edges_per_entry ) { dims[0] = numblkdim; dims[1] = nedgperentdim; if ((status = nc_def_var(exoid, vedgcon, conn_int_type, 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create edge connectivity array for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } if ( vfaccon && blocks[i].num_faces_per_entry ) { dims[0] = numblkdim; dims[1] = nfacperentdim; if ((status = nc_def_var(exoid, vfaccon, conn_int_type, 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to create face connectivity array for %s %"PRId64" in file id %d", ex_name_of_object(blocks[i].type), blocks[i].id,exoid); ex_err("ex_put_block_params",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } } } free(blocks_to_define); /* leave define mode */ if ((exerrval=nc_enddef (exoid)) != NC_NOERR) { sprintf(errmsg, "Error: failed to complete %s definition in file id %d", ex_name_of_object(blocks[i].type), exoid); ex_err("ex_put_block_params",errmsg,exerrval); return (EX_FATAL); } for (i=0; i < block_count; i++) { switch (blocks[i].type) { case EX_EDGE_BLOCK: vblkids = VAR_ID_ED_BLK; break; case EX_FACE_BLOCK: vblkids = VAR_ID_FA_BLK; break; case EX_ELEM_BLOCK: vblkids = VAR_ID_EL_BLK; break; default: return (EX_FATAL); /* should have been handled earlier; quiet compiler here */ } nc_inq_varid(exoid, vblkids, &att_name_varid); if (blocks[i].num_attribute > 0 && att_name_varid >= 0) { /* Output a dummy empty attribute name in case client code doesn't write anything; avoids corruption in some cases. */ size_t count[2]; char *text = ""; size_t j; count[0] = 1; start[1] = 0; count[1] = strlen(text)+1; for (j = 0; j < blocks[i].num_attribute; j++) { start[0] = j; nc_put_vara_text(exoid, att_name_varid, start, count, text); } } } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: free(blocks_to_define); 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_block_params",errmsg,exerrval); } return (EX_FATAL); }
int CNetCdfInterface::ncPutVaraType(int ncid, int varid, const StdSize* start, const StdSize* count, const char* data) { return nc_put_vara_text(ncid, varid, start, count, data); }
// NetcdfFile::NC_create() int NetcdfFile::NC_create(std::string const& Name, NCTYPE type, int natomIn, CoordinateInfo const& coordInfo, std::string const& title) { if (Name.empty()) return 1; int dimensionID[NC_MAX_VAR_DIMS]; int NDIM; nc_type dataType; if (ncdebug_>1) mprintf("DEBUG: NC_create: %s natom=%i V=%i F=%i box=%i temp=%i time=%i\n", Name.c_str(),natomIn,(int)coordInfo.HasVel(), (int)coordInfo.HasForce(),(int)coordInfo.HasBox(), (int)coordInfo.HasTemp(),(int)coordInfo.HasTime()); if ( checkNCerr( nc_create( Name.c_str(), NC_64BIT_OFFSET, &ncid_) ) ) return 1; ncatom_ = natomIn; ncatom3_ = ncatom_ * 3; // Set number of dimensions based on file type switch (type) { case NC_AMBERENSEMBLE: NDIM = 4; dataType = NC_FLOAT; break; case NC_AMBERTRAJ: NDIM = 3; dataType = NC_FLOAT; break; case NC_AMBERRESTART: NDIM = 2; dataType = NC_DOUBLE; break; default: mprinterr("Error: NC_create (%s): Unrecognized type (%i)\n",Name.c_str(),(int)type); return 1; } if (type == NC_AMBERENSEMBLE) { // Ensemble dimension for ensemble if (coordInfo.EnsembleSize() < 1) { mprinterr("Internal Error: NetcdfFile: ensembleSize < 1\n"); return 1; } if ( checkNCerr(nc_def_dim(ncid_, NCENSEMBLE, coordInfo.EnsembleSize(), &ensembleDID_)) ) { mprinterr("Error: Defining ensemble dimension.\n"); return 1; } dimensionID[1] = ensembleDID_; } ncframe_ = 0; if (type == NC_AMBERTRAJ || type == NC_AMBERENSEMBLE) { // Frame dimension for traj if ( checkNCerr( nc_def_dim( ncid_, NCFRAME, NC_UNLIMITED, &frameDID_)) ) { mprinterr("Error: Defining frame dimension.\n"); return 1; } // Since frame is UNLIMITED, it must be lowest dim. dimensionID[0] = frameDID_; } // Time variable and units if (coordInfo.HasTime()) { if ( checkNCerr( nc_def_var(ncid_, NCTIME, dataType, NDIM-2, dimensionID, &timeVID_)) ) { mprinterr("Error: Defining time variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text(ncid_, timeVID_, "units", 10, "picosecond")) ) { mprinterr("Error: Writing time VID units.\n"); return 1; } } // Spatial dimension and variable if ( checkNCerr( nc_def_dim( ncid_, NCSPATIAL, 3, &spatialDID_)) ) { mprinterr("Error: Defining spatial dimension.\n"); return 1; } dimensionID[0] = spatialDID_; if ( checkNCerr( nc_def_var( ncid_, NCSPATIAL, NC_CHAR, 1, dimensionID, &spatialVID_)) ) { mprinterr("Error: Defining spatial variable.\n"); return 1; } // Atom dimension if ( checkNCerr( nc_def_dim( ncid_, NCATOM, ncatom_, &atomDID_)) ) { mprinterr("Error: Defining atom dimension.\n"); return 1; } // Setup dimensions for Coords/Velocity // NOTE: THIS MUST BE MODIFIED IF NEW TYPES ADDED if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; dimensionID[2] = atomDID_; dimensionID[3] = spatialDID_; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; dimensionID[1] = atomDID_; dimensionID[2] = spatialDID_; } else { dimensionID[0] = atomDID_; dimensionID[1] = spatialDID_; } // Coord variable if ( checkNCerr( nc_def_var( ncid_, NCCOORDS, dataType, NDIM, dimensionID, &coordVID_)) ) { mprinterr("Error: Defining coordinates variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, coordVID_, "units", 8, "angstrom")) ) { mprinterr("Error: Writing coordinates variable units.\n"); return 1; } // Velocity variable if (coordInfo.HasVel()) { if ( checkNCerr( nc_def_var( ncid_, NCVELO, dataType, NDIM, dimensionID, &velocityVID_)) ) { mprinterr("Error: Defining velocities variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, velocityVID_, "units", 19, "angstrom/picosecond")) ) { mprinterr("Error: Writing velocities variable units.\n"); return 1; } if ( checkNCerr( nc_put_att_double( ncid_, velocityVID_, "scale_factor", NC_DOUBLE, 1, &Constants::AMBERTIME_TO_PS)) ) { mprinterr("Error: Writing velocities scale factor.\n"); return 1; } } // Force variable if (coordInfo.HasForce()) { if ( checkNCerr( nc_def_var( ncid_, NCFRC, dataType, NDIM, dimensionID, &frcVID_)) ) { mprinterr("Error: Defining forces variable\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, frcVID_, "units", 25, "kilocalorie/mole/angstrom")) ) { mprinterr("Error: Writing forces variable units.\n"); return 1; } } // Replica Temperature if (coordInfo.HasTemp()) { // NOTE: Setting dimensionID should be OK for Restart, will not be used. dimensionID[0] = frameDID_; if ( NC_defineTemperature( dimensionID, NDIM-2 ) ) return 1; } // Replica indices int remDimTypeVID = -1; if (coordInfo.HasReplicaDims()) { // Define number of replica dimensions remd_dimension_ = coordInfo.ReplicaDimensions().Ndims(); int remDimDID = -1; if ( checkNCerr(nc_def_dim(ncid_, NCREMD_DIMENSION, remd_dimension_, &remDimDID)) ) { mprinterr("Error: Defining replica indices dimension.\n"); return 1; } dimensionID[0] = remDimDID; // For each dimension, store the type if ( checkNCerr(nc_def_var(ncid_, NCREMD_DIMTYPE, NC_INT, 1, dimensionID, &remDimTypeVID)) ) { mprinterr("Error: Defining replica dimension type variable.\n"); return 1; } // Need to store the indices of replica in each dimension each frame // NOTE: THIS MUST BE MODIFIED IF NEW TYPES ADDED if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; dimensionID[2] = remDimDID; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; dimensionID[1] = remDimDID; } else { dimensionID[0] = remDimDID; } if (checkNCerr(nc_def_var(ncid_, NCREMD_INDICES, NC_INT, NDIM-1, dimensionID, &indicesVID_))) { mprinterr("Error: Defining replica indices variable ID.\n"); return 1; } // TODO: Determine if groups are really necessary for restarts. If not, // remove from AmberNetcdf.F90. } // Box Info if (coordInfo.HasBox()) { // Cell Spatial if ( checkNCerr( nc_def_dim( ncid_, NCCELL_SPATIAL, 3, &cell_spatialDID_)) ) { mprinterr("Error: Defining cell spatial dimension.\n"); return 1; } dimensionID[0] = cell_spatialDID_; if ( checkNCerr( nc_def_var(ncid_, NCCELL_SPATIAL, NC_CHAR, 1, dimensionID, &cell_spatialVID_))) { mprinterr("Error: Defining cell spatial variable.\n"); return 1; } // Cell angular if ( checkNCerr( nc_def_dim( ncid_, NCLABEL, NCLABELLEN, &labelDID_)) ) { mprinterr("Error: Defining label dimension.\n"); return 1; } if ( checkNCerr( nc_def_dim( ncid_, NCCELL_ANGULAR, 3, &cell_angularDID_)) ) { mprinterr("Error: Defining cell angular dimension.\n"); return 1; } dimensionID[0] = cell_angularDID_; dimensionID[1] = labelDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_ANGULAR, NC_CHAR, 2, dimensionID, &cell_angularVID_)) ) { mprinterr("Error: Defining cell angular variable.\n"); return 1; } // Setup dimensions for Box // NOTE: This must be modified if more types added int boxdim; if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; boxdim = 2; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; boxdim = 1; } else { boxdim = 0; } dimensionID[boxdim] = cell_spatialDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_LENGTHS, NC_DOUBLE, NDIM-1, dimensionID, &cellLengthVID_)) ) { mprinterr("Error: Defining cell length variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, cellLengthVID_, "units", 8, "angstrom")) ) { mprinterr("Error: Writing cell length variable units.\n"); return 1; } dimensionID[boxdim] = cell_angularDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_ANGLES, NC_DOUBLE, NDIM-1, dimensionID, &cellAngleVID_)) ) { mprinterr("Error: Defining cell angle variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, cellAngleVID_, "units", 6, "degree")) ) { mprinterr("Error: Writing cell angle variable units.\n"); return 1; } } // Attributes if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"title",title.size(),title.c_str())) ) { mprinterr("Error: Writing title.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"application",5,"AMBER")) ) { mprinterr("Error: Writing application.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"program",7,"cpptraj")) ) { mprinterr("Error: Writing program.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"programVersion", NETCDF_VERSION_STRLEN, NETCDF_VERSION_STRING)) ) { mprinterr("Error: Writing program version.\n"); return 1; } // TODO: Make conventions a static string bool errOccurred = false; if ( type == NC_AMBERENSEMBLE ) errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",13,"AMBERENSEMBLE")); else if ( type == NC_AMBERTRAJ ) errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",5,"AMBER")); else errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",12,"AMBERRESTART")); if (errOccurred) { mprinterr("Error: Writing conventions.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"ConventionVersion",3,"1.0")) ) { mprinterr("Error: Writing conventions version.\n"); return 1; } // Set fill mode if (checkNCerr(nc_set_fill(ncid_, NC_NOFILL, dimensionID))) { mprinterr("Error: NetCDF setting fill value.\n"); return 1; } // End netcdf definitions if (checkNCerr(nc_enddef(ncid_))) { mprinterr("NetCDF error on ending definitions."); return 1; } // Specify spatial dimension labels start_[0] = 0; count_[0] = 3; char xyz[3]; xyz[0] = 'x'; xyz[1] = 'y'; xyz[2] = 'z'; if (checkNCerr(nc_put_vara_text(ncid_, spatialVID_, start_, count_, xyz))) { mprinterr("Error on NetCDF output of spatial VID 'x', 'y' and 'z'"); return 1; } if ( coordInfo.HasBox() ) { xyz[0] = 'a'; xyz[1] = 'b'; xyz[2] = 'c'; if (checkNCerr(nc_put_vara_text(ncid_, cell_spatialVID_, start_, count_, xyz))) { mprinterr("Error on NetCDF output of cell spatial VID 'a', 'b' and 'c'"); return 1; } char abc[15] = { 'a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 'a', ' ', 'g', 'a', 'm', 'm', 'a' }; start_[0] = 0; start_[1] = 0; count_[0] = 3; count_[1] = NCLABELLEN; if (checkNCerr(nc_put_vara_text(ncid_, cell_angularVID_, start_, count_, abc))) { mprinterr("Error on NetCDF output of cell angular VID 'alpha', 'beta ' and 'gamma'"); return 1; } } // Store the type of each replica dimension. if (coordInfo.HasReplicaDims()) { ReplicaDimArray const& remdDim = coordInfo.ReplicaDimensions(); start_[0] = 0; count_[0] = remd_dimension_; int* tempDims = new int[ remd_dimension_ ]; for (int i = 0; i < remd_dimension_; ++i) tempDims[i] = remdDim[i]; if (checkNCerr(nc_put_vara_int(ncid_, remDimTypeVID, start_, count_, tempDims))) { mprinterr("Error: writing replica dimension types.\n"); delete[] tempDims; return 1; } delete[] tempDims; } return 0; }
int main(int argc,char *argv[]) { struct DataMap *ptr; struct DataMapScalar *sx,*sy; struct DataMapArray *ax,*ay; size_t index[256]; size_t start[256]; size_t count[256]; int s; unsigned char vbflg=0; unsigned char help=0; unsigned char option=0; unsigned char zflg=0; FILE *fp=NULL; gzFile zfp=0; FILE *mapfp; int n,c,x; int ncid; int block=0; int varid; int strsze; char **strptr; char *tmpbuf=NULL; OptionAdd(&opt,"-help",'x',&help); OptionAdd(&opt,"-option",'x',&option); OptionAdd(&opt,"vb",'x',&vbflg); OptionAdd(&opt,"z",'x',&zflg); if (argc>1) { arg=OptionProcess(1,argc,argv,&opt,NULL); if (help==1) { OptionPrintInfo(stdout,hlpstr); exit(0); } if (option==1) { OptionDump(stdout,&opt); exit(0); } if (zflg) { zfp=gzopen(argv[arg],"r"); if (zfp==0) { fprintf(stderr,"File not found.\n"); exit(-1); } } else { fp=fopen(argv[arg],"r"); if (fp==NULL) { fprintf(stderr,"File not found.\n"); exit(-1); } } } else { OptionPrintInfo(stdout,errstr); exit(-1); } /* load the map */ mapfp=fopen(argv[arg+1],"r"); loadmap(mapfp); fclose(mapfp); s=nc_open(argv[arg+2],NC_WRITE,&ncid); if (s !=NC_NOERR) { fprintf(stderr,"Error opening CDF file.\n"); exit(-1); } block=0; while (1) { if (zflg) ptr=DataMapReadZ(zfp); else ptr=DataMapFread(fp); if (ptr==NULL) break; for (c=0;c<ptr->snum;c++) { sx=ptr->scl[c]; for (n=0;n<snum;n++) { sy=sptr[n]; if (strcmp(sx->name,sy->name) !=0) continue; if (sx->type !=sy->type) continue; break; } if (n !=snum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfsname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } index[0]=block; switch (sx->type) { case DATACHAR: s=nc_put_var1_text(ncid,varid,index,sx->data.cptr); break; case DATASHORT: s=nc_put_var1_short(ncid,varid,index,sx->data.sptr); break; case DATAINT: s=nc_put_var1_int(ncid,varid,index,sx->data.iptr); break; case DATAFLOAT: s=nc_put_var1_float(ncid,varid,index,sx->data.fptr); break; case DATADOUBLE: s=nc_put_var1_double(ncid,varid,index,sx->data.dptr); break; case DATASTRING: start[0]=block; start[1]=0; count[0]=1; count[1]=strlen(*((char **) sx->data.vptr))+1; s=nc_put_vara_text(ncid,varid,start,count, *((char **) sx->data.vptr)); break; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } for (c=0;c<ptr->anum;c++) { ax=ptr->arr[c]; for (n=0;n<anum;n++) { ay=aptr[n]; if (strcmp(ax->name,ay->name) !=0) continue; if (ax->type !=ay->type) continue; if (ax->dim !=ay->dim) continue; break; } if (n !=anum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfaname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } start[0]=block; count[0]=1; n=1; for (x=0;x<ax->dim;x++) { start[1+x]=0; count[1+x]=ax->rng[x]; n=n*ax->rng[x]; } if (ax->type==DATASTRING) { int ndims; int dimids[NC_MAX_VAR_DIMS]; size_t dimlen; s=nc_inq_varndims(ncid,varid,&ndims); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } s=nc_inq_vardimid(ncid,varid,dimids); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } if (ndims-2!=ax->dim) { fprintf(stderr,"Error matching dimensions.\n"); exit(-1); } s=nc_inq_dimlen(ncid,dimids[ndims-1],&dimlen); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } strsze=dimlen; tmpbuf=malloc(n*strsze); if (tmpbuf==NULL) { fprintf(stderr,"Failed to allocate buffer.\n"); exit(-1); } memset(tmpbuf,0,n*strsze); start[1+ax->dim]=0; count[1+ax->dim]=strsze; strptr=(char **) ax->data.vptr; for (x=0;x<n;x++) strncpy(tmpbuf+x*strsze,strptr[x],strsze); } switch (ax->type) { case DATACHAR: s=nc_put_vara_text(ncid,varid,start,count,ax->data.cptr); break; case DATASHORT: s=nc_put_vara_short(ncid,varid,start,count,ax->data.sptr); break; case DATAINT: s=nc_put_vara_int(ncid,varid,start,count,ax->data.iptr); break; case DATAFLOAT: s=nc_put_vara_float(ncid,varid,start,count,ax->data.fptr); break; case DATADOUBLE: s=nc_put_vara_double(ncid,varid,start,count,ax->data.dptr); break; case DATASTRING: s=nc_put_vara_text(ncid,varid,start,count,tmpbuf); break; } if (tmpbuf !=NULL) { free(tmpbuf); tmpbuf=NULL; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } DataMapFree(ptr); block++; } nc_close(ncid); if (zflg) gzclose(zfp); else fclose(fp); return 0; }
/*! * writes the attribute names for a block * \param exoid exodus file id * \param blk_type block type (edge, face, elem) * \param blk_id block id * \param names ptr to array of attribute names */ int ex_put_attr_names(int exoid, ex_entity_type blk_type, int blk_id, char* names[]) { int status; int varid, numattrdim, blk_id_ndx; size_t num_attr, start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; size_t i; exerrval = 0; /* clear error code */ blk_id_ndx = ex_id_lkup(exoid, blk_type, blk_id); /* Determine index of blk_id in blk_id_ndx array */ if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: no attributes allowed for NULL %s %d in file id %d", ex_name_of_object(blk_type),blk_id,exoid); ex_err("ex_put_attr_names",errmsg,EX_MSG); return (EX_WARN); /* no attributes for this block */ } else { sprintf(errmsg, "Error: no %s id %d in %s array in file id %d", ex_name_of_object(blk_type), blk_id, VAR_ID_EL_BLK, exoid); ex_err("ex_put_attr_names",errmsg,exerrval); return (EX_FATAL); } } /* inquire id's of previously defined dimensions */ switch (blk_type) { case EX_SIDE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_SS(blk_id_ndx), &numattrdim); break; case EX_NODE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NS(blk_id_ndx), &numattrdim); break; case EX_EDGE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ES(blk_id_ndx), &numattrdim); break; case EX_FACE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FS(blk_id_ndx), &numattrdim); break; case EX_ELEM_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ELS(blk_id_ndx), &numattrdim); break; case EX_NODAL: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NBLK, &numattrdim); break; case EX_EDGE_BLOCK: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_EBLK(blk_id_ndx), &numattrdim); break; case EX_FACE_BLOCK: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FBLK(blk_id_ndx), &numattrdim); break; case EX_ELEM_BLOCK: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_BLK(blk_id_ndx), &numattrdim); break; default: exerrval = 1005; sprintf(errmsg, "Internal Error: unrecognized object type in switch: %d in file id %d", blk_type,exoid); ex_err("ex_put_attr_names",errmsg,EX_MSG); return (EX_FATAL); /* number of attributes not defined */ } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: number of attributes not defined for %s %d in file id %d", ex_name_of_object(blk_type),blk_id,exoid); ex_err("ex_put_attr_names",errmsg,EX_MSG); return (EX_FATAL); /* number of attributes not defined */ } if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of attributes for %s %d in file id %d", ex_name_of_object(blk_type),blk_id,exoid); ex_err("ex_put_attr_names",errmsg,exerrval); return (EX_FATAL); } switch (blk_type) { case EX_SIDE_SET: status = nc_inq_varid (exoid, VAR_NAME_SSATTRIB(blk_id_ndx), &varid); break; case EX_NODE_SET: status = nc_inq_varid (exoid, VAR_NAME_NSATTRIB(blk_id_ndx), &varid); break; case EX_EDGE_SET: status = nc_inq_varid (exoid, VAR_NAME_ESATTRIB(blk_id_ndx), &varid); break; case EX_FACE_SET: status = nc_inq_varid (exoid, VAR_NAME_FSATTRIB(blk_id_ndx), &varid); break; case EX_ELEM_SET: status = nc_inq_varid (exoid, VAR_NAME_ELSATTRIB(blk_id_ndx), &varid); break; case EX_NODAL: status = nc_inq_varid (exoid, VAR_NAME_NATTRIB, &varid); break; case EX_EDGE_BLOCK: status = nc_inq_varid (exoid, VAR_NAME_EATTRIB(blk_id_ndx), &varid); break; case EX_FACE_BLOCK: status = nc_inq_varid (exoid, VAR_NAME_FATTRIB(blk_id_ndx), &varid); break; case EX_ELEM_BLOCK: status = nc_inq_varid (exoid, VAR_NAME_ATTRIB(blk_id_ndx), &varid); break; default: exerrval = 1005; sprintf(errmsg, "Internal Error: unrecognized object type in switch: %d in file id %d", blk_type,exoid); ex_err("ex_put_attr_names",errmsg,EX_MSG); return (EX_FATAL); /* number of attributes not defined */ } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate %s attribute names for %s %d in file id %d", ex_name_of_object(blk_type),ex_name_of_object(blk_type),blk_id, exoid); ex_err("ex_put_attr_names",errmsg,exerrval); return (EX_FATAL); } /* write out the attributes */ for (i = 0; i < num_attr; i++) { start[0] = i; start[1] = 0; count[0] = 1; count[1] = strlen(names[i])+1; if ((status = nc_put_vara_text(exoid, varid, start, count, (void*) names[i])) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to put attribute namess for %s %d in file id %d", ex_name_of_object(blk_type),blk_id,exoid); ex_err("ex_put_attr_names",errmsg,exerrval); return (EX_FATAL); } } return(EX_NOERR); }