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 c90bbd3dce184f13e6550b2cfe68a35f681d2122
parent 7bcf9f7e2cfb3264d20b295e4b31e826e0bda4c0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  9 Feb 2022 11:53:24 +0100

Implement and test shitran_isotope_metadata_find_isotope

Diffstat:
Msrc/shitran.h | 27+++++++++++++++++----------
Msrc/shitran_isotope_metadata.c | 32++++++++++++++++++++++++++++++--
Msrc/test_shitran_isotope_metadata.c | 35++++++++++++++++++++++++++++++++---
3 files changed, 79 insertions(+), 15 deletions(-)

diff --git a/src/shitran.h b/src/shitran.h @@ -52,13 +52,16 @@ struct shitran_isotope { double abundance; /* in ]0, 1] */ double Q296K; /* Partition function at Tref = 296K */ double molar_mass; /* In g */ - size_t molecule; /* Index of the molecule to which the isotope belongs */ + size_t imolecule; /* Index of the molecule to which the isotope belongs */ int gj; /* State independent degeneracy factor */ int id; /* Unique identifier of the isotope */ }; -#define SHITRAN_ISOTOPOLOGUE_NULL__ {0,0,0,0,0,0} -static const struct shitran_isotope SHITRAN_ISOTOPOLOGUE_NULL = - SHITRAN_ISOTOPOLOGUE_NULL__; +#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; @@ -70,11 +73,8 @@ struct shitran_molecule { static const struct shitran_molecule SHITRAN_MOLECULE_NULL = SHITRAN_MOLECULE_NULL__; -#define SHITRAN_MOLECULE_IS_NULL(Molecule) \ - ( (Molecule)->name == SHITRAN_MOLECULE_NULL.name \ - && (Molecule)->nisotopes == SHITRAN_MOLECULE_NULL.nisotopes \ - && (Molecule)->isotopes == SHITRAN_MOLECULE_NULL.isotopes \ - && (Molecule)->id == SHITRAN_MOLECULE_NULL.id) +#define SHITRAN_MOLECULE_IS_NULL(Molecule) \ + ((Molecule)->id == SHITRAN_MOLECULE_NULL.id) /* Forward declarations */ struct shitran; @@ -133,13 +133,20 @@ shitran_isotope_metadata_get_molecule const size_t imolecule, /* Index of the molecule in [0, molecules_count[ */ struct shitran_molecule* molecule); -/* `molecule' is set to SHITRAN_MOLECULE_NULL if ti molecule id is not found */ +/* `molecule' is set to SHITRAN_MOLECULE_NULL if `molecule_id' is not found */ SHITRAN_API res_T shitran_isotope_metadata_find_molecule (struct shitran_isotope_metadata* metadata, const int molecule_id, /* Unique identifier of the molecule */ struct shitran_molecule* molecule); +/* `isotope' is set to SHITRAN_ISOTOPE_NULL if `isotope_id' is not found */ +SHITRAN_API res_T +shitran_isotope_metadata_find_isotope + (struct shitran_isotope_metadata* metadata, + const int isotope_id, /* Unique identifier of the isotope */ + struct shitran_isotope* isotope); + END_DECLS #endif /* SHITRAN_H */ diff --git a/src/shitran_isotope_metadata.c b/src/shitran_isotope_metadata.c @@ -350,7 +350,7 @@ parse_isotope (struct shitran_isotope_metadata* metadata, struct txtrdr* txtrdr) { - struct shitran_isotope isotope = SHITRAN_ISOTOPOLOGUE_NULL; + struct shitran_isotope isotope = SHITRAN_ISOTOPE_NULL; struct param_desc param_desc = PARAM_DESC_NULL; struct shitran* shitran = NULL; char* tk = NULL; @@ -366,7 +366,7 @@ parse_isotope line_num = txtrdr_get_line_num(txtrdr); /* Fetch the index of the molecule to which the isotope belongs */ - isotope.molecule = darray_molecule_size_get(&metadata->molecules); + isotope.imolecule = darray_molecule_size_get(&metadata->molecules); tk = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); param_desc.name = "isotope id"; @@ -703,3 +703,31 @@ error: goto exit; } +res_T +shitran_isotope_metadata_find_isotope + (struct shitran_isotope_metadata* metadata, + const int isotope_id, /* Unique identifier of the isotope */ + struct shitran_isotope* out_isotope) +{ + size_t* piisotope = NULL; + res_T res = RES_OK; + + if(!metadata || !out_isotope) { + res = RES_BAD_ARG; + goto error; + } + + piisotope = htable_id2entry_find(&metadata->isoid2idx, &isotope_id); + if(!piisotope) { + *out_isotope = SHITRAN_ISOTOPE_NULL; + } else { + ASSERT(piisotope < darray_isotope_size_get(&metadata->isotopes)); + *out_isotope = darray_isotope_cdata_get(&metadata->isotopes)[*piisotope]; + } + +exit: + return res; +error: + goto exit; +} + diff --git a/src/test_shitran_isotope_metadata.c b/src/test_shitran_isotope_metadata.c @@ -80,7 +80,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->molecule == i1->molecule + && i0->imolecule == i1->imolecule && i0->gj == i1->gj && i0->id == i1->id; } @@ -104,12 +104,33 @@ molecule_eq(const struct shitran_molecule* m0, const struct shitran_molecule* m1 } static void -test_load(struct shitran* shitran) +check_molecule_isotopes + (struct shitran_isotope_metadata* metadata, + const struct shitran_molecule* molecule) { + struct shitran_isotope isotope = SHITRAN_ISOTOPE_NULL; + struct shitran_molecule molecule2 = SHITRAN_MOLECULE_NULL; + size_t i; + CHK(metadata && molecule); + + FOR_EACH(i, 0, H2O.nisotopes) { + CHK(shitran_isotope_metadata_find_isotope + (metadata, molecule->isotopes[i].id, &isotope) == RES_OK); + CHK(!SHITRAN_ISOTOPE_IS_NULL(&isotope)); + CHK(isotope_eq(&isotope, &molecule->isotopes[i])); + CHK(shitran_isotope_metadata_get_molecule + (metadata, isotope.imolecule, &molecule2) == RES_OK); + CHK(molecule_eq(molecule, &molecule2)); + } +} +static void +test_load(struct shitran* shitran) +{ + struct shitran_isotope isotope = SHITRAN_ISOTOPE_NULL; + struct shitran_molecule molecule = SHITRAN_MOLECULE_NULL; const char* filename = "test_isotope_metadata.txt"; struct shitran_isotope_metadata* metadata = NULL; - struct shitran_molecule molecule = SHITRAN_MOLECULE_NULL; FILE* fp = NULL; size_t nmolecules = 0; @@ -152,6 +173,14 @@ test_load(struct shitran* shitran) CHK(shitran_isotope_metadata_find_molecule(metadata, 0, &molecule) == RES_OK); CHK(SHITRAN_MOLECULE_IS_NULL(&molecule)); + CHK(shitran_isotope_metadata_find_isotope(NULL, 161, &isotope) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_find_isotope(metadata, 161, NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_find_isotope(metadata, 0, &isotope) == RES_OK); + CHK(SHITRAN_ISOTOPE_IS_NULL(&isotope)); + + check_molecule_isotopes(metadata, &H2O); + check_molecule_isotopes(metadata, &CO2); + CHK(shitran_isotope_metadata_ref_get(NULL) == RES_BAD_ARG); CHK(shitran_isotope_metadata_ref_get(metadata) == RES_OK); CHK(shitran_isotope_metadata_ref_put(NULL) == RES_BAD_ARG);