netCDF  4.2.1.1
/usr/src/RPM/BUILD/libnetcdf7-mpi-4.2.1.1/examples/C/simple_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_nc4.nc"
00021 
00022 /* We are writing 2D data, a 6 x 12 grid. */
00023 #define NDIMS 2
00024 #define NX 6
00025 #define NY 12
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    /* When we create netCDF variables, groups, dimensions, or types,
00036     * we get back an ID for each one. */
00037    int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
00038    int dimids[NDIMS];
00039 
00040    /* This is the data array we will write. It will be filled with a
00041     * progression of numbers for this example. */
00042    unsigned long long data_out[NX][NY];
00043 
00044    /* Loop indexes, and error handling. */
00045    int x, y, retval;
00046 
00047    /* The following struct is written as a compound type. */
00048    struct s1 
00049    {
00050          int i1;
00051          int i2;
00052    };
00053    struct s1 compound_data[NX][NY];
00054 
00055    /* Create some pretend data. */
00056    for (x = 0; x < NX; x++)
00057       for (y = 0; y < NY; y++)
00058       {
00059          data_out[x][y] = x * NY + y;
00060          compound_data[x][y].i1 = 42;
00061          compound_data[x][y].i2 = -42;
00062       }
00063 
00064    /* Create the file. The NC_NETCDF4 flag tells netCDF to
00065     * create a netCDF-4/HDF5 file.*/
00066    if ((retval = nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)))
00067       ERR(retval);
00068 
00069    /* Define the dimensions in the root group. Dimensions are visible
00070     * in all subgroups. */
00071    if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
00072       ERR(retval);
00073    if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
00074       ERR(retval);
00075 
00076    /* The dimids passes the IDs of the dimensions of the variable. */
00077    dimids[0] = x_dimid;
00078    dimids[1] = y_dimid;
00079 
00080    /* Define two groups, "grp1" and "grp2." */
00081    if ((retval = nc_def_grp(ncid, "grp1", &grp1id)))
00082       ERR (retval);
00083    if ((retval = nc_def_grp(ncid, "grp2", &grp2id)))
00084       ERR (retval);
00085 
00086    /* Define an unsigned 64bit integer variable in grp1, using dimensions
00087     * in the root group. */
00088    if ((retval = nc_def_var(grp1id, "data", NC_UINT64, NDIMS, 
00089                             dimids, &varid1)))
00090       ERR(retval);
00091 
00092    /* Write unsigned long long data to the file. For netCDF-4 files,
00093     * nc_enddef will be called automatically. */
00094    if ((retval = nc_put_var_ulonglong(grp1id, varid1, &data_out[0][0])))
00095       ERR(retval);
00096 
00097    /* Create a compound type. This will cause nc_reddef to be called. */
00098    if (nc_def_compound(grp2id, sizeof(struct s1), "sample_compound_type", 
00099                        &typeid))
00100       ERR(retval);
00101    if (nc_insert_compound(grp2id, typeid, "i1", 
00102                           offsetof(struct s1, i1), NC_INT))
00103       ERR(retval);
00104    if (nc_insert_compound(grp2id, typeid, "i2", 
00105                           offsetof(struct s1, i2), NC_INT))
00106       ERR(retval);
00107 
00108    /* Define a compound type variable in grp2, using dimensions
00109     * in the root group. */
00110    if ((retval = nc_def_var(grp2id, "data", typeid, NDIMS, 
00111                             dimids, &varid2)))
00112       ERR(retval);
00113 
00114    /* Write the array of struct to the file. This will cause nc_endef
00115     * to be called. */
00116    if ((retval = nc_put_var(grp2id, varid2, &compound_data[0][0])))
00117       ERR(retval);
00118 
00119    /* Close the file. */
00120    if ((retval = nc_close(ncid)))
00121       ERR(retval);
00122 
00123    printf("*** SUCCESS writing example file simple_nc4.nc!\n");
00124    return 0;
00125 }
 All Data Structures Files Functions Variables Typedefs Defines

Generated on Fri Sep 14 2012 07:36:39 for netCDF. NetCDF is a Unidata library.