52 {
53
55
56 static atm_t atm, atm2;
57
58 static obs_t obs, obs_scratch;
59 static los_t los_scratch;
60
61 static FILE *in, *out;
62
63 static char line[
LEN];
64
66 scl = 1.0, scl_err, c0, c1, cov00, cov01, cov11, sumsq,
69
72
73 static int i, ig, n, nl, ndata[
NMAX], nprof;
74
75
77
78
79 if (argc < 6)
80 ERRMSG(
"Missing or invalid command-line arguments.\n\n"
81 "Usage: invert <ctl> <prof> <inv> <atm> <kernel> [KEY VALUE ...]\n\n"
82 "Use -h for full help.");
83
84
86 const double dt =
scan_ctl(argc, argv,
"INVERT_DT", -1,
"86400", NULL);
87 const double obs_err =
88 scan_ctl(argc, argv,
"INVERT_OBS_ERR", -1,
"1.0", NULL);
89 const int data = (int)
scan_ctl(argc, argv,
"INVERT_DATA", -1,
"2", NULL);
90 const int fit = (int)
scan_ctl(argc, argv,
"INVERT_FIT", -1,
"3", NULL);
91 const int itmax =
92 (int)
scan_ctl(argc, argv,
"INVERT_ITMAX", -1,
"10", NULL);
93 const double tol =
scan_ctl(argc, argv,
"INVERT_TOL", -1,
"1e-4", NULL);
94
95
98 if (strcmp(ctl.
emitter[0],
"SO2") != 0)
99 ERRMSG(
"Set EMITTER[0] = SO2!");
100 if (strcmp(ctl.
emitter[1],
"H2O") != 0)
101 ERRMSG(
"Set EMITTER[1] = H2O!");
102 if (strcmp(ctl.
emitter[2],
"O3") != 0)
103 ERRMSG(
"Set EMITTER[2] = O3!");
104 if (strcmp(ctl.
emitter[3],
"CO2") != 0)
105 ERRMSG(
"Set EMITTER[3] = CO2!");
108
109
112
113
116
117
119
120
121
122
123
124
125 LOG(1,
"Read profile data: %s", argv[2]);
126
127
128 if (!(in = fopen(argv[2], "r")))
129 ERRMSG(
"Cannot open file!");
130
131
132 while (fgets(line,
LEN, in)) {
133
134
135 if (sscanf(line, "%lg %lg %lg %lg %g %g %g %g %g %g", &rtime[nl],
136 &rz[nl], &rlon[nl], &rlat[nl], &rp[nl], &rt[nl], &rso2[nl],
137 &rh2o[nl], &ro3[nl], &robs[nl]) != 10)
138 continue;
140 ERRMSG(
"Too many profile data points!");
141 }
142
143
144 fclose(in);
145
146
147
148
149
150
151 for (int it = 0; it < itmax; it++) {
152
153
155 for (i = 0; i <
NMAX; i++) {
156 ndata[i] = 0;
157 x[i] = y[i] = NAN;
158 }
159
160
161 for (int il = 0; il < nl; il++) {
162
163
164 if ((rtime[il] != atm.
time[0]
165 || rlon[il] != atm.
lon[0]
166 || rlat[il] != atm.
lat[0])
168
169
170 if (
formod(&ctl, tbl, &atm, &obs, &los_scratch, &obs_scratch)
172 ERRMSG(
"Forward model failed!");
173 const double obs_sim = obs.
rad[0][0] - obs.
rad[1][0];
174
175
176 i = (int) ((atm.
time[0] - rtime[0]) / dt);
177 if (i < 0 || i >=
NMAX)
178 ERRMSG(
"Time index out of range!");
179
180
181 if (data == 1) {
182 x[i] = (isfinite(x[i]) ?
MAX(x[i], obs_sim) : obs_sim);
183 y[i] = (isfinite(y[i]) ?
MAX(y[i], obs_meas) : obs_meas);
184 y_err[i] = obs_err;
185 if (isfinite(x[i]) && isfinite(y[i]))
186 ndata[i] = 1;
187 }
188
189
190 else if (data == 2) {
191 if (ndata[i] == 0) {
192 x[i] = obs_sim;
193 y[i] = obs_meas;
194 y_err[i] =
POW2(obs_meas);
195 } else {
196 x[i] += obs_sim;
197 y[i] += obs_meas;
198 y_err[i] +=
POW2(obs_meas);
199 }
200 ndata[i]++;
201 }
202
203
204 nprof++;
206 for (
int ip = 0; ip < atm.
np; ip++) {
208 atm2.
z[ip] += atm.
z[ip];
209 atm2.
lon[ip] += atm.
lon[ip];
210 atm2.
lat[ip] += atm.
lat[ip];
211 atm2.
p[ip] += atm.
p[ip];
212 atm2.
t[ip] += atm.
t[ip];
213 for (ig = 1; ig < ctl.
ng; ig++)
214 atm2.
q[ig][ip] += atm.
q[ig][ip];
215 }
216
217
219 }
220
221
222 obs_meas = robs[il];
223 atm.
time[atm.
np] = rtime[il];
224 atm.
z[atm.
np] = rz[il];
225 atm.
lon[atm.
np] = rlon[il];
226 atm.
lat[atm.
np] = rlat[il];
227 atm.
p[atm.
np] = rp[il];
228 atm.
t[atm.
np] = rt[il];
229 atm.
q[0][atm.
np] = rso2[il] * scl;
230 atm.
q[1][atm.
np] = rh2o[il];
231 atm.
q[2][atm.
np] = ro3[il];
234 ERRMSG(
"Too many data points!");
235 }
236
237
238 if (data == 2)
239 for (i = 0; i <
NMAX; i++)
240 if (ndata[i] > 0) {
241 x[i] /= ndata[i];
242 y[i] /= ndata[i];
243 y_err[i] = sqrt(
MAX(y_err[i] / ndata[i] -
POW2(y[i]), 0.0))
244 / sqrt(ndata[i]);
245 }
246
247
248 n = 0;
249 for (i = 0; i <
NMAX; i++)
250 if (ndata[i] > 0 && isfinite(x[i]) && isfinite(y[i])
251 && isfinite(y_err[i])) {
252 x2[n] = x[i];
253 y2[n] = y[i];
254 y2_err[n] = y_err[i];
255 w2[n] = 1. /
POW2(y_err[i]);
256 n++;
257 }
258
259
260 if (fit == 1)
261 gsl_fit_mul(x2, 1, y2, 1, (size_t) n, &c1, &cov11, &sumsq);
262 else if (fit == 2)
263 gsl_fit_wmul(x2, 1, w2, 1, y2, 1, (size_t) n, &c1, &cov11, &sumsq);
264 else if (fit == 3)
265 gsl_fit_linear(x2, 1, y2, 1, (size_t) n, &c0, &c1, &cov00, &cov01,
266 &cov11, &sumsq);
267 else if (fit == 4)
268 gsl_fit_wlinear(x2, 1, w2, 1, y2, 1, (size_t) n, &c0, &c1, &cov00,
269 &cov01, &cov11, &sumsq);
270 else
271 ERRMSG(
"Check INVERT_FIT!");
272
273
274 double scl_old = scl;
275 scl_err = scl * sqrt(cov11);
276 scl *= c1;
277
278
279 LOG(1,
" it= %d | scl= %g +/- %g | RMSE= %g",
280 it, scl, scl_err, sqrt(sumsq / n));
281
282
283 if (fabs(2.0 * (scl - scl_old) / (scl + scl_old)) < tol)
284 break;
285 }
286
287
288
289
290
291
292 LOG(1,
"Write inversion data: %s", argv[3]);
293
294
295 if (!(out = fopen(argv[3], "w")))
296 ERRMSG(
"Cannot create file!");
297
298
299 fprintf(out,
300 "# $1 = time (seconds since 2000-01-01T00:00Z)\n"
301 "# $2 = simulated SO2 index [K]\n"
302 "# $3 = scaled simulated SO2 index [K]\n"
303 "# $4 = error of scaled simulated SO2 index [K]\n"
304 "# $5 = observed SO2 index [K]\n"
305 "# $6 = error of observed SO2 index [K]\n\n");
306
307
308 for (i = 0; i < n; i++) {
309
310
311 if (fit == 1 || fit == 2)
312 gsl_fit_mul_est(x2[i], c1, cov11, &y2_sim[i], &y2_sim_err[i]);
313 else if (fit == 3 || fit == 4)
314 gsl_fit_linear_est(x2[i], c0, c1, cov00, cov01, cov11, &y2_sim[i],
315 &y2_sim_err[i]);
316
317
318 fprintf(out, "%.2f %g %g %g %g %g\n", rtime[0] + (i + 0.5) * dt,
319 x2[i], y2_sim[i], y2_sim_err[i], y2[i], y2_err[i]);
320 }
321
322
323 fprintf(out, "\n");
324 fprintf(out, "# scl= %g +/- %g\n", scl, scl_err);
325 fprintf(out, "# c1= %g +/- %g\n", c1, sqrt(cov11));
326 if (fit == 3 || fit == 4) {
327 fprintf(out, "# c0= %g +/- %g\n", c0, sqrt(cov00));
328 fprintf(out, "# corr= %g\n", cov01 / (sqrt(cov00) * sqrt(cov11)));
329 }
330 fprintf(out, "# RMSE= %g\n", sqrt(sumsq / n));
331 fprintf(out, "# n= %d\n", n);
332
333
334 fclose(out);
335
336
337
338
339
340
341 for (
int ip = 0; ip < atm2.
np; ip++) {
342 atm2.
time[ip] /= nprof;
344 atm2.
lon[ip] /= nprof;
345 atm2.
lat[ip] /= nprof;
348 for (ig = 0; ig < ctl.
ng; ig++)
349 atm2.
q[ig][ip] /= nprof;
350 }
351
352
353 const size_t nk =
atm2x(&ctl, &atm2, NULL, NULL, NULL);
354 const size_t mk =
obs2y(&ctl, &obs, NULL, NULL, NULL);
355
356
357 gsl_matrix *k = gsl_matrix_alloc(mk, nk);
358
359
360 kernel(&ctl, tbl, &atm2, &obs, k);
361
362
364
365
366 write_matrix(NULL, argv[5], &ctl, k, &atm2, &obs,
"y",
"x",
"r", 0);
367
368
369 gsl_matrix_free(k);
371
372 return EXIT_SUCCESS;
373}
#define NMAX
Maximum number of data points...
#define NLMAX
Maximum number of data lines...
void write_atm(const char *dirname, const char *filename, const ctl_t *ctl, const atm_t *atm, int profile)
Write atmospheric data to a file.
void tbl_free(const ctl_t *ctl, tbl_t *tbl)
Free lookup table and all internally allocated memory.
void read_ctl(int argc, char *argv[], ctl_t *ctl)
Read model control parameters from command-line and configuration input.
void write_matrix(const char *dirname, const char *filename, const ctl_t *ctl, const gsl_matrix *matrix, const atm_t *atm, const obs_t *obs, const char *rowspace, const char *colspace, const char *sort, int dataset)
Write a fully annotated matrix (e.g., Jacobian or gain matrix) to file.
void kernel(const ctl_t *ctl, const tbl_t *tbl, atm_t *atm, obs_t *obs, gsl_matrix *k)
Compute the Jacobian (kernel) matrix by finite differences.
double scan_ctl(int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Scan control file or command-line arguments for a configuration variable.
tbl_t * read_tbl(const ctl_t *ctl)
Read emissivity lookup tables from disk.
size_t obs2y(const ctl_t *ctl, const obs_t *obs, gsl_vector *y, int *ida, int *ira)
Convert observation radiances into a measurement vector.
size_t atm2x(const ctl_t *ctl, const atm_t *atm, gsl_vector *x, int *iqa, int *ipa)
Convert atmospheric data to state vector elements.
int formod(const ctl_t *ctl, const tbl_t *tbl, atm_t *atm, obs_t *obs, los_t *los_scratch, obs_t *obs_scratch)
Execute one complete forward-model calculation.
#define LEN
Maximum length of ASCII data lines.
#define POW2(x)
Compute the square of a value.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define USAGE
Print usage information on -h or --help.
#define CO2_FIT(TIME)
Climatological CO2 volume mixing ratio as a quadratic function of time.
#define NP
Maximum number of atmospheric data points.
#define LOG(level,...)
Print a log message with a specified logging level.
#define MAX(a, b)
Determine the maximum of two values.
Atmospheric profile data.
double time[NP]
Time (seconds since 2000-01-01T00:00Z).
double lat[NP]
Latitude [deg].
double lon[NP]
Longitude [deg].
double t[NP]
Temperature [K].
int np
Number of data points.
double z[NP]
Altitude [km].
double q[NG][NP]
Volume mixing ratio [ppv].
double p[NP]
Pressure [hPa].
int write_matrix
Write matrix file (0=no, 1=yes).
int ng
Number of emitters.
int nd
Number of radiance channels.
char emitter[NG][LEN]
Name of each emitter.
int write_bbt
Use brightness temperature instead of radiance (0=no, 1=yes).
Observation geometry and radiance data.
double rad[ND][NR]
Radiance [W/(m^2 sr cm^-1)].
double obsz[NR]
Observer altitude [km].
int nr
Number of ray paths.
Emissivity look-up tables.