Introduction to NetCDF Ernesto Munoz

advertisement
Introduction to NetCDF
Ernesto Munoz
Outline




Overview of NetCDF
NetCDF file information
CDL utilities: ncdump, ncgen
Reading NetCDF file
• with Fortran
• with GrADS
• with Matlab

Writing NetCDF file
What is NetCDF?






Network Common Data Form (NetCDF)
An interface for (array-oriented) data
access
A collection of libraries of data-access
routines (for Fortran, C++, etc.)
A machine-independent format for
scientific data
Developed at Unidata UCAR
More information at:
• www.unidata.ucar.edu/software/netcdf/
Positive Attributes of NetCDF



Self-describing – includes
information about the data it
contains
Portable - A machine-independent
binary data format
Direct-access – can access
efficiently a subset of the dataset
NetCDF file contents

Dimensions
Information about its name and length
At most one unlimited dimension (i.e., a dimension with no maximum size)

Variables
Information about its name, type, shape, attributes
& values
• Coordinate variable: 1-D array with the same name as its dimension (e.g.,
latitude)
• Primary variable: the data
• Fixed-length variable: primary variable with fixed dimensions
• Record variable: primary variable with an unlimited dimension

Attributes – “metadata”
Information about its name, type, values & length
• Variable attributes
• Global attributes
Example of NetCDF file contents

Data values on a 2-D latitude/longitude
grid
• E.g., zonal and meridional wind data

Information about the dimensions
• Latitudes, longitudes, vertical levels and
times


The units of the data values
Scaling factor and offset to be applied to
data
CDL - Common Data Language
NetCDF ↔ ASCII translator
 Its two main utilities:
1. ncdump (from netcdf to ascii)

• to get overview of NetCDF file
contents
2. ncgen (from ascii to netcdf)
• to create NetCDF files
• to create Fortran (or C) programs to
write NetCDF files
ncdump
For quick glimpse at file’s “metadata”
 To view header info:
ncdump -h myfile.nc

To view header info plus values of
coordinate variables:
ncdump -c myfile.nc

To output data values in ascii form:
ncdump –v varname myfile.nc
Exercise
Look at description of file with ncdump
 Compare -c and -h options
ncdump -h myfile.nc > filehdr.txt
ncdump -c myfile.nc > filehdrdim.txt
Reading NetCDF with Fortran
Typical order of calls to read a NetCDF file
NF_OPEN
.....
NF_INQ_DIMID
.....
NF_INQ_DIMLEN
.....
NF_INQ_VARID
.....
NF_GET_ATT
.....
NF_GET_VAR
.....
NF_CLOSE
! open NetCDF dataset
! get dimensions IDs
! get dimensions lengths
! get variables IDs
! get attribute values
! get values of variables
! close NetCDF dataset
Reading NetCDF

With Matlab
• Read Takemasa’s memo at:
• http://www.atmos.umd.edu/~miyoshi/memorandum/me
mo_netcdf.html

With GrADS
• Open GrADS with gradsnc command (instead of
grads)
• Open NetCDF file with sdfopen command
(instead of open)
• For more information refer to:
http://grads.iges.org/grads/gadoc/SDFdescriptorfile.html
Writing a NetCDF file with ncgen

ncgen can be used to:
• Generate code that will produce a NetCDF file
Fortran: ncgen -f mydata.cdl > mydata.f
C:
ncgen -c mydata.cdl > mydata.c
• Generate a NetCDF file
ncgen -o mydata.nc mydata.cdl

Best practices to writing NetCDF files:
• www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html
Download