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 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
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
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
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
181 LOG(1,
"Write subgrid data file: %s", argv[2]);
182 if (!(out = fopen(argv[2], "w")))
183 ERRMSG(
"Cannot create file!");
184
185
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
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",
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
209 fclose(out);
210
211
212 mptrac_free(NULL, NULL, clim, met0, met1, NULL, NULL, dd);
213
214 return EXIT_SUCCESS;
215}
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].