This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>To: address@hidden >From: address@hidden >Subject: nc_inq_var of netcdf >Organization: . >Keywords: 199909241257.GAA06898 Hi Thomas, > I am working with the 32-bit version of your netcdf.dll on Windows NT. > nc_inq_libvers brings "VERSION of Mar 17 1998 16:25:22 $". > Writing C++ code under Borland C++ Builder I have a problem with function > nc_inq_var: the varId that is passed as value is overwritten, see below. > Could you provide a solution or explanation to this problem? I am unable to duplicate the problem, and suspect something else is happening to make you think that varId is overwritten, but varId cannot be overwritten by a function to which you only pass its value rather than its address. > > static int GetNcValue( const String andiAttr, > int cdfId, > double *pAndiDbl ) > { > int err; > int varId; > nc_type type; > int dims; > > err = nc_inq_varid( cdfId, andiAttr.c_str(), &varId ); > >>>>>>>>>>>>>>>> varId == 3 > if (NC_NOERR != err) > { > return( err ); > } > err = nc_inq_var( cdfId, > varId, > NULL, // name > &type, > &dims, > NULL, // dimids > NULL ); // natts > >>>>>>>>>>>>>>>> varId == 0 Passing the argument "varId" to any function cannot change its value, since only a copy of the value of varId is passed to the function. You would have to pass "&varId" as a parameter to the function for it to overwrite the value of varId. Even then, nc_inq_var does not overwrite its second parameter. I have appended a program that creates a netCDF file and runs code similar to what you have provided, printing the value of "varId" before and after the call to nc_inq_var(), and the value is unchanged. Could you compile this, link it against netcdf.dll you have, and verify that it behaves as expected, producing the following output: varId = 5 after nc_inq_var(), varId = 5 So I'm afraid I cannot explain how you could be seeing the value of varId changed by a call to c_inq_var(). Incidentally, if you are using C++, you might find the C++ interface to netCDF easier to use. It detects more errors at compile time than the C interface. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu #include "netcdf.h" #include <stdio.h> static int GetNcValue( const char *varName, int cdfId, double *pAndiDbl ) { int err; int varId; nc_type type; int dims; err = nc_inq_varid( cdfId, varName, &varId ); printf( "varId = %d\n", varId); if (NC_NOERR != err) { return( err ); } err = nc_inq_var( cdfId, varId, NULL, // name &type, &dims, NULL, // dimids NULL ); // natts printf ("after nc_inq_var(), varId = %d\n", varId); if (NC_NOERR != err) { return( err ); } if (NC_CHAR == type) // this item is no numeric value { printf("expected numeric value\n"); return( -1 ); } if (0 != dims) // this is an array instead of a single value { printf("expected scalar\n"); return( -2 ); } // get the the value as double err = nc_get_var_double( cdfId, varId, pAndiDbl ); if (NC_NOERR != err) { return( err ); } return( 0 ); } int main() { int status; int ncid; /* dimension ids */ int lat_dim, lon_dim, frtime_dim, timelen_dim; /* variable ids */ int P_id, lat_id, lon_id, frtime_id, reftime_id, scalarv_id; /* variable shapes */ int dims[3]; /* containers for scalar attributes */ float float_val; double double_val; /* attribute vectors */ float P_valid_range[2]; status = nc_create("example.nc", NC_CLOBBER,&ncid); /* define dimensions */ status = nc_def_dim(ncid, "lat", 4L , &lat_dim); status = nc_def_dim(ncid, "lon", 3L, &lon_dim); status = nc_def_dim(ncid, "frtime", NC_UNLIMITED, &frtime_dim); status = nc_def_dim(ncid, "timelen", 20L, &timelen_dim); /* define variables and assign attributes */ dims[0] = frtime_dim; dims[1] = lat_dim; dims[2] = lon_dim; status = nc_def_var(ncid, "P", NC_FLOAT, 3, dims, &P_id); nc_put_att_text(ncid, P_id, "long_name", 24, "pressure at maximum wind"); nc_put_att_text(ncid, P_id, "units", 12, "hectopascals"); P_valid_range[0] = 0; P_valid_range[1] = 1500; nc_put_att_float(ncid, P_id, "valid_range", NC_FLOAT , 2 , P_valid_range); float_val = -9999; nc_put_att_float(ncid, P_id, "_FillValue", NC_FLOAT, 1 , &float_val); dims[0] = lat_dim; status = nc_def_var(ncid, "lat", NC_FLOAT, 1, dims, &lat_id); nc_put_att_text(ncid, lat_id, "long_name", 8, "latitude"); nc_put_att_text(ncid, lat_id, "units", 13, "degrees_north"); dims[0] = lon_dim; status = nc_def_var(ncid, "lon", NC_FLOAT, 1, dims , &lon_id); nc_put_att_text(ncid, lon_id, "long_name", 9, "longitude"); nc_put_att_text(ncid, lon_id, "units", 12, "degrees_east"); dims[0] = frtime_dim; status = nc_def_var(ncid, "frtime", NC_LONG, 1, dims , &frtime_id); nc_put_att_text(ncid, frtime_id, "long_name", 13, "forecast time"); nc_put_att_text(ncid, frtime_id, "units", 5, "hours"); dims[0] = timelen_dim; status = nc_def_var(ncid, "reftime", NC_CHAR, 1, dims , &reftime_id); nc_put_att_text(ncid, reftime_id, "long_name", 14,"reference time"); nc_put_att_text(ncid, reftime_id, "units", 9,"text_time"); status = nc_def_var(ncid, "scalarv", NC_DOUBLE, 0, 0 , &scalarv_id); double_val = 1; nc_put_att_double(ncid, scalarv_id, "scalar_att", NC_DOUBLE, 1, &double_val); /* Global attributes */ nc_put_att_text(ncid, NC_GLOBAL, "history", 41,"created by Unidata LDM from NPS broadcast"); nc_put_att_text(ncid, NC_GLOBAL, "title", 48,"NMC Global Product Set: Pressure at Maximum Wind"); /* leave define mode */ nc_enddef(ncid); { /* store lat */ static unsigned int lat_start[] = {0}; static unsigned int lat_edges[] = {4}; static float lat[] = {-90, -87.5, -85, -82.5}; nc_put_vara_float(ncid, lat_id, lat_start, lat_edges, lat); } { /* store lon */ static unsigned int lon_start[] = {0}; static unsigned int lon_edges[] = {3}; static float lon[] = {-180, -175, -170}; nc_put_vara_float(ncid, lon_id, lon_start, lon_edges, lon); } { /* store frtime */ static unsigned int frtime_start[] = {0}; static unsigned int frtime_edges[] = {1}; static long frtime[] = {12}; nc_put_vara_long(ncid, frtime_id, frtime_start, frtime_edges,frtime); } { /* store frtime */ static unsigned int frtime_start[] = {1}; static unsigned int frtime_edges[] = {1}; static long frtime[] = {18}; nc_put_vara_long(ncid, frtime_id, frtime_start, frtime_edges,frtime); } { /* store reftime */ static unsigned int reftime_start[] = {0}; static unsigned int reftime_edges[] = {20}; static char reftime[] = {"1992 03 04 12:00"}; nc_put_vara_text(ncid, reftime_id, reftime_start, reftime_edges,reftime); } { /* store P */ static unsigned int P_start[] = {0, 0, 0}; static unsigned int P_edges[] = {2, 4, 3}; static float P[] = { 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973 }; nc_put_vara_float(ncid, P_id, P_start, P_edges, P); } { /* store scalarv */ static double scalarv = {3.14159265358979}; nc_put_var1_double(ncid, scalarv_id, (unsigned int *)0, &scalarv); } double dval; status = GetNcValue( "scalarv", ncid, &dval ); nc_close (ncid); return 0; }