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

Example pages containing: tips | resources | functions/procedures

NCL: Write to netCDF

This page shows various ways you can write data to a netCDF file. There are four types of NetCDF files currently supported by NCL: classic, 64-bit offset, netCDF-4 classic, and netCDF-4. See the NetCDF FAQ for more information.

If you have a newer version of NetCDF installed on your system, you can use "ncdump -k" to determine the type of NetCDF file you have. It will output either "classic", "64-bit offset", or "netCDF-4" for the various types of NetCDF files.

Some of the examples below make use of the setfileoption procedure for setting options before you write to a file.


NetCDF-3, NetCDF-3 64-bit offset, NetCDF-4 classic

For NetCDF-3, 64-bit offset, and NetCDF-4 classic files, there are two ways to write the data.

Method 1 - INEFFICIENT, simple to use

This method is quick and easy to implement, but will take noticeably longer to execute with the more variables you have, especially if each variable has attributes and/or coordinate arrays attached. It is recommended you use the efficient method in this case.

  • Sends the data to the output file without any file predefinition
  • Speed depends on number of variables, dimension sizes of variables, presence of an UNLIMITED dimension, etc.

Method 2 - EFFICIENT, requires more coding

This method is recommended for large files and/or files with lots of variables.

  • Enter "Define Mode" prior to predefining file contents
  • Predefine the following before writing any values to the file:
    • the dimension sizes and dimension names of all variables
    • the variable names and types
    • the variable attributes, if any
  • Write the values using special (/.../) syntax to strip off metadata


Comparison of writing NetCDF files using inefficient and efficient methods

To compare how slow the inefficient method is to the efficient method, run the two sets of script below. They all require the create_netcdf_file_utils.ncl script.

These two scripts produce identical NetCDF files, using 20 hard-coded variables "var01" through "var20". The timings were done on a MacBookPro.

  Inefficient script: create_netcdf_file_ineff.ncl
  Timing: 125.91 CPU seconds

  Efficient script: create_netcdf_file_eff.ncl
  Timing: 21.31 CPU seconds

These two scripts also produce identical NetCDF files. You can specify how many variables you want to write by changing the nvars variable.

  Inefficient script: create_netcdf_file_ineff_nvars.ncl
  Timing: 114.55 CPU seconds

  Efficient script: create_netcdf_file_eff_nvars.ncl
  Timing: 55.04 CPU seconds


NetCDF-4

Here are some examples on creating NetCDF-4 files:

  1. Write one unlimited dimension (large) variable
  2. Write variable with multiple unlimited dimensions
  3. Write NetCDF4 data with groups
  4. Write VLEN (Variable LENgth array) data
  5. Write ENUM data
  6. Write STRING(s)
  7. Write OPAQUE data
  8. Write COMPOUND data