MPTRAC
met_lapse.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-2025 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27/* ------------------------------------------------------------
28 Dimensions...
29 ------------------------------------------------------------ */
30
32#define LAPSEMIN -20.0
33
35#define DLAPSE 0.1
36
38#define IDXMAX 400
39
40/* ------------------------------------------------------------
41 Main...
42 ------------------------------------------------------------ */
43
44int main(
45 int argc,
46 char *argv[]) {
47
48 ctl_t ctl;
49
50 clim_t *clim;
51
52 met_t *met;
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 /* Allocate... */
63 ALLOC(clim, clim_t, 1);
64 ALLOC(met, met_t, 1);
65
66 /* Check arguments... */
67 if (argc < 4)
68 ERRMSG("Give parameters: <ctl> <lapse.tab> <met0> [ <met1> ... ]");
69
70 /* Read control parameters... */
71 read_ctl(argv[1], argc, argv, &ctl);
72 const int dz =
73 (int) scan_ctl(argv[1], argc, argv, "LAPSE_DZ", -1, "20", NULL);
74 const double lat0 =
75 (int) scan_ctl(argv[1], argc, argv, "LAPSE_LAT0", -1, "-90", NULL);
76 const double lat1 =
77 (int) scan_ctl(argv[1], argc, argv, "LAPSE_LAT1", -1, "90", NULL);
78 const double z0 =
79 (int) scan_ctl(argv[1], argc, argv, "LAPSE_Z0", -1, "0", NULL);
80 const double z1 =
81 (int) scan_ctl(argv[1], argc, argv, "LAPSE_Z1", -1, "100", NULL);
82 const int intpol =
83 (int) scan_ctl(argv[1], argc, argv, "LAPSE_INTPOL", -1, "1", NULL);
84
85 /* Read climatological data... */
86 read_clim(&ctl, clim);
87
88 /* Loop over files... */
89 for (int i = 3; i < argc; i++) {
90
91 /* Read meteorological data... */
92 if (!read_met(argv[i], &ctl, clim, met))
93 continue;
94
95 /* Get altitude and pressure profiles... */
96 for (int iz = 0; iz < met->np; iz++)
97 z[iz] = Z(met->p[iz]);
98 for (int iz = 0; iz <= 250; iz++) {
99 z2[iz] = 0.0 + 0.1 * iz;
100 p2[iz] = P(z2[iz]);
101 }
102
103 /* Loop over grid points... */
104 for (int ix = 0; ix < met->nx; ix++)
105 for (int iy = 0; iy < met->ny; iy++) {
106
107 /* Check latitude range... */
108 if (met->lat[iy] < lat0 || met->lat[iy] > lat1)
109 continue;
110
111 /* Interpolate temperature profile... */
112 for (int iz = 0; iz < met->np; iz++)
113 t[iz] = met->t[ix][iy][iz];
114 if (intpol == 1)
115 spline(z, t, met->np, z2, t2, 251, ctl.met_tropo_spline);
116 else
117 for (int iz = 0; iz <= 250; iz++) {
118 int idx = locate_irr(z, met->np, z2[iz]);
119 t2[iz] = LIN(z[idx], t[idx], z[idx + 1], t[idx + 1], z2[iz]);
120 }
121
122 /* Loop over vertical levels... */
123 for (int iz = 0; iz <= 250; iz++) {
124
125 /* Check height range... */
126 if (z2[iz] < z0 || z2[iz] > z1)
127 continue;
128
129 /* Check surface pressure... */
130 if (p2[iz] > met->ps[ix][iy])
131 continue;
132
133 /* Get mean latitude and height... */
134 lat_mean += met->lat[iy];
135 z_mean += z2[iz];
136 np++;
137
138 /* Get lapse rates within a vertical layer... */
139 int nlapse = 0;
140 double lapse_max = -1e99, lapse_min = 1e99, lapse_mean =
141 0, lapse_sig = 0;
142 for (int iz2 = iz + 1; iz2 <= iz + dz; iz2++) {
143 lapse_max =
144 MAX(LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_max);
145 lapse_min =
146 MIN(LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]), lapse_min);
147 lapse_mean += LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]);
148 lapse_sig += SQR(LAPSE(p2[iz], t2[iz], p2[iz2], t2[iz2]));
149 nlapse++;
150 }
151 lapse_mean /= nlapse;
152 lapse_sig = sqrt(MAX(lapse_sig / nlapse - SQR(lapse_mean), 0));
153
154 /* Get histograms... */
155 int idx = (int) ((lapse_max - LAPSEMIN) / DLAPSE);
156 if (idx >= 0 && idx < IDXMAX) {
157 hist_max[idx]++;
158 nhist_max++;
159 }
160
161 idx = (int) ((lapse_min - LAPSEMIN) / DLAPSE);
162 if (idx >= 0 && idx < IDXMAX) {
163 hist_min[idx]++;
164 nhist_min++;
165 }
166
167 idx = (int) ((lapse_mean - LAPSEMIN) / DLAPSE);
168 if (idx >= 0 && idx < IDXMAX) {
169 hist_mean[idx]++;
170 nhist_mean++;
171 }
172
173 idx = (int) ((lapse_sig - LAPSEMIN) / DLAPSE);
174 if (idx >= 0 && idx < IDXMAX) {
175 hist_sig[idx]++;
176 nhist_sig++;
177 }
178 }
179 }
180 }
181
182 /* Create output file... */
183 LOG(1, "Write lapse rate data: %s", argv[2]);
184 if (!(out = fopen(argv[2], "w")))
185 ERRMSG("Cannot create file!");
186
187 /* Write header... */
188 fprintf(out,
189 "# $1 = mean altitude [km]\n"
190 "# $2 = mean latitude [deg]\n"
191 "# $3 = lapse rate [K/km]\n"
192 "# $4 = counts of maxima per bin\n"
193 "# $5 = total number of maxima\n"
194 "# $6 = normalized frequency of maxima\n"
195 "# $7 = counts of minima per bin\n"
196 "# $8 = total number of minima\n"
197 "# $9 = normalized frequency of minima\n"
198 "# $10 = counts of means per bin\n"
199 "# $11 = total number of means\n"
200 "# $12 = normalized frequency of means\n"
201 "# $13 = counts of sigmas per bin\n"
202 "# $14 = total number of sigmas\n"
203 "# $15 = normalized frequency of sigmas\n\n");
204
205 /* Write data... */
206 double nmax_max = 0, nmax_min = 0, nmax_mean = 0, nmax_sig = 0;
207 for (int idx = 0; idx < IDXMAX; idx++) {
208 nmax_max = MAX(hist_max[idx], nmax_max);
209 nmax_min = MAX(hist_min[idx], nmax_min);
210 nmax_mean = MAX(hist_mean[idx], nmax_mean);
211 nmax_sig = MAX(hist_sig[idx], nmax_sig);
212 }
213 for (int idx = 0; idx < IDXMAX; idx++)
214 fprintf(out,
215 "%g %g %g %d %d %g %d %d %g %d %d %g %d %d %g\n",
216 z_mean / np, lat_mean / np, (idx + .5) * DLAPSE + LAPSEMIN,
217 hist_max[idx], nhist_max,
218 (double) hist_max[idx] / (double) nmax_max, hist_min[idx],
219 nhist_min, (double) hist_min[idx] / (double) nmax_min,
220 hist_mean[idx], nhist_mean,
221 (double) hist_mean[idx] / (double) nmax_mean, hist_sig[idx],
222 nhist_sig, (double) hist_sig[idx] / (double) nmax_sig);
223
224 /* Close file... */
225 fclose(out);
226
227 /* Free... */
228 free(clim);
229 free(met);
230
231 return EXIT_SUCCESS;
232}
int main(int argc, char *argv[])
Definition: met_lapse.c:44
#define LAPSEMIN
Lapse rate minimum [K/km.
Definition: met_lapse.c:32
#define IDXMAX
Maximum number of histogram bins.
Definition: met_lapse.c:38
#define DLAPSE
Lapse rate bin size [K/km].
Definition: met_lapse.c:35
int read_met(const char *filename, const ctl_t *ctl, const clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:6017
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.
Definition: mptrac.c:2114
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.
Definition: mptrac.c:8608
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:8503
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:5156
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4824
MPTRAC library declarations.
#define LAPSE(p1, t1, p2, t2)
Calculate lapse rate.
Definition: mptrac.h:840
#define MIN(a, b)
Macro to determine the minimum of two values.
Definition: mptrac.h:987
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1916
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1741
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1304
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:349
#define SQR(x)
Compute the square of a value.
Definition: mptrac.h:1557
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1846
#define LIN(x0, y0, x1, y1, x)
Linear interpolation.
Definition: mptrac.h:859
#define MAX(a, b)
Macro to determine the maximum of two values.
Definition: mptrac.h:886
Climatological data.
Definition: mptrac.h:3315
Control parameters.
Definition: mptrac.h:2170
int met_tropo_spline
Tropopause interpolation method (0=linear, 1=spline).
Definition: mptrac.h:2625
Meteo data structure.
Definition: mptrac.h:3374
int nx
Number of longitudes.
Definition: mptrac.h:3380
int ny
Number of latitudes.
Definition: mptrac.h:3383
float ps[EX][EY]
Surface pressure [hPa].
Definition: mptrac.h:3404
int np
Number of pressure levels.
Definition: mptrac.h:3386
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3479
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3395
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3398