rnatm

Load and structure data describing an atmosphere
git clone git://git.meso-star.fr/rnatm.git
Log | Files | Refs | README | LICENSE

commit 8589f6f6b577b910dedc5ba6afc5ddb5e8330002
parent d3cef5227c97a0d98dcb06451ef51b4397f549b2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 21 Sep 2022 10:39:16 +0200

Rely on RNSL library to load the phase function list

Diffstat:
MREADME.md | 1+
Mcmake/CMakeLists.txt | 4+++-
Msrc/rnatm_properties.c | 129++++++++++++++++---------------------------------------------------------------
Msrc/test_rnatm.c | 2+-
4 files changed, 30 insertions(+), 106 deletions(-)

diff --git a/README.md b/README.md @@ -17,6 +17,7 @@ This library is compatible with 64-bits POSIX systems. It relies the [CMake](http://www.cmake.org) and the [RCMake](https://gitlab.com/vaplv/rcmake/) packages to build. It also depends on the [Rad-Net Scattering Function](https://gitlab.com/meso-star/rnsf), +[RNSL](https://gitlab.com/meso-star/rnsl), [RSys](https://gitlab.com/vaplv/rsys/), [Star-Aerosol](https://gitlab.com/meso-star/star-aerosol), [Star-Buffer](https://gitlab.com/meso-star/star-buffer), diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -30,6 +30,7 @@ option(NO_TEST "Do not build tests" OFF) ################################################################################ find_package(OpenMP 1.2 REQUIRED) find_package(RCMake 0.4 REQUIRED) +find_package(RNSL REQUIRED) find_package(RNSF REQUIRED) find_package(RSys 0.12.1 REQUIRED) find_package(StarAerosol REQUIRED) @@ -44,6 +45,7 @@ include(rcmake) include(rcmake_runtime) include_directories( + ${RNSL_INCLUDE_DIR} ${RNSF_INCLUDE_DIR} ${RSys_INCLUDE_DIR} ${StarAerosol_INCLUDE_DIR} @@ -86,7 +88,7 @@ rcmake_prepend_path(RNATM_FILES_INC_API ${RNATM_SOURCE_DIR}) rcmake_prepend_path(RNATM_FILES_DOC ${PROJECT_SOURCE_DIR}/../) add_library(rnatm SHARED ${RNATM_FILES_SRC} ${RNATM_FILES_INC} ${RNATM_FILES_INC_API}) -target_link_libraries(rnatm RNSF RSys StarAerosol StarBuffer StarCK StarMesh +target_link_libraries(rnatm RNSL RNSF RSys StarAerosol StarBuffer StarCK StarMesh StarUVM StarVX m) set_target_properties(rnatm PROPERTIES diff --git a/src/rnatm_properties.c b/src/rnatm_properties.c @@ -18,13 +18,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#define _POSIX_C_SOURCE 200112L /* strtok_r and wordexp */ - #include "rnatm.h" #include "rnatm_c.h" #include "rnatm_log.h" #include <rad-net/rnsf.h> +#include <rad-net/rnsl.h> #include <star/sars.h> #include <star/sbuf.h> @@ -32,10 +31,6 @@ #include <rsys/clock_time.h> #include <rsys/cstr.h> -#include <rsys/text_reader.h> - -#include <string.h> -#include <wordexp.h> /******************************************************************************* * Helper functions @@ -142,56 +137,15 @@ check_aerosol_sars_desc } static res_T -parse_phase_fn +load_phase_fn (struct rnatm* atm, - struct aerosol* aerosol, - struct txtrdr* txtrdr, + const char* filename, struct rnsf** out_phase_fn) { - wordexp_t wexp; - char* tk = NULL; - char* tk_ctx = NULL; struct rnsf_create_args args = RNSF_CREATE_ARGS_DEFAULT; struct rnsf* phase_fn = NULL; - int wexp_is_allocated = 0; res_T res = RES_OK; - int err = 0; - ASSERT(atm && aerosol && txtrdr && out_phase_fn); - - res = txtrdr_read_line(txtrdr); - if(res != RES_OK) { - log_err(atm, "%s: can't read the line `%lu' -- %s\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - res_to_cstr(res)); - goto error; - } - - if(!txtrdr_get_cline(txtrdr)) { - const size_t nexpect = darray_phase_fn_size_get(&aerosol->phase_fn_lst); - const size_t nparsed = - (size_t)(out_phase_fn - darray_phase_fn_cdata_get(&aerosol->phase_fn_lst)); - log_err(atm, - "%s:%lu: missing a phase function filename. " - "Expecting %lu phase function%s while %lu %s parsed.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - (unsigned long)nexpect, nexpect == 1 ? " " : "s ", - (unsigned long)nparsed, nparsed > 1 ? "were" : "was"); - res = RES_BAD_ARG; - goto error; - } - - tk = strtok_r(txtrdr_get_line(txtrdr), "", &tk_ctx); - ASSERT(tk); - - err = wordexp(tk, &wexp, 0/*flags*/); - if(err) { - log_err(atm, "%s:%lu: unable to expand phase function filename\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); - res = RES_BAD_ARG; - goto error; - } - wexp_is_allocated = 1; - ASSERT(wexp.we_wordc != 0); + ASSERT(atm && filename && out_phase_fn); args.verbose = atm->verbose; args.logger = atm->logger; @@ -199,22 +153,15 @@ parse_phase_fn res = rnsf_create(&args, &phase_fn); if(res != RES_OK) { log_err(atm, - "%s:%lu: could not create the Rad-Net Scattering Function data structure\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); + "%s: could not create the Rad-Net Scattering Function data structure\n", + filename); goto error; } - res = rnsf_load(phase_fn, wexp.we_wordv[0]); + res = rnsf_load(phase_fn, filename); if(res != RES_OK) goto error; - if(wexp.we_wordc > 1) { - log_warn(atm, "%s:%lu: unexpected text `%s'\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - wexp.we_wordv[1]); - } - exit: - if(wexp_is_allocated) wordfree(&wexp); *out_phase_fn = phase_fn; return res; error: @@ -223,74 +170,48 @@ error: } static res_T -parse_phase_fn_list +load_phase_fn_list (struct rnatm* atm, struct aerosol* aerosol, const struct rnatm_aerosol_args* args) { - struct txtrdr* txtrdr = NULL; - char* tk = NULL; - char* tk_ctx = NULL; - size_t iphase_fn = 0; - unsigned long nphase_fn = 0; + struct rnsl_create_args rnsl_args = RNSL_CREATE_ARGS_DEFAULT; + struct rnsl* rnsl = NULL; + size_t iphase_fn, nphase_fn; res_T res = RES_OK; - ASSERT(atm && aerosol && args); - res = txtrdr_file(atm->allocator, args->phase_fn_lst_filename, '#', &txtrdr); + rnsl_args.logger = atm->logger; + rnsl_args.allocator = atm->allocator; + rnsl_args.verbose = atm->verbose; + res = rnsl_create(&rnsl_args, &rnsl); if(res != RES_OK) { - log_err(atm, "could not create text reader to parse file `%s' -- %s\n", + log_err(atm, + "Failed to create loader for phase function list `%s' -- %s\n", args->phase_fn_lst_filename, res_to_cstr(res)); goto error; } - res = txtrdr_read_line(txtrdr); - if(res != RES_OK) { - log_err(atm, "%s: can't read the line %lu --%s\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - res_to_cstr(res)); - goto error; - } - - if(!txtrdr_get_cline(txtrdr)) { - log_err(atm, "%s: file cannot be empty\n", txtrdr_get_name(txtrdr)); - res = RES_BAD_ARG; - goto error; - } - - tk = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); - ASSERT(tk); - - res = cstr_to_ulong(tk, &nphase_fn); - if(res == RES_OK && nphase_fn == 0) res = RES_BAD_ARG; - if(res != RES_OK) { - log_err(atm, "%s:%lu: invalid number of phase functions %lu\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - nphase_fn); - goto error; - } + res = rnsl_load(rnsl, args->phase_fn_lst_filename); + if(res != RES_OK) goto error; + nphase_fn = rnsl_get_strings_count(rnsl); res = darray_phase_fn_resize(&aerosol->phase_fn_lst, nphase_fn); if(res != RES_OK) { log_err(atm, "%s: could not allocate the list of %lu phase functions -- %s\n", - txtrdr_get_name(txtrdr), nphase_fn, res_to_cstr(res)); + args->phase_fn_lst_filename, nphase_fn, res_to_cstr(res)); goto error; } - tk = strtok_r(NULL, " \t", &tk_ctx); - if(tk) { - log_warn(atm, "%s:%lu: unexpected text `%s'\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), tk); - } - FOR_EACH(iphase_fn, 0, nphase_fn) { + const char* filename = rnsl_get_string(rnsl, iphase_fn); struct rnsf** phase_fn = darray_phase_fn_data_get(&aerosol->phase_fn_lst)+iphase_fn; - res = parse_phase_fn(atm, aerosol, txtrdr, phase_fn); + res = load_phase_fn(atm, filename, phase_fn); if(res != RES_OK) goto error; } exit: - if(txtrdr) txtrdr_ref_put(txtrdr); + if(rnsl) RNSL(ref_put(rnsl)); return res; error: darray_phase_fn_clear(&aerosol->phase_fn_lst); @@ -390,7 +311,7 @@ setup_aerosol_properties log_info(atm, "load aerosol%lu properties\n", (unsigned long)iaerosol); time_current(&t0); - res = parse_phase_fn_list(atm, aerosol, aerosol_args); + res = load_phase_fn_list(atm, aerosol, aerosol_args); if(res != RES_OK) goto error; /* Create the Star-Buffer loader */ diff --git a/src/test_rnatm.c b/src/test_rnatm.c @@ -58,7 +58,7 @@ print_help(const char* cmd) printf( " mesh=path aerosol mesh (smsh(5))\n" " radprop=path radiative properties (sars(5))\n" -" phasefn=path list of phase functions\n" +" phasefn=path list of phase functions (rnsl(5))\n" " phaseids=path phase function id per node\n\n"); printf( " -d file write the builded octrees to file according to the VTK\n"