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

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

 fin = "$NCARGTEST/nclscripts/nc4_files/"
 fi = addfile(fin, "r")


 sf_id = fi->/dimension_data.starfleet_id


 abili = fi->/dimension_data.abilities



Create a file to write NetCDF4 compound data.
 setfileoption("nc", "FileStructure", "Advanced")
 setfileoption("nc", "Format",  "NetCDF4")

 fon = ""
 system("/bin/rm -f " + fon)
 fo = addfile(fon, "c")

In the above code segment, there are two setfileoptions, both can active NCL to use its advanced file strucuture.

At least one option should be used, as NCL must uses advanced file structure to handle compound data.

Define some file attributes:

 fAtt               = True            ; assign file attributes
 fAtt@title         = "NCL generated netCDF file with compound"
 fAtt@source_file   = fon
 fAtt@Conventions   = "None"
 fAtt@creation_date = systemfunc("date")


 fileattdef(fo, fAtt)

Define dimensions:
 nvals   = 100
 dimNames = (/"starfleet"/)
 dimSizes = (/ nvals   /)
 dimUnlim = (/ False    /)
 filedimdef(fo, dimNames, dimSizes, dimUnlim)

Define the variable:
 var_name = "nc4_compound_data"
 mem_name = (/"starfleet_id", "abilities"/)
 mem_type = (/"integer", "integer"/)
 mem_size = (/1, 7/)
 filecompounddef(fo, "compound_var", var_name, dimNames, \
                 mem_name, mem_type, mem_size)

 complist = new(dimSizes, list)

 do n = 0, nvals - 1
    ListPush(complist[n], (/sf_id(n)/))
    ListPush(complist[n], (/abili(n,:)/))
 end do


Write the compound data out, and close the file:
 filewritecompound(fo, "compound_var", var_name, mem_name, complist)


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

    creation_date       :       Mon Jan 28 08:58:40 MST 2013
    Conventions :       None
    source_file :
    title       :       NCL generated netCDF file with compound

    starfleet   = 100

    nc4_compound_data:    [ 100  ]