MPTRAC
Functions
met_subgrid.c File Reference

Calculate standard deviations of horizontal wind and vertical velocity. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

void usage (void)
 Print command-line help. More...
 
int main (int argc, char *argv[])
 

Detailed Description

Calculate standard deviations of horizontal wind and vertical velocity.

Definition in file met_subgrid.c.

Function Documentation

◆ usage()

void usage ( void  )

Print command-line help.

Definition at line 226 of file met_subgrid.c.

227 {
228
229 printf("\nMPTRAC met_subgrid tool.\n\n");
230 printf
231 ("Calculate subgrid-scale wind and vertical velocity standard deviations.\n");
232 printf("\n");
233 printf("Usage:\n");
234 printf
235 (" met_subgrid <ctl> <subgrid.tab> <met0> <met1> [<met0b> <met1b> ...]\n");
236 printf("\n");
237 printf("Arguments:\n");
238 printf(" <ctl> Control file.\n");
239 printf(" <subgrid.tab> Output table.\n");
240 printf(" <met*> Meteorological input file pairs.\n");
241 printf("\nFurther information:\n");
242 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
243}

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 39 of file met_subgrid.c.

41 {
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 ALLOC(clim, clim_t, 1);
61 ALLOC(met0, met_t, 1);
62 ALLOC(met1, met_t, 1);
63 ALLOC(dd, dd_t, 1);
64
65 /* Print usage information... */
66 USAGE;
67
68 /* Check arguments... */
69 if (argc < 4 && argc % 2 != 0)
70 ERRMSG("Missing or invalid command-line arguments.\n\n"
71 "Usage: met_subgrid <ctl> <subgrid.tab> <met0> <met1> [<met0b> <met1b> ...]\n\n"
72 "Use -h for full help.");
73
74 /* Read control parameters... */
75 mptrac_read_ctl(argv[1], argc, argv, &ctl);
76
77 /* Read climatological data... */
78 mptrac_read_clim(&ctl, clim);
79
80 /* Loop over data files... */
81 for (int i = 3; i < argc - 1; i += 2) {
82
83 /* Read meteorological data... */
84 if (!mptrac_read_met(argv[i], &ctl, clim, met0, dd))
85 ERRMSG("Cannot open file!");
86 if (!mptrac_read_met(argv[i + 1], &ctl, clim, met1, dd))
87 ERRMSG("Cannot open file!");
88
89 /* Loop over grid boxes... */
90 for (int ix = 0; ix < met0->nx - 1; ix++)
91 for (int iy = 0; iy < met0->ny - 1; iy++)
92 for (int iz = 0; iz < met0->np - 1; iz++) {
93
94 /* Collect local wind data... */
95 u[0] = met0->u[ix][iy][iz];
96 u[1] = met0->u[ix + 1][iy][iz];
97 u[2] = met0->u[ix][iy + 1][iz];
98 u[3] = met0->u[ix + 1][iy + 1][iz];
99 u[4] = met0->u[ix][iy][iz + 1];
100 u[5] = met0->u[ix + 1][iy][iz + 1];
101 u[6] = met0->u[ix][iy + 1][iz + 1];
102 u[7] = met0->u[ix + 1][iy + 1][iz + 1];
103
104 v[0] = met0->v[ix][iy][iz];
105 v[1] = met0->v[ix + 1][iy][iz];
106 v[2] = met0->v[ix][iy + 1][iz];
107 v[3] = met0->v[ix + 1][iy + 1][iz];
108 v[4] = met0->v[ix][iy][iz + 1];
109 v[5] = met0->v[ix + 1][iy][iz + 1];
110 v[6] = met0->v[ix][iy + 1][iz + 1];
111 v[7] = met0->v[ix + 1][iy + 1][iz + 1];
112
113 w[0] = (float) (1e3 * DP2DZ(met0->w[ix][iy][iz], met0->p[iz]));
114 w[1] = (float) (1e3 * DP2DZ(met0->w[ix + 1][iy][iz], met0->p[iz]));
115 w[2] = (float) (1e3 * DP2DZ(met0->w[ix][iy + 1][iz], met0->p[iz]));
116 w[3] =
117 (float) (1e3 * DP2DZ(met0->w[ix + 1][iy + 1][iz], met0->p[iz]));
118 w[4] =
119 (float) (1e3 * DP2DZ(met0->w[ix][iy][iz + 1], met0->p[iz + 1]));
120 w[5] =
121 (float) (1e3 *
122 DP2DZ(met0->w[ix + 1][iy][iz + 1], met0->p[iz + 1]));
123 w[6] =
124 (float) (1e3 *
125 DP2DZ(met0->w[ix][iy + 1][iz + 1], met0->p[iz + 1]));
126 w[7] =
127 (float) (1e3 *
128 DP2DZ(met0->w[ix + 1][iy + 1][iz + 1], met0->p[iz + 1]));
129
130 /* Collect local wind data... */
131 u[8] = met1->u[ix][iy][iz];
132 u[9] = met1->u[ix + 1][iy][iz];
133 u[10] = met1->u[ix][iy + 1][iz];
134 u[11] = met1->u[ix + 1][iy + 1][iz];
135 u[12] = met1->u[ix][iy][iz + 1];
136 u[13] = met1->u[ix + 1][iy][iz + 1];
137 u[14] = met1->u[ix][iy + 1][iz + 1];
138 u[15] = met1->u[ix + 1][iy + 1][iz + 1];
139
140 v[8] = met1->v[ix][iy][iz];
141 v[9] = met1->v[ix + 1][iy][iz];
142 v[10] = met1->v[ix][iy + 1][iz];
143 v[11] = met1->v[ix + 1][iy + 1][iz];
144 v[12] = met1->v[ix][iy][iz + 1];
145 v[13] = met1->v[ix + 1][iy][iz + 1];
146 v[14] = met1->v[ix][iy + 1][iz + 1];
147 v[15] = met1->v[ix + 1][iy + 1][iz + 1];
148
149 w[8] = (float) (1e3 * DP2DZ(met1->w[ix][iy][iz], met1->p[iz]));
150 w[9] = (float) (1e3 * DP2DZ(met1->w[ix + 1][iy][iz], met1->p[iz]));
151 w[10] = (float) (1e3 * DP2DZ(met1->w[ix][iy + 1][iz], met1->p[iz]));
152 w[11] =
153 (float) (1e3 * DP2DZ(met1->w[ix + 1][iy + 1][iz], met1->p[iz]));
154 w[12] =
155 (float) (1e3 * DP2DZ(met1->w[ix][iy][iz + 1], met1->p[iz + 1]));
156 w[13] =
157 (float) (1e3 *
158 DP2DZ(met1->w[ix + 1][iy][iz + 1], met1->p[iz + 1]));
159 w[14] =
160 (float) (1e3 *
161 DP2DZ(met1->w[ix][iy + 1][iz + 1], met1->p[iz + 1]));
162 w[15] =
163 (float) (1e3 *
164 DP2DZ(met1->w[ix + 1][iy + 1][iz + 1], met1->p[iz + 1]));
165
166 /* Get standard deviations of local wind data... */
167 usig[iz][iy] += stddev(u, 16);
168 vsig[iz][iy] += stddev(v, 16);
169 wsig[iz][iy] += stddev(w, 16);
170 n[iz][iy]++;
171
172 /* Check surface pressure... */
173 if (met0->p[iz] > met0->ps[ix][iy]
174 || met1->p[iz] > met1->ps[ix][iy]) {
175 usig[iz][iy] = NAN;
176 vsig[iz][iy] = NAN;
177 wsig[iz][iy] = NAN;
178 n[iz][iy] = 0;
179 }
180 }
181 }
182
183 /* Create output file... */
184 LOG(1, "Write subgrid data file: %s", argv[2]);
185 if (!(out = fopen(argv[2], "w")))
186 ERRMSG("Cannot create file!");
187
188 /* Write header... */
189 fprintf(out,
190 "# $1 = time [s]\n"
191 "# $2 = altitude [km]\n"
192 "# $3 = longitude [deg]\n"
193 "# $4 = latitude [deg]\n"
194 "# $5 = zonal wind standard deviation [m/s]\n"
195 "# $6 = meridional wind standard deviation [m/s]\n"
196 "# $7 = vertical velocity standard deviation [m/s]\n"
197 "# $8 = number of data points\n");
198
199 /* Write output... */
200 for (int iy = 0; iy < met0->ny - 1; iy++) {
201 fprintf(out, "\n");
202 for (int iz = 0; iz < met0->np - 1; iz++)
203 fprintf(out, "%.2f %g %g %g %g %g %g %d\n",
204 0.5 * (met0->time + met1->time),
205 0.5 * (Z(met0->p[iz]) + Z(met1->p[iz + 1])),
206 0.0, 0.5 * (met0->lat[iy] + met1->lat[iy + 1]),
207 usig[iz][iy] / n[iz][iy], vsig[iz][iy] / n[iz][iy],
208 wsig[iz][iy] / n[iz][iy], n[iz][iy]);
209 }
210
211 /* Close file... */
212 fclose(out);
213
214 /* Free... */
215 free(clim);
216 free(met0);
217 free(met1);
218 free(dd);
219
220 return EXIT_SUCCESS;
221}
float stddev(const float *data, const int n)
Calculates the standard deviation of a set of data.
Definition: mptrac.c:10897
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:5188
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:6151
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:5248
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2102
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:340
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:1909
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1939
#define DP2DZ(dp, p)
Convert a pressure difference to a height difference in the vertical direction.
Definition: mptrac.h:629
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:453
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2032
#define EP
Maximum number of pressure levels for meteo data.
Definition: mptrac.h:330
Climatological data.
Definition: mptrac.h:3436
Control parameters.
Definition: mptrac.h:2190
Domain decomposition data structure.
Definition: mptrac.h:3669
Meteo data structure.
Definition: mptrac.h:3495
float w[EX][EY][EP]
Vertical velocity [hPa/s].
Definition: mptrac.h:3618
int nx
Number of longitudes.
Definition: mptrac.h:3501
int ny
Number of latitudes.
Definition: mptrac.h:3504
float ps[EX][EY]
Surface pressure [hPa].
Definition: mptrac.h:3534
int np
Number of pressure levels.
Definition: mptrac.h:3507
float u[EX][EY][EP]
Zonal wind [m/s].
Definition: mptrac.h:3612
float v[EX][EY][EP]
Meridional wind [m/s].
Definition: mptrac.h:3615
double time
Time [s].
Definition: mptrac.h:3498
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3516
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3519
Here is the call graph for this function: