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

Re: 20031104: netcdf



>To: address@hidden
>From: P Flatau <address@hidden>
>Subject: netcdf
>Organization: UCAR/Unidata
>Keywords: 200310291816.h9TIGNOb017725

Hi Piotr,

> I am trying to convert from double precision to single
> precision when using call to nf_vara_real. This
> doesn't work. 
>
> Is there a way of writting double precison variable
> "wrk" (real*8) as single precision to a netcdf file?

If you instead use nf_vara_double it will work fine.  The name of the
type in the function call is the type of values you are giving the put
function.  It already knows the type of variable in the netCDF file,
and will convert from the type you give it (in your case real*8, which
is the same as double) to the type of variable in the file (nf_float).
So just use the statement

      ierr=nf_put_vara_double(idnc,idwrk,istart,icount,wrk)

and I think you will get what you expect.

--Russ

> Is there a way of writting double precison variable
> "wrk" (real*8) as single precision to a netcdf file?
> 
> Piotr
> 
> 
> 
> 
>       program main
>       include 'netcdf.inc'
>       parameter (maxi=30, maxj=20)
>       real*8  wrk(maxi, maxj)
>       integer iddim(2),istart(2), icount(2)
>       lun=10
> C assign data
>       k=0
>       do  i=1,maxi
>         do j=1, maxj
>           wrk(i,j)=float(k+1)+0.784
>           k=k+1
>         enddo
>       enddo
> 
>       open(unit=lun,file='out.bin', 
>      $              form='unformatted',
> status='unknown')
>       write(lun) icdtg,timemodel
>       write(lun) wrk
>       close(lun)
> C open netcdf file 'out.nc'
>       ierr=nf_create('out.nc',nf_clobber,idnc)
> C we are now in define mode; define dimensions
>       ierr=nf_def_dim(idnc,'dimi',maxi, idi)
>       ierr=nf_def_dim(idnc,'dimj',maxj,idj)
>       iddim(1)=idi
>       iddim(2)=idj
>       ierr=nf_def_var(idnc,'wrk',nf_float,2,iddim,
> idwrk)
>       ierr=nf_enddef(idnc)
> C we are now in data mode
>       istart(1)=1
>       istart(2)=1
>       icount(1)=maxi
>       icount(2)=maxj
>      
> ierr=nf_put_vara_real(idnc,idwrk,istart,icount,wrk)
>       print*, ' define wrk ', ierr
>       ierr= nf_close(idnc)
>       stop
>       end