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

[netCDF #XYH-561731]: Reading netCDF files.



Johanna,

> Would I create a new NcVar to read in the data? Or how do I get it to put
> it on the heap?

As the examples show, you typically allocate memory in the calling program
in which to read the data.  Also, the data for netCDF variables is typically
larger than you need to keep in memory all at one time, or is read in slices
that require less memory than the whole variable.  However, if you have
enough memory, you can read all the values of a variable into one big chunk
of contiguous memory, allocated with a single "new" constructor, or declared
statically.

For example, in the netCDF-3 C++ API you are using, to read all the
data in at once from:

  float h(time, levels, latitude, longitude) ;

where the sizes of the dimensions are

        time = UNLIMITED ; // (8 currently)
        levels = 37 ;
        longitude = 288 ;
        latitude = 144 ;

you could allocate a multidimensional array using something like

  float h_vals[8][37][288][144];

or 

  h_vals = new float[8][37][288][144];

and then read in all the values at once into the h_vals array using
something like:

  NcVar *hVar = dataFile.get_var("h");
  boolean ret = hVar.get(&h_vals[0][0][0][0], 8, 37, 288, 144);

following the documenation of the NcVar::get method here:

  http://www.unidata.ucar.edu/netcdf/docs/netcdf-cxx.html#Class-NcVar

But you could also use the same get method to just read the data from
one time with each call, using something like

  float h_vals[37][288][144];

or 

  h_vals = new float[37][288][144];

and then read in all the values for the fourth time into the h_vals array using
something like:

  int time = 3;  // the first time is time 0
  NcVar *hVar = dataFile.get_var("h");
  hVar.setcur(time, 0, 0, 0);
  boolean ret = hVar.get(&h_vals[0][0][0], 37, 288, 144);

You should check the return value from these method calls to make sure
you catch any errors ...

--Russ

> 2012/3/12 Unidata netCDF Support <address@hidden>
> 
> > Hi Johanna,
> >
> > > Hi,
> > >
> > > I am trying to open netCDF files containing
> > > 8*37*144*288 dimensional variables. But I keep getting segmentation
> > fault.
> > > I think this is because of the fact that the files are rather big and are
> > > being put in the stack. I want to put them in the heap but I can't seem
> > to
> > > figure out how. Since NcVar is a private I can't use new.
> > >
> > > Do you know how my issue can be resolved?
> >
> > If you're going to try to read all the values of a netCDF variable on disk
> > into memory,
> > you need to allocate memory into which the values will be read.  The code
> > shown
> > below just tries to read all the data into space pointed to by an
> > uninitialized pointer.
> >
> > There are some example programs that use the netCDF C++ interface here:
> >
> >  http://www.unidata.ucar.edu/netcdf/examples/programs/index.html
> >
> > I think the example that's closest to what you want is
> > sfc_pres_temp_rd.cpp or
> > pres_temp_4D_wr.cpp.
> >
> > I hope that helps.
> >
> > --Russ
> >
> > > I am trying to open the relative humidity, pressure levels and
> > temperature
> > > from these files:
> > >
> > >
> > http://goldsmr3.sci.gsfc.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2Fs4pa%2FMERRA%2FMAI3CPASM.5.2.0%2F2011%2F11%2FMERRA300.prod.assim.inst3_3d_asm_Cp.20111130.hdf&FORMAT=TmV0Q0RGLw&BBOX=-90%2C-180%2C90%2C180&LABEL=MERRA300.prod.assim.inst3_3d_asm_Cp.20111130.SUB.nc&SHORTNAME=MAI3CPASM&SERVICE=SUBSET_LATS4D&LAYERS=LAYER_1000%2C250%2C975%2C200%2C950%2C150%2C925%2C100%2C900%2C70%2C875%2C50%2C850%2C40%2C825%2C30%2C800%2C20%2C775%2C10%2C750%2C7%2C725%2C5%2C700%2C4%2C650%2C3%2C600%2C2%2C550%2C1%2C500%2C450%2C400%2C350%2C300&VERSION=1.02&DATASET_VERSION=5.2.0&VARIABLES=h%2Crh%2Ct
> > >
> > >
> > > Code fragment:
> > > // Open the file.
> > > NcFile dataFile("file1.nc", NcFile::ReadOnly);
> > >
> > > // Get pointers to the pressure and temperature variables.
> > > NcVar *hVar, *tempVar, *rhVar;
> > > if (!(hVar = dataFile.get_var("h")))
> > > return NC_ERR;
> > > if (!(tempVar  = dataFile.get_var("t")))
> > > return NC_ERR;
> > > if (!(rhVar  = dataFile.get_var("rh")))
> > > return NC_ERR;
> > >
> > > cout << "*** SUCCESS reading example file file1.nc!" << endl;
> > > return 0;
> > >
> > >
> > > Thank you!
> > >
> > > Sincerely
> > > Johanna Juhl
> > >
> > >
> >
> > Russ Rew                                         UCAR Unidata Program
> > address@hidden                      http://www.unidata.ucar.edu
> >
> >
> >
> > Ticket Details
> > ===================
> > Ticket ID: XYH-561731
> > Department: Support netCDF
> > Priority: Normal
> > Status: Closed
> >
> >
> 
> 
Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: XYH-561731
Department: Support netCDF
Priority: Normal
Status: Closed