43 {
44
46
48
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,
61
62 static int *np, *npc, *npt, nx, ny;
63
64
157 ALLOC(rhicem,
double,
165
166
167
168 printf(
"met size %lu \n",
sizeof(
met_t));
169 printf(
"atm size %lu \n",
sizeof(
atm_t));
170 printf(
"ctl size %lu \n",
sizeof(
ctl_t));
171 printf(
"clim size %lu \n",
sizeof(
clim_t));
172
173
174 if (argc < 4)
175 ERRMSG(
"Give parameters: <ctl> <map.tab> <met0> [ <met1> ... ]");
176
177
178 read_ctl(argv[1], argc, argv, &ctl);
179 double p0 =
P(
scan_ctl(argv[1], argc, argv,
"MAP_Z0", -1,
"10", NULL));
180 double lon0 =
scan_ctl(argv[1], argc, argv,
"MAP_LON0", -1,
"-180", NULL);
181 double lon1 =
scan_ctl(argv[1], argc, argv,
"MAP_LON1", -1,
"180", NULL);
182 double dlon =
scan_ctl(argv[1], argc, argv,
"MAP_DLON", -1,
"-999", NULL);
183 double lat0 =
scan_ctl(argv[1], argc, argv,
"MAP_LAT0", -1,
"-90", NULL);
184 double lat1 =
scan_ctl(argv[1], argc, argv,
"MAP_LAT1", -1,
"90", NULL);
185 double dlat =
scan_ctl(argv[1], argc, argv,
"MAP_DLAT", -1,
"-999", NULL);
186 double theta =
scan_ctl(argv[1], argc, argv,
"MAP_THETA", -1,
"-999", NULL);
187
188
190
191
192 for (int i = 3; i < argc; i++) {
193
194
195 if (!
read_met(argv[i], &ctl, clim, met))
196 continue;
197
198
199 if (dlon <= 0)
200 dlon = fabs(met->
lon[1] - met->
lon[0]);
201 if (dlat <= 0)
202 dlat = fabs(met->
lat[1] - met->
lat[0]);
203 if (lon0 < -360 && lon1 > 360) {
204 lon0 = gsl_stats_min(met->
lon, 1, (
size_t) met->
nx);
205 lon1 = gsl_stats_max(met->
lon, 1, (
size_t) met->
nx);
206 }
207 nx = ny = 0;
208 for (lon = lon0; lon <= lon1; lon += dlon) {
209 lons[nx] = lon;
211 ERRMSG(
"Too many longitudes!");
212 }
213 if (lat0 < -90 && lat1 > 90) {
214 lat0 = gsl_stats_min(met->
lat, 1, (
size_t) met->
ny);
215 lat1 = gsl_stats_max(met->
lat, 1, (
size_t) met->
ny);
216 }
217 for (lat = lat0; lat <= lat1; lat += dlat) {
218 lats[ny] = lat;
220 ERRMSG(
"Too many latitudes!");
221 }
222
223
224 for (int ix = 0; ix < nx; ix++)
225 for (int iy = 0; iy < ny; iy++) {
226
227
229 if (theta > 0) {
230 ptop = met->
p[met->
np - 1];
232 do {
233 p0 = 0.5 * (ptop + pbot);
235 &t0, ci, cw, 1);
236 if (
THETA(p0, t0) > theta)
237 ptop = p0;
238 else
239 pbot = p0;
240 } while (fabs(ptop - pbot) > 1e-5);
241 }
242
243
245
246
247 timem[iy * nx + ix] += met->
time;
248 zm[iy * nx + ix] += z;
249 pm[iy * nx + ix] += p0;
250 tm[iy * nx + ix] += t;
251 um[iy * nx + ix] += u;
252 vm[iy * nx + ix] += v;
253 wm[iy * nx + ix] += w;
254 pvm[iy * nx + ix] += pv;
255 h2om[iy * nx + ix] += h2o;
256 o3m[iy * nx + ix] += o3;
257 lwcm[iy * nx + ix] += lwc;
258 rwcm[iy * nx + ix] += rwc;
259 iwcm[iy * nx + ix] += iwc;
260 swcm[iy * nx + ix] += swc;
261 ccm[iy * nx + ix] += cc;
262 psm[iy * nx + ix] += ps;
263 tsm[iy * nx + ix] += ts;
264 zsm[iy * nx + ix] += zs;
265 usm[iy * nx + ix] += us;
266 vsm[iy * nx + ix] += vs;
267 lsmm[iy * nx + ix] += lsm;
268 sstm[iy * nx + ix] += sst;
269 pblm[iy * nx + ix] += pbl;
270 pctm[iy * nx + ix] += pct;
271 pcbm[iy * nx + ix] += pcb;
272 clm[iy * nx + ix] += cl;
273 if (isfinite(plfc) && isfinite(pel) && cape >= ctl.
conv_cape
275 plclm[iy * nx + ix] += plcl;
276 plfcm[iy * nx + ix] += plfc;
277 pelm[iy * nx + ix] += pel;
278 capem[iy * nx + ix] += cape;
279 cinm[iy * nx + ix] += cin;
280 npc[iy * nx + ix]++;
281 }
282 if (isfinite(pt)) {
283 ptm[iy * nx + ix] += pt;
284 ztm[iy * nx + ix] += zt;
285 ttm[iy * nx + ix] += tt;
286 h2otm[iy * nx + ix] += h2ot;
287 npt[iy * nx + ix]++;
288 }
289 o3cm[iy * nx + ix] += o3c;
291 tnatm[iy * nx + ix] +=
294 ohm[iy * nx + ix] +=
295 clim_oh(&ctl, clim, met->
time, lons[ix], lats[iy], p0);
297 ho2m[iy * nx + ix] +=
clim_zm(&clim->
ho2, met->
time, lats[iy], p0);
298 o1dm[iy * nx + ix] +=
clim_zm(&clim->
o1d, met->
time, lats[iy], p0);
299 rhm[iy * nx + ix] +=
RH(p0, t, h2o);
300 rhicem[iy * nx + ix] +=
RHICE(p0, t, h2o);
301 tdewm[iy * nx + ix] +=
TDEW(p0, h2o);
302 ticem[iy * nx + ix] +=
TICE(p0, h2o);
303 np[iy * nx + ix]++;
304 }
305 }
306
307
308 LOG(1,
"Write meteorological data file: %s", argv[2]);
309 if (!(out = fopen(argv[2], "w")))
310 ERRMSG(
"Cannot create file!");
311
312
314
315
316 for (int iy = 0; iy < ny; iy++) {
317 fprintf(out, "\n");
318 for (int ix = 0; ix < nx; ix++)
319 fprintf(out,
320 "%.2f %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
321 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
322 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %d\n",
323 timem[iy * nx + ix] / np[iy * nx + ix],
324 Z(pm[iy * nx + ix] / np[iy * nx + ix]), lons[ix], lats[iy],
325 pm[iy * nx + ix] / np[iy * nx + ix],
326 tm[iy * nx + ix] / np[iy * nx + ix],
327 um[iy * nx + ix] / np[iy * nx + ix],
328 vm[iy * nx + ix] / np[iy * nx + ix],
329 wm[iy * nx + ix] / np[iy * nx + ix],
330 h2om[iy * nx + ix] / np[iy * nx + ix],
331 o3m[iy * nx + ix] / np[iy * nx + ix],
332 zm[iy * nx + ix] / np[iy * nx + ix],
333 pvm[iy * nx + ix] / np[iy * nx + ix],
334 psm[iy * nx + ix] / np[iy * nx + ix],
335 tsm[iy * nx + ix] / np[iy * nx + ix],
336 zsm[iy * nx + ix] / np[iy * nx + ix],
337 usm[iy * nx + ix] / np[iy * nx + ix],
338 vsm[iy * nx + ix] / np[iy * nx + ix],
339 lsmm[iy * nx + ix] / np[iy * nx + ix],
340 sstm[iy * nx + ix] / np[iy * nx + ix],
341 ptm[iy * nx + ix] / npt[iy * nx + ix],
342 ztm[iy * nx + ix] / npt[iy * nx + ix],
343 ttm[iy * nx + ix] / npt[iy * nx + ix],
344 h2otm[iy * nx + ix] / npt[iy * nx + ix],
345 lwcm[iy * nx + ix] / np[iy * nx + ix],
346 rwcm[iy * nx + ix] / np[iy * nx + ix],
347 iwcm[iy * nx + ix] / np[iy * nx + ix],
348 swcm[iy * nx + ix] / np[iy * nx + ix],
349 ccm[iy * nx + ix] / np[iy * nx + ix],
350 clm[iy * nx + ix] / np[iy * nx + ix],
351 pctm[iy * nx + ix] / np[iy * nx + ix],
352 pcbm[iy * nx + ix] / np[iy * nx + ix],
353 plclm[iy * nx + ix] / npc[iy * nx + ix],
354 plfcm[iy * nx + ix] / npc[iy * nx + ix],
355 pelm[iy * nx + ix] / npc[iy * nx + ix],
356 capem[iy * nx + ix] / npc[iy * nx + ix],
357 cinm[iy * nx + ix] / npc[iy * nx + ix],
358 rhm[iy * nx + ix] / np[iy * nx + ix],
359 rhicem[iy * nx + ix] / np[iy * nx + ix],
360 tdewm[iy * nx + ix] / np[iy * nx + ix],
361 ticem[iy * nx + ix] / np[iy * nx + ix],
362 tnatm[iy * nx + ix] / np[iy * nx + ix],
363 hno3m[iy * nx + ix] / np[iy * nx + ix],
364 ohm[iy * nx + ix] / np[iy * nx + ix],
365 h2o2m[iy * nx + ix] / np[iy * nx + ix],
366 ho2m[iy * nx + ix] / np[iy * nx + ix],
367 o1dm[iy * nx + ix] / np[iy * nx + ix],
368 pblm[iy * nx + ix] / np[iy * nx + ix],
369 o3cm[iy * nx + ix] / np[iy * nx + ix], np[iy * nx + ix],
370 npt[iy * nx + ix], npc[iy * nx + ix]);
371 }
372
373
374 fclose(out);
375
376
377 free(clim);
378 free(met);
379 free(timem);
380 free(psm);
381 free(tsm);
382 free(zsm);
383 free(usm);
384 free(vsm);
385 free(lsmm);
386 free(sstm);
387 free(pblm);
388 free(ptm);
389 free(pm);
390 free(tm);
391 free(um);
392 free(vm);
393 free(wm);
394 free(h2om);
395 free(h2otm);
396 free(o3m);
397 free(hno3m);
398 free(ohm);
399 free(h2o2m);
400 free(ho2m);
401 free(o1dm);
402 free(tdewm);
403 free(ticem);
404 free(tnatm);
405 free(lwcm);
406 free(rwcm);
407 free(iwcm);
408 free(swcm);
409 free(ccm);
410 free(zm);
411 free(pvm);
412 free(ztm);
413 free(ttm);
414 free(pctm);
415 free(pcbm);
416 free(clm);
417 free(plclm);
418 free(plfcm);
419 free(pelm);
420 free(capem);
421 free(cinm);
422 free(o3cm);
423 free(rhm);
424 free(rhicem);
425 free(np);
426 free(npc);
427 free(npt);
428
429 return EXIT_SUCCESS;
430}
#define NX
Maximum number of longitudes.
#define NY
Maximum number of latitudes.
double clim_zm(const clim_zm_t *zm, const double t, const double lat, const double p)
Interpolates monthly mean zonal mean climatological variables.
double nat_temperature(const double p, const double h2o, const double hno3)
Calculates the nitric acid trihydrate (NAT) temperature.
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.
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.
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...
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.
int read_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
#define INTPOL_SPACE_ALL(p, lon, lat)
Interpolate multiple meteorological variables in space.
#define INTPOL_INIT
Initialize arrays for interpolation.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define Z(p)
Convert pressure to altitude.
#define P(z)
Compute pressure at given altitude.
#define THETA(p, t)
Compute potential temperature.
#define MET_HEADER
Write header for meteorological data file.
#define TICE(p, h2o)
Calculate frost point temperature (WMO, 2018).
#define RHICE(p, t, h2o)
Compute relative humidity over ice.
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
#define RH(p, t, h2o)
Compute relative humidity over water.
#define LOG(level,...)
Print a log message with a specified logging level.
#define TDEW(p, h2o)
Calculate dew point temperature.
clim_zm_t ho2
HO2 zonal means.
clim_zm_t hno3
HNO3 zonal means.
clim_zm_t o1d
O(1D) zonal means.
clim_zm_t h2o2
H2O2 zonal means.
double conv_cape
CAPE threshold for convection module [J/kg].
double conv_cin
CIN threshold for convection module [J/kg].
int nx
Number of longitudes.
int ny
Number of latitudes.
int np
Number of pressure levels.
float t[EX][EY][EP]
Temperature [K].
double lon[EX]
Longitude [deg].
double lat[EY]
Latitude [deg].
double p[EP]
Pressure levels [hPa].