Netcdf file overview and read/write

Source: Internet
Author: User
Introduction to netcdf

Netcdf (Network common data format), which is a common network data format. It was first developed by unidata, a program funded by the National Scientific Commission of America. It intended to provide a common data access method under different application projects in the unidata program, data shapes include observations of single points, time sequences, regular grids, and image files of satellites or radars. Netcdf can be simply regarded as an access interface. any file that uses the netcdf ACCESS format can be called a netcdf file. As for the functions of netcdf, it provides link libraries for C, Fortran, C ++, Perl, or other language I/O, so that program developers can read and write data files, it has the ability to explain and can span the platform and machine restrictions. Each netcdf file can contain multi-dimensional named variables, including long integers, single-factor and double-precision real numbers, and characters, each variable has its own self-introduced data, including the unit, full name, and meaning of the measurement, which is the real data itself after being extracted. The netcdf interface is a multi-dimensional data distribution system. The files generated by this interface have a multi-dimensional data format. When you need a certain piece of data, the program will not read the data you need from the first data, but the netcdf software will directly access that data. In this way, the Data Access time in the mode operation is greatly reduced. But because of this, netcdf requires a lot of space, because it has many self-explanatory statements.

Netcdf file structure

The operations on netcdf files mainly include reading and writing. Before learning about these two aspects, you must first understand the structure of netcdf files. netcdf files are mainly dimensions, variables, attributes, data is composed of four parts: Dimensions mainly refers to the structure of multi-dimensional data, such as longitude, latitude, time, and other variables, such as temperature and other attributes, data, such as the unit of the Variable

The following figure shows the basic structure of the netcdf file (the arrow points to an operable object)

The basic structure of the netcdf file according to this special structure of the netcdf file, the ncfile class used contains objects of ncdim, ncvar, and ncatt classes as members, corresponding to the preceding dimensions, respectively, variables, attributes section. Read/write netcdf files

1.1 create engineering documentsUse vc6 to create a project, use the dynamic link library netcdf, and then include the file netcdfcpp. hnetcdfcpp. cpp, ncvalues. h ncvalues. cpp, and ncconfig. H into the project. 1.2 read netcdf filesFirst, define an ncfile Class Object. Use the ncfile class constructor to initialize ncfile (const char * path, filemode = readonly ,...... ); Path is the file storage path, and filemode is the file opening method. Besides readonly, write, new, replace, and other methods, we only need to read the file and select readonly, other methods will be introduced later. You can use default values for other parameters, such as ncfile NC ("G: // file. NC ", ncfile: readonly); Use NC. is_valid () to determine whether the file is opened successfully, so that the next step is to read the netcdf file data. The netcdf file consists of four parts: Dimensions, variables, attributes, and data. The content of each part of the file is read below. Dimensions: You can use the netcdf member function num_dims () to obtain the number of dimensions in the file, and then use another ncfile member function get_dim (int id), or get_dim (nctoken name) --- the parameter can be an ID (INT) or a dim name (nctoken) to obtain the dimensions of each file. Use the member functions ID (), name () of the ncdim class (), size () can read the ID number, name, and size of each dimensions in sequence. For example: for (INT I = 0; I <NC. num_dims ()-1; I ++) {string. format (string + "dimid = % d name = % s length = % d/N", NC. get_dim (I)-> ID (), NC. get_dim (I)-> name (), NC. get_dim (I)-> size ();} using a for loop, all dim information can be read at a time according to the dim ID variables: Similarly, the netcdf member function num_vars () you can get the number of variables in the file. You can use the ncfile class get_var (int id) or get_var (nctoken name) to read the variables in each file or the information such as ID and name, the difference is that variables can also use num_dims () to read the number of dimensions they contain, and variables can also use Ge T_dim (int id) reads the dimensions information it contains, and the data information of the file is operated by variables. Data Reading: first, we need to calculate the data size based on the size of each dimensions contained in variables, and define an array of the corresponding size to store data. Then, the first parameter using get (type * array, long * num) of variables is the data storage array just defined, and the second parameter is the custom one-dimensional array, array elements are used to store the size of each dimension. it is generally used for multi-dimensional data. Example: Float RHS [50]; long array [3]; array [0] = 1; array [1] = 5; array [2] = 10; NC. get_var ("RH")-> get (RHs, array); you can also use get (type * Vals, long edge0 = 0, long edge1 = 0, long edge2 = 0, long edge3 = 0, long edge4 = 0) The first const parameter is the data storage array. The following parameters record the size of Dimension 1, 2, 3, 4, and 5 respectively, the default value is 0. For example, int n = NC. get_dim ("Lat")-> size (); int lats [5]; NC. get_var ("Lat")-> get (lats, n); for (I = 0; I <5; I ++) {string. format (string + "% d", Lats [I]);} if Varia Bles also contains the description of attributes. The get_att (int id) function can be used. The parameter is the ID of attributes and its attributes can be read. Example: string. format ("/natt:/n % s: % s/n", NC. get_var (0)-> get_att (0)-> name (), NC. get_var (0)-> get_att (0)-> as_string (0); attributes: attributes can be divided into two types: Description of the entire file, that is, global attributes can be obtained through the get_att (int id) function of the ncfile class. The parameter is the ID number, for example, String. format (string + "/n % s: % s", NC. get_att (0)-> name (), NC. get_att (0)-> as_string (0); there is also a description, which can be obtained through the method described in variables. The information read mainly includes the name and corresponding content, it can be determined by name (), as_type (long ID), and type based on the attributes data type, such as S_string (long ID), as_int (long ID), etc. The as_type (long ID) function is mainly used to retrieve the content of each variables. The example is behind variables. Data: we have read the data through variables. 1.3 netcdf file writingLike reading a file, you must first define an ncfile class object, such as ncfile NC ("G: // file. NC ", ncfile: replace); Use NC. is_valid () is used to determine whether the file is successfully opened. You can choose new to create a new file. If the file already exists, an error is returned, you can also use write and replace. Select replace. If the file already exists, it will be overwritten. When using write, the file must already exist. The data written to the file will be added after the existing data of the file. First, create a netcdf file. The data to be written is the same as the data just read, including dimensions, variables, attributes, data; dimensions: Use add_dim (nctoken name, long dimsize) The member function is added to dimensions. If the size is unlimited and the add_dim (nctoken name) function of the ncfile class is used, the ncfile class automatically processes the size as unlimited. Example: NC. add_dim ("Lat", 5); NC. add_dim ("time"); // when the size is unlimited, variables: Use add_var (nctoken name, nctype type, type dim1, type dim2 ,... ...) Add a member function to variables, dim1 ,..., Dimn, which is the dimensions contained in variables. Example: NC. add_var ("Lat", ncint, NC. get_dim (0); // a dim is NC. add_var ("RH", ncfloat, NC. get_dim ("time"), NC. get_dim ("Lat"), NC. get_dim ("Lon"); // attributes when multiple Dim: the attributes of the entire file uses the add_att (nctoken attname, type value) of ncfile) function type is determined based on the attributes type. variables can be added using its member function add_att (attname, type value. For example, // The main parameter NC for writing files. add_att ("sourse", "fictional model output"); // write the parameter NC for a single variable. get_var (0)-> add_att ("long_name", "temperature"); Data: first defines an array and writes the data to be written to the file into an array, the put (type * arr, const long * count) Type Function of variables is a data type or variable function. Put (type * Vals, long edge0 = 0, long edge1 = 0, long edge2 = 0, long edge3 = 0, long edge4 = 0) Const. The first parameter is the data storage array. The following parameters record the numbers 1, 2, 3, 4, 5-dimension size. The default value is 0, for example, int lats [5] = {20, 30, 40, 50, 60}; // lat NC. get_var ("Lat")-> put (lats, 5); float rhss [50]; for (INT I = 0; I <1; I ++) for (Int J = 0; j <5; j ++) for (int K = 0; k <10; k ++) {rhss [I * 5*10 + J * 10 + k] = (float) (I * 5*10 + J * 10 + k + 1)/100 ;} long Count [3]; // Since RH has multiple dim, define a one-dimensional array count [0] = 1; // describe the size of each dimension. Count [1] = 5; count [2] = 10; NC. get_var ("RH")-> put (rhss, count );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.