commit c36eaaccbf54c0becdb8928429284154dae25254
parent a0d248bea82b178b89e8d1d9015cb002eac261b0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 16 Jul 2018 16:21:34 +0200
Cross check the loaded data in the load_from_file test
Diffstat:
5 files changed, 173 insertions(+), 42 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -19,6 +19,10 @@ enable_testing()
option(NO_TEST "Do not build tests" OFF)
+set(HTCP_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+set(NETCDF_PATH ${PROJECT_SOURCE_DIR}/../ CACHE PATH
+ "Path where the NetCDF test files are stored.")
+
################################################################################
# Prerequisites
################################################################################
@@ -31,6 +35,10 @@ include(rcmake_runtime)
include_directories(${RSys_INCLUDE_DIR})
+if(CMAKE_COMPILER_IS_GNUCC)
+ set(MATH_LIB m)
+endif()
+
################################################################################
# Test utilities
################################################################################
@@ -38,7 +46,7 @@ if(NOT NO_TEST)
function(build_test _name)
add_executable(${_name}
${HTCP_SOURCE_DIR}/${_name}.c)
- target_link_libraries(${_name} htcp)
+ target_link_libraries(${_name} htcp ${ARGN})
endfunction()
function(new_test _name)
@@ -53,3 +61,53 @@ endif()
add_subdirectory(htcp)
add_subdirectory(les2htcp)
+################################################################################
+# Test both the les2htcp utility and the htcp library.
+################################################################################
+if(NOT NO_TEST)
+ build_test(test_htcp_load_from_file ${MATH_LIB})
+
+ set(TEST_FILES
+ CEN2D.1.ARMCU.008.diaKCL
+ CERK4.1.ARMCu.008
+ DZVAR.1.ARMCU.008.diaKCL
+ L25km.1.ARMCU.008.diaKCL)
+
+ set(VAR_LISTS RVT RCT PABST THT)
+
+ set(_script ${HTCP_SOURCE_DIR}/dump_netcdf_data.sh)
+ foreach(_file IN LISTS TEST_FILES)
+ set(_netcdf ${NETCDF_PATH}/${_file}.nc)
+
+ if(EXISTS ${_netcdf})
+ set(_output_base ${CMAKE_CURRENT_BINARY_DIR}/${_file})
+
+ add_custom_command(
+ OUTPUT ${_output_base}.htcp
+ COMMAND les2htcp -i ${_netcdf} -fo ${_output_base}.htcp
+ DEPENDS les2htcp
+ COMMENT "${_file}.nc: convert to HTCP fileformat"
+ VERBATIM)
+ add_custom_target(les2htcp-${_file}.nc ALL DEPENDS ${_output_base}.htcp)
+
+ set(_ref_files)
+ set(_cmds)
+ foreach(_var IN LISTS VAR_LISTS)
+ set(_cmds ${_cmds} COMMAND sh ${_script} ${_var} ${_netcdf})
+ set(_ref_files ${_ref_files} ${_output_base}_${_var})
+ endforeach()
+
+ add_custom_command(
+ OUTPUT ${_ref_files}
+ ${_cmds}
+ DEPENDS ${_script} les2htcp-${_file}.nc
+ COMMENT "${_file}.nc: dump raw data"
+ VERBATIM)
+ add_custom_target(dump-${_file}.nc ALL DEPENDS ${_ref_files})
+
+ add_test(test_htcp_load_from_file_${_file}
+ test_htcp_load_from_file ${_output_base}.htcp ${CMAKE_CURRENT_BINARY_DIR})
+ endif()
+ endforeach()
+endif()
+
diff --git a/cmake/htcp/CMakeLists.txt b/cmake/htcp/CMakeLists.txt
@@ -16,8 +16,6 @@
cmake_minimum_required(VERSION 2.8)
project(htcp-library C)
-set(HTCP_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src)
-
################################################################################
# Configure and define targets
################################################################################
@@ -53,7 +51,6 @@ rcmake_setup_devel(htcp HTCP ${VERSION} high_tune/htcp.h)
if(NOT NO_TEST)
new_test(test_htcp)
new_test(test_htcp_load)
- build_test(test_htcp_load_from_file)
endif()
################################################################################
diff --git a/cmake/les2htcp/CMakeLists.txt b/cmake/les2htcp/CMakeLists.txt
@@ -16,8 +16,6 @@
cmake_minimum_required(VERSION 2.8)
project(les2htcp C)
-set(LES2HTCP_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src)
-
################################################################################
# Check dependencies
################################################################################
@@ -38,7 +36,7 @@ set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
-configure_file(${LES2HTCP_SOURCE_DIR}/les2htcp.h.in
+configure_file(${HTCP_SOURCE_DIR}/les2htcp.h.in
${CMAKE_CURRENT_BINARY_DIR}/les2htcp.h @ONLY)
################################################################################
@@ -48,9 +46,9 @@ set(LES2HTCP_FILES_SRC les2htcp.c)
set(LES2HTCP_FILES_INC les2htcp.h.in)
set(LES2HTCP_FILES_DOC COPYING)
-# Prepend each file in the `HTNC_FILES_<SRC|INC>' list by `LES2HTCP_SOURCE_DIR'
-rcmake_prepend_path(LES2HTCP_FILES_SRC ${LES2HTCP_SOURCE_DIR})
-rcmake_prepend_path(LES2HTCP_FILES_INC ${LES2HTCP_SOURCE_DIR})
+# Prepend each file in the `HTNC_FILES_<SRC|INC>' list by `HTCP_SOURCE_DIR'
+rcmake_prepend_path(LES2HTCP_FILES_SRC ${HTCP_SOURCE_DIR})
+rcmake_prepend_path(LES2HTCP_FILES_INC ${HTCP_SOURCE_DIR})
rcmake_prepend_path(LES2HTCP_FILES_DOC ${PROJECT_SOURCE_DIR}/../../)
add_executable(les2htcp ${LES2HTCP_FILES_SRC})
diff --git a/src/dump_netcdf_data.sh b/src/dump_netcdf_data.sh
@@ -0,0 +1,38 @@
+#!/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 LES-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_htcp_load_from_file.c b/src/test_htcp_load_from_file.c
@@ -13,60 +13,100 @@
* 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 "htcp.h"
#include "test_htcp_utils.h"
+#include <rsys/math.h>
+#include <string.h>
+
+/* Potential temperature to temperature
+ * T = THT * (PABST/P0)^(R/(M_DA*Cp0)) */
+static FINLINE double
+THT_to_T(const double THT, const size_t i /*Id of THT*/, const void* ctx)
+{
+ const double* PABST = ctx;
+ const double P0 = 101325;
+#if 0
+ const double R = 8.3144598; /* In kg.m^2.s^-2.mol^-1.K */
+ const double M_DA = 28.9644E-3; /* In kg.mol^-1 */
+ const double Cp0 = 1.005E+3; /* In J/kg/K */
+#endif
+ const double exponent = 0.28562974993698609; /* R/M_DA*cp0 */
+ return THT * pow(PABST[i]/P0, exponent);
+}
+
+static void
+check_variable
+ (const double* loaded_data,
+ const size_t ndata,
+ const char* var, /* Name of the variable to check */
+ const char* path, /* Path where the reference values are */
+ const char* basename, /* Basename of the reference files */
+ double (*convert)(const double, const size_t, const void*), /* May be NULL */
+ const void* ctx)
+{
+ char buf[128];
+ FILE* fp = NULL;
+ size_t i;
+
+ printf("Check the %s variable\n", var);
+
+ CHK(loaded_data && ndata && var && path && basename);
+
+ i = (size_t)snprintf(buf, sizeof(buf), "%s/%s_%s", path, basename, var);
+ CHK(i < sizeof(buf));
+ CHK(fp = fopen(buf, "r"));
+ FOR_EACH(i, 0, ndata) {
+ double val;
+ CHK(fscanf(fp, "%lg", &val) == 1);
+ if(convert) val = convert(val, i, ctx);
+ CHK(eq_eps(val, loaded_data[i], loaded_data[i]*1.e-6));
+ }
+ CHK(fscanf(fp, "%*g") == EOF);
+ CHK(fclose(fp) == 0);
+}
+
int
main(int argc, char** argv)
{
struct htcp* htcp = NULL;
struct htcp_desc desc = HTCP_DESC_NULL;
- size_t i, n;
+ char* filename = NULL;
+ char* path = NULL;
+ char* base = NULL;
+ char* p = NULL;
+ size_t n;
- if(argc < 2) {
- fprintf(stderr, "Usage: %s HTCP_FILE\n", argv[0]);
+ if(argc < 3) {
+ fprintf(stderr, "Usage: %s <htcp> <ref-data-path> \n", argv[0]);
return -1;
}
+ filename = argv[1];
+ path = argv[2];
+
CHK(htcp_create(NULL, &mem_default_allocator, 1, &htcp) == RES_OK);
- CHK(htcp_load(htcp, argv[1]) == RES_OK);
+ CHK(htcp_load(htcp, filename) == RES_OK);
CHK(htcp_get_desc(htcp, &desc) == RES_OK);
- printf("pagesize: %lu\n", (unsigned long)desc.pagesize);
- printf("irregular Z: %i\n", desc.irregular_z);
- printf("#X: %lu; #Y: %lu; #Z: %lu; #times: %lu\n",
- desc.spatial_definition[0],
- desc.spatial_definition[1],
- desc.spatial_definition[2],
- desc.time_definition);
- printf("lower pos: %g %g %g\n", SPLIT3(desc.lower));
- printf("voxel size: %g %g ", desc.vxsz_x, desc.vxsz_y);
- if(!desc.irregular_z) {
- printf("%g\n", desc.vxsz_z[0]);
- } else {
- printf("{");
- FOR_EACH(i, 0, desc.spatial_definition[2]) {
- printf("%g", desc.vxsz_z[i]);
- if(i != desc.spatial_definition[2]-1) printf(", ");
- }
- printf("}");
- }
+ /* Compute the basename of the ref file from the submitted htcp file */
+ p = strrchr(filename, '/');
+ if(p) base = p+1;
+ p = strrchr(base, '.');
+ if(p) *p = '\0';
n = desc.spatial_definition[0]
* desc.spatial_definition[1]
* desc.spatial_definition[2]
* desc.time_definition;
- printf("RVT:\n");
- FOR_EACH(i, 0, n) {
- printf("%g\n", desc.RVT[i]);
- }
-
- printf("RCT:\n");
- FOR_EACH(i, 0, n) {
- printf("%g\n", desc.RCT[i]);
- }
+ check_variable(desc.RVT, n, "RVT", path, base, NULL, NULL);
+ check_variable(desc.RCT, n, "RCT", path, base, NULL, NULL);
+ check_variable(desc.PABST, n, "PABST", path, base, NULL, NULL);
+ check_variable(desc.T, n, "THT", path, base, THT_to_T, desc.PABST);
CHK(htcp_ref_put(htcp) == RES_OK);
check_memory_allocator(&mem_default_allocator);