NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
pres_temp_4D_rd.c
Go to the documentation of this file.
1 
18 #include <stdio.h>
19 #include <string.h>
20 #include <netcdf.h>
21 
22 /* This is the name of the data file we will read. */
23 #define FILE_NAME "pres_temp_4D.nc"
24 
25 /* We are reading 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
26  timesteps of data. */
27 #define NDIMS 4
28 #define NLAT 6
29 #define NLON 12
30 #define LAT_NAME "latitude"
31 #define LON_NAME "longitude"
32 #define NREC 2
33 #define REC_NAME "time"
34 #define LVL_NAME "level"
35 #define NLVL 2
36 
37 /* Names of things. */
38 #define PRES_NAME "pressure"
39 #define TEMP_NAME "temperature"
40 #define UNITS "units"
41 #define DEGREES_EAST "degrees_east"
42 #define DEGREES_NORTH "degrees_north"
43 
44 /* These are used to calculate the values we expect to find. */
45 #define SAMPLE_PRESSURE 900
46 #define SAMPLE_TEMP 9.0
47 #define START_LAT 25.0
48 #define START_LON -125.0
49 
50 /* For the units attributes. */
51 #define UNITS "units"
52 #define PRES_UNITS "hPa"
53 #define TEMP_UNITS "celsius"
54 #define LAT_UNITS "degrees_north"
55 #define LON_UNITS "degrees_east"
56 #define MAX_ATT_LEN 80
57 
58 /* Handle errors by printing an error message and exiting with a
59  * non-zero status. */
60 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
61 
62 int
63 main()
64 {
65  int ncid, pres_varid, temp_varid;
66  int lat_varid, lon_varid;
67 
68  /* The start and count arrays will tell the netCDF library where to
69  read our data. */
70  size_t start[NDIMS], count[NDIMS];
71 
72  /* Program variables to hold the data we will read. We will only
73  need enough space to hold one timestep of data; one record. */
74  float pres_in[NLVL][NLAT][NLON];
75  float temp_in[NLVL][NLAT][NLON];
76 
77  /* These program variables hold the latitudes and longitudes. */
78  float lats[NLAT], lons[NLON];
79 
80  /* Loop indexes. */
81  int lvl, lat, lon, rec, i = 0;
82 
83  /* Error handling. */
84  int retval;
85 
86  /* Open the file. */
87  if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
88  ERR(retval);
89 
90  /* Get the varids of the latitude and longitude coordinate
91  * variables. */
92  if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
93  ERR(retval);
94  if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
95  ERR(retval);
96 
97  /* Read the coordinate variable data. */
98  if ((retval = nc_get_var_float(ncid, lat_varid, &lats[0])))
99  ERR(retval);
100  if ((retval = nc_get_var_float(ncid, lon_varid, &lons[0])))
101  ERR(retval);
102 
103  /* Check the coordinate variable data. */
104  for (lat = 0; lat < NLAT; lat++)
105  if (lats[lat] != START_LAT + 5.*lat)
106  return 2;
107  for (lon = 0; lon < NLON; lon++)
108  if (lons[lon] != START_LON + 5.*lon)
109  return 2;
110 
111  /* Get the varids of the pressure and temperature netCDF
112  * variables. */
113  if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
114  ERR(retval);
115  if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
116  ERR(retval);
117 
118  /* Read the data. Since we know the contents of the file we know
119  * that the data arrays in this program are the correct size to
120  * hold one timestep. */
121  count[0] = 1;
122  count[1] = NLVL;
123  count[2] = NLAT;
124  count[3] = NLON;
125  start[1] = 0;
126  start[2] = 0;
127  start[3] = 0;
128 
129  /* Read and check one record at a time. */
130  for (rec = 0; rec < NREC; rec++)
131  {
132  start[0] = rec;
133  if ((retval = nc_get_vara_float(ncid, pres_varid, start,
134  count, &pres_in[0][0][0])))
135  ERR(retval);
136  if ((retval = nc_get_vara_float(ncid, temp_varid, start,
137  count, &temp_in[0][0][0])))
138  ERR(retval);
139 
140  /* Check the data. */
141  i = 0;
142  for (lvl = 0; lvl < NLVL; lvl++)
143  for (lat = 0; lat < NLAT; lat++)
144  for (lon = 0; lon < NLON; lon++)
145  {
146  if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i)
147  return 2;
148  if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i)
149  return 2;
150  i++;
151  }
152 
153  } /* next record */
154 
155  /* Close the file. */
156  if ((retval = nc_close(ncid)))
157  ERR(retval);
158 
159  printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
160  return 0;
161 }
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().
Definition: netcdf.h:123
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1470
int nc_get_vara_float(int ncid, int varid, const size_t *startp, const size_t *countp, float *ip)
Read an array of values from a variable.
Definition: dvarget.c:805
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:828
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
Definition: dvarinq.c:62
int nc_get_var_float(int ncid, int varid, float *ip)
Read an entire variable in one call.
Definition: dvarget.c:1087

Return to the Main Unidata NetCDF page.
Generated on Sat Apr 6 2019 08:19:00 for NetCDF. NetCDF is a Unidata library.