MPTRAC
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 /* Collect local wind data... */
92 u[0] = met0->u[ix][iy][iz];
93 u[1] = met0->u[ix + 1][iy][iz];
94 u[2] = met0->u[ix][iy + 1][iz];
95 u[3] = met0->u[ix + 1][iy + 1][iz];
96 u[4] = met0->u[ix][iy][iz + 1];
97 u[5] = met0->u[ix + 1][iy][iz + 1];
98 u[6] = met0->u[ix][iy + 1][iz + 1];
99 u[7] = met0->u[ix + 1][iy + 1][iz + 1];
100
101 v[0] = met0->v[ix][iy][iz];
102 v[1] = met0->v[ix + 1][iy][iz];
103 v[2] = met0->v[ix][iy + 1][iz];
104 v[3] = met0->v[ix + 1][iy + 1][iz];
105 v[4] = met0->v[ix][iy][iz + 1];
106 v[5] = met0->v[ix + 1][iy][iz + 1];
107 v[6] = met0->v[ix][iy + 1][iz + 1];
108 v[7] = met0->v[ix + 1][iy + 1][iz + 1];
109
110 w[0] = (float) (1e3 * DP2DZ(met0->w[ix][iy][iz], met0->p[iz]));
111 w[1] = (float) (1e3 * DP2DZ(met0->w[ix + 1][iy][iz], met0->p[iz]));
112 w[2] = (float) (1e3 * DP2DZ(met0->w[ix][iy + 1][iz], met0->p[iz]));
113 w[3] =
114 (float) (1e3 * DP2DZ(met0->w[ix + 1][iy + 1][iz], met0->p[iz]));
115 w[4] =
116 (float) (1e3 * DP2DZ(met0->w[ix][iy][iz + 1], met0->p[iz + 1]));
117 w[5] =
118 (float) (1e3 *
119 DP2DZ(met0->w[ix + 1][iy][iz + 1], met0->p[iz + 1]));
120 w[6] =
121 (float) (1e3 *
122 DP2DZ(met0->w[ix][iy + 1][iz + 1], met0->p[iz + 1]));
123 w[7] =
124 (float) (1e3 *
125 DP2DZ(met0->w[ix + 1][iy + 1][iz + 1], met0->p[iz + 1]));
126
127 /* Collect local wind data... */
128 u[8] = met1->u[ix][iy][iz];
129 u[9] = met1->u[ix + 1][iy][iz];
130 u[10] = met1->u[ix][iy + 1][iz];
131 u[11] = met1->u[ix + 1][iy + 1][iz];
132 u[12] = met1->u[ix][iy][iz + 1];
133 u[13] = met1->u[ix + 1][iy][iz + 1];
134 u[14] = met1->u[ix][iy + 1][iz + 1];
135 u[15] = met1->u[ix + 1][iy + 1][iz + 1];
136
137 v[8] = met1->v[ix][iy][iz];
138 v[9] = met1->v[ix + 1][iy][iz];
139 v[10] = met1->v[ix][iy + 1][iz];
140 v[11] = met1->v[ix + 1][iy + 1][iz];
141 v[12] = met1->v[ix][iy][iz + 1];
142 v[13] = met1->v[ix + 1][iy][iz + 1];
143 v[14] = met1->v[ix][iy + 1][iz + 1];
144 v[15] = met1->v[ix + 1][iy + 1][iz + 1];
145
146 w[8] = (float) (1e3 * DP2DZ(met1->w[ix][iy][iz], met1->p[iz]));
147 w[9] = (float) (1e3 * DP2DZ(met1->w[ix + 1][iy][iz], met1->p[iz]));
148 w[10] = (float) (1e3 * DP2DZ(met1->w[ix][iy + 1][iz], met1->p[iz]));
149 w[11] =
150 (float) (1e3 * DP2DZ(met1->w[ix + 1][iy + 1][iz], met1->p[iz]));
151 w[12] =
152 (float) (1e3 * DP2DZ(met1->w[ix][iy][iz + 1], met1->p[iz + 1]));
153 w[13] =
154 (float) (1e3 *
155 DP2DZ(met1->w[ix + 1][iy][iz + 1], met1->p[iz + 1]));
156 w[14] =
157 (float) (1e3 *
158 DP2DZ(met1->w[ix][iy + 1][iz + 1], met1->p[iz + 1]));
159 w[15] =
160 (float) (1e3 *
161 DP2DZ(met1->w[ix + 1][iy + 1][iz + 1], met1->p[iz + 1]));
162
163 /* Get standard deviations of local wind data... */
164 usig[iz][iy] += stddev(u, 16);
165 vsig[iz][iy] += stddev(v, 16);
166 wsig[iz][iy] += stddev(w, 16);
167 n[iz][iy]++;
168
169 /* Check surface pressure... */
170 if (met0->p[iz] > met0->ps[ix][iy]
171 || met1->p[iz] > met1->ps[ix][iy]) {
172 usig[iz][iy] = NAN;
173 vsig[iz][iy] = NAN;
174 wsig[iz][iy] = NAN;
175 n[iz][iy] = 0;
176 }
177 }
178 }
179
180 /* Create output file... */
181 LOG(1, "Write subgrid data file: %s", argv[2]);
182 if (!(out = fopen(argv[2], "w")))
183 ERRMSG("Cannot create file!");
184
185 /* Write header... */
186 fprintf(out,
187 "# $1 = time [s]\n"
188 "# $2 = altitude [km]\n"
189 "# $3 = longitude [deg]\n"
190 "# $4 = latitude [deg]\n"
191 "# $5 = zonal wind standard deviation [m/s]\n"
192 "# $6 = meridional wind standard deviation [m/s]\n"
193 "# $7 = vertical velocity standard deviation [m/s]\n"
194 "# $8 = number of data points\n");
195
196 /* Write output... */
197 for (int iy = 0; iy < met0->ny - 1; iy++) {
198 fprintf(out, "\n");
199 for (int iz = 0; iz < met0->np - 1; iz++)
200 fprintf(out, "%.2f %g %g %g %g %g %g %d\n",
201 0.5 * (met0->time + met1->time),
202 0.5 * (Z(met0->p[iz]) + Z(met1->p[iz + 1])),
203 0.0, 0.5 * (met0->lat[iy] + met1->lat[iy + 1]),
204 usig[iz][iy] / n[iz][iy], vsig[iz][iy] / n[iz][iy],
205 wsig[iz][iy] / n[iz][iy], n[iz][iy]);
206 }
207
208 /* Close file... */
209 fclose(out);
210
211 /* Free... */
212 mptrac_free(NULL, NULL, clim, met0, met1, NULL, NULL, dd);
213
214 return EXIT_SUCCESS;
215}
216
217/*****************************************************************************/
218
220void usage(
221 void) {
222
223 printf("\nMPTRAC met_subgrid tool.\n\n");
224 printf
225 ("Calculate subgrid-scale wind and vertical velocity standard deviations.\n");
226 printf("\n");
227 printf("Usage:\n");
228 printf
229 (" met_subgrid <ctl> <subgrid.tab> <met0> <met1> [<met0b> <met1b> ...]\n");
230 printf("\n");
231 printf("Arguments:\n");
232 printf(" <ctl> Control file.\n");
233 printf(" <subgrid.tab> Output table.\n");
234 printf(" <met*> Meteorological input file pairs.\n");
235 printf("\nFurther information:\n");
236 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
237}
int main(int argc, char *argv[])
Definition: met_subgrid.c:39
void usage(void)
Print command-line help.
Definition: met_subgrid.c:220
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:6397
float stddev(const float *data, const int n)
Calculates the standard deviation of a set of data.
Definition: mptrac.c:12614
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:6683
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:6314
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:7767
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:6743
MPTRAC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2405
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:553
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:2212
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:2242
#define DP2DZ(dp, p)
Convert a pressure difference to a height difference in the vertical direction.
Definition: mptrac.h:881
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2335
#define EP
Maximum number of pressure levels for meteo data.
Definition: mptrac.h:543
Climatological data.
Definition: mptrac.h:3787
Control parameters.
Definition: mptrac.h:2493
Domain decomposition data structure.
Definition: mptrac.h:4023
Meteo data structure.
Definition: mptrac.h:3846
float w[EX][EY][EP]
Vertical velocity [hPa/s].
Definition: mptrac.h:3972
int nx
Number of longitudes.
Definition: mptrac.h:3855
int ny
Number of latitudes.
Definition: mptrac.h:3858
float ps[EX][EY]
Surface pressure [hPa].
Definition: mptrac.h:3888
int np
Number of pressure levels.
Definition: mptrac.h:3861
float u[EX][EY][EP]
Zonal wind [m/s].
Definition: mptrac.h:3966
float v[EX][EY][EP]
Meridional wind [m/s].
Definition: mptrac.h:3969
double time
Time [s].
Definition: mptrac.h:3849
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3870
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3873