JURASSIC
matfmt.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 usage(
33 void);
34
35/* ------------------------------------------------------------
36 Main...
37 ------------------------------------------------------------ */
38
39int main(
40 int argc,
41 char *argv[]) {
42
43 ctl_t ctl;
44 int prof_in, prof_out;
45
46 static atm_t atm;
47 static obs_t obs;
48
49 gsl_matrix *matrix;
50
51 size_t nr, nc;
52
53 /* Print usage information... */
54 USAGE;
55
56 /* Check arguments... */
57 if (argc < 11)
58 ERRMSG("Missing or invalid command-line arguments.\n\n"
59 "Usage: matfmt <ctl> <atm> <obs> <rowspace> <colspace> <sort> <matrix_in> <matrixfmt_in> <matrix_out> <matrixfmt_out> [KEY VALUE ...]\n\n"
60 "Use -h for full help.");
61
62 /* Read control parameters... */
63 read_ctl(argc, argv, &ctl);
64
65 /* Read dataset indices... */
66 prof_in = (int) scan_ctl(argc, argv, "PROF_IN", -1, "0", NULL);
67 prof_out = (int) scan_ctl(argc, argv, "PROF_OUT", -1, "0", NULL);
68
69 /* Check matrix metadata... */
70 if (argv[4][0] != 'x' && argv[4][0] != 'y')
71 ERRMSG("Unknown row space, use x or y!");
72 if (argv[5][0] != 'x' && argv[5][0] != 'y')
73 ERRMSG("Unknown column space, use x or y!");
74 if (argv[6][0] != 'r' && argv[6][0] != 'c')
75 ERRMSG("Unknown sort order, use r or c!");
76
77 /* Read atmospheric data if needed... */
78 if (argv[4][0] == 'x' || argv[5][0] == 'x')
79 read_atm(NULL, argv[2], &ctl, &atm, 0);
80
81 /* Read observation data if needed... */
82 if (argv[4][0] == 'y' || argv[5][0] == 'y')
83 read_obs(NULL, argv[3], &ctl, &obs, 0);
84
85 /* Determine matrix size... */
86 if (argv[4][0] == 'y')
87 nr = obs2y(&ctl, &obs, NULL, NULL, NULL);
88 else
89 nr = atm2x(&ctl, &atm, NULL, NULL, NULL);
90
91 if (argv[5][0] == 'y')
92 nc = obs2y(&ctl, &obs, NULL, NULL, NULL);
93 else
94 nc = atm2x(&ctl, &atm, NULL, NULL, NULL);
95
96 /* Allocate matrix... */
97 matrix = gsl_matrix_alloc(nr, nc);
98
99 /* Read matrix data... */
100 ctl.matrixfmt = atoi(argv[8]);
101 read_matrix(NULL, argv[7], &ctl, matrix, prof_in);
102
103 /* Write matrix data... */
104 ctl.write_matrix = 1;
105 ctl.matrixfmt = atoi(argv[10]);
106 write_matrix(NULL, argv[9], &ctl, matrix, &atm, &obs, argv[4], argv[5],
107 argv[6], prof_out);
108
109 /* Free... */
110 gsl_matrix_free(matrix);
111
112 return EXIT_SUCCESS;
113}
114
115/*****************************************************************************/
116
117static void usage(
118 void) {
119 printf("\nJURASSIC matrix-format converter.\n\n");
120 printf("Convert matrix files between supported MATRIXFMT formats and\n");
121 printf("annotate them using atmospheric or observation metadata.\n\n");
122 printf("Usage:\n");
123 printf(" matfmt <ctl> <atm> <obs> <rowspace> <colspace> <sort>\n");
124 printf
125 (" <matrix_in> <matrixfmt_in> <matrix_out> <matrixfmt_out>\n");
126 printf(" [KEY VALUE ...]\n\n");
127 printf("Arguments:\n");
128 printf(" <ctl> Control file.\n");
129 printf(" <atm> Atmospheric data file.\n");
130 printf(" <obs> Observation data file.\n");
131 printf(" <rowspace> Row space: x or y.\n");
132 printf(" <colspace> Column space: x or y.\n");
133 printf(" <sort> Sort order: r or c.\n");
134 printf(" <matrix_in> Input matrix file.\n");
135 printf(" <matrixfmt_in> Input matrix format identifier.\n");
136 printf(" <matrix_out> Output matrix file.\n");
137 printf(" <matrixfmt_out> Output matrix format identifier.\n");
138 printf(" [KEY VALUE] Optional control parameters.\n\n");
139 printf("Tool-specific control parameters:\n");
140 printf(" PROF_IN <n> Read profile index <n> from the input file.\n");
141 printf(" PROF_OUT <n> Write output as profile index <n>.\n\n");
142 printf("Common control parameters:\n");
143 printf
144 (" ATMFMT, OBSFMT Atmospheric and observation file formats.\n");
145 printf(" NG, EMITTER[i] Active emitters.\n");
146 printf(" ND, NU[i], NW, WINDOW[i] Spectral channels and windows.\n");
147 printf
148 (" NCL, CLNU[i], NSF, SFNU[i] Cloud and surface spectral grids.\n");
149 printf(" RET*_ZMIN, RET*_ZMAX State-vector altitude limits.\n\n");
150 printf("Further information:\n");
151 printf(" Manual: https://slcs-jsc.github.io/jurassic/\n");
152}
void usage(void)
Print command-line help.
Definition: formod.c:163
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
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.
Definition: jurassic.c:7753
void read_matrix(const char *dirname, const char *filename, const ctl_t *ctl, gsl_matrix *matrix, int dataset)
Read a numerical matrix from a file.
Definition: jurassic.c:5643
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
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:6612
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.
Definition: jurassic.c:4627
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.
Definition: jurassic.c:125
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
int main(int argc, char *argv[])
Definition: matfmt.c:39
Atmospheric profile data.
Definition: jurassic.h:1375
Control parameters.
Definition: jurassic.h:1428
int write_matrix
Write matrix file (0=no, 1=yes).
Definition: jurassic.h:1572
int matrixfmt
Matrix data file format (1=ASCII, 2=binary, 3=netCDF).
Definition: jurassic.h:1491
Observation geometry and radiance data.
Definition: jurassic.h:1657