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

20031106: subset fortran



Piotr,

> Date:    Wed, 05 Nov 2003 15:13:31 -0800
> From:    P Flatau <address@hidden>
> To:      Russ Rew <address@hidden>
> Subject: Re: subset fortran 

The above message contained the following:

>      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=1
>       nlat_corner=1
>       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),
> istride(2),imap(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
>       istride(1)=nlon
>       istride(2)=1
>       imap(1)=1
>       imap(2)=1
>       print*, ' istart ', istart
>       print*, ' icount ', icount
>      
> ierr=nf_put_varm_real(idnc,idwrk,istart,icount,istride,imap,wrk)
>       print*, ' ierr ', ierr
>       print*, nf_strerror(ierr)
>       ierr= nf_close(idnc)
>       stop
>       end

Try this:

      subroutine ncsub(wrk, nlon, nlat, 
     $                 nlon_corner, nlat_corner, ndx, ndy)
      include 'netcdf.inc'
      integer iddim(2),istart(2), icount(2), istride(2),imap(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
      istride(1)=1      ! was: istride(1) = nlon
      istride(2)=1
      imap(1)=1
      imap(2)=nlon      ! was: imap(2) = 1
      print*, ' istart ', istart
      print*, ' icount ', icount
     
      ierr=nf_put_varm_real(idnc,idwrk,istart,icount,istride,imap,wrk)
      print*, ' ierr ', ierr
      print*, nf_strerror(ierr)
      ierr= nf_close(idnc)
      stop
      end

istride(1)
    is the amount by which the "lon" index is incremented when
    iterating through the array (=1).
imap(1)
    is the distance between adjacent elements in the "lon" dimension 
    (=1).
imap(2)
    is the distance between adjacent elements in the "lat" dimension 
    (=nlon).

Please let me know if this helps.

> Piotr J. Flatau
> address@hidden

Regards,
Steve Emmerson