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

Re: subset fortran



>To: address@hidden
>From: P Flatau <address@hidden>
>Subject: Re: 20031105: subset fortran
>Organization: UCSD
>Keywords: 200311051551.hA5FpKOb002249

Piotr,

> I would like to subset an array which has dimension
> nlon by nlat AND write it to NetCDF in such a way that
> it has smaller dimensions (ndx by ndy. 
> 
> This doesn't work. 
> 
> Clearly I could do it by copying elements of larger
> array to a work array but I do not want to introduce
> work arrays. Also, I do not want to allocate arrays
> (as in F90).
>  
> Can this be done gracefully  using nf_put_vara?

Not using nf_put_vara, because the values you want to write are not
contiguous in memory.  nf_put_vara lets you access non-contiguous
array subsets on the disk, but requires that the values in memory be
contiguous.  However, I'm fairly certain you can do what you want
using nf_put_varm:

  
http://www.unidata.ucar.edu/packages/netcdf/guidef/guidef-12.html#HEADING12-687

--Russ

> Piotr
> 
> 
> 
>       program main
>       parameter (nlon=4, nlat=3)
>       real  wrk(nlon, nlat)
> C assign data
>       k=0
>       do  i=1,nlon
>         do j=1, nlat
>           k=k+1
>           wrk(i,j)=k
>           print*, i,j, wrk(i,j)
>         enddo
>       enddo
>       nlon_corner=2
>       nlat_corner=2
>       ndx=2
>       ndy=2
>       call ncsub(wrk, nlon, nlat, nlon_corner,
> nlat_corner, ndx, ndy)
>       close(lun)
>       stop
>       end
> 
>       subroutine ncsub(wrk, nlon, nlat, 
>      $                 nlon_corner, nlat_corner, ndx,
> ndy)
>       include 'netcdf.inc'
>       integer iddim(2),istart(2), icount(2)
>       real wrk(nlon, nlat)
> 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,'nlon_sub',ndx, idlon_sub)
>       ierr=nf_def_dim(idnc,'nlat_sub',ndy, idlat_sub)
>       iddim(1)=idlon_sub
>       iddim(2)=idlat_sub
>       ierr=nf_def_var(idnc,'wrk',nf_float,2,iddim,
> idwrk)
>       ierr=nf_enddef(idnc)
> C we are now in data mode
>       istart(1)=nlon_corner
>       istart(2)=nlat_corner
>       icount(1)=ndx
>       icount(2)=ndy
>       print*, ' istart ', istart
>       print*, ' icount ', icount
>       
> ierr=nf_put_vara_real(idnc,idwrk,istart,icount,wrk)
>       print*, ' ierr ', ierr
>       print*, nf_strerror(ierr)
>       ierr= nf_close(idnc)
>       stop
>       end
> 
> 
> 
> =====
> Piotr J. Flatau
> address@hidden