51 {
52
54
56
58
60
61 FILE *out;
62
73 zt, tt, plev[
NZ], ps, ts, zs, us, vs, ess, nss, shf, lsm, sst, pbl,
74 pt, pct, pcb, plcl, plfc, pel, cape, cin, o3c, cl, t, u, v, w, pv,
75 h2o, h2ot, o3, lwc, rwc, iwc, swc, cc, lat, lats[
NY], lonm[
NZ][
NY], cw[3];
76
77 static int np[
NZ][
NY], npc[
NZ][
NY], npt[
NZ][
NY], ny, nz, ci[3];
78
79
83
84
86
87
88 if (argc < 4)
89 ERRMSG(
"Missing or invalid command-line arguments.\n\n"
90 "Usage: met_zm <ctl> <zm.tab> <met0> [<met1> ...]\n\n"
91 "Use -h for full help.");
92
93
95 double z0 =
scan_ctl(argv[1], argc, argv,
"ZM_Z0", -1,
"-999", NULL);
96 double z1 =
scan_ctl(argv[1], argc, argv,
"ZM_Z1", -1,
"-999", NULL);
97 double dz =
scan_ctl(argv[1], argc, argv,
"ZM_DZ", -1,
"-999", NULL);
98 double lon0 =
scan_ctl(argv[1], argc, argv,
"ZM_LON0", -1,
"-360", NULL);
99 double lon1 =
scan_ctl(argv[1], argc, argv,
"ZM_LON1", -1,
"360", NULL);
100 double lat0 =
scan_ctl(argv[1], argc, argv,
"ZM_LAT0", -1,
"-90", NULL);
101 double lat1 =
scan_ctl(argv[1], argc, argv,
"ZM_LAT1", -1,
"90", NULL);
102 double dlat =
scan_ctl(argv[1], argc, argv,
"ZM_DLAT", -1,
"-999", NULL);
103
104
106
107
108 for (int i = 3; i < argc; i++) {
109
110
112 continue;
113
114
115 if (z0 < 0)
117 if (z1 < 0)
118 z1 =
Z(met->
p[met->
np - 1]);
119 nz = 0;
120 if (dz < 0) {
121 for (
int iz = 0; iz < met->
np; iz++)
122 if (
Z(met->
p[iz]) >= z0 &&
Z(met->
p[iz]) <= z1) {
123 plev[nz] = met->
p[iz];
125 ERRMSG(
"Too many pressure levels!");
126 }
127 } else
128 for (z = z0; z <= z1; z += dz) {
131 ERRMSG(
"Too many pressure levels!");
132 }
133
134
135 if (dlat <= 0)
136 dlat = fabs(met->
lat[1] - met->
lat[0]);
137 ny = 0;
138 if (lat0 < -90 && lat1 > 90) {
139 lat0 = gsl_stats_min(met->
lat, 1, (
size_t) met->
ny);
140 lat1 = gsl_stats_max(met->
lat, 1, (
size_t) met->
ny);
141 }
142 for (lat = lat0; lat <= lat1 + 0.001; lat += dlat) {
143 lats[ny] = round(lat * 1e3) / 1e3;
145 ERRMSG(
"Too many latitudes!");
146 }
147
148
149 for (
int ix = 0; ix < met->
nx; ix++)
150 if (met->
lon[ix] >= lon0 && met->
lon[ix] <= lon1)
151 for (int iy = 0; iy < ny; iy++)
152 for (int iz = 0; iz < nz; iz++) {
153
154
156
157
158 timem[iz][iy] += met->
time;
159 lonm[iz][iy] += met->
lon[ix];
160 zm[iz][iy] += z;
161 tm[iz][iy] += t;
162 um[iz][iy] += u;
163 vm[iz][iy] += v;
164 wm[iz][iy] += w;
165 pvm[iz][iy] += pv;
166 h2om[iz][iy] += h2o;
167 o3m[iz][iy] += o3;
168 lwcm[iz][iy] += lwc;
169 rwcm[iz][iy] += rwc;
170 iwcm[iz][iy] += iwc;
171 swcm[iz][iy] += swc;
172 ccm[iz][iy] += cc;
173 psm[iz][iy] += ps;
174 tsm[iz][iy] += ts;
175 zsm[iz][iy] += zs;
176 usm[iz][iy] += us;
177 vsm[iz][iy] += vs;
178 essm[iz][iy] += ess;
179 nssm[iz][iy] += nss;
180 shfm[iz][iy] += shf;
181 lsmm[iz][iy] += lsm;
182 sstm[iz][iy] += sst;
183 pblm[iz][iy] += pbl;
184 pctm[iz][iy] += pct;
185 pcbm[iz][iy] += pcb;
186 clm[iz][iy] += cl;
187 if (isfinite(plfc) && isfinite(pel) && cape >= ctl.
conv_cape
189 plclm[iz][iy] += plcl;
190 plfcm[iz][iy] += plfc;
191 pelm[iz][iy] += pel;
192 capem[iz][iy] += cape;
193 cinm[iz][iy] += cin;
194 npc[iz][iy]++;
195 }
196 if (isfinite(pt)) {
197 ptm[iz][iy] += pt;
198 ztm[iz][iy] += zt;
199 ttm[iz][iy] += tt;
200 h2otm[iz][iy] += h2ot;
201 npt[iz][iy]++;
202 }
203 o3cm[iz][iy] += o3c;
204 rhm[iz][iy] +=
RH(plev[iz], t, h2o);
205 rhicem[iz][iy] +=
RHICE(plev[iz], t, h2o);
206 tdewm[iz][iy] +=
TDEW(plev[iz], h2o);
207 ticem[iz][iy] +=
TICE(plev[iz], h2o);
208 hno3m[iz][iy] +=
210 tnatm[iz][iy] +=
213 plev[iz]));
214 ohm[iz][iy] +=
216 plev[iz]);
217 h2o2m[iz][iy]
219 ho2m[iz][iy]
221 o1dm[iz][iy]
223 np[iz][iy]++;
224 }
225 }
226
227
228 LOG(1,
"Write meteorological data file: %s", argv[2]);
229 if (!(out = fopen(argv[2], "w")))
230 ERRMSG(
"Cannot create file!");
231
232
234
235
236 for (int iz = 0; iz < nz; iz++) {
237 fprintf(out, "\n");
238 for (int iy = 0; iy < ny; iy++)
239 fprintf(out,
240 "%.2f %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
241 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
242 " %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %d\n",
243 timem[iz][iy] / np[iz][iy],
Z(plev[iz]),
244 lonm[iz][iy] / np[iz][iy], lats[iy],
245 plev[iz], tm[iz][iy] / np[iz][iy], um[iz][iy] / np[iz][iy],
246 vm[iz][iy] / np[iz][iy], wm[iz][iy] / np[iz][iy],
247 h2om[iz][iy] / np[iz][iy], o3m[iz][iy] / np[iz][iy],
248 zm[iz][iy] / np[iz][iy], pvm[iz][iy] / np[iz][iy],
249 psm[iz][iy] / np[iz][iy], tsm[iz][iy] / np[iz][iy],
250 zsm[iz][iy] / np[iz][iy], usm[iz][iy] / np[iz][iy],
251 vsm[iz][iy] / np[iz][iy], essm[iz][iy] / np[iz][iy],
252 nssm[iz][iy] / np[iz][iy], shfm[iz][iy] / np[iz][iy],
253 lsmm[iz][iy] / np[iz][iy],
254 sstm[iz][iy] / np[iz][iy], ptm[iz][iy] / npt[iz][iy],
255 ztm[iz][iy] / npt[iz][iy], ttm[iz][iy] / npt[iz][iy],
256 h2otm[iz][iy] / npt[iz][iy], lwcm[iz][iy] / np[iz][iy],
257 rwcm[iz][iy] / np[iz][iy], iwcm[iz][iy] / np[iz][iy],
258 swcm[iz][iy] / np[iz][iy], ccm[iz][iy] / np[iz][iy],
259 clm[iz][iy] / np[iz][iy], pctm[iz][iy] / np[iz][iy],
260 pcbm[iz][iy] / np[iz][iy], plclm[iz][iy] / npc[iz][iy],
261 plfcm[iz][iy] / npc[iz][iy], pelm[iz][iy] / npc[iz][iy],
262 capem[iz][iy] / npc[iz][iy], cinm[iz][iy] / npc[iz][iy],
263 rhm[iz][iy] / np[iz][iy], rhicem[iz][iy] / np[iz][iy],
264 tdewm[iz][iy] / np[iz][iy], ticem[iz][iy] / np[iz][iy],
265 tnatm[iz][iy] / np[iz][iy], hno3m[iz][iy] / np[iz][iy],
266 ohm[iz][iy] / np[iz][iy], h2o2m[iz][iy] / np[iz][iy],
267 ho2m[iz][iy] / np[iz][iy], o1dm[iz][iy] / np[iz][iy],
268 pblm[iz][iy] / np[iz][iy], o3cm[iz][iy] / np[iz][iy],
269 np[iz][iy], npt[iz][iy], npc[iz][iy]);
270 }
271
272
273 fclose(out);
274
275
276 free(clim);
277 free(met);
278 free(dd);
279
280 return EXIT_SUCCESS;
281}
#define NY
Maximum number of latitudes.
#define NZ
Maximum number of altitudes.
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.
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.
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
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...
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.
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.
#define INTPOL_SPACE_ALL(p, lon, lat)
Interpolate multiple meteorological variables in space.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define USAGE
Print usage information on -h or --help.
#define Z(p)
Convert pressure to altitude.
#define P(z)
Compute pressure at given altitude.
#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].
Domain decomposition data structure.
int nx
Number of longitudes.
int ny
Number of latitudes.
int np
Number of pressure levels.
double lon[EX]
Longitudes [deg].
double lat[EY]
Latitudes [deg].
double p[EP]
Pressure levels [hPa].