/* Add any or all of these four attributes to a file or variable. */ int nccf_def_notes(int ncid, int varid, const char *institution, const char *source, const char *comment, const char *references) { int ret; if (institution) if ((ret = nc_put_att_text(ncid, varid, CF_INSTITUTION, strlen(institution) + 1, institution))) return ret; if (source) if ((ret = nc_put_att_text(ncid, varid, CF_SOURCE, strlen(source) + 1, source))) return ret; if (comment) if ((ret = nc_put_att_text(ncid, varid, CF_COMMENT, strlen(comment) + 1, comment))) return ret; if (references) if ((ret = nc_put_att_text(ncid, varid, CF_REFERENCES, strlen(references) + 1, references))) return ret; return CF_NOERR; }
// Define a function for initializing NetCDF void NcInit() { // Create the NetCDF dataset TryNc(nc_create(FILENAME, NC_CLOBBER, &NcId)); // Declare dimension IDs for each independent variable int dimIds[INDEPENDENT_VAR_COUNT]; // Define NetCDF dimensions for each independent variable TryNc(nc_def_dim(NcId, TIME_NAME, TIME_COUNT, &(dimIds[0]))); TryNc(nc_def_dim(NcId, Y_NAME, ENVIRONMENT_LENGTH, &(dimIds[1]))); TryNc(nc_def_dim(NcId, X_NAME, ENVIRONMENT_LENGTH, &(dimIds[2]))); // Define NetCDF variables for each variable TryNc(nc_def_var(NcId, TIME_NAME, NC_INT, 1, &(dimIds[0]), &TimeVarId)); TryNc(nc_def_var(NcId, Y_NAME, NC_INT, 1, &(dimIds[1]), &YVarId)); TryNc(nc_def_var(NcId, X_NAME, NC_INT, 1, &(dimIds[2]), &XVarId)); TryNc(nc_def_var(NcId, STATE_NAME, NC_INT, INDEPENDENT_VAR_COUNT, dimIds, &StateVarId)); // Define NetCDF units text for each variable TryNc(nc_put_att_text(NcId, TimeVarId, "units", strlen(TIME_UNITS), TIME_UNITS)); TryNc(nc_put_att_text(NcId, YVarId, "units", strlen(Y_UNITS), Y_UNITS)); TryNc(nc_put_att_text(NcId, XVarId, "units", strlen(X_UNITS), X_UNITS)); // Leave NetCDF define mode TryNc(nc_enddef(NcId)); // Write the values for the each dimension PutDimVals(TimeVarId, TIME_COUNT); PutDimVals(YVarId, ENVIRONMENT_LENGTH); PutDimVals(XVarId, ENVIRONMENT_LENGTH); }
/* Define a transform. This will create a scalar var of type NC_CHAR * to hold some attributes. */ int nccf_def_transform(int ncid, const char *name, const char *transform_type, const char *transform_name, int *transform_varid) { int varid; int ret; /* Create a scalar var, type NC_CHAR, to hold the attribute that * defines this transform. */ if ((ret = nc_def_var(ncid, name, NC_CHAR, 0, NULL, &varid))) return ret; /* If the user wants it, return the varid of this system. */ if (transform_varid) *transform_varid = varid; /* Store the transform_type, if provided by the user. */ if (transform_type) if ((ret = nc_put_att_text(ncid, varid, TRANSFORM_TYPE, strlen(transform_type) + 1, transform_type))) return ret; /* Store the transform name, if provided by the user. */ if (transform_type) if ((ret = nc_put_att_text(ncid, varid, TRANSFORM_NAME, strlen(transform_name) + 1, transform_name))) return ret; return NC_NOERR; }
/* Append string to a named global attribute. Create the attribute if * it doesn't exist. */ static int nccf_append_att(int ncid, const char *name, const char *string) { char *att_str = NULL; size_t len, new_len; int ret; /* Find out if there is an attribute of this name. */ ret = nc_inq_attlen(ncid, NC_GLOBAL, name, &len); if (ret == NC_ENOTATT) { /* Create the attribute. I will null-terminate this * attribute. */ if ((ret = nc_put_att_text(ncid, NC_GLOBAL, name, strlen(string) + 1, string))) return ret; } else if (ret == NC_NOERR) { /* The attribute already exists. Get memory to hold the existing * att plus our version string. Add one for the space, and one * for a terminating null. */ new_len = len + strlen(string) + 1; if (!(att_str = malloc(new_len + 1))) return CF_ENOMEM; /* Get the existing attribute value. */ if ((ret = nc_get_att_text(ncid, NC_GLOBAL, name, att_str))) BAIL(CF_ENETCDF); /* If it's already in the string, our work is done.*/ if (strstr(att_str, string)) { free(att_str); return CF_NOERR; } /* Append our string to the existing att. */ att_str[len] = 0; strcat(att_str, " "); strcat(att_str, string); /* Delete the existing attribute, so we can rewrite it. */ if ((ret = nc_del_att(ncid, NC_GLOBAL, name))) BAIL(ret); /* Rewrite the attribute with our string appended. */ if ((ret = nc_put_att_text(ncid, NC_GLOBAL, name, strlen(att_str) + 1, att_str))) BAIL(ret); } exit: if (att_str) free(att_str); return ret; }
static NCerror buildattribute3a(NCDAPCOMMON* dapcomm, NCattribute* att, nc_type vartype, int varid) { int i; NCerror ncstat = NC_NOERR; unsigned int nvalues = nclistlength(att->values); NC* drno = dapcomm->controller; /* If the type of the attribute is string, then we need*/ /* to convert to a single character string by concatenation. modified: 10/23/09 to insert newlines. modified: 10/28/09 to interpret escapes */ if(att->etype == NC_STRING || att->etype == NC_URL) { char* newstring; size_t newlen = 0; for(i=0;i<nvalues;i++) { char* s = (char*)nclistget(att->values,i); newlen += (1+strlen(s)); } newstring = (char*)malloc(newlen); MEMCHECK(newstring,NC_ENOMEM); newstring[0] = '\0'; for(i=0;i<nvalues;i++) { char* s = (char*)nclistget(att->values,i); if(i > 0) strcat(newstring,"\n"); strcat(newstring,s); } dapexpandescapes(newstring); if(newstring[0]=='\0') ncstat = nc_put_att_text(drno->substrate,varid,att->name,1,newstring); else ncstat = nc_put_att_text(drno->substrate,varid,att->name,strlen(newstring),newstring); free(newstring); } else { nc_type atype; unsigned int typesize; void* mem; /* It turns out that some servers upgrade the type of _FillValue in order to correctly preserve the original value. However, since the type of the underlying variable is not changes, we get a type mismatch. So, make sure the type of the fillvalue is the same as that of the controlling variable. */ if(varid != NC_GLOBAL && strcmp(att->name,"_FillValue")==0) atype = nctypeconvert(dapcomm,vartype); else atype = nctypeconvert(dapcomm,att->etype); typesize = nctypesizeof(atype); mem = malloc(typesize * nvalues); ncstat = dapcvtattrval3(atype,mem,att->values); ncstat = nc_put_att(drno->substrate,varid,att->name,atype,nvalues,mem); nullfree(mem); } return THROW(ncstat); }
int nccf_def_var(int ncid, int varid, const char *units, const char *long_name, const char *standard_name, int ncoord_vars, int *coord_varids) { char coord_name[NC_MAX_NAME + 1]; char coords_str[CF_MAX_COORD_LEN + 1]; int c, ret; /* Write units. */ if (units) if ((ret = nc_put_att_text(ncid, varid, CF_UNITS, strlen(units) + 1, units))) return ret; /* Write long_name. */ if (long_name) if ((ret = nc_put_att_text(ncid, varid, CF_LONG_NAME, strlen(long_name) + 1, long_name))) return ret; /* Write standard_name. */ if (standard_name) if ((ret = nc_put_att_text(ncid, varid, CF_STANDARD_NAME, strlen(standard_name) + 1, standard_name))) return ret; /* Now add the "coordinates" atttribute, which holds a space * separated list of auxilary coordinate variables. */ if (ncoord_vars) { if (ncoord_vars > CF_MAX_COORDS) return CF_EMAXCOORDS; if (!coord_varids) return CF_EINVAL; strcpy(coords_str, ""); for (c = 0; c < ncoord_vars; c++) { /* Find the names of this coordinate var. */ if ((ret = nc_inq_varname(ncid, coord_varids[c], coord_name))) return ret; strcat(coords_str, coord_name); strcat(coords_str, " "); } if ((ret = nc_put_att_text(ncid, varid, CF_COORDINATES, strlen(coords_str) + 1, coords_str))) return ret; } return NC_NOERR; }
int main(int argc, char **argv) { int ncid, dimid, varid; int var_dims[VAR_RANK]; int g1id, g2id, g3id; float vals[] = {1.0, 2.0, 3.0, 4.0}; printf("\n*** Testing groups.\n"); printf("*** creating nested group file %s...", FILE_NAME); /* Create a file with nested groups. */ if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR; /* At root level define dim, var, atts */ if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(ncid, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(ncid, varid, ATT_NAME, strlen(ATT_VAL), ATT_VAL)) ERR; if (nc_put_att_text(ncid, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL), GATT_VAL)) ERR; if (nc_enddef(ncid)) ERR; if (nc_put_var_float(ncid, varid, vals)) ERR; /* put dim, var, atts with same names in a group */ if (nc_def_grp(ncid, G1_NAME, &g1id)) ERR; if (nc_def_dim(g1id, DIM_NAME, DIM_LEN1, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g1id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g1id, varid, ATT_NAME, strlen(ATT_VAL1), ATT_VAL1)) ERR; if (nc_put_att_text(g1id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL1), GATT_VAL1)) ERR; if (nc_enddef(g1id)) ERR; if (nc_put_var_float(g1id, varid, vals)) ERR; /* put dim, var, atts with same names in a second group */ if (nc_def_grp(ncid, G2_NAME, &g2id)) ERR; if (nc_def_dim(g2id, DIM_NAME, DIM_LEN2, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g2id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g2id, varid, ATT_NAME, strlen(ATT_VAL2), ATT_VAL2)) ERR; if (nc_put_att_text(g2id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL2), GATT_VAL2)) ERR; if (nc_enddef(g2id)) ERR; if (nc_put_var_float(g2id, varid, vals)) ERR; /* put dim, var, atts with same names in a subgroup of second group */ if (nc_def_grp(g2id, G3_NAME, &g3id)) ERR; if (nc_def_dim(g3id, DIM_NAME, DIM_LEN3, &dimid)) ERR; var_dims[0] = dimid; if (nc_def_var(g3id, VAR_NAME, NC_FLOAT, VAR_RANK, var_dims, &varid)) ERR; if (nc_put_att_text(g3id, varid, ATT_NAME, strlen(ATT_VAL3), ATT_VAL3)) ERR; if (nc_put_att_text(g3id, NC_GLOBAL, GATT_NAME, strlen(GATT_VAL3), GATT_VAL3)) ERR; if (nc_enddef(g3id)) ERR; if (nc_put_var_float(g3id, varid, vals)) ERR; if (nc_close(ncid)) ERR; SUMMARIZE_ERR; FINAL_RESULTS; }
/* 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; }
NCerror showprojection3(NCDAPCOMMON* dapcomm, CDFnode* var) { int i,rank; NCerror ncstat = NC_NOERR; NCbytes* projection = ncbytesnew(); NClist* path = nclistnew(); NC* drno = dapcomm->controller; /* Collect the set of DDS node name forming the xpath */ collectnodepath3(var,path,WITHOUTDATASET); for(i=0;i<nclistlength(path);i++) { CDFnode* node = (CDFnode*)nclistget(path,i); if(i > 0) ncbytescat(projection,"."); ncbytescat(projection,node->ocname); } /* Now, add the dimension info */ rank = nclistlength(var->array.dimset0); for(i=0;i<rank;i++) { CDFnode* dim = (CDFnode*)nclistget(var->array.dimset0,i); char tmp[32]; ncbytescat(projection,"["); snprintf(tmp,sizeof(tmp),"%lu",(unsigned long)dim->dim.declsize); ncbytescat(projection,tmp); ncbytescat(projection,"]"); } /* Define the attribute */ ncstat = nc_put_att_text(getncid(drno),var->ncid, "_projection", ncbyteslength(projection), ncbytescontents(projection)); return ncstat; }
/* * Add or change a character attribute of an open netCDF file. */ static void c_ncaptc( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ nc_type datatype, /* attribute datatype */ size_t attlen, /* attribute length */ const char* value, /* pointer to data values */ int* rcode /* returned error code */ ) { int status; if (datatype != NC_CHAR) status = NC_ECHAR; else status = nc_put_att_text(ncid, varid, attname, attlen, value); if (status == 0) *rcode = 0; else { nc_advise("NCAPTC", status, ""); *rcode = ncerr; } }
void write_UV10 () { int nc_id; int dim_ids[2]; int u10_id; int v10_id; static char title[] = "example netCDF dataset"; nc_error(nc_create("out/wind/uv10.vec.nc", 0, &nc_id)); nc_error(nc_def_dim(nc_id, "south_north", nSN, &dim_ids[0])); nc_error(nc_def_dim(nc_id, "west_east", nWE, &dim_ids[1])); nc_error(nc_def_var (nc_id, "u10", NC_FLOAT, 2, dim_ids, &u10_id)); nc_error(nc_def_var (nc_id, "v10", NC_FLOAT, 2, dim_ids, &v10_id)); nc_error(nc_put_att_text (nc_id, NC_GLOBAL, "title", strlen(title), title)); nc_error(nc_enddef(nc_id)); nc_error(nc_put_var_float(nc_id, u10_id, U10)); nc_error(nc_put_var_float(nc_id, v10_id, V10)); nc_error(nc_close(nc_id)); }
/* Define a coordinate system. This creates a var with contains a text * attribute, which is the names of the (coordinate) vars which make * up the axes of a coordinate system. */ int nccf_def_coord_system(int ncid, const char *name, int naxes, int *axis_varids, int *system_varid) { int dimid, a; int varid; char var_name[NC_MAX_NAME + 1]; char *coord_axes_str; int ret; /* naxes must be positive and less than max_dims. */ if (naxes < 0) return NC_EINVAL; if (naxes > NC_MAX_DIMS) return NC_EMAXDIMS; /* Create a scalar var, type NC_CHAR, to hold the attribute that * defines this system. */ if ((ret = nc_def_var(ncid, name, NC_CHAR, 0, NULL, &varid))) return ret; /* If the user wants it, return the varid of this system. */ if (system_varid) *system_varid = varid; /* malloc space for the string which will hold the names of all the * axes. */ if (!(coord_axes_str = malloc(naxes * NC_MAX_NAME + naxes))) return NC_NOERR; coord_axes_str[0] = 0; /* Store the names of the axes in an attribute. At the same time, * make sure that all the axis_varids are actually coordinate * variables - that is, there is a dimension withn the same * name. */ for (a = 0; a < naxes; a++) { if ((ret = nc_inq_varname(ncid, axis_varids[a], var_name))) break; if ((ret = nc_inq_dimid(ncid, var_name, &dimid))) break; strcat(coord_axes_str, var_name); if (a < naxes - 1) strcat(coord_axes_str, " "); } /* If we arrived here without error, then write the coord_axes_str * to the appropriate attribute, to save the system in the file. */ if (!ret) ret = nc_put_att_text(ncid, varid, COORDINATE_AXES, strlen(coord_axes_str) + 1, coord_axes_str); /* Free the memory we allocated. */ free(coord_axes_str); return ret; }
NcBool NcVar::add_att(NcToken aname, const char* val) { if (! the_file->define_mode()) return FALSE; if (nc_put_att_text(the_file->id(), the_id, aname, strlen(val), val) != NC_NOERR) return FALSE; return TRUE; }
static int ex_write_object_params(int exoid, const char *type, const char *dimension_name, const char *status_dim_name, const char *id_array_dim_name, size_t count, int *dimension) { int dim[2]; int varid; int status; int int_type; char errmsg[MAX_ERR_LENGTH]; /* Can have nonzero model->num_elem_blk even if model->num_elem == 0 */ if (count > 0) { if ((status = nc_def_dim(exoid, dimension_name, count, dimension)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of %ss in file id %d", type, exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; /* exit define mode and return */ } /* ...and some variables */ /* element block id status array */ dim[0] = *dimension; if ((status = nc_def_var (exoid, status_dim_name, NC_INT, 1, dim, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define %s status array in file id %d", type, exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; /* exit define mode and return */ } /* type id array */ int_type = NC_INT; if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { int_type = NC_INT64; } if ((status = nc_def_var (exoid, id_array_dim_name, int_type, 1, dim, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define %s id array in file id %d", type, exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; /* exit define mode and return */ } /* store property name as attribute of property array variable */ if ((status=nc_put_att_text(exoid, varid, ATT_PROP_NAME, 3, "ID")) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s property name %s in file id %d", type, "ID", exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; } } return NC_NOERR; }
// NetcdfFile::NC_defineTemperature() int NetcdfFile::NC_defineTemperature(int* dimensionID, int NDIM) { if (checkNCerr(nc_def_var(ncid_,NCTEMPERATURE,NC_DOUBLE,NDIM,dimensionID,&TempVID_))) { mprinterr("NetCDF error on defining temperature.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,TempVID_,"units",6,"kelvin"))) { mprinterr("NetCDF error on defining temperature units.\n"); return 1; } return 0; }
NcBool NcVar::add_att(NcToken aname, int len, const char* vals) { if (! the_file->define_mode()) return FALSE; if (NcError::set_err( nc_put_att_text (the_file->id(), the_id, aname, len, vals) ) != NC_NOERR) return FALSE; return TRUE; }
/* Here are functions for coordinate axis stuff. */ int nccf_def_axis_type(int ncid, int varid, int axis_type) { int dimid; char var_name[NC_MAX_NAME + 1]; int ret; /* Is the axis type valid? */ if (axis_type < 0 || axis_type > NCCF_RADDIST) return NC_EINVAL; /* Make sure there's a dimension with the same name in this * ncid. */ if ((ret = nc_inq_varname(ncid, varid, var_name))) return ret; if ((ret = nc_inq_dimid(ncid, var_name, &dimid))) return ret; /* Now write the attribute which stores the axis type. */ if ((ret = nc_put_att_text(ncid, varid, COORDINATE_AXIS_TYPE, strlen(axis_type_name[axis_type]) + 1, axis_type_name[axis_type]))) return ret; /* For height we write another attribute, indicating up * vs. down. */ if (axis_type == NCCF_HEIGHT_UP) { if ((ret = nc_put_att_text(ncid, varid, COORDINATE_Z_IS_POSITIVE, strlen(Z_UP) + 1, Z_UP))) return ret; } else if (axis_type == NCCF_HEIGHT_DOWN) { if ((ret = nc_put_att_text(ncid, varid, COORDINATE_Z_IS_POSITIVE, strlen(Z_UP) + 1, Z_UP))) return ret; } return NC_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; }
void netcdfPutAttributeText( int ncid, int vid, char *attribute, char *text ) { int err; err = nc_put_att_text(ncid, vid, attribute, strlen(text), text); if (err != NC_NOERR) { fprintf(stdout, "netcdfPutAttributeText: Error putting attribute (%s): %s\n", attribute, nc_strerror(err)); } }
static void genbin_defineglobalspecials(void) { int stat = NC_NOERR; const char* format = NULL; if(usingclassic) return; if(!/*Main.*/format_attribute) return; /* Watch out, this is a global Attribute */ format = kind_string(/*Main.*/format_flag); stat = nc_put_att_text(rootgroup->ncid,NC_GLOBAL,"_Format",strlen(format),format); check_err(stat,__LINE__,__FILE__); }
static void createtestvars(int id, const struct tcdfvar *testvars, size_t count) { int ii; int varid; const struct tcdfvar *vp = testvars; for(ii = 0; (size_t) ii < count; ii++, vp++ ) { assert(nc_def_var(id, vp->mnem, vp->type, vp->ndims, vp->dims, &varid) == NC_NOERR ); assert( nc_put_att_text(id,ii,reqattr[0],strlen(vp->units), vp->units) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[1],NC_DOUBLE,1, &vp->validmin) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[2],NC_DOUBLE,1, &vp->validmax) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[3],NC_DOUBLE,1, &vp->scalemin) == NC_NOERR); assert( nc_put_att_double(id,ii,reqattr[4],NC_DOUBLE,1, &vp->scalemax) == NC_NOERR); assert( nc_put_att_text(id,ii,reqattr[5],strlen(vp->fieldnam), vp->fieldnam) == NC_NOERR); } }
void sdatio_add_metadata(struct sdatio_file * sfile, const int metadata_type, const char * key, const void * value){ int retval; sdatio_recommence_definitions(sfile); switch (metadata_type){ case SDATIO_CHAR: if ((retval = nc_put_att_text(sfile->nc_file_id, NC_GLOBAL, key, strlen(value), value))) ERR(retval); break; default: if ((retval = nc_put_att(sfile->nc_file_id, NC_GLOBAL, key, sdatio_netcdf_variable_type(metadata_type), 1, value))) ERR(retval); } sdatio_end_definitions(sfile); }
static int write_string_attribute(int ncid, int varid, const char *name, const char *data) { int result; result = nc_put_att_text(ncid, varid, name, strlen(data), data); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } return 0; }
void R_nc_put_att_text( int *ncid, int *varid, char **attname, int *type_to_create, int *natts, char **attribute, int *retval ) { size_t attlen; R_nc_ttc_to_nctype( *type_to_create ); /* For some reason the C interface does not include the nc_type for this call */ attlen = strlen(attribute[0]); *retval = nc_put_att_text(*ncid, *varid, attname[0], attlen, attribute[0] ); if( *retval != NC_NOERR ) REprintf( "Error in R_nc_put_att_text: %s\n", nc_strerror(*retval) ); }
NCstate NCdataSetTextAttribute(int ncid, int varid, const char *attName, const char *text) { int status; bool redef; redef = nc_redef(ncid) == NC_NOERR ? true : false; status = nc_put_att_text(ncid, varid, attName, strlen(text) + 1, text); if (redef) nc_enddef(ncid); if (status != NC_NOERR) { NCprintNCError (status, "NCdataSetTextAttribute"); return (NCfailed); } return (NCsucceeded); }
int ncd_attr_str_ds (int ncid, char *path, char *name, void *val) { int valid,retval; char *text=(char *)val; retval=nc_redef(ncid); retval=nc_inq_varid(ncid,path,&valid); //printf("varname=%s, valid=%d\n",path,valid); if(valid>0) { retval=nc_put_att_text(ncid,valid,name,strlen(text),text); ERR(retval); printf("varname=%s, valid=%d text=%s len=%d\n",path,valid,text,strlen(text)); } retval=nc_enddef(ncid); return 0; }
int put_nc_global_attr(int ncid, int dim_latlon, int grid_template) { char *str; str=""; if (dim_latlon == 1) str = "COARDS"; else if (dim_latlon == 2) str = "CF-1.0"; else fatal_error("netcdf:create_nc_dims: %s","unsupported lat-lon dimension"); netcdf_func( nc_put_att_text(ncid, NC_GLOBAL, "Conventions", strlen(str), str) ); netcdf_func( nc_put_att_int (ncid, NC_GLOBAL, "GRIB2_grid_template", NC_INT, 1, &grid_template) ); }
static int ex_write_map_params(int exoid, const char *map_name, const char *map_dim_name, const char *map_id_name, size_t map_count, int *map_dimension) { int dim[2]; int varid; int status; char errmsg[MAX_ERR_LENGTH]; int int_type = NC_INT; if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { int_type = NC_INT64; } /* Can have nonzero model->num_XXXX_map even if model->num_XXXX == 0 */ if ((map_count) > 0) { if ((status = nc_def_dim(exoid, map_dim_name, map_count, map_dimension)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define number of %ss in file id %d", map_name, exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; /* exit define mode and return */ } dim[0] = *map_dimension; /* map_name id array */ if ((status = nc_def_var(exoid, map_id_name, int_type, 1, dim, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define %s id array in file id %d", map_name, exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return status; /* exit define mode and return */ } /* store property name as attribute of property array variable */ if ((status=nc_put_att_text(exoid, varid, ATT_PROP_NAME, 3, "ID")) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store %s property name %s in file id %d", map_name, "ID",exoid); ex_err("ex_put_init_ext",errmsg,exerrval); return (EX_FATAL); } } return NC_NOERR; }
void write_UV10_pol () { int nc_id; int dim_ids[2]; int dir10_id; int speed10_id; static char title[] = "example netCDF dataset"; float * dir10 = malloc (nWE * nSN * sizeof(float)); if (dir10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate DIR10\n"); exit(-1);} float * speed10 = malloc (nWE * nSN * sizeof(float)); if (speed10==NULL) {fprintf(stderr, "uv10.c : Cannot allocate SPEED10\n"); exit(-1);} int i; int nvals = nWE * nSN; for (i=0; i<nvals; i++) { dir10[i] = fmod( (atan2(V10[i], U10[i]) * 180 / M_PI + 360) , 360); speed10[i] = sqrt( pow(U10[i],2) + pow(V10[i],2) ); } nc_error(nc_create("out/wind/uv10.pol.nc", 0, &nc_id)); nc_error(nc_def_dim(nc_id, "south_north", nSN, &dim_ids[0])); nc_error(nc_def_dim(nc_id, "west_east", nWE, &dim_ids[1])); nc_error(nc_def_var (nc_id, "dir10", NC_FLOAT, 2, dim_ids, &dir10_id)); nc_error(nc_def_var (nc_id, "speed10", NC_FLOAT, 2, dim_ids, &speed10_id)); nc_error(nc_put_att_text (nc_id, NC_GLOBAL, "title", strlen(title), title)); nc_error(nc_enddef(nc_id)); nc_error(nc_put_var_float(nc_id, dir10_id, dir10)); nc_error(nc_put_var_float(nc_id, speed10_id, speed10)); free (dir10); free (speed10); nc_error(nc_close(nc_id)); }
/********************************************************************* void mpp_def_global_att(int fid, const char *name, const char *val) write out global attribute ********************************************************************/ void mpp_def_global_att(int fid, const char *name, const char *val) { size_t status; char errmsg[512]; if( mpp_pe() != mpp_root_pe() ) return; if(fid<0 || fid >=nfiles) mpp_error("mpp_io(mpp_def_global_att): invalid fid number, fid should be " "a nonnegative integer that less than nfiles"); status = nc_put_att_text(files[fid].ncid, NC_GLOBAL, name, strlen(val), val); if(status != NC_NOERR) { sprintf(errmsg, "mpp_io(mpp_def_global_att): Error in put glboal attribute %s of file %s", name, files[fid].name ); netcdf_error(errmsg, status); } }; /* mpp_def_global_att */