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

Re: mixed programming, C and Fortran and netcdf



>To: <address@hidden>
>From: "ufuk turuncoglu" <address@hidden>
>Subject: Re: 20040728:  mixed programming, C and Fortran and netcdf
>Organization: Informatics Institute, Istanbul Technical University
>Keywords: 200407281439.i6SEd0aW023381

Hi,

> i place the two netcdf file into fallowing directory to determine the
> problem.
> 
> http://www.be.itu.edu.tr/~turuncu/IC.nc (created using C, size ~ 4.5 mb)
> http://www.be.itu.edu.tr/~turuncu/pom2k.nc (read from f77 and created in
> f77, size ~ 16 mb)
> 
> The file IC.nc is created using mixed language programming, the variables
> calculated in f90 and the C interface is used for creating netcdf. To avoid
> memory allocation differencies between C and f90, one extra subroutine  is
> added to C and it convert the input data (from f90) to C array indexing
> format and correct it. It can be seen in fallowing code,
> 
> double* Df2TOC1(double *in, int im, int jm){
>     //Converts two dimensional double fortran array to
>     //one dimensional double C vector
>     int offset, i, j;
>     double *out;
> 
>     //Allocate one dimensional vector
>     out = (double*) calloc(im*jm, sizeof(double));
> 
>     //Changage indices order
>     for (i = 0; i < im; i++){
>        offset=jm*i;
>        for (j= 0; j < jm; j++){
>           out[offset+j]=in[j*im+i];
>        }
>     }
>     return(out);
> }

The fact that you are trying to transpose the 2-dimensional matrix to
change the order of indices indicates that you aren't understanding
the language-neutral model for netCDF data.  The data are not written
to a netCDF file in "C-order" or "Fortran-order", but merely in the
order they occur in memory, which is the same if you use the C or
Fortran conventions for storing the data.  

Please read the section 3.2.2  - An Example of Array-Section Access in
the C Users' Guide for netCDF at

  
http://my.unidata.ucar.edu/content/software/netcdf/guidec/guidec-8.html#HEADING8-81

and the corresponding section in the Fortran Users' Guide for netCDF
at

  
http://my.unidata.ucar.edu/content/software/netcdf/guidef/guidef-8.html#HEADING8-81

and you will see that a C variable declared as

  float   temp[TIMES][LEVELS][LATS][LONS]

corresponds exactly to a Fortran variable declared as

  REAL TEMP(LONS, LATS, LEVELS, TIMES)

and in particular, the paragraph:

  Different dimension orders for the C, FORTRAN, or other language
  interfaces do not reflect a different order for values stored on the
  disk, but merely different orders supported by the procedural
  interfaces to the languages. In general, it does not matter whether
  a netCDF dataset is written using the C, FORTRAN, or another
  language interface; netCDF datasets written from any supported
  language may be read by programs written in other supported
  languages.

Another way to see this is to use ncgen -c and ncgen -f to generate
the C and Fortran code for a simple netCDF file represented in CDL,
for example

netcdf order {
dimensions:
        lon = 4 ;
        lat = 3 ;
        time = 2 ;
variables:
        float var(time, lat, lon) ;
data:

 var =
    1, 2, 3, 4,
    5, 6, 7, 8,
    9, 10, 11, 12,
    13, 14, 15, 16,
    17, 18, 19, 20,
    21, 22, 23, 24 ;
}

To generate the C and Fortran code, store the above CDL in a file
named "order.cdl" and invoke

  $ ncgen -c order.cdl > order.c
  $ ncgen -f order.cdl > order.f

and note the declarations and exactly analogous calls in the two
languages to create the netCDF file. If you compile and run order.c
and order.f, the created files are exactly the same, bit-for-bit.
There is no difference based on the language the file is written in.
The C and Fortran interfaces were designed to adapt to all the
conventions of the corresponding languages, including subscript order.

As I noted in an earlier email, I'll be on vacation for the next two
weeks, so if you have additional questions about this, please send
them to address@hidden.  Thanks.

--Russ

_____________________________________________________________________

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