NetCDF (.nc)
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, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import 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 dataset named 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 single‐precision numbers "Real64" IEEE double‐precision 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:
-
attr attributes 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:
-
data stores 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 $ByteOrdering what byte ordering to use OverwriteTarget True whether 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 allBasic Examples (3)Summary of the most common use cases
Get the names of datasets stored in a netCDF file:

https://wolfram.com/xid/03e2qryxru-fsa


https://wolfram.com/xid/03e2qryxru-vynfaz

Read metainformation from a netCDF file:

https://wolfram.com/xid/03e2qryxru-3fklby

Export raster image data to the netCDF format:

https://wolfram.com/xid/03e2qryxru-lksdsi

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

https://wolfram.com/xid/03e2qryxru-e3pnlv

Scope (13)Survey of the scope of standard use cases
Import (7)
Show all elements available in the file:

https://wolfram.com/xid/03e2qryxru-4nwp2m

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

https://wolfram.com/xid/03e2qryxru-zt3k23

Import the file structure, specifying the format explicitly:

https://wolfram.com/xid/03e2qryxru-u5g197

Import contents of a dataset by specifying its name:

https://wolfram.com/xid/03e2qryxru-bpbgyz

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

https://wolfram.com/xid/03e2qryxru-geiai5

Import dimensions and data format of a single dataset:

https://wolfram.com/xid/03e2qryxru-e0wwyt


https://wolfram.com/xid/03e2qryxru-c0uuuo
Render the imported data as graphics:

https://wolfram.com/xid/03e2qryxru-8cbpa1

Export (6)

https://wolfram.com/xid/03e2qryxru-kituy0

Show the datasets contained in this file:

https://wolfram.com/xid/03e2qryxru-50dj4l


https://wolfram.com/xid/03e2qryxru-hd2ljh


https://wolfram.com/xid/03e2qryxru-ojkn4a

Export two matrices to NetCDF:

https://wolfram.com/xid/03e2qryxru-kcox1g


https://wolfram.com/xid/03e2qryxru-kxuo23

Export a named dataset with given data type:

https://wolfram.com/xid/03e2qryxru-f2g8a3


https://wolfram.com/xid/03e2qryxru-c0g5ym

Export a named compound data type in a group:

https://wolfram.com/xid/03e2qryxru-kwvvyk


https://wolfram.com/xid/03e2qryxru-d897lz

Export named datasets with attributes:

https://wolfram.com/xid/03e2qryxru-2i1jd0

Import attributes of a particular group:

https://wolfram.com/xid/03e2qryxru-ln3xn0

Import all attributes stored in a file:

https://wolfram.com/xid/03e2qryxru-lpsiij

Import Elements (23)
Attributes (3)
Import attributes of all groups and datasets in the file:

https://wolfram.com/xid/03e2qryxru-q9buq4

Import attributes of a specific dataset:

https://wolfram.com/xid/03e2qryxru-5j6o9v

Import attributes of multiple objects:

https://wolfram.com/xid/03e2qryxru-2nx19w

Data (3)
Get data from all datasets in the file:

https://wolfram.com/xid/03e2qryxru-3rd7gl

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

https://wolfram.com/xid/03e2qryxru-byu4o4

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

https://wolfram.com/xid/03e2qryxru-8wp8ee

Import data from multiple datasets:

https://wolfram.com/xid/03e2qryxru-zha5jr

DataFormat (2)
Get data type description for every dataset in the file:

https://wolfram.com/xid/03e2qryxru-h62h6b

Simple numeric and string types have one-word descriptions:

https://wolfram.com/xid/03e2qryxru-k123la

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

https://wolfram.com/xid/03e2qryxru-5drjob

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

https://wolfram.com/xid/03e2qryxru-9dx01h

Datasets (2)
Dimensions (3)
Get dimensions of all datasets in a file:

https://wolfram.com/xid/03e2qryxru-2bnom

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

https://wolfram.com/xid/03e2qryxru-5wqa7v


https://wolfram.com/xid/03e2qryxru-nu4xwm

Get dimensions of a specific dataset:

https://wolfram.com/xid/03e2qryxru-swr7p0

DimensionNames (2)
Groups (1)
Metadata (2)
NamedDimensions (3)
Import all named dimensions in the file:

https://wolfram.com/xid/03e2qryxru-ntdc31


https://wolfram.com/xid/03e2qryxru-i7imss

Import named dimension to get coordinate values along this dimension:

https://wolfram.com/xid/03e2qryxru-6hsu2l

Inspecting attributes often gives more information about the dimension:

https://wolfram.com/xid/03e2qryxru-vmzfly

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

https://wolfram.com/xid/03e2qryxru-o5kb81

Export Elements (25)
"Attributes" (4)
Attach attributes to the root group:

https://wolfram.com/xid/03e2qryxru-49xowb

Specify data format for attributes:

https://wolfram.com/xid/03e2qryxru-4tny94

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

https://wolfram.com/xid/03e2qryxru-8wee6b

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

https://wolfram.com/xid/03e2qryxru-667ubt

Overwrite attribute value in existing file:

https://wolfram.com/xid/03e2qryxru-xonnge

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

https://wolfram.com/xid/03e2qryxru-ltu9qr


https://wolfram.com/xid/03e2qryxru-znaxye


https://wolfram.com/xid/03e2qryxru-3779ij

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

https://wolfram.com/xid/03e2qryxru-cbjbwa

https://wolfram.com/xid/03e2qryxru-dzovn3

Inspect the structure of the NetCDF file:

https://wolfram.com/xid/03e2qryxru-f753ts

Export a dataset with a custom data format:

https://wolfram.com/xid/03e2qryxru-kfjwib

Export a dataset with an attribute:

https://wolfram.com/xid/03e2qryxru-nqj7l3

Create a scalar dataset with a single integer:

https://wolfram.com/xid/03e2qryxru-iw2omo

Create an array of integer numbers:

https://wolfram.com/xid/03e2qryxru-gz2auq

Export an array of real numbers:

https://wolfram.com/xid/03e2qryxru-qwxwoo

Create a dataset with strings:

https://wolfram.com/xid/03e2qryxru-vb1kbq

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

https://wolfram.com/xid/03e2qryxru-7ee5sx

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

https://wolfram.com/xid/03e2qryxru-dbvimd


https://wolfram.com/xid/03e2qryxru-yr6f21

"Groups" (2)
"NamedDatatypes" (3)
Export a compound named data type to a NetCDF file:

https://wolfram.com/xid/03e2qryxru-pjdatj


https://wolfram.com/xid/03e2qryxru-il1ikv

Use the previously exported type when exporting a dataset:

https://wolfram.com/xid/03e2qryxru-qcxyf8

Import data format and dimensions of the dataset:

https://wolfram.com/xid/03e2qryxru-ci2ufi


https://wolfram.com/xid/03e2qryxru-n6st7j

Export a nested compound type:

https://wolfram.com/xid/03e2qryxru-hc2ql4

Simple scalar types cannot be stored in a NetCDF file:

https://wolfram.com/xid/03e2qryxru-pkml5r


"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:

https://wolfram.com/xid/03e2qryxru-sviw5i


https://wolfram.com/xid/03e2qryxru-jgs929

Specify dimension names explicitly:

https://wolfram.com/xid/03e2qryxru-ld7hy3


https://wolfram.com/xid/03e2qryxru-xnsw8p


https://wolfram.com/xid/03e2qryxru-3zf4ub

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

https://wolfram.com/xid/03e2qryxru-jy2r0y


https://wolfram.com/xid/03e2qryxru-dxs9v7

Named dimensions can later be used when defining datasets:

https://wolfram.com/xid/03e2qryxru-sxvhqr


https://wolfram.com/xid/03e2qryxru-u55zjm

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

https://wolfram.com/xid/03e2qryxru-te6kcn


https://wolfram.com/xid/03e2qryxru-ck875b

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

https://wolfram.com/xid/03e2qryxru-d2pd56


https://wolfram.com/xid/03e2qryxru-wc2o33

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

https://wolfram.com/xid/03e2qryxru-rz5xa


https://wolfram.com/xid/03e2qryxru-cf1jly

Export Options (6)
OverwriteTarget (2)
By default, a new file is created for each call to Export:

https://wolfram.com/xid/03e2qryxru-h3toce

With OverwriteTargetFalse, existing files will not be overwritten:

https://wolfram.com/xid/03e2qryxru-jdz52g


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

https://wolfram.com/xid/03e2qryxru-u6lx27


https://wolfram.com/xid/03e2qryxru-z7gnz8

"Version" (4)
By default, Export creates NetCDF4 files with enhanced data model:

https://wolfram.com/xid/03e2qryxru-w69xnm


https://wolfram.com/xid/03e2qryxru-1nik9u

Export data to classic NetCDF format:

https://wolfram.com/xid/03e2qryxru-z2ijpx


https://wolfram.com/xid/03e2qryxru-wu0dj7

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

https://wolfram.com/xid/03e2qryxru-yp7tig


https://wolfram.com/xid/03e2qryxru-rvkzur

Export data to NetCDF4 format with classic data model:

https://wolfram.com/xid/03e2qryxru-b3ii91


https://wolfram.com/xid/03e2qryxru-ycf4uz

Applications (1)Sample problems that can be solved with this function
Download a netCDF file with sea surface temperature data:

https://wolfram.com/xid/03e2qryxru-u8i85k

Import general information about the file:

https://wolfram.com/xid/03e2qryxru-07mma9

Get the list of all datasets in the file:

https://wolfram.com/xid/03e2qryxru-f6kly4

Read attributes of every dataset:

https://wolfram.com/xid/03e2qryxru-rk5s6a
Get a description of every dataset to see what they contain:

https://wolfram.com/xid/03e2qryxru-ltx6j7

Read all attributes of the "/sst" dataset:

https://wolfram.com/xid/03e2qryxru-y405ng

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

https://wolfram.com/xid/03e2qryxru-t3mbvn


https://wolfram.com/xid/03e2qryxru-u35sf0
Determine the range of collected data (removing duplicated first) and add a small margin to it:

https://wolfram.com/xid/03e2qryxru-d0f1nm

Visualize the changes of sea surface temperature:

https://wolfram.com/xid/03e2qryxru-8qlkt2

https://wolfram.com/xid/03e2qryxru-583p21

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

https://wolfram.com/xid/03e2qryxru-r6sbbl

Properties & Relations (1)Properties of the function, and connections to other functions
Every netCDF4 file is also a valid HDF5 file:

https://wolfram.com/xid/03e2qryxru-cpz97m


https://wolfram.com/xid/03e2qryxru-skl8tf

Not every HDF5 file is a valid netCDF file:

https://wolfram.com/xid/03e2qryxru-28mk2a


https://wolfram.com/xid/03e2qryxru-twzjsm


Simple HDF5 files can be read by netCDF import:

https://wolfram.com/xid/03e2qryxru-2j754j


https://wolfram.com/xid/03e2qryxru-q2kk2k


https://wolfram.com/xid/03e2qryxru-fqe5ke
