MPTRAC
Loading...
Searching...
No Matches
atm_split.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-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27/* ------------------------------------------------------------
28 Functions...
29 ------------------------------------------------------------ */
30
32void usage(
33 void);
34
35/* ------------------------------------------------------------
36 Main...
37 ------------------------------------------------------------ */
38
39int main(
40 int argc,
41 char *argv[]) {
42
43 atm_t *atm, *atm2;
44
45 ctl_t ctl;
46
47 char kernel[LEN];
48
49 double k, kw[EP], kz[EP], mmax = 0, mtot = 0, z, zmin = 0, zmax = 0;
50
51 int ip, nk = 0;
52
53 /* Print usage information... */
54 USAGE;
55
56 /* Check arguments... */
57 if (argc < 4)
58 ERRMSG("Missing or invalid command-line arguments.\n\n"
59 "Usage: atm_split <ctl> <atm_in> <atm_out> [KEY VALUE ...]\n\n"
60 "Use -h for full help.");
61
62 /* Allocate... */
63 ALLOC(atm, atm_t, 1);
64 ALLOC(atm2, atm_t, 1);
65
66 /* Read control parameters... */
67 mptrac_read_ctl(argv[1], argc, argv, &ctl);
68 const double n_input =
69 scan_ctl(argv[1], argc, argv, "SPLIT_N", -1, "", NULL);
70 if (!isfinite(n_input) || n_input < 1 || n_input > NP
71 || floor(n_input) != n_input)
72 ERRMSG("SPLIT_N must be an integer between 1 and NP!");
73 const int n = (int) n_input;
74 const double m = scan_ctl(argv[1], argc, argv, "SPLIT_M", -1, "-999", NULL);
75 const double um = scan_ctl(argv[1], argc, argv, "SPLIT_UM", -1, "0", NULL);
76 const double dt = scan_ctl(argv[1], argc, argv, "SPLIT_DT", -1, "0", NULL);
77 const double t0 = scan_ctl(argv[1], argc, argv, "SPLIT_T0", -1, "0", NULL);
78 const double t1 = scan_ctl(argv[1], argc, argv, "SPLIT_T1", -1, "0", NULL);
79 const double dz = scan_ctl(argv[1], argc, argv, "SPLIT_DZ", -1, "0", NULL);
80 const double z0 = scan_ctl(argv[1], argc, argv, "SPLIT_Z0", -1, "0", NULL);
81 const double z1 = scan_ctl(argv[1], argc, argv, "SPLIT_Z1", -1, "0", NULL);
82 const double dx = scan_ctl(argv[1], argc, argv, "SPLIT_DX", -1, "0", NULL);
83 const double lon0 =
84 scan_ctl(argv[1], argc, argv, "SPLIT_LON0", -1, "0", NULL);
85 const double lon1 =
86 scan_ctl(argv[1], argc, argv, "SPLIT_LON1", -1, "0", NULL);
87 const double lat0 =
88 scan_ctl(argv[1], argc, argv, "SPLIT_LAT0", -1, "0", NULL);
89 const double lat1 =
90 scan_ctl(argv[1], argc, argv, "SPLIT_LAT1", -1, "0", NULL);
91 scan_ctl(argv[1], argc, argv, "SPLIT_KERNEL", -1, "-", kernel);
92
93 /* Init random number generator... */
94 gsl_rng_env_setup();
95 gsl_rng *rng = gsl_rng_alloc(gsl_rng_default);
96
97 /* Read atmospheric data... */
98 if (!mptrac_read_atm(argv[2], &ctl, atm))
99 ERRMSG("Cannot open file!");
100
101 /* Read kernel function... */
102 if (kernel[0] != '-') {
103 read_kernel(kernel, kz, kw, &nk);
104 zmax = gsl_stats_max(kz, 1, (size_t) nk);
105 zmin = gsl_stats_min(kz, 1, (size_t) nk);
106 }
107
108 /* Validate parent masses and find the rejection-sampling scale... */
109 if (ctl.qnt_m >= 0)
110 for (ip = 0; ip < atm->np; ip++) {
111 if (!isfinite(atm->q[ctl.qnt_m][ip]) || atm->q[ctl.qnt_m][ip] < 0)
112 ERRMSG("Parent particle masses must be finite and nonnegative!");
113 mtot += atm->q[ctl.qnt_m][ip];
114 mmax = MAX(mmax, atm->q[ctl.qnt_m][ip]);
115 }
116 if (ctl.qnt_m >= 0 && mmax <= 0)
117 ERRMSG("At least one parent particle must have positive mass!");
118 if (m >= 0)
119 mtot = m;
120
121 /* Loop over air parcels... */
122 for (int i = 0; i < n; i++) {
123
124 if (atm2->np >= NP)
125 ERRMSG("Too many air parcels!");
126
127 /* Select parents by mass when available, uniformly otherwise... */
128 if (ctl.qnt_m >= 0)
129 do {
130 ip = (int) gsl_rng_uniform_int(rng, (long unsigned int) atm->np);
131 } while (gsl_rng_uniform(rng) > atm->q[ctl.qnt_m][ip] / mmax);
132 else
133 ip = (int) gsl_rng_uniform_int(rng, (long unsigned int) atm->np);
134
135 /* Set time... */
136 if (t1 > t0)
137 atm2->time[atm2->np] = t0 + (t1 - t0) * gsl_rng_uniform_pos(rng);
138 else
139 atm2->time[atm2->np] = atm->time[ip]
140 + gsl_ran_gaussian_ziggurat(rng, dt / 2.3548);
141
142 /* Set vertical position... */
143 do {
144 if (nk > 0) {
145 do {
146 z = zmin + (zmax - zmin) * gsl_rng_uniform_pos(rng);
147 k = kernel_weight(kz, kw, nk, P(z));
148 } while (gsl_rng_uniform(rng) > k);
149 atm2->p[atm2->np] = P(z);
150 } else if (z1 > z0)
151 atm2->p[atm2->np] = P(z0 + (z1 - z0) * gsl_rng_uniform_pos(rng));
152 else
153 atm2->p[atm2->np] = atm->p[ip]
154 + DZ2DP(gsl_ran_gaussian_ziggurat(rng, dz / 2.3548), atm->p[ip]);
155 } while (atm2->p[atm2->np] < P(100.) || atm2->p[atm2->np] > P(-1.));
156
157 /* Set horizontal position... */
158 if (lon1 > lon0 && lat1 > lat0) {
159 atm2->lon[atm2->np] = lon0 + (lon1 - lon0) * gsl_rng_uniform_pos(rng);
160 atm2->lat[atm2->np] = lat0 + (lat1 - lat0) * gsl_rng_uniform_pos(rng);
161 } else {
162 double dx_coord, dy_coord;
163 if (ctl.met_coord_type == 0) {
164 dx_coord = DX2DEG(dx, atm->lat[ip]);
165 dy_coord = DY2DEG(dx);
166 } else {
167 dx_coord = 1000. * dx;
168 dy_coord = 1000. * dx;
169 }
170 atm2->lon[atm2->np] = atm->lon[ip]
171 + gsl_ran_gaussian_ziggurat(rng, dx_coord / 2.3548);
172 atm2->lat[atm2->np] = atm->lat[ip]
173 + gsl_ran_gaussian_ziggurat(rng, dy_coord / 2.3548);
174 }
175
176 /* Copy quantities... */
177 for (int iq = 0; iq < ctl.nq; iq++)
178 atm2->q[iq][atm2->np] = atm->q[iq][ip];
179
180 /* Adjust mass... */
181 if (ctl.qnt_m >= 0)
182 atm2->q[ctl.qnt_m][atm2->np]
183 = (mtot + (um > 0 ? um * (gsl_rng_uniform_pos(rng) - 0.5) : 0.0)) / n;
184
185 /* Adjust air parcel index... */
186 if (ctl.qnt_idx >= 0)
187 atm2->q[ctl.qnt_idx][atm2->np] = atm2->np;
188
189 /* Increment particle counter... */
190 ++atm2->np;
191 }
192
193 /* Save data and close file... */
194 mptrac_write_atm(argv[3], &ctl, atm2, 0);
195
196 /* Free... */
197 free(atm);
198 free(atm2);
199
200 return EXIT_SUCCESS;
201}
202
203/*****************************************************************************/
204
206void usage(
207 void) {
208
209 printf("\nMPTRAC atm_split tool.\n\n");
210 printf("Split air parcels into a larger number of parcels.\n");
211 printf("\n");
212 printf("Usage:\n");
213 printf(" atm_split <ctl> <atm_in> <atm_out> [KEY VALUE ...]\n");
214 printf("\n");
215 printf("Arguments:\n");
216 printf(" <ctl> Control file.\n");
217 printf(" <atm_in> Atmospheric input file.\n");
218 printf(" <atm_out> Atmospheric output file.\n");
219 printf(" [KEY VALUE] Optional control parameters.\n");
220 printf("\nFurther information:\n");
221 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
222}
int main(int argc, char *argv[])
Definition atm_split.c:39
void usage(void)
Print command-line help.
Definition atm_split.c:206
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 read_kernel(const char *filename, double kz[EP], double kw[EP], int *nk)
Reads kernel function data from a file and populates the provided arrays.
Definition mptrac.c:9857
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
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:7202
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
double kernel_weight(const double kz[EP], const double kw[EP], const int nk, const double p)
Calculates the kernel weight based on altitude and given kernel data.
Definition mptrac.c:3642
MPTRAC library declarations.
#define LEN
Maximum length of ASCII data lines.
Definition mptrac.h:559
#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 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 DZ2DP(dz, p)
Convert a change in altitude to a change in pressure.
Definition mptrac.h:943
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition mptrac.h:668
#define NP
Maximum number of atmospheric data points.
Definition mptrac.h:569
#define EP
Maximum number of pressure levels for meteo data.
Definition mptrac.h:544
#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
Control parameters.
Definition mptrac.h:2557
int qnt_m
Quantity array index for mass.
Definition mptrac.h:2588
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
int nq
Number of quantities.
Definition mptrac.h:2564