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