Creating NetCDF Files

The following IDL commands should be used to create a new netCDF file:

NCDF_CREATE: Call this procedure to begin creating a new file. The new file is put into define mode.

NCDF_DIMDEF: Create dimensions for the file.

NCDF_VARDEF: Define the variables to be used in the file.

NCDF_ATTPUT: Optionally, use attributes to describe the data.

NCDF_CONTROL, /ENDEF: Call NCDF_CONTROL and set the ENDEF keyword to leave define mode and enter data mode.

NCDF_VARPUT: Write the appropriate data to the netCDF file.

NCDF_CLOSE: Close the file.

Reading NetCDF Files

The following commands should be used to read data from a netCDF file:

NCDF_OPEN: Open an existing netCDF file.

NCDF_INQUIRE: Call this function to find the format of the netCDF file.

NCDF_DIMINQ: Retrieve the names and sizes of dimensions in the file.

NCDF_VARINQ: Retrieve the names, types, and sizes of variables in the file.

NCDF_ATTNAME: Optionally, retrieve attribute names.

NCDF_ATTINQ: Optionally, retrieve the types and lengths of attributes.

NCDF_ATTGET: Optionally, retrieve the attributes.

NCDF_VARGET: Read the data from the variables.

NCDF_CLOSE: Close the file.

If the structure of the netCDF file is already known, the inquiry routines do not need to be called--only NCDF_OPEN, NCDF_ATTGET, NCDF_VARGET, and NCDF_CLOSE would be needed.

NetCDF Examples

Two example files that demonstrate the use of the netCDF routines can be found in the hdf_ncdf subdirectory of the examples subdirectory of the IDL distribution. The file nncdf_cat.pro prints a summary of basic information about a netCDF file. The file ncdf_rdwr.pro creates a new netCDF file and then reads the information back from that file.

A Complete Example with Unlimited Dimensions

The following example shows how to create a netCDF file, populate it with data, read data from the file, and make a simple plot from the data.The resulting graphic is shown in SHOW3 Result of Unlimited Dimensions Example .

id = NCDF_CREATE('inquire.nc', /CLOBBER)
; Create a new NetCDF file with the filename inquire.nc.

NCDF_CONTROL, id, /FILL ; Fill the file with default values.

hours = INDGEN(5) ; We'll create some time-dependant data, so here is an array of hours from 0 to 5.

data = FLTARR(5,10) ; Create a 5 by 10 array to hold floating-point data.

FOR i=0,9 DO $ ; Generate some values.

    data(*,i) = (i+0.5) * EXP(-hours/2.) / SIN((i+1)/30.*!PI)

xid = NCDF_DIMDEF(id, 'x', 10) ; Make dimensions.

zid = NCDF_DIMDEF(id, 'z', /UNLIMITED)

hid = NCDF_VARDEF(id, 'Hour', [zid], /SHORT)
; Define variables.

vid = NCDF_VARDEF(id, 'Temperature', [xid,zid], /FLOAT)
; Define variable.

NCDF_ATTPUT, id, vid, 'units', 'Degrees x 100 F'

NCDF_ATTPUT, id, vid, 'long_name', 'Warp Core Temperature'

NCDF_ATTPUT, id, hid, 'long_name', 'Hours Since Shutdown'

NCDF_ATTPUT, id, /GLOBAL, 'Title', 'Really important data'

NCDF_CONTROL, id, /ENDEF ; Put file in data mode.

NCDF_VARPUT, id, hid, hours ; Input data.

FOR i=0,4 DO NCDF_VARPUT, id, vid, $

    REFORM(data(i,*)), OFFSET=[0,i]; Oops! We forgot the 6th hour! This is not a problem, however, as you can dynamically expand a netCDF file if the unlimited dimension is used.

NCDF_VARPUT, id, hid, 6, OFFSET=[5] ; Add the hour and data.

NCDF_VARPUT, id, vid, FINDGEN(10)*EXP(-6./2), OFFSET=[0,5]
; Add the temperature.

NCDF_VARGET, id, vid, output_data ; Read the data back out.

NCDF_ATTGET, id, vid, 'long_name', ztitle

NCDF_ATTGET, id, hid, 'long_name', ytitle

NCDF_ATTGET, id, vid, 'units', subtitle

!P.CHARSIZE = 2.5

!X.TITLE = 'Location'

!Y.TITLE = STRING(ytitle) ; Convert from bytes to strings.

!Z.TITLE = STRING(ztitle) + '!C' + STRING(subtitle)

NCDF_CLOSE, id ; Close the NetCDF file.

SHOW3, output_data ; Display the data.