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

Re: writing doubles to netcdf



>To: address@hidden
>From: Ben Foster <address@hidden>
>Subject: Re: 20020611: writing doubles to netcdf
>Organization: HAO

Hi Ben,

> I have a question re writing 8-byte reals to netcdf files (NF_DOUBLE).
> I am porting some old cray code to IBM and in the process must read
> cray-blocked binary files and write equivalent netcdf files for the
> IBM to read.
> 
> In one file, there are some mixed-type records, so I am reading (and
> writing) from SGI (dataproc), so I can use ffio "assign -F cos -N cray" 
> to read the cray file. This is working fine. My problem arises in writing
> to the netcdf file.
> 
> Here is how I am compiling on dataproc:
> 
> FC = f90
> FFLAGS= -I/usr/local/include -r4 -64 -mips4 -ftpp -DSGI
> LIBS= -L/usr/local/lib_mips4_r4 -L /usr/local/lib64/r4i4 -lhlu -lncarg \
>       -lncarg_gks -lncarg_c -lX11 -lm -lmss -lnetcdf -lncaru
> LDFLAGS = -64

Was the netcdf library that you are linking to also compiled with
similar flags?  If it was compiled with "-r8", for example, you would
have problems similar to what you are seeing.

> So I am reading from the cray file into 4-byte reals, and they look
> ok when I write them to stdout. I want to write 8-byte reals to the
> netcdf file, so I declare kind=8 and assign the 8-byte reals from the
> 4-byte reals that I've read. Again, printing the 8-byte reals to stdout
> look fine. 

This sounds OK, but is unnecessary, since netCDF can do the
conversion, writing 4-byte values into an 8-byte netCDF variable doing
the conversion from single precision to double-precision on the fly.
If the netCDF variable is defined as NF_DOUBLE and you use
nf_put_var_float() to write float values to it, they get converted to
double inside the library, as they are being written.

> Then I define the var as NF_DOUBLE:
> 
>       ids1(1) = id_nsigt ! nsigt==6
>       istat = nf_def_var(ncid,"sigtn",NF_DOUBLE,1,ids1,id_sigtn)
> 
> (the ncdump shows this variable definition correctly)      

This looks OK.  I'm assuming that you're not using an unlimited
dimension here, since you comment that nsigt==6.  If the dimension
were unlimited, you would have to know that the number of records in
the output file matched the number of values you were writing ...

> Then, after exiting define mode, I give values of the 8-byte reals:
> 
>       istat = nf_put_var_double(ncid,id_sigtn,sigtn8)
> 
> There is no error, but when I do an ncdump, the values are garbage:
> 
> data:
> 
>  sigtn = 5.62950066300518e+15, 3.15252019172475e+16, 1.6212962284929e+17, 0, 
>     2.12199579096527e-314, 0 ;
> 
> Can you see what I'm doing wrong?  I can send the code if you need
> to see it. Thanks,

Presumably "sigtn8" is the name of your array of 8-byte reals that you
printed out before.  Also I assume you closed the file properly with
nf_close() before trying to ncdump it to look at the contents.  If
those assumptions are true, then I don't see anything wrong and would
have to see the code to try to reproduce the problem here.  If you
could create a small example that demonstrated the problem independent
of any Cray I/O libraries or ncarg libraries, it would help, since we
don't have those installed here.

--Russ

_____________________________________________________________________

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