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
Example Code
Two example files that demonstrate the use of the netCDF routines can be found in the examples/doc/sdf subdirectory of the IDL distribution. The file ncdf_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. Run these example procedures by entering ncdf_cat or ncdf_rdwr at the IDL command prompt or view the files in an IDL Editor window by entering .EDIT ncdf_cat.pro or .EDIT ncdf_rdwr.pro.
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 below.
; Create a new NetCDF file with the filename inquire.nc: id = NCDF_CREATE('inquire.nc', /CLOBBER) ; Fill the file with default values: NCDF_CONTROL, id, /FILL ; We'll create some time-dependent data, so here is an ; array of hours from 0 to 5: hours = INDGEN(5) ; Create a 5 by 10 array to hold floating-point data: data = FLTARR(5,10) ; Generate some values. FOR i=0,9 DO $ 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) ; Define variables: hid = NCDF_VARDEF(id, 'Hour', [zid], /SHORT) vid = NCDF_VARDEF(id, 'Temperature', [xid,zid], /FLOAT) 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' ; Put file in data mode: NCDF_CONTROL, id, /ENDEF ; Input data: NCDF_VARPUT, id, hid, hours FOR i=0,4 DO NCDF_VARPUT, id, vid, $ ; 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. REFORM(data(i,*)), OFFSET=[0,i] ; Add the hour and data: NCDF_VARPUT, id, hid, 6, OFFSET=[5] ; Add the temperature: NCDF_VARPUT, id, vid, FINDGEN(10)*EXP(-6./2), OFFSET=[0,5] ; Read the data back out: NCDF_VARGET, id, vid, output_data 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.
