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

[netCDF #IZY-489192]: Reg : Reading netCDF files



Hi Rekha,

> I am trying to read a netCDF file which has the following data.
> 
> [image: Inline image 1]
  double windx(time, node);

(It would be easier to answer questions and archive answers for other users 
 to find if you would paste the text into your email, rather than attaching
 an image of a screen display.  If you can't get the text characters any other 
 way, try looking at the short video "Optical Character Recognition (OCR) 
 in Google Docs Tutorial", which shows one free and easy way to do it.)

> I want to retrieve the windx values which has dimensions of time and node.
> Here the time has 96 values and node has 1181186 values. The program runs
> really very slow.
> 
> I have the following code, which reads 10 nodes for each timestep
> 
> [image: Inline image 2]

  if (attnames[j] == windx")
  {
      int countNodex=1;
      for(int incNode=0;incNode<10;incNode++)
      {
         size_t start[] = {0,incNode};
         size_t end[] = {95,countNodex};
         boost:shared_array<float> temp(new float[node]);
         nc_get_vara_float (ncid, i, start, end, temp.get());
         testing_windx.push_back(temp);
      }
      found = true;
  }

> *I wanted to know whether the nc_get_vara_float reads the netCDF file each
> time? If that is the case, then lot of computation needs to be done and
> hence the code can be slow. Is there any way to retrieve all the windx
> values from netCDF and store it in memory and then use the values from the
> memory?*

Yes, though I'm not familiar enough with the C++ boost library to show how to
do it with the boost shared array, as you are trying to do above.

In C, if you want to read all the values into a memory array declared as

  #define NTIMES 96
  #define NNODES 1181186
  float data[NTIMES][NNODES] /* or as a 1D array, data[NTIMES*NNODES */

you can just call nc_get_vara() once to fill up the data array, assuming
the 2D variable on the disk has exactly NTIMES*NNODES values:

  nc_get_var_float(ncid, varid, &data[0][0]); /* or &data[0] for 1D array */

If there might be more values on disk, for example more times or nodes than
you want to handle all at once, then you need to specify start and count 
arrays to read just the NTIMES*NNODES values, but you still need only one
call to nc_get_vara_float():

  int start[] = {0, 0};
  int count[] = {NTIMES, NNODES};
    ...
  nc_get_vara_float(ncid, varid, start, count, &data[0][0]);

The way you are now doing it, you read only 95 values for 95 different times 
in each loop iteration, which gets you only one value per disk access, which
would be a fairly slow way to read the data.

--Russ

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



Ticket Details
===================
Ticket ID: IZY-489192
Department: Support netCDF
Priority: Normal
Status: Closed