NCL Website header
NCL Home> Application examples> File IO || Data files for some examples

Example pages containing: tips | resources | functions/procedures > output netCDF

Output netCDF with multiple unlimited dimensions


Support for NetCDF-4 files is in beta-testing in V6.1.1.
We encourage people to test it out and give us feedback on our ncl-talk email list.

Below is a script which creates a two unlimited dimension variable and then expands it in both dimensions.

;Based on code for JIRA-1603.
;This script also serves the purpose of test its correctness.
;load "$NCARGTEST/nclscripts/functions.ncl"
;------------------------------------------------------------
;Make sure we set the file output as NetCDF4.
 setfileoption("nc", "Format",  "NetCDF4")
 fn = "nc4_multiudim.nc"
 system("/bin/rm -f " + fn) ; remove if exists
 f = addfile(fn, "c")

;===================================================================
; explicitly declare file definition mode. Improve efficiency.
 setfileoption(f,"DefineMode",True)

;Create some file attributes
 fAtt               = True            ; assign file attributes
 fAtt@title         = "NCL generated netCDF file with multiple unlimited dimensions"
 fAtt@source_file   = fn
 fAtt@creation_date = systemfunc ("date")
 fileattdef(f, fAtt)

;We want to create a variable of two dimensions:
; 0: use time as dimension
; 1: use space as dimension
;Define some parameters for dimension.
 nd = 2
 nt = 5
 ns = 5
 nt2 = 2 * nt
 ns2 = 2 * ns
;===================================================================
;Define the file dimensions, NOTE that both dimensions are unlimited.
 dimNames = (/"time", "space"/)
 dimSizes = (/    -1,     -1/)
 dimUnlim = (/  True,   True/)
 filedimdef(f, dimNames, dimSizes, dimUnlim)

;===================================================================
;Generate time and space coordinate variable.
 time = ispan(1, nt, 1)
 time@name = "time"
 time!0 = "time"
 filevardef(f, "time", typeof(time), getvardims(time))
 filevarattdef(f,"time", time)

 space = ispan(1, ns, 1)
 space@name = "space"
 space!0 = "space"
 filevardef(f, "space", typeof(space), getvardims(space))
 filevarattdef(f,"space", space)

;===================================================================
;Make a Fibonacci series (somehow in 2d)
;Where: value(0, 0) = 1
;       value(1, 0) = 1
;       value(0, 1) = 1
;       value(0, i) = value(0, i-1) + value(0, i-2) (i > 1)
;       value(j, 0) = value(j-1, 0) + value(j-2, 0) (j > 1)
;       value(j, i) = value(i-1, j) + value(i, j-1) (i > 0, j > 0)

 value = new((/nt, ns/), integer)
 value@name = "value"
 value!0 = "time"
 value!1 = "space"
 filevardef(f, "value", typeof(value), getvardims(value))
 filevarattdef(f,"value", value)

 value(0, 0) = 1
 value(1, 0) = 1
 value(0, 1) = 1

 do i = 2, ns - 1
    value(0, i) = value(0, i-1) + value(0, i-2)
 end do

 do j = 2, nt - 1
    value(j, 0) = value(j-1, 0) + value(j-2, 0)
 end do

 do j = 1, nt - 1
 do i = 1, ns - 1
    value(j, i) = value(j-1, i) + value(j, i-1)
 end do
 end do

;Write the data out.
 f->time  = (/time/)
 f->space = (/space/)
 f->value = (/value/)
;===================================================================

The above script has created a variable with two unlimited dimensions. We are going to expand this variable in space and time directions (The following script are continuing from above).

Expand in space first:


;===================================================================
;Extend is space:
;The ":=" is called reassignment, which is available in
;version 6.1.1 or later.
;See: http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#Reassignment
;for more details.
 space := ispan(1, ns2, 1)
 f->space(ns:ns2-1) = (/space(ns:ns2-1)/)

 value1 = new((/nt, ns2/), integer)
 value1@name = "value"
 value1!0 = "time"
 value1!1 = "space"

;printVarSummary(value1)

 value1(:, :ns-1) = value(:, :ns-1)

 do i = ns, ns2 - 1
    value1(0, i) = value1(0, i-1) + value1(0, i-2)
 end do

 do j = 1, nt - 1
 do i = ns, ns2 - 1
    value1(j, i) = value1(j-1, i) + value1(j, i-1)
 end do
 end do

;Write
 f->value(:, ns:ns2-1) = (/value1(:, ns:ns2-1)/)
;===================================================================

The above script has created a variable with two unlimited dimensions. Expand in time:


;===================================================================
;Extend is time:

 time := ispan(1, ns2, 1)
 f->time(nt:nt2-1) = (/time(nt:nt2-1)/)

 value2 = new((/nt2, ns2/), integer)
 value2@name = "value"
 value2!0 = "time"
 value2!1 = "space"

;printVarSummary(value2)

 value2(:nt-1, :) = value1(:nt-1, :)

 do j = nt, nt2 - 1
    value2(j, 0) = value2(j-1, 0) + value2(j-2, 0)
 end do

 do j = nt, nt2 - 1
 do i = 1, ns2 - 1
    value2(j, i) = value2(j-1, i) + value2(j, i-1)
 end do
 end do

 f->value(nt:nt2-1, :) = (/value2(nt:nt2-1, :)/)

 delete(f)

;Uncomment the following line (and the load line at top) to test.
;f2 = addfile(fn, "r")
;value := f2->value
;check_values("Check", "nc4_multipleunlimiteddims", value2, value, True, True, value@_FillValue)

This script produced (use "ncdump nc4_multiudim.nc"):
netcdf nc4_multiudim {
dimensions:
        time = UNLIMITED ; // (10 currently)
        space = UNLIMITED ; // (10 currently)
variables:
        int time(time) ;
                string time:name = "time" ;
        int space(space) ;
                string space:name = "space" ;
        int value(time, space) ;
                string value:name = "value" ;
                value:_FillValue = -2147483647 ;

// global attributes:
                string :creation_date = "Tue Jan 15 13:35:23 MST 2013" ;
                string :source_file = "nc4_multiudim.nc" ;
                string :title = "NCL generated netCDF file with multiple unlimited dimensions" ;
data:

 time = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;

 space = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;

 value =
  1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
  1, 2, 4, 7, 12, 20, 33, 54, 88, 143,
  2, 4, 8, 15, 27, 47, 80, 134, 222, 365,
  3, 7, 15, 30, 57, 104, 184, 318, 540, 905,
  5, 12, 27, 57, 114, 218, 402, 720, 1260, 2165,
  8, 20, 47, 104, 218, 436, 838, 1558, 2818, 4983,
  13, 33, 80, 184, 402, 838, 1676, 3234, 6052, 11035,
  21, 54, 134, 318, 720, 1558, 3234, 6468, 12520, 23555,
  34, 88, 222, 540, 1260, 2818, 6052, 12520, 25040, 48595,
  55, 143, 365, 905, 2165, 4983, 11035, 23555, 48595, 97190 ;
}