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.");
67 mptrac_alloc(&ctl, &cache, &clim, &met0, &met1, &atm, &depo, &dd);
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);
80 scan_ctl(argv[1], argc, argv,
"INIT_RELZ0", -1,
"-999", NULL);
82 scan_ctl(argv[1], argc, argv,
"INIT_RELZ1", -1,
"-999", NULL);
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);
89 scan_ctl(argv[1], argc, argv,
"INIT_LON0", -1,
"0", NULL);
91 scan_ctl(argv[1], argc, argv,
"INIT_LON1", -1,
"0", NULL);
93 scan_ctl(argv[1], argc, argv,
"INIT_DLON", -1,
"1", NULL);
95 scan_ctl(argv[1], argc, argv,
"INIT_LAT0", -1,
"0", NULL);
97 scan_ctl(argv[1], argc, argv,
"INIT_LAT1", -1,
"0", NULL);
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);
103 scan_ctl(argv[1], argc, argv,
"INIT_SLON", -1,
"0", NULL);
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);
110 scan_ctl(argv[1], argc, argv,
"INIT_ULON", -1,
"0", NULL);
112 scan_ctl(argv[1], argc, argv,
"INIT_ULAT", -1,
"0", NULL);
114 (int)
scan_ctl(argv[1], argc, argv,
"INIT_EVENLY", -1,
"0", NULL);
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);
120 (int)
scan_ctl(argv[1], argc, argv,
"INIT_REP", -1,
"1", NULL);
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);
132 ERRMSG(
"INIT_EVENLY is only supported for lat/lon grids");
134 ERRMSG(
"INIT_BELLRAD is only supported for lat/lon grids");
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))
145 (
"INIT time, vertical, longitude, and latitude ranges must be ascending!");
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))
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))
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!");
176 gsl_rng *rng = gsl_rng_alloc(gsl_rng_default);
179 double ptop = 0, dpcolmax = 0;
180 if (col_mass || use_relz) {
182 ptop = gsl_stats_min(met0->
p, 1, (
size_t) met0->
np);
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]);
189 dpcolmax = psmax - ptop;
194 const long long ntrymax = init_np > 0 ? 1000000LL * init_np : 0;
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);
201 for (
double vert = vert0;
202 vert <= vert1 && !(init_np > 0 && atm->
np >= init_np);
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);
210 irep < rep && !(init_np > 0 && atm->
np >= init_np); irep++) {
213 ERRMSG(
"Too many particles!");
216 if (init_np > 0 && ++ntry > ntrymax)
218 (
"INIT_NP could not be reached by INIT_WELL_MIXED sampling!");
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);
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);
233 rg = gsl_ran_gaussian_ziggurat(rng, slon / 2.3548);
234 const double sx_coord =
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);
246 rg = gsl_ran_gaussian_ziggurat(rng, slat / 2.3548);
247 const double sy_coord =
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) >
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)
266 atm->
lat[atm->
np], &ps, ci, cw, 1);
269 atm->
lat[atm->
np], &pbl, ci, cw, 1);
274 const double zsurf =
Z(ps);
275 const double zpbl =
Z(pbl);
276 const double depth = zpbl - zsurf;
279 atm->
p[atm->
np] =
P(zsurf + (relz_sample + rg + ru) * depth);
285 geo2cart(0.0, 0.5 * (lon0 + lon1), 0.5 * (lat0 + lat1), x0);
294 0.5 * (1. + cos(M_PI * rad / bellrad));
297 0.5 * (1. + cos(M_PI * rad / bellrad));
306 atm->
lat[atm->
np], &ps, ci, cw, 1);
307 const double dpcol = ps - ptop;
308 if (gsl_rng_uniform(rng) > dpcol / dpcolmax)
311 atm->
p[atm->
np] = ptop + dpcol * gsl_rng_uniform(rng);
318 }
while (init_np > 0 && atm->
np < init_np);
322 ERRMSG(
"Did not create any air parcels!");
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;
330 if (ctl->
qnt_vmr >= 0 && bellrad <= 0)
331 for (
int ip = 0; ip < atm->
np; ip++)
336 for (
int ip = 0; ip < atm->
np; ip++)
337 atm->
q[ctl->
qnt_idx][ip] = idx_offset + ip;
341 for (
int ip = 0; ip < atm->
np; ip++)
349 mptrac_free(ctl, cache, clim, met0, met1, atm, depo, dd);
360 printf(
"\nMPTRAC atm_init tool.\n\n");
362 (
"Create an atmospheric data file with initial air parcel positions.\n");
365 printf(
" atm_init <ctl> <atm_out> [KEY VALUE ...]\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");
int main(int argc, char *argv[])
void usage(void)
Print command-line help.
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.
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.
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_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.
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
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.
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.
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.
void geo2cart(const double z, const double lon, const double lat, double *x)
Converts geographic coordinates (longitude, latitude, altitude) to Cartesian coordinates.
MPTRAC library declarations.
#define RE
Mean radius of Earth [km].
#define DOTP(a, b)
Calculate the dot product of two vectors.
#define INTPOL_INIT
Initialize arrays for interpolation.
#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 DX2DEG(dx, lat)
Convert a distance in kilometers to degrees longitude at a given latitude.
#define DEG2RAD(deg)
Converts degrees to radians.
#define NP
Maximum number of atmospheric data points.
#define DY2DEG(dy)
Convert a distance in kilometers to degrees latitude.
#define MAX(a, b)
Macro to determine the maximum of two values.
double lat[NP]
Latitude [deg].
double lon[NP]
Longitude [deg].
int np
Number of air parcels.
double q[NQ][NP]
Quantity data (for various, user-defined attributes).
double p[NP]
Pressure [hPa].
int qnt_m
Quantity array index for mass.
int qnt_aoa
Quantity array index for the age-of-air reference timestamp [s since 2000-01-01 00:00 UTC].
int qnt_vmr
Quantity array index for volume mixing ratio.
int qnt_pbl
Quantity array index for boundary layer pressure.
int qnt_ps
Quantity array index for surface pressure.
int qnt_ens
Quantity array index for ensemble IDs.
int met_coord_type
Type of coordinates for meteo data (-1=detect, 0=lat/lon [deg], 1=UTM [m]).
int qnt_idx
Quantity array index for air parcel IDs.
Domain decomposition data structure.
Ground inventories of deposited radionuclides.
int nx
Number of longitudes.
int ny
Number of latitudes.
float ps[EX][EY]
Surface pressure [hPa].
int np
Number of pressure levels.
float pbl[EX][EY]
Boundary layer pressure [hPa].
double p[EP]
Pressure levels [hPa].