MPTRAC
wind.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 static ctl_t ctl;
44 met_t *met;
45
46 static char filename[LEN];
47
48 static int year, mon, day, hour, min, sec;
49 static double r;
50
51 /* Print usage information... */
52 USAGE;
53
54 /* Check arguments... */
55 if (argc < 3)
56 ERRMSG("Missing or invalid command-line arguments.\n\n"
57 "Usage: wind <ctl> <metbase> [KEY VALUE ...]\n\n"
58 "Use -h for full help.");
59
60 /* Read control parameters... */
61 mptrac_read_ctl(argv[1], argc, argv, &ctl);
62 const double t0 = scan_ctl(argv[1], argc, argv, "WIND_T0", -1, "0", NULL);
63 const int nx =
64 (int) scan_ctl(argv[1], argc, argv, "WIND_NX", -1, "360", NULL);
65 const int ny =
66 (int) scan_ctl(argv[1], argc, argv, "WIND_NY", -1, "181", NULL);
67 const int nz =
68 (int) scan_ctl(argv[1], argc, argv, "WIND_NZ", -1, "61", NULL);
69 const double z0 = scan_ctl(argv[1], argc, argv, "WIND_Z0", -1, "0", NULL);
70 const double z1 = scan_ctl(argv[1], argc, argv, "WIND_Z1", -1, "60", NULL);
71 const double u0 =
72 scan_ctl(argv[1], argc, argv, "WIND_U0", -1, "38.587660177302", NULL);
73 const double u1 =
74 scan_ctl(argv[1], argc, argv, "WIND_U1", -1, "38.587660177302", NULL);
75 const double w0 = scan_ctl(argv[1], argc, argv, "WIND_W0", -1, "0", NULL);
76 const double alpha =
77 scan_ctl(argv[1], argc, argv, "WIND_ALPHA", -1, "0.0", NULL);
78 const int lat_reverse =
79 (int) scan_ctl(argv[1], argc, argv, "WIND_LAT_REVERSE", -1, "0", NULL);
80 const double temp0 =
81 scan_ctl(argv[1], argc, argv, "WIND_TEMP0", -1, "280", NULL);
82 const double temp1 =
83 scan_ctl(argv[1], argc, argv, "WIND_TEMP1", -1, "280", NULL);
84 const double ps =
85 scan_ctl(argv[1], argc, argv, "WIND_PS", -1, "100000", NULL);
86 const double zs = scan_ctl(argv[1], argc, argv, "WIND_ZS", -1, "0", NULL);
87 const double t2m =
88 scan_ctl(argv[1], argc, argv, "WIND_T2M", -1, "280", NULL);
89 const double iews =
90 scan_ctl(argv[1], argc, argv, "WIND_IEWS", -1, "0", NULL);
91 const double inss =
92 scan_ctl(argv[1], argc, argv, "WIND_INSS", -1, "0", NULL);
93 const double ishf =
94 scan_ctl(argv[1], argc, argv, "WIND_ISHF", -1, "0", NULL);
95 const double lsm = scan_ctl(argv[1], argc, argv, "WIND_LSM", -1, "1", NULL);
96 const double sst =
97 scan_ctl(argv[1], argc, argv, "WIND_SST", -1, "280", NULL);
98 const double blh =
99 scan_ctl(argv[1], argc, argv, "WIND_BLH", -1, "1000", NULL);
100 const double q = scan_ctl(argv[1], argc, argv, "WIND_Q", -1, "0", NULL);
101 const double o3 = scan_ctl(argv[1], argc, argv, "WIND_O3", -1, "0", NULL);
102
103 /* Check dimensions... */
104 if (nx < 2 || nx > EX)
105 ERRMSG("Set 2 <= NX <= MAX!");
106 if (ny < 2 || ny > EY)
107 ERRMSG("Set 2 <= NY <= MAX!");
108 if (nz < 2 || nz > EP)
109 ERRMSG("Set 2 <= NZ <= MAX!");
110
111 /* Get time and output filename... */
112 jsec2time(t0, &year, &mon, &day, &hour, &min, &sec, &r);
113 sprintf(filename, "%s_%d_%02d_%02d_%02d.nc", argv[2], year, mon, day, hour);
114
115 /* Initialize synthetic meteorological data. */
116 ALLOC(met, met_t, 1);
117 met->time = t0;
118 met->coord_type = 0;
119 met->nx = nx;
120 met->ny = ny;
121 met->np = nz;
122
123 /* Set grid... */
124 for (int ix = 0; ix < nx; ix++)
125 met->lon[ix] = 360.0 / nx * (double) ix;
126 for (int iy = 0; iy < ny; iy++)
127 met->lat[iy] = (lat_reverse ? -(180.0 / (ny - 1) * (double) iy - 90.0)
128 : (180.0 / (ny - 1) * (double) iy - 90.0));
129 for (int iz = 0; iz < nz; iz++)
130 met->p[iz] = P(LIN(0.0, z0, nz - 1.0, z1, iz));
131
132 /* Set meteo data... */
133 for (int ix = 0; ix < nx; ix++)
134 for (int iy = 0; iy < ny; iy++) {
135 const double usfc = u0
136 * (cos(DEG2RAD(met->lat[iy])) * cos(DEG2RAD(alpha))
137 + sin(DEG2RAD(met->lat[iy])) * cos(DEG2RAD(met->lon[ix]))
138 * sin(DEG2RAD(alpha)));
139 const double vsfc =
140 -u0 * sin(DEG2RAD(met->lon[ix])) * sin(DEG2RAD(alpha));
141
142 met->ps[ix][iy] = (float) (ps / 100.0);
143 met->zs[ix][iy] = (float) zs;
144 met->ts[ix][iy] = (float) t2m;
145 met->us[ix][iy] = (float) usfc;
146 met->vs[ix][iy] = (float) vsfc;
147 met->ess[ix][iy] = (float) iews;
148 met->nss[ix][iy] = (float) inss;
149 met->shf[ix][iy] = (float) ishf;
150 met->lsm[ix][iy] = (float) lsm;
151 met->sst[ix][iy] = (float) sst;
152 met->pbl[ix][iy] = (float) P(zs + blh / 1000.0);
153
154 for (int iz = 0; iz < nz; iz++) {
155 const double u = LIN(0.0, u0, nz - 1.0, u1, iz)
156 * (cos(DEG2RAD(met->lat[iy])) * cos(DEG2RAD(alpha))
157 + sin(DEG2RAD(met->lat[iy])) * cos(DEG2RAD(met->lon[ix]))
158 * sin(DEG2RAD(alpha)));
159 const double v = -LIN(0.0, u0, nz - 1.0, u1, iz)
160 * sin(DEG2RAD(met->lon[ix])) * sin(DEG2RAD(alpha));
161
162 met->t[ix][iy][iz] = (float) LIN(0.0, temp0, nz - 1.0, temp1, iz);
163 met->u[ix][iy][iz] = (float) u;
164 met->v[ix][iy][iz] = (float) v;
165 met->w[ix][iy][iz] = (float) DZ2DP(1e-3 * w0, met->p[iz]);
166 met->h2o[ix][iy][iz] = (float) (q * MA / MH2O);
167 met->o3[ix][iy][iz] = (float) (o3 * MA / MO3);
168 }
169 }
170
171 /* Write synthetic meteorological data. */
172 write_met_nc(filename, &ctl, met);
173
174 /* Free... */
175 free(met);
176
177 return EXIT_SUCCESS;
178}
179
180/*****************************************************************************/
181
183void usage(
184 void) {
185
186 printf("\nMPTRAC wind tool.\n\n");
187 printf("Create meteorological data with synthetic wind fields.\n");
188 printf("\n");
189 printf("Usage:\n");
190 printf(" wind <ctl> <metbase> [KEY VALUE ...]\n");
191 printf("\n");
192 printf("Arguments:\n");
193 printf(" <ctl> Control file.\n");
194 printf(" <metbase> Basename of the output meteorological data.\n");
195 printf(" [KEY VALUE] Optional control parameters.\n");
196 printf("\nFurther information:\n");
197 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
198}
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:11856
void write_met_nc(const char *filename, const ctl_t *ctl, met_t *met)
Writes meteorological data to a NetCDF file.
Definition: mptrac.c:13646
void jsec2time(const double jsec, int *year, int *mon, int *day, int *hour, int *min, int *sec, double *remain)
Converts Julian seconds to calendar date and time components.
Definition: mptrac.c:3212
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:6223
MPTRAC library declarations.
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:349
#define MA
Molar mass of dry air [g/mol].
Definition: mptrac.h:290
#define MH2O
Molar mass of water vapor [g/mol].
Definition: mptrac.h:295
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2172
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:344
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:1979
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1550
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:339
#define DZ2DP(dz, p)
Convert a change in altitude to a change in pressure.
Definition: mptrac.h:707
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:457
#define DEG2RAD(deg)
Converts degrees to radians.
Definition: mptrac.h:623
#define MO3
Molar mass of ozone [g/mol].
Definition: mptrac.h:300
#define EP
Maximum number of pressure levels for meteo data.
Definition: mptrac.h:334
#define LIN(x0, y0, x1, y1, x)
Linear interpolation.
Definition: mptrac.h:1117
Control parameters.
Definition: mptrac.h:2260
Meteo data structure.
Definition: mptrac.h:3565
float sst[EX][EY]
Sea surface temperature [K].
Definition: mptrac.h:3634
float h2o[EX][EY][EP]
Water vapor volume mixing ratio [1].
Definition: mptrac.h:3697
int coord_type
Definition: mptrac.h:3571
float w[EX][EY][EP]
Vertical velocity [hPa/s].
Definition: mptrac.h:3691
int nx
Number of longitudes.
Definition: mptrac.h:3574
int ny
Number of latitudes.
Definition: mptrac.h:3577
float shf[EX][EY]
Surface sensible heat flux [W/m^2].
Definition: mptrac.h:3628
float ps[EX][EY]
Surface pressure [hPa].
Definition: mptrac.h:3607
float us[EX][EY]
Surface zonal wind [m/s].
Definition: mptrac.h:3616
float zs[EX][EY]
Surface geopotential height [km].
Definition: mptrac.h:3613
float o3[EX][EY][EP]
Ozone volume mixing ratio [1].
Definition: mptrac.h:3700
int np
Number of pressure levels.
Definition: mptrac.h:3580
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3682
float ts[EX][EY]
Surface temperature [K].
Definition: mptrac.h:3610
float u[EX][EY][EP]
Zonal wind [m/s].
Definition: mptrac.h:3685
float ess[EX][EY]
Eastward turbulent surface stress [N/m^2].
Definition: mptrac.h:3622
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3586
float pbl[EX][EY]
Boundary layer pressure [hPa].
Definition: mptrac.h:3637
float vs[EX][EY]
Surface meridional wind [m/s].
Definition: mptrac.h:3619
float v[EX][EY][EP]
Meridional wind [m/s].
Definition: mptrac.h:3688
float lsm[EX][EY]
Land-sea mask [1].
Definition: mptrac.h:3631
double time
Time [s].
Definition: mptrac.h:3568
float nss[EX][EY]
Northward turbulent surface stress [N/m^2].
Definition: mptrac.h:3625
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3589
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3592
int main(int argc, char *argv[])
Definition: wind.c:39
void usage(void)
Print command-line help.
Definition: wind.c:183