int ncvarrename( int ncid, int varid, const char* name ) { const int status = nc_rename_var(ncid, varid, name); if(status != NC_NOERR) { nc_advise("ncvarrename", status, "ncid %d", ncid); return -1; } return varid; }
int ncdimrename( int ncid, int dimid, const char* name ) { const int status = nc_rename_dim(ncid, dimid, name); if(status != NC_NOERR) { nc_advise("ncdimrename", status, "ncid %d", ncid); return -1; } return dimid; }
int ncvarid( int ncid, const char* name ) { int varid = -1; const int status = nc_inq_varid(ncid, name, &varid); if(status != NC_NOERR) { nc_advise("ncvarid", status, "ncid %d", ncid); return -1; } return varid; }
int ncsetfill( int ncid, int fillmode ) { int oldmode = -1; const int status = nc_set_fill(ncid, fillmode, &oldmode); if(status != NC_NOERR) { nc_advise("ncsetfill", status, "ncid %d", ncid); return -1; } return oldmode; }
int ncrecput( int ncid, long recnum, void* const* datap ) { const int status = nc_put_rec(ncid, (size_t)recnum, datap); if(status != NC_NOERR) { nc_advise("ncrecput", status, "ncid %d", ncid); return -1; } return 0; }
int ncattdel( int ncid, int varid, const char* name ) { const int status = nc_del_att(ncid, varid, name); if(status != NC_NOERR) { nc_advise("ncattdel", status, "ncid %d", ncid); return -1; } return 1; }
int ncdimdef( int ncid, const char* name, long length ) { int dimid; const int status = nc_def_dim(ncid, name, (size_t)length, &dimid); if(status != NC_NOERR) { nc_advise("ncdimdef", status, "ncid %d", ncid); return -1; } return dimid; }
int ncattget( int ncid, int varid, const char* name, void* value ) { const int status = nc_get_att(ncid, varid, name, value); if(status != NC_NOERR) { nc_advise("ncattget", status, "ncid %d", ncid); return -1; } return 1; }
int ncattrename( int ncid, int varid, const char* name, const char* newname ) { const int status = nc_rename_att(ncid, varid, name, newname); if(status != NC_NOERR) { nc_advise("ncattrename", status, "ncid %d", ncid); return -1; } return 1; }
int ncattname( int ncid, int varid, int attnum, char* name ) { const int status = nc_inq_attname(ncid, varid, attnum, name); if(status != NC_NOERR) { nc_advise("ncattname", status, "ncid %d", ncid); return -1; } return attnum; }
int ncattcopy( int ncid_in, int varid_in, const char* name, int ncid_out, int varid_out ) { const int status = nc_copy_att(ncid_in, varid_in, name, ncid_out, varid_out); if(status != NC_NOERR) { nc_advise("ncattcopy", status, "%s", name); return -1; } return 0; }
/* * Get the value of a netCDF character attribute given its variable * ID and name. */ static void c_ncagtc( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ char* value, /* pointer to data values */ int attlen, /* length of string argument */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_atttype(ncid, varid, attname, &datatype)) == 0) { if (datatype != NC_CHAR) status = NC_ECHAR; else { size_t len; status = nc_inq_attlen(ncid, varid, attname, &len); if (status == 0) { if (attlen < len) status = NC_ESTS; else { status = nc_get_att_text(ncid, varid, attname, value); if (status == 0) (void) memset(value+len, ' ', attlen - len); } } } } if (status == 0) *rcode = 0; else { nc_advise("NCAGTC", status, ""); *rcode = ncerr; } }
int ncattput( int ncid, int varid, const char* name, nc_type datatype, int len, const void* value ) { const int status = nc_put_att(ncid, varid, name, datatype, len, value); if(status != NC_NOERR) { nc_advise("ncattput", status, "ncid %d", ncid); return -1; } return 0; }
int ncvardef( int ncid, const char* name, nc_type datatype, int ndims, const int* dim ) { int varid = -1; const int status = nc_def_var(ncid, name, datatype, ndims, dim, &varid); if(status != NC_NOERR) { nc_advise("ncvardef", status, "ncid %d", ncid); return -1; } return varid; }
/* * 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; } }
int ncvargetg( int ncid, int varid, const long* start, const long* count, const long* stride, const long* map, void* value ) { if(map == NULL) return ncvargets(ncid, varid, start, count, stride, value); /* else */ { NDIMS_DECL A_DECL(stp, size_t, ndims, start); A_DECL(cntp, size_t, ndims, count); A_DECL(strdp, ptrdiff_t, ndims, stride); A_DECL(imp, ptrdiff_t, ndims, map); A_INIT(stp, size_t, ndims, start); A_INIT(cntp, size_t, ndims, count); A_INIT(strdp, ptrdiff_t, ndims, stride); A_INIT(imp, ptrdiff_t, ndims, map); { const int status = nc_get_varm(ncid, varid, stp, cntp, strdp, imp, value); A_FREE(imp); A_FREE(strdp); A_FREE(cntp); A_FREE(stp); if(status != NC_NOERR) { nc_advise("ncvargetg", status, "ncid %d", ncid); return -1; } } return 0; } }
int ncrecinq( int ncid, int* nrecvars, int* recvarids, long* recsizes ) { size_t nrv = 0; size_t *rs = NULL; int status = NC_NOERR; rs = (size_t*)malloc(sizeof(size_t)*NC_MAX_VARS); if(rs == NULL) return NC_ENOMEM; status = nc_inq_rec(ncid, &nrv, recvarids, rs); if(status != NC_NOERR) { nc_advise("ncrecinq", status, "ncid %d", ncid); if(rs != NULL) free(rs); return -1; } if(nrecvars != NULL) *nrecvars = (int) nrv; if(recsizes != NULL) { size_t ii; for(ii = 0; ii < nrv; ii++) { recsizes[ii] = (long) rs[ii]; } } if(rs != NULL) free(rs); return (int) nrv; }
/* * Read a generalized hypercube of character values from a netCDF variable * of an open netCDF file. */ static void c_ncvggc( 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 ptrdiff_t* strides,/* netCDF variable access strides */ const ptrdiff_t* imap, /* memory values access basis vector */ char* value, /* block of data values to be written */ int* rcode /* returned error code */ ) { int status; int rank; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0 && (status = nc_inq_varndims(ncid, varid, &rank)) == 0) { switch (datatype) { case NC_CHAR: status = nc_get_varm_text(ncid, varid, start, count, strides, imap, value); break; default: status = NC_ECHAR; break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVGGC", status, ""); *rcode = ncerr; } }
/* * Read a hypercube of character values from a netCDF variable. */ static void c_ncvgtc( 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 */ char* value, /* block of data values to be read */ 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 if ((status = nc_get_vara_text(ncid, varid, start, count, value)) == 0) { int rank; if ((status = nc_inq_varndims(ncid, varid, &rank)) == 0) { size_t total = dimprod(count, rank); (void) memset(value+total, ' ', lenstr - total); } } } if (status == 0) *rcode = 0; else { nc_advise("NCVGTC", status, ""); *rcode = ncerr; } }
int ncvarget1( int ncid, int varid, const long* index, void* value ) { NDIMS_DECL A_DECL(coordp, size_t, ndims, index); A_INIT(coordp, size_t, ndims, index); { const int status = nc_get_var1(ncid, varid, coordp, value); A_FREE(coordp); if(status != NC_NOERR) { nc_advise("ncdimid", status, "ncid %d", ncid); return -1; } } return 0; }
int ncattinq( int ncid, int varid, const char* name, nc_type* datatype, int* len ) { size_t ll; const int status = nc_inq_att(ncid, varid, name, datatype, &ll); if(status != NC_NOERR) { nc_advise("ncattinq", status, "ncid %d", ncid); return -1; } if(len != NULL) *len = (int) ll; return 1; }
int ncdiminq( int ncid, int dimid, char* name, long* length ) { size_t ll; const int status = nc_inq_dim(ncid, dimid, name, &ll); if(status != NC_NOERR) { nc_advise("ncdiminq", status, "ncid %d", ncid); return -1; } /* else */ if(length != NULL) *length = (int) ll; return dimid; }
/* * This is how much space is required by the user, as in * * vals = malloc(nel * nctypelen(var.type)); * ncvarget(cdfid, varid, cor, edg, vals); */ int nctypelen(nc_type type) { switch(type){ case NC_BYTE : case NC_CHAR : return((int)sizeof(char)); case NC_SHORT : return(int)(sizeof(short)); case NC_INT : return((int)sizeof(int)); case NC_FLOAT : return((int)sizeof(float)); case NC_DOUBLE : return((int)sizeof(double)); } #ifndef NO_NETCDF_2 /* else */ nc_advise("nctypelen", NC_EBADTYPE, "Unknown type %d", (int)type); #endif /* NO_NETCDF_2 */ return -1; }
/* * Write a generalized hypercube of numeric values into a netCDF variable of * an open netCDF file. */ static void c_ncvptg ( 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 ptrdiff_t* strides,/* netCDF variable access strides */ const ptrdiff_t* imap, /* memory values access mapping vector */ const void* value, /* block of data values to be written */ int* rcode /* returned error code */ ) { int status; int rank; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0 && (status = nc_inq_varndims(ncid, varid, &rank)) == 0) { switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_varm_schar(ncid, varid, start, count, strides, imap, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_varm_short(ncid, varid, start, count, strides, imap, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_varm_int(ncid, varid, start, count, strides, imap, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_varm_long(ncid, varid, start, count, strides, imap, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_varm_short(ncid, varid, start, count, strides, imap, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_varm_int(ncid, varid, start, count, strides, imap, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_varm_long(ncid, varid, start, count, strides, imap, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_varm_int(ncid, varid, start, count, strides, imap, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_varm_long(ncid, varid, start, count, strides, imap, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_varm_float(ncid, varid, start, count, strides, imap, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_varm_double(ncid, varid, start, count, strides, imap, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_varm_float(ncid, varid, start, count, strides, imap, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_varm_double(ncid, varid, start, count, strides, imap, (const double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVPTG", status, ""); *rcode = ncerr; } }
/* * Put a single numeric data value into a variable of an open netCDF. */ static void c_ncvpt1 ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const size_t* indices,/* multidim index of data to be written */ const void* value, /* pointer to data value to be written */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0) { switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_var1_schar(ncid, varid, indices, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_var1_short(ncid, varid, indices, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_var1_short(ncid, varid, indices, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_var1_float(ncid, varid, indices, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_var1_double(ncid, varid, indices, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_var1_float(ncid, varid, indices, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_var1_double(ncid, varid, indices, (const double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVPT1", status, ""); *rcode = ncerr; } }
/* * Get the value of a netCDF attribute given its variable ID and name. */ static void c_ncagt( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ void* value, /* pointer to data values */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_atttype(ncid, varid, attname, &datatype)) == 0) { switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_get_att_schar(ncid, varid, attname, (signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_get_att_short(ncid, varid, attname, (short*)value); # elif NF_INT1_IS_C_INT status = nc_get_att_int(ncid, varid, attname, (int*)value); # elif NF_INT1_IS_C_LONG status = nc_get_att_long(ncid, varid, attname, (long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_get_att_short(ncid, varid, attname, (short*)value); # elif NF_INT2_IS_C_INT status = nc_get_att_int(ncid, varid, attname, (int*)value); # elif NF_INT2_IS_C_LONG status = nc_get_att_long(ncid, varid, attname, (long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_get_att_int(ncid, varid, attname, (int*)value); # elif NF_INT_IS_C_LONG status = nc_get_att_long(ncid, varid, attname, (long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_get_att_float(ncid, varid, attname, (float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_get_att_double(ncid, varid, attname, (double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_get_att_float(ncid, varid, attname, (float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_get_att_double(ncid, varid, attname, (double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCAGT", status, ""); *rcode = ncerr; } }
/* * Add or changes a numeric variable or global attribute of an open * netCDF file. */ static void c_ncapt ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ nc_type datatype, /* attribute datatype */ size_t attlen, /* attribute length */ const void* value, /* pointer to data values */ int* rcode /* returned error code */ ) { int status; switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_att_schar(ncid, varid, attname, datatype, attlen, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; } if (status == 0) *rcode = 0; else { nc_advise("NCAPT", status, ""); *rcode = ncerr; } }
/* * Read a hypercube of numeric values from a netCDF variable of an open * netCDF file. */ static void c_ncvgt( 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 */ void* value, /* block of data values to be read */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0) { switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_get_vara_schar(ncid, varid, start, count, (signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_get_vara_short(ncid, varid, start, count, (short*)value); # elif NF_INT1_IS_C_INT status = nc_get_vara_int(ncid, varid, start, count, (int*)value); # elif NF_INT1_IS_C_LONG status = nc_get_vara_long(ncid, varid, start, count, (long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_get_vara_short(ncid, varid, start, count, (short*)value); # elif NF_INT2_IS_C_INT status = nc_get_vara_int(ncid, varid, start, count, (int*)value); # elif NF_INT2_IS_C_LONG status = nc_get_vara_long(ncid, varid, start, count, (long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_get_vara_int(ncid, varid, start, count, (int*)value); # elif NF_INT_IS_C_LONG status = nc_get_vara_long(ncid, varid, start, count, (long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_get_vara_float(ncid, varid, start, count, (float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_get_vara_double(ncid, varid, start, count, (double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_get_vara_float(ncid, varid, start, count, (float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_get_vara_double(ncid, varid, start, count, (double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVGT", status, ""); *rcode = ncerr; } }