MPTRAC
Loading...
Searching...
No Matches
atm_init.c File Reference

Create atmospheric data file with initial air parcel positions. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

void usage (void)
 Print command-line help.
 
int main (int argc, char *argv[])
 

Detailed Description

Create atmospheric data file with initial air parcel positions.

Definition in file atm_init.c.

Function Documentation

◆ usage()

void usage ( void )

Print command-line help.

Definition at line 357 of file atm_init.c.

358 {
359
360 printf("\nMPTRAC atm_init tool.\n\n");
361 printf
362 ("Create an atmospheric data file with initial air parcel positions.\n");
363 printf("\n");
364 printf("Usage:\n");
365 printf(" atm_init <ctl> <atm_out> [KEY VALUE ...]\n");
366 printf("\n");
367 printf("Arguments:\n");
368 printf(" <ctl> Control file.\n");
369 printf(" <atm_out> Atmospheric output file.\n");
370 printf(" [KEY VALUE] Optional control parameters.\n");
371 printf("\nFurther information:\n");
372 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
373}

◆ main()

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

Definition at line 39 of file atm_init.c.

41 {
42
43 ctl_t *ctl;
44
45 cache_t *cache;
46
47 clim_t *clim;
48
49 atm_t *atm;
50
51 depo_t *depo;
52
53 met_t *met0, *met1;
54
55 dd_t *dd;
56
57 /* Print usage information... */
58 USAGE;
59
60 /* Check arguments... */
61 if (argc < 3)
62 ERRMSG("Missing or invalid command-line arguments.\n\n"
63 "Usage: atm_init <ctl> <atm_out> [KEY VALUE ...]\n\n"
64 "Use -h for full help.");
65
66 /* Allocate... */
67 mptrac_alloc(&ctl, &cache, &clim, &met0, &met1, &atm, &depo, &dd);
68
69 /* Read control parameters... */
70 mptrac_read_ctl(argv[1], argc, argv, ctl);
71 const int ens =
72 (int) scan_ctl(argv[1], argc, argv, "INIT_ENS", -1, "0", NULL);
73 const double t0 = scan_ctl(argv[1], argc, argv, "INIT_T0", -1, "0", NULL);
74 const double t1 = scan_ctl(argv[1], argc, argv, "INIT_T1", -1, "0", NULL);
75 const double dt = scan_ctl(argv[1], argc, argv, "INIT_DT", -1, "1", NULL);
76 const double z0 = scan_ctl(argv[1], argc, argv, "INIT_Z0", -1, "0", NULL);
77 const double z1 = scan_ctl(argv[1], argc, argv, "INIT_Z1", -1, "0", NULL);
78 const double dz = scan_ctl(argv[1], argc, argv, "INIT_DZ", -1, "1", NULL);
79 const double relz0 =
80 scan_ctl(argv[1], argc, argv, "INIT_RELZ0", -1, "-999", NULL);
81 const double relz1 =
82 scan_ctl(argv[1], argc, argv, "INIT_RELZ1", -1, "-999", NULL);
83 const double drelz =
84 scan_ctl(argv[1], argc, argv, "INIT_DRELZ", -1, "1", NULL);
85 const int random_relz =
86 (int) scan_ctl(argv[1], argc, argv, "INIT_RANDOM_RELZ", -1, "0", NULL);
87 const int use_relz = (relz0 > -900.0 || relz1 > -900.0);
88 const double lon0 =
89 scan_ctl(argv[1], argc, argv, "INIT_LON0", -1, "0", NULL);
90 const double lon1 =
91 scan_ctl(argv[1], argc, argv, "INIT_LON1", -1, "0", NULL);
92 const double dlon =
93 scan_ctl(argv[1], argc, argv, "INIT_DLON", -1, "1", NULL);
94 const double lat0 =
95 scan_ctl(argv[1], argc, argv, "INIT_LAT0", -1, "0", NULL);
96 const double lat1 =
97 scan_ctl(argv[1], argc, argv, "INIT_LAT1", -1, "0", NULL);
98 const double dlat =
99 scan_ctl(argv[1], argc, argv, "INIT_DLAT", -1, "1", NULL);
100 const double st = scan_ctl(argv[1], argc, argv, "INIT_ST", -1, "0", NULL);
101 const double sz = scan_ctl(argv[1], argc, argv, "INIT_SZ", -1, "0", NULL);
102 const double slon =
103 scan_ctl(argv[1], argc, argv, "INIT_SLON", -1, "0", NULL);
104 const double slat =
105 scan_ctl(argv[1], argc, argv, "INIT_SLAT", -1, "0", NULL);
106 const double sx = scan_ctl(argv[1], argc, argv, "INIT_SX", -1, "0", NULL);
107 const double ut = scan_ctl(argv[1], argc, argv, "INIT_UT", -1, "0", NULL);
108 const double uz = scan_ctl(argv[1], argc, argv, "INIT_UZ", -1, "0", NULL);
109 const double ulon =
110 scan_ctl(argv[1], argc, argv, "INIT_ULON", -1, "0", NULL);
111 const double ulat =
112 scan_ctl(argv[1], argc, argv, "INIT_ULAT", -1, "0", NULL);
113 const int even =
114 (int) scan_ctl(argv[1], argc, argv, "INIT_EVENLY", -1, "0", NULL);
115 const int col_mass =
116 (int) scan_ctl(argv[1], argc, argv, "INIT_COL_MASS", -1, "0", NULL);
117 const int well_mixed =
118 (int) scan_ctl(argv[1], argc, argv, "INIT_WELL_MIXED", -1, "0", NULL);
119 const int rep =
120 (int) scan_ctl(argv[1], argc, argv, "INIT_REP", -1, "1", NULL);
121 const int init_np =
122 (int) scan_ctl(argv[1], argc, argv, "INIT_NP", -1, "0", NULL);
123 const double m = scan_ctl(argv[1], argc, argv, "INIT_MASS", -1, "0", NULL);
124 const double vmr = scan_ctl(argv[1], argc, argv, "INIT_VMR", -1, "0", NULL);
125 const double bellrad =
126 scan_ctl(argv[1], argc, argv, "INIT_BELLRAD", -1, "0", NULL);
127 const int idx_offset =
128 (int) scan_ctl(argv[1], argc, argv, "INIT_IDX_OFFSET", -1, "0", NULL);
129
130 /* Check arguments... */
131 if (ctl->met_coord_type != 0 && even)
132 ERRMSG("INIT_EVENLY is only supported for lat/lon grids");
133 if (ctl->met_coord_type != 0 && bellrad > 0)
134 ERRMSG("INIT_BELLRAD is only supported for lat/lon grids");
135 if (rep <= 0)
136 ERRMSG("INIT_REP must be positive!");
137 if (init_np < 0 || init_np > NP)
138 ERRMSG("INIT_NP must be between 0 and NP!");
139 if (!isfinite(dt) || dt <= 0 || !isfinite(dlon) || dlon <= 0
140 || !isfinite(dlat) || dlat <= 0)
141 ERRMSG("INIT_DT, INIT_DLON, and INIT_DLAT must be positive and finite!");
142 if (t1 < t0 || lon1 < lon0 || lat1 < lat0
143 || (use_relz ? relz1 < relz0 : z1 < z0))
144 ERRMSG
145 ("INIT time, vertical, longitude, and latitude ranges must be ascending!");
146 if (well_mixed && ctl->met_coord_type != 0)
147 ERRMSG("INIT_WELL_MIXED is only supported for lat/lon grids!");
148 if (well_mixed && !even)
149 ERRMSG("INIT_WELL_MIXED requires INIT_EVENLY=1!");
150 if (well_mixed && !col_mass)
151 ERRMSG("INIT_WELL_MIXED requires INIT_COL_MASS=1!");
152 if (init_np > 0 && !well_mixed)
153 ERRMSG("INIT_NP requires INIT_WELL_MIXED!");
154 if (col_mass && (t1 != t0 || st > 0 || ut > 0))
155 ERRMSG
156 ("INIT_COL_MASS/INIT_WELL_MIXED require INIT_T0 == INIT_T1 and no time scattering!");
157 if (use_relz && (z0 != 0.0 || z1 != 0.0))
158 ERRMSG("INIT_RELZ0/INIT_RELZ1 cannot be combined with INIT_Z0/INIT_Z1!");
159 if (use_relz && (relz0 <= -900.0 || relz1 <= -900.0))
160 ERRMSG("INIT_RELZ0 and INIT_RELZ1 must both be set!");
161 if (use_relz && (t1 != t0 || st > 0 || ut > 0))
162 ERRMSG
163 ("INIT_RELZ0/INIT_RELZ1 require INIT_T0 == INIT_T1 and no time scattering!");
164 if (use_relz && random_relz && relz1 < relz0)
165 ERRMSG("INIT_RELZ1 must be greater than or equal to INIT_RELZ0!");
166 if (use_relz && !random_relz && drelz <= 0)
167 ERRMSG("INIT_DRELZ must be positive!");
168 if (!use_relz && dz <= 0)
169 ERRMSG("INIT_DZ must be positive!");
170
171 /* Read climatological data... */
172 mptrac_read_clim(ctl, clim);
173
174 /* Initialize random number generator... */
175 gsl_rng_env_setup();
176 gsl_rng *rng = gsl_rng_alloc(gsl_rng_default);
177
178 /* Get meteorological data for column-mass, well-mixed, and relative-PBL initialization... */
179 double ptop = 0, dpcolmax = 0;
180 if (col_mass || use_relz) {
181 mptrac_get_met(ctl, clim, t0, &met0, &met1, dd);
182 ptop = gsl_stats_min(met0->p, 1, (size_t) met0->np);
183 double psmax = -1;
184 for (int ix = 0; ix < met0->nx; ix++)
185 for (int iy = 0; iy < met0->ny; iy++) {
186 psmax = MAX(psmax, met0->ps[ix][iy]);
187 psmax = MAX(psmax, met1->ps[ix][iy]);
188 }
189 dpcolmax = psmax - ptop;
190 }
191
192 /* Repeat the ordered release grid when INIT_NP needs more accepted particles. */
193 long long ntry = 0;
194 const long long ntrymax = init_np > 0 ? 1000000LL * init_np : 0;
195 do {
196 const double vert0 = use_relz ? relz0 : z0;
197 const double vert1 = use_relz ? (random_relz ? relz0 : relz1) : z1;
198 const double dvert = use_relz ? (random_relz ? 1.0 : drelz) : dz;
199 for (double t = t0; t <= t1 && !(init_np > 0 && atm->np >= init_np);
200 t += dt)
201 for (double vert = vert0;
202 vert <= vert1 && !(init_np > 0 && atm->np >= init_np);
203 vert += dvert)
204 for (double lon = lon0;
205 lon <= lon1 && !(init_np > 0 && atm->np >= init_np); lon += dlon)
206 for (double lat = lat0;
207 lat <= lat1 && !(init_np > 0 && atm->np >= init_np);
208 lat += dlat)
209 for (int irep = 0;
210 irep < rep && !(init_np > 0 && atm->np >= init_np); irep++) {
211
212 if (atm->np >= NP)
213 ERRMSG("Too many particles!");
214
215 /* Check number of attempts... */
216 if (init_np > 0 && ++ntry > ntrymax)
217 ERRMSG
218 ("INIT_NP could not be reached by INIT_WELL_MIXED sampling!");
219
220 /* Set time... */
221 double rg = gsl_ran_gaussian_ziggurat(rng, st / 2.3548);
222 double ru = ut * (gsl_rng_uniform(rng) - 0.5);
223 atm->time[atm->np] = (t + rg + ru);
224
225 if (!use_relz) {
226 /* Keep the legacy RNG order for absolute-height initialization. */
227 rg = gsl_ran_gaussian_ziggurat(rng, sz / 2.3548);
228 ru = uz * (gsl_rng_uniform(rng) - 0.5);
229 atm->p[atm->np] = P(vert + rg + ru);
230 }
231
232 /* Set horizontal position... */
233 rg = gsl_ran_gaussian_ziggurat(rng, slon / 2.3548);
234 const double sx_coord =
235 ctl->met_coord_type == 0 ? DX2DEG(sx, lat) : sx;
236 double rx = gsl_ran_gaussian_ziggurat(rng, sx_coord / 2.3548);
237 ru = ulon * (gsl_rng_uniform(rng) - 0.5);
238 atm->lon[atm->np] = (lon + rg + rx + ru);
239
240 /* Set ensemble index... */
241 if (ctl->qnt_ens >= 0)
242 atm->q[ctl->qnt_ens][atm->np] = ens;
243
244 /* Apply cosine-latitude weighting... */
245 do {
246 rg = gsl_ran_gaussian_ziggurat(rng, slat / 2.3548);
247 const double sy_coord =
248 ctl->met_coord_type == 0 ? DY2DEG(sx) : sx;
249 rx = gsl_ran_gaussian_ziggurat(rng, sy_coord / 2.3548);
250 ru = ulat * (gsl_rng_uniform(rng) - 0.5);
251 atm->lat[atm->np] = (lat + rg + rx + ru);
252 } while (even && gsl_rng_uniform(rng) >
253 fabs(cos(DEG2RAD(atm->lat[atm->np]))));
254
255 if (use_relz) {
256 /* Relative-PBL initialization needs the final horizontal position first. */
257 rg = gsl_ran_gaussian_ziggurat(rng, sz / 2.3548);
258 ru = uz * (gsl_rng_uniform(rng) - 0.5);
259 const double relz_sample =
260 random_relz ? relz0 + gsl_rng_uniform(rng) * (relz1 - relz0)
261 : vert;
263 double ps, pbl;
264 intpol_met_time_2d(met0, met0->ps, met1, met1->ps,
265 atm->time[atm->np], atm->lon[atm->np],
266 atm->lat[atm->np], &ps, ci, cw, 1);
267 intpol_met_time_2d(met0, met0->pbl, met1, met1->pbl,
268 atm->time[atm->np], atm->lon[atm->np],
269 atm->lat[atm->np], &pbl, ci, cw, 1);
270 if (ctl->qnt_ps >= 0)
271 atm->q[ctl->qnt_ps][atm->np] = ps;
272 if (ctl->qnt_pbl >= 0)
273 atm->q[ctl->qnt_pbl][atm->np] = pbl;
274 const double zsurf = Z(ps);
275 const double zpbl = Z(pbl);
276 const double depth = zpbl - zsurf;
277 if (depth <= 0)
278 continue;
279 atm->p[atm->np] = P(zsurf + (relz_sample + rg + ru) * depth);
280 }
281
282 /* Apply cosine bell (Williamson et al., 1992)... */
283 if (bellrad > 0) {
284 double x0[3], x1[3];
285 geo2cart(0.0, 0.5 * (lon0 + lon1), 0.5 * (lat0 + lat1), x0);
286 geo2cart(0.0, atm->lon[atm->np], atm->lat[atm->np], x1);
287 const double rad =
288 RE * acos(DOTP(x0, x1) / sqrt(DOTP(x0, x0)) /
289 sqrt(DOTP(x1, x1)));
290 if (rad > bellrad)
291 continue;
292 if (ctl->qnt_m >= 0)
293 atm->q[ctl->qnt_m][atm->np] =
294 0.5 * (1. + cos(M_PI * rad / bellrad));
295 if (ctl->qnt_vmr >= 0)
296 atm->q[ctl->qnt_vmr][atm->np] =
297 0.5 * (1. + cos(M_PI * rad / bellrad));
298 }
299
300 /* Apply column-mass and globally well-mixed air-mass weighting... */
301 if (col_mass) {
303 double ps;
304 intpol_met_time_2d(met0, met0->ps, met1, met1->ps,
305 atm->time[atm->np], atm->lon[atm->np],
306 atm->lat[atm->np], &ps, ci, cw, 1);
307 const double dpcol = ps - ptop;
308 if (gsl_rng_uniform(rng) > dpcol / dpcolmax)
309 continue;
310 if (well_mixed)
311 atm->p[atm->np] = ptop + dpcol * gsl_rng_uniform(rng);
312 }
313
314 /* Set particle counter... */
315 ++atm->np;
316 }
317
318 } while (init_np > 0 && atm->np < init_np);
319
320 /* Check number of air parcels... */
321 if (atm->np <= 0)
322 ERRMSG("Did not create any air parcels!");
323
324 /* Initialize mass... */
325 if (ctl->qnt_m >= 0 && bellrad <= 0)
326 for (int ip = 0; ip < atm->np; ip++)
327 atm->q[ctl->qnt_m][ip] = m / atm->np;
328
329 /* Initialize volume mixing ratio... */
330 if (ctl->qnt_vmr >= 0 && bellrad <= 0)
331 for (int ip = 0; ip < atm->np; ip++)
332 atm->q[ctl->qnt_vmr][ip] = vmr;
333
334 /* Initialize air parcel index... */
335 if (ctl->qnt_idx >= 0)
336 for (int ip = 0; ip < atm->np; ip++)
337 atm->q[ctl->qnt_idx][ip] = idx_offset + ip;
338
339 /* Initialize age of air... */
340 if (ctl->qnt_aoa >= 0)
341 for (int ip = 0; ip < atm->np; ip++)
342 atm->q[ctl->qnt_aoa][ip] = atm->time[ip];
343
344 /* Save data... */
345 mptrac_write_atm(argv[2], ctl, atm, 0);
346
347 /* Free... */
348 gsl_rng_free(rng);
349 mptrac_free(ctl, cache, clim, met0, met1, atm, depo, dd);
350
351 return EXIT_SUCCESS;
352}
void mptrac_write_atm(const char *filename, const ctl_t *ctl, const atm_t *atm, const double t)
Writes air parcel data to a file in various formats.
Definition mptrac.c:9103
void mptrac_free(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t *met0, met_t *met1, atm_t *atm, depo_t *depo, dd_t *dd)
Frees memory resources allocated for MPTRAC.
Definition mptrac.c:6956
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:13771
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:7034
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition mptrac.c:7277
void mptrac_alloc(ctl_t **ctl, cache_t **cache, clim_t **clim, met_t **met0, met_t **met1, atm_t **atm, depo_t **depo, dd_t **dd)
Allocates and initializes memory resources for MPTRAC.
Definition mptrac.c:6866
void intpol_met_time_2d(const met_t *met0, float array0[EX][EY], const met_t *met1, float array1[EX][EY], const double ts, const double lon, const double lat, double *var, int *ci, double *cw, const int init)
Interpolates meteorological data in 2D space and time.
Definition mptrac.c:3472
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:7351
void geo2cart(const double z, const double lon, const double lat, double *x)
Converts geographic coordinates (longitude, latitude, altitude) to Cartesian coordinates.
Definition mptrac.c:2931
#define RE
Mean radius of Earth [km].
Definition mptrac.h:315
#define DOTP(a, b)
Calculate the dot product of two vectors.
Definition mptrac.h:1038
#define INTPOL_INIT
Initialize arrays for interpolation.
Definition mptrac.h:1219
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition mptrac.h:2466
#define USAGE
Print usage information on -h or --help.
Definition mptrac.h:2273
#define Z(p)
Convert pressure to altitude.
Definition mptrac.h:2303
#define P(z)
Compute pressure at given altitude.
Definition mptrac.h:1843
#define DX2DEG(dx, lat)
Convert a distance in kilometers to degrees longitude at a given latitude.
Definition mptrac.h:906
#define DEG2RAD(deg)
Converts degrees to radians.
Definition mptrac.h:859
#define NP
Maximum number of atmospheric data points.
Definition mptrac.h:569
#define DY2DEG(dy)
Convert a distance in kilometers to degrees latitude.
Definition mptrac.h:924
#define MAX(a, b)
Macro to determine the maximum of two values.
Definition mptrac.h:1425
Air parcel data.
Definition mptrac.h:3662
double time[NP]
Time [s].
Definition mptrac.h:3668
double lat[NP]
Latitude [deg].
Definition mptrac.h:3677
double lon[NP]
Longitude [deg].
Definition mptrac.h:3674
int np
Number of air parcels.
Definition mptrac.h:3665
double q[NQ][NP]
Quantity data (for various, user-defined attributes).
Definition mptrac.h:3680
double p[NP]
Pressure [hPa].
Definition mptrac.h:3671
Cache data structure.
Definition mptrac.h:3726
Climatological data.
Definition mptrac.h:3893
Control parameters.
Definition mptrac.h:2557
int qnt_m
Quantity array index for mass.
Definition mptrac.h:2588
int qnt_aoa
Quantity array index for the age-of-air reference timestamp [s since 2000-01-01 00:00 UTC].
Definition mptrac.h:2865
int qnt_vmr
Quantity array index for volume mixing ratio.
Definition mptrac.h:2591
int qnt_pbl
Quantity array index for boundary layer pressure.
Definition mptrac.h:2630
int qnt_ps
Quantity array index for surface pressure.
Definition mptrac.h:2600
int qnt_ens
Quantity array index for ensemble IDs.
Definition mptrac.h:2582
int met_coord_type
Type of coordinates for meteo data (-1=detect, 0=lat/lon [deg], 1=UTM [m]).
Definition mptrac.h:2917
int qnt_idx
Quantity array index for air parcel IDs.
Definition mptrac.h:2579
Domain decomposition data structure.
Definition mptrac.h:4129
Ground inventories of deposited radionuclides.
Definition mptrac.h:3762
Meteo data structure.
Definition mptrac.h:3952
int nx
Number of longitudes.
Definition mptrac.h:3961
int ny
Number of latitudes.
Definition mptrac.h:3964
float ps[EX][EY]
Surface pressure [hPa].
Definition mptrac.h:3994
int np
Number of pressure levels.
Definition mptrac.h:3967
float pbl[EX][EY]
Boundary layer pressure [hPa].
Definition mptrac.h:4024
double p[EP]
Pressure levels [hPa].
Definition mptrac.h:3979
Here is the call graph for this function: