MPTRAC
Functions
wind.c File Reference

Create meteorological data files with synthetic wind fields. 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

Create meteorological data files with synthetic wind fields.

Definition in file wind.c.

Function Documentation

◆ usage()

void usage ( void  )

Print command-line help.

Definition at line 183 of file wind.c.

184 {
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}

◆ main()

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

Definition at line 39 of file wind.c.

41 {
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, "1013.25", 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, "1.0", 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;
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);
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}
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:12110
void write_met_nc(const char *filename, const ctl_t *ctl, met_t *met)
Writes meteorological data to a NetCDF file.
Definition: mptrac.c:13922
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:3265
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:6466
#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:2196
#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:2003
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1574
#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:731
#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:647
#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:1141
Control parameters.
Definition: mptrac.h:2284
Meteo data structure.
Definition: mptrac.h:3595
float sst[EX][EY]
Sea surface temperature [K].
Definition: mptrac.h:3664
float h2o[EX][EY][EP]
Water vapor volume mixing ratio [1].
Definition: mptrac.h:3727
int coord_type
Definition: mptrac.h:3601
float w[EX][EY][EP]
Vertical velocity [hPa/s].
Definition: mptrac.h:3721
int nx
Number of longitudes.
Definition: mptrac.h:3604
int ny
Number of latitudes.
Definition: mptrac.h:3607
float shf[EX][EY]
Surface sensible heat flux [W/m^2].
Definition: mptrac.h:3658
float ps[EX][EY]
Surface pressure [hPa].
Definition: mptrac.h:3637
float us[EX][EY]
Surface zonal wind [m/s].
Definition: mptrac.h:3646
float zs[EX][EY]
Surface geopotential height [km].
Definition: mptrac.h:3643
float o3[EX][EY][EP]
Ozone volume mixing ratio [1].
Definition: mptrac.h:3730
int np
Number of pressure levels.
Definition: mptrac.h:3610
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3712
float ts[EX][EY]
Surface temperature [K].
Definition: mptrac.h:3640
float u[EX][EY][EP]
Zonal wind [m/s].
Definition: mptrac.h:3715
float ess[EX][EY]
Eastward turbulent surface stress [N/m^2].
Definition: mptrac.h:3652
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3616
float pbl[EX][EY]
Boundary layer pressure [hPa].
Definition: mptrac.h:3667
float vs[EX][EY]
Surface meridional wind [m/s].
Definition: mptrac.h:3649
float v[EX][EY][EP]
Meridional wind [m/s].
Definition: mptrac.h:3718
float lsm[EX][EY]
Land-sea mask [1].
Definition: mptrac.h:3661
double time
Time [s].
Definition: mptrac.h:3598
float nss[EX][EY]
Northward turbulent surface stress [N/m^2].
Definition: mptrac.h:3655
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3619
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3622
Here is the call graph for this function: