MPTRAC
Functions
tropo.c File Reference

Create tropopause data set from meteorological data. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

Create tropopause data set from meteorological data.

Definition in file tropo.c.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file tropo.c.

29 {
30
31 ctl_t ctl;
32
33 clim_t *clim;
34
35 met_t *met;
36
37 dd_t *dd;
38
39 static double ps[EX * EY], pt[EX * EY], qt[EX * EY], o3t[EX * EY],
40 zs[EX * EY], zt[EX * EY], tt[EX * EY], lon, lons[EX], lat, lats[EY];
41
42 static int init, nx, ny, nt, ncid, varid, dims[3];
43
44 static size_t count[10], start[10];
45
46 /* Allocate... */
47 ALLOC(clim, clim_t, 1);
48 ALLOC(met, met_t, 1);
49 ALLOC(dd, dd_t, 1);
50
51 /* Check arguments... */
52 if (argc < 4)
53 ERRMSG("Give parameters: <ctl> <tropo.nc> <met0> [ <met1> ... ]");
54
55 /* Read control parameters... */
56 mptrac_read_ctl(argv[1], argc, argv, &ctl);
57 double lon0 = scan_ctl(argv[1], argc, argv, "TROPO_LON0", -1, "-180", NULL);
58 double lon1 = scan_ctl(argv[1], argc, argv, "TROPO_LON1", -1, "180", NULL);
59 double dlon = scan_ctl(argv[1], argc, argv, "TROPO_DLON", -1, "-999", NULL);
60 double lat0 = scan_ctl(argv[1], argc, argv, "TROPO_LAT0", -1, "-90", NULL);
61 double lat1 = scan_ctl(argv[1], argc, argv, "TROPO_LAT1", -1, "90", NULL);
62 double dlat = scan_ctl(argv[1], argc, argv, "TROPO_DLAT", -1, "-999", NULL);
63 int h2o = (int) scan_ctl(argv[1], argc, argv, "TROPO_H2O", -1, "1", NULL);
64 int o3 = (int) scan_ctl(argv[1], argc, argv, "TROPO_O3", -1, "1", NULL);
65
66 /* Read climatological data... */
67 mptrac_read_clim(&ctl, clim);
68
69 /* Loop over files... */
70 for (int i = 3; i < argc; i++) {
71
72 /* Set control parameters... */
73 ctl.met_tropo = 0;
74
75 /* Read meteorological data... */
76 if (!mptrac_read_met(argv[i], &ctl, clim, met, dd))
77 continue;
78
79 /* Set horizontal grid... */
80 if (!init) {
81 init = 1;
82
83 /* Get grid... */
84 if (dlon <= 0)
85 dlon = fabs(met->lon[1] - met->lon[0]);
86 if (dlat <= 0)
87 dlat = fabs(met->lat[1] - met->lat[0]);
88 if (lon0 < -360 && lon1 > 360) {
89 lon0 = gsl_stats_min(met->lon, 1, (size_t) met->nx);
90 lon1 = gsl_stats_max(met->lon, 1, (size_t) met->nx);
91 }
92 nx = ny = 0;
93 for (lon = lon0; lon <= lon1 + 0.001; lon += dlon) {
94 lons[nx] = round(lon * 1e3) / 1e3;
95 if ((++nx) >= EX)
96 ERRMSG("Too many longitudes!");
97 }
98 if (lat0 < -90 && lat1 > 90) {
99 lat0 = gsl_stats_min(met->lat, 1, (size_t) met->ny);
100 lat1 = gsl_stats_max(met->lat, 1, (size_t) met->ny);
101 }
102 for (lat = lat0; lat <= lat1 + 0.001; lat += dlat) {
103 lats[ny] = round(lat * 1e3) / 1e3;
104 if ((++ny) >= EY)
105 ERRMSG("Too many latitudes!");
106 }
107
108 /* Create netCDF file... */
109 LOG(1, "Write tropopause data file: %s", argv[2]);
110 NC(nc_create(argv[2], NC_NETCDF4, &ncid));
111
112 /* Create dimensions... */
113 NC(nc_def_dim(ncid, "time", (size_t) NC_UNLIMITED, &dims[0]));
114 NC(nc_def_dim(ncid, "lat", (size_t) ny, &dims[1]));
115 NC(nc_def_dim(ncid, "lon", (size_t) nx, &dims[2]));
116
117 /* Create variables... */
118 NC_DEF_VAR("time", NC_DOUBLE, 1, &dims[0], "time",
119 "seconds since 2000-01-01 00:00:00 UTC", 0, 0);
120 NC_DEF_VAR("lat", NC_DOUBLE, 1, &dims[1], "latitude", "degrees_north",
121 0, 0);
122 NC_DEF_VAR("lon", NC_DOUBLE, 1, &dims[2], "longitude", "degrees_east",
123 0, 0);
124
125 NC_DEF_VAR("clp_z", NC_FLOAT, 3, &dims[0], "cold point height", "km", 0,
126 0);
127 NC_DEF_VAR("clp_p", NC_FLOAT, 3, &dims[0], "cold point pressure", "hPa",
128 0, 0);
129 NC_DEF_VAR("clp_t", NC_FLOAT, 3, &dims[0], "cold point temperature",
130 "K", 0, 0);
131 if (h2o)
132 NC_DEF_VAR("clp_q", NC_FLOAT, 3, &dims[0], "cold point water vapor",
133 "ppv", 0, 0);
134 if (o3)
135 NC_DEF_VAR("clp_o3", NC_FLOAT, 3, &dims[0], "cold point ozone",
136 "ppv", 0, 0);
137
138 NC_DEF_VAR("dyn_z", NC_FLOAT, 3, &dims[0],
139 "dynamical tropopause height", "km", 0, 0);
140 NC_DEF_VAR("dyn_p", NC_FLOAT, 3, &dims[0],
141 "dynamical tropopause pressure", "hPa", 0, 0);
142 NC_DEF_VAR("dyn_t", NC_FLOAT, 3, &dims[0],
143 "dynamical tropopause temperature", "K", 0, 0);
144 if (h2o)
145 NC_DEF_VAR("dyn_q", NC_FLOAT, 3, &dims[0],
146 "dynamical tropopause water vapor", "ppv", 0, 0);
147 if (o3)
148 NC_DEF_VAR("dyn_o3", NC_FLOAT, 3, &dims[0],
149 "dynamical tropopause ozone", "ppv", 0, 0);
150
151 NC_DEF_VAR("wmo_1st_z", NC_FLOAT, 3, &dims[0],
152 "WMO 1st tropopause height", "km", 0, 0);
153 NC_DEF_VAR("wmo_1st_p", NC_FLOAT, 3, &dims[0],
154 "WMO 1st tropopause pressure", "hPa", 0, 0);
155 NC_DEF_VAR("wmo_1st_t", NC_FLOAT, 3, &dims[0],
156 "WMO 1st tropopause temperature", "K", 0, 0);
157 if (h2o)
158 NC_DEF_VAR("wmo_1st_q", NC_FLOAT, 3, &dims[0],
159 "WMO 1st tropopause water vapor", "ppv", 0, 0);
160 if (o3)
161 NC_DEF_VAR("wmo_1st_o3", NC_FLOAT, 3, &dims[0],
162 "WMO 1st tropopause ozone", "ppv", 0, 0);
163
164 NC_DEF_VAR("wmo_2nd_z", NC_FLOAT, 3, &dims[0],
165 "WMO 2nd tropopause height", "km", 0, 0);
166 NC_DEF_VAR("wmo_2nd_p", NC_FLOAT, 3, &dims[0],
167 "WMO 2nd tropopause pressure", "hPa", 0, 0);
168 NC_DEF_VAR("wmo_2nd_t", NC_FLOAT, 3, &dims[0],
169 "WMO 2nd tropopause temperature", "K", 0, 0);
170 if (h2o)
171 NC_DEF_VAR("wmo_2nd_q", NC_FLOAT, 3, &dims[0],
172 "WMO 2nd tropopause water vapor", "ppv", 0, 0);
173 if (o3)
174 NC_DEF_VAR("wmo_2nd_o3", NC_FLOAT, 3, &dims[0],
175 "WMO 2nd tropopause ozone", "ppv", 0, 0);
176
177 NC_DEF_VAR("ps", NC_FLOAT, 3, &dims[0], "surface pressure", "hPa", 0,
178 0);
179 NC_DEF_VAR("zs", NC_FLOAT, 3, &dims[0], "surface height", "km", 0, 0);
180
181 /* End definition... */
182 NC(nc_enddef(ncid));
183
184 /* Write longitude and latitude... */
185 NC_PUT_DOUBLE("lat", lats, 0);
186 NC_PUT_DOUBLE("lon", lons, 0);
187 }
188
189 /* Write time... */
190 start[0] = (size_t) nt;
191 count[0] = 1;
192 start[1] = 0;
193 count[1] = (size_t) ny;
194 start[2] = 0;
195 count[2] = (size_t) nx;
196 NC_PUT_DOUBLE("time", &met->time, 1);
197
198 /* Get cold point... */
199 get_tropo(2, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
200 zs);
201 NC_PUT_DOUBLE("clp_z", zt, 1);
202 NC_PUT_DOUBLE("clp_p", pt, 1);
203 NC_PUT_DOUBLE("clp_t", tt, 1);
204 if (h2o)
205 NC_PUT_DOUBLE("clp_q", qt, 1);
206 if (o3)
207 NC_PUT_DOUBLE("clp_o3", o3t, 1);
208
209 /* Get dynamical tropopause... */
210 get_tropo(5, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
211 zs);
212 NC_PUT_DOUBLE("dyn_z", zt, 1);
213 NC_PUT_DOUBLE("dyn_p", pt, 1);
214 NC_PUT_DOUBLE("dyn_t", tt, 1);
215 if (h2o)
216 NC_PUT_DOUBLE("dyn_q", qt, 1);
217 if (o3)
218 NC_PUT_DOUBLE("dyn_o3", o3t, 1);
219
220 /* Get WMO 1st tropopause... */
221 get_tropo(3, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
222 zs);
223 NC_PUT_DOUBLE("wmo_1st_z", zt, 1);
224 NC_PUT_DOUBLE("wmo_1st_p", pt, 1);
225 NC_PUT_DOUBLE("wmo_1st_t", tt, 1);
226 if (h2o)
227 NC_PUT_DOUBLE("wmo_1st_q", qt, 1);
228 if (o3)
229 NC_PUT_DOUBLE("wmo_1st_o3", o3t, 1);
230
231 /* Get WMO 2nd tropopause... */
232 get_tropo(4, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
233 zs);
234 NC_PUT_DOUBLE("wmo_2nd_z", zt, 1);
235 NC_PUT_DOUBLE("wmo_2nd_p", pt, 1);
236 NC_PUT_DOUBLE("wmo_2nd_t", tt, 1);
237 if (h2o)
238 NC_PUT_DOUBLE("wmo_2nd_q", qt, 1);
239 if (o3)
240 NC_PUT_DOUBLE("wmo_2nd_o3", o3t, 1);
241
242 /* Write surface data... */
243 NC_PUT_DOUBLE("ps", ps, 1);
244 NC_PUT_DOUBLE("zs", zs, 1);
245
246 /* Increment time step counter... */
247 nt++;
248 }
249
250 /* Close file... */
251 NC(nc_close(ncid));
252
253 /* Free... */
254 free(clim);
255 free(met);
256
257 return EXIT_SUCCESS;
258}
void get_tropo(const int met_tropo, ctl_t *ctl, clim_t *clim, met_t *met, const double *lons, const int nx, const double *lats, const int ny, double *pt, double *zt, double *tt, double *qt, double *o3t, double *ps, double *zs)
Calculate tropopause data.
Definition: mptrac.c:2024
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:10899
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:5406
int mptrac_read_met(const char *filename, const ctl_t *ctl, const clim_t *clim, met_t *met, dd_t *dd)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:6357
void mptrac_read_ctl(const char *filename, int argc, char *argv[], ctl_t *ctl)
Reads control parameters from a configuration file and populates the given structure.
Definition: mptrac.c:5466
#define NC(cmd)
Execute a NetCDF command and check for errors.
Definition: mptrac.h:1167
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2043
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:293
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:288
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:416
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1973
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1196
#define NC_PUT_DOUBLE(varname, ptr, hyperslab)
Write double precision data to a NetCDF variable.
Definition: mptrac.h:1280
Climatological data.
Definition: mptrac.h:3487
Control parameters.
Definition: mptrac.h:2264
int met_tropo
Tropopause definition (0=none, 1=clim, 2=cold point, 3=WMO_1st, 4=WMO_2nd, 5=dynamical).
Definition: mptrac.h:2738
Domain decomposition data structure.
Definition: mptrac.h:3720
Meteo data structure.
Definition: mptrac.h:3546
int nx
Number of longitudes.
Definition: mptrac.h:3552
int ny
Number of latitudes.
Definition: mptrac.h:3555
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3564
double time
Time [s].
Definition: mptrac.h:3549
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3567
Here is the call graph for this function: