Debian Codegraph @

libharp/harp-ingest-aeolus_l2a.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-ingestion.h"
34
35 #include <assert.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #define MAX_PATH_LENGTH 256
42
43 typedef struct ingest_info_struct
44 {
45 coda_product *product;
46 long num_properties;
47 coda_cursor *properties_cursor;
48 coda_cursor *pcd_cursor;
49 } ingest_info;
50
51
52 static int read_dimensions(void *user_data, long dimension[HARP_NUM_DIM_TYPES])
53 {
54 dimension[harp_dimension_time] = ((ingest_info *)user_data)->num_properties;
55 dimension[harp_dimension_vertical] = 24;
56 return 0;
57 }
58
59 static int get_double_value(coda_cursor cursor, const char *path, harp_array data)
60 {
61 if (coda_cursor_goto(&cursor, path) != 0)
62 {
63 harp_set_error(HARP_ERROR_CODA, NULL);
64 return -1;
65 }
66 if (coda_cursor_read_double(&cursor, data.double_data) != 0)
67 {
68 harp_set_error(HARP_ERROR_CODA, NULL);
69 return -1;
70 }
71
72 return 0;
73 }
74
75 static int get_int8_array(coda_cursor cursor, const char *field1, const char *field2, harp_array data)
76 {
77 int i;
78
79 if (coda_cursor_goto_record_field_by_name(&cursor, field1) != 0)
80 {
81 harp_set_error(HARP_ERROR_CODA, NULL);
82 return -1;
83 }
84 if (coda_cursor_goto_first_array_element(&cursor) != 0)
85 {
86 harp_set_error(HARP_ERROR_CODA, NULL);
87 return -1;
88 }
89 for (i = 0; i < 24; i++)
90 {
91 if (coda_cursor_goto_record_field_by_name(&cursor, field2) != 0)
92 {
93 harp_set_error(HARP_ERROR_CODA, NULL);
94 return -1;
95 }
96 if (coda_cursor_read_int8(&cursor, &data.int8_data[i]) != 0)
97 {
98 harp_set_error(HARP_ERROR_CODA, NULL);
99 return -1;
100 }
101 coda_cursor_goto_parent(&cursor);
102 if (i < 23)
103 {
104 if (coda_cursor_goto_next_array_element(&cursor) != 0)
105 {
106 harp_set_error(HARP_ERROR_CODA, NULL);
107 return -1;
108 }
109
110 }
111 }
112
113 return 0;
114 }
115
116 static int get_double_array(coda_cursor cursor, const char *field1, const char *field2, harp_array data)
117 {
118 int i;
119
120 if (coda_cursor_goto_record_field_by_name(&cursor, field1) != 0)
121 {
122 harp_set_error(HARP_ERROR_CODA, NULL);
123 return -1;
124 }
125 if (coda_cursor_goto_first_array_element(&cursor) != 0)
126 {
127 harp_set_error(HARP_ERROR_CODA, NULL);
128 return -1;
129 }
130 for (i = 0; i < 24; i++)
131 {
132 if (coda_cursor_goto_record_field_by_name(&cursor, field2) != 0)
133 {
134 harp_set_error(HARP_ERROR_CODA, NULL);
135 return -1;
136 }
137 if (coda_cursor_read_double(&cursor, &data.double_data[i]) != 0)
138 {
139 harp_set_error(HARP_ERROR_CODA, NULL);
140 return -1;
141 }
142 coda_cursor_goto_parent(&cursor);
143 if (i < 23)
144 {
145 if (coda_cursor_goto_next_array_element(&cursor) != 0)
146 {
147 harp_set_error(HARP_ERROR_CODA, NULL);
148 return -1;
149 }
150
151 }
152 }
153
154 return 0;
155 }
156
157 static int get_sqrt_double_array(coda_cursor cursor, const char *field1, const char *field2, harp_array data)
158 {
159 int i;
160
161 if (get_double_array(cursor, field1, field2, data) != 0)
162 {
163 return -1;
164 }
165 for (i = 0; i < 24; i++)
166 {
167 data.double_data[i] = sqrt(data.double_data[i]);
168 }
169
170 return 0;
171 }
172
173 static int init_cursors(ingest_info *info)
174 {
175 coda_cursor cursor;
176 long num_elements;
177 int i;
178
179 if (coda_cursor_set_product(&cursor, info->product) != 0)
180 {
181 harp_set_error(HARP_ERROR_CODA, NULL);
182 return -1;
183 }
184 if (coda_cursor_goto_record_field_by_name(&cursor, "sca_optical_properties") != 0)
185 {
186 harp_set_error(HARP_ERROR_CODA, NULL);
187 return -1;
188 }
189 if (coda_cursor_get_num_elements(&cursor, &info->num_properties) != 0)
190 {
191 harp_set_error(HARP_ERROR_CODA, NULL);
192 return -1;
193 }
194
195 info->properties_cursor = malloc(info->num_properties * sizeof(coda_cursor));
196 if (info->properties_cursor == NULL)
197 {
198 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
199 info->num_properties * sizeof(coda_cursor), __FILE__, __LINE__);
200 return -1;
201 }
202
203 if (coda_cursor_goto_first_array_element(&cursor) != 0)
204 {
205 harp_set_error(HARP_ERROR_CODA, NULL);
206 return -1;
207 }
208 for (i = 0; i < info->num_properties; i++)
209 {
210 info->properties_cursor[i] = cursor;
211 if (i < info->num_properties - 1)
212 {
213 if (coda_cursor_goto_next_array_element(&cursor) != 0)
214 {
215 harp_set_error(HARP_ERROR_CODA, NULL);
216 return -1;
217 }
218 }
219 }
220
221 if (coda_cursor_goto(&cursor, "/sca_pcd") != 0)
222 {
223 harp_set_error(HARP_ERROR_CODA, NULL);
224 return -1;
225 }
226 if (coda_cursor_get_num_elements(&cursor, &num_elements) != 0)
227 {
228 harp_set_error(HARP_ERROR_CODA, NULL);
229 return -1;
230 }
231 if (num_elements != info->num_properties)
232 {
233 harp_set_error(HARP_ERROR_INGESTION, "product error detected (pcd data set contains %ld records, "
234 "but expected %ld (= number of properties)", num_elements, info->num_properties);
235 return -1;
236 }
237
238 info->pcd_cursor = malloc(info->num_properties * sizeof(coda_cursor));
239 if (info->pcd_cursor == NULL)
240 {
241 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
242 info->num_properties * sizeof(coda_cursor), __FILE__, __LINE__);
243 return -1;
244 }
245
246 if (coda_cursor_goto_first_array_element(&cursor) != 0)
247 {
248 harp_set_error(HARP_ERROR_CODA, NULL);
249 return -1;
250 }
251 for (i = 0; i < info->num_properties; i++)
252 {
253 info->pcd_cursor[i] = cursor;
254 if (i < info->num_properties - 1)
255 {
256 if (coda_cursor_goto_next_array_element(&cursor) != 0)
257 {
258 harp_set_error(HARP_ERROR_CODA, NULL);
259 return -1;
260 }
261 }
262 }
263
264 return 0;
265 }
266
267 static int read_datetime(void *user_data, long index, harp_array data)
268 {
269 return get_double_value(((ingest_info *)user_data)->properties_cursor[index], "starttime", data);
270 }
271
272 static int read_datetime_length(void *user_data, long index, harp_array data)
273 {
274 (void)user_data;
275 (void)index;
276
277 *data.double_data = 12.0;
278
279 return 0;
280 }
281
282 static int read_orbit_index(void *user_data, harp_array data)
283 {
284 ingest_info *info = (ingest_info *)user_data;
285 coda_cursor cursor;
286
287 if (coda_cursor_set_product(&cursor, info->product) != 0)
288 {
289 harp_set_error(HARP_ERROR_CODA, NULL);
290 return -1;
291 }
292 if (coda_cursor_goto(&cursor, "/mph/abs_orbit") != 0)
293 {
294 harp_set_error(HARP_ERROR_CODA, NULL);
295 return -1;
296 }
297 /* we hard cast the unsigned integer to signed (we don't expect orbit numbers > 2^31) */
298 if (coda_cursor_read_uint32(&cursor, (uint32_t *)data.int32_data) != 0)
299 {
300 harp_set_error(HARP_ERROR_CODA, NULL);
301 return -1;
302 }
303
304 return 0;
305 }
306
307 static int read_latitude(void *user_data, long index, harp_array data)
308 {
309 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "geolocation_middle_bins", "latitude",
310 data);
311 }
312
313 static int read_longitude(void *user_data, long index, harp_array data)
314 {
315 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "geolocation_middle_bins",
316 "longitude", data);
317 }
318
319 static int read_altitude(void *user_data, long index, harp_array data)
320 {
321 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "geolocation_middle_bins",
322 "altitude", data);
323 }
324
325 static int read_extinction(void *user_data, long index, harp_array data)
326 {
327 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "sca_optical_properties",
328 "extinction", data);
329 }
330
331 static int read_extinction_uncertainty(void *user_data, long index, harp_array data)
332 {
333 return get_sqrt_double_array(((ingest_info *)user_data)->pcd_cursor[index], "profile_pcd_bins",
334 "extinction_variance", data);
335 }
336
337 static int read_backscatter(void *user_data, long index, harp_array data)
338 {
339 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "sca_optical_properties",
340 "backscatter", data);
341 }
342
343 static int read_backscatter_uncertainty(void *user_data, long index, harp_array data)
344 {
345 return get_sqrt_double_array(((ingest_info *)user_data)->pcd_cursor[index], "profile_pcd_bins",
346 "backscatter_variance", data);
347 }
348
349 static int read_lod(void *user_data, long index, harp_array data)
350 {
351 return get_double_array(((ingest_info *)user_data)->properties_cursor[index], "sca_optical_properties", "lod",
352 data);
353 }
354
355 static int read_lod_uncertainty(void *user_data, long index, harp_array data)
356 {
357 return get_sqrt_double_array(((ingest_info *)user_data)->pcd_cursor[index], "profile_pcd_bins", "lod_variance",
358 data);
359 }
360
361 static int read_validity(void *user_data, long index, harp_array data)
362 {
363 return get_int8_array(((ingest_info *)user_data)->pcd_cursor[index], "profile_pcd_bins", "processing_qc_flag",
364 data);
365 }
366
367 static void ingestion_done(void *user_data)
368 {
369 ingest_info *info = (ingest_info *)user_data;
370
371 if (info->properties_cursor != NULL)
372 {
373 free(info->properties_cursor);
374 }
375
376 free(info);
377 }
378
379 static int ingestion_init(const harp_ingestion_module *module, coda_product *product,
380 const harp_ingestion_options *options, harp_product_definition **definition, void **user_data)
381 {
382 ingest_info *info;
383
384 (void)options;
385
386 info = malloc(sizeof(ingest_info));
387 if (info == NULL)
388 {
389 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
390 sizeof(ingest_info), __FILE__, __LINE__);
391 return -1;
392 }
393 info->product = product;
394 info->properties_cursor = NULL;
395
396 if (init_cursors(info) != 0)
397 {
398 ingestion_done(info);
399 return -1;
400 }
401
402 *definition = *module->product_definition;
403 *user_data = info;
404
405 return 0;
406 }
407
408 int harp_ingestion_module_aeolus_l2a_init(void)
409 {
410 harp_ingestion_module *module;
411 harp_product_definition *product_definition;
412 harp_variable_definition *variable_definition;
413 harp_dimension_type dimension_type[2];
414 const char *description;
415
416 dimension_type[0] = harp_dimension_time;
417 dimension_type[1] = harp_dimension_vertical;
418
419 description = "AEOLUS Level 2A Optical Properties Product";
420 module = harp_ingestion_register_module("AEOLUS_L2A", "AEOLUS", "AEOLUS", "ALD_U_N_2A", description,
421 ingestion_init, ingestion_done);
422
423 description = "AEOLUS Level 2A Standard Correct Algorithm (SCA) optical properties";
424 product_definition = harp_ingestion_register_product(module, "AEOLUS_L2A_SCA", description, read_dimensions);
425
426 /* datetime_start */
427 description = "start time of observation";
428 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "datetime",
429 harp_type_double, 1, dimension_type, NULL,
430 description, "seconds since 2000-01-01",
431 NULL, read_datetime);
432 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, "/sca_optical_properties[]/starttime", NULL);
433
434 /* datetime_length */
435 description = "duration of the observation";
436 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "datetime_length",
437 harp_type_double, 1, dimension_type, NULL,
438 description, "s", NULL, read_datetime_length);
439 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, NULL, "set to fixed value of 12 seconds");
440
441 /* orbit_index */
442 description = "absolute orbit number";
443 variable_definition =
444 harp_ingestion_register_variable_full_read(product_definition, "orbit_index", harp_type_int32, 0, NULL, NULL,
445 description, NULL, NULL, read_orbit_index);
446 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, "/mph/abs_orbit", NULL);
447
448 /* latitude */
449 description = "latitude of the bin center";
450 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "latitude",
451 harp_type_double, 2, dimension_type, NULL,
452 description, "degree_north", NULL, read_latitude);
453 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
454 "/sca_optical_properties[]/geolocation_middle_bins[]/latitude", NULL);
455
456 /* longitude */
457 description = "longitude of the bin center";
458 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "longitude",
459 harp_type_double, 2, dimension_type, NULL,
460 description, "degree_east", NULL, read_longitude);
461 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
462 "/sca_optical_properties[]/geolocation_middle_bins[]/longitude", NULL);
463
464 /* altitude */
465 description = "altitude of the bin center";
466 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "altitude",
467 harp_type_double, 2, dimension_type, NULL,
468 description, "m", NULL, read_altitude);
469 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
470 "/sca_optical_properties[]/geolocation_middle_bins[]/altitude", NULL);
471
472 /* extinction_coefficient */
473 description = "particle extinction";
474 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "extinction_coefficient",
475 harp_type_double, 2, dimension_type, NULL,
476 description, "(1e-6)/m", NULL, read_extinction);
477 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
478 "/sca_optical_properties[]/sca_optical_properties[]/extinction", NULL);
479
480 /* extinction_coefficient_uncertainty */
481 description = "uncertainty of the particle extinction";
482 variable_definition = harp_ingestion_register_variable_block_read(product_definition,
483 "extinction_coefficient_uncertainty",
484 harp_type_double, 2, dimension_type, NULL,
485 description, "1/m", NULL,
486 read_extinction_uncertainty);
487 description = "the square root of the variance is taken as uncertainty";
488 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
489 "/sca_pcd[]/profile_pcd_bins[]/extinction_variance", description);
490
491 /* backscatter_coefficient */
492 description = "particle backscatter";
493 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "backscatter_coefficient",
494 harp_type_double, 2, dimension_type, NULL,
495 description, "(1e-6)/m/sr", NULL,
496 read_backscatter);
497 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
498 "/sca_optical_properties[]/sca_optical_properties[]/backscatter", NULL);
499
500 /* backscatter_coefficient_uncertainty */
501 description = "uncertainty of the particle backscatter";
502 variable_definition = harp_ingestion_register_variable_block_read(product_definition,
503 "backscatter_coefficient_uncertainty",
504 harp_type_double, 2, dimension_type, NULL,
505 description, "1/m/sr", NULL,
506 read_backscatter_uncertainty);
507 description = "the square root of the variance is taken as uncertainty";
508 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
509 "/sca_pcd[]/profile_pcd_bins[]/backscatter_variance", description);
510
511 /* optical_depth */
512 description = "particle local optical depth";
513 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "optical_depth",
514 harp_type_double, 2, dimension_type, NULL,
515 description, HARP_UNIT_DIMENSIONLESS, NULL,
516 read_lod);
517 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
518 "/sca_optical_properties[]/sca_optical_properties[]/lod", NULL);
519
520 /* optical_depth_uncertainty */
521 description = "uncertainty of the particle local optical depth";
522 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "optical_depth_uncertainty",
523 harp_type_double, 2, dimension_type, NULL,
524 description, HARP_UNIT_DIMENSIONLESS, NULL,
525 read_lod_uncertainty);
526 description = "the square root of the variance is taken as uncertainty";
527 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
528 "/sca_pcd[]/profile_pcd_bins[]/lod_variance", description);
529
530 /* validity */
531 description = "processing qc flag";
532 variable_definition = harp_ingestion_register_variable_block_read(product_definition, "validity", harp_type_int8,
533 2, dimension_type, NULL, description, NULL, NULL,
534 read_validity);
535 harp_variable_definition_add_mapping(variable_definition, NULL, NULL,
536 "/sca_pcd[]/profile_pcd_bins[]/processing_qc_flag", NULL);
537
538 return 0;
539 }