MPTRAC
Loading...
Searching...
No Matches
met_subgrid.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-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27/* ------------------------------------------------------------
28 Functions...
29 ------------------------------------------------------------ */
30
32void usage(
33 void);
34
35/* ------------------------------------------------------------
36 Main...
37 ------------------------------------------------------------ */
38
39int main(
40 int argc,
41 char *argv[]) {
42
43 ctl_t ctl;
44
45 clim_t *clim;
46
47 met_t *met0, *met1;
48
49 dd_t *dd;
50
51 FILE *out;
52
53 static double usig[EP][EY], vsig[EP][EY], wsig[EP][EY];
54
55 static float u[16], v[16], w[16];
56
57 static int n[EP][EY];
58
59 /* Allocate... */
60 mptrac_alloc(NULL, NULL, &clim, &met0, &met1, NULL, NULL, &dd);
61
62 /* Print usage information... */
63 USAGE;
64
65 /* Check arguments... */
66 if (argc < 4 && argc % 2 != 0)
67 ERRMSG("Missing or invalid command-line arguments.\n\n"
68 "Usage: met_subgrid <ctl> <subgrid.tab> <met0> <met1> [<met0b> <met1b> ...]\n\n"
69 "Use -h for full help.");
70
71 /* Read control parameters... */
72 mptrac_read_ctl(argv[1], argc, argv, &ctl);
73
74 /* Read climatological data... */
75 mptrac_read_clim(&ctl, clim);
76
77 /* Loop over data files... */
78 for (int i = 3; i < argc - 1; i += 2) {
79
80 /* Read meteorological data... */
81 if (!mptrac_read_met(argv[i], &ctl, clim, met0, dd))
82 ERRMSG("Cannot open file!");
83 if (!mptrac_read_met(argv[i + 1], &ctl, clim, met1, dd))
84 ERRMSG("Cannot open file!");
85
86 /* Loop over grid boxes... */
87 for (int ix = 0; ix < met0->nx - 1; ix++)
88 for (int iy = 0; iy < met0->ny - 1; iy++)
89 for (int iz = 0; iz < met0->np - 1; iz++) {
90
91 /* Skip cells below the surface without changing earlier statistics... */
92 if (met0->p[iz] > met0->ps[ix][iy]
93 || met1->p[iz] > met1->ps[ix][iy])
94 continue;
95
96 /* Collect wind values from the first meteorological time... */
97 u[0] = met0->u[ix][iy][iz];
98 u[1] = met0->u[ix + 1][iy][iz];
99 u[2] = met0->u[ix][iy + 1][iz];
100 u[3] = met0->u[ix + 1][iy + 1][iz];
101 u[4] = met0->u[ix][iy][iz + 1];
102 u[5] = met0->u[ix + 1][iy][iz + 1];
103 u[6] = met0->u[ix][iy + 1][iz + 1];
104 u[7] = met0->u[ix + 1][iy + 1][iz + 1];
105
106 v[0] = met0->v[ix][iy][iz];
107 v[1] = met0->v[ix + 1][iy][iz];
108 v[2] = met0->v[ix][iy + 1][iz];
109 v[3] = met0->v[ix + 1][iy + 1][iz];
110 v[4] = met0->v[ix][iy][iz + 1];
111 v[5] = met0->v[ix + 1][iy][iz + 1];
112 v[6] = met0->v[ix][iy + 1][iz + 1];
113 v[7] = met0->v[ix + 1][iy + 1][iz + 1];
114
115 w[0] = (float) (1e3 * DP2DZ(met0->w[ix][iy][iz], met0->p[iz]));
116 w[1] = (float) (1e3 * DP2DZ(met0->w[ix + 1][iy][iz], met0->p[iz]));
117 w[2] = (float) (1e3 * DP2DZ(met0->w[ix][iy + 1][iz], met0->p[iz]));
118 w[3] =
119 (float) (1e3 * DP2DZ(met0->w[ix + 1][iy + 1][iz], met0->p[iz]));
120 w[4] =
121 (float) (1e3 * DP2DZ(met0->w[ix][iy][iz + 1], met0->p[iz + 1]));
122 w[5] =
123 (float) (1e3 *
124 DP2DZ(met0->w[ix + 1][iy][iz + 1], met0->p[iz + 1]));
125 w[6] =
126 (float) (1e3 *
127 DP2DZ(met0->w[ix][iy + 1][iz + 1], met0->p[iz + 1]));
128 w[7] =
129 (float) (1e3 *
130 DP2DZ(met0->w[ix + 1][iy + 1][iz + 1], met0->p[iz + 1]));
131
132 /* Add the same neighboring values from the second time... */
133 u[8] = met1->u[ix][iy][iz];
134 u[9] = met1->u[ix + 1][iy][iz];
135 u[10] = met1->u[ix][iy + 1][iz];
136 u[11] = met1->u[ix + 1][iy + 1][iz];
137 u[12] = met1->u[ix][iy][iz + 1];
138 u[13] = met1->u[ix + 1][iy][iz + 1];
139 u[14] = met1->u[ix][iy + 1][iz + 1];
140 u[15] = met1->u[ix + 1][iy + 1][iz + 1];
141
142 v[8] = met1->v[ix][iy][iz];
143 v[9] = met1->v[ix + 1][iy][iz];
144 v[10] = met1->v[ix][iy + 1][iz];
145 v[11] = met1->v[ix + 1][iy + 1][iz];
146 v[12] = met1->v[ix][iy][iz + 1];
147 v[13] = met1->v[ix + 1][iy][iz + 1];
148 v[14] = met1->v[ix][iy + 1][iz + 1];
149 v[15] = met1->v[ix + 1][iy + 1][iz + 1];
150
151 w[8] = (float) (1e3 * DP2DZ(met1->w[ix][iy][iz], met1->p[iz]));
152 w[9] = (float) (1e3 * DP2DZ(met1->w[ix + 1][iy][iz], met1->p[iz]));
153 w[10] = (float) (1e3 * DP2DZ(met1->w[ix][iy + 1][iz], met1->p[iz]));
154 w[11] =
155 (float) (1e3 * DP2DZ(met1->w[ix + 1][iy + 1][iz], met1->p[iz]));
156 w[12] =
157 (float) (1e3 * DP2DZ(met1->w[ix][iy][iz + 1], met1->p[iz + 1]));
158 w[13] =
159 (float) (1e3 *
160 DP2DZ(met1->w[ix + 1][iy][iz + 1], met1->p[iz + 1]));
161 w[14] =
162 (float) (1e3 *
163 DP2DZ(met1->w[ix][iy + 1][iz + 1], met1->p[iz + 1]));
164 w[15] =
165 (float) (1e3 *
166 DP2DZ(met1->w[ix + 1][iy + 1][iz + 1], met1->p[iz + 1]));
167
168 /* Get standard deviations of local wind data... */
169 usig[iz][iy] += stddev(u, 16);
170 vsig[iz][iy] += stddev(v, 16);
171 wsig[iz][iy] += stddev(w, 16);
172 n[iz][iy]++;
173 }
174 }
175
176 /* Create output file... */
177 LOG(1, "Write subgrid data file: %s", argv[2]);
178 if (!(out = fopen(argv[2], "w")))
179 ERRMSG("Cannot create file!");
180
181 /* Write header... */
182 fprintf(out,
183 "# $1 = time [s]\n"
184 "# $2 = altitude [km]\n"
185 "# $3 = longitude [deg]\n"
186 "# $4 = latitude [deg]\n"
187 "# $5 = zonal wind standard deviation [m/s]\n"
188 "# $6 = meridional wind standard deviation [m/s]\n"
189 "# $7 = vertical velocity standard deviation [m/s]\n"
190 "# $8 = number of data points\n");
191
192 /* Write output... */
193 for (int iy = 0; iy < met0->ny - 1; iy++) {
194 fprintf(out, "\n");
195 for (int iz = 0; iz < met0->np - 1; iz++)
196 fprintf(out, "%.2f %g %g %g %g %g %g %d\n",
197 0.5 * (met0->time + met1->time),
198 0.5 * (Z(met0->p[iz]) + Z(met1->p[iz + 1])),
199 0.0, 0.5 * (met0->lat[iy] + met1->lat[iy + 1]),
200 n[iz][iy] > 0 ? usig[iz][iy] / n[iz][iy] : NAN,
201 n[iz][iy] > 0 ? vsig[iz][iy] / n[iz][iy] : NAN,
202 n[iz][iy] > 0 ? wsig[iz][iy] / n[iz][iy] : NAN, n[iz][iy]);
203 }
204
205 /* Close file... */
206 fclose(out);
207
208 /* Free... */
209 mptrac_free(NULL, NULL, clim, met0, met1, NULL, NULL, dd);
210
211 return EXIT_SUCCESS;
212}
213
214/*****************************************************************************/
215
217void usage(
218 void) {
219
220 printf("\nMPTRAC met_subgrid tool.\n\n");
221 printf
222 ("Calculate subgrid-scale wind and vertical velocity standard deviations.\n");
223 printf("\n");
224 printf("Usage:\n");
225 printf
226 (" met_subgrid <ctl> <subgrid.tab> <met0> <met1> [<met0b> <met1b> ...]\n");
227 printf("\n");
228 printf("Arguments:\n");
229 printf(" <ctl> Control file.\n");
230 printf(" <subgrid.tab> Output table.\n");
231 printf(" <met*> Meteorological input file pairs.\n");
232 printf("\nFurther information:\n");
233 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
234}
int main(int argc, char *argv[])
Definition met_subgrid.c:39
void usage(void)
Print command-line help.
void mptrac_free(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t *met0, met_t *met1, atm_t *atm, depo_t *depo, dd_t *dd)
Frees memory resources allocated for MPTRAC.
Definition mptrac.c:6956
float stddev(const float *data, const int n)
Calculates the standard deviation of a set of data.
Definition mptrac.c:13949
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition mptrac.c:7277
void mptrac_alloc(ctl_t **ctl, cache_t **cache, clim_t **clim, met_t **met0, met_t **met1, atm_t **atm, depo_t **depo, dd_t **dd)
Allocates and initializes memory resources for MPTRAC.
Definition mptrac.c:6866
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.
Definition mptrac.c:8673
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.
Definition mptrac.c:7351
MPTRAC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition mptrac.h:2466
#define EY
Maximum number of latitudes for meteo data.
Definition mptrac.h:554
#define USAGE
Print usage information on -h or --help.
Definition mptrac.h:2273
#define Z(p)
Convert pressure to altitude.
Definition mptrac.h:2303
#define DP2DZ(dp, p)
Convert a pressure difference to a height difference in the vertical direction.
Definition mptrac.h:884
#define LOG(level,...)
Print a log message with a specified logging level.
Definition mptrac.h:2396
#define EP
Maximum number of pressure levels for meteo data.
Definition mptrac.h:544
Climatological data.
Definition mptrac.h:3893
Control parameters.
Definition mptrac.h:2557
Domain decomposition data structure.
Definition mptrac.h:4129
Meteo data structure.
Definition mptrac.h:3952
float w[EX][EY][EP]
Vertical velocity [hPa/s].
Definition mptrac.h:4078
int nx
Number of longitudes.
Definition mptrac.h:3961
int ny
Number of latitudes.
Definition mptrac.h:3964
float ps[EX][EY]
Surface pressure [hPa].
Definition mptrac.h:3994
int np
Number of pressure levels.
Definition mptrac.h:3967
float u[EX][EY][EP]
Zonal wind [m/s].
Definition mptrac.h:4072
float v[EX][EY][EP]
Meridional wind [m/s].
Definition mptrac.h:4075
double time
Time [s].
Definition mptrac.h:3955
double lat[EY]
Latitudes [deg].
Definition mptrac.h:3976
double p[EP]
Pressure levels [hPa].
Definition mptrac.h:3979