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

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

Write netCDF4 vlen(variable LENgth array) data

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 demonstrates how to read/write NetCDF4 vlen data.

Read NetCDF4 vlen data from a file generated from NetCDF vlen example code.

 fn = "$NCARGTEST/nclscripts/nc4_files/"

 fa = addfile(fn, "r")


 var = fa->var

 ne = ListCount(var)
 print("Number of Elements = " + ne)


Create a file to write NetCDF4 vlen data.
 setfileoption("nc", "format", "NETCDF4")
 fn = ""
 system("/bin/rm -f " + fn)
 fo = addfile(fn, "c")

Define some file attributes:
 fAtt               = True
 fAtt@title         = "NCL generated netCDF file"
 fAtt@source_file   = fn
 fAtt@Conventions   = "None"
 fAtt@creation_date = systemfunc("date")

 fileattdef(fo, fAtt)

Define dimensions:
 dimNames = (/"vldim"/)
 dimSizes = (/ ne /)
 dimUnlim = (/ False /)
 filedimdef(fo, dimNames, dimSizes, dimUnlim)

Define the variable:
 vlen_name = "vlen"
 var_name = "var"
 filevlendef(fo, vlen_name, var_name, "integer", dimNames)


Write the compound data out, and close the file:
 fo->$var_name$ = (/var/)


This script produced (use "ncl_filedump"):
Variable: f
Type: file
filename:       nc4_vl
User Defined Types:

Number of Attributes:4
    creation_date       :       Tue Nov  5 13:32:18 MST 2013
    Conventions :       None
    source_file :
    title       :       NCL generated netCDF4 file

    vldim       = 3

    Variable: var
    Type: list
    Total Size: 3 values
                12 bytes
    Number of Dimensions: 1
    Dimensions and sizes:       [ 3  ]