JURASSIC
retrieval.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#ifdef MPI
28#include <mpi.h>
29#endif
30
31/* ------------------------------------------------------------
32 Functions...
33 ------------------------------------------------------------ */
34
36void usage(
37 void);
38
39/* ------------------------------------------------------------
40 Main...
41 ------------------------------------------------------------ */
42
43int main(
44 int argc,
45 char *argv[]) {
46
47 static atm_t atm_i, atm_apr;
48 static ctl_t ctl;
49 static obs_t obs_i, obs_meas;
50 static ret_t ret;
51
52 FILE *dirlist;
53 FILE *proflist = NULL;
54
55 /* MPI task distribution (optional)... */
56 int ntask = -1;
57 int rank = 0;
58 int size = 1;
59
60#ifdef MPI
61 MPI_Init(&argc, &argv);
62 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
63 MPI_Comm_size(MPI_COMM_WORLD, &size);
64#endif
65
66 /* Print usage information... */
67 USAGE;
68
69 /* Check arguments... */
70 if (argc < 3)
71 ERRMSG("Missing or invalid command-line arguments.\n\n"
72 "Usage: retrieval <ctl> <dirlist> [KEY VALUE ...]\n\n"
73 "Use -h for full help.");
74
75 /* Read control parameters... */
76 SELECT_TIMER("READ_CTL", "INPUT");
77 read_ctl(argc, argv, &ctl);
78 SELECT_TIMER("READ_RET", "INPUT");
79 read_ret(argc, argv, &ctl, &ret);
80
81 /* Initialize look-up tables... */
82 SELECT_TIMER("READ_TBL", "INPUT");
83 tbl_t *tbl = read_tbl(&ctl);
84
85 /* Open directory list... */
86 SELECT_TIMER("OPEN_CASELISTS", "INPUT");
87 dirlist = NULL;
88 if (argv[2][0] != '-') {
89 if (!(dirlist = fopen(argv[2], "r")))
90 ERRMSG("Cannot open directory list!");
91 }
92 if (ret.shared_io_proflist[0] != '-')
93 if (!(proflist = fopen(ret.shared_io_proflist, "r")))
94 ERRMSG("Cannot open profile list!");
95 if (dirlist == NULL && proflist == NULL)
96 ERRMSG("Give a directory list or set SHARED_IO_PROFLIST!");
97
98 /* Loop over retrieval cases... */
99 while (1) {
100
101 int have_dir = 0, have_profile = 0;
102
103 if (dirlist != NULL)
104 have_dir = fscanf(dirlist, "%4999s", ret.dir) == 1;
105 else {
106 sprintf(ret.dir, ".");
107 have_dir = 1;
108 }
109
110 if (proflist != NULL)
111 have_profile = fscanf(proflist, "%d", &ret.shared_io_profile) == 1;
112 else {
113 ret.shared_io_profile = 0;
114 have_profile = 1;
115 }
116
117 if ((dirlist != NULL && !have_dir) || (proflist != NULL && !have_profile)) {
118 if ((dirlist != NULL && have_dir) || (proflist != NULL && have_profile))
119 ERRMSG("DIRLIST and SHARED_IO_PROFLIST have different lengths!");
120 break;
121 }
122
123 /* Distribute directories with MPI (optional)... */
124 if ((++ntask) % size != rank)
125 continue;
126
127 /* Write info... */
128 if (size > 1 && proflist != NULL && dirlist != NULL) {
129 LOG(1,
130 "\nRetrieve profile %d in directory %s on rank %d of %d...\n",
131 ret.shared_io_profile, ret.dir, rank + 1, size);
132 } else if (size > 1 && proflist != NULL) {
133 LOG(1, "\nRetrieve profile %d on rank %d of %d...\n",
134 ret.shared_io_profile, rank + 1, size);
135 } else if (size > 1) {
136 LOG(1, "\nRetrieve in directory %s on rank %d of %d...\n",
137 ret.dir, rank + 1, size);
138 } else if (proflist != NULL && dirlist != NULL) {
139 LOG(1, "\nRetrieve profile %d in directory %s...\n",
140 ret.shared_io_profile, ret.dir);
141 } else if (proflist != NULL) {
142 LOG(1, "\nRetrieve profile %d...\n", ret.shared_io_profile);
143 } else
144 LOG(1, "\nRetrieve in directory %s...\n", ret.dir);
145
146 /* Read atmospheric data... */
147 SELECT_TIMER("READ_ATM", "INPUT");
148 const char *dirname;
149 int profile;
150 const char *filename =
151 shared_io_input_target(&ret, ret.shared_io_atm_apr_file, "atm_apr.tab",
152 &dirname, &profile);
153 read_atm(dirname, filename, &ctl, &atm_apr, profile);
154
155 /* Read observation data... */
156 SELECT_TIMER("READ_OBS", "INPUT");
157 filename =
159 "obs_meas.tab", &dirname, &profile);
160 read_obs(dirname, filename, &ctl, &obs_meas, profile);
161
162 /* Run retrieval... */
163 double chisq;
164 optimal_estimation(&ret, &ctl, tbl, &obs_meas, &obs_i, &atm_apr, &atm_i,
165 &chisq);
166 }
167
168 /* Write info... */
169 SELECT_TIMER("FINALIZE", "OVERHEAD");
170 LOG(1, "\nRetrieval done...");
171
172 /* Free... */
173 if (dirlist != NULL)
174 fclose(dirlist);
175 if (proflist != NULL)
176 fclose(proflist);
177 tbl_free(&ctl, tbl);
178
179#ifdef MPI
180 MPI_Finalize();
181#endif
182
184
185 return EXIT_SUCCESS;
186}
187
188/*****************************************************************************/
189
190void usage(
191 void) {
192
193 printf("\nJURASSIC retrieval tool.\n\n");
194 printf("Run optimal-estimation retrievals from measured observations,\n");
195 printf("a priori atmospheric input, and control settings.\n\n");
196 printf("Usage:\n");
197 printf(" retrieval <ctl> <dirlist> [KEY VALUE ...]\n\n");
198 printf("Arguments:\n");
199 printf(" <ctl> Control file.\n");
200 printf(" <dirlist> File containing working directories to process.\n");
201 printf(" Use '-' together with shared I/O settings such as\n");
202 printf(" SHARED_IO_PROFLIST for shared-file workflows.\n");
203 printf(" [KEY VALUE] Optional control parameters.\n\n");
204 printf("Tool-specific control parameters:\n");
205 printf(" KERNEL_RECOMP Kernel recomputation mode.\n");
206 printf(" CONV_* Convergence settings.\n");
207 printf(" ERR_* Retrieval error and covariance settings.\n");
208 printf
209 (" SHARED_IO_PROFLIST Profile-index file for shared-file retrievals.\n");
210 printf(" SHARED_IO_* Shared input and output filenames.\n");
211 printf("\n");
212 printf("Common control parameters:\n");
213 printf
214 (" TBLBASE, TBLFMT Lookup-table base name and format.\n");
215 printf(" ATMFMT, OBSFMT, MATRIXFMT Input/output file formats.\n");
216 printf(" NG, EMITTER[i] Active emitters.\n");
217 printf(" ND, NU[i], NW, WINDOW[i] Spectral channels and windows.\n");
218 printf
219 (" NCL, CLNU[i], NSF, SFNU[i] Cloud and surface spectral grids.\n");
220 printf(" RET*_ZMIN, RET*_ZMAX State-vector altitude limits.\n");
221 printf
222 (" WRITE_BBT, FORMOD Output units and forward-model selection.\n");
223 printf(" CTM_*, REFRAC Continua and refractivity.\n");
224 printf
225 (" RAYDS, RAYDZ, FOV Ray tracing and field of view.\n\n");
226 printf("Further information:\n");
227 printf(" Manual: https://slcs-jsc.github.io/jurassic/\n");
228}
void tbl_free(const ctl_t *ctl, tbl_t *tbl)
Free lookup table and all internally allocated memory.
Definition: jurassic.c:7002
void read_ctl(int argc, char *argv[], ctl_t *ctl)
Read model control parameters from command-line and configuration input.
Definition: jurassic.c:5516
void read_obs(const char *dirname, const char *filename, const ctl_t *ctl, obs_t *obs, int profile)
Read observation data from an input file.
Definition: jurassic.c:5810
const char * shared_io_input_target(const ret_t *ret, const char *shared_file, const char *legacy_file, const char **dirname, int *profile)
Resolve retrieval input target for legacy-directory and shared-file modes.
Definition: jurassic.c:6840
void optimal_estimation(ret_t *ret, ctl_t *ctl, tbl_t *tbl, obs_t *obs_meas, obs_t *obs_i, atm_t *atm_apr, atm_t *atm_i, double *chisq)
Perform optimal estimation retrieval using Levenberg–Marquardt minimization.
Definition: jurassic.c:4654
void read_atm(const char *dirname, const char *filename, const ctl_t *ctl, atm_t *atm, int profile)
Read atmospheric input data from a file.
Definition: jurassic.c:5193
void read_ret(int argc, char *argv[], const ctl_t *ctl, ret_t *ret)
Read retrieval configuration and error parameters.
Definition: jurassic.c:6143
tbl_t * read_tbl(const ctl_t *ctl)
Read emissivity lookup tables from disk.
Definition: jurassic.c:6332
JURASSIC library declarations.
#define SELECT_TIMER(id, group)
Switch to a named aggregated timer.
Definition: jurassic.h:1158
#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 PRINT_TIMERS
Print aggregated timer statistics.
Definition: jurassic.h:1168
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: jurassic.h:1255
int main(int argc, char *argv[])
Definition: retrieval.c:43
void usage(void)
Print command-line help.
Definition: retrieval.c:190
Atmospheric profile data.
Definition: jurassic.h:1375
Control parameters.
Definition: jurassic.h:1428
Observation geometry and radiance data.
Definition: jurassic.h:1657
Retrieval control parameters.
Definition: jurassic.h:1709
char shared_io_proflist[LEN]
Optional file containing retrieval profile indices.
Definition: jurassic.h:1787
char shared_io_obs_meas_file[LEN]
Optional shared measured-observation input file.
Definition: jurassic.h:1793
char shared_io_atm_apr_file[LEN]
Optional shared atmospheric a priori input file.
Definition: jurassic.h:1790
char dir[LEN]
Working directory.
Definition: jurassic.h:1712
int shared_io_profile
Profile index used for shared netCDF retrieval inputs and outputs.
Definition: jurassic.h:1784
Emissivity look-up tables.
Definition: jurassic.h:1842