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 c09eeb7577cb61744719b802044d5b0a9304e37f
parent 31809f4f63f8bc681abe7eef910cabf44f6a96f3
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue,  8 Mar 2022 19:30:47 +0100

Minor refactoring of the shtr_lines_list.c file

Diffstat:
Msrc/shtr.h | 14+++++++-------
Msrc/shtr_lines_list.c | 130++++++++++++++++++++++++++++++++++++-------------------------------------------
Asrc/shtr_lines_list_c.h | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+), 78 deletions(-)

diff --git a/src/shtr.h b/src/shtr.h @@ -170,37 +170,37 @@ shtr_isotope_metadata_find_molecule struct shtr_molecule* molecule); /******************************************************************************* - * Transitions API + * Lines API ******************************************************************************/ SHTR_API res_T shtr_lines_list_load (struct shtr* shtr, const char* path, - struct shtr_lines_list** trlst); + struct shtr_lines_list** lnlst); SHTR_API res_T shtr_lines_list_load_stream (struct shtr* shtr, FILE* stream, const char* stream_name, /* NULL <=> use default stream name */ - struct shtr_lines_list** trlst); + struct shtr_lines_list** lnlst); SHTR_API res_T shtr_lines_list_ref_get - (struct shtr_lines_list* trlst); + (struct shtr_lines_list* lnlst); SHTR_API res_T shtr_lines_list_ref_put - (struct shtr_lines_list* trlst); + (struct shtr_lines_list* lnlst); SHTR_API res_T shtr_lines_list_get_size - (const struct shtr_lines_list* trlst, + (const struct shtr_lines_list* lnlst, size_t* nlines); SHTR_API res_T shtr_lines_list_get - (const struct shtr_lines_list* trlst, + (const struct shtr_lines_list* lnlst, const struct shtr_line* lines[]); END_DECLS diff --git a/src/shtr_lines_list.c b/src/shtr_lines_list.c @@ -18,26 +18,13 @@ #include "shtr.h" #include "shtr_c.h" +#include "shtr_lines_list_c.h" #include "shtr_log.h" #include "shtr_param.h" #include <rsys/cstr.h> -#include <rsys/dynamic_array.h> #include <rsys/text_reader.h> -/* Generate the dynamic array of trlst */ -#define DARRAY_NAME line -#define DARRAY_DATA struct shtr_line -#include <rsys/dynamic_array.h> - -struct shtr_lines_list { - /* List of trlst */ - struct darray_line lines; - - struct shtr* shtr; - ref_T ref; -}; - /******************************************************************************* * Helper functions ******************************************************************************/ @@ -46,32 +33,32 @@ create_lines_list (struct shtr* shtr, struct shtr_lines_list** out_lines) { - struct shtr_lines_list* trlst = NULL; + struct shtr_lines_list* lnlst = NULL; res_T res = RES_OK; ASSERT(shtr && out_lines); - trlst = MEM_CALLOC(shtr->allocator, 1, sizeof(*trlst)); - if(!trlst) { - log_err(shtr, "Could not allocate the trlst data structure.\n"); + lnlst = MEM_CALLOC(shtr->allocator, 1, sizeof(*lnlst)); + if(!lnlst) { + log_err(shtr, "Could not allocate the lnlst data structure.\n"); res = RES_MEM_ERR; goto error; } - ref_init(&trlst->ref); + ref_init(&lnlst->ref); SHTR(ref_get(shtr)); - trlst->shtr = shtr; - darray_line_init(shtr->allocator, &trlst->lines); + lnlst->shtr = shtr; + darray_line_init(shtr->allocator, &lnlst->lines); exit: - *out_lines = trlst; + *out_lines = lnlst; return res; error: goto exit; } static res_T -parse_line(struct shtr_lines_list* trlst, struct txtrdr* txtrdr) +parse_line(struct shtr_lines_list* lnlst, struct txtrdr* txtrdr) { - struct shtr_line tr = SHTR_LINE_NULL; + struct shtr_line ln = SHTR_LINE_NULL; struct param_desc param = PARAM_DESC_NULL; struct shtr* shtr = NULL; char* line = NULL; @@ -82,12 +69,12 @@ parse_line(struct shtr_lines_list* trlst, struct txtrdr* txtrdr) int isotope_id_local; res_T res = RES_OK; - ASSERT(trlst && txtrdr); + ASSERT(lnlst && txtrdr); line = txtrdr_get_line(txtrdr); ASSERT(line); - shtr = trlst->shtr; + shtr = lnlst->shtr; param.path = txtrdr_get_name(txtrdr); param.line = txtrdr_get_line_num(txtrdr); @@ -112,39 +99,40 @@ parse_line(struct shtr_lines_list* trlst, struct txtrdr* txtrdr) } (void)0 PARSE(&molecule_id, 2, int, "molecule identifier", 0,99,1,1); - tr.molecule_id = (int32_t)molecule_id; + ln.molecule_id = (int32_t)molecule_id; PARSE(&isotope_id_local, 1, int, "isotope local identifier", 0,9,1,1); - tr.isotope_id_local = (int32_t) + ln.isotope_id_local = (int32_t) (isotope_id_local == 0 ? 9 : (isotope_id_local - 1)); - PARSE(&tr.wavenumber, 12, double, "central wavenumber", 0,INF,0,1); - PARSE(&tr.intensity, 10, double, "reference intensity", 0,INF,0,1); + PARSE(&ln.wavenumber, 12, double, "central wavenumber", 0,INF,0,1); + PARSE(&ln.intensity, 10, double, "reference intensity", 0,INF,0,1); NEXT(10); /* Skip the Enstein coef */ - PARSE(&tr.gamma_air, 5, double, "air broadening half-width", 0,INF,1,1); - PARSE(&tr.gamma_self, 5, double, "self broadening half-width", 0,INF,1,1); + PARSE(&ln.gamma_air, 5, double, "air broadening half-width", 0,INF,1,1); + PARSE(&ln.gamma_self, 5, double, "self broadening half-width", 0,INF,1,1); /* Handle unavailable lower state energy */ - PARSE(&tr.lower_state_energy, 10, double, "lower state energy",-INF,INF,1,1); - if(tr.lower_state_energy == -1) { + PARSE(&ln.lower_state_energy, 10, double, "lower state energy",-INF,INF,1,1); + if(ln.lower_state_energy == -1) { log_warn(shtr, "%s:%lu: the lower state energy is unavailable for this line, so it is " - "ignored.\n", param.path, param.line); + "ignored.\n", txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr)); goto exit; /* Skip the line */ } /* Check the domain validity */ - if(tr.lower_state_energy < 0) { + if(ln.lower_state_energy < 0) { log_err(shtr, "%s:%lu: invalid lower state energy %g. It must be in [0, INF].\n", - param.path, param.line, tr.lower_state_energy); + txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr), + ln.lower_state_energy); res = RES_BAD_ARG; goto error; } - PARSE(&tr.n_air, 4, double, "temperature-dependent exponent",-INF,INF,1,1); - PARSE(&tr.delta_air, 8, double, "air-pressure wavenumber shift", -INF,INF,1,1); + PARSE(&ln.n_air, 4, double, "temperature-dependent exponent",-INF,INF,1,1); + PARSE(&ln.delta_air, 8, double, "air-pressure wavenumber shift", -INF,INF,1,1); /* Skip the remaining values */ @@ -156,7 +144,7 @@ parse_line(struct shtr_lines_list* trlst, struct txtrdr* txtrdr) *end = backup; str = end; if(strlen(str) != 93) { - log_err(trlst->shtr, "%s:%lu: missing data after delta air.\n", + log_err(lnlst->shtr, "%s:%lu: missing data after delta air.\n", param.path, (unsigned long)param.line); res = RES_BAD_ARG; goto error; @@ -164,7 +152,7 @@ parse_line(struct shtr_lines_list* trlst, struct txtrdr* txtrdr) res = darray_line_push_back(&trlst->lines, &tr); if(res != RES_OK) { - log_err(trlst->shtr, + log_err(lnlst->shtr, "%s:%lu: error storing the line -- %s.\n", param.path, (unsigned long)param.line, res_to_cstr(res)); goto error; @@ -183,15 +171,15 @@ load_stream const char* name, struct shtr_lines_list** out_lines) { - struct shtr_lines_list* trlst = NULL; + struct shtr_lines_list* lnlst = NULL; struct txtrdr* txtrdr = NULL; res_T res = RES_OK; ASSERT(shtr && stream && name && out_lines); - res = create_lines_list(shtr, &trlst); + res = create_lines_list(shtr, &lnlst); if(res != RES_OK) goto error; - res = txtrdr_stream(trlst->shtr->allocator, stream, name, + res = txtrdr_stream(lnlst->shtr->allocator, stream, name, 0/*No comment char*/, &txtrdr); if(res != RES_OK) { log_err(shtr, "%s: error creating the text reader -- %s.\n", @@ -208,18 +196,18 @@ load_stream } if(!txtrdr_get_cline(txtrdr)) break; /* No more parsed line */ - res = parse_line(trlst, txtrdr); + res = parse_line(lnlst, txtrdr); if(res != RES_OK) goto error; } exit: if(txtrdr) txtrdr_ref_put(txtrdr); - *out_lines = trlst; + *out_lines = lnlst; return res; error: - if(trlst) { - SHTR(lines_list_ref_put(trlst)); - trlst = NULL; + if(lnlst) { + SHTR(lines_list_ref_put(lnlst)); + lnlst = NULL; } goto exit; } @@ -228,12 +216,12 @@ static void release_lines(ref_T * ref) { struct shtr* shtr = NULL; - struct shtr_lines_list* trlst = CONTAINER_OF + struct shtr_lines_list* lnlst = CONTAINER_OF (ref, struct shtr_lines_list, ref); ASSERT(ref); - shtr = trlst->shtr; - darray_line_release(&trlst->lines); - MEM_RM(shtr->allocator, trlst); + shtr = lnlst->shtr; + darray_line_release(&lnlst->lines); + MEM_RM(shtr->allocator, lnlst); SHTR(ref_put(shtr)); } @@ -244,12 +232,12 @@ res_T shtr_lines_list_load (struct shtr* shtr, const char* path, - struct shtr_lines_list** trlst) + struct shtr_lines_list** lnlst) { FILE* file = NULL; res_T res = RES_OK; - if(!shtr || !path || !trlst) { + if(!shtr || !path || !lnlst) { res = RES_BAD_ARG; goto error; } @@ -261,7 +249,7 @@ shtr_lines_list_load goto error; } - res = load_stream(shtr, file, path, trlst); + res = load_stream(shtr, file, path, lnlst); if(res != RES_OK) goto error; exit: @@ -276,45 +264,45 @@ shtr_lines_list_load_stream (struct shtr* shtr, FILE* stream, const char* stream_name, - struct shtr_lines_list** trlst) + struct shtr_lines_list** lnlst) { - if(!shtr || !stream || !trlst) return RES_BAD_ARG; + if(!shtr || !stream || !lnlst) return RES_BAD_ARG; return load_stream - (shtr, stream, stream_name ? stream_name : "<stream>", trlst); + (shtr, stream, stream_name ? stream_name : "<stream>", lnlst); } res_T -shtr_lines_list_ref_get(struct shtr_lines_list* trlst) +shtr_lines_list_ref_get(struct shtr_lines_list* lnlst) { - if(!trlst) return RES_BAD_ARG; - ref_get(&trlst->ref); + if(!lnlst) return RES_BAD_ARG; + ref_get(&lnlst->ref); return RES_OK; } res_T -shtr_lines_list_ref_put(struct shtr_lines_list* trlst) +shtr_lines_list_ref_put(struct shtr_lines_list* lnlst) { - if(!trlst) return RES_BAD_ARG; - ref_put(&trlst->ref, release_lines); + if(!lnlst) return RES_BAD_ARG; + ref_put(&lnlst->ref, release_lines); return RES_OK; } res_T shtr_lines_list_get_size - (const struct shtr_lines_list* trlst, + (const struct shtr_lines_list* lnlst, size_t* nlines) { - if(!trlst || !nlines) return RES_BAD_ARG; - *nlines = darray_line_size_get(&trlst->lines); + if(!lnlst || !nlines) return RES_BAD_ARG; + *nlines = darray_line_size_get(&lnlst->lines); return RES_OK; } res_T shtr_lines_list_get - (const struct shtr_lines_list* trlst, + (const struct shtr_lines_list* lnlst, const struct shtr_line* lines_list[]) { - if(!trlst || !lines_list) return RES_BAD_ARG; - *lines_list = darray_line_cdata_get(&trlst->lines); + if(!lnlst || !lines_list) return RES_BAD_ARG; + *lines_list = darray_line_cdata_get(&lnlst->lines); return RES_OK; } diff --git a/src/shtr_lines_list_c.h b/src/shtr_lines_list_c.h @@ -0,0 +1,40 @@ +/* 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 SHTR_LINES_LIST_C_H +#define SHTR_LINES_LIST_C_H + +#include <rsys/dynamic_array.h> +#include <rsys/ref_count.h> + +/* Generate the dynamic array of lines */ +#define DARRAY_NAME line +#define DARRAY_DATA struct shtr_line +#include <rsys/dynamic_array.h> + +struct shtr; + +struct shtr_lines_list { + /* Lines sorted in ascending order wrt their wavenumber */ + struct darray_line lines; + + struct shtr* shtr; + ref_T ref; +}; + +#endif /* SHTR_LINES_LIST_C_H */