MPTRAC
Functions
tropo.c File Reference

Create tropopause data set from meteorological data. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

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

Detailed Description

Create tropopause data set from meteorological data.

Definition in file tropo.c.

Function Documentation

◆ main()

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

Definition at line 27 of file tropo.c.

29 {
30
31 ctl_t ctl;
32
33 clim_t *clim;
34
35 met_t *met;
36
37 static double ps[EX * EY], pt[EX * EY], qt[EX * EY], o3t[EX * EY],
38 zs[EX * EY], zt[EX * EY], tt[EX * EY], lon, lons[EX], lat, lats[EY];
39
40 static int init, nx, ny, nt, ncid, varid, dims[3];
41
42 static size_t count[10], start[10];
43
44 /* Allocate... */
45 ALLOC(clim, clim_t, 1);
46 ALLOC(met, met_t, 1);
47
48 /* Check arguments... */
49 if (argc < 4)
50 ERRMSG("Give parameters: <ctl> <tropo.nc> <met0> [ <met1> ... ]");
51
52 /* Read control parameters... */
53 read_ctl(argv[1], argc, argv, &ctl);
54 double lon0 = scan_ctl(argv[1], argc, argv, "TROPO_LON0", -1, "-180", NULL);
55 double lon1 = scan_ctl(argv[1], argc, argv, "TROPO_LON1", -1, "180", NULL);
56 double dlon = scan_ctl(argv[1], argc, argv, "TROPO_DLON", -1, "-999", NULL);
57 double lat0 = scan_ctl(argv[1], argc, argv, "TROPO_LAT0", -1, "-90", NULL);
58 double lat1 = scan_ctl(argv[1], argc, argv, "TROPO_LAT1", -1, "90", NULL);
59 double dlat = scan_ctl(argv[1], argc, argv, "TROPO_DLAT", -1, "-999", NULL);
60 int h2o = (int) scan_ctl(argv[1], argc, argv, "TROPO_H2O", -1, "1", NULL);
61 int o3 = (int) scan_ctl(argv[1], argc, argv, "TROPO_O3", -1, "1", NULL);
62
63 /* Read climatological data... */
64 read_clim(&ctl, clim);
65
66 /* Loop over files... */
67 for (int i = 3; i < argc; i++) {
68
69 /* Set control parameters... */
70 ctl.met_tropo = 0;
71
72 /* Read meteorological data... */
73 if (!read_met(argv[i], &ctl, clim, met))
74 continue;
75
76 /* Set horizontal grid... */
77 if (!init) {
78 init = 1;
79
80 /* Get grid... */
81 if (dlon <= 0)
82 dlon = fabs(met->lon[1] - met->lon[0]);
83 if (dlat <= 0)
84 dlat = fabs(met->lat[1] - met->lat[0]);
85 if (lon0 < -360 && lon1 > 360) {
86 lon0 = gsl_stats_min(met->lon, 1, (size_t) met->nx);
87 lon1 = gsl_stats_max(met->lon, 1, (size_t) met->nx);
88 }
89 nx = ny = 0;
90 for (lon = lon0; lon <= lon1; lon += dlon) {
91 lons[nx] = lon;
92 if ((++nx) >= EX)
93 ERRMSG("Too many longitudes!");
94 }
95 if (lat0 < -90 && lat1 > 90) {
96 lat0 = gsl_stats_min(met->lat, 1, (size_t) met->ny);
97 lat1 = gsl_stats_max(met->lat, 1, (size_t) met->ny);
98 }
99 for (lat = lat0; lat <= lat1; lat += dlat) {
100 lats[ny] = lat;
101 if ((++ny) >= EY)
102 ERRMSG("Too many latitudes!");
103 }
104
105 /* Create netCDF file... */
106 LOG(1, "Write tropopause data file: %s", argv[2]);
107 NC(nc_create(argv[2], NC_NETCDF4, &ncid));
108
109 /* Create dimensions... */
110 NC(nc_def_dim(ncid, "time", (size_t) NC_UNLIMITED, &dims[0]));
111 NC(nc_def_dim(ncid, "lat", (size_t) ny, &dims[1]));
112 NC(nc_def_dim(ncid, "lon", (size_t) nx, &dims[2]));
113
114 /* Create variables... */
115 NC_DEF_VAR("time", NC_DOUBLE, 1, &dims[0], "time",
116 "seconds since 2000-01-01 00:00:00 UTC", 0, 0);
117 NC_DEF_VAR("lat", NC_DOUBLE, 1, &dims[1], "latitude", "degrees_north",
118 0, 0);
119 NC_DEF_VAR("lon", NC_DOUBLE, 1, &dims[2], "longitude", "degrees_east",
120 0, 0);
121
122 NC_DEF_VAR("clp_z", NC_FLOAT, 3, &dims[0], "cold point height", "km", 0,
123 0);
124 NC_DEF_VAR("clp_p", NC_FLOAT, 3, &dims[0], "cold point pressure", "hPa",
125 0, 0);
126 NC_DEF_VAR("clp_t", NC_FLOAT, 3, &dims[0], "cold point temperature",
127 "K", 0, 0);
128 if (h2o)
129 NC_DEF_VAR("clp_q", NC_FLOAT, 3, &dims[0], "cold point water vapor",
130 "ppv", 0, 0);
131 if (o3)
132 NC_DEF_VAR("clp_o3", NC_FLOAT, 3, &dims[0], "cold point ozone",
133 "ppv", 0, 0);
134
135 NC_DEF_VAR("dyn_z", NC_FLOAT, 3, &dims[0],
136 "dynamical tropopause height", "km", 0, 0);
137 NC_DEF_VAR("dyn_p", NC_FLOAT, 3, &dims[0],
138 "dynamical tropopause pressure", "hPa", 0, 0);
139 NC_DEF_VAR("dyn_t", NC_FLOAT, 3, &dims[0],
140 "dynamical tropopause temperature", "K", 0, 0);
141 if (h2o)
142 NC_DEF_VAR("dyn_q", NC_FLOAT, 3, &dims[0],
143 "dynamical tropopause water vapor", "ppv", 0, 0);
144 if (o3)
145 NC_DEF_VAR("dyn_o3", NC_FLOAT, 3, &dims[0],
146 "dynamical tropopause ozone", "ppv", 0, 0);
147
148 NC_DEF_VAR("wmo_1st_z", NC_FLOAT, 3, &dims[0],
149 "WMO 1st tropopause height", "km", 0, 0);
150 NC_DEF_VAR("wmo_1st_p", NC_FLOAT, 3, &dims[0],
151 "WMO 1st tropopause pressure", "hPa", 0, 0);
152 NC_DEF_VAR("wmo_1st_t", NC_FLOAT, 3, &dims[0],
153 "WMO 1st tropopause temperature", "K", 0, 0);
154 if (h2o)
155 NC_DEF_VAR("wmo_1st_q", NC_FLOAT, 3, &dims[0],
156 "WMO 1st tropopause water vapor", "ppv", 0, 0);
157 if (o3)
158 NC_DEF_VAR("wmo_1st_o3", NC_FLOAT, 3, &dims[0],
159 "WMO 1st tropopause ozone", "ppv", 0, 0);
160
161 NC_DEF_VAR("wmo_2nd_z", NC_FLOAT, 3, &dims[0],
162 "WMO 2nd tropopause height", "km", 0, 0);
163 NC_DEF_VAR("wmo_2nd_p", NC_FLOAT, 3, &dims[0],
164 "WMO 2nd tropopause pressure", "hPa", 0, 0);
165 NC_DEF_VAR("wmo_2nd_t", NC_FLOAT, 3, &dims[0],
166 "WMO 2nd tropopause temperature", "K", 0, 0);
167 if (h2o)
168 NC_DEF_VAR("wmo_2nd_q", NC_FLOAT, 3, &dims[0],
169 "WMO 2nd tropopause water vapor", "ppv", 0, 0);
170 if (o3)
171 NC_DEF_VAR("wmo_2nd_o3", NC_FLOAT, 3, &dims[0],
172 "WMO 2nd tropopause ozone", "ppv", 0, 0);
173
174 NC_DEF_VAR("ps", NC_FLOAT, 3, &dims[0], "surface pressure", "hPa", 0,
175 0);
176 NC_DEF_VAR("zs", NC_FLOAT, 3, &dims[0], "surface height", "km", 0, 0);
177
178 /* End definition... */
179 NC(nc_enddef(ncid));
180
181 /* Write longitude and latitude... */
182 NC_PUT_DOUBLE("lat", lats, 0);
183 NC_PUT_DOUBLE("lon", lons, 0);
184 }
185
186 /* Write time... */
187 start[0] = (size_t) nt;
188 count[0] = 1;
189 start[1] = 0;
190 count[1] = (size_t) ny;
191 start[2] = 0;
192 count[2] = (size_t) nx;
193 NC_PUT_DOUBLE("time", &met->time, 1);
194
195 /* Get cold point... */
196 get_tropo(2, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
197 zs);
198 NC_PUT_DOUBLE("clp_z", zt, 1);
199 NC_PUT_DOUBLE("clp_p", pt, 1);
200 NC_PUT_DOUBLE("clp_t", tt, 1);
201 if (h2o)
202 NC_PUT_DOUBLE("clp_q", qt, 1);
203 if (o3)
204 NC_PUT_DOUBLE("clp_o3", o3t, 1);
205
206 /* Get dynamical tropopause... */
207 get_tropo(5, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
208 zs);
209 NC_PUT_DOUBLE("dyn_z", zt, 1);
210 NC_PUT_DOUBLE("dyn_p", pt, 1);
211 NC_PUT_DOUBLE("dyn_t", tt, 1);
212 if (h2o)
213 NC_PUT_DOUBLE("dyn_q", qt, 1);
214 if (o3)
215 NC_PUT_DOUBLE("dyn_o3", o3t, 1);
216
217 /* Get WMO 1st tropopause... */
218 get_tropo(3, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
219 zs);
220 NC_PUT_DOUBLE("wmo_1st_z", zt, 1);
221 NC_PUT_DOUBLE("wmo_1st_p", pt, 1);
222 NC_PUT_DOUBLE("wmo_1st_t", tt, 1);
223 if (h2o)
224 NC_PUT_DOUBLE("wmo_1st_q", qt, 1);
225 if (o3)
226 NC_PUT_DOUBLE("wmo_1st_o3", o3t, 1);
227
228 /* Get WMO 2nd tropopause... */
229 get_tropo(4, &ctl, clim, met, lons, nx, lats, ny, pt, zt, tt, qt, o3t, ps,
230 zs);
231 NC_PUT_DOUBLE("wmo_2nd_z", zt, 1);
232 NC_PUT_DOUBLE("wmo_2nd_p", pt, 1);
233 NC_PUT_DOUBLE("wmo_2nd_t", tt, 1);
234 if (h2o)
235 NC_PUT_DOUBLE("wmo_2nd_q", qt, 1);
236 if (o3)
237 NC_PUT_DOUBLE("wmo_2nd_o3", o3t, 1);
238
239 /* Write surface data... */
240 NC_PUT_DOUBLE("ps", ps, 1);
241 NC_PUT_DOUBLE("zs", zs, 1);
242
243 /* Increment time step counter... */
244 nt++;
245 }
246
247 /* Close file... */
248 NC(nc_close(ncid));
249
250 /* Free... */
251 free(clim);
252 free(met);
253
254 return EXIT_SUCCESS;
255}
void get_tropo(const int met_tropo, ctl_t *ctl, clim_t *clim, met_t *met, const double *lons, const int nx, const double *lats, const int ny, double *pt, double *zt, double *tt, double *qt, double *o3t, double *ps, double *zs)
Calculate tropopause data.
Definition: mptrac.c:1239
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
int read_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:5655
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4473
#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 EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:262
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1831
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1030
#define NC_PUT_DOUBLE(varname, ptr, hyperslab)
Write double precision data to a NetCDF variable.
Definition: mptrac.h:1111
Climatological data.
Definition: mptrac.h:3282
Control parameters.
Definition: mptrac.h:2155
int met_tropo
Tropopause definition (0=none, 1=clim, 2=cold point, 3=WMO_1st, 4=WMO_2nd, 5=dynamical).
Definition: mptrac.h:2601
Meteo data structure.
Definition: mptrac.h:3341
int nx
Number of longitudes.
Definition: mptrac.h:3347
int ny
Number of latitudes.
Definition: mptrac.h:3350
double lon[EX]
Longitude [deg].
Definition: mptrac.h:3359
double time
Time [s].
Definition: mptrac.h:3344
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3362
Here is the call graph for this function: