MPTRAC
atm_stat.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-2024 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27int main(
28 int argc,
29 char *argv[]) {
30
31 ctl_t ctl;
32
33 atm_t *atm, *atm_filt;
34
35 FILE *out;
36
37 double latm, lonm, t, t0 = NAN, qm[NQ], *work, zm, *zs;
38
39 int init = 0;
40
41 /* Allocate... */
42 ALLOC(atm, atm_t, 1);
43 ALLOC(atm_filt, atm_t, 1);
44 ALLOC(work, double,
45 NP);
46 ALLOC(zs, double,
47 NP);
48
49 /* Check arguments... */
50 if (argc < 4)
51 ERRMSG("Give parameters: <ctl> <stat.tab> <param> <atm1> [<atm2> ...]");
52
53 /* Read control parameters... */
54 read_ctl(argv[1], argc, argv, &ctl);
55 int ens = (int) scan_ctl(argv[1], argc, argv, "STAT_ENS", -1, "-999", NULL);
56 double p0 = P(scan_ctl(argv[1], argc, argv, "STAT_Z0", -1, "-1000", NULL));
57 double p1 = P(scan_ctl(argv[1], argc, argv, "STAT_Z1", -1, "1000", NULL));
58 double lat0 = scan_ctl(argv[1], argc, argv, "STAT_LAT0", -1, "-1000", NULL);
59 double lat1 = scan_ctl(argv[1], argc, argv, "STAT_LAT1", -1, "1000", NULL);
60 double lon0 = scan_ctl(argv[1], argc, argv, "STAT_LON0", -1, "-1000", NULL);
61 double lon1 = scan_ctl(argv[1], argc, argv, "STAT_LON1", -1, "1000", NULL);
62
63 /* Write info... */
64 LOG(1, "Write air parcel statistics: %s", argv[2]);
65
66 /* Create output file... */
67 if (!(out = fopen(argv[2], "w")))
68 ERRMSG("Cannot create file!");
69
70 /* Write header... */
71 fprintf(out,
72 "# $1 = time [s]\n"
73 "# $2 = time difference [s]\n"
74 "# $3 = altitude (%s) [km]\n"
75 "# $4 = longitude (%s) [deg]\n"
76 "# $5 = latitude (%s) [deg]\n", argv[3], argv[3], argv[3]);
77 for (int iq = 0; iq < ctl.nq; iq++)
78 fprintf(out, "# $%d = %s (%s) [%s]\n", iq + 6,
79 ctl.qnt_name[iq], argv[3], ctl.qnt_unit[iq]);
80 fprintf(out, "# $%d = number of particles\n\n", ctl.nq + 6);
81
82 /* Loop over files... */
83 for (int f = 4; f < argc; f++) {
84
85 /* Read atmopheric data... */
86 if (!read_atm(argv[f], &ctl, atm))
87 continue;
88
89 /* Get time from filename... */
90 t = time_from_filename(argv[f], ctl.atm_type < 2 ? 20 : 19);
91
92 /* Save initial time... */
93 if (!init) {
94 init = 1;
95 t0 = t;
96 }
97
98 /* Filter data... */
99 atm_filt->np = 0;
100 for (int ip = 0; ip < atm->np; ip++) {
101
102 /* Check time... */
103 if (!isfinite(atm->time[ip]))
104 continue;
105
106 /* Check ensemble index... */
107 if (ctl.qnt_ens > 0 && atm->q[ctl.qnt_ens][ip] != ens)
108 continue;
109
110 /* Check spatial range... */
111 if (atm->p[ip] > p0 || atm->p[ip] < p1
112 || atm->lon[ip] < lon0 || atm->lon[ip] > lon1
113 || atm->lat[ip] < lat0 || atm->lat[ip] > lat1)
114 continue;
115
116 /* Save data... */
117 atm_filt->time[atm_filt->np] = atm->time[ip];
118 atm_filt->p[atm_filt->np] = atm->p[ip];
119 atm_filt->lon[atm_filt->np] = atm->lon[ip];
120 atm_filt->lat[atm_filt->np] = atm->lat[ip];
121 for (int iq = 0; iq < ctl.nq; iq++)
122 atm_filt->q[iq][atm_filt->np] = atm->q[iq][ip];
123 atm_filt->np++;
124 }
125
126 /* Get heights... */
127 for (int ip = 0; ip < atm_filt->np; ip++)
128 zs[ip] = Z(atm_filt->p[ip]);
129
130 /* Get statistics... */
131 if (strcasecmp(argv[3], "mean") == 0) {
132 zm = gsl_stats_mean(zs, 1, (size_t) atm_filt->np);
133 lonm = gsl_stats_mean(atm_filt->lon, 1, (size_t) atm_filt->np);
134 latm = gsl_stats_mean(atm_filt->lat, 1, (size_t) atm_filt->np);
135 for (int iq = 0; iq < ctl.nq; iq++)
136 qm[iq] = gsl_stats_mean(atm_filt->q[iq], 1, (size_t) atm_filt->np);
137 } else if (strcasecmp(argv[3], "stddev") == 0) {
138 zm = gsl_stats_sd(zs, 1, (size_t) atm_filt->np);
139 lonm = gsl_stats_sd(atm_filt->lon, 1, (size_t) atm_filt->np);
140 latm = gsl_stats_sd(atm_filt->lat, 1, (size_t) atm_filt->np);
141 for (int iq = 0; iq < ctl.nq; iq++)
142 qm[iq] = gsl_stats_sd(atm_filt->q[iq], 1, (size_t) atm_filt->np);
143 } else if (strcasecmp(argv[3], "min") == 0) {
144 zm = gsl_stats_min(zs, 1, (size_t) atm_filt->np);
145 lonm = gsl_stats_min(atm_filt->lon, 1, (size_t) atm_filt->np);
146 latm = gsl_stats_min(atm_filt->lat, 1, (size_t) atm_filt->np);
147 for (int iq = 0; iq < ctl.nq; iq++)
148 qm[iq] = gsl_stats_min(atm_filt->q[iq], 1, (size_t) atm_filt->np);
149 } else if (strcasecmp(argv[3], "max") == 0) {
150 zm = gsl_stats_max(zs, 1, (size_t) atm_filt->np);
151 lonm = gsl_stats_max(atm_filt->lon, 1, (size_t) atm_filt->np);
152 latm = gsl_stats_max(atm_filt->lat, 1, (size_t) atm_filt->np);
153 for (int iq = 0; iq < ctl.nq; iq++)
154 qm[iq] = gsl_stats_max(atm_filt->q[iq], 1, (size_t) atm_filt->np);
155 } else if (strcasecmp(argv[3], "skew") == 0) {
156 zm = gsl_stats_skew(zs, 1, (size_t) atm_filt->np);
157 lonm = gsl_stats_skew(atm_filt->lon, 1, (size_t) atm_filt->np);
158 latm = gsl_stats_skew(atm_filt->lat, 1, (size_t) atm_filt->np);
159 for (int iq = 0; iq < ctl.nq; iq++)
160 qm[iq] = gsl_stats_skew(atm_filt->q[iq], 1, (size_t) atm_filt->np);
161 } else if (strcasecmp(argv[3], "kurt") == 0) {
162 zm = gsl_stats_kurtosis(zs, 1, (size_t) atm_filt->np);
163 lonm = gsl_stats_kurtosis(atm_filt->lon, 1, (size_t) atm_filt->np);
164 latm = gsl_stats_kurtosis(atm_filt->lat, 1, (size_t) atm_filt->np);
165 for (int iq = 0; iq < ctl.nq; iq++)
166 qm[iq] =
167 gsl_stats_kurtosis(atm_filt->q[iq], 1, (size_t) atm_filt->np);
168 } else if (strcasecmp(argv[3], "median") == 0) {
169 zm = gsl_stats_median(zs, 1, (size_t) atm_filt->np);
170 lonm = gsl_stats_median(atm_filt->lon, 1, (size_t) atm_filt->np);
171 latm = gsl_stats_median(atm_filt->lat, 1, (size_t) atm_filt->np);
172 for (int iq = 0; iq < ctl.nq; iq++)
173 qm[iq] = gsl_stats_median(atm_filt->q[iq], 1, (size_t) atm_filt->np);
174 } else if (strcasecmp(argv[3], "absdev") == 0) {
175 zm = gsl_stats_absdev(zs, 1, (size_t) atm_filt->np);
176 lonm = gsl_stats_absdev(atm_filt->lon, 1, (size_t) atm_filt->np);
177 latm = gsl_stats_absdev(atm_filt->lat, 1, (size_t) atm_filt->np);
178 for (int iq = 0; iq < ctl.nq; iq++)
179 qm[iq] = gsl_stats_absdev(atm_filt->q[iq], 1, (size_t) atm_filt->np);
180 } else if (strcasecmp(argv[3], "mad") == 0) {
181 zm = gsl_stats_mad0(zs, 1, (size_t) atm_filt->np, work);
182 lonm = gsl_stats_mad0(atm_filt->lon, 1, (size_t) atm_filt->np, work);
183 latm = gsl_stats_mad0(atm_filt->lat, 1, (size_t) atm_filt->np, work);
184 for (int iq = 0; iq < ctl.nq; iq++)
185 qm[iq] =
186 gsl_stats_mad0(atm_filt->q[iq], 1, (size_t) atm_filt->np, work);
187 } else
188 ERRMSG("Unknown parameter!");
189
190 /* Write data... */
191 fprintf(out, "%.2f %.2f %g %g %g", t, t - t0, zm, lonm, latm);
192 for (int iq = 0; iq < ctl.nq; iq++) {
193 fprintf(out, " ");
194 fprintf(out, ctl.qnt_format[iq], qm[iq]);
195 }
196 fprintf(out, " %d\n", atm_filt->np);
197 }
198
199 /* Close file... */
200 fclose(out);
201
202 /* Free... */
203 free(atm);
204 free(atm_filt);
205 free(work);
206 free(zs);
207
208 return EXIT_SUCCESS;
209}
int main(int argc, char *argv[])
Definition: atm_stat.c:27
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:8098
double time_from_filename(const char *filename, const int offset)
Extracts and converts a timestamp from a filename to Julian seconds.
Definition: mptrac.c:8413
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:4805
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.
Definition: mptrac.c:4215
MPTRAC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1901
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1726
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1289
#define NQ
Maximum number of quantities per data point.
Definition: mptrac.h:246
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define NP
Maximum number of atmospheric data points.
Definition: mptrac.h:241
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1831
Air parcel data.
Definition: mptrac.h:3120
double time[NP]
Time [s].
Definition: mptrac.h:3126
double lat[NP]
Latitude [deg].
Definition: mptrac.h:3135
double lon[NP]
Longitude [deg].
Definition: mptrac.h:3132
int np
Number of air parcels.
Definition: mptrac.h:3123
double q[NQ][NP]
Quantity data (for various, user-defined attributes).
Definition: mptrac.h:3138
double p[NP]
Pressure [hPa].
Definition: mptrac.h:3129
Control parameters.
Definition: mptrac.h:2155
char qnt_format[NQ][LEN]
Quantity output format.
Definition: mptrac.h:2192
int atm_type
Type of atmospheric data files (0=ASCII, 1=binary, 2=netCDF, 3=CLaMS_traj, 4=CLaMS_pos).
Definition: mptrac.h:2905
char qnt_unit[NQ][LEN]
Quantity units.
Definition: mptrac.h:2189
char qnt_name[NQ][LEN]
Quantity names.
Definition: mptrac.h:2183
int qnt_ens
Quantity array index for ensemble IDs.
Definition: mptrac.h:2198
int nq
Number of quantities.
Definition: mptrac.h:2180