Debian Codegraph @

libharp/harp-ingest-earlinet.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 <stdlib.h>
37 #include <string.h>
38
39 typedef struct ingest_info_struct
40 {
41 coda_product *product;
42 long num_times;
43 long num_altitudes;
44 long num_wavelengths;
45 int has_backscatter;
46 int has_extinction;
47 } ingest_info;
48
49 static void ingestion_done(void *user_data)
50 {
51 ingest_info *info = (ingest_info *)user_data;
52
53 if (info != NULL)
54 {
55 free(info);
56 }
57 }
58
59 static int read_array_variable(ingest_info *info, const char *name, harp_data_type data_type, long num_elements,
60 harp_array data)
61 {
62 harp_scalar fill_value;
63 coda_cursor cursor;
64 long actual_num_elements;
65
66 if (coda_cursor_set_product(&cursor, info->product) != 0)
67 {
68 harp_set_error(HARP_ERROR_CODA, NULL);
69 return -1;
70 }
71 if (coda_cursor_goto_record_field_by_name(&cursor, name) != 0)
72 {
73 harp_set_error(HARP_ERROR_CODA, NULL);
74 return -1;
75 }
76 if (coda_cursor_get_num_elements(&cursor, &actual_num_elements) != 0)
77 {
78 harp_set_error(HARP_ERROR_CODA, NULL);
79 return -1;
80 }
81 if (actual_num_elements != num_elements)
82 {
83 harp_set_error(HARP_ERROR_INGESTION, "variable %s has %ld elements (expected %ld)", name, actual_num_elements,
84 num_elements);
85 return -1;
86 }
87 switch (data_type)
88 {
89 case harp_type_float:
90 if (coda_cursor_read_float_array(&cursor, data.float_data, coda_array_ordering_c) != 0)
91 {
92 harp_set_error(HARP_ERROR_CODA, NULL);
93 return -1;
94 }
95 if (coda_cursor_goto(&cursor, "@FillValue[0]") == 0)
96 {
97 if (coda_cursor_read_float(&cursor, &fill_value.float_data) != 0)
98 {
99 harp_set_error(HARP_ERROR_CODA, NULL);
100 return -1;
101 }
102 /* Replace values equal to the _FillValue variable attribute by NaN. */
103 harp_array_replace_fill_value(data_type, num_elements, data, fill_value);
104 }
105 break;
106 case harp_type_double:
107 if (coda_cursor_read_double_array(&cursor, data.double_data, coda_array_ordering_c) != 0)
108 {
109 harp_set_error(HARP_ERROR_CODA, NULL);
110 return -1;
111 }
112 if (coda_cursor_goto(&cursor, "@FillValue[0]") == 0)
113 {
114 if (coda_cursor_read_double(&cursor, &fill_value.double_data) != 0)
115 {
116 harp_set_error(HARP_ERROR_CODA, NULL);
117 return -1;
118 }
119 /* Replace values equal to the _FillValue variable attribute by NaN. */
120 harp_array_replace_fill_value(data_type, num_elements, data, fill_value);
121 }
122 break;
123 default:
124 assert(0);
125 exit(1);
126 }
127
128 return 0;
129 }
130
131 /* Specific read functions */
132
133 static int read_latitude(void *user_data, harp_array data)
134 {
135 ingest_info *info = (ingest_info *)user_data;
136
137 return read_array_variable(info, "latitude", harp_type_float, 1, data);
138 }
139
140 static int read_longitude(void *user_data, harp_array data)
141 {
142 ingest_info *info = (ingest_info *)user_data;
143
144 return read_array_variable(info, "longitude", harp_type_float, 1, data);
145 }
146
147 static int read_time_start(void *user_data, harp_array data)
148 {
149 ingest_info *info = (ingest_info *)user_data;
150 harp_array time_bounds;
151 long i;
152
153 time_bounds.ptr = malloc(info->num_times * 2 * sizeof(double));
154 if (time_bounds.ptr == NULL)
155 {
156 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
157 info->num_times * 2 * sizeof(double), __FILE__, __LINE__);
158 return -1;
159 }
160
161 if (read_array_variable(info, "time_bounds", harp_type_double, info->num_times * 2, time_bounds) != 0)
162 {
163 free(time_bounds.ptr);
164 return -1;
165 }
166
167 for (i = 0; i < info->num_times; i++)
168 {
169 data.double_data[i] = time_bounds.double_data[i * 2];
170 }
171
172 free(time_bounds.ptr);
173
174 return 0;
175 }
176
177 static int read_time_stop(void *user_data, harp_array data)
178 {
179 ingest_info *info = (ingest_info *)user_data;
180 harp_array time_bounds;
181 long i;
182
183 time_bounds.ptr = malloc(info->num_times * 2 * sizeof(double));
184 if (time_bounds.ptr == NULL)
185 {
186 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
187 info->num_times * 2 * sizeof(double), __FILE__, __LINE__);
188 return -1;
189 }
190
191 if (read_array_variable(info, "time_bounds", harp_type_double, info->num_times * 2, time_bounds) != 0)
192 {
193 free(time_bounds.ptr);
194 return -1;
195 }
196
197 for (i = 0; i < info->num_times; i++)
198 {
199 data.double_data[i] = time_bounds.double_data[i * 2 + 1];
200 }
201
202 free(time_bounds.ptr);
203
204 return 0;
205 }
206
207 static int read_station_altitude(void *user_data, harp_array data)
208 {
209 ingest_info *info = (ingest_info *)user_data;
210
211 return read_array_variable(info, "station_altitude", harp_type_float, 1, data);
212 }
213
214 static int read_zenith_angle(void *user_data, harp_array data)
215 {
216 ingest_info *info = (ingest_info *)user_data;
217
218 return read_array_variable(info, "zenith_angle", harp_type_float, 1, data);
219 }
220
221 static int read_wavelength(void *user_data, harp_array data)
222 {
223 ingest_info *info = (ingest_info *)user_data;
224
225 return read_array_variable(info, "wavelength", harp_type_float, info->num_wavelengths, data);
226 }
227
228 static int read_altitude(void *user_data, harp_array data)
229 {
230 ingest_info *info = (ingest_info *)user_data;
231
232 return read_array_variable(info, "altitude", harp_type_double, info->num_altitudes, data);
233 }
234
235 static int read_backscatter(void *user_data, harp_array data)
236 {
237 ingest_info *info = (ingest_info *)user_data;
238 long dimension[3] = { info->num_wavelengths, info->num_times, info->num_altitudes };
239 int order[3] = { 1, 0, 2 };
240
241 if (read_array_variable(info, "backscatter", harp_type_double,
242 info->num_wavelengths * info->num_times * info->num_altitudes, data) != 0)
243 {
244 return -1;
245 }
246
247 return harp_array_transpose(harp_type_double, 3, dimension, order, data);
248 }
249
250 static int read_error_backscatter(void *user_data, harp_array data)
251 {
252 ingest_info *info = (ingest_info *)user_data;
253 long dimension[3] = { info->num_wavelengths, info->num_times, info->num_altitudes };
254 int order[3] = { 1, 0, 2 };
255
256 if (read_array_variable(info, "error_backscatter", harp_type_double,
257 info->num_wavelengths * info->num_times * info->num_altitudes, data) != 0)
258 {
259 return -1;
260 }
261
262 return harp_array_transpose(harp_type_double, 3, dimension, order, data);
263 }
264
265 static int read_extinction(void *user_data, harp_array data)
266 {
267 ingest_info *info = (ingest_info *)user_data;
268 long dimension[3] = { info->num_wavelengths, info->num_times, info->num_altitudes };
269 int order[3] = { 1, 0, 2 };
270
271 if (read_array_variable(info, "extinction", harp_type_double,
272 info->num_wavelengths * info->num_times * info->num_altitudes, data) != 0)
273 {
274 return -1;
275 }
276
277 return harp_array_transpose(harp_type_double, 3, dimension, order, data);
278 }
279
280 static int read_error_extinction(void *user_data, harp_array data)
281 {
282 ingest_info *info = (ingest_info *)user_data;
283 long dimension[3] = { info->num_wavelengths, info->num_times, info->num_altitudes };
284 int order[3] = { 1, 0, 2 };
285
286 if (read_array_variable(info, "error_extinction", harp_type_double,
287 info->num_wavelengths * info->num_times * info->num_altitudes, data) != 0)
288 {
289 return -1;
290 }
291
292 return harp_array_transpose(harp_type_double, 3, dimension, order, data);
293 }
294
295 static int include_backscatter(void *user_data)
296 {
297 return ((ingest_info *)user_data)->has_backscatter;
298 }
299
300 static int include_extinction(void *user_data)
301 {
302 return ((ingest_info *)user_data)->has_extinction;
303 }
304
305 static int read_dimensions(void *user_data, long dimension[HARP_NUM_DIM_TYPES])
306 {
307 ingest_info *info = (ingest_info *)user_data;
308
309 dimension[harp_dimension_time] = info->num_times;
310 dimension[harp_dimension_vertical] = info->num_altitudes;
311 dimension[harp_dimension_spectral] = info->num_wavelengths;
312
313 return 0;
314 }
315
316 static int get_dimensions_and_availability(ingest_info *info)
317 {
318 coda_cursor cursor;
319 long index;
320
321 if (coda_cursor_set_product(&cursor, info->product) != 0)
322 {
323 harp_set_error(HARP_ERROR_CODA, NULL);
324 return -1;
325 }
326
327 if (coda_cursor_goto(&cursor, "time") != 0)
328 {
329 harp_set_error(HARP_ERROR_CODA, NULL);
330 return -1;
331 }
332 if (coda_cursor_get_num_elements(&cursor, &info->num_times) != 0)
333 {
334 harp_set_error(HARP_ERROR_CODA, NULL);
335 return -1;
336 }
337 coda_cursor_goto_parent(&cursor);
338
339 if (coda_cursor_goto(&cursor, "altitude") != 0)
340 {
341 harp_set_error(HARP_ERROR_CODA, NULL);
342 return -1;
343 }
344 if (coda_cursor_get_num_elements(&cursor, &info->num_altitudes) != 0)
345 {
346 harp_set_error(HARP_ERROR_CODA, NULL);
347 return -1;
348 }
349 coda_cursor_goto_parent(&cursor);
350
351 if (coda_cursor_goto(&cursor, "wavelength") != 0)
352 {
353 harp_set_error(HARP_ERROR_CODA, NULL);
354 return -1;
355 }
356 if (coda_cursor_get_num_elements(&cursor, &info->num_wavelengths) != 0)
357 {
358 harp_set_error(HARP_ERROR_CODA, NULL);
359 return -1;
360 }
361 coda_cursor_goto_parent(&cursor);
362
363 if (coda_cursor_get_record_field_index_from_name(&cursor, "backscatter", &index) == 0)
364 {
365 info->has_backscatter = 1;
366 }
367
368 if (coda_cursor_get_record_field_index_from_name(&cursor, "extinction", &index) == 0)
369 {
370 info->has_extinction = 1;
371 }
372
373 return 0;
374 }
375
376 static int ingestion_init(const harp_ingestion_module *module, coda_product *product,
377 const harp_ingestion_options *options, harp_product_definition **definition, void **user_data)
378 {
379 ingest_info *info;
380
381 (void)options;
382
383 info = malloc(sizeof(ingest_info));
384 if (info == NULL)
385 {
386 harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
387 sizeof(ingest_info), __FILE__, __LINE__);
388 return -1;
389 }
390 info->product = product;
391 info->num_times = 0;
392 info->num_altitudes = 0;
393 info->num_wavelengths = 0;
394 info->has_backscatter = 0;
395 info->has_extinction = 0;
396
397 if (get_dimensions_and_availability(info) != 0)
398 {
399 ingestion_done(info);
400 return -1;
401 }
402
403 *definition = *module->product_definition;
404 *user_data = info;
405
406 return 0;
407 }
408
409 int harp_ingestion_module_earlinet_init(void)
410 {
411 harp_ingestion_module *module;
412 harp_product_definition *product_definition;
413 harp_variable_definition *variable_definition;
414 harp_dimension_type dimension_type[3] = { harp_dimension_time, harp_dimension_spectral, harp_dimension_vertical };
415 const char *description;
416 const char *path;
417
418 module = harp_ingestion_register_module("EARLINET", "EARLINET", "EARLINET", "EARLINET",
419 "EARLINET aerosol backscatter and extinction profiles", ingestion_init,
420 ingestion_done);
421 product_definition = harp_ingestion_register_product(module, "EARLINET", NULL, read_dimensions);
422
423 /* datetime_start */
424 description = "start time of measurement";
425 variable_definition =
426 harp_ingestion_register_variable_full_read(product_definition, "datetime_start", harp_type_double, 1,
427 dimension_type, NULL, description, "seconds since 1970-01-01", NULL,
428 read_time_start);
429 path = "/time_bounds[:,0]";
430 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
431
432 /* datetime_stop */
433 description = "stop time of measurement";
434 variable_definition =
435 harp_ingestion_register_variable_full_read(product_definition, "datetime_stop", harp_type_double, 1,
436 dimension_type, NULL, description, "seconds since 1970-01-01", NULL,
437 read_time_stop);
438 path = "/time_bounds[:,1]";
439 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
440
441 /* latitude */
442 description = "latitude";
443 variable_definition =
444 harp_ingestion_register_variable_full_read(product_definition, "latitude", harp_type_float, 0, dimension_type,
445 NULL, description, "degrees", NULL, read_latitude);
446 path = "/latitude";
447 harp_variable_definition_set_valid_range_float(variable_definition, -90.0, 90.0);
448 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
449
450 /* longitude */
451 description = "longitude";
452 variable_definition =
453 harp_ingestion_register_variable_full_read(product_definition, "longitude", harp_type_float, 0, dimension_type,
454 NULL, description, "degrees", NULL, read_longitude);
455 path = "/longitude";
456 harp_variable_definition_set_valid_range_float(variable_definition, -180.0, 180.0);
457 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
458
459 /* sensor_altitude */
460 description = "sensor altitude";
461 variable_definition =
462 harp_ingestion_register_variable_full_read(product_definition, "sensor_altitude", harp_type_float, 0,
463 dimension_type, NULL, description, "m", NULL, read_station_altitude);
464 path = "/station_altitude";
465 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
466
467 /* viewing_zenith_angle */
468 description = "viewing zenith angle";
469 variable_definition =
470 harp_ingestion_register_variable_full_read(product_definition, "viewing_zenith_angle", harp_type_float, 0,
471 dimension_type, NULL, description, "degrees", NULL,
472 read_zenith_angle);
473 path = "/zenith_angle";
474 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
475
476 /* wavelength */
477 description = "wavelength";
478 variable_definition =
479 harp_ingestion_register_variable_full_read(product_definition, "wavelength", harp_type_float, 1,
480 &dimension_type[1], NULL, description, "nm", NULL, read_wavelength);
481 path = "/wavelength";
482 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
483
484 /* altitude */
485 description = "altitude";
486 variable_definition =
487 harp_ingestion_register_variable_full_read(product_definition, "altitude", harp_type_double, 1,
488 &(dimension_type[2]), NULL, description, "m", NULL, read_altitude);
489 path = "/altitude";
490 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
491
492 /* backscatter_coefficient */
493 description = "backscatter coefficient";
494 variable_definition =
495 harp_ingestion_register_variable_full_read(product_definition, "backscatter_coefficient", harp_type_double, 3,
496 dimension_type, NULL, description, "1/(m*sr)", include_backscatter,
497 read_backscatter);
498 path = "/backscatter";
499 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
500
501 /* backscatter_coefficient_uncertainty */
502 description = "backscatter coefficient uncertainty";
503 variable_definition =
504 harp_ingestion_register_variable_full_read(product_definition, "backscatter_coefficient_uncertainty",
505 harp_type_double, 3, dimension_type, NULL, description, "1/(m*sr)",
506 include_backscatter, read_error_backscatter);
507 path = "/error_backscatter";
508 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
509
510 /* extinction_coefficient */
511 description = "extinction coefficient";
512 variable_definition =
513 harp_ingestion_register_variable_full_read(product_definition, "extinction_coefficient", harp_type_double, 3,
514 dimension_type, NULL, description, "1/m", include_extinction,
515 read_extinction);
516 path = "/extinction";
517 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
518
519 /* extinction_coefficient_uncertainty */
520 description = "extinction coefficient uncertainty";
521 variable_definition =
522 harp_ingestion_register_variable_full_read(product_definition, "extinction_coefficient_uncertainty",
523 harp_type_double, 3, dimension_type, NULL, description, "1/m",
524 include_extinction, read_error_extinction);
525 path = "/error_extinction";
526 harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL);
527
528 return 0;
529 }