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-2021 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/* Maximum line length: */
38#define MAXLINE 100000
39
40/* ------------------------------------------------------------
41 Main...
42 ------------------------------------------------------------ */
43
44int main(
45 int argc,
46 char *argv[]) {
47
48 FILE *in;
49
50 static char line[MAXLINE];
51
52 static double dnu, abs[MAXNPTS], epsold, f, filt[MAXNF],
53 nu, nu0, nu1, nuf[MAXNF], press, r0, temp, u;
54
55 static int i, i0, idx, nf, npts;
56
57 /* Read command line arguments... */
58 if (argc != 5)
59 ERRMSG("Give parameters: <press> <temp> <spec> <filter>");
60 sscanf(argv[1], "%lg", &press);
61 sscanf(argv[2], "%lg", &temp);
62
63 /* Compute column density [molec/cm^2] (1 km path length, 1 ppmv)... */
64 u = 1e-6 * press * 100 / (1.380658e-23 * temp) * 1000 / 1e4;
65
66 /* Read filter function... */
67 if (!(in = fopen(argv[4], "r")))
68 ERRMSG("Cannot open filter file!");
69 while (fgets(line, MAXLINE, in))
70 if (sscanf(line, "%lg %lg", &nuf[nf], &filt[nf]) == 2)
71 if (++nf >= MAXNF)
72 ERRMSG("Too many points in filter function");
73 fclose(in);
74
75 /* Read spectrum... */
76 if (!(in = fopen(argv[3], "r")))
77 ERRMSG("Cannot open spectrum!");
78 if (!fgets(line, MAXLINE, in))
79 ERRMSG("Error while reading spectrum!");
80 if (!fgets(line, MAXLINE, in))
81 ERRMSG("Error while reading spectrum!");
82 if (!fgets(line, MAXLINE, in))
83 ERRMSG("Error while reading spectrum!");
84 if (!fgets(line, MAXLINE, in))
85 ERRMSG("Error while reading spectrum!");
86 sscanf(line, "%d %lg %lg %lg", &npts, &nu0, &dnu, &nu1);
87 if (npts > MAXNPTS)
88 ERRMSG("Too many points in optical depth spectrum!");
89 i = 0;
90 while (fgets(line, MAXLINE, in)) {
91 char *tok;
92 if ((tok = strtok(line, " \t\n")) != NULL) {
93 sscanf(tok, "%lg", &abs[i]);
94 abs[i] /= u;
95 i++;
96 }
97 while ((tok = strtok(NULL, " \t\n")) != NULL) {
98 sscanf(tok, "%lg", &abs[i]);
99 abs[i] /= u;
100 i++;
101 }
102 }
103 fclose(in);
104
105 /* Set grid spacing... */
106 dnu = (nu1 - nu0) / ((double) npts - 1.0);
107 r0 = (nuf[0] - nu0) / (nu1 - nu0) * (double) npts;
108 i0 = (int) r0;
109
110 /* Loop over column densities... */
111 for (u = 1.0; u <= 1e30; u *= 1.122) {
112
113 /* Integrate... */
114 double epssum = 0, fsum = 0;
115 for (i = i0; i < npts; i++) {
116 nu = nu0 + dnu * (double) i;
117 if (nu < nuf[0])
118 continue;
119 else if (nu > nuf[nf - 1])
120 break;
121 else {
122 if (nu < nuf[idx] || nu > nuf[idx + 1])
123 idx = locate_irr(nuf, nf, nu);
124 f = LIN(nuf[idx], filt[idx], nuf[idx + 1], filt[idx + 1], nu);
125 fsum += f;
126 epssum += f * exp(-abs[i] * u);
127 }
128 }
129 epssum = 1 - epssum / fsum;
130
131 /* Write output... */
132 if (epssum >= 1e-6 && epssum <= 0.999999 && epssum > epsold)
133 printf("%g %g %g %g\n", press, temp, u, epssum);
134 epsold = epssum;
135
136 /* Check for termination... */
137 if (epssum > 0.999999)
138 return EXIT_SUCCESS;
139 }
140
141 return EXIT_SUCCESS;
142}
int locate_irr(const double *xx, const int n, const double x)
Find array index for irregular grid.
Definition: jurassic.c:4107
JURASSIC library declarations.
#define ERRMSG(...)
Print error message and quit program.
Definition: jurassic.h:237
#define LIN(x0, y0, x1, y1, x)
Compute linear interpolation.
Definition: jurassic.h:164
int main(int argc, char *argv[])
Definition: tblgen.c:44
#define MAXLINE
Definition: tblgen.c:38
#define MAXNF
Definition: tblgen.c:32
#define MAXNPTS
Definition: tblgen.c:35