netCDF
4.2.1.1
|
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 read. */ 00020 #define FILE_NAME "simple_nc4.nc" 00021 00022 /* We are reading 2D data, a 6 x 12 grid. */ 00023 #define NX 6 00024 #define NY 12 00025 00026 /* Handle errors by printing an error message and exiting with a 00027 * non-zero status. */ 00028 #define ERRCODE 2 00029 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} 00030 00031 int 00032 main() 00033 { 00034 /* There will be netCDF IDs for the file, each group, and each 00035 * variable. */ 00036 int ncid, varid1, varid2, grp1id, grp2id; 00037 00038 unsigned long long data_in[NX][NY]; 00039 00040 /* Loop indexes, and error handling. */ 00041 int x, y, retval; 00042 00043 /* The following struct is written as a compound type. */ 00044 struct s1 00045 { 00046 int i1; 00047 int i2; 00048 }; 00049 struct s1 compound_data[NX][NY]; 00050 00051 /* Open the file. NC_NOWRITE tells netCDF we want read-only access 00052 * to the file.*/ 00053 if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid))) 00054 ERR(retval); 00055 00056 /* Get the group ids of our two groups. */ 00057 if ((retval = nc_inq_ncid(ncid, "grp1", &grp1id))) 00058 ERR(retval); 00059 if ((retval = nc_inq_ncid(ncid, "grp2", &grp2id))) 00060 ERR(retval); 00061 00062 /* Get the varid of the uint64 data variable, based on its name, in 00063 * grp1. */ 00064 if ((retval = nc_inq_varid(grp1id, "data", &varid1))) 00065 ERR(retval); 00066 00067 /* Read the data. */ 00068 if ((retval = nc_get_var_ulonglong(grp1id, varid1, &data_in[0][0]))) 00069 ERR(retval); 00070 00071 /* Get the varid of the compound data variable, based on its name, 00072 * in grp2. */ 00073 if ((retval = nc_inq_varid(grp2id, "data", &varid2))) 00074 ERR(retval); 00075 00076 /* Read the data. */ 00077 if ((retval = nc_get_var(grp2id, varid2, &compound_data[0][0]))) 00078 ERR(retval); 00079 00080 /* Check the data. */ 00081 for (x = 0; x < NX; x++) 00082 for (y = 0; y < NY; y++) 00083 { 00084 if (data_in[x][y] != x * NY + y || 00085 compound_data[x][y].i1 != 42 || 00086 compound_data[x][y].i2 != -42) 00087 return ERRCODE; 00088 } 00089 00090 /* Close the file, freeing all resources. */ 00091 if ((retval = nc_close(ncid))) 00092 ERR(retval); 00093 00094 printf("*** SUCCESS reading example file %s!\n", FILE_NAME); 00095 return 0; 00096 }