netCDF 4.2.1.1
|
00001 00011 #include <stdlib.h> 00012 #include <stdio.h> 00013 #include <netcdf.h> 00014 00015 /* This is the name of the data file we will read. */ 00016 #define FILE_NAME "simple_xy.nc" 00017 00018 /* We are reading 2D data, a 6 x 12 grid. */ 00019 #define NX 6 00020 #define NY 12 00021 00022 /* Handle errors by printing an error message and exiting with a 00023 * non-zero status. */ 00024 #define ERRCODE 2 00025 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} 00026 00027 int 00028 main() 00029 { 00030 /* This will be the netCDF ID for the file and data variable. */ 00031 int ncid, varid; 00032 00033 int data_in[NX][NY]; 00034 00035 /* Loop indexes, and error handling. */ 00036 int x, y, retval; 00037 00038 /* Open the file. NC_NOWRITE tells netCDF we want read-only access 00039 * to the file.*/ 00040 if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid))) 00041 ERR(retval); 00042 00043 /* Get the varid of the data variable, based on its name. */ 00044 if ((retval = nc_inq_varid(ncid, "data", &varid))) 00045 ERR(retval); 00046 00047 /* Read the data. */ 00048 if ((retval = nc_get_var_int(ncid, varid, &data_in[0][0]))) 00049 ERR(retval); 00050 00051 /* Check the data. */ 00052 for (x = 0; x < NX; x++) 00053 for (y = 0; y < NY; y++) 00054 if (data_in[x][y] != x * NY + y) 00055 return ERRCODE; 00056 00057 /* Close the file, freeing all resources. */ 00058 if ((retval = nc_close(ncid))) 00059 ERR(retval); 00060 00061 printf("*** SUCCESS reading example file %s!\n", FILE_NAME); 00062 return 0; 00063 }