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

[netCDF #KJN-301947]: Writing a Subset of a 3-D Array to a NetCDF File



Ben,

> What I would really like to see from your examples is this:
> 1. Define an array of values, "data_out", with dimensions NX=3, NY=4
> and NZ =5
> 2. Output a subset of "data_out", e.g. NX=3, NY=2, NZ=5, without
> defining a new array, such as "slab3"
>
> Is this possible?

Yes, see below.  I agree that this makes the example clearer for the
purposes of illustrating one common use of the varm functions.

--Russ
C     Example that writes a 3D array of sample data, then rewrites a
C     subset of that data, to show that elements not in the subset are
C     not overwritten.

      program overwrite3d
      implicit none
      include 'netcdf.inc'

      character*(*) FILE_NAME
      parameter (FILE_NAME='overwrite3d.nc')

      integer NDIMS
      parameter (NDIMS=3)
      integer NX, NY, NZ
      parameter (NX = 3, NY = 4, NZ = 5)
      integer ncid, varid, dimids(NDIMS)
      integer x_dimid, y_dimid, z_dimid
      integer x, y, z, i, ret
      integer start(NDIMS), count(NDIMS), stride(NDIMS), map(NDIMS)

C     Data arrays of different shapes to be written
      integer data_out(NZ, NY, NX)

      do x = 1, NX
         do y = 1, NY
            do z = 1, NZ
               data_out(z, y, x) = 11
            end do
         end do
      end do

      ret = nf_create(FILE_NAME, NF_CLOBBER, ncid)
      if (ret .ne. nf_noerr) call handle_err(ret)

      ret = nf_def_dim(ncid, "x", NX, x_dimid)
      if (ret .ne. nf_noerr) call handle_err(ret)
      ret = nf_def_dim(ncid, "y", NY, y_dimid)
      if (ret .ne. nf_noerr) call handle_err(ret)
      ret = nf_def_dim(ncid, "z", NZ, z_dimid)
      if (ret .ne. nf_noerr) call handle_err(ret)

      dimids(1) = z_dimid
      dimids(2) = y_dimid
      dimids(3) = x_dimid

      ret = nf_def_var(ncid, "data", NF_INT, NDIMS, dimids, varid)
      if (ret .ne. nf_noerr) call handle_err(ret)

      ret = nf_enddef(ncid)
      if (ret .ne. nf_noerr) call handle_err(ret)

      ret = nf_put_var_int(ncid, varid, data_out)
      if (ret .ne. nf_noerr) call handle_err(ret)

C  Now
C
C  data =
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11,
C   11, 11, 11, 11, 11 ;

C Use mapped put to overwrite middle 6 elements of each Y-Z slab with
C 2's
      start(1) = 2
      start(2) = 2
      start(3) = 1
      count(1) = 3
      count(2) = 2
      count(3) = NX
C Just use default strides in this example
      stride(1) = 1
      stride(2) = 1
      stride(3) = 1
C Distance between successive elements along axes in memory of data_out array
      map(1) =     1
      map(2) =   5*1
      map(3) = 4*5*1

      i = 21
      do x = 1, NX
         do y = 1, NY
            do z = 1, NZ
               data_out(z, y, x) = i
               i = i+1
            end do
         end do
      end do
C Just overwrite the 3x2x5 subset of elements starting at the
C data_out(2,2,1) element
      ret = nf_put_varm_int(ncid, varid, start, count, stride,
     *                      map, data_out(2, 2, 1))

C Now
C
C  data =
C   11, 11, 11, 11, 11,
C   11, 27, 28, 29, 11,
C   11, 32, 33, 34, 11,
C   11, 11, 11, 11, 11,
C
C   11, 11, 11, 11, 11,
C   11, 47, 48, 49, 11,
C   11, 52, 53, 54, 11,
C   11, 11, 11, 11, 11,
C
C   11, 11, 11, 11, 11,
C   11, 67, 68, 69, 11,
C   11, 72, 73, 74, 11,
C   11, 11, 11, 11, 11 ;

      ret = nf_close(ncid)
      if (ret .ne. nf_noerr) call handle_err(ret)

      print *,'*** SUCCESS writing example file', FILE_NAME
      end

      subroutine handle_err(errcode)
      implicit none
      include 'netcdf.inc'
      integer errcode

      print *, 'Error: ', nf_strerror(errcode)
      stop 2
      end


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



Ticket Details
===================
Ticket ID: KJN-301947
Department: Support netCDF
Priority: Normal
Status: Closed