NetCDF 4.9.3
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 UNITS "units"
53#define PRES_UNITS "hPa"
54#define TEMP_UNITS "celsius"
55#define LAT_UNITS "degrees_north"
56#define LON_UNITS "degrees_east"
57#define MAX_ATT_LEN 80
58
59/* Handle errors by printing an error message and exiting with a
60 * non-zero status. */
61#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
62
63int
64main()
65{
66 int ncid, pres_varid, temp_varid;
67 int lat_varid, lon_varid;
68
69 /* The start and count arrays will tell the netCDF library where to
70 read our data. */
71 size_t start[NDIMS], count[NDIMS];
72
73 /* Program variables to hold the data we will read. We will only
74 need enough space to hold one timestep of data; one record. */
75 float pres_in[NLVL][NLAT][NLON];
76 float temp_in[NLVL][NLAT][NLON];
77
78 /* These program variables hold the latitudes and longitudes. */
79 float lats[NLAT], lons[NLON];
80
81 /* Loop indexes. */
82 int lvl, lat, lon, i = 0;
83
84 /* Error handling. */
85 int retval;
86
87 /* Open the file. */
88 if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
89 ERR(retval);
90
91 /* Get the varids of the latitude and longitude coordinate
92 * variables. */
93 if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
94 ERR(retval);
95 if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
96 ERR(retval);
97
98 /* Read the coordinate variable data. */
99 if ((retval = nc_get_var_float(ncid, lat_varid, &lats[0])))
100 ERR(retval);
101 if ((retval = nc_get_var_float(ncid, lon_varid, &lons[0])))
102 ERR(retval);
103
104 /* Check the coordinate variable data. */
105 for (lat = 0; lat < NLAT; lat++)
106 if (lats[lat] != START_LAT + 5.*lat)
107 return 2;
108 for (lon = 0; lon < NLON; lon++)
109 if (lons[lon] != START_LON + 5.*lon)
110 return 2;
111
112 /* Get the varids of the pressure and temperature netCDF
113 * variables. */
114 if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
115 ERR(retval);
116 if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
117 ERR(retval);
118
119 /* Read the data. Since we know the contents of the file we know
120 * that the data arrays in this program are the correct size to
121 * hold one timestep. */
122 count[0] = 1;
123 count[1] = NLVL;
124 count[2] = NLAT;
125 count[3] = NLON;
126 start[1] = 0;
127 start[2] = 0;
128 start[3] = 0;
129
130 /* Read and check one record at a time. */
131 for (size_t rec = 0; rec < NREC; rec++)
132 {
133 start[0] = rec;
134 if ((retval = nc_get_vara_float(ncid, pres_varid, start,
135 count, &pres_in[0][0][0])))
136 ERR(retval);
137 if ((retval = nc_get_vara_float(ncid, temp_varid, start,
138 count, &temp_in[0][0][0])))
139 ERR(retval);
140
141 /* Check the data. */
142 i = 0;
143 for (lvl = 0; lvl < NLVL; lvl++)
144 for (lat = 0; lat < NLAT; lat++)
145 for (lon = 0; lon < NLON; lon++)
146 {
147 if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i)
148 return 2;
149 if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i)
150 return 2;
151 i++;
152 }
153
154 } /* next record */
155
156 /* Close the file. */
157 if ((retval = nc_close(ncid)))
158 ERR(retval);
159
160 printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
161 return 0;
162}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition dfile.c:1300
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition dfile.c:664
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