NetCDF

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page NetCDF et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

Description

NetCDF (Network Common Data Format) is a library for formatting data independently of the underlying computer architecture. The library is useful for creating, reading and transferring scientific data. There have been significant changes to the library with the release of version 4.0. Since this version was released in 2008, this is the one we will focus our attention on. Note that NetCDF 4.x is backwards compatible - it can read files produced by an earlier version but the opposite isn't true.

You can download a NetCDF version and its documentation from the Internet. NetCDF can also be installed using Git. Several interfaces are available: C, Fortran, C++, Java and Python. Note that the Python interface distributed with SciPy is based on version 3 of NetCDF. The version 4 interface is available from netcdf4-python.

Advantages and Disadvantages

Here is a brief list of the principal advantages and disadvantages of the NetCDF4 library:

  • The data are independent of the processor architecture (endianness).
  • The data are structured in a way that keeps track of all the pertinent information (e.g. physical units).
  • If it's built using a parallel version of HDF5, NetCDF4 can read and write in parallel.
  • Data can be compressed as it's written.
  • A simpler interface than HDF5.
  • It's free software for most platforms.
  • The Python interface doesn't support parallelism (version 1.0.5).
  • Certain files produced with HDF5 cannot be read using NetCDF.

Example

The following example shows how to save data associated with two dimensions x and y in NetCDF 4 format.


File : ex_netcdf4.c
/**
Write the simple_xy file, with some of the features of netCDF-4.
 
This is a very simple example which is based on the simple_xy example,
but whch uses netCDF-4 features, such as compression.
 
This program writes a 2D netCDF variable (called "data") and fills it
with sample data.  It has two dimensions, "x" and "y".
 
Full documentation for netCDF can be found at:
http://www.unidata.ucar.edu/netcdf/docs
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
 
/* Name of the file which will be created */
#define FILE_NAME "simple_xy_nc4.nc"
 
/* We write 2D data on a 60 x 120 grid */
#define NDIMS 2
#define NX 60
#define NY 120
 
/* Define how to handle errors */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
 
int
main()
{
   int ncid, x_dimid, y_dimid, varid;
   int dimids[NDIMS];
   size_t chunks[NDIMS];
   int shuffle, deflate, deflate_level;
   int data_out[NX][NY];
   int x, y, retval;
 
   /* Set the data layout */
   shuffle = NC_SHUFFLE;
   deflate = 1;
   deflate_level = 1;
 
   /* Create some nonsense data for testing purposes */
   for (x = 0; x < NX; x++)
      for (y = 0; y < NY; y++)
         data_out[x][y] = x * NY + y;
 
   /* Create the file. The NC_NETCDF4 parameter indicates that the file type is NetCDF-4/HDF5 */
   if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
      ERR(retval);
 
   /* Define the dimensions */
   if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
      ERR(retval);
   if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
      ERR(retval);
 
   /* Create the variables */
   dimids[0] = x_dimid;
   dimids[1] = y_dimid;
   chunks[0] = NX/4;
   chunks[1] = NY/4;
 
   /* Define the variables */
   if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
                            dimids, &varid)))
      ERR(retval);
   if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
      ERR(retval);
   if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
                                    deflate_level)))
      ERR(retval);
 
   /* Write the data to the file */
   if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
      ERR(retval);
 
   /* Close the file */
   if ((retval = nc_close(ncid)))
      ERR(retval);
 
   printf("*** SUCCESS in writing the file simple_xy_nc4.nc!\n");
   return 0;
}


You can compile this example in the following manner if you have the NetCDF4 and HDF5 libraries in your environment.

[nom@serveur $] icc ex_netcdf4.c -lnetcdf


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager