37 double *ahtd, *aqtd, *avtd, ahtdm, aqtdm[
NQ], avtdm, *lat1_old, *lat2_old,
38 *lh1, *lh2, *lon1_old, *lon2_old, *lv1, *lv2, *rhtd, *rqtd, *rvtd, rhtdm,
39 rqtdm[
NQ], rvtdm, t0 =
40 0, x0[3], x1[3], x2[3], z1, *z1_old, z2, *z2_old, *work;
42 int f, init = 0, ip, iq, np;
47 ALLOC(lon1_old,
double,
49 ALLOC(lat1_old,
double,
57 ALLOC(lon2_old,
double,
59 ALLOC(lat2_old,
double,
84 ERRMSG(
"Give parameters: <ctl> <dist.tab> <param> <atm1a> <atm1b>"
85 " [<atm2a> <atm2b> ...]");
89 int ens = (int)
scan_ctl(argv[1], argc, argv,
"DIST_ENS", -1,
"-999", NULL);
90 double p0 =
P(
scan_ctl(argv[1], argc, argv,
"DIST_Z0", -1,
"-1000", NULL));
91 double p1 =
P(
scan_ctl(argv[1], argc, argv,
"DIST_Z1", -1,
"1000", NULL));
92 double lat0 =
scan_ctl(argv[1], argc, argv,
"DIST_LAT0", -1,
"-1000", NULL);
93 double lat1 =
scan_ctl(argv[1], argc, argv,
"DIST_LAT1", -1,
"1000", NULL);
94 double lon0 =
scan_ctl(argv[1], argc, argv,
"DIST_LON0", -1,
"-1000", NULL);
95 double lon1 =
scan_ctl(argv[1], argc, argv,
"DIST_LON1", -1,
"1000", NULL);
97 scan_ctl(argv[1], argc, argv,
"DIST_ZSCORE", -1,
"-999", NULL);
100 LOG(1,
"Write transport deviations: %s", argv[2]);
103 if (!(out = fopen(argv[2],
"w")))
104 ERRMSG(
"Cannot create file!");
109 "# $2 = time difference [s]\n"
110 "# $3 = absolute horizontal distance (%s) [km]\n"
111 "# $4 = relative horizontal distance (%s) [%%]\n"
112 "# $5 = absolute vertical distance (%s) [km]\n"
113 "# $6 = relative vertical distance (%s) [%%]\n",
114 argv[3], argv[3], argv[3], argv[3]);
115 for (iq = 0; iq < ctl.
nq; iq++)
117 "# $%d = %s absolute difference (%s) [%s]\n"
118 "# $%d = %s relative difference (%s) [%%]\n",
120 8 + 2 * iq, ctl.
qnt_name[iq], argv[3]);
121 fprintf(out,
"# $%d = number of particles\n\n", 7 + 2 * ctl.
nq);
124 for (f = 4; f < argc; f += 2) {
131 if (atm1->
np != atm2->
np)
132 ERRMSG(
"Different numbers of particles!");
145 for (ip = 0; ip < atm1->
np; ip++) {
146 ahtd[ip] = avtd[ip] = rhtd[ip] = rvtd[ip] = 0;
147 for (iq = 0; iq < ctl.
nq; iq++)
148 aqtd[iq *
NP + ip] = rqtd[iq *
NP + ip] = 0;
152 for (ip = 0; ip < atm1->
np; ip++) {
157 ERRMSG(
"Air parcel index does not match!");
162 || atm2->
q[ctl.
qnt_ens][ip] != ens))
166 if (!isfinite(atm1->
time[ip]) || !isfinite(atm2->
time[ip]))
170 if (atm1->
p[ip] > p0 || atm1->
p[ip] < p1
171 || atm1->
lon[ip] < lon0 || atm1->
lon[ip] > lon1
172 || atm1->
lat[ip] < lat0 || atm1->
lat[ip] > lat1)
174 if (atm2->
p[ip] > p0 || atm2->
p[ip] < p1
175 || atm2->
lon[ip] < lon0 || atm2->
lon[ip] > lon1
176 || atm2->
lat[ip] < lat0 || atm2->
lat[ip] > lat1)
186 ahtd[np] =
DIST(x1, x2);
188 for (iq = 0; iq < ctl.
nq; iq++)
189 aqtd[iq *
NP + np] = atm1->
q[iq][ip] - atm2->
q[iq][ip];
195 geo2cart(0, lon1_old[ip], lat1_old[ip], x0);
196 lh1[ip] +=
DIST(x0, x1);
197 lv1[ip] += fabs(z1_old[ip] - z1);
199 geo2cart(0, lon2_old[ip], lat2_old[ip], x0);
200 lh2[ip] +=
DIST(x0, x2);
201 lv2[ip] += fabs(z2_old[ip] - z2);
204 if (lh1[ip] + lh2[ip] > 0)
205 rhtd[np] = 200. *
DIST(x1, x2) / (lh1[ip] + lh2[ip]);
206 if (lv1[ip] + lv2[ip] > 0)
207 rvtd[np] = 200. * (z1 - z2) / (lv1[ip] + lv2[ip]);
211 for (iq = 0; iq < ctl.
nq; iq++)
212 rqtd[iq *
NP + np] = 200. * (atm1->
q[iq][ip] - atm2->
q[iq][ip])
213 / (fabs(atm1->
q[iq][ip]) + fabs(atm2->
q[iq][ip]));
216 lon1_old[ip] = atm1->
lon[ip];
217 lat1_old[ip] = atm1->
lat[ip];
220 lon2_old[ip] = atm2->
lon[ip];
221 lat2_old[ip] = atm2->
lat[ip];
229 if (zscore > 0 && np > 1) {
232 size_t n = (size_t) np;
233 double muh = gsl_stats_mean(ahtd, 1, n);
234 double muv = gsl_stats_mean(avtd, 1, n);
235 double sigh = gsl_stats_sd(ahtd, 1, n);
236 double sigv = gsl_stats_sd(avtd, 1, n);
240 for (
size_t i = 0; i < n; i++)
241 if (fabs((ahtd[i] - muh) / sigh) < zscore
242 && fabs((avtd[i] - muv) / sigv) < zscore) {
247 for (iq = 0; iq < ctl.
nq; iq++) {
248 aqtd[iq *
NP + np] = aqtd[iq *
NP + (int) i];
249 rqtd[iq *
NP + np] = rqtd[iq *
NP + (int) i];
256 if (strcasecmp(argv[3],
"mean") == 0) {
257 ahtdm = gsl_stats_mean(ahtd, 1, (
size_t) np);
258 rhtdm = gsl_stats_mean(rhtd, 1, (
size_t) np);
259 avtdm = gsl_stats_mean(avtd, 1, (
size_t) np);
260 rvtdm = gsl_stats_mean(rvtd, 1, (
size_t) np);
261 for (iq = 0; iq < ctl.
nq; iq++) {
262 aqtdm[iq] = gsl_stats_mean(&aqtd[iq *
NP], 1, (
size_t) np);
263 rqtdm[iq] = gsl_stats_mean(&rqtd[iq *
NP], 1, (
size_t) np);
265 }
else if (strcasecmp(argv[3],
"stddev") == 0) {
266 ahtdm = gsl_stats_sd(ahtd, 1, (
size_t) np);
267 rhtdm = gsl_stats_sd(rhtd, 1, (
size_t) np);
268 avtdm = gsl_stats_sd(avtd, 1, (
size_t) np);
269 rvtdm = gsl_stats_sd(rvtd, 1, (
size_t) np);
270 for (iq = 0; iq < ctl.
nq; iq++) {
271 aqtdm[iq] = gsl_stats_sd(&aqtd[iq *
NP], 1, (
size_t) np);
272 rqtdm[iq] = gsl_stats_sd(&rqtd[iq *
NP], 1, (
size_t) np);
274 }
else if (strcasecmp(argv[3],
"min") == 0) {
275 ahtdm = gsl_stats_min(ahtd, 1, (
size_t) np);
276 rhtdm = gsl_stats_min(rhtd, 1, (
size_t) np);
277 avtdm = gsl_stats_min(avtd, 1, (
size_t) np);
278 rvtdm = gsl_stats_min(rvtd, 1, (
size_t) np);
279 for (iq = 0; iq < ctl.
nq; iq++) {
280 aqtdm[iq] = gsl_stats_min(&aqtd[iq *
NP], 1, (
size_t) np);
281 rqtdm[iq] = gsl_stats_min(&rqtd[iq *
NP], 1, (
size_t) np);
283 }
else if (strcasecmp(argv[3],
"max") == 0) {
284 ahtdm = gsl_stats_max(ahtd, 1, (
size_t) np);
285 rhtdm = gsl_stats_max(rhtd, 1, (
size_t) np);
286 avtdm = gsl_stats_max(avtd, 1, (
size_t) np);
287 rvtdm = gsl_stats_max(rvtd, 1, (
size_t) np);
288 for (iq = 0; iq < ctl.
nq; iq++) {
289 aqtdm[iq] = gsl_stats_max(&aqtd[iq *
NP], 1, (
size_t) np);
290 rqtdm[iq] = gsl_stats_max(&rqtd[iq *
NP], 1, (
size_t) np);
292 }
else if (strcasecmp(argv[3],
"skew") == 0) {
293 ahtdm = gsl_stats_skew(ahtd, 1, (
size_t) np);
294 rhtdm = gsl_stats_skew(rhtd, 1, (
size_t) np);
295 avtdm = gsl_stats_skew(avtd, 1, (
size_t) np);
296 rvtdm = gsl_stats_skew(rvtd, 1, (
size_t) np);
297 for (iq = 0; iq < ctl.
nq; iq++) {
298 aqtdm[iq] = gsl_stats_skew(&aqtd[iq *
NP], 1, (
size_t) np);
299 rqtdm[iq] = gsl_stats_skew(&rqtd[iq *
NP], 1, (
size_t) np);
301 }
else if (strcasecmp(argv[3],
"kurt") == 0) {
302 ahtdm = gsl_stats_kurtosis(ahtd, 1, (
size_t) np);
303 rhtdm = gsl_stats_kurtosis(rhtd, 1, (
size_t) np);
304 avtdm = gsl_stats_kurtosis(avtd, 1, (
size_t) np);
305 rvtdm = gsl_stats_kurtosis(rvtd, 1, (
size_t) np);
306 for (iq = 0; iq < ctl.
nq; iq++) {
307 aqtdm[iq] = gsl_stats_kurtosis(&aqtd[iq *
NP], 1, (
size_t) np);
308 rqtdm[iq] = gsl_stats_kurtosis(&rqtd[iq *
NP], 1, (
size_t) np);
310 }
else if (strcasecmp(argv[3],
"absdev") == 0) {
311 ahtdm = gsl_stats_absdev_m(ahtd, 1, (
size_t) np, 0.0);
312 rhtdm = gsl_stats_absdev_m(rhtd, 1, (
size_t) np, 0.0);
313 avtdm = gsl_stats_absdev_m(avtd, 1, (
size_t) np, 0.0);
314 rvtdm = gsl_stats_absdev_m(rvtd, 1, (
size_t) np, 0.0);
315 for (iq = 0; iq < ctl.
nq; iq++) {
316 aqtdm[iq] = gsl_stats_absdev_m(&aqtd[iq *
NP], 1, (
size_t) np, 0.0);
317 rqtdm[iq] = gsl_stats_absdev_m(&rqtd[iq *
NP], 1, (
size_t) np, 0.0);
319 }
else if (strcasecmp(argv[3],
"median") == 0) {
320 ahtdm = gsl_stats_median(ahtd, 1, (
size_t) np);
321 rhtdm = gsl_stats_median(rhtd, 1, (
size_t) np);
322 avtdm = gsl_stats_median(avtd, 1, (
size_t) np);
323 rvtdm = gsl_stats_median(rvtd, 1, (
size_t) np);
324 for (iq = 0; iq < ctl.
nq; iq++) {
325 aqtdm[iq] = gsl_stats_median(&aqtd[iq *
NP], 1, (
size_t) np);
326 rqtdm[iq] = gsl_stats_median(&rqtd[iq *
NP], 1, (
size_t) np);
328 }
else if (strcasecmp(argv[3],
"mad") == 0) {
329 ahtdm = gsl_stats_mad0(ahtd, 1, (
size_t) np, work);
330 rhtdm = gsl_stats_mad0(rhtd, 1, (
size_t) np, work);
331 avtdm = gsl_stats_mad0(avtd, 1, (
size_t) np, work);
332 rvtdm = gsl_stats_mad0(rvtd, 1, (
size_t) np, work);
333 for (iq = 0; iq < ctl.
nq; iq++) {
334 aqtdm[iq] = gsl_stats_mad0(&aqtd[iq *
NP], 1, (
size_t) np, work);
335 rqtdm[iq] = gsl_stats_mad0(&rqtd[iq *
NP], 1, (
size_t) np, work);
338 ERRMSG(
"Unknown parameter!");
341 fprintf(out,
"%.2f %.2f %g %g %g %g", t, t - t0,
342 ahtdm, rhtdm, avtdm, rvtdm);
343 for (iq = 0; iq < ctl.
nq; iq++) {
349 fprintf(out,
" %d\n", np);
int main(int argc, char *argv[])
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.
double time_from_filename(const char *filename, const int offset)
Extracts and converts a timestamp from a filename to Julian seconds.
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.
int 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.
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 ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define Z(p)
Convert pressure to altitude.
#define P(z)
Compute pressure at given altitude.
#define NQ
Maximum number of quantities per data point.
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
#define NP
Maximum number of atmospheric data points.
#define LOG(level,...)
Print a log message with a specified logging level.
#define DIST(a, b)
Calculate the distance between two points in Cartesian coordinates.
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].
char qnt_format[NQ][LEN]
Quantity output format.
int atm_type
Type of atmospheric data files (0=ASCII, 1=binary, 2=netCDF, 3=CLaMS_traj, 4=CLaMS_pos).
char qnt_unit[NQ][LEN]
Quantity units.
char qnt_name[NQ][LEN]
Quantity names.
int qnt_ens
Quantity array index for ensemble IDs.
int qnt_idx
Quantity array index for air parcel IDs.
int nq
Number of quantities.