29 {
30
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
52
53
54 if (argc < 3)
55 ERRMSG(
"Give parameters: <ctl> <metbase>");
56
57
59 double t0 =
scan_ctl(argv[1], argc, argv,
"WIND_T0", -1,
"0", NULL);
60 const int nx =
61 (int)
scan_ctl(argv[1], argc, argv,
"WIND_NX", -1,
"360", NULL);
62 const int ny =
63 (int)
scan_ctl(argv[1], argc, argv,
"WIND_NY", -1,
"181", NULL);
64 const int nz =
65 (int)
scan_ctl(argv[1], argc, argv,
"WIND_NZ", -1,
"61", NULL);
66 const double z0 =
scan_ctl(argv[1], argc, argv,
"WIND_Z0", -1,
"0", NULL);
67 const double z1 =
scan_ctl(argv[1], argc, argv,
"WIND_Z1", -1,
"60", NULL);
68 const double u0 =
69 scan_ctl(argv[1], argc, argv,
"WIND_U0", -1,
"38.587660177302", NULL);
70 const double u1 =
71 scan_ctl(argv[1], argc, argv,
"WIND_U1", -1,
"38.587660177302", NULL);
72 const double w0 =
scan_ctl(argv[1], argc, argv,
"WIND_W0", -1,
"0", NULL);
73 const double alpha =
74 scan_ctl(argv[1], argc, argv,
"WIND_ALPHA", -1,
"0.0", NULL);
75
76
77 if (nx < 1 || nx >
EX)
78 ERRMSG(
"Set 1 <= NX <= MAX!");
79 if (ny < 1 || ny >
EY)
80 ERRMSG(
"Set 1 <= NY <= MAX!");
81 if (nz < 1 || nz >
EP)
82 ERRMSG(
"Set 1 <= NZ <= MAX!");
83
84
85 jsec2time(t0, &year, &mon, &day, &hour, &min, &sec, &r);
86 t0 = year * 10000. + mon * 100. + day + hour / 24.;
87
88
89 sprintf(filename, "%s_%d_%02d_%02d_%02d.nc", argv[2], year, mon, day, hour);
90
91
92 NC(nc_create(filename, NC_NETCDF4, &ncid));
93
94
95 NC(nc_def_dim(ncid,
"time", 1, &dims[0]));
96 NC(nc_def_dim(ncid,
"lev", (
size_t) nz, &dims[1]));
97 NC(nc_def_dim(ncid,
"lat", (
size_t) ny, &dims[2]));
98 NC(nc_def_dim(ncid,
"lon", (
size_t) nx, &dims[3]));
99
100
101 NC_DEF_VAR(
"time", NC_DOUBLE, 1, &dims[0],
"time",
"day as %Y%m%d.%f", 0,
102 0);
103 NC_DEF_VAR(
"lev", NC_DOUBLE, 1, &dims[1],
"air_pressure",
"Pa", 0, 0);
104 NC_DEF_VAR(
"lat", NC_DOUBLE, 1, &dims[2],
"latitude",
"degrees_north", 0,
105 0);
106 NC_DEF_VAR(
"lon", NC_DOUBLE, 1, &dims[3],
"longitude",
"degrees_east", 0,
107 0);
108 NC_DEF_VAR(
"T", NC_FLOAT, 4, &dims[0],
"Temperature",
"K", 0, 0);
109 NC_DEF_VAR(
"U", NC_FLOAT, 4, &dims[0],
"zonal wind",
"m s**-1", 0, 0);
110 NC_DEF_VAR(
"V", NC_FLOAT, 4, &dims[0],
"meridional wind",
"m s**-1", 0, 0);
111 NC_DEF_VAR(
"W", NC_FLOAT, 4, &dims[0],
"vertical velocity",
"Pa s**-1", 0,
112 0);
113
114
116
117
118 for (int ix = 0; ix < nx; ix++)
119 dataLon[ix] = 360.0 / nx * (double) ix;
120 for (int iy = 0; iy < ny; iy++)
121 dataLat[iy] = 180.0 / (ny - 1) * (double) iy - 90;
122 for (int iz = 0; iz < nz; iz++)
123 dataZ[iz] = 100. *
P(
LIN(0.0, z0, nz - 1.0, z1, iz));
124
125
130
131
132 for (int ix = 0; ix < nx; ix++)
133 for (int iy = 0; iy < ny; iy++)
134 for (int iz = 0; iz < nz; iz++) {
135 int idx = (iz * ny + iy) * nx + ix;
136 dataU[idx] = (float) (
LIN(0.0, u0, nz - 1.0, u1, iz)
142 dataV[idx] = (float) (-
LIN(0.0, u0, nz - 1.0, u1, iz)
145 dataW[idx] = (float)
DZ2DP(1e-3 * w0, dataZ[iz]);
146 }
147
148
153
154
156
157
158 free(dataT);
159 free(dataU);
160 free(dataV);
161 free(dataW);
162
163 return EXIT_SUCCESS;
164}
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.
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.
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.
#define LEN
Maximum length of ASCII data lines.
#define NC(cmd)
Execute a NetCDF command and check for errors.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define EY
Maximum number of latitudes for meteo data.
#define P(z)
Compute pressure at given altitude.
#define EX
Maximum number of longitudes for meteo data.
#define DZ2DP(dz, p)
Convert a change in altitude to a change in pressure.
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
#define DEG2RAD(deg)
Converts degrees to radians.
#define NC_PUT_FLOAT(varname, ptr, hyperslab)
Write a float array to a NetCDF file.
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
#define EP
Maximum number of pressure levels for meteo data.
#define NC_PUT_DOUBLE(varname, ptr, hyperslab)
Write double precision data to a NetCDF variable.
#define LIN(x0, y0, x1, y1, x)
Linear interpolation.