NetCDF 4.10.1
Loading...
Searching...
No Matches
pres_temp_4D_rd.c
Go to the documentation of this file.
1/* Copyright 2006-2011 University Corporation for Atmospheric
2Research/Unidata. See COPYRIGHT file for conditions of use. */
18
19#include <stdio.h>
20#include <string.h>
21#include <netcdf.h>
22
23/* This is the name of the data file we will read. */
24#define FILE_NAME "pres_temp_4D.nc"
25
26/* We are reading 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
27 timesteps of data. */
28#define NDIMS 4
29#define NLAT 6
30#define NLON 12
31#define LAT_NAME "latitude"
32#define LON_NAME "longitude"
33#define NREC 2
34#define REC_NAME "time"
35#define LVL_NAME "level"
36#define NLVL 2
37
38/* Names of things. */
39#define PRES_NAME "pressure"
40#define TEMP_NAME "temperature"
41#define UNITS "units"
42#define DEGREES_EAST "degrees_east"
43#define DEGREES_NORTH "degrees_north"
44
45/* These are used to calculate the values we expect to find. */
46#define SAMPLE_PRESSURE 900
47#define SAMPLE_TEMP 9.0
48#define START_LAT 25.0
49#define START_LON -125.0
50
51/* For the units attributes. */
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
62int
63main()
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, 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 (size_t 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}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition dfile.c:1330
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition dfile.c:694
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
Definition dvarinq.c:60
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().
Definition netcdf.h:135