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

[netCDF #AYT-822283]: Question about nc_get_vara_double



Markus,

According to your original question, the netCDF variable TMP is
dimensioned as

>         double TMP(time, lat, lon) ;

so the longitude values vary most rapidly, that is there are 190
values associated with longitudes for each latitude.

But according to your response, you have a "lon-lat grid array", from
which I assume it has latitude values varying most rapidly, with 190
latitude values for each longitude.  Therefor, you either need to
transpose the values for each time in the temperature arrays you read
in, or transpose the grid values to correspond to the order of the
temperature data.

The code you have proposed for transposing the temperature grid:

> x = 0;
> for(i=0; i<12; i++)
>     for(j=0; j<190; j++)
>        for(k=0; k<190; k++)
>
temperature[x++]=temperature[i*climate->iin*climate->jjn+j*climate->jjn+k];

unfortunately won't work, because it tries to transpose the grid in
memory, overwriting values that are later needed.  You must use a
separate array in memory to transpose an array simply.  (It's possible
to transpose an array in place, but the algorithm is complex,
involving the prime factorization of the dimensions ...).

So I think you need something like:

 double tempx[12][190][190];

 for(i=0; i<12; i++)
     for(j=0; j<190; j++)
        for(k=0; k<190; k++)
            tempx[i][k][j] = temperature[i*NLAT*NLON+j*NLON+k];

Note the order of the dimensions is k,j on the left and j,k on the
right, to accomplish the transpose.  If tempx must be a singly
dimensioned array, you could instead use:

 double tempx[12*190*190];

 for(i=0; i<12; i++)
     for(j=0; j<190; j++)
        for(k=0; k<190; k++)
            tempx[i*NLAT*NLON+k*NLON+j] = temperature[i*NLAT*NLON+j*NLON+k];

--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