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

[netCDF #AYT-822283]: Question about nc_get_vara_double



Hi Markus,

> I'm a bit confused on how the data read with nc_get_vara_double
> function are stored into the given array.
>
> I have such input:
>
> netcdf TMP_1901-2002 {
> dimensions:
>         lat = 190 ;
>         lon = 190 ;
>         time = 1224 ;
> variables:
>         double latitude(lat, lon) ;
>         double longitude(lat, lon) ;
>         double time(time) ;
>         double TMP(time, lat, lon) ;
>
> I read the first 12 months for the whole lon and lat dimentions using:
> nc_get_vara_double (ncid, tempid,start, count, temperature);

So I'm assuming start was {0, 0, 0} and count was {12, 190, 190),
reading 12*190*190 doubles into the temperature array in your C
program.

> Now I would like to have a sequence of 190*190 data for each month.

I'm not sure what you mean, do you just want to copy data out of the
temperature array into another array temp?  Or do you want temp to be
a smaller array that just hold's one month of data at a time?

> I read the documentation but when I use the suggested code I got wrong
> results. I used these loops:
>
> for(i=0; i<12; i++)
>      for(j=0; j<NLAT; j++)
>         for(k=0; k<NLON; k++)
>                temp[i*NLAT*NLON+j*NLON+k]=temperature[i*NLAT*NLON+j*NLON+k];

That loop just copies all the elements in the temperature array into
the temp array, and is equivalent to the simpler loop:

  for(m=0; m < 12 * NLAT * NLON; m++)
       temp[m] = temperature[m];

If instead you want to work with one month of data at a time, stored
in the variable temp, declared

  double temp[NLAT][NLON];

for example, then you could use code something like this:

   for(i=0; i<12; i++) {
       for(j=0; j<NLAT; j++) {
           for(k=0; k<NLON; k++) {
               temp[j][k] = temperature[i*NLAT*NLON+j*NLON+k];
           }
       }
   /* Now do something for month i with temp array */
 ...
   }

But you could also just have temp_ptr be a pointer and not bother to
copy the numbers, but instead have it point to the data for month i,
something like:

   double *temp_ptr;
 ...
   for(i=0; i<12; i++) {
       temp_ptr = &temperature[i*NLAT*NLON];
   /* Now do something for month i with array temp_ptr points at */
 ...
   }

I'm not sure whether either of these is what you had in mind.

--Russ

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



Ticket Details
===================
Ticket ID: AYT-822283
Department: Support netCDF
Priority: Normal
Status: Closed