netCDF  4.2.1.1
/usr/src/RPM/BUILD/libnetcdf7-seq-4.2.1.1/examples/C/simple_xy_nc4_wr.c
Go to the documentation of this file.
00001 
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <netcdf.h>
00018 
00019 /* This is the name of the data file we will create. */
00020 #define FILE_NAME "simple_xy_nc4.nc"
00021 
00022 /* We are writing 2D data, a 6 x 12 grid. */
00023 #define NDIMS 2
00024 #define NX 60
00025 #define NY 120
00026 
00027 /* Handle errors by printing an error message and exiting with a
00028  * non-zero status. */
00029 #define ERRCODE 2
00030 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
00031 
00032 int
00033 main()
00034 {
00035    int ncid, x_dimid, y_dimid, varid;
00036    int dimids[NDIMS];
00037    size_t chunks[NDIMS];
00038    int shuffle, deflate, deflate_level;
00039    int data_out[NX][NY];
00040    int x, y, retval;
00041 
00042    /* Set chunking, shuffle, and deflate. */
00043    shuffle = NC_SHUFFLE;
00044    deflate = 1;
00045    deflate_level = 1;
00046 
00047    /* Create some pretend data. If this wasn't an example program, we
00048     * would have some real data to write, for example, model output. */
00049    for (x = 0; x < NX; x++)
00050       for (y = 0; y < NY; y++)
00051          data_out[x][y] = x * NY + y;
00052 
00053    /* Create the file. The NC_NETCDF4 parameter tells netCDF to create
00054     * a file in netCDF-4/HDF5 standard. */
00055    if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
00056       ERR(retval);
00057 
00058    /* Define the dimensions. */
00059    if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
00060       ERR(retval);
00061    if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
00062       ERR(retval);
00063 
00064    /* Set up variabe data. */
00065    dimids[0] = x_dimid;
00066    dimids[1] = y_dimid;
00067    chunks[0] = NX/4;
00068    chunks[1] = NY/4;
00069 
00070    /* Define the variable. */
00071    if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
00072                             dimids, &varid)))
00073       ERR(retval);
00074    if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
00075       ERR(retval);
00076    if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
00077                                     deflate_level)))
00078       ERR(retval);
00079 
00080    /* No need to explicitly end define mode for netCDF-4 files. Write
00081     * the pretend data to the file. */
00082    if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
00083       ERR(retval);
00084 
00085    /* Close the file. */
00086    if ((retval = nc_close(ncid)))
00087       ERR(retval);
00088 
00089    printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
00090    return 0;
00091 }
00092 
 All Data Structures Files Functions Variables Typedefs Defines

Generated on Sat Sep 15 2012 12:44:32 for netCDF. NetCDF is a Unidata library.