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

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

Detailed Description

Create meteorological data files with synthetic wind fields.

Definition in file wind.c.

Function Documentation

◆ main()

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

Definition at line 27 of file wind.c.

29 {
30
31 ctl_t ctl;
32
33 static char filename[LEN];
34
35 static double r, dataLon[EX], dataLat[EY], dataZ[EP];
36
37 static float *dataT, *dataU, *dataV, *dataW;
38
39 static int ncid, varid, dims[4], year, mon, day, hour, min, sec;
40
41 static size_t start[4], count[4];
42
43 /* Allocate... */
44 ALLOC(dataT, float,
45 EP * EY * EX);
46 ALLOC(dataU, float,
47 EP * EY * EX);
48 ALLOC(dataV, float,
49 EP * EY * EX);
50 ALLOC(dataW, float,
51 EP * EY * EX);
52
53 /* Check arguments... */
54 if (argc < 3)
55 ERRMSG("Give parameters: <ctl> <metbase>");
56
57 /* Read control parameters... */
58 read_ctl(argv[1], argc, argv, &ctl);
59 double t0 = scan_ctl(argv[1], argc, argv, "WIND_T0", -1, "0", NULL);
60 int nx = (int) scan_ctl(argv[1], argc, argv, "WIND_NX", -1, "360", NULL);
61 int ny = (int) scan_ctl(argv[1], argc, argv, "WIND_NY", -1, "181", NULL);
62 int nz = (int) scan_ctl(argv[1], argc, argv, "WIND_NZ", -1, "61", NULL);
63 double z0 = scan_ctl(argv[1], argc, argv, "WIND_Z0", -1, "0", NULL);
64 double z1 = scan_ctl(argv[1], argc, argv, "WIND_Z1", -1, "60", NULL);
65 double u0 =
66 scan_ctl(argv[1], argc, argv, "WIND_U0", -1, "38.587660177302", NULL);
67 double u1 =
68 scan_ctl(argv[1], argc, argv, "WIND_U1", -1, "38.587660177302", NULL);
69 double w0 = scan_ctl(argv[1], argc, argv, "WIND_W0", -1, "0", NULL);
70 double alpha = scan_ctl(argv[1], argc, argv, "WIND_ALPHA", -1, "0.0", NULL);
71
72 /* Check dimensions... */
73 if (nx < 1 || nx > EX)
74 ERRMSG("Set 1 <= NX <= MAX!");
75 if (ny < 1 || ny > EY)
76 ERRMSG("Set 1 <= NY <= MAX!");
77 if (nz < 1 || nz > EP)
78 ERRMSG("Set 1 <= NZ <= MAX!");
79
80 /* Get time... */
81 jsec2time(t0, &year, &mon, &day, &hour, &min, &sec, &r);
82 t0 = year * 10000. + mon * 100. + day + hour / 24.;
83
84 /* Set filename... */
85 sprintf(filename, "%s_%d_%02d_%02d_%02d.nc", argv[2], year, mon, day, hour);
86
87 /* Create netCDF file... */
88 NC(nc_create(filename, NC_NETCDF4, &ncid));
89
90 /* Create dimensions... */
91 NC(nc_def_dim(ncid, "time", 1, &dims[0]));
92 NC(nc_def_dim(ncid, "lev", (size_t) nz, &dims[1]));
93 NC(nc_def_dim(ncid, "lat", (size_t) ny, &dims[2]));
94 NC(nc_def_dim(ncid, "lon", (size_t) nx, &dims[3]));
95
96 /* Create variables... */
97 NC_DEF_VAR("time", NC_DOUBLE, 1, &dims[0], "time", "day as %Y%m%d.%f", 0,
98 0);
99 NC_DEF_VAR("lev", NC_DOUBLE, 1, &dims[1], "air_pressure", "Pa", 0, 0);
100 NC_DEF_VAR("lat", NC_DOUBLE, 1, &dims[2], "latitude", "degrees_north", 0,
101 0);
102 NC_DEF_VAR("lon", NC_DOUBLE, 1, &dims[3], "longitude", "degrees_east", 0,
103 0);
104 NC_DEF_VAR("T", NC_FLOAT, 4, &dims[0], "Temperature", "K", 0, 0);
105 NC_DEF_VAR("U", NC_FLOAT, 4, &dims[0], "zonal wind", "m s**-1", 0, 0);
106 NC_DEF_VAR("V", NC_FLOAT, 4, &dims[0], "meridional wind", "m s**-1", 0, 0);
107 NC_DEF_VAR("W", NC_FLOAT, 4, &dims[0], "vertical velocity", "Pa s**-1", 0,
108 0);
109
110 /* End definition... */
111 NC(nc_enddef(ncid));
112
113 /* Set coordinates... */
114 for (int ix = 0; ix < nx; ix++)
115 dataLon[ix] = 360.0 / nx * (double) ix;
116 for (int iy = 0; iy < ny; iy++)
117 dataLat[iy] = 180.0 / (ny - 1) * (double) iy - 90;
118 for (int iz = 0; iz < nz; iz++)
119 dataZ[iz] = 100. * P(LIN(0.0, z0, nz - 1.0, z1, iz));
120
121 /* Write coordinates... */
122 NC_PUT_DOUBLE("time", &t0, 0);
123 NC_PUT_DOUBLE("lev", dataZ, 0);
124 NC_PUT_DOUBLE("lat", dataLat, 0);
125 NC_PUT_DOUBLE("lon", dataLon, 0);
126
127 /* Create wind fields (Williamson et al., 1992)... */
128 for (int ix = 0; ix < nx; ix++)
129 for (int iy = 0; iy < ny; iy++)
130 for (int iz = 0; iz < nz; iz++) {
131 int idx = (iz * ny + iy) * nx + ix;
132 dataU[idx] = (float) (LIN(0.0, u0, nz - 1.0, u1, iz)
133 * (cos(DEG2RAD(dataLat[iy]))
134 * cos(DEG2RAD(alpha))
135 + sin(DEG2RAD(dataLat[iy]))
136 * cos(DEG2RAD(dataLon[ix]))
137 * sin(DEG2RAD(alpha))));
138 dataV[idx] = (float) (-LIN(0.0, u0, nz - 1.0, u1, iz)
139 * sin(DEG2RAD(dataLon[ix]))
140 * sin(DEG2RAD(alpha)));
141 dataW[idx] = (float) DZ2DP(1e-3 * w0, dataZ[iz]);
142 }
143
144 /* Write data... */
145 NC_PUT_FLOAT("T", dataT, 0);
146 NC_PUT_FLOAT("U", dataU, 0);
147 NC_PUT_FLOAT("V", dataV, 0);
148 NC_PUT_FLOAT("W", dataW, 0);
149
150 /* Close file... */
151 NC(nc_close(ncid));
152
153 /* Free... */
154 free(dataT);
155 free(dataU);
156 free(dataV);
157 free(dataW);
158
159 return EXIT_SUCCESS;
160}
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:8098
void 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:4805
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:1832
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:236
#define NC(cmd)
Execute a NetCDF command and check for errors.
Definition: mptrac.h:1001
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1901
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:269
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1289
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:262
#define DZ2DP(dz, p)
Convert a change in altitude to a change in pressure.
Definition: mptrac.h:556
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define DEG2RAD(deg)
Converts degrees to radians.
Definition: mptrac.h:476
#define NC_PUT_FLOAT(varname, ptr, hyperslab)
Write a float array to a NetCDF file.
Definition: mptrac.h:1135
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1030
#define EP
Maximum number of pressure levels for meteo data.
Definition: mptrac.h:257
#define NC_PUT_DOUBLE(varname, ptr, hyperslab)
Write double precision data to a NetCDF variable.
Definition: mptrac.h:1111
#define LIN(x0, y0, x1, y1, x)
Linear interpolation.
Definition: mptrac.h:847
Control parameters.
Definition: mptrac.h:2155
Here is the call graph for this function: