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

Re: 20010709: time as unlimited variable



>To: address@hidden
>From: Reggie Chang <address@hidden>
>Subject: Re: 20010709: time as unlimited variable
>Organization: ZONA Technology
>Keywords: Fortran interface, unlimited variable

Hi Reggie,

> I searched the support email archive and
> found your email address in a related problem
> raised by another fellow.
> 
> Although this could be a same old problem to
> you, your (or your colleague's) assistance is
> very much appreciated.
> 
> I plan to use time as an unlimited variable, but
> had a hard time to find a good example.  Even
> with the sample code given in the User's Guide
> for FORTRAN (version 3), I cannot retrieve
> the data successfully saved to the *.nc file.
> 
> Two programs are attached.  testTime.for is the
> one to write out *.nc and it follows the sample
> codes given in UG.  I used testTime_read.for
> to get the data in *.nc but the number is not
> correct.

I compiled and ran testTime.for, and it created the file
test_unlimited.nc, but if you look at this file using the ncdump
utility, it contains no data:

 $ ncdump test_unlimited.nc
 netcdf test_unlimited {
 dimensions:
         lat = 5 ;
         lon = 9 ;
         time = UNLIMITED ; // (0 currently)
 variables:
         int rh(time, lat, lon) ;
 data:
 }

Looking at the program to explain this, I see you are using the
simplest interface, 

        status = nf_put_var_int(ncid, rhid, rhvals)

to write all the values at once.  This interface relies on figuring
our how many values to write from the shape of the netCDF variable rh,
which is currently a 0x5x9 array, since the size of the unlimited
dimension is 0 before any records have been written, so it unhelpfully
writes 0 values to the variable and returns without any error
indication.  (Note that it can't use the declared shape of the
internal Fortran variable rhvals which is 9x5x3, because that shape is
not passed to through the interface to nf_put_var_int().)

There should be a warning in the documentation for this interface not
to use it for writing record variables unless you are overwriting an
existing variable or using the same number of records as the other
record variables in the file, because it uses the current size of the
unlimited dimension in the file to determine how many records to
write.

Using instead the nf_put_vara_int() function that explicitly specifies
the number of values to write will work in this case:

        integer start(3), count(3)
        data start /1, 1, 1/
        data count /lons, lats, times/
 ...
        status = nf_put_vara_int(ncid, rhid, start, count, rhvals)
 ...

where you must declare the start and count arrays to specify which
index in each dimension to start at and how far along each dimension
to include in the slab of values to be written.  

Note also that when you created the file, you used "no clobber" mode,
so you should explicitly delete the old file before rerunning your
program to avoid an error on creation, or change the mode to "clobber"
with

        !create a database file
        status = nf_create('test_unlimited.nc', nf_clobber, ncid)
 ...

> One more thing, I cannot use the function call
> nf_strerror(status) and had to comment it out
> in the program.  Does this imply an installation
> problem?

It looks like your handle_err() subroutine needs the 

        include 'netcdf.inc'

statement added among the declarations, to inform the Fortran compiler
that the nf_strerror() function returns a character string.  Otherwise
it assumes it returns an integer and prints the resulting character
string as an integer.  This is another flaw in the documentation, which
should show the above include statement in the sample handle_err()
subroutine:

      subroutine handle_err(status)
        include 'netcdf.inc'
        integer status

        if (status .ne. nf_noerr) then
            print *, nf_strerror(status)
                print *, 'error occurs!'
            stop  'Stopped'
        end if

        end

With the above changes, I believe everything will work as you expect.

--Russ

_____________________________________________________________________

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