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

Re: examples



>To: address@hidden
>From:  "Reula, Oscar" <address@hidden>
>Subject: Re: 20020910: subject
>Organization: FaMAF, Univ. Nac. de Cordoba, 5000 Cordoba, ARGENTINA
>Keywords: 200210031824.g93IO1129487 nc_close

Hi Oscar,

> Hi, I would like to dump data on netcdf files from my c program
> simulations. I have tried for a couple of days, but it seems
> I run into problems which I can not debug. Everything runs, and
> get to create files and put data on it, but ncdump will not
> show it.

It sounds like you might not be calling nc_close(ncid) after you are
done writing, or at least nc_sync(ncid) after you are done writing
each record.  This is necessary to flush the data to disk, and these
won't automatically be called on exit unless you register a function
that calls them with atexit().

> Is there a C example in writing data to a file? In my case I have
> 3-d regular grided data which is computed at different times and
> I have to dump it every time step into the file.

Here's a simple example creating a netCDF dataset in C:

  http://www.unidata.ucar.edu/packages/netcdf/examples/create.c

You'll notice it calls nc_close() at the end.

Another way to get an example is to create a CDL file, such as

<example file="example1.cdl">
netcdf example1 {
dimensions:
        lat = 4 ;
        lon = 3 ;
        frtime = UNLIMITED ; // (2 currently)
        timelen = 20 ;
variables:
        float P(frtime, lat, lon) ;
                P:long_name = "pressure at maximum wind" ;
                P:units = "hectopascals" ;
                P:valid_range = 0.f, 1500.f ;
                P:_FillValue = -9999.f ;
        float lat(lat) ;
                lat:long_name = "latitude" ;
                lat:units = "degrees_north" ;
        float lon(lon) ;
                lon:long_name = "longitude" ;
                lon:units = "degrees_east" ;
        int frtime(frtime) ;
                frtime:long_name = "forecast time" ;
                frtime:units = "hours" ;
        char reftime(timelen) ;
                reftime:long_name = "reference time" ;
                reftime:units = "text_time" ;
// global attributes:
                :history = "created by Unidata LDM from NPS broadcast" ;
                :title = "NMC Global Product Set: Pressure at Maximum Wind" ;
data:

 P =
  950, 951, 952,
  953, 954, 955,
  956, 957, 958,
  959, 960, 961,
  962, 963, 964,
  965, 966, 967,
  968, 969, 970,
  971, 972, 973 ;

 lat = -90, -87.5, -85, -82.5 ;

 lon = -180, -175, -170 ;

 frtime = 12, 18 ;

 reftime = "1992 03 04 12:00" ;
}
</example>

Then create a C program that generates the corresponding netCDF file
by invoking the ncgen program:

 ncgen -c example1.cdl > example1.c

--Russ

_____________________________________________________________________

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