1# Helper functions to abstract interpolation tools/environments
2
3
4# Set testing tools/environments:
5# - int (internal)
6# - gg_gridname (internal)
7# - grib_compare (bundle, on provided path, or on system path)
8# - cmp (strict binary comparison)
9
10if( ENABLE_INSTALL_TOOLS )
11  get_target_property( CMD_INT         int         LOCATION )
12  get_target_property( CMD_GG_GRIDNAME gg_gridname LOCATION )
13endif()
14
15if( TARGET grib_compare )
16  get_target_property( CMD_GRIB_COMPARE grib_compare LOCATION )
17endif()
18if( NOT CMD_GRIB_COMPARE )
19  find_program( CMD_GRIB_COMPARE grib_compare NO_DEFAULT_PATH HINTS "${eccodes_BASE_DIR}" PATH_SUFFIXES "bin" )
20endif()
21if( NOT CMD_GRIB_COMPARE )
22  find_program( CMD_GRIB_COMPARE grib_compare )
23endif()
24
25find_program( CMD_CMP cmp )
26
27
28if( NOT eccodes_BASE_DIR AND CMD_GRIB_COMPARE )
29  get_filename_component( _grib_compare_dir "${CMD_GRIB_COMPARE}" PATH )
30  get_filename_component( eccodes_BASE_DIR "${_grib_compare_dir}" ABSOLUTE )
31  set( eccodes_BASE_DIR "${eccodes_BASE_DIR}/../" )
32endif()
33
34unset( _grib_environment )
35if( grib_api_BASE_DIR )
36  set( _grib_environment
37    GRIB_DEFINITION_PATH=${grib_api_BASE_DIR}/share/${grib_handling_pkg}/definitions
38    GRIB_SAMPLES_PATH=${grib_api_BASE_DIR}/share/${grib_handling_pkg}/samples )
39endif()
40set( _emos_environment
41  "${_grib_environment}"
42# JDCNDBG=2
43# INTF2_DEBUG=1
44  CONFIG_INTERP=ON_FLY
45  MARS_LSM_PATH=${PROJECT_SOURCE_DIR}/tables/interpol
46  ECMWF_LOCAL_TABLE_PATH=${PROJECT_SOURCE_DIR}/gribtables
47  LOCAL_DEFINITION_TEMPLATES=${PROJECT_SOURCE_DIR}/gribtemplates )
48
49
50# log information
51ecbuild_info( "Test tool int:          ${CMD_INT}" )
52ecbuild_info( "Test tool gg_gridname:  ${CMD_GG_GRIDNAME}" )
53ecbuild_info( "Test tool grib_compare: ${CMD_GRIB_COMPARE}" )
54ecbuild_info( "Test tool cmp:          ${CMD_CMP}" )
55if( NOT CMD_INT OR NOT CMD_GG_GRIDNAME OR NOT CMD_GRIB_COMPARE OR NOT CMD_CMP )
56  ecbuild_warn( "(some tools not found, some tests disabled)" )
57endif()
58ecbuild_debug( "Test _grib_environment: ${_grib_environment}" )
59ecbuild_debug( "Test _emos_environment: ${_emos_environment}" )
60
61
62# Easily add interpolation tests
63# (optional arg #5: test dependencies, default none)
64# (optional arg #6: environment variables, default none additional)
65function( interpolation_add_test_interpol
66        _label
67        _file1
68        _file2
69        _options )
70    set( _depends     "" )
71    set( _environment "" )
72    if( ${ARGC} GREATER 4 )
73        set( _depends "${ARGV4}" )
74    endif()
75    if( ${ARGC} GREATER 5 )
76        set( _environment "${ARGV5}" )
77    endif()
78    ecbuild_add_test(
79        TARGET       ${_label}_interpol
80        TEST_DEPENDS ${_depends}
81        DEPENDS      int
82        COMMAND      ${CMD_INT}
83        ARGS         --input=${_file1} --output=${_file2} ${_options}
84        ENVIRONMENT  ${_environment} ${_emos_environment} )
85endfunction()
86
87
88# Easily add comparison-to-reference results tests
89# (optional arg #4: test dependencies, default none)
90function( interpolation_add_test_compare
91        _label
92        _file1
93        _file2 )
94    set( _depends "" )
95    if( ${ARGC} GREATER 3 )
96        set( _depends "${ARGV3}" )
97    endif()
98    set( _tolerate "" )
99    if(    (label MATCHES "F640_to_(F48|F80_sub-area|regular_ll)$")
100        OR (label MATCHES "(N640|O1280)_to_(F48|F80|regular_ll)(|_sub-area)$")
101        OR (label MATCHES "(N640|O1280)_to_(N80|O80)$")
102        OR (label MATCHES "O1280_to_regular_ll_1-16$")
103        OR (label MATCHES "regular_ll_to_regular_ll$") )
104        set( _tolerate -P -T 2 )
105    endif()
106    ecbuild_add_test(
107        TARGET       ${_label}_compare
108        TEST_DEPENDS ${_label}_interpol ${_depends}
109        COMMAND      ${CMD_GRIB_COMPARE}
110        ARGS         ${_tolerate} ${_file1} ${_file2}
111        ENVIRONMENT  ${_grib_environment} )
112endfunction()
113
114
115# Easily add expected-gridname tests
116function( interpolation_add_test_gridname
117        _label
118        _file
119        _gridname )
120    ecbuild_add_test(
121        TARGET       ${_label}_gridname
122        TEST_DEPENDS ${_label}_interpol
123        CONDITION    ENABLE_INSTALL_TOOLS
124        COMMAND      ${CMD_GG_GRIDNAME}
125        ARGS         --eq=${_gridname} ${_file}
126        ENVIRONMENT  ${_grib_environment} )
127endfunction()
128
129