JURASSIC
tblgen.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) 2003-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "jurassic.h"
26
27/* ------------------------------------------------------------
28 Dimensions...
29 ------------------------------------------------------------ */
30
31/* Maximum number of grid points for filter files: */
32#define MAXNF 20000
33
34/* Maximum number of grid points for spectra: */
35#define MAXNPTS 10000000
36
37/* ------------------------------------------------------------
38 Functions...
39 ------------------------------------------------------------ */
40
42static void usage(
43 void);
44
45/* ------------------------------------------------------------
46 Main...
47 ------------------------------------------------------------ */
48
49int main(
50 int argc,
51 char *argv[]) {
52
53 FILE *in;
54
55 static char *line = NULL;
56
57 static double dnu, abs[MAXNPTS], epsold, f, filt[MAXNF],
58 nu, nu0, nu1, nuf[MAXNF], press, temp, u;
59
60 static int i, idx, nf, npts;
61
62 size_t line_buf_size = 0;
63
64 /* Print usage information... */
65 USAGE;
66
67 /* Read command line arguments... */
68 if (argc != 5)
69 ERRMSG("Missing or invalid command-line arguments.\n\n"
70 "Usage: tblgen <press> <temp> <spec> <filter>\n\n"
71 "Use -h for full help.");
72 sscanf(argv[1], "%lg", &press);
73 sscanf(argv[2], "%lg", &temp);
74
75 /* Compute column density [molec/cm^2] (1 km path length, 1 ppmv)... */
76 u = 1e-6 * press * 100 / (1.380658e-23 * temp) * 1000 / 1e4;
77
78 /* Read filter function... */
79 if (!(in = fopen(argv[4], "r")))
80 ERRMSG("Cannot open filter file!");
81 while (getline(&line, &line_buf_size, in) != -1)
82 if (sscanf(line, "%lg %lg", &nuf[nf], &filt[nf]) == 2)
83 if (++nf >= MAXNF)
84 ERRMSG("Too many points in filter function");
85 fclose(in);
86
87 /* Read spectrum... */
88 if (!(in = fopen(argv[3], "r")))
89 ERRMSG("Cannot open spectrum!");
90 for (i = 0; i < 4; ++i)
91 if (getline(&line, &line_buf_size, in) == -1)
92 ERRMSG("Error while reading spectrum!");
93 sscanf(line, "%d %lg %lg %lg", &npts, &nu0, &dnu, &nu1);
94 if (npts > MAXNPTS)
95 ERRMSG("Too many points in optical depth spectrum!");
96 i = 0;
97 while (getline(&line, &line_buf_size, in) != -1) {
98 char *tok = strtok(line, " \t\n");
99 while (tok != NULL) {
100 if (i >= MAXNPTS)
101 ERRMSG("Too many data points in spectrum file!");
102 sscanf(tok, "%lg", &abs[i]);
103 abs[i] /= u;
104 ++i;
105 tok = strtok(NULL, " \t\n");
106 }
107 }
108 fclose(in);
109
110 /* Set grid spacing... */
111 dnu = (nu1 - nu0) / ((double) npts - 1.0);
112 const double r0 = (nuf[0] - nu0) / (nu1 - nu0) * (double) npts;
113 const int i0 = (int) r0;
114
115 /* Loop over column densities... */
116 for (u = 1.0; u <= 1e30; u *= 1.122) {
117
118 /* Integrate... */
119 double epssum = 0, fsum = 0;
120 for (i = i0; i < npts; i++) {
121 nu = nu0 + dnu * (double) i;
122 if (nu < nuf[0])
123 continue;
124 else if (nu > nuf[nf - 1])
125 break;
126 else {
127 if (nu < nuf[idx] || nu > nuf[idx + 1])
128 idx = locate_irr(nuf, nf, nu);
129 f = LIN(nuf[idx], filt[idx], nuf[idx + 1], filt[idx + 1], nu);
130 fsum += f;
131 epssum += f * exp(-abs[i] * u);
132 }
133 }
134 epssum = 1 - epssum / fsum;
135
136 /* Write output... */
137 if (epssum >= 1e-6 && epssum <= 0.999999 && epssum > epsold)
138 printf("%g %g %g %g\n", press, temp, u, epssum);
139 epsold = epssum;
140
141 /* Check for termination... */
142 if (epssum > 0.999999)
143 return EXIT_SUCCESS;
144 }
145
146 return EXIT_SUCCESS;
147}
148
149/*****************************************************************************/
150
151static void usage(
152 void) {
153 printf("\nJURASSIC lookup-table generator.\n\n");
154 printf("Prepare transmission look-up table entries from monochromatic\n");
155 printf("absorption spectra and a filter function.\n\n");
156 printf("Usage:\n");
157 printf(" tblgen <press> <temp> <spec> <filter>\n\n");
158 printf("Arguments:\n");
159 printf(" <press> Pressure [hPa].\n");
160 printf(" <temp> Temperature [K].\n");
161 printf(" <spec> Monochromatic absorption spectrum file.\n");
162 printf(" <filter> Filter-function file.\n\n");
163 printf("Output:\n");
164 printf(" Writes results to standard output.\n\n");
165 printf("Further information:\n");
166 printf(" Manual: https://slcs-jsc.github.io/jurassic/\n");
167}
void usage(void)
Print command-line help.
Definition: formod.c:163
int locate_irr(const double *xx, const int n, const double x)
Locate index for interpolation on an irregular grid.
Definition: jurassic.c:4481
JURASSIC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: jurassic.h:1325
#define USAGE
Print usage information on -h or --help.
Definition: jurassic.h:1206
#define LIN(x0, y0, x1, y1, x)
Compute linear interpolation.
Definition: jurassic.h:624
int main(int argc, char *argv[])
Definition: tblgen.c:49
#define MAXNF
Definition: tblgen.c:32
#define MAXNPTS
Definition: tblgen.c:35