WOLFRAM

  • Import supports Version 3 and 4 of the netCDF format.
  • Export by default creates the enhanced data model netCDF-4.

Background & Context

    • MIME type: application/x-netcdf
    • NetCDF data file format.
    • Self-describing format for exchanging scientific data.
    • Used in atmospheric research, GIS, and related fields.
    • NetCDF is an acronym derived from network Common Data Form.
    • Binary file format.
    • Conceptually based on NASA's Common Data Format, but incompatible with this format.
    • Developed by the Unidata center at the University Corporation for Atmospheric Research (UCAR).

Import & Export

  • Import["file.nc"] imports a netCDF file, returning the names of the datasets stored in the file.
  • Import["file.nc",elem] imports the specified element from a netCDF file.
  • The import format can be specified with Import["file","NetCDF"] or Import["file",{"NetCDF",elem,}].
  • Export["file.nc",expr] exports a numeric array to netCDF.
  • Export["file.nc",{expr1,}, {"Datasets",{"dataset1",}}] creates a netCDF file, storing the data arrays {expr1,} as separate datasets.
  • Export["file.nc",expr,elem] exports the specified element to a netCDF file.
  • Export["file.nc",elem1->expr1,,"Rules"] uses rules to specify the elements to be exported.
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Structure elements:
  • "Datasets"names of all datasets
    "Groups"names of all groups
    "StructureGraph"a directed graph showing the structure of the datasets
    "StructureGraphLegend"legend for the structure graph
    "Summary"summary of properties
    "Version"netCDF version of the file
  • Names of groups and datasets are given as the absolute paths starting with the root group name "/".
  • Import by default uses the "Datasets" element for the netCDF format.
  • Data representation elements:
  • "Data"all datasets imported as an association
    {"Data",dataset} or datasetnamed dataset
  • The following basic data types are supported:
  • "Integer8"8-bit integers
    "Integer16"16-bit integers
    "Integer32"32-bit integers
    "Integer64"64-bit integers
    "UnsignedInteger8"8-bit unsigned integers
    "UnsignedInteger16"16-bit unsigned integers
    "UnsignedInteger32"32-bit unsigned integers
    "UnsignedInteger64"64-bit unsigned integers
    "Real32"IEEE singleprecision numbers
    "Real64"IEEE doubleprecision numbers
    "String"string of ASCII characters
  • The following structured data types are supported:
  • "ByteArray"a ByteArray of an arbitrary length
    "Enum"an enumeration
    "Compound"a compound dataset consisting of any other data format and other compound datasets
  • Metadata elements:
  • "Attributes"attributes of all groups and datasets
    "DataEncoding"specifies how each dataset is compressed
    "DataFormat"type used to represent each dataset
    "Dimensions"data dimensions of each dataset
    "DimensionNames"names of dimensions of each dataset
    {"metadata",dataset}metadata of the named dataset
    "Metadata"global attributes (attributes of the root group)

Export Elements

  • General Export element:
  • "Rules"a list of imported elements in the form of elemexpr
  • Export["file.nc",{elem1->expr1,},"Rules"] uses rules to specify the elements to be exported.
  • Available Export elements:
  • "Attributes"attributes associated to groups and datasets
    "Datasets"datasets and their associated elements
    "Dimensions"dimensions of the datasets
    "Groups"a list of group names
    "NamedDataTypes"named data types
  • With the "Attributes" element, the following expressions can be given:
  • attrattributes associated to the root group "/"
    {path1attr1,}attributes attri associated to the specified pathi
  • Attributes attri should be given in the form "attname"->attval.
  • With the "Datasets" element, the following expressions can be given:
  • datastores data under "Dataset1"
    {"name1"data1,}a list of dataset names and their associated data
    {"name1"ds1,}specifies each dataset dsi using a list of rules
  • Datasets dsi can take the following keys:
  • "Attributes"dataset attributes
    "Data"array of data
    "DataEncoding"one or more data encodings to use
    "DataFormat"data type
    "DimensionNames"names associated to array dimensions
  • The "NamedDataTypes" element can be a list of {"name1"type1,} where each typei can be an association of the following forms:
  • <|"Class""ByteArray",|>takes a "Length" key
    <|"Class""Compound",|>takes "Dimensions" and "Structure" keys
    <|"Class""Enumerated",|>takes "DataFormat" and "Structure"
    <|"Class""RaggedArray",|>takes "DataFormat" and "Dimensions" keys

Options

  • Export option:
  • ByteOrdering$ByteOrderingwhat byte ordering to use
    OverwriteTargetTruewhether to overwrite an existing file
    "Version""NC4 Enhanced"version of the file format to create
  • Using OverwriteTarget->"Append", new objects may be added to an existing file.
  • Possible values for "Version" are: "Classic" (Version 3), "Classic 64bit", "NC4 Classic" or "NC4 Enhanced" (Version 4).

Examples

open allclose all

Basic Examples  (3)Summary of the most common use cases

Get the names of datasets stored in a netCDF file:

Out[1]=1

Get the file summary:

Out[2]=2

Read metainformation from a netCDF file:

Out[1]=1

Export raster image data to the netCDF format:

Out[1]=1

This gives the names of the datasets stored in this file:

Out[2]=2

Scope  (13)Survey of the scope of standard use cases

Import  (7)

Show all elements available in the file:

Out[1]=1

Import, by default, returns the list of datasets in the file:

Out[1]=1

Import the file structure, specifying the format explicitly:

Out[1]=1

Import contents of a dataset by specifying its name:

Out[1]=1

Import dimensions and data format for all datasets in the file:

Out[1]=1

Import dimensions and data format of a single dataset:

Out[1]=1

Import a dataset:

Render the imported data as graphics:

Out[2]=2

Export  (6)

Export a matrix to NetCDF:

Out[1]=1

Show the datasets contained in this file:

Out[2]=2

Export a named dataset:

Out[1]=1
Out[2]=2

Export two matrices to NetCDF:

Out[1]=1
Out[2]=2

Export a named dataset with given data type:

Out[1]=1

Import the data format:

Out[2]=2

Export a named compound data type in a group:

Out[1]=1

Import the file structure:

Out[2]=2

Export named datasets with attributes:

Out[1]=1

Import attributes of a particular group:

Out[2]=2

Import all attributes stored in a file:

Out[3]=3

Import Elements  (23)

Attributes  (3)

Import attributes of all groups and datasets in the file:

Out[1]=1

Import attributes of a specific dataset:

Out[1]=1

Import attributes of multiple objects:

Out[1]=1

Data  (3)

Get data from all datasets in the file:

Out[2]=2

Import data from the "MyData" dataset inside the "MyGroup" group:

Out[1]=1

When importing data from a single dataset, the "Data" element should not be specifically provided:

Out[2]=2

Import data from multiple datasets:

Out[1]=1

DataFormat  (2)

Get data type description for every dataset in the file:

Out[1]=1

Simple numeric and string types have one-word descriptions:

Out[1]=1

"DataFormat" for a compound type shows class and structure:

Out[2]=2

"DataFormat" for enumerated types includes class, base data format and a list of values and names:

Out[3]=3

Datasets  (2)

Import names of all datasets in a file:

Out[1]=1

"Datasets" is the default netCDF element:

Out[1]=1

Dimensions  (3)

Get dimensions of all datasets in a file:

Out[1]=1

Dimensions of data with only a single element are indicated by an empty list:

Out[1]=1
Out[2]=2

Get dimensions of a specific dataset:

Out[1]=1

DimensionNames  (2)

Get dimension names of all datasets in a file:

Out[1]=1

Get dimension names of a specific dataset:

Out[1]=1

Groups  (1)

Import names of all groups in the file:

Out[1]=1

Metadata  (2)

Get file metadata (attributes of the root group):

Out[1]=1

Metadata often carries information about data source:

Out[1]=1

NamedDimensions  (3)

Import all named dimensions in the file:

Out[2]=2

Dimensions may be unlimited:

Out[1]=1

Import named dimension to get coordinate values along this dimension:

Out[1]=1

Inspecting attributes often gives more information about the dimension:

Out[2]=2

Interpret time coordinates according to the information obtained from the attribute:

Summary  (1)

Get the file summary:

Out[1]=1

Version  (1)

Get the version of netCDF format:

Out[2]=2

NetCDF Version 4 is supported:

Out[1]=1

Export Elements  (25)

"Attributes"  (4)

Attach attributes to the root group:

Out[2]=2

Specify data format for attributes:

Out[1]=1

Export dataset attributes both in "Datasets" and "Attributes" elements:

Out[1]=1

Each attribute must have a name that is unique among all attributes attached to the same object:

Out[1]=1

Overwrite attribute value in existing file:

Out[2]=2

"Datasets"  (10)

"Datasets" is the default export element. Data format and dimensions are automatically inferred from the expression:

Out[1]=1
Out[2]=2
Out[3]=3

Export expressions into different datasets, each of which can have a full path:

Out[2]=2

Inspect the structure of the NetCDF file:

Out[3]=3

Export a dataset with a custom data format:

Out[1]=1

Export a dataset with an attribute:

Out[1]=1

Create a scalar dataset with a single integer:

Out[1]=1

Create an array of integer numbers:

Out[1]=1

Export an array of real numbers:

Out[1]=1

Create a dataset with strings:

Out[1]=1

A ByteArray is stored as a "ByteArray" type (also called the opaque type in NetCDF4):

Out[1]=1

Associations with string keys are exported as elements of a compound type:

Out[1]=1
Out[2]=2

"Groups"  (2)

NetCDF files always have a root group (with path "/"):

Out[1]=1
Out[2]=2

Export multiple groups by giving a list of paths:

Out[1]=1

"NamedDatatypes"  (3)

Export a compound named data type to a NetCDF file:

Out[24]=24
Out[4]=4

Use the previously exported type when exporting a dataset:

Out[25]=25

Import data format and dimensions of the dataset:

Out[27]=27

Inspect exported data:

Out[28]=28

Export a nested compound type:

Out[1]=1

Simple scalar types cannot be stored in a NetCDF file:

Out[1]=1

"NamedDimensions"  (5)

Every dimension of a NetCDF dataset must be stored in the file as separate entity. When dimensions' names are not explicitly specified, they will be assigned automatically generated names:

Out[1]=1
Out[2]=2

Specify dimension names explicitly:

Out[1]=1
Out[2]=2
Out[3]=3

Export dimensions explicitly in the form of "name"size, where as size means unlimited dimension:

Out[1]=1
Out[2]=2

Named dimensions can later be used when defining datasets:

Out[3]=3
Out[4]=4

Notice that the size of the "x" dimension has increased:

Out[5]=5

See the file structure:

Out[6]=6

Export a dimension with explicit coordinate values that will be stored in a dataset with the same name:

Out[1]=1
Out[2]=2

Create an unlimited dimension with only the first few coordinates specified:

Out[1]=1
Out[2]=2

"Rules"  (1)

Export two matrices using the "Rules" specification:

Out[3]=3
Out[4]=4

Export Options  (6)

OverwriteTarget  (2)

By default, a new file is created for each call to Export:

Out[1]=1

With OverwriteTargetFalse, existing files will not be overwritten:

Out[2]=2

To append datasets to an existing file, use OverwriteTarget"Append":

Out[1]=1
Out[2]=2

"Version"  (4)

By default, Export creates NetCDF4 files with enhanced data model:

Out[1]=1
Out[2]=2

Export data to classic NetCDF format:

Out[1]=1
Out[2]=2

Export data to classic NetCDF format with 64-bit offset:

Out[1]=1
Out[2]=2

Export data to NetCDF4 format with classic data model:

Out[1]=1
Out[2]=2

Applications  (1)Sample problems that can be solved with this function

Download a netCDF file with sea surface temperature data:

Out[1]=1

Import general information about the file:

Out[2]=2

Get the list of all datasets in the file:

Out[4]=4

Read attributes of every dataset:

Get a description of every dataset to see what they contain:

Out[13]=13

Read all attributes of the "/sst" dataset:

Out[17]=17

Import dimensions of the dataset; notice that time is the first dimension:

Out[18]=18

Read the data:

Determine the range of collected data (removing duplicated first) and add a small margin to it:

Out[30]=30

Visualize the changes of sea surface temperature:

Out[29]=29

Learn more about the file contents by inspecting metadata, for instance, how the data was collected:

Out[3]=3

Properties & Relations  (1)Properties of the function, and connections to other functions

Every netCDF4 file is also a valid HDF5 file:

Out[11]=11
Out[7]=7

Not every HDF5 file is a valid netCDF file:

Out[10]=10
Out[9]=9

Simple HDF5 files can be read by netCDF import:

Out[12]=12
Out[17]=17
Out[16]=16