htmie

Optical properties of water droplets
git clone git://git.meso-star.fr/htmie.git
Log | Files | Refs | README | LICENSE

commit 90b34bf25de0d45aaef60834617919a95aa40f2e
parent dc8518ff8d2f4ae34af2ab71cff43fe0293c2813
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  4 Jul 2018 15:25:49 +0200

Test the loading procedure

Diffstat:
A.gitattributes | 1+
M.gitignore | 1-
AMie_LUT_Cloud.nc | 2++
Mcmake/CMakeLists.txt | 34+++++++++++++++++++++++++++++++++-
Asrc/dump_netcdf_data.sh | 39+++++++++++++++++++++++++++++++++++++++
Msrc/test_htmie_load.c | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
6 files changed, 151 insertions(+), 7 deletions(-)

diff --git a/.gitattributes b/.gitattributes @@ -0,0 +1 @@ +Mie_LUT_Cloud.nc filter=wad diff --git a/.gitignore b/.gitignore @@ -9,5 +9,4 @@ tmp *.orig *~ tags -*.nc *.htcp diff --git a/Mie_LUT_Cloud.nc b/Mie_LUT_Cloud.nc @@ -0,0 +1 @@ +#$# git-wad 63c4b463b903e6415b3194d0b9c12bcf3883c393816044ad8faa948e6f078ede 62504928 +\ No newline at end of file diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -88,7 +88,39 @@ if(NOT NO_TEST) endfunction() new_test(test_htmie) - new_test(test_htmie_load) + build_test(test_htmie_load) + + set(TEST_FILES Mie_LUT_Cloud) + + foreach(_file IN LISTS TEST_FILES) + set(_netcdf ${PROJECT_SOURCE_DIR}/../${_file}.nc) + set(_script ${HTMIE_SOURCE_DIR}/dump_netcdf_data.sh) + set(_output_base ${CMAKE_CURRENT_BINARY_DIR}/${_file}) + add_custom_command( + OUTPUT + ${_output_base}_lambda + ${_output_base}_macs + ${_output_base}_mscs + ${_output_base}_rmod + ${_output_base}_smod + COMMAND sh ${_script} lambda ${_netcdf} + COMMAND sh ${_script} macs ${_netcdf} + COMMAND sh ${_script} mscs ${_netcdf} + COMMAND sh ${_script} rmod ${_netcdf} + COMMAND sh ${_script} smod ${_netcdf} + DEPENDS ${_script} + COMMENT "Dump data of the ${_file} NetCDF" + VERBATIM) + add_test(test_htmie_load_${_file} + test_htmie_load ${_netcdf} ${CMAKE_CURRENT_BINARY_DIR}) + add_custom_target(dump-NetCDF-data ALL DEPENDS + ${_output_base}_lambda + ${_output_base}_macs + ${_output_base}_mscs + ${_output_base}_rmod + ${_output_base}_smod) + endforeach() + endif() ################################################################################ diff --git a/src/dump_netcdf_data.sh b/src/dump_netcdf_data.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Copyright (C) 2018 |Meso|Star> (contact@meso-star.com) +# +# 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/>. */ +set -e +set -o pipefail + +if [ $# -lt 2 ]; then + echo "Usage: $0 VAR-NAME MIE-NETCDF " + exit 0 +fi + +if [ ! -f $2 ]; then + echo "\"$2\" is not a valid file." + exit 0 +fi + +name=$(basename $2) +name=${name%%.*} + +ncdump -v $1 $2 \ + | sed -n "/^ *$1 *=/,\$p" \ + | sed "s/^ *$1 *= *//g" \ + | sed 's/[;} ]//g' \ + | sed 's/,/\n/g' \ + | sed '/^ *$/d' > ${name}_${1} + diff --git a/src/test_htmie_load.c b/src/test_htmie_load.c @@ -1,6 +1,6 @@ /* Copyright (C) 2018 |Meso|Star> (contact@meso-star.com) * -* This program is free software: you can redistribute it and/or modify + * 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. @@ -13,22 +13,36 @@ * 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 /* snprintf support */ + #include "htmie.h" #include "test_htmie_utils.h" +#include <rsys/math.h> +#include <string.h> + int main(int argc, char** argv) { - struct htmie* htmie = NULL; - const char* filename = NULL; + char buf[128]; struct mem_allocator allocator; + char* filename = NULL; + char* path = NULL; + char* base = NULL; + char* p = NULL; + FILE* fp = NULL; + struct htmie* htmie = NULL; + double rmod, smod; + double r_bar, avg_vol; + size_t i; - if(argc < 2) { - fprintf(stderr, "Usage: %s NETCDF\n", argv[0]); + if(argc < 3) { + fprintf(stderr, "Usage: %s <netcdf> <ref-data-path>\n", argv[0]); return -1; } filename = argv[1]; + path = argv[2]; CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); CHK(htmie_create(NULL, &allocator, 1, &htmie) == RES_OK); @@ -38,6 +52,63 @@ main(int argc, char** argv) CHK(htmie_load(NULL, filename) == RES_BAD_ARG); CHK(htmie_load(htmie, filename) == RES_OK); + p = strrchr(filename, '/'); + if(p) base = p+1; + p = strrchr(base, '.'); + if(p) *p = '\0'; + + /* Check the wavelengths list */ + CHK((size_t)snprintf(buf, sizeof(buf), "%s/%s_lambda", path, base)<sizeof(buf)); + CHK(fp = fopen(buf, "r")); + FOR_EACH(i, 0, htmie_get_wavelengths_count(htmie)) { + double lambda; + CHK(fscanf(fp, "%lg", &lambda) == 1); + CHK(eq_eps(lambda, htmie_get_wavelengths(htmie)[i], 1.e-6)); + } + CHK(fscanf(fp, "%*g") == EOF); + CHK(fclose(fp) == 0); + + /* Read rmod */ + CHK((size_t)snprintf(buf, sizeof(buf), "%s/%s_rmod", path, base)<sizeof(buf)); + CHK(fp = fopen(buf, "r")); + CHK(fscanf(fp, "%lg", &rmod) == 1); + CHK(fscanf(fp, "%*g") == EOF); + CHK(fclose(fp) == 0); + + /* Read smod */ + CHK((size_t)snprintf(buf, sizeof(buf), "%s/%s_smod", path, base)<sizeof(buf)); + CHK(fp = fopen(buf, "r")); + CHK(fscanf(fp, "%lg", &smod) == 1); + CHK(fscanf(fp, "%*g") == EOF); + CHK(fclose(fp) == 0); + + r_bar = exp(rmod); + avg_vol = 4.0/3.0*PI*r_bar*r_bar*r_bar*exp(4.5*smod*smod); + + /* Check absorption cross sections */ + CHK((size_t)snprintf(buf, sizeof(buf), "%s/%s_macs", path, base)<sizeof(buf)); + CHK(fp = fopen(buf, "r")); + FOR_EACH(i, 0, htmie_get_wavelengths_count(htmie)) { + const double Cabs = htmie_get_xsections_absorption(htmie)[i]; + double macs; + CHK(fscanf(fp, "%lg", &macs) == 1); + CHK(eq_eps(macs, Cabs / (avg_vol * 1.e-15), 1.e-6)); + } + CHK(fscanf(fp, "%*g") == EOF); + CHK(fclose(fp) == 0); + + /* Check scattering cross sections */ + CHK((size_t)snprintf(buf, sizeof(buf), "%s/%s_mscs", path, base)<sizeof(buf)); + CHK(fp = fopen(buf, "r")); + FOR_EACH(i, 0, htmie_get_wavelengths_count(htmie)) { + const double Csca = htmie_get_xsections_scattering(htmie)[i]; + double mscs; + CHK(fscanf(fp, "%lg", &mscs) == 1); + CHK(eq_eps(mscs, Csca / (avg_vol * 1.e-15), 1.e-6)); + } + CHK(fscanf(fp, "%*g") == EOF); + CHK(fclose(fp) == 0); + CHK(htmie_ref_put(htmie) == RES_OK); check_memory_allocator(&allocator);