Getting started with netcdf
Author: laomai
Reviewer: ybb
When reprinting, please indicate the source:Http://blog.csdn.net/laomai
Recently I am working on a data collection-related project, which uses the netcdf library. I have a rough look at it. Here I will write my experiences
Refer to later.
I. Overview
Netcdf is called network common data format. The Chinese translation is "Network Common Data Format ",
For programmers, it is similar to zip, JPEG, and BMP file formats and is a standard file format. Netcdf
The document was designed to store data in meteorological science and has become a file generated by many data collection software.
Format.
In mathematics, the data stored by netcdf is a single-valued function with multiple independent variables. The formula is
F (x, y, z,...) = value. The independent variables X, Y, and Z of the function are called dimensions in netcdf)
Or coordinate axis (axix), function value is called variable in netcdf. Independent variable and function value
Some physical properties, such as measurement units (dimensions) and names of physics
In netcdf, attributes are called attributes ).
Ii. Download netcdf
The official website of netcdf isHttp://www.unidata.ucar.edu/software/netcdf/.
In this article, we mainly discuss how to use the netcdf software library on Windows. We will
Download the following resources
(1) Source Code of netcdf. The current address is
Ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4/netcdf-beta.tar.gz
(2) netcdf pre-compiled DLL on Windows. The address is
Ftp://ftp.unidata.ucar.edu/pub/netcdf/contrib/win32/netcdf-3.6.1-win32.zip
After decompression, there are the following items:
Netcdf. dll is the compiled DLL.
Ncgen.exe is a tool for generating netcdf files.
Ncdump.exe is a tool used to read netcdf files.
Netcdf. lib and netcdf. Exp are used in programming.
(3) Documents related to netcdf, including
① Netcdf user manual, which isHttp://www.unidata.ucar.edu/software/netcdf/docs/netcdf.pdf
② Netcdf getting started tutorial,Http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial.pdf
③ Netcdf C interface API manual, which isHttp://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.pdf
Next, let's take a look at the specific content of the netcdf file.
Iii. Content of the netcdf File
The structure of a netcdf file includes the following objects:
1. Variables)
Variables correspond to actual physical data. For example, for an electric meter in our house, the readings displayed at each time point indicate the user's power consumption at that time point.
The read value can be expressed by the variable in netcdf. It is a single value that takes time as the independent variable (or the number of independent variables as one dimension ).
Function. For another example, in meteorology, we need to make a pressure chart, that is, "How much is the atmospheric pressure of the east longitude XX degrees, the point of north latitude YY degrees", which is
A two-dimensional single-valued function. The two dimensions are longitude and latitude. The function value is atmospheric pressure.
As shown in the preceding example, the variable in netcdf is an n-dimensional array, and the dimension of the array is the number of independent variables in the actual problem,
The number group value is the observed physical value. There are six Storage types of variables (array values) in netcdf: ASCII character (char), byte, short INTEGER (short), INTEGER (INT ), float, double ). obviously, these types are the same as those in C, so C's friends should be able to understand them soon.
2. Dimension)
A dimension corresponds to an independent variable in the function, or an coordinate axis in the embedding image, which is an n-dimensional vector in linear algebra.
(This is also the origin of the dimension name ). In netcdf, a dimension has a name and a range (or length, that is
It is a set of discrete vertices or continuous intervals ). In netcdf, the length of a dimension is basically limited,
You can only have one dimension with an infinite length.
3. Attribute)
Comments or explanations of attributes on the specific physical meanings of variable values and dimensions. Because both variables and dimensions are infinite numbers in netcdf,
To let people understand the specific meanings of these numbers, they have to rely on the attribute object.
In netcdf, an attribute is composed of an attribute name and an attribute value (generally a string. For example, in a CDL File
(The specific format of the CDL file is described in the next section ).
Temperature: Units = "Celsius ";
The previous temperature is a defined variable (variable), that is, temperature. The units after the colon is the attribute name,
Indicates the physical unit. = the value of the units attribute is "Celsius", that is, degree celsius. The entire line of code indicates
The unit of temperature is Celsius, which is easy to understand.
Iii. CDL Structure
CDL is called network common data form description language. It is used to describe netcdf files.
Is a syntax format. It includes the specific definitions of the three netcdf objects (variables, dimensions, and attributes) mentioned above.
Let's look at a specific example (the CDL file in this example is extracted from section 2.1 the simple XY example in the netcdf tutorial)
Netcdf simple_xy {
Dimensions:
X = 6;
Y = 12;
Variables:
Int data (x, y );
Data:
Data =
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71;
}
The code above defines a structure in the netcdf format simple_xy,
This structure consists of three parts:
1. Dimension definition, starting with dimensions: keyword
Dimensions:
X = 6;
Y = 12;
Two Axes (or two dimensions) are defined, respectively named X and Y, and the length of the X axis (precisely the number of coordinate points) is 6,
The Y axis length is 12.
2. variable definition: starts with variables:
Variables:
Int data (x, y );
Defines a function data with the X and Y axes as independent variables. The mathematical formula is f (x, y) = data;
Note that the order in which dimensions appear is ordered. It determines the specific value assignment result in the data segment.
3. Data Definition, starting with data:
Data:
Data =
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71;
This section of data can be viewed using the mathematical function formula F (x, y) = data,
Is x = 0, y = 0, Data = 0;
X = 0, y = 1, data = 1;
X = 5, y = 11 Yes, Data = 71;
Note that,
1. Assignment order:
We provide a C-format CDL file, so the assignment order here is the same as that in C, that is, the "Row-Based Assignment ",
In the Fortran language, it is a "column-based value assignment". Therefore, in the CDL file in the Fortran format, the numerical order of the data segment is exactly the same as that in this
Transpose rows and columns.
2. default values and Coordinate Variables of Independent Variables
If only the length of a dimension is given, the dimension value starts from 0 by default, and then 1 is automatically added to stop (length-1,
In many cases, we need to give the coordinates of each point. In this case, we need to use the Coordinate Variables in netcdf.
"Coordinate varibles": adds a dimension-related mona1 function (independent variable) and provides its value range.
For example, the following CDL file (from 2.2 The SFC pres temp example in the netcdf tutorial)
Netcdf sfc_pres_temp {
Dimensions:
Latitude = 6; // latitude axis
Longpolling = 12; // longitude axis
Variables:
Float latitude (latitude); // coordinate variable, which stores the specific latitude
Latitude: Units = "degrees_north ";
Float longpolling (longpolling); // coordinate variable, storing the specific latitude
Longpolling: Units = "degrees_east ";
Float Pressure (latitude, longpolling); // atmospheric pressure value of a certain point (intersection of longitude and latitude)
Pressure: Units = "HPA"; // The unit of atmospheric pressure is
Float temperature (latitude, longpolling); // The temperature value of a certain point (intersection of longitude and latitude)
Temperature: Units = "Celsius"; // The unit of temperature is
Data:
Latitude = 25, 30, 35, 40, 45, 50;
Longpolling =-125,-120,-115,-110,-105,-100,-95,-90,-85,-80,-75,-70;
Pressure =
900,906,912,918,924,930,936,942,948,954,960,966,
901,907,913,919,925,931,937,943,949,955,961,967,
902,908,914,920,926,932,938,944,950,956,962,968,
903,909,915,921,927,933,939,945,951,957,963,969,
904,910,916,922,928,934,940,946,952,958,964,970,
905,911,917,923,929,935,941,947,953,959,965,971;
Temperature =
9, 10.5, 12, 13.5, 15, 16.5, 18, 19.5, 21, 22.5, 24, 25.5,
9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 21.25, 22.75, 24.25,
25.75,
9.5, 11, 12.5, 14, 15.5, 17, 18.5, 20, 21.5, 23, 24.5, 26,
9.75, 11.25, 12.75, 14.25, 15.75, 17.25, 18.75, 20.25, 21.75, 23.25, 24.75,
26.25,
10, 11.5, 13, 14.5, 16, 17.5, 19, 20.5, 22, 23.5, 25, 26.5,
10.25, 11.75, 13.25, 14.75, 16.25, 17.75, 19.25, 20.75, 22.25, 23.75,
25.25
For the above data
When latitude = 25, longpolling =-125, pressure = 900, temperature = 9;
When latitude = 25, longpolling =-120, pressure = 906, temperature = 10.5;
And so on.
Iv. Read and Write netcdf files
"What I Learned" is the basic knowledge of netcdf, which is used to read and write files in netcdf format.
To pave the way, let's take a look at how to create a netcdf file and read its content.
1. Read and Write netcdf files under the command line
(1) create a simple_xy.cdl file. The content is the first example in the "CDL structure" in the previous section.
Use the ncgen.exe tool (see section 2 above) to create a netcdf file.
Copy the ncgen.exe file to the directory where simple_xy.cdl is located.
② Run ncgen-O simple_xy.nc simple_xy.cdl to generate the netcdf Format File simple_xy.nc
(3) The generated simple_xy.nc is a binary file. To restore data information from this file, you must use the ncdump tool.
Copy the ncdump.exe file to the directory of simple_xy.nc.
② Execute ncdump simple_xy.nc in the command line. At this time, the screen output is exactly the same as simple_xy.cdl content. Description
All of our file read/write operations are correct.
2. Programming to read and write netcdf files
We know how to create and read the netddf file manually. Let's take a look at how to implement netcdf using code in the program.
File Creation and analysis. My programming environment is Win2000 + vc6.0 and the VC SP6 patch package is installed. Select the sample code from
Section 2.1 of the netcdf tutorial the simple XY example
(1) decompress the source code of netcdf. We will use the libsrc/netcdf. h header file.
(2) create an empty Win32 console project in vc6. The project name is simplexywrite, which is used to create the netcdf file.
(3) copy the following files to the project directory.
① Libsrc/netcdf. h header file in netcdf source code
② Examples/C/simple_xy_wr.c file in the netcdf source code and renamed it simple_xy_wr.cpp.
③ Netcdf. dll and netcdf. Lib files in the netcdf pre-compiled package
(4) add netcdf. h file and simple_xy_wr.cpp to the file list of the project.
(For details, choose Project> Add to project> files)
Worker adds netcdf. lib to the Lib list of the project.
(For details, choose Project> Add to project> Settings> link> Object/library modules)
Compile compile and run this project. A simple_xy.nc file is generated under the project directory. The content of this file and
The file content is exactly the same.
The simple_xy_wr.c file is the C code for creating the netcdf file, while the examples/C/simple_xy_rd.c file is for analysis.
The code of the netcdf file can be compiled in vc6 using similar steps. During runtime
Simple_xy.nc: copy the file to the project directory. If the file format is correct, a message is displayed.
* ** Success reading example file simple_xy.nc!
Then exit.
3. Use the ncgen command to automatically generate C code
Given the simple_xy.cdl file, you can use
Ncgen-C simple_xy.cdl
Command to Output C Code directly on the screen.
However, this method is only applicable when CDL data is relatively simple. For real projects,
We need to write the code ourselves.
V. Summary
Based on the introduction above, I believe the readers have a general understanding of netcdf. In general,
The core content of netcdf is to create a netcdf format file through the CDL description syntax. Grasping this point,
When readers continue to look at other documents of netcdf, it should not be too difficult. If so, the author's
The goal is achieved.
Vi. Appendix
To facilitate readers, the contents of several files used in this article are listed here
1. Content of the simple_xy.cdl File
Netcdf simple_xy {
Dimensions:
X = 6;
Y = 12;
Variables:
Int data (x, y );
Data:
Data =
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71;
}
2. Contents of the simple_xy-wr.c File
/* This is part of the netcdf package.
Copyright 2006 University Corporation for Atmospheric Research/unidata.
See copyright file for conditions of use.
This is a very simple example which writes a 2D array
Sample Data. To handle this in netcdf we create two shared
Dimensions, "x" and "y", and a netcdf variable, called "data ".
This example demonstrates the netcdf c api. This is part of
Netcdf tutorial, which can be found:
Http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netcdf c api can be found:
Http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$ ID: simple_xy_wr.c, V 1.12 2007/02/14 20:59:21 ed exp $
*/
# Include <stdlib. h>
# Include <stdio. h>
# Include <netcdf. h>
/* This is the name of the data file we will create .*/
# Define file_name "simple_xy.nc"
/* We are writing 2D data, a 6x12 grid .*/
# Define ndims 2
# Define NX 6
# Define NY 12
/* Handle errors by printing an error message and exiting with
* Non-zero status .*/
# Define errcode 2
# Define err (e) {printf ("error: % s/n", nc_strerror (e); exit (errcode );}
Int
Main ()
{
/* When we create netcdf variables and dimensions, we get back
* ID for each one .*/
Int NCID, x_dimid, y_dimid, varid;
Int dimids [ndims];
/* This is the data array we will write. It will be filled with
* Progression of numbers for this example .*/
Int data_out [NX] [NY];
/* Loop indexes, and error handling .*/
Int X, Y, retval;
/* Create some pretend data. If this wasn't an example program, we
* Wocould have some real data to write, for example, Model
* Output .*/
For (x = 0; x <NX; X ++)
For (y = 0; y <NY; y ++)
Data_out [x] [Y] = x * ny + Y;
/* Always check the return code of every netcdf function call. In
* This example program, any retval which is not equal to nc_noerr
* (0) will cause the program to print an error message and exit
* With a non-zero return code .*/
/* Create the file. The nc_clobber parameter tells netcdf
* Overwrite this file, if it already exists .*/
If (retval = nc_create (file_name, nc_clobber, & NCID )))
Err (retval );
/* Define the dimensions. netcdf will hand back an ID for each .*/
If (retval = nc_def_dim (NCID, "X", NX, & x_dimid )))
Err (retval );
If (retval = nc_def_dim (NCID, "Y", NY, & y_dimid )))
Err (retval );
/* The dimids array is used to pass the IDs of the dimensions
* The variable .*/
Dimids [0] = x_dimid;
Dimids [1] = y_dimid;
/* Define the variable. The type of the variable in this case is
* Nc_int (4-byte integer ).*/
If (retval = nc_def_var (NCID, "data", nc_int, ndims,
Dimids, & varid )))
Err (retval );
/* End define mode. This tells netcdf we are done defining
* Metadata .*/
If (retval = nc_enddef (NCID )))
Err (retval );
/* Write the pretend data to the file. Although netcdf supports
* Reading and writing subsets of data, in this case we write all
* The data in one operation .*/
If (retval = nc_put_var_int (NCID, varid, & data_out [0] [0])
Err (retval );
/* Close the file. This frees up any internal netcdf Resources
* Associated with the file, and flushes any buffers .*/
If (retval = nc_close (NCID )))
Err (retval );
Printf ("*** success writing example file simple_xy.nc! /N ");
Return 0;
}
3. Content of simple_xy_rd.c
/* This is part of the netcdf package.
Copyright 2006 University Corporation for Atmospheric Research/unidata.
See copyright file for conditions of use.
This is a simple example which reads a small dummy array, which was
Written by simple_xy_wr.c. This is intended to restrict strate the use
Of the netcdf c api.
This program is part of the netcdf Tutorial:
Http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netcdf c api can be found:
Http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$ ID: simple_xy_rd.c, V 1.9 2006/08/17 23:00:55 Russ exp $
*/
# Include <stdlib. h>
# Include <stdio. h>
# Include <netcdf. h>
/* This is the name of the data file we will read .*/
# Define file_name "simple_xy.nc"
/* We are reading 2D data, a 6x12 grid .*/
# Define NX 6
# Define NY 12
/* Handle errors by printing an error message and exiting with
* Non-zero status .*/
# Define errcode 2
# Define err (e) {printf ("error: % s/n", nc_strerror (e); exit (errcode );}
Int
Main ()
{
/* This will be the netcdf ID for the file and data variable .*/
Int NCID, varid;
Int data_in [NX] [NY];
/* Loop indexes, and error handling .*/
Int X, Y, retval;
/* Open the file. nc_nowrite tells netcdf we want read-only access
* To the file .*/
If (retval = nc_open (file_name, nc_nowrite, & NCID )))
Err (retval );
/* Get the varid of the Data variable, based on its name .*/
If (retval = nc_inq_varid (NCID, "data", & varid )))
Err (retval );
/* Read the data .*/
If (retval = nc_get_var_int (NCID, varid, & data_in [0] [0])
Err (retval );
/* Check the data .*/
For (x = 0; x <NX; X ++)
For (y = 0; y <NY; y ++)
If (data_in [x] [Y]! = X * ny + Y)
Return errcode;
/* Close the file, freeing all resources .*/
If (retval = nc_close (NCID )))
Err (retval );
Printf ("*** success reading example file % s! /N ", file_name );
Return 0;
}