41 {
42
44
46
48
50
51 FILE *out;
52
54
55 static float u[16], v[16], w[16];
56
58
59
60 mptrac_alloc(NULL, NULL, &clim, &met0, &met1, NULL, NULL, &dd);
61
62
64
65
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
73
74
76
77
78 for (int i = 3; i < argc - 1; i += 2) {
79
80
82 ERRMSG(
"Cannot open file!");
84 ERRMSG(
"Cannot open file!");
85
86
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
92 if (met0->
p[iz] > met0->
ps[ix][iy]
93 || met1->
p[iz] > met1->
ps[ix][iy])
94 continue;
95
96
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
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
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
177 LOG(1,
"Write subgrid data file: %s", argv[2]);
178 if (!(out = fopen(argv[2], "w")))
179 ERRMSG(
"Cannot create file!");
180
181
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
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",
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
206 fclose(out);
207
208
209 mptrac_free(NULL, NULL, clim, met0, met1, NULL, NULL, dd);
210
211 return EXIT_SUCCESS;
212}
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.
float stddev(const float *data, const int n)
Calculates the standard deviation of a set of data.
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
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.
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 ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define EY
Maximum number of latitudes for meteo data.
#define USAGE
Print usage information on -h or --help.
#define Z(p)
Convert pressure to altitude.
#define DP2DZ(dp, p)
Convert a pressure difference to a height difference in the vertical direction.
#define LOG(level,...)
Print a log message with a specified logging level.
#define EP
Maximum number of pressure levels for meteo data.
Domain decomposition data structure.
float w[EX][EY][EP]
Vertical velocity [hPa/s].
int nx
Number of longitudes.
int ny
Number of latitudes.
float ps[EX][EY]
Surface pressure [hPa].
int np
Number of pressure levels.
float u[EX][EY][EP]
Zonal wind [m/s].
float v[EX][EY][EP]
Meridional wind [m/s].
double lat[EY]
Latitudes [deg].
double p[EP]
Pressure levels [hPa].