Previous: Setting the Chunksizes in NetCDF-4 Next: The Cache in NetCDF-4 Table of contents Frames User guide
2008 Unidata NetCDF Workshop for Developers and Data Providers > Chunking Data with NetCDF-4

12.3 Example of Setting Chunksizes in Fortran 90
In this example code (from nf_test/f90tst_vars.f90) the chunksize is set for a 2-dimensional variable in Fortran 90.

 


  ! This is the name of the data file we will create.
  character (len = *), parameter :: FILE_NAME = "f90tst_vars.nc"

  ! We are writing 2D data, a 6 x 12 grid. 
  integer, parameter :: MAX_DIMS = 2
  integer, parameter :: NX = 6, NY = 12
  integer :: ncid, varid, dimids(MAX_DIMS), chunksizes(MAX_DIMS), chunksizes_in(MAX_DIMS)
  integer :: x_dimid, y_dimid, contig
  integer :: data_out(NY, NX), data_in(NY, NX)
  integer :: mode_flag
  integer :: nvars, ngatts, ndims, unlimdimid, file_format
  integer :: x, y, retval

  print *,'*** Testing definition of netCDF-4 vars from Fortran 90.'

  ! Create some pretend data.
  do x = 1, NX
     do y = 1, NY
        data_out(y, x) = (x - 1) * NY + (y - 1)
     end do
  end do

  ! Create the netCDF file. 
  mode_flag = IOR(nf90_netcdf4, nf90_classic_model) 
  retval = nf90_create(FILE_NAME, mode_flag, ncid)
  if (retval /= nf90_noerr) call handle_err(retval)

  ! Define the dimensions.
  retval = nf90_def_dim(ncid, "x", NX, x_dimid)
  if (retval /= nf90_noerr) call handle_err(retval)
  retval = nf90_def_dim(ncid, "y", NY, y_dimid)
  if (retval /= nf90_noerr) call handle_err(retval)
  dimids =  (/ y_dimid, x_dimid /)

  ! Define the variable. 
  retval = nf90_def_var(ncid, "data", NF90_INT, dimids, varid)
  if (retval /= nf90_noerr) call handle_err(retval)


  ! Set up chunking.
  chunksizes = (/ NY, NX /)
  retval = nf90_def_var_chunking(ncid, varid, 0, chunksizes)
  if (retval /= nf90_noerr) call handle_err(retval)


  ! With classic model netCDF-4 file, enddef must be called.
  retval = nf90_enddef(ncid)
  if (retval /= nf90_noerr) call handle_err(retval)

  ! Write the pretend data to the file.
  retval = nf90_put_var(ncid, varid, data_out)
  if (retval /= nf90_noerr) call handle_err(retval)

  ! Close the file. 
  retval = nf90_close(ncid)
  if (retval /= nf90_noerr) call handle_err(retval)

 


Previous: Setting the Chunksizes in NetCDF-4 Next: The Cache in NetCDF-4 Table of contents Frames User guide
2008 Unidata NetCDF Workshop for Developers and Data Providers > Chunking Data with NetCDF-4