[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #FZU-731621]: Problem with variables in netcdf



Hi Birgit,

> I am a student in German Aerospace Centre. I tried to write a netdf-file
> with C++ which is bigger than 4GB, but I were not able to do this.
> So i created a dummy case with only one variable and 30 points, but it
> doesn't work too.
> 
> My variable becomes not recognized by netcdf.
> 
> I build a stand-alone code example, which I append. Here are the versions
> I used:
> 
> netcdf-4.1.3
> hdf5-1.8.8
> zlib-1.2.5
> gcc (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973]
> 
> What I have to change?

You should check for error returns on the netCDF function calls,
rather than just assuming they worked OK.  For example, if instead 
of:

  nc_def_var(cdfid, varname, NC_DOUBLE, 1, &compid, &lineid);

you use:

  int err;
  err = nc_def_var(cdfid, varname, NC_DOUBLE, 1, &compid, &lineid);
  if (err != NC_NOERR)
       std::cout << nc_strerror(err) << std::endl;

you will get the message:

  NetCDF: Invalid dimension ID or name

The name is OK, but the "&compid" argument isn't an array containing the
dimension IDs for the variable, returned by the nc_def_dim() call above.

If you change the "&compid" to "&dim", then everything works OK:

  err = nc_def_var(cdfid, varname, NC_DOUBLE, 1, &dim, &lineid);

and similarly for the following nc_def_var call.

--Russ

> Attachment code:
> //################################################################
> //main.cpp
> #include <iostream>
> #include <stdlib.h>
> #include <string.h>
> #include <stdio.h>
> 
> #include <netcdf.h>
> 
> #define FILETYPE "pointdata"
> #define SCALARTYPE "Scalar"
> 
> void netcdf_write_pdata(char *filename, char *varname, double *primvar,
> int npnt)
> {
> int dimless_output = 1;
> int grid = 1;
> int cdfid;
> int compid;
> int dim;
> int lineid;
> int varid;
> 
> double first_res = 1.0;
> double act_time = 0.1;
> double old_time = 0.01;
> 
> /*----------------------------------------------------------------------------
> | set up for netcdf
> ----------------------------------------------------------------------------*/
> 
> nc_create(filename, NC_CLOBBER | NC_64BIT_OFFSET, &cdfid);
> 
> ncopts = NC_VERBOSE | NC_FATAL;
> 
> ncsetfill(cdfid, NC_NOFILL);
> 
> /*----------------------------------------------------------------------------
> | set the attributes
> ----------------------------------------------------------------------------*/
> 
> nc_put_att_text(cdfid, NC_GLOBAL, "type", (int) strlen(FILETYPE) + 1,
> FILETYPE);
> 
> nc_put_att_text(cdfid, NC_GLOBAL, "var_type", (int) strlen(SCALARTYPE) + 1,
> SCALARTYPE);
> 
> nc_put_att_int(cdfid, NC_GLOBAL, "dimless_pointdata", NC_INT, 1,
> &dimless_output);
> 
> nc_put_att_int(cdfid, NC_GLOBAL, "grid", NC_INT, 1, &grid);
> 
> nc_put_att_double(cdfid, NC_GLOBAL, "first_residual", NC_DOUBLE, 1,
> &first_res);
> 
> nc_put_att_double(cdfid, NC_GLOBAL, "actual_time", NC_DOUBLE, 1, &act_time);
> 
> nc_put_att_double(cdfid, NC_GLOBAL, "old_time", NC_DOUBLE, 1, &old_time);
> 
> /*----------------------------------------------------------------------------
> | set dimensions
> ----------------------------------------------------------------------------*/
> 
> nc_def_dim(cdfid, "no_of_points", npnt, &dim);
> 
> /*----------------------------------------------------------------------------
> | define variable
> ----------------------------------------------------------------------------*/
> 
> nc_def_var(cdfid, varname, NC_DOUBLE, 1, &compid, &lineid);
> 
> nc_def_var(cdfid, "global_id", NC_INT, 1, &compid, &lineid);
> 
> /*----------------------------------------------------------------------------
> | end of define
> ----------------------------------------------------------------------------*/
> 
> ncendef(cdfid);
> 
> /*----------------------------------------------------------------------------
> | output
> ----------------------------------------------------------------------------*/
> 
> nc_inq_varid(cdfid, varname, &varid);
> nc_put_var_double(cdfid, varid, primvar);
> 
> //B int *globalid_tmp = (int *) check_malloc(npnt * sizeof (int));
> int *globalid_tmp = new int[npnt];
> 
> for (int ii = 0; ii < npnt; ii++)
> {
> globalid_tmp[ii] = ii;
> }
> 
> nc_inq_varid(cdfid, "global_id", &varid);
> nc_put_var_int(cdfid, varid, globalid_tmp);
> 
> /*----------------------------------------------------------------------------
> | close netcdf file
> ----------------------------------------------------------------------------*/
> 
> ncclose(cdfid);
> 
> return;
> }
> 
> int main()
> {
> int npnt = 30;
> char* filename = "main.pval";
> char* varname= "DummyOne";
> double* primvar = new double[npnt];
> for (int i = 0; i < npnt; i++)
> {
> primvar[i] = i;
> }
> netcdf_write_pdata(filename, varname, primvar, npnt);
> std::cout << "Write is ready" << std::endl;
> 
> return 0;
> }
> //################################################################
> 
> 

Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: FZU-731621
Department: Support netCDF
Priority: Normal
Status: Closed