netCDF 4.2.1.1
|
00001 00006 #include <stdlib.h> 00007 #include <stdio.h> 00008 #include <netcdf.h> 00009 00010 /* This is the name of the data file we will create. */ 00011 #define FILE_NAME "simple_xy.nc" 00012 00013 /* We are writing 2D data, a 6 x 12 grid. */ 00014 #define NDIMS 2 00015 #define NX 6 00016 #define NY 12 00017 00018 /* Handle errors by printing an error message and exiting with a 00019 * non-zero status. */ 00020 #define ERRCODE 2 00021 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} 00022 00023 int 00024 main() 00025 { 00026 /* When we create netCDF variables and dimensions, we get back an 00027 * ID for each one. */ 00028 int ncid, x_dimid, y_dimid, varid; 00029 int dimids[NDIMS]; 00030 00031 /* This is the data array we will write. It will be filled with a 00032 * progression of numbers for this example. */ 00033 int data_out[NX][NY]; 00034 00035 /* Loop indexes, and error handling. */ 00036 int x, y, retval; 00037 00038 /* Create some pretend data. If this wasn't an example program, we 00039 * would have some real data to write, for example, model 00040 * output. */ 00041 for (x = 0; x < NX; x++) 00042 for (y = 0; y < NY; y++) 00043 data_out[x][y] = x * NY + y; 00044 00045 /* Always check the return code of every netCDF function call. In 00046 * this example program, any retval which is not equal to NC_NOERR 00047 * (0) will cause the program to print an error message and exit 00048 * with a non-zero return code. */ 00049 00050 /* Create the file. The NC_CLOBBER parameter tells netCDF to 00051 * overwrite this file, if it already exists.*/ 00052 if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid))) 00053 ERR(retval); 00054 00055 /* Define the dimensions. NetCDF will hand back an ID for each. */ 00056 if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid))) 00057 ERR(retval); 00058 if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid))) 00059 ERR(retval); 00060 00061 /* The dimids array is used to pass the IDs of the dimensions of 00062 * the variable. */ 00063 dimids[0] = x_dimid; 00064 dimids[1] = y_dimid; 00065 00066 /* Define the variable. The type of the variable in this case is 00067 * NC_INT (4-byte integer). */ 00068 if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS, 00069 dimids, &varid))) 00070 ERR(retval); 00071 00072 /* End define mode. This tells netCDF we are done defining 00073 * metadata. */ 00074 if ((retval = nc_enddef(ncid))) 00075 ERR(retval); 00076 00077 /* Write the pretend data to the file. Although netCDF supports 00078 * reading and writing subsets of data, in this case we write all 00079 * the data in one operation. */ 00080 if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0]))) 00081 ERR(retval); 00082 00083 /* Close the file. This frees up any internal netCDF resources 00084 * associated with the file, and flushes any buffers. */ 00085 if ((retval = nc_close(ncid))) 00086 ERR(retval); 00087 00088 printf("*** SUCCESS writing example file simple_xy.nc!\n"); 00089 return 0; 00090 }