| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
long num_latitude; |
| 46 |
long num_longitude; |
| 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 |
|
| 87 |
if (coda_cursor_read_double_array(&cursor, data.double_data, coda_array_ordering_c) != 0) |
| 88 |
{ |
| 89 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
if (coda_cursor_goto(&cursor, "@missing_value[0]") == 0) |
| 93 |
{ |
| 94 |
if (coda_cursor_read_double(&cursor, &fill_value.double_data) != 0) |
| 95 |
{ |
| 96 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
harp_array_replace_fill_value(data_type, num_elements, data, fill_value); |
| 101 |
} |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int read_datetime_start(void *user_data, harp_array data) |
| 107 |
{ |
| 108 |
ingest_info *info = (ingest_info *)user_data; |
| 109 |
char datetime_string[27]; |
| 110 |
coda_cursor cursor; |
| 111 |
|
| 112 |
if (coda_cursor_set_product(&cursor, info->product) != 0) |
| 113 |
{ |
| 114 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
if (coda_cursor_goto(&cursor, "/@RangeBeginningDate") != 0) |
| 118 |
{ |
| 119 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
if (coda_cursor_read_string(&cursor, datetime_string, 11) != 0) |
| 123 |
{ |
| 124 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
datetime_string[10] = 'T'; |
| 128 |
if (coda_cursor_goto(&cursor, "../RangeBeginningTime") != 0) |
| 129 |
{ |
| 130 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
if (coda_cursor_read_string(&cursor, &datetime_string[11], 16) != 0) |
| 134 |
{ |
| 135 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
if (coda_time_string_to_double("yyyy-MM-dd'T'HH:mm:ss.SSSSSS", datetime_string, data.double_data) != 0) |
| 139 |
{ |
| 140 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
static int read_datetime_length(void *user_data, harp_array data) |
| 148 |
{ |
| 149 |
(void)user_data; |
| 150 |
|
| 151 |
data.double_data[0] = 86400; |
| 152 |
|
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
static int read_latitude(void *user_data, harp_array data) |
| 157 |
{ |
| 158 |
return read_dataset((ingest_info *)user_data, "lat", harp_type_double, |
| 159 |
((ingest_info *)user_data)->num_latitude, data); |
| 160 |
} |
| 161 |
|
| 162 |
static int read_longitude(void *user_data, harp_array data) |
| 163 |
{ |
| 164 |
return read_dataset((ingest_info *)user_data, "lon", harp_type_double, |
| 165 |
((ingest_info *)user_data)->num_longitude, data); |
| 166 |
} |
| 167 |
|
| 168 |
static int read_xco2(void *user_data, harp_array data) |
| 169 |
{ |
| 170 |
ingest_info *info = (ingest_info *)user_data; |
| 171 |
|
| 172 |
return read_dataset(info, "XCO2", harp_type_double, info->num_latitude * info->num_longitude, data); |
| 173 |
} |
| 174 |
|
| 175 |
static int read_xco2_uncertainty(void *user_data, harp_array data) |
| 176 |
{ |
| 177 |
ingest_info *info = (ingest_info *)user_data; |
| 178 |
|
| 179 |
return read_dataset(info, "XCO2PREC", harp_type_double, info->num_latitude * info->num_longitude, data); |
| 180 |
} |
| 181 |
|
| 182 |
static int read_dimensions(void *user_data, long dimension[HARP_NUM_DIM_TYPES]) |
| 183 |
{ |
| 184 |
ingest_info *info = (ingest_info *)user_data; |
| 185 |
|
| 186 |
dimension[harp_dimension_time] = info->num_time; |
| 187 |
dimension[harp_dimension_latitude] = info->num_latitude; |
| 188 |
dimension[harp_dimension_longitude] = info->num_longitude; |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static int init_dimensions(ingest_info *info) |
| 194 |
{ |
| 195 |
coda_cursor cursor; |
| 196 |
long coda_dim[CODA_MAX_NUM_DIMS]; |
| 197 |
int num_coda_dims; |
| 198 |
|
| 199 |
if (coda_cursor_set_product(&cursor, info->product) != 0) |
| 200 |
{ |
| 201 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
if (coda_cursor_goto(&cursor, "/time") != 0) |
| 205 |
{ |
| 206 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
if (coda_cursor_get_array_dim(&cursor, &num_coda_dims, coda_dim) != 0) |
| 210 |
{ |
| 211 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
if (num_coda_dims != 1) |
| 215 |
{ |
| 216 |
harp_set_error(HARP_ERROR_INGESTION, "dataset has %d dimensions, expected 1", num_coda_dims); |
| 217 |
harp_add_coda_cursor_path_to_error_message(&cursor); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
if (coda_dim[0] != 1) |
| 221 |
{ |
| 222 |
harp_set_error(HARP_ERROR_INGESTION, "time dimension should have length 1 for daily grids"); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
info->num_time = 1; |
| 226 |
|
| 227 |
if (coda_cursor_goto(&cursor, "/lat") != 0) |
| 228 |
{ |
| 229 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
if (coda_cursor_get_array_dim(&cursor, &num_coda_dims, coda_dim) != 0) |
| 233 |
{ |
| 234 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 235 |
return -1; |
| 236 |
} |
| 237 |
if (num_coda_dims != 1) |
| 238 |
{ |
| 239 |
harp_set_error(HARP_ERROR_INGESTION, "dataset has %d dimensions, expected 1", num_coda_dims); |
| 240 |
harp_add_coda_cursor_path_to_error_message(&cursor); |
| 241 |
return -1; |
| 242 |
} |
| 243 |
info->num_latitude = coda_dim[0]; |
| 244 |
|
| 245 |
if (coda_cursor_goto(&cursor, "/lon") != 0) |
| 246 |
{ |
| 247 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 248 |
return -1; |
| 249 |
} |
| 250 |
if (coda_cursor_get_array_dim(&cursor, &num_coda_dims, coda_dim) != 0) |
| 251 |
{ |
| 252 |
harp_set_error(HARP_ERROR_CODA, NULL); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
if (num_coda_dims != 1) |
| 256 |
{ |
| 257 |
harp_set_error(HARP_ERROR_INGESTION, "dataset has %d dimensions, expected 1", num_coda_dims); |
| 258 |
harp_add_coda_cursor_path_to_error_message(&cursor); |
| 259 |
return -1; |
| 260 |
} |
| 261 |
info->num_longitude = coda_dim[0]; |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
static int ingestion_init(const harp_ingestion_module *module, coda_product *product, |
| 267 |
const harp_ingestion_options *options, harp_product_definition **definition, void **user_data) |
| 268 |
{ |
| 269 |
ingest_info *info; |
| 270 |
|
| 271 |
(void)options; |
| 272 |
|
| 273 |
info = malloc(sizeof(ingest_info)); |
| 274 |
if (info == NULL) |
| 275 |
{ |
| 276 |
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", |
| 277 |
sizeof(ingest_info), __FILE__, __LINE__); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
info->product = product; |
| 281 |
if (init_dimensions(info) != 0) |
| 282 |
{ |
| 283 |
ingestion_done(info); |
| 284 |
return -1; |
| 285 |
} |
| 286 |
*definition = *module->product_definition; |
| 287 |
*user_data = info; |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
int harp_ingestion_module_oco_l3co2_init(void) |
| 293 |
{ |
| 294 |
harp_ingestion_module *module; |
| 295 |
harp_product_definition *product_definition; |
| 296 |
harp_variable_definition *variable_definition; |
| 297 |
harp_dimension_type dimension_type[3] = { harp_dimension_time, harp_dimension_latitude, harp_dimension_longitude }; |
| 298 |
const char *description; |
| 299 |
const char *path; |
| 300 |
|
| 301 |
module = harp_ingestion_register_module("OCO_OCO2_L3CO2", "OCO", "OCO", "oco2_GEOS_L3CO2", "OCO-2 L3 CO2", |
| 302 |
ingestion_init, ingestion_done); |
| 303 |
product_definition = harp_ingestion_register_product(module, "OCO_OCO2_L3CO2", NULL, read_dimensions); |
| 304 |
|
| 305 |
|
| 306 |
description = "range beginning date"; |
| 307 |
variable_definition = |
| 308 |
harp_ingestion_register_variable_full_read(product_definition, "datetime_start", harp_type_double, 1, |
| 309 |
dimension_type, NULL, description, "seconds since 2000-01-01", NULL, |
| 310 |
read_datetime_start); |
| 311 |
path = "/@RangeBeginningDate, /@RangeBeginningTime"; |
| 312 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL); |
| 313 |
|
| 314 |
|
| 315 |
description = "averaging period"; |
| 316 |
variable_definition = |
| 317 |
harp_ingestion_register_variable_full_read(product_definition, "datetime_length", harp_type_double, 1, |
| 318 |
dimension_type, NULL, description, "s", NULL, read_datetime_length); |
| 319 |
description = "Fixed value of 86400"; |
| 320 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, NULL, description); |
| 321 |
|
| 322 |
|
| 323 |
description = "latitude"; |
| 324 |
variable_definition = |
| 325 |
harp_ingestion_register_variable_full_read(product_definition, "latitude", harp_type_double, 1, |
| 326 |
&dimension_type[1], NULL, description, "degree_north", NULL, |
| 327 |
read_latitude); |
| 328 |
harp_variable_definition_set_valid_range_double(variable_definition, -90.0, 90.0); |
| 329 |
path = "/lat[]"; |
| 330 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL); |
| 331 |
|
| 332 |
|
| 333 |
description = "longitude"; |
| 334 |
variable_definition = |
| 335 |
harp_ingestion_register_variable_full_read(product_definition, "longitude", harp_type_double, 1, |
| 336 |
&dimension_type[2], NULL, description, "degree_east", NULL, |
| 337 |
read_longitude); |
| 338 |
harp_variable_definition_set_valid_range_double(variable_definition, -180.0, 180.0); |
| 339 |
path = "/lon[]"; |
| 340 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
description = "Assimilated dry-air column average CO2 daily mean"; |
| 345 |
variable_definition = |
| 346 |
harp_ingestion_register_variable_full_read(product_definition, "CO2_column_volume_mixing_ratio_dry_air", |
| 347 |
harp_type_double, 3, dimension_type, NULL, description, "ppmv", NULL, |
| 348 |
read_xco2); |
| 349 |
path = "/XCO2[]"; |
| 350 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL); |
| 351 |
|
| 352 |
|
| 353 |
description = "precission of XCO2 daily mean"; |
| 354 |
variable_definition = |
| 355 |
harp_ingestion_register_variable_full_read(product_definition, |
| 356 |
"CO2_column_volume_mixing_ratio_dry_air_uncertainty", |
| 357 |
harp_type_double, 3, dimension_type, NULL, description, "ppmv", NULL, |
| 358 |
read_xco2_uncertainty); |
| 359 |
path = "/XCO2PREC[]"; |
| 360 |
harp_variable_definition_add_mapping(variable_definition, NULL, NULL, path, NULL); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |