MPTRAC
Macros | Functions
met_map.c File Reference

Extract map from meteorological data. More...

#include "mptrac.h"

Go to the source code of this file.

Macros

#define NX   EX
 Maximum number of longitudes. More...
 
#define NY   EY
 Maximum number of latitudes. More...
 

Functions

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

Detailed Description

Extract map from meteorological data.

Definition in file met_map.c.

Macro Definition Documentation

◆ NX

#define NX   EX

Maximum number of longitudes.

Definition at line 32 of file met_map.c.

◆ NY

#define NY   EY

Maximum number of latitudes.

Definition at line 35 of file met_map.c.

Function Documentation

◆ main()

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

Definition at line 41 of file met_map.c.

43 {
44
45 ctl_t ctl;
46
47 clim_t *clim;
48
49 met_t *met;
50
51 FILE *out;
52
53 static double *timem, ps, *psm, ts, *tsm, zs, *zsm, us, *usm, vs, *vsm, lsm,
54 *lsmm, sst, *sstm, pbl, *pblm, pt, *ptm, t, *pm, *tm, u, *um, v, *vm, w,
55 *wm, h2o, *h2om, h2ot, *h2otm, o3, *o3m, *hno3m, *ohm, *h2o2m, *ho2m,
56 *o1dm, *tdewm, *ticem, *tnatm, lwc, *lwcm, rwc, *rwcm, iwc, *iwcm, swc,
57 *swcm, cc, *ccm, z, *zm, pv, *pvm, zt, *ztm, tt, *ttm, pct, *pctm, pcb,
58 *pcbm, cl, *clm, plcl, *plclm, plfc, *plfcm, pel, *pelm, cape, *capem,
59 cin, *cinm, o3c, *o3cm, *rhm, *rhicem, ptop, pbot, t0, lon, lons[NX], lat,
60 lats[NY];
61
62 static int *np, *npc, *npt, nx, ny;
63
64 /* Allocate... */
65 ALLOC(clim, clim_t, 1);
66 ALLOC(met, met_t, 1);
67 ALLOC(timem, double,
68 NX * NY);
69 ALLOC(psm, double,
70 NX * NY);
71 ALLOC(tsm, double,
72 NX * NY);
73 ALLOC(zsm, double,
74 NX * NY);
75 ALLOC(usm, double,
76 NX * NY);
77 ALLOC(vsm, double,
78 NX * NY);
79 ALLOC(lsmm, double,
80 NX * NY);
81 ALLOC(sstm, double,
82 NX * NY);
83 ALLOC(pblm, double,
84 NX * NY);
85 ALLOC(ptm, double,
86 NX * NY);
87 ALLOC(pm, double,
88 NX * NY);
89 ALLOC(tm, double,
90 NX * NY);
91 ALLOC(um, double,
92 NX * NY);
93 ALLOC(vm, double,
94 NX * NY);
95 ALLOC(wm, double,
96 NX * NY);
97 ALLOC(h2om, double,
98 NX * NY);
99 ALLOC(h2otm, double,
100 NX * NY);
101 ALLOC(o3m, double,
102 NX * NY);
103 ALLOC(hno3m, double,
104 NX * NY);
105 ALLOC(ohm, double,
106 NX * NY);
107 ALLOC(h2o2m, double,
108 NX * NY);
109 ALLOC(ho2m, double,
110 NX * NY);
111 ALLOC(o1dm, double,
112 NX * NY);
113 ALLOC(tdewm, double,
114 NX * NY);
115 ALLOC(ticem, double,
116 NX * NY);
117 ALLOC(tnatm, double,
118 NX * NY);
119 ALLOC(lwcm, double,
120 NX * NY);
121 ALLOC(rwcm, double,
122 NX * NY);
123 ALLOC(iwcm, double,
124 NX * NY);
125 ALLOC(swcm, double,
126 NX * NY);
127 ALLOC(ccm, double,
128 NX * NY);
129 ALLOC(zm, double,
130 NX * NY);
131 ALLOC(pvm, double,
132 NX * NY);
133 ALLOC(ztm, double,
134 NX * NY);
135 ALLOC(ttm, double,
136 NX * NY);
137 ALLOC(pctm, double,
138 NX * NY);
139 ALLOC(pcbm, double,
140 NX * NY);
141 ALLOC(clm, double,
142 NX * NY);
143 ALLOC(plclm, double,
144 NX * NY);
145 ALLOC(plfcm, double,
146 NX * NY);
147 ALLOC(pelm, double,
148 NX * NY);
149 ALLOC(capem, double,
150 NX * NY);
151 ALLOC(cinm, double,
152 NX * NY);
153 ALLOC(o3cm, double,
154 NX * NY);
155 ALLOC(rhm, double,
156 NX * NY);
157 ALLOC(rhicem, double,
158 NX * NY);
159 ALLOC(np, int,
160 NX * NY);
161 ALLOC(npc, int,
162 NX * NY);
163 ALLOC(npt, int,
164 NX * NY);
165
166 /* Check arguments... */
167 if (argc < 4)
168 ERRMSG("Give parameters: <ctl> <map.tab> <met0> [ <met1> ... ]");
169
170 /* Read control parameters... */
171 read_ctl(argv[1], argc, argv, &ctl);
172 double p0 = P(scan_ctl(argv[1], argc, argv, "MAP_Z0", -1, "10", NULL));
173 double lon0 = scan_ctl(argv[1], argc, argv, "MAP_LON0", -1, "-180", NULL);
174 double lon1 = scan_ctl(argv[1], argc, argv, "MAP_LON1", -1, "180", NULL);
175 double dlon = scan_ctl(argv[1], argc, argv, "MAP_DLON", -1, "-999", NULL);
176 double lat0 = scan_ctl(argv[1], argc, argv, "MAP_LAT0", -1, "-90", NULL);
177 double lat1 = scan_ctl(argv[1], argc, argv, "MAP_LAT1", -1, "90", NULL);
178 double dlat = scan_ctl(argv[1], argc, argv, "MAP_DLAT", -1, "-999", NULL);
179 double theta = scan_ctl(argv[1], argc, argv, "MAP_THETA", -1, "-999", NULL);
180
181 /* Read climatological data... */
182 read_clim(&ctl, clim);
183
184 /* Loop over files... */
185 for (int i = 3; i < argc; i++) {
186
187 /* Read meteorological data... */
188 if (!read_met(argv[i], &ctl, clim, met))
189 continue;
190
191 /* Set horizontal grid... */
192 if (dlon <= 0)
193 dlon = fabs(met->lon[1] - met->lon[0]);
194 if (dlat <= 0)
195 dlat = fabs(met->lat[1] - met->lat[0]);
196 if (lon0 < -360 && lon1 > 360) {
197 lon0 = gsl_stats_min(met->lon, 1, (size_t) met->nx);
198 lon1 = gsl_stats_max(met->lon, 1, (size_t) met->nx);
199 }
200 nx = ny = 0;
201 for (lon = lon0; lon <= lon1; lon += dlon) {
202 lons[nx] = lon;
203 if ((++nx) >= NX)
204 ERRMSG("Too many longitudes!");
205 }
206 if (lat0 < -90 && lat1 > 90) {
207 lat0 = gsl_stats_min(met->lat, 1, (size_t) met->ny);
208 lat1 = gsl_stats_max(met->lat, 1, (size_t) met->ny);
209 }
210 for (lat = lat0; lat <= lat1; lat += dlat) {
211 lats[ny] = lat;
212 if ((++ny) >= NY)
213 ERRMSG("Too many latitudes!");
214 }
215
216 /* Average... */
217 for (int ix = 0; ix < nx; ix++)
218 for (int iy = 0; iy < ny; iy++) {
219
220 /* Find pressure level for given theta level... */
222 if (theta > 0) {
223 ptop = met->p[met->np - 1];
224 pbot = met->p[0];
225 do {
226 p0 = 0.5 * (ptop + pbot);
227 intpol_met_space_3d(met, met->t, p0, lons[ix], lats[iy],
228 &t0, ci, cw, 1);
229 if (THETA(p0, t0) > theta)
230 ptop = p0;
231 else
232 pbot = p0;
233 } while (fabs(ptop - pbot) > 1e-5);
234 }
235
236 /* Interpolate meteo data... */
237 INTPOL_SPACE_ALL(p0, lons[ix], lats[iy]);
238
239 /* Averaging... */
240 timem[iy * nx + ix] += met->time;
241 zm[iy * nx + ix] += z;
242 pm[iy * nx + ix] += p0;
243 tm[iy * nx + ix] += t;
244 um[iy * nx + ix] += u;
245 vm[iy * nx + ix] += v;
246 wm[iy * nx + ix] += w;
247 pvm[iy * nx + ix] += pv;
248 h2om[iy * nx + ix] += h2o;
249 o3m[iy * nx + ix] += o3;
250 lwcm[iy * nx + ix] += lwc;
251 rwcm[iy * nx + ix] += rwc;
252 iwcm[iy * nx + ix] += iwc;
253 swcm[iy * nx + ix] += swc;
254 ccm[iy * nx + ix] += cc;
255 psm[iy * nx + ix] += ps;
256 tsm[iy * nx + ix] += ts;
257 zsm[iy * nx + ix] += zs;
258 usm[iy * nx + ix] += us;
259 vsm[iy * nx + ix] += vs;
260 lsmm[iy * nx + ix] += lsm;
261 sstm[iy * nx + ix] += sst;
262 pblm[iy * nx + ix] += pbl;
263 pctm[iy * nx + ix] += pct;
264 pcbm[iy * nx + ix] += pcb;
265 clm[iy * nx + ix] += cl;
266 if (isfinite(plfc) && isfinite(pel) && cape >= ctl.conv_cape
267 && (ctl.conv_cin <= 0 || cin < ctl.conv_cin)) {
268 plclm[iy * nx + ix] += plcl;
269 plfcm[iy * nx + ix] += plfc;
270 pelm[iy * nx + ix] += pel;
271 capem[iy * nx + ix] += cape;
272 cinm[iy * nx + ix] += cin;
273 npc[iy * nx + ix]++;
274 }
275 if (isfinite(pt)) {
276 ptm[iy * nx + ix] += pt;
277 ztm[iy * nx + ix] += zt;
278 ttm[iy * nx + ix] += tt;
279 h2otm[iy * nx + ix] += h2ot;
280 npt[iy * nx + ix]++;
281 }
282 o3cm[iy * nx + ix] += o3c;
283 hno3m[iy * nx + ix] += clim_zm(&clim->hno3, met->time, lats[iy], p0);
284 tnatm[iy * nx + ix] +=
285 nat_temperature(p0, h2o,
286 clim_zm(&clim->hno3, met->time, lats[iy], p0));
287 ohm[iy * nx + ix] +=
288 clim_oh(&ctl, clim, met->time, lons[ix], lats[iy], p0);
289 h2o2m[iy * nx + ix] += clim_zm(&clim->h2o2, met->time, lats[iy], p0);
290 ho2m[iy * nx + ix] += clim_zm(&clim->ho2, met->time, lats[iy], p0);
291 o1dm[iy * nx + ix] += clim_zm(&clim->o1d, met->time, lats[iy], p0);
292 rhm[iy * nx + ix] += RH(p0, t, h2o);
293 rhicem[iy * nx + ix] += RHICE(p0, t, h2o);
294 tdewm[iy * nx + ix] += TDEW(p0, h2o);
295 ticem[iy * nx + ix] += TICE(p0, h2o);
296 np[iy * nx + ix]++;
297 }
298 }
299
300 /* Create output file... */
301 LOG(1, "Write meteorological data file: %s", argv[2]);
302 if (!(out = fopen(argv[2], "w")))
303 ERRMSG("Cannot create file!");
304
305 /* Write header... */
307
308 /* Write data... */
309 for (int iy = 0; iy < ny; iy++) {
310 fprintf(out, "\n");
311 for (int ix = 0; ix < nx; ix++)
312 fprintf(out,
313 "%.2f %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
314 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
315 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %d\n",
316 timem[iy * nx + ix] / np[iy * nx + ix],
317 Z(pm[iy * nx + ix] / np[iy * nx + ix]), lons[ix], lats[iy],
318 pm[iy * nx + ix] / np[iy * nx + ix],
319 tm[iy * nx + ix] / np[iy * nx + ix],
320 um[iy * nx + ix] / np[iy * nx + ix],
321 vm[iy * nx + ix] / np[iy * nx + ix],
322 wm[iy * nx + ix] / np[iy * nx + ix],
323 h2om[iy * nx + ix] / np[iy * nx + ix],
324 o3m[iy * nx + ix] / np[iy * nx + ix],
325 zm[iy * nx + ix] / np[iy * nx + ix],
326 pvm[iy * nx + ix] / np[iy * nx + ix],
327 psm[iy * nx + ix] / np[iy * nx + ix],
328 tsm[iy * nx + ix] / np[iy * nx + ix],
329 zsm[iy * nx + ix] / np[iy * nx + ix],
330 usm[iy * nx + ix] / np[iy * nx + ix],
331 vsm[iy * nx + ix] / np[iy * nx + ix],
332 lsmm[iy * nx + ix] / np[iy * nx + ix],
333 sstm[iy * nx + ix] / np[iy * nx + ix],
334 ptm[iy * nx + ix] / npt[iy * nx + ix],
335 ztm[iy * nx + ix] / npt[iy * nx + ix],
336 ttm[iy * nx + ix] / npt[iy * nx + ix],
337 h2otm[iy * nx + ix] / npt[iy * nx + ix],
338 lwcm[iy * nx + ix] / np[iy * nx + ix],
339 rwcm[iy * nx + ix] / np[iy * nx + ix],
340 iwcm[iy * nx + ix] / np[iy * nx + ix],
341 swcm[iy * nx + ix] / np[iy * nx + ix],
342 ccm[iy * nx + ix] / np[iy * nx + ix],
343 clm[iy * nx + ix] / np[iy * nx + ix],
344 pctm[iy * nx + ix] / np[iy * nx + ix],
345 pcbm[iy * nx + ix] / np[iy * nx + ix],
346 plclm[iy * nx + ix] / npc[iy * nx + ix],
347 plfcm[iy * nx + ix] / npc[iy * nx + ix],
348 pelm[iy * nx + ix] / npc[iy * nx + ix],
349 capem[iy * nx + ix] / npc[iy * nx + ix],
350 cinm[iy * nx + ix] / npc[iy * nx + ix],
351 rhm[iy * nx + ix] / np[iy * nx + ix],
352 rhicem[iy * nx + ix] / np[iy * nx + ix],
353 tdewm[iy * nx + ix] / np[iy * nx + ix],
354 ticem[iy * nx + ix] / np[iy * nx + ix],
355 tnatm[iy * nx + ix] / np[iy * nx + ix],
356 hno3m[iy * nx + ix] / np[iy * nx + ix],
357 ohm[iy * nx + ix] / np[iy * nx + ix],
358 h2o2m[iy * nx + ix] / np[iy * nx + ix],
359 ho2m[iy * nx + ix] / np[iy * nx + ix],
360 o1dm[iy * nx + ix] / np[iy * nx + ix],
361 pblm[iy * nx + ix] / np[iy * nx + ix],
362 o3cm[iy * nx + ix] / np[iy * nx + ix], np[iy * nx + ix],
363 npt[iy * nx + ix], npc[iy * nx + ix]);
364 }
365
366 /* Close file... */
367 fclose(out);
368
369 /* Free... */
370 free(clim);
371 free(met);
372 free(timem);
373 free(psm);
374 free(tsm);
375 free(zsm);
376 free(usm);
377 free(vsm);
378 free(lsmm);
379 free(sstm);
380 free(pblm);
381 free(ptm);
382 free(pm);
383 free(tm);
384 free(um);
385 free(vm);
386 free(wm);
387 free(h2om);
388 free(h2otm);
389 free(o3m);
390 free(hno3m);
391 free(ohm);
392 free(h2o2m);
393 free(ho2m);
394 free(o1dm);
395 free(tdewm);
396 free(ticem);
397 free(tnatm);
398 free(lwcm);
399 free(rwcm);
400 free(iwcm);
401 free(swcm);
402 free(ccm);
403 free(zm);
404 free(pvm);
405 free(ztm);
406 free(ttm);
407 free(pctm);
408 free(pcbm);
409 free(clm);
410 free(plclm);
411 free(plfcm);
412 free(pelm);
413 free(capem);
414 free(cinm);
415 free(o3cm);
416 free(rhm);
417 free(rhicem);
418 free(np);
419 free(npc);
420 free(npt);
421
422 return EXIT_SUCCESS;
423}
#define NX
Maximum number of longitudes.
Definition: met_map.c:32
#define NY
Maximum number of latitudes.
Definition: met_map.c:35
void read_clim(ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4441
double clim_zm(const clim_zm_t *zm, const double t, const double lat, const double p)
Interpolates monthly mean zonal mean climatological variables.
Definition: mptrac.c:394
double nat_temperature(const double p, const double h2o, const double hno3)
Calculates the nitric acid trihydrate (NAT) temperature.
Definition: mptrac.c:4159
double clim_oh(const ctl_t *ctl, const clim_t *clim, const double t, const double lon, const double lat, const double p)
Calculates the hydroxyl radical (OH) concentration from climatology data, with an optional diurnal co...
Definition: mptrac.c:88
void intpol_met_space_3d(met_t *met, float array[EX][EY][EP], double p, double lon, double lat, double *var, int *ci, double *cw, int init)
Interpolates meteorological variables in 3D space.
Definition: mptrac.c:1409
void 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:4789
int read_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file and populates the provided structures.
Definition: mptrac.c:5611
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:7943
#define INTPOL_SPACE_ALL(p, lon, lat)
Interpolate multiple meteorological variables in space.
Definition: mptrac.h:706
#define INTPOL_INIT
Initialize arrays for interpolation.
Definition: mptrac.h:656
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1881
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1706
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1255
#define THETA(p, t)
Compute potential temperature.
Definition: mptrac.h:1609
#define MET_HEADER
Write header for meteorological data file.
Definition: mptrac.h:868
#define TICE(p, h2o)
Calculate frost point temperature (WMO, 2018).
Definition: mptrac.h:1585
#define RHICE(p, t, h2o)
Compute relative humidity over ice.
Definition: mptrac.h:1390
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:344
#define RH(p, t, h2o)
Compute relative humidity over water.
Definition: mptrac.h:1360
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1811
#define TDEW(p, h2o)
Calculate dew point temperature.
Definition: mptrac.h:1560
Climatological data.
Definition: mptrac.h:3230
clim_zm_t ho2
HO2 zonal means.
Definition: mptrac.h:3260
clim_zm_t hno3
HNO3 zonal means.
Definition: mptrac.h:3251
clim_zm_t o1d
O(1D) zonal means.
Definition: mptrac.h:3263
clim_zm_t h2o2
H2O2 zonal means.
Definition: mptrac.h:3257
Control parameters.
Definition: mptrac.h:2135
double conv_cape
CAPE threshold for convection module [J/kg].
Definition: mptrac.h:2622
double conv_cin
CIN threshold for convection module [J/kg].
Definition: mptrac.h:2625
Meteo data structure.
Definition: mptrac.h:3289
int nx
Number of longitudes.
Definition: mptrac.h:3295
int ny
Number of latitudes.
Definition: mptrac.h:3298
int np
Number of pressure levels.
Definition: mptrac.h:3301
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3385
double lon[EX]
Longitude [deg].
Definition: mptrac.h:3307
double time
Time [s].
Definition: mptrac.h:3292
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3310
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3313
Here is the call graph for this function: