Debian Codegraph @

libharp/harp-ingest-oco-ltsif.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2015-2026 S[&]T, The Netherlands.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "coda.h"
33 #include "harp-constants.h"
34 #include "harp-ingestion.h"
35
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 typedef struct ingest_info_struct
42 {
43 coda_product *product;
44 long num_time;
45 int sif_wavelength;
46 int use_daily_correction;
47 } ingest_info;
48
49 static void ingestion_done(void *user_data)
50 {
51 ingest_info *info = (ingest_info *)user_data;
52
53 free(info);
54 }
55
56 static int read_dataset(ingest_info *info, const char *path, harp_data_type data_type, long num_elements,
57 harp_array data)
58 {
59 coda_cursor cursor;
60 long coda_num_elements;
61 harp_scalar fill_value;
62
63 if (coda_cursor_set_product(&cursor, info->product) != 0)
64 {
65 harp_set_error(HARP_ERROR_CODA, NULL);
66 return -1;
67 }
68 if (coda_cursor_goto(&cursor, path) != 0)
69 {
70 harp_set_error(HARP_ERROR_CODA, NULL);
71 return -1;
72 }
73 if (coda_cursor_get_num_elements(&cursor, &coda_num_elements) != 0)
74 {
75 harp_set_error(HARP_ERROR_CODA, NULL);
76 return -1;
77 }
78 if (coda_num_elements != num_elements)
79 {
80 harp_set_error(HARP_ERROR_INGESTION, "dataset has %ld elements (expected %ld)", coda_num_elements,
81 num_elements);
82 harp_add_coda_cursor_path_to_error_message(&cursor);
83 harp_add_error_message(" (%s:%lu)", __FILE__, __LINE__);
84 return -1;
85 }
86 switch (data_type)
87 {
88 case harp_type_int16:
89 if (coda_cursor_read_int16_array(&cursor, data.int16_data, coda_array_ordering_c) != 0)
90 {
91 harp_set_error(HARP_ERROR_CODA, NULL);
92 return -1;
93 }
94 break;
95 case harp_type_double:
96 if (coda_cursor_read_double_array(&cursor, data.double_data, coda_array_ordering_c) != 0)
97 {
98 harp_set_error(HARP_ERROR_CODA, NULL);
99 return -1;
100 }
101 if (coda_cursor_goto(&cursor, "@missing_value[0]") != 0)
102 {
103 harp_set_error(HARP_ERROR_CODA, NULL);
104 return -1;
105 }
106 if (coda_cursor_read_double(&cursor, &fill_value.double_data) != 0)
107 {
108 harp_set_error(HARP_ERROR_CODA, NULL);
109 return -1;
110 }
111 /* Replace values equal to the missing_value variable attribute by NaN. */
112 harp_array_replace_fill_value(data_type, num_elements, data, fill_value);
113 break;
114 default:
115 assert(0);
116 exit(1);
117 }
118
119 return 0;
120 }
121
122 static int read_datetime(void *user_data, harp_array data)
123 {
124 ingest_info *info = (ingest_info *)user_data;
125
126 return read_dataset(info, "/Delta_Time", harp_type_double, info->num_time, data);
127 }
128
129 static int read_latitude(void *user_data, harp_array data)
130 {
131 return read_dataset((ingest_info *)user_data, "Latitude", harp_type_double,
132 ((ingest_info *)user_data)->num_time, data);
133 }
134
135 static int read_latitude_corners(void *user_data, harp_array data)
136 {
137 return read_dataset((ingest_info *)user_data, "Latitude_Corners", harp_type_double,
138 ((ingest_info *)user_data)->num_time * 4, data);
139 }
140
141 static int read_longitude(void *user_data, harp_array data)
142 {
143 return read_dataset((ingest_info *)user_data, "Longitude", harp_type_double,
144 ((ingest_info *)user_data)->num_time, data);
145 }
146
147 static int read_longitude_corners(void *user_data, harp_array data)
148 {
149 return read_dataset((ingest_info *)user_data, "Longitude_Corners", harp_type_double,
150 ((ingest_info *)user_data)->num_time * 4, data);
151 }
152
153 static int read_saz(void *user_data, harp_array data)
154 {
155 return read_dataset((ingest_info *)user_data, "SAz", harp_type_double, ((ingest_info *)user_data)->num_time, data);
156 }
157
158 static int read_sza(void *user_data, harp_array data)
159 {
160 return read_dataset((ingest_info *)user_data, "SZA", harp_type_double, ((ingest_info *)user_data)->num_time, data);
161 }
162
163 static int read_vaz(void *user_data, harp_array data)
164 {
165 return read_dataset((ingest_info *)user_data, "VAz", harp_type_double, ((ingest_info *)user_data)->num_time, data);
166 }
167
168 static int read_vza(void *user_data, harp_array data)
169 {
170 return read_dataset((ingest_info *)user_data, "VZA", harp_type_double, ((ingest_info *)user_data)->num_time, data);
171 }
172
173 static int read_sif(void *user_data, harp_array data)
174 {
175 const char *variable_path = NULL;
176 ingest_info *info = (ingest_info *)user_data;
177
178 if (info->use_daily_correction)
179 {
180 switch (info->sif_wavelength)
181 {
182 case 757:
183 variable_path = "Science/SIF_757nm";
184 break;
185 case 771:
186 variable_path = "Science/SIF_771nm";
187 break;
188 default:
189 variable_path = "SIF_740nm";
190 }
191 }
192 else
193 {
194 switch (info->sif_wavelength)
195 {
196 case 757:
197 variable_path = "Daily_SIF_757nm";
198 break;
199 case 771:
200 variable_path = "Daily_SIF_771nm";
201 break;
202 default:
203 variable_path = "Daily_SIF_740nm";
204 }
205 }
206 return read_dataset(info, variable_path, harp_type_double, info->num_time, data);
207 }
208
209 static int read_sif_uncertainty(void *user_data, harp_array data)
210 {
211 const char *variable_path = NULL;
212 ingest_info *info = (ingest_info *)user_data;
213
214 switch (info->sif_wavelength)
215 {
216 case 757:
217 variable_path = "Science/SIF_Uncertainty_757nm";
218 break;
219 case 771:
220 variable_path = "Science/SIF_Uncertainty_771nm";
221 break;
222 default:
223 variable_path = "SIF_Uncertainty_740nm";
224 }
225
226 return read_dataset(info, variable_path, harp_type_double, info->num_time, data);
227 }
228
229 static int read_quality_flag(void *user_data, harp_array data)
230 {
231 ingest_info *info = (ingest_info *)user_data;
232
233 return read_dataset(info, "quality_flag", harp_type_int16, info->num_time, data);
234 }
235
236 static int read_wavelength(void *user_data, harp_array data)
237 {
238 ingest_info *info = (ingest_info *)user_data;
239
240 switch (info->sif_wavelength)
241 {
242 case 757:
243 data.double_data[0] = 757;
244 break;
245 case 771:
246 data.double_data[0] = 771;
247 break;
248 default:
249 data.double_data[0] = 740;
250 }
251
252 return 0;
253 }
254
255 static int read_dimensions(void *user_data, long dimension[HARP_NUM_DIM_TYPES])
256 {
257 ingest_info *info = (ingest_info *)user_data;
258
259 dimension[harp_dimension_time] = info->num_time;
260
261 return 0;
262 }
263
264 static int init_dimensions(ingest_info *info)
265 {
266 coda_cursor cursor;
267 long coda_dim[CODA_MAX_NUM_DIMS];
268 int num_coda_dims;
269
270 if (coda_cursor_set_product(&cursor, info->product) != 0)
271 {
272 harp_set_error(HARP_ERROR_CODA, NULL);
273 return -1;
274 }
275 if (coda_cursor_goto(&cursor, "/Delta_Time") != 0)
276 {
277 harp_set_error(HARP_ERROR_CODA, NULL);
278 return -1;
279 }
280 if (coda_cursor_get_array_dim(&cursor, &num_coda_dims, coda_dim) != 0)
281 {
282 harp_set_error(HARP_ERROR_CODA, NULL);
283 return -1;
284 }
285 if (num_coda_dims != 1)
286 {
287 harp_set_error(HARP_ERROR_INGESTION, "dataset has %d dimensions, expected 1", num_coda_dims);
288 harp_add_coda_cursor_path_to_error_message(&cursor);
289 return -1;
290 }
291 info->num_time = coda_dim[0];
292
293 return 0;
294 }
295
296 static int ingestion_init(const harp_ingestion_module *module, coda_product *product,
297 const harp_ingestion_options *options, harp_product_definition **definition, void **user_data)
298 {
299 const char *option_value;
300 ingest_info *info;
301
302 info = malloc(sizeof(ingest_info));
303 if (info == NULL)
304 {
305 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
306 sizeof(ingest_info), __FILE__, __LINE__);
307 return -1;
308 }
309 info->product = product;
310 info->sif_wavelength = 740;
311 info->use_daily_correction = 0;
312
313 if (harp_ingestion_options_has_option(options, "daily_correction"))
314 {
315 info->use_daily_correction = 1;
316 }
317 if (harp_ingestion_options_has_option(options, "sif_wavelength"))
318 {
319 if (harp_ingestion_options_get_option(options, "sif_wavelength", &option_value) != 0)
320 {
321 ingestion_done(info);
322 return -1;
323 }
324 if (strcmp(option_value, "757") == 0)
325 {
326 info->sif_wavelength = 757;
327 }
328 else if (strcmp(option_value, "771") == 0)
329 {
330 info->sif_wavelength = 771;
331 }
332 }
333
334 if (init_dimensions(info) != 0)
335 {
336 ingestion_done(info);
337 return -1;
338 }
339 *definition = *module->product_definition;
340 *user_data = info;
341
342 return 0;
343 }
344
345 static int include_sif_uncertainty(void *user_data)
346 {
347 return !((ingest_info *)user_data)->use_daily_correction;
348 }
349
350 static void register_fields(harp_product_definition *product_definition)
351 {
352 harp_variable_definition *variable_definition;
353 harp_dimension_type dimension_type[2] = { harp_dimension_time, harp_dimension_independent };
354 long bounds_dimension[2] = { -1, 4 };
355 const char *description;
356 const char *path;
357
358 /* datetime */
359 description = "time";
360 variable_definition =
361 harp_ingestion_register_variable_full_read(product_definition, "datetime", harp_type_double, 1, dimension_type,
362 NULL, description, "seconds since 1990-01-01", NULL, read_datetime);
363 path = "/Delta_Time[]";
364 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
365
366 /* latitude */
367 description = "center latitude";
368 variable_definition =
369 harp_ingestion_register_variable_full_read(product_definition, "latitude", harp_type_double, 1, dimension_type,
370 NULL, description, "degree_north", NULL, read_latitude);
371 harp_variable_definition_set_valid_range_double(variable_definition, -90.0, 90.0);
372 path = "/Latitude[]";
373 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
374
375 /* longitude */
376 description = "center longitude";
377 variable_definition =
378 harp_ingestion_register_variable_full_read(product_definition, "longitude", harp_type_double, 1, dimension_type,
379 NULL, description, "degree_east", NULL, read_longitude);
380 harp_variable_definition_set_valid_range_double(variable_definition, -180.0, 180.0);
381 path = "/Longitude[]";
382 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
383
384 /* latitude_bounds */
385 description = "corner latitude";
386 variable_definition =
387 harp_ingestion_register_variable_full_read(product_definition, "latitude_bounds", harp_type_double, 2,
388 dimension_type, bounds_dimension, description, "degree_north",
389 NULL, read_latitude_corners);
390 harp_variable_definition_set_valid_range_double(variable_definition, -90.0, 90.0);
391 path = "/Latitude_Corners[]";
392 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
393
394 /* longitude */
395 description = "corner longitude";
396 variable_definition =
397 harp_ingestion_register_variable_full_read(product_definition, "longitude_bounds", harp_type_double, 2,
398 dimension_type, bounds_dimension, description, "degree_east",
399 NULL, read_longitude_corners);
400 harp_variable_definition_set_valid_range_double(variable_definition, -180.0, 180.0);
401 path = "/Longitude_Corners[]";
402 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
403
404 /* solar_azimuth_angle */
405 description = "solar azimuth angle";
406 variable_definition =
407 harp_ingestion_register_variable_full_read(product_definition, "solar_azimuth_angle", harp_type_double, 1,
408 dimension_type, NULL, description, "degree", NULL, read_saz);
409 path = "/SZa[]";
410 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
411
412 /* solar_zenith_angle */
413 description = "solar zenith angle";
414 variable_definition =
415 harp_ingestion_register_variable_full_read(product_definition, "solar_zenith_angle", harp_type_double, 1,
416 dimension_type, NULL, description, "degree", NULL, read_sza);
417 path = "/SZA[]";
418 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
419
420 /* viewing_azimuth_angle */
421 description = "viewing azimuth angle";
422 variable_definition =
423 harp_ingestion_register_variable_full_read(product_definition, "viewing_azimuth_angle", harp_type_double, 1,
424 dimension_type, NULL, description, "degree", NULL, read_vaz);
425 path = "/VAz[]";
426 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
427
428 /* viewing_zenith_angle */
429 description = "viewing zenith angle";
430 variable_definition =
431 harp_ingestion_register_variable_full_read(product_definition, "viewing_zenith_angle", harp_type_double, 1,
432 dimension_type, NULL, description, "degree", NULL, read_vza);
433 path = "/VZA[]";
434 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
435
436 /* solar_induced_fluorescence */
437 description = "Solar Induced Fluorescence";
438 variable_definition =
439 harp_ingestion_register_variable_full_read(product_definition, "solar_induced_fluorescence",
440 harp_type_double, 1, dimension_type, NULL, description, "", NULL,
441 read_sif);
442 path = "/SIF_740nm[]";
443 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength unset, daily_correction unset",
444 path, NULL);
445 path = "/Science/SIF_757nm[]";
446 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=757, daily_correction unset", path,
447 NULL);
448 path = "/Science/SIF_771nm[]";
449 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=771, daily_correction unset", path,
450 NULL);
451 path = "/Daily_SIF_740nm[]";
452 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength unset, daily_correction=applied",
453 path, NULL);
454 path = "/Daily_SIF_757nm[]";
455 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=757, daily_correction=applied",
456 path, NULL);
457 path = "/Daily_SIF_771nm[]";
458 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=771, daily_correction=applied",
459 path, NULL);
460
461 /* solar_induced_fluorescence_uncertainty */
462 description = "Estimated 1-Sigma Uncertainty of Solar Induced Fluorescence";
463 variable_definition =
464 harp_ingestion_register_variable_full_read(product_definition, "solar_induced_fluorescence_uncertainty",
465 harp_type_double, 1, dimension_type, NULL, description, "",
466 include_sif_uncertainty, read_sif_uncertainty);
467 path = "/SIF_Uncertainty_740nm[]";
468 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength unset, daily_correction unset",
469 path, NULL);
470 path = "/Science/SIF_Uncertainty_757nm[]";
471 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=757, daily_correction unset", path,
472 NULL);
473 path = "/Science/SIF_Uncertainty_771nm[]";
474 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=771, daily_correction unset", path,
475 NULL);
476
477 /* wavelength */
478 description = "SIF wavelength";
479 variable_definition = harp_ingestion_register_variable_full_read(product_definition, "wavelength", harp_type_double,
480 0, NULL, NULL, description, "nm", NULL,
481 read_wavelength);
482 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength unset", NULL, "741");
483 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=757", NULL, "757");
484 harp_variable_definition_add_mapping(variable_definition, NULL, "sif_wavelength=771", NULL, "771");
485
486 /* validity */
487 description = "quality flag";
488 variable_definition =
489 harp_ingestion_register_variable_full_read(product_definition, "validity", harp_type_int16, 1, dimension_type,
490 NULL, description, NULL, NULL, read_quality_flag);
491 path = "/Quality_Flag[]";
492 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
493 }
494
495 static void register_options(harp_ingestion_module *module)
496 {
497 const char *sif_wavelength_options[] = { "757", "771" };
498 const char *sif_daily_correction[] = { "applied" };
499
500 harp_ingestion_register_option(module, "sif_wavelength", "whether to ingest SIF at 740nm (default) or the "
501 "one at 757nm (sif_wavelength=757) or the one at 771nm (sif_wavelength=771)", 2,
502 sif_wavelength_options);
503
504 harp_ingestion_register_option(module, "daily_correction", "whether to ingest the instantaneous SIF (default) or "
505 "the daily averaged SIF based on geometric correction (daily_correction=applied)", 1,
506 sif_daily_correction);
507 }
508
509 static void register_module_oco2_LtSIF(void)
510 {
511 harp_ingestion_module *module;
512 harp_product_definition *product_definition;
513
514 module = harp_ingestion_register_module("OCO_OCO2_LtSIF", "OCO", "OCO", "oco2_LtSIF", "OCO-2 L2 Lite SIF",
515 ingestion_init, ingestion_done);
516 register_options(module);
517 product_definition = harp_ingestion_register_product(module, "OCO_OCO2_LtSIF", NULL, read_dimensions);
518
519 register_fields(product_definition);
520 }
521
522 static void register_module_oco3_LtSIF(void)
523 {
524 harp_ingestion_module *module;
525 harp_product_definition *product_definition;
526
527 module = harp_ingestion_register_module("OCO_OCO3_LtSIF", "OCO", "OCO", "oco3_LtSIF", "OCO-3 L2 Lite SIF",
528 ingestion_init, ingestion_done);
529 register_options(module);
530 product_definition = harp_ingestion_register_product(module, "OCO_OCO3_LtSIF", NULL, read_dimensions);
531
532 register_fields(product_definition);
533 }
534
535 int harp_ingestion_module_oco_ltsif_init(void)
536 {
537 register_module_oco2_LtSIF();
538 register_module_oco3_LtSIF();
539 return 0;
540 }