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 string 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 string data.

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

 fn = "$NCARGTEST/nclscripts/nc4_files/"

 fori= addfile(fn, "r")


 strs = fori->universal_declaration_of_human_rights


Create a file to write NetCDF4 string 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 4 file"
 fAtt@source_file   = fn
 fAtt@Conventions   = "None"
 fAtt@creation_date = systemfunc("date")


 fileattdef(fo, fAtt)

Define dimensions:
 nstrs    = dimsizes(strs)
 dimNames = (/"Sentence"/)
 dimSizes = (/ nstrs    /)
 dimUnlim = (/ False    /)
 filedimdef(fo, dimNames, dimSizes, dimUnlim)

Define the variable:
 strsdims = getvardims(strs)

 strsdims = "Sentence"

 filevardef(fo, "nc4_string", typeof(strs), strsdims)

Write the compound data out, and close the file:
 fo->nc4_string = (/strs/)


This script produced (use "ncl_filedump"):
Variable: f
Type: file
filename:       nc4_wrt_string
    creation_date       :       Mon Jan 28 10:21:01 MST 2013
    Conventions :       None
    source_file :
    title       :       NCL generated netCDF 4 file

    Sentence    = 30

    nc4_string:         [ 30  ]