JURASSIC
tblrdc.c
Go to the documentation of this file.
1/*
2 This file is part of JURASSIC.
3
4 JURASSIC is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 JURASSIC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with JURASSIC. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright (C) 2013-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "jurassic.h"
26
27/* ------------------------------------------------------------
28 Functions...
29 ------------------------------------------------------------ */
30
32static void copy_density(
33 tbl_t * tbl,
34 int id,
35 int ig,
36 int ip,
37 int it,
38 int dst,
39 int src);
40
42static void reduction(
43 tbl_t * tbl,
44 int id,
45 int ig,
46 int ip,
47 int it,
48 int left,
49 int right,
50 int *write_idx,
51 double atol,
52 double rtol);
53
54/* ------------------------------------------------------------
55 Main...
56 ------------------------------------------------------------ */
57
58int main(
59 int argc,
60 char *argv[]) {
61 ctl_t ctl;
62
63 /* Check arguments... */
64 if (argc < 6)
65 ERRMSG("tblrdc: Give parameters: <ctl> <tblbase_in> <tblfmt_in>"
66 " <tblbase_out> <tblfmt_out>");
67
68 /* Read control parameters... */
69 read_ctl(argc, argv, &ctl);
70 const double atol = scan_ctl(argc, argv, "ATOL", -1, "1e-9", NULL);
71 const double rtol = scan_ctl(argc, argv, "RTOL", -1, "0.01", NULL);
72
73 /* Read tables... */
74 sprintf(ctl.tblbase, "%s", argv[2]);
75 ctl.tblfmt = atoi(argv[3]);
76 tbl_t *tbl = read_tbl(&ctl);
77
78 /* Loop over trace gases and channels... */
79 for (int id = 0; id < ctl.nd; id++)
80 for (int ig = 0; ig < ctl.ng; ig++) {
81
82 /* Init counters... */
83 int total_input = 0;
84 int total_output = 0;
85
86 /* Loop over (p, T) combinations... */
87 for (int ip = 0; ip < tbl->np[id][ig]; ip++) {
88 for (int it = 0; it < tbl->nt[id][ig][ip]; it++) {
89
90 const int num_points = tbl->nu[id][ig][ip][it];
91
92 /* First entry is implicitly kept (in-place reduction)... */
93 int write_idx = 1;
94
95 /* Reduce Middle-Entries if more than 2 entries... */
96 if (num_points > 2)
97 reduction(tbl, id, ig, ip, it, 0, num_points - 1, &write_idx,
98 atol, rtol);
99
100 /* Keep last entry if more than 1 entry... */
101 if (num_points > 1) {
102 copy_density(tbl, id, ig, ip, it, write_idx, num_points - 1);
103 write_idx++;
104 }
105
106 /* Update number of kept points... */
107 tbl->nu[id][ig][ip][it] = write_idx;
108 total_input += num_points;
109 total_output += write_idx;
110 }
111 }
112
113 /* Write info... */
114 LOG(1, "Reduction: %s | %.4f cm^-1 | n= %d | CR= %g", ctl.emitter[ig],
115 ctl.nu[id], total_input,
116 total_output > 0 ? (double) total_input / total_output : NAN);
117
118 }
119
120 /* Write tables... */
121 sprintf(ctl.tblbase, "%s", argv[4]);
122 ctl.tblfmt = atoi(argv[5]);
123 write_tbl(&ctl, tbl);
124
125 /* Free... */
126 tbl_free(&ctl, tbl);
127
128 return EXIT_SUCCESS;
129}
130
131/*****************************************************************************/
132
133static void copy_density(
134 tbl_t *tbl,
135 int id,
136 int ig,
137 int ip,
138 int it,
139 int dst,
140 int src) {
141
142 tbl->logu[id][ig][ip][it][dst] = tbl->logu[id][ig][ip][it][src];
143 tbl->logeps[id][ig][ip][it][dst] = tbl->logeps[id][ig][ip][it][src];
144}
145
146/*****************************************************************************/
147
148static void reduction(
149 tbl_t *tbl,
150 int id,
151 int ig,
152 int ip,
153 int it,
154 int left,
155 int right,
156 int *write_idx,
157 double atol,
158 double rtol) {
159
160 int worstindex = -1;
161 double max_err = 0.0;
162
163 /* Scan interior points and find the largest interpolation error... */
164 for (int iu = left + 1; iu < right; iu++) {
165
166 /* Linear interpolation in log-log space: interpolate log(eps) linearly over log(u)... */
167 const double interp_logeps =
168 LIN(tbl->logu[id][ig][ip][it][left], tbl->logeps[id][ig][ip][it][left],
169 tbl->logu[id][ig][ip][it][right],
170 tbl->logeps[id][ig][ip][it][right],
171 tbl->logu[id][ig][ip][it][iu]);
172
173 /* Compute relative error in linear eps (eps = exp(logeps)) */
174 const double eps_i = exp((double) tbl->logeps[id][ig][ip][it][iu]);
175 const double interp_eps = exp(interp_logeps);
176 const double diff = fabs(eps_i - interp_eps);
177
178 /* Check interpolation errors and find maximum error... */
179 if (diff > atol + rtol * fabs(eps_i) && diff > max_err) {
180 max_err = diff;
181 worstindex = iu;
182 }
183 }
184
185 /* Check whether any point violated the error threshold... */
186 if (worstindex != -1) {
187
188 /* Recursion on left segment... */
189 if (worstindex - left > 1)
190 reduction(tbl, id, ig, ip, it, left, worstindex, write_idx, atol, rtol);
191
192 /* Keep worst entry... */
193 copy_density(tbl, id, ig, ip, it, *write_idx, worstindex);
194 (*write_idx)++;
195
196 /* Recursion on right segment... */
197 if (right - worstindex > 1)
198 reduction(tbl, id, ig, ip, it, worstindex, right, write_idx, atol,
199 rtol);
200 }
201}
void tbl_free(const ctl_t *ctl, tbl_t *tbl)
Free lookup table and all internally allocated memory.
Definition: jurassic.c:6648
void read_ctl(int argc, char *argv[], ctl_t *ctl)
Read model control parameters from command-line and configuration input.
Definition: jurassic.c:5450
void write_tbl(const ctl_t *ctl, const tbl_t *tbl)
Write emissivity lookup tables to disk.
Definition: jurassic.c:7976
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.
Definition: jurassic.c:6380
tbl_t * read_tbl(const ctl_t *ctl)
Read emissivity lookup tables from disk.
Definition: jurassic.c:6106
JURASSIC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: jurassic.h:1194
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: jurassic.h:1124
#define LIN(x0, y0, x1, y1, x)
Compute linear interpolation.
Definition: jurassic.h:544
Control parameters.
Definition: jurassic.h:1297
double nu[ND]
Centroid wavenumber of each channel [cm^-1].
Definition: jurassic.h:1321
char tblbase[LEN]
Basename for table files and filter function files.
Definition: jurassic.h:1348
int ng
Number of emitters.
Definition: jurassic.h:1300
int nd
Number of radiance channels.
Definition: jurassic.h:1318
char emitter[NG][LEN]
Name of each emitter.
Definition: jurassic.h:1303
int tblfmt
Look-up table file format (1=ASCII, 2=binary, 3=netCDF).
Definition: jurassic.h:1351
Emissivity look-up tables.
Definition: jurassic.h:1657
float * logu[ND][NG][TBLNP][TBLNT]
Logarithm of column density [molecules/cm^2].
Definition: jurassic.h:1675
int nu[ND][NG][TBLNP][TBLNT]
Number of column densities.
Definition: jurassic.h:1666
int nt[ND][NG][TBLNP]
Number of temperatures.
Definition: jurassic.h:1663
float * logeps[ND][NG][TBLNP][TBLNT]
Logarithm of emissivity.
Definition: jurassic.h:1678
int np[ND][NG]
Number of pressure levels.
Definition: jurassic.h:1660
int main(int argc, char *argv[])
Definition: tblrdc.c:58