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

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

 fn = "$NCARGTEST/nclscripts/nc4_files/enum.nc"

 fori = addfile(fn, "r")

;print(fori)
;printVarSummary(fori)

 ori_v = fori->primary_cloud

 enum_name = ori_v@enum_name
 enum_value = ori_v@enum_value

;print(ori_v)

;print(enum_name)
;print(enum_value)

 ori_nv = dimsizes(ori_v)
 
;print(ori_nv)



Create a file to write NetCDF4 string data.
;===================================================================
 setfileoption("nc", "Format",  "NetCDF4")
 fn = "ncl_wrt_enum.nc"
 system("/bin/rm -f " + fn)
 fo = addfile(fn, "c")



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

 fileattdef(fo, fAtt)



Define dimensions:
;===================================================================
 nvals   = dimsizes(ori_v)
 dimNames = (/"station"/)
 dimSizes = (/ nvals   /)
 dimUnlim = (/ False    /)
 filedimdef(fo, dimNames, dimSizes, dimUnlim)



Define the variable:
;===================================================================
 var_name = "primary_cloud"
 fileenumdef(fo, "enum_var", var_name, dimNames, enum_name, enum_value)



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

 delete(fo)




This script produced (use "ncl_filedump ncl_wrt_enum.nc"):
Variable: f
Type: file
filename:       ncl_wrt_enum
path:   ncl_wrt_enum.nc
User Defined Types:
    enum_var
    {
        Clear,
        Cumulonimbus,
        Stratus,
        Stratocumulus,
        Cumulus,
        Altostratus,
        Nimbostratus,
        Altocumulus,
        Cirrostratus,
        Cirrocumulus,
        Cirrus,
        Missing,
    };

attributes:
    creation_date       :       Mon Jan 28 15:31:46 MST 2013
    Conventions :       None
    source_file :       ncl_wrt_enum.nc
    title       :       NCL generated netCDF file with enum

dimensions:
    station     = 5

variables:
    primary_cloud:        [ 5  ]
        attributes:
            _FillValue  :       0