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