MPTRAC
Macros | Functions
met_zm.c File Reference

Extract zonal mean from meteorological data. More...

#include "mptrac.h"

Go to the source code of this file.

Macros

#define NZ   1000
 Maximum number of altitudes. More...
 
#define NY   EY
 Maximum number of latitudes. More...
 

Functions

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

Detailed Description

Extract zonal mean from meteorological data.

Definition in file met_zm.c.

Macro Definition Documentation

◆ NZ

#define NZ   1000

Maximum number of altitudes.

Definition at line 32 of file met_zm.c.

◆ NY

#define NY   EY

Maximum number of latitudes.

Definition at line 35 of file met_zm.c.

Function Documentation

◆ main()

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

Definition at line 41 of file met_zm.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[NZ][NY], psm[NZ][NY], tsm[NZ][NY], zsm[NZ][NY],
54 usm[NZ][NY], vsm[NZ][NY], essm[NZ][NY], nssm[NZ][NY], shfm[NZ][NY],
55 lsmm[NZ][NY], sstm[NZ][NY], pblm[NZ][NY],
56 ptm[NZ][NY], pctm[NZ][NY], pcbm[NZ][NY], clm[NZ][NY], plclm[NZ][NY],
57 plfcm[NZ][NY], pelm[NZ][NY], capem[NZ][NY], cinm[NZ][NY], o3cm[NZ][NY],
58 ttm[NZ][NY], ztm[NZ][NY], tm[NZ][NY], um[NZ][NY], vm[NZ][NY], wm[NZ][NY],
59 h2om[NZ][NY], h2otm[NZ][NY], pvm[NZ][NY], o3m[NZ][NY], lwcm[NZ][NY],
60 rwcm[NZ][NY], iwcm[NZ][NY], swcm[NZ][NY], ccm[NZ][NY], zm[NZ][NY],
61 rhm[NZ][NY], rhicem[NZ][NY], tdewm[NZ][NY], ticem[NZ][NY], tnatm[NZ][NY],
62 hno3m[NZ][NY], ohm[NZ][NY], h2o2m[NZ][NY], ho2m[NZ][NY], o1dm[NZ][NY], z,
63 zt, tt, plev[NZ], ps, ts, zs, us, vs, ess, nss, shf, lsm, sst, pbl,
64 pt, pct, pcb, plcl, plfc, pel, cape, cin, o3c, cl, t, u, v, w, pv,
65 h2o, h2ot, o3, lwc, rwc, iwc, swc, cc, lat, lats[NY], lonm[NZ][NY], cw[3];
66
67 static int np[NZ][NY], npc[NZ][NY], npt[NZ][NY], ny, nz, ci[3];
68
69 /* Allocate... */
70 ALLOC(clim, clim_t, 1);
71 ALLOC(met, met_t, 1);
72
73 /* Check arguments... */
74 if (argc < 4)
75 ERRMSG("Give parameters: <ctl> <zm.tab> <met0> [ <met1> ... ]");
76
77 /* Read control parameters... */
78 read_ctl(argv[1], argc, argv, &ctl);
79 double z0 = scan_ctl(argv[1], argc, argv, "ZM_Z0", -1, "-999", NULL);
80 double z1 = scan_ctl(argv[1], argc, argv, "ZM_Z1", -1, "-999", NULL);
81 double dz = scan_ctl(argv[1], argc, argv, "ZM_DZ", -1, "-999", NULL);
82 double lon0 = scan_ctl(argv[1], argc, argv, "ZM_LON0", -1, "-360", NULL);
83 double lon1 = scan_ctl(argv[1], argc, argv, "ZM_LON1", -1, "360", NULL);
84 double lat0 = scan_ctl(argv[1], argc, argv, "ZM_LAT0", -1, "-90", NULL);
85 double lat1 = scan_ctl(argv[1], argc, argv, "ZM_LAT1", -1, "90", NULL);
86 double dlat = scan_ctl(argv[1], argc, argv, "ZM_DLAT", -1, "-999", NULL);
87
88 /* Read climatological data... */
89 read_clim(&ctl, clim);
90
91 /* Loop over files... */
92 for (int i = 3; i < argc; i++) {
93
94 /* Read meteorological data... */
95 if (!read_met(argv[i], &ctl, clim, met))
96 continue;
97
98 /* Set vertical grid... */
99 if (z0 < 0)
100 z0 = Z(met->p[0]);
101 if (z1 < 0)
102 z1 = Z(met->p[met->np - 1]);
103 nz = 0;
104 if (dz < 0) {
105 for (int iz = 0; iz < met->np; iz++)
106 if (Z(met->p[iz]) >= z0 && Z(met->p[iz]) <= z1) {
107 plev[nz] = met->p[iz];
108 if ((++nz) >= NZ)
109 ERRMSG("Too many pressure levels!");
110 }
111 } else
112 for (z = z0; z <= z1; z += dz) {
113 plev[nz] = P(z);
114 if ((++nz) >= NZ)
115 ERRMSG("Too many pressure levels!");
116 }
117
118 /* Set horizontal grid... */
119 if (dlat <= 0)
120 dlat = fabs(met->lat[1] - met->lat[0]);
121 ny = 0;
122 if (lat0 < -90 && lat1 > 90) {
123 lat0 = gsl_stats_min(met->lat, 1, (size_t) met->ny);
124 lat1 = gsl_stats_max(met->lat, 1, (size_t) met->ny);
125 }
126 for (lat = lat0; lat <= lat1; lat += dlat) {
127 lats[ny] = lat;
128 if ((++ny) >= NY)
129 ERRMSG("Too many latitudes!");
130 }
131
132 /* Average... */
133 for (int ix = 0; ix < met->nx; ix++)
134 if (met->lon[ix] >= lon0 && met->lon[ix] <= lon1)
135 for (int iy = 0; iy < ny; iy++)
136 for (int iz = 0; iz < nz; iz++) {
137
138 /* Interpolate meteo data... */
139 INTPOL_SPACE_ALL(plev[iz], met->lon[ix], lats[iy]);
140
141 /* Averaging... */
142 timem[iz][iy] += met->time;
143 lonm[iz][iy] += met->lon[ix];
144 zm[iz][iy] += z;
145 tm[iz][iy] += t;
146 um[iz][iy] += u;
147 vm[iz][iy] += v;
148 wm[iz][iy] += w;
149 pvm[iz][iy] += pv;
150 h2om[iz][iy] += h2o;
151 o3m[iz][iy] += o3;
152 lwcm[iz][iy] += lwc;
153 rwcm[iz][iy] += rwc;
154 iwcm[iz][iy] += iwc;
155 swcm[iz][iy] += swc;
156 ccm[iz][iy] += cc;
157 psm[iz][iy] += ps;
158 tsm[iz][iy] += ts;
159 zsm[iz][iy] += zs;
160 usm[iz][iy] += us;
161 vsm[iz][iy] += vs;
162 essm[iz][iy] += ess;
163 nssm[iz][iy] += nss;
164 shfm[iz][iy] += shf;
165 lsmm[iz][iy] += lsm;
166 sstm[iz][iy] += sst;
167 pblm[iz][iy] += pbl;
168 pctm[iz][iy] += pct;
169 pcbm[iz][iy] += pcb;
170 clm[iz][iy] += cl;
171 if (isfinite(plfc) && isfinite(pel) && cape >= ctl.conv_cape
172 && (ctl.conv_cin <= 0 || cin < ctl.conv_cin)) {
173 plclm[iz][iy] += plcl;
174 plfcm[iz][iy] += plfc;
175 pelm[iz][iy] += pel;
176 capem[iz][iy] += cape;
177 cinm[iz][iy] += cin;
178 npc[iz][iy]++;
179 }
180 if (isfinite(pt)) {
181 ptm[iz][iy] += pt;
182 ztm[iz][iy] += zt;
183 ttm[iz][iy] += tt;
184 h2otm[iz][iy] += h2ot;
185 npt[iz][iy]++;
186 }
187 o3cm[iz][iy] += o3c;
188 rhm[iz][iy] += RH(plev[iz], t, h2o);
189 rhicem[iz][iy] += RHICE(plev[iz], t, h2o);
190 tdewm[iz][iy] += TDEW(plev[iz], h2o);
191 ticem[iz][iy] += TICE(plev[iz], h2o);
192 hno3m[iz][iy] +=
193 clim_zm(&clim->hno3, met->time, lats[iy], plev[iz]);
194 tnatm[iz][iy] +=
195 nat_temperature(plev[iz], h2o,
196 clim_zm(&clim->hno3, met->time, lats[iy],
197 plev[iz]));
198 ohm[iz][iy] +=
199 clim_oh(&ctl, clim, met->time, met->lon[ix], lats[iy],
200 plev[iz]);
201 h2o2m[iz][iy]
202 += clim_zm(&clim->h2o2, met->time, lats[iy], plev[iz]);
203 ho2m[iz][iy]
204 += clim_zm(&clim->ho2, met->time, lats[iy], plev[iz]);
205 o1dm[iz][iy]
206 += clim_zm(&clim->o1d, met->time, lats[iy], plev[iz]);
207 np[iz][iy]++;
208 }
209 }
210
211 /* Create output file... */
212 LOG(1, "Write meteorological data file: %s", argv[2]);
213 if (!(out = fopen(argv[2], "w")))
214 ERRMSG("Cannot create file!");
215
216 /* Write header... */
218
219 /* Write data... */
220 for (int iz = 0; iz < nz; iz++) {
221 fprintf(out, "\n");
222 for (int iy = 0; iy < ny; iy++)
223 fprintf(out,
224 "%.2f %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
225 " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g"
226 " %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %d\n",
227 timem[iz][iy] / np[iz][iy], Z(plev[iz]),
228 lonm[iz][iy] / np[iz][iy], lats[iy],
229 plev[iz], tm[iz][iy] / np[iz][iy], um[iz][iy] / np[iz][iy],
230 vm[iz][iy] / np[iz][iy], wm[iz][iy] / np[iz][iy],
231 h2om[iz][iy] / np[iz][iy], o3m[iz][iy] / np[iz][iy],
232 zm[iz][iy] / np[iz][iy], pvm[iz][iy] / np[iz][iy],
233 psm[iz][iy] / np[iz][iy], tsm[iz][iy] / np[iz][iy],
234 zsm[iz][iy] / np[iz][iy], usm[iz][iy] / np[iz][iy],
235 vsm[iz][iy] / np[iz][iy], essm[iz][iy] / np[iz][iy],
236 nssm[iz][iy] / np[iz][iy], shfm[iz][iy] / np[iz][iy],
237 lsmm[iz][iy] / np[iz][iy],
238 sstm[iz][iy] / np[iz][iy], ptm[iz][iy] / npt[iz][iy],
239 ztm[iz][iy] / npt[iz][iy], ttm[iz][iy] / npt[iz][iy],
240 h2otm[iz][iy] / npt[iz][iy], lwcm[iz][iy] / np[iz][iy],
241 rwcm[iz][iy] / np[iz][iy], iwcm[iz][iy] / np[iz][iy],
242 swcm[iz][iy] / np[iz][iy], ccm[iz][iy] / np[iz][iy],
243 clm[iz][iy] / np[iz][iy], pctm[iz][iy] / np[iz][iy],
244 pcbm[iz][iy] / np[iz][iy], plclm[iz][iy] / npc[iz][iy],
245 plfcm[iz][iy] / npc[iz][iy], pelm[iz][iy] / npc[iz][iy],
246 capem[iz][iy] / npc[iz][iy], cinm[iz][iy] / npc[iz][iy],
247 rhm[iz][iy] / np[iz][iy], rhicem[iz][iy] / np[iz][iy],
248 tdewm[iz][iy] / np[iz][iy], ticem[iz][iy] / np[iz][iy],
249 tnatm[iz][iy] / np[iz][iy], hno3m[iz][iy] / np[iz][iy],
250 ohm[iz][iy] / np[iz][iy], h2o2m[iz][iy] / np[iz][iy],
251 ho2m[iz][iy] / np[iz][iy], o1dm[iz][iy] / np[iz][iy],
252 pblm[iz][iy] / np[iz][iy], o3cm[iz][iy] / np[iz][iy],
253 np[iz][iy], npt[iz][iy], npc[iz][iy]);
254 }
255
256 /* Close file... */
257 fclose(out);
258
259 /* Free... */
260 free(clim);
261 free(met);
262
263 return EXIT_SUCCESS;
264}
#define NY
Maximum number of latitudes.
Definition: met_zm.c:35
#define NZ
Maximum number of altitudes.
Definition: met_zm.c:32
int read_met(const char *filename, const ctl_t *ctl, const clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:6017
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:401
double nat_temperature(const double p, const double h2o, const double hno3)
Calculates the nitric acid trihydrate (NAT) temperature.
Definition: mptrac.c:4520
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:8503
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 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:5156
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4824
#define INTPOL_SPACE_ALL(p, lon, lat)
Interpolate multiple meteorological variables in space.
Definition: mptrac.h:732
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1916
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1741
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1304
#define MET_HEADER
Write header for meteorological data file.
Definition: mptrac.h:900
#define TICE(p, h2o)
Calculate frost point temperature (WMO, 2018).
Definition: mptrac.h:1620
#define RHICE(p, t, h2o)
Compute relative humidity over ice.
Definition: mptrac.h:1456
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:349
#define RH(p, t, h2o)
Compute relative humidity over water.
Definition: mptrac.h:1426
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1846
#define TDEW(p, h2o)
Calculate dew point temperature.
Definition: mptrac.h:1595
Climatological data.
Definition: mptrac.h:3315
clim_zm_t ho2
HO2 zonal means.
Definition: mptrac.h:3345
clim_zm_t hno3
HNO3 zonal means.
Definition: mptrac.h:3336
clim_zm_t o1d
O(1D) zonal means.
Definition: mptrac.h:3348
clim_zm_t h2o2
H2O2 zonal means.
Definition: mptrac.h:3342
Control parameters.
Definition: mptrac.h:2170
double conv_cape
CAPE threshold for convection module [J/kg].
Definition: mptrac.h:2693
double conv_cin
CIN threshold for convection module [J/kg].
Definition: mptrac.h:2696
Meteo data structure.
Definition: mptrac.h:3374
int nx
Number of longitudes.
Definition: mptrac.h:3380
int ny
Number of latitudes.
Definition: mptrac.h:3383
int np
Number of pressure levels.
Definition: mptrac.h:3386
double lon[EX]
Longitude [deg].
Definition: mptrac.h:3392
double time
Time [s].
Definition: mptrac.h:3377
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3395
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3398
Here is the call graph for this function: