41 {
42
44
46
47
49
50
51 if (argc < 4)
52 ERRMSG(
"Missing or invalid command-line arguments.\n\n"
53 "Usage: atm_select <ctl> <atm_select> <atm1> [<atm2> ...]\n\n"
54 "Use -h for full help.");
55
56
59
60
62 const int stride =
63 (int)
scan_ctl(argv[1], argc, argv,
"SELECT_STRIDE", -1,
"1", NULL);
64 const int idx0 =
65 (int)
scan_ctl(argv[1], argc, argv,
"SELECT_IDX0", -1,
"-999", NULL);
66 const int idx1 =
67 (int)
scan_ctl(argv[1], argc, argv,
"SELECT_IDX1", -1,
"-999", NULL);
68 int ip0 =
69 (int)
scan_ctl(argv[1], argc, argv,
"SELECT_IP0", -1,
"-999", NULL);
70 int ip1 =
71 (int)
scan_ctl(argv[1], argc, argv,
"SELECT_IP1", -1,
"-999", NULL);
72 const double t0 =
scan_ctl(argv[1], argc, argv,
"SELECT_T0", -1,
"0", NULL);
73 const double t1 =
scan_ctl(argv[1], argc, argv,
"SELECT_T1", -1,
"0", NULL);
74 const double p0 =
75 P(
scan_ctl(argv[1], argc, argv,
"SELECT_Z0", -1,
"0", NULL));
76 const double p1 =
77 P(
scan_ctl(argv[1], argc, argv,
"SELECT_Z1", -1,
"0", NULL));
78 const double theta0 =
79 scan_ctl(argv[1], argc, argv,
"SELECT_THETA0", -1,
"0", NULL);
80 const double theta1 =
81 scan_ctl(argv[1], argc, argv,
"SELECT_THETA1", -1,
"0", NULL);
82 const double lon0 =
83 scan_ctl(argv[1], argc, argv,
"SELECT_LON0", -1,
"0", NULL);
84 const double lon1 =
85 scan_ctl(argv[1], argc, argv,
"SELECT_LON1", -1,
"0", NULL);
86 const double lat0 =
87 scan_ctl(argv[1], argc, argv,
"SELECT_LAT0", -1,
"0", NULL);
88 const double lat1 =
89 scan_ctl(argv[1], argc, argv,
"SELECT_LAT1", -1,
"0", NULL);
90 const double r0 =
scan_ctl(argv[1], argc, argv,
"SELECT_R0", -1,
"0", NULL);
91 const double r1 =
scan_ctl(argv[1], argc, argv,
"SELECT_R1", -1,
"0", NULL);
92 const double rlon =
93 scan_ctl(argv[1], argc, argv,
"SELECT_RLON", -1,
"0", NULL);
94 const double rlat =
95 scan_ctl(argv[1], argc, argv,
"SELECT_RLAT", -1,
"0", NULL);
96
97
99 ERRMSG(
"atm_select currently supports only lat/lon grids");
100
101
102 double x0[3], x1[3];
104
105
106 for (int f = 3; f < argc; f++) {
107
108
110 continue;
111
112
113 if (ip0 < 0)
114 ip0 = 0;
115 ip0 =
MIN(ip0, atm->
np - 1);
116 if (ip1 < 0)
118 ip1 =
MIN(ip1, atm->
np - 1);
119 if (ip1 < ip0)
120 ip1 = ip0;
121
122
123 for (int ip = ip0; ip <= ip1; ip += stride) {
124
125
126 if (ctl.
qnt_idx >= 0 && idx0 >= 0 && idx1 >= 0)
128 continue;
129
130
131 if (t0 != t1)
132 if ((t1 > t0 && (atm->
time[ip] < t0 || atm->
time[ip] > t1))
133 || (t1 < t0 && (atm->
time[ip] < t0 && atm->
time[ip] > t1)))
134 continue;
135
136
137 if (p0 != p1)
138 if ((p0 > p1 && (atm->
p[ip] > p0 || atm->
p[ip] < p1))
139 || (p0 < p1 && (atm->
p[ip] > p0 && atm->
p[ip] < p1)))
140 continue;
141
142
143 if (theta0 != theta1) {
144 double theta;
147 else if (ctl.
qnt_t >= 0)
149 else
151 ("Filtering requires temperature or potential temperature data!");
152 if ((theta1 > theta0 && (theta < theta0 || theta > theta1))
153 || (theta1 < theta0 && (theta < theta0 && theta > theta1)))
154 continue;
155 }
156
157
158 if (lon0 != lon1)
159 if ((lon1 > lon0 && (atm->
lon[ip] < lon0 || atm->
lon[ip] > lon1))
160 || (lon1 < lon0 && (atm->
lon[ip] < lon0 && atm->
lon[ip] > lon1)))
161 continue;
162
163
164 if (lat0 != lat1)
165 if ((lat1 > lat0 && (atm->
lat[ip] < lat0 || atm->
lat[ip] > lat1))
166 || (lat1 < lat0 && (atm->
lat[ip] < lat0 && atm->
lat[ip] > lat1)))
167 continue;
168
169
170 if (r0 != r1) {
172 double r =
DIST(x0, x1);
173 if ((r1 > r0 && (r < r0 || r > r1))
174 || (r1 < r0 && (r < r0 && r > r1)))
175 continue;
176 }
177
178
180 atm2->
p[atm2->
np] = atm->
p[ip];
183 for (
int iq = 0; iq < ctl.
nq; iq++)
184 atm2->
q[iq][atm2->
np] = atm->
q[iq][ip];
185 if ((++atm2->
np) >
NP)
186 ERRMSG(
"Too many air parcels!");
187 }
188 }
189
190
192
193
194 free(atm);
195 free(atm2);
196
197 return EXIT_SUCCESS;
198}
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.
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.
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.
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.
#define MIN(a, b)
Macro to determine the minimum of two values.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define USAGE
Print usage information on -h or --help.
#define P(z)
Compute pressure at given altitude.
#define THETA(p, t)
Compute potential temperature.
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
#define NP
Maximum number of atmospheric data points.
#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].
int qnt_theta
Quantity array index for potential temperature.
int qnt_t
Quantity array index for temperature.
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.
int nq
Number of quantities.