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

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

Output 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/compound.nc"
 fi = addfile(fin, "r")

;print(fi)
;printVarSummary(fi)

 sf_id = fi->/dimension_data.starfleet_id

;print(sf_id)
;printVarSummary(sf_id)

 abili = fi->/dimension_data.abilities

;print(abili)
;printVarSummary(abili)

 delete(fi)



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

 fon = "ncl_wrt_comp_dat.nc"
 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")

;print(fAtt)

 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)
 printVarSummary(complist)

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

;printVarSummary(complist)



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

 delete(fo)




This script produced (use "ncl_filedump ncl_wrt_comp_dat.nc"):
Variable: f
Type: file
filename:       ncl_wrt_comp_dat
path:   ncl_wrt_comp_dat.nc
User Defined Types:
    compound_var
    {
        starfleet_id,
        abilities,
    };

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

dimensions:
    starfleet   = 100

variables:
    nc4_compound_data:    [ 100  ]