54 {
55
57
59
61
63
64 FILE *out;
65
66 static double p2[1000], t[1000], t2[1000], z[1000], z2[1000], lat_mean,
67 z_mean;
68
69 static int hist_max[1000], hist_min[1000], hist_mean[1000], hist_sig[1000],
70 nhist_max, nhist_min, nhist_mean, nhist_sig, np;
71
72
76
77
79
80
81 if (argc < 4)
82 ERRMSG(
"Missing or invalid command-line arguments.\n\n"
83 "Usage: met_lapse <ctl> <lapse.tab> <met0> [<met1> ...]\n\n"
84 "Use -h for full help.");
85
86
88 const int dz =
89 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_DZ", -1,
"20", NULL);
90 const double lat0 =
91 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_LAT0", -1,
"-90", NULL);
92 const double lat1 =
93 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_LAT1", -1,
"90", NULL);
94 const double z0 =
95 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_Z0", -1,
"0", NULL);
96 const double z1 =
97 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_Z1", -1,
"100", NULL);
98 const int intpol =
99 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_INTPOL", -1,
"1", NULL);
100
101
103
104
105 for (int i = 3; i < argc; i++) {
106
107
109 continue;
110
111
112 for (
int iz = 0; iz < met->
np; iz++)
113 z[iz] =
Z(met->
p[iz]);
114 for (int iz = 0; iz <= 250; iz++) {
115 z2[iz] = 0.0 + 0.1 * iz;
117 }
118
119
120 for (
int ix = 0; ix < met->
nx; ix++)
121 for (
int iy = 0; iy < met->
ny; iy++) {
122
123
124 if (met->
lat[iy] < lat0 || met->
lat[iy] > lat1)
125 continue;
126
127
128 for (
int iz = 0; iz < met->
np; iz++)
129 t[iz] = met->
t[ix][iy][iz];
130 if (intpol == 1)
132 else
133 for (int iz = 0; iz <= 250; iz++) {
135 t2[iz] =
LIN(z[idx], t[idx], z[idx + 1], t[idx + 1], z2[iz]);
136 }
137
138
139 for (int iz = 0; iz <= 250; iz++) {
140
141
142 if (z2[iz] < z0 || z2[iz] > z1)
143 continue;
144
145
146 if (p2[iz] > met->
ps[ix][iy])
147 continue;
148
149
150 lat_mean += met->
lat[iy];
151 z_mean += z2[iz];
152 np++;
153
154
155 int nlapse = 0;
156 double lapse_max = -1e99, lapse_min = 1e99, lapse_mean =
157 0, lapse_sig = 0;
158 for (int iz2 = iz + 1; iz2 <= iz + dz; iz2++) {
159 lapse_max =
160 MAX(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_max);
161 lapse_min =
162 MIN(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_min);
163 lapse_mean +=
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]);
164 lapse_sig +=
SQR(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]));
165 nlapse++;
166 }
167 lapse_mean /= nlapse;
168 lapse_sig = sqrt(
MAX(lapse_sig / nlapse -
SQR(lapse_mean), 0));
169
170
172 if (idx >= 0 && idx <
IDXMAX) {
173 hist_max[idx]++;
174 nhist_max++;
175 }
176
178 if (idx >= 0 && idx <
IDXMAX) {
179 hist_min[idx]++;
180 nhist_min++;
181 }
182
184 if (idx >= 0 && idx <
IDXMAX) {
185 hist_mean[idx]++;
186 nhist_mean++;
187 }
188
190 if (idx >= 0 && idx <
IDXMAX) {
191 hist_sig[idx]++;
192 nhist_sig++;
193 }
194 }
195 }
196 }
197
198
199 LOG(1,
"Write lapse rate data: %s", argv[2]);
200 if (!(out = fopen(argv[2], "w")))
201 ERRMSG(
"Cannot create file!");
202
203
204 fprintf(out,
205 "# $1 = mean altitude [km]\n"
206 "# $2 = mean latitude [deg]\n"
207 "# $3 = lapse rate [K/km]\n"
208 "# $4 = counts of maxima per bin\n"
209 "# $5 = total number of maxima\n"
210 "# $6 = normalized frequency of maxima\n"
211 "# $7 = counts of minima per bin\n"
212 "# $8 = total number of minima\n"
213 "# $9 = normalized frequency of minima\n"
214 "# $10 = counts of means per bin\n"
215 "# $11 = total number of means\n"
216 "# $12 = normalized frequency of means\n"
217 "# $13 = counts of sigmas per bin\n"
218 "# $14 = total number of sigmas\n"
219 "# $15 = normalized frequency of sigmas\n\n");
220
221
222 double nmax_max = 0, nmax_min = 0, nmax_mean = 0, nmax_sig = 0;
223 for (
int idx = 0; idx <
IDXMAX; idx++) {
224 nmax_max =
MAX(hist_max[idx], nmax_max);
225 nmax_min =
MAX(hist_min[idx], nmax_min);
226 nmax_mean =
MAX(hist_mean[idx], nmax_mean);
227 nmax_sig =
MAX(hist_sig[idx], nmax_sig);
228 }
229 for (
int idx = 0; idx <
IDXMAX; idx++)
230 fprintf(out,
231 "%g %g %g %d %d %g %d %d %g %d %d %g %d %d %g\n",
233 hist_max[idx], nhist_max,
234 (double) hist_max[idx] / (double) nmax_max, hist_min[idx],
235 nhist_min, (double) hist_min[idx] / (double) nmax_min,
236 hist_mean[idx], nhist_mean,
237 (double) hist_mean[idx] / (double) nmax_mean, hist_sig[idx],
238 nhist_sig, (double) hist_sig[idx] / (double) nmax_sig);
239
240
241 fclose(out);
242
243
244 free(clim);
245 free(met);
246 free(dd);
247
248 return EXIT_SUCCESS;
249}
#define LAPSEMIN
Lapse rate minimum [K/km].
#define IDXMAX
Maximum number of histogram bins.
#define DLAPSE
Lapse rate bin size [K/km].
int locate_irr(const double *xx, const int n, const double x)
Locate the index of the interval containing a given value in a sorted array.
void spline(const double *x, const double *y, const int n, const double *x2, double *y2, const int n2, const int method)
Performs spline interpolation or linear interpolation.
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_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
int mptrac_read_met(const char *filename, const ctl_t *ctl, const clim_t *clim, met_t *met, dd_t *dd)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
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.
#define LAPSE(p1, t1, p2, t2)
Calculate lapse rate.
#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 Z(p)
Convert pressure to altitude.
#define P(z)
Compute pressure at given altitude.
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
#define SQR(x)
Compute the square of a value.
#define LOG(level,...)
Print a log message with a specified logging level.
#define LIN(x0, y0, x1, y1, x)
Linear interpolation.
#define MAX(a, b)
Macro to determine the maximum of two values.
int met_tropo_spline
Tropopause interpolation method (0=linear, 1=spline).
Domain decomposition data structure.
int nx
Number of longitudes.
int ny
Number of latitudes.
float ps[EX][EY]
Surface pressure [hPa].
int np
Number of pressure levels.
float t[EX][EY][EP]
Temperature [K].
double lat[EY]
Latitudes [deg].
double p[EP]
Pressure levels [hPa].