MPTRAC
met_sample.c
Go to the documentation of this file.
1/*
2 This file is part of MPTRAC.
3
4 MPTRAC is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 MPTRAC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with MPTRAC. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright (C) 2013-2025 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27/* ------------------------------------------------------------
28 Main...
29 ------------------------------------------------------------ */
30
31int main(
32 int argc,
33 char *argv[]) {
34
35 ctl_t ctl;
36
37 clim_t *clim;
38
39 atm_t *atm;
40
41 met_t *met0, *met1;
42
43 dd_t *dd;
44
45 FILE *out;
46
47 double h2o, h2ot, o3, lwc, rwc, iwc, swc, cc, p0, p1, ps, ts, zs, us, vs,
48 ess, nss, shf, lsm, sst, pbl, pt, pct, pcb, cl, plcl, plfc, pel,
49 cape, cin, o3c, pv, t, tt, u, v, w, z, zm, zref, zt, time_old = -999,
50 p_old = -999, lon_old = -999, lat_old = -999;
51
52 /* Check arguments... */
53 if (argc < 3)
54 ERRMSG("Give parameters: <ctl> <sample.tab> <atm_in>");
55
56 /* Allocate... */
57 ALLOC(clim, clim_t, 1);
58 ALLOC(atm, atm_t, 1);
59 ALLOC(met0, met_t, 1);
60 ALLOC(met1, met_t, 1);
61 ALLOC(dd, dd_t, 1);
62
63
64 /* Read control parameters... */
65 mptrac_read_ctl(argv[1], argc, argv, &ctl);
66 const int geopot =
67 (int) scan_ctl(argv[1], argc, argv, "SAMPLE_GEOPOT", -1, "0", NULL);
68 const int grid_time =
69 (int) scan_ctl(argv[1], argc, argv, "SAMPLE_GRID_TIME", -1, "0", NULL);
70 const int grid_z =
71 (int) scan_ctl(argv[1], argc, argv, "SAMPLE_GRID_Z", -1, "0", NULL);
72 const int grid_lon =
73 (int) scan_ctl(argv[1], argc, argv, "SAMPLE_GRID_LON", -1, "0", NULL);
74 const int grid_lat =
75 (int) scan_ctl(argv[1], argc, argv, "SAMPLE_GRID_LAT", -1, "0", NULL);
76
77 /* Read climatological data... */
78 mptrac_read_clim(&ctl, clim);
79
80 /* Read atmospheric data... */
81 if (!mptrac_read_atm(argv[3], &ctl, atm))
82 ERRMSG("Cannot open file!");
83
84 /* Create output file... */
85 LOG(1, "Write meteorological data file: %s", argv[2]);
86 if (!(out = fopen(argv[2], "w")))
87 ERRMSG("Cannot create file!");
88
89 /* Write header... */
91
92 /* Loop over air parcels... */
93 for (int ip = 0; ip < atm->np; ip++) {
94
95 /* Get meteorological data... */
96 mptrac_get_met(&ctl, clim, atm->time[ip], &met0, &met1, dd);
97
98 /* Set reference pressure for interpolation... */
100 double pref = atm->p[ip];
101 if (geopot) {
102 zref = Z(pref);
103 p0 = met0->p[0];
104 p1 = met0->p[met0->np - 1];
105 for (int it = 0; it < 24; it++) {
106 pref = 0.5 * (p0 + p1);
107 intpol_met_time_3d(met0, met0->z, met1, met1->z, atm->time[ip], pref,
108 atm->lon[ip], atm->lat[ip], &zm, ci, cw, 1);
109 if (zref > zm || !isfinite(zm))
110 p0 = pref;
111 else
112 p1 = pref;
113 }
114 pref = 0.5 * (p0 + p1);
115 }
116
117 /* Interpolate meteo data... */
118 INTPOL_TIME_ALL(atm->time[ip], pref, atm->lon[ip], atm->lat[ip]);
119
120 /* Make blank lines... */
121 if (ip == 0 || (grid_time && atm->time[ip] != time_old)
122 || (grid_z && atm->p[ip] != p_old)
123 || (grid_lon && atm->lon[ip] != lon_old)
124 || (grid_lat && atm->lat[ip] != lat_old))
125 fprintf(out, "\n");
126 time_old = atm->time[ip];
127 p_old = atm->p[ip];
128 lon_old = atm->lon[ip];
129 lat_old = atm->lat[ip];
130
131 /* Write data... */
132 fprintf(out,
133 "%.2f %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
134 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
135 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g 1 1 1\n",
136 atm->time[ip], Z(atm->p[ip]), atm->lon[ip], atm->lat[ip],
137 atm->p[ip], t, u, v, w, h2o, o3, z, pv, ps, ts, zs, us, vs,
138 ess, nss, shf, lsm,
139 sst, pt, zt, tt, h2ot, lwc, rwc, iwc, swc, cc, cl, pct, pcb, plcl,
140 plfc, pel, cape, cin, RH(atm->p[ip], t, h2o), RHICE(atm->p[ip], t,
141 h2o),
142 TDEW(atm->p[ip], h2o), TICE(atm->p[ip], h2o),
143 nat_temperature(atm->p[ip], h2o,
144 clim_zm(&clim->hno3, atm->time[ip], atm->lat[ip],
145 atm->p[ip])), clim_zm(&clim->hno3,
146 atm->time[ip],
147 atm->lat[ip],
148 atm->p[ip]),
149 clim_oh(&ctl, clim, atm->time[ip], atm->lon[ip], atm->lat[ip],
150 atm->p[ip]), clim_zm(&clim->h2o2, atm->time[ip],
151 atm->lat[ip], atm->p[ip]),
152 clim_zm(&clim->ho2, atm->time[ip], atm->lat[ip], atm->p[ip]),
153 clim_zm(&clim->o1d, atm->time[ip], atm->lat[ip], atm->p[ip]), pbl,
154 o3c);
155 }
156
157 /* Close file... */
158 fclose(out);
159
160 /* Free... */
161 free(clim);
162 free(atm);
163 free(met0);
164 free(met1);
165
166 return EXIT_SUCCESS;
167}
int main(int argc, char *argv[])
Definition: met_sample.c:31
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
void intpol_met_time_3d(const met_t *met0, float array0[EX][EY][EP], const met_t *met1, float array1[EX][EY][EP], const double ts, const double p, const double lon, const double lat, double *var, int *ci, double *cw, const int init)
Interpolates meteorological data in 3D space and time.
Definition: mptrac.c:2382
double nat_temperature(const double p, const double h2o, const double hno3)
Calculates the nitric acid trihydrate (NAT) temperature.
Definition: mptrac.c:6921
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_get_met(ctl_t *ctl, clim_t *clim, const double t, met_t **met0, met_t **met1, dd_t *dd)
Retrieves meteorological data for the specified time.
Definition: mptrac.c:5194
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_atm(const char *filename, const ctl_t *ctl, atm_t *atm)
Reads air parcel data from a specified file into the given atmospheric structure.
Definition: mptrac.c:5335
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
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
MPTRAC library declarations.
#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 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 INTPOL_TIME_ALL(time, p, lon, lat)
Interpolate multiple meteorological variables in time.
Definition: mptrac.h:937
#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
Air parcel data.
Definition: mptrac.h:3292
double time[NP]
Time [s].
Definition: mptrac.h:3298
double lat[NP]
Latitude [deg].
Definition: mptrac.h:3307
double lon[NP]
Longitude [deg].
Definition: mptrac.h:3304
int np
Number of air parcels.
Definition: mptrac.h:3295
double p[NP]
Pressure [hPa].
Definition: mptrac.h:3301
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
Domain decomposition data structure.
Definition: mptrac.h:3720
Meteo data structure.
Definition: mptrac.h:3546
int np
Number of pressure levels.
Definition: mptrac.h:3558
float z[EX][EY][EP]
Geopotential height [km].
Definition: mptrac.h:3657
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3570