star-hitran

Load line-by-line data from the HITRAN database
git clone git://git.meso-star.fr/star-hitran.git
Log | Files | Refs | README | LICENSE

commit 0137c8983b0702bc42a1274518917f870f0c6d0a
parent 8b72b53d71c91a7ca6bc66b53bbeea4c92027ff8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 14 Feb 2022 12:03:20 +0100

Refactor the isotope metadata

Rename the shitran_isotope member variable imolecule to
molecule_id_local. Move the parse_param_<type> internal functions to a
separate file and minor rename of internal functions.

Diffstat:
Mcmake/CMakeLists.txt | 3++-
Msrc/shitran.h | 10+++++-----
Msrc/shitran_isotope_metadata.c | 101+++++++++++--------------------------------------------------------------------
Asrc/shitran_param.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_param.h | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/test_shitran_isotope_metadata.c | 4++--
6 files changed, 166 insertions(+), 95 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -46,7 +46,8 @@ set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(SHITRAN_FILES_SRC shitran.c shitran_log.c - shitran_isotope_metadata.c) + shitran_isotope_metadata.c + shitran_param.c) set(SHITRAN_FILES_INC shitran_c.h shitran_log.h) diff --git a/src/shitran.h b/src/shitran.h @@ -52,17 +52,17 @@ struct shitran_isotope { double abundance; /* in ]0, 1] */ double Q296K; /* Partition function at Tref = 296K */ double molar_mass; /* In g */ - size_t imolecule; /* Index of the molecule to which the isotope belongs */ + + /* Local idx of the molecule to which the isotope belongs */ + size_t molecule_id_local; + int gj; /* State independent degeneracy factor */ - int id; /* Identifier of the isotope */ + int id; /* Identifier of the isotope <=> Global index */ }; #define SHITRAN_ISOTOPE_NULL__ {0,0,0,0,0,-1} static const struct shitran_isotope SHITRAN_ISOTOPE_NULL = SHITRAN_ISOTOPE_NULL__; -#define SHITRAN_ISOTOPE_IS_NULL(Isotope) \ - ((Isotope)->id == SHITRAN_ISOTOPE_NULL.id) - struct shitran_molecule { const char* name; size_t nisotopes; /* Number of isotopes */ diff --git a/src/shitran_isotope_metadata.c b/src/shitran_isotope_metadata.c @@ -21,6 +21,7 @@ #include "shitran.h" #include "shitran_c.h" #include "shitran_log.h" +#include "shitran_param.h" #include <rsys/cstr.h> #include <rsys/dynamic_array.h> @@ -32,22 +33,6 @@ #include <ctype.h> #include <string.h> -#define C_FORMAT_double "%g" -#define C_FORMAT_int "%d" - -struct param_desc { - const char* path; /* Path where the param lies */ - const char* name; /* Name of the param */ - size_t iline; /* Line number of the param */ - - /* Domain of the param */ - double low, upp; - int is_low_incl; /* Define if the lower bound is inclusive */ - int is_upp_incl; /* Define if the upper bound is inclusive */ -}; -#define PARAM_DESC_NULL__ {NULL, NULL, 0, 0, 0, 0, 0} -static const struct param_desc PARAM_DESC_NULL = PARAM_DESC_NULL__; - /* Generate the dynamic array of isotopes */ #define DARRAY_NAME isotope #define DARRAY_DATA struct shitran_isotope @@ -131,61 +116,8 @@ struct shitran_isotope_metadata { /******************************************************************************* * Helper functions ******************************************************************************/ -#define DEFINE_PARSE_PARAM_FUNCTION(Type) \ - static res_T \ - parse_param_##Type \ - (struct shitran* shitran, \ - const char* str, \ - const char* path, \ - size_t line_num, \ - const struct param_desc* desc, \ - Type* out_param) \ - { \ - Type param = 0; \ - res_T res = RES_OK; \ - ASSERT(shitran && path && desc && out_param); \ - ASSERT(desc->low < desc->upp \ - || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ - \ - if(!str) { \ - log_err(shitran, "%s:%lu: %s is missing.\n", \ - path, (unsigned long)line_num, desc->name); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - res = cstr_to_##Type(str, &param); \ - if(res != RES_OK) { \ - log_err(shitran, "%s:%lu: invalid %s `%s'.\n", \ - desc->path, (unsigned long)desc->iline, desc->name, str); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ - || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ - log_err(shitran, \ - "%s:%lu: invalid %s `%s'. It must be in " \ - "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ - path, (unsigned long)line_num, desc->name, str, \ - desc->is_low_incl ? '[' : ']', (Type)desc->low, \ - (Type)desc->upp, desc->is_upp_incl ? ']' : '['); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - exit: \ - *out_param = param; \ - return res; \ - error: \ - goto exit; \ - } -DEFINE_PARSE_PARAM_FUNCTION(double) -DEFINE_PARSE_PARAM_FUNCTION(int) -#undef DEFINE_PARSE_PARAM_FUNCTION - static res_T -create_isotoplogues +create_isotope_metadata (struct shitran* shitran, struct shitran_isotope_metadata** out_isotopes) { @@ -195,7 +127,8 @@ create_isotoplogues metadata = MEM_CALLOC(shitran->allocator, 1, sizeof(*metadata)); if(!metadata) { - log_err(shitran, "Could not allocate the metadata data structure.\n"); + log_err(shitran, + "Could not allocate the isotope metadata data structure.\n"); res = RES_MEM_ERR; goto error; } @@ -355,18 +288,16 @@ parse_isotope struct shitran* shitran = NULL; char* tk = NULL; char* tk_ctx = NULL; - const char* path = NULL; - size_t line_num = 0; size_t local_id = SIZE_MAX; res_T res = RES_OK; ASSERT(metadata && txtrdr); shitran = metadata->shitran; - path = txtrdr_get_name(txtrdr); - line_num = txtrdr_get_line_num(txtrdr); + param_desc.path = txtrdr_get_name(txtrdr); + param_desc.line = txtrdr_get_line_num(txtrdr); /* Fetch the index of the molecule to which the isotope belongs */ - isotope.imolecule = darray_molecule_size_get(&metadata->molecules); + isotope.molecule_id_local = darray_molecule_size_get(&metadata->molecules); tk = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); param_desc.name = "isotope id"; @@ -374,7 +305,7 @@ parse_isotope param_desc.upp = INT_MAX; param_desc.is_low_incl = 1; param_desc.is_upp_incl = 1; - res = parse_param_int(shitran, tk, path, line_num, &param_desc, &isotope.id); + res = parse_param_int(shitran, tk, &param_desc, &isotope.id); if(res != RES_OK) goto error; tk = strtok_r(NULL, " \t", &tk_ctx); @@ -383,8 +314,7 @@ parse_isotope param_desc.upp = 1; param_desc.is_low_incl = 0; param_desc.is_upp_incl = 1; - res = parse_param_double - (shitran, tk, path, line_num, &param_desc, &isotope.abundance); + res = parse_param_double(shitran, tk, &param_desc, &isotope.abundance); if(res != RES_OK) goto error; tk = strtok_r(NULL, " \t", &tk_ctx); @@ -393,8 +323,7 @@ parse_isotope param_desc.upp = INF; param_desc.is_low_incl = 0; param_desc.is_upp_incl = 1; - res = parse_param_double - (shitran, tk, path, line_num, &param_desc, &isotope.Q296K); + res = parse_param_double(shitran, tk, &param_desc, &isotope.Q296K); if(res != RES_OK) goto error; tk = strtok_r(NULL, " \t", &tk_ctx); @@ -403,8 +332,7 @@ parse_isotope param_desc.upp = INT_MAX; param_desc.is_low_incl = 1; param_desc.is_upp_incl = 1; - res = parse_param_int - (shitran, tk, path, line_num, &param_desc, &isotope.gj); + res = parse_param_int(shitran, tk, &param_desc, &isotope.gj); if(res != RES_OK) goto error; tk = strtok_r(NULL, " \t", &tk_ctx), @@ -413,8 +341,7 @@ parse_isotope param_desc.upp = INF; param_desc.is_low_incl = 0; param_desc.is_upp_incl = 1; - res = parse_param_double - (shitran, tk, path, line_num, &param_desc, &isotope.molar_mass); + res = parse_param_double(shitran, tk, &param_desc, &isotope.molar_mass); if(res != RES_OK) goto error; local_id = darray_isotope_size_get(&metadata->isotopes); @@ -423,7 +350,7 @@ parse_isotope res = darray_isotope_push_back(&metadata->isotopes, &isotope); if(res != RES_OK) { log_err(shitran, "%s:%lu: error storing the isotope %d -- %s.\n", - path, (unsigned long)line_num, isotope.id, res_to_cstr(res)); + param_desc.path, param_desc.line, isotope.id, res_to_cstr(res)); res = RES_OK; goto error; } @@ -490,7 +417,7 @@ load_stream molecule_init(shitran->allocator, &molecule); - res = create_isotoplogues(shitran, &metadata); + res = create_isotope_metadata(shitran, &metadata); if(res != RES_OK) goto error; res = txtrdr_stream(metadata->shitran->allocator, stream, name, diff --git a/src/shitran_param.c b/src/shitran_param.c @@ -0,0 +1,82 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "shitran_c.h" +#include "shitran_log.h" +#include "shitran_param.h" + +#include <rsys/cstr.h> + +#define C_FORMAT_double "%g" +#define C_FORMAT_float "%g" +#define C_FORMAT_int "%d" + +/******************************************************************************* + * Local functions + ******************************************************************************/ +#define DEFINE_PARSE_PARAM_FUNCTION(Type) \ + res_T \ + parse_param_##Type \ + (struct shitran* shitran, \ + const char* str, \ + const struct param_desc* desc, \ + Type* out_param) \ + { \ + Type param = 0; \ + res_T res = RES_OK; \ + ASSERT(shitran && desc && out_param); \ + ASSERT(desc->low < desc->upp \ + || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ + \ + if(!str) { \ + log_err(shitran, "%s:%lu: %s is missing.\n", \ + desc->path, (unsigned long)desc->line, desc->name); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + res = cstr_to_##Type(str, &param); \ + if(res != RES_OK) { \ + log_err(shitran, "%s:%lu: invalid %s `%s'.\n", \ + desc->path, (unsigned long)desc->line, desc->name, str); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ + || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ + log_err(shitran, \ + "%s:%lu: invalid %s `%s'. It must be in " \ + "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ + desc->path, (unsigned long)desc->line, desc->name, str, \ + desc->is_low_incl ? '[' : ']', (Type)desc->low, \ + (Type)desc->upp, desc->is_upp_incl ? ']' : '['); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + exit: \ + *out_param = param; \ + return res; \ + error: \ + goto exit; \ + } +DEFINE_PARSE_PARAM_FUNCTION(int) +DEFINE_PARSE_PARAM_FUNCTION(double) +DEFINE_PARSE_PARAM_FUNCTION(float) +#undef DEFINE_PARSE_PARAM_FUNCTION diff --git a/src/shitran_param.h b/src/shitran_param.h @@ -0,0 +1,61 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef SHITRAN_PARAM_H +#define SHITRAN_PARAM_H + +#include <rsys/rsys.h> + +struct param_desc { + const char* path; /* Path where the param lies */ + const char* name; /* Name of the param */ + size_t line; /* Line number of the param */ + + /* Domain of the param */ + double low, upp; + int is_low_incl; /* Define if the lower bound is inclusive */ + int is_upp_incl; /* Define if the upper bound is inclusive */ +}; +#define PARAM_DESC_NULL__ {NULL, NULL, 0, 0, 0, 0, 0} +static const struct param_desc PARAM_DESC_NULL = PARAM_DESC_NULL__; + +/* Forware declaration */ +struct shitran; + +extern LOCAL_SYM res_T +parse_param_int + (struct shitran* shitran, + const char* str, + const struct param_desc* desc, + int* param); + +extern LOCAL_SYM res_T +parse_param_double + (struct shitran* shitran, + const char* str, + const struct param_desc* desc, + double* param); + +extern LOCAL_SYM res_T +parse_param_float + (struct shitran* shitran, + const char* str, + const struct param_desc* desc, + float* param); + +#endif /* SHITRAN_PARAM_H */ diff --git a/src/test_shitran_isotope_metadata.c b/src/test_shitran_isotope_metadata.c @@ -88,7 +88,7 @@ isotope_eq(const struct shitran_isotope* i0, const struct shitran_isotope* i1) return i0->abundance == i1->abundance && i0->Q296K == i1->Q296K && i0->molar_mass == i1->molar_mass - && i0->imolecule == i1->imolecule + && i0->molecule_id_local == i1->molecule_id_local && i0->gj == i1->gj && i0->id == i1->id; } @@ -130,7 +130,7 @@ check_isotope CHK(isotope->id >= 0); CHK(shitran_isotope_metadata_get_molecule - (mdata, isotope->imolecule, &molecule2) == RES_OK); + (mdata, isotope->molecule_id_local, &molecule2) == RES_OK); CHK(molecule_eq(molecule, &molecule2)); }