46 {
47
49
51
53
54 FILE *out;
55
56 static double p2[1000], t[1000], t2[1000], z[1000], z2[1000], lat_mean,
57 z_mean;
58
59 static int hist_max[1000], hist_min[1000], hist_mean[1000], hist_sig[1000],
60 nhist_max, nhist_min, nhist_mean, nhist_sig, np;
61
62
65
66
67 if (argc < 4)
68 ERRMSG(
"Give parameters: <ctl> <lapse.tab> <met0> [ <met1> ... ]");
69
70
72 int dz = (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_DZ", -1,
"20", NULL);
73 double lat0 =
74 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_LAT0", -1,
"-90", NULL);
75 double lat1 =
76 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_LAT1", -1,
"90", NULL);
77 double z0 = (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_Z0", -1,
"0", NULL);
78 double z1 =
79 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_Z1", -1,
"100", NULL);
80 int intpol =
81 (int)
scan_ctl(argv[1], argc, argv,
"LAPSE_INTPOL", -1,
"1", NULL);
82
83
85
86
87 for (int i = 3; i < argc; i++) {
88
89
90 if (!
read_met(argv[i], &ctl, clim, met))
91 continue;
92
93
94 for (
int iz = 0; iz < met->
np; iz++)
95 z[iz] =
Z(met->
p[iz]);
96 for (int iz = 0; iz <= 250; iz++) {
97 z2[iz] = 0.0 + 0.1 * iz;
99 }
100
101
102 for (
int ix = 0; ix < met->
nx; ix++)
103 for (
int iy = 0; iy < met->
ny; iy++) {
104
105
106 if (met->
lat[iy] < lat0 || met->
lat[iy] > lat1)
107 continue;
108
109
110 for (
int iz = 0; iz < met->
np; iz++)
111 t[iz] = met->
t[ix][iy][iz];
112 if (intpol == 1)
114 else
115 for (int iz = 0; iz <= 250; iz++) {
117 t2[iz] =
LIN(z[idx], t[idx], z[idx + 1], t[idx + 1], z2[iz]);
118 }
119
120
121 for (int iz = 0; iz <= 250; iz++) {
122
123
124 if (z2[iz] < z0 || z2[iz] > z1)
125 continue;
126
127
128 if (p2[iz] > met->
ps[ix][iy])
129 continue;
130
131
132 lat_mean += met->
lat[iy];
133 z_mean += z2[iz];
134 np++;
135
136
137 int nlapse = 0;
138 double lapse_max = -1e99, lapse_min = 1e99, lapse_mean =
139 0, lapse_sig = 0;
140 for (int iz2 = iz + 1; iz2 <= iz + dz; iz2++) {
141 lapse_max =
142 MAX(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_max);
143 lapse_min =
144 MIN(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_min);
145 lapse_mean +=
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]);
146 lapse_sig +=
SQR(
LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]));
147 nlapse++;
148 }
149 lapse_mean /= nlapse;
150 lapse_sig = sqrt(
MAX(lapse_sig / nlapse -
SQR(lapse_mean), 0));
151
152
154 if (idx >= 0 && idx <
IDXMAX) {
155 hist_max[idx]++;
156 nhist_max++;
157 }
158
160 if (idx >= 0 && idx <
IDXMAX) {
161 hist_min[idx]++;
162 nhist_min++;
163 }
164
166 if (idx >= 0 && idx <
IDXMAX) {
167 hist_mean[idx]++;
168 nhist_mean++;
169 }
170
172 if (idx >= 0 && idx <
IDXMAX) {
173 hist_sig[idx]++;
174 nhist_sig++;
175 }
176 }
177 }
178 }
179
180
181 LOG(1,
"Write lapse rate data: %s", argv[2]);
182 if (!(out = fopen(argv[2], "w")))
183 ERRMSG(
"Cannot create file!");
184
185
186 fprintf(out,
187 "# $1 = mean altitude [km]\n"
188 "# $2 = mean latitude [deg]\n"
189 "# $3 = lapse rate [K/km]\n"
190 "# $4 = counts of maxima per bin\n"
191 "# $5 = total number of maxima\n"
192 "# $6 = normalized frequency of maxima\n"
193 "# $7 = counts of minima per bin\n"
194 "# $8 = total number of minima\n"
195 "# $9 = normalized frequency of minima\n"
196 "# $10 = counts of means per bin\n"
197 "# $11 = total number of means\n"
198 "# $12 = normalized frequency of means\n"
199 "# $13 = counts of sigmas per bin\n"
200 "# $14 = total number of sigmas\n"
201 "# $15 = normalized frequency of sigmas\n\n");
202
203
204 double nmax_max = 0, nmax_min = 0, nmax_mean = 0, nmax_sig = 0;
205 for (
int idx = 0; idx <
IDXMAX; idx++) {
206 nmax_max =
MAX(hist_max[idx], nmax_max);
207 nmax_min =
MAX(hist_min[idx], nmax_min);
208 nmax_mean =
MAX(hist_mean[idx], nmax_mean);
209 nmax_sig =
MAX(hist_sig[idx], nmax_sig);
210 }
211 for (
int idx = 0; idx <
IDXMAX; idx++)
212 fprintf(out,
213 "%g %g %g %d %d %g %d %d %g %d %d %g %d %d %g\n",
215 hist_max[idx], nhist_max,
216 (double) hist_max[idx] / (double) nmax_max, hist_min[idx],
217 nhist_min, (double) hist_min[idx] / (double) nmax_min,
218 hist_mean[idx], nhist_mean,
219 (double) hist_mean[idx] / (double) nmax_mean, hist_sig[idx],
220 nhist_sig, (double) hist_sig[idx] / (double) nmax_sig);
221
222
223 fclose(out);
224
225
226 free(clim);
227 free(met);
228
229 return EXIT_SUCCESS;
230}
#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 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_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology 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 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).
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]
Latitude [deg].
double p[EP]
Pressure levels [hPa].