Skip to content

Commit

Permalink
feature_2253_extract_tiles_tests (#2368)
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Sharples authored Sep 28, 2023
1 parent 8e7618c commit 2aef7ef
Showing 1 changed file with 100 additions and 17 deletions.
117 changes: 100 additions & 17 deletions internal/tests/pytests/wrappers/extract_tiles/test_extract_tiles.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# !/usr/bin/env python3

import pytest

from unittest import mock
import os
import datetime

from metplus.wrappers.extract_tiles_wrapper import ExtractTilesWrapper


def extract_tiles_wrapper(metplus_config):
config = metplus_config
config.set('config', 'PROCESS_LIST', 'ExtractTiles')
Expand All @@ -34,26 +33,42 @@ def extract_tiles_wrapper(metplus_config):
config.set('config', 'EXTRACT_TILES_OUTPUT_DIR',
'{OUTPUT_BASE}/extract_tiles')

config.set('config','FCST_EXTRACT_TILES_INPUT_TEMPLATE', '{INPUT_BASE}/fcst{lead?fmt=%HHH}.grb2')
config.set('config','OBS_EXTRACT_TILES_INPUT_TEMPLATE', '{INPUT_BASE}/obs{lead?fmt=%HHH}.grb2')
config.set('config','FCST_EXTRACT_TILES_OUTPUT_TEMPLATE', 'fcst_out.nc')
config.set('config','OBS_EXTRACT_TILES_OUTPUT_TEMPLATE', 'obs_out.nc')
config.set('config','FCST_EXTRACT_TILES_INPUT_DIR', '{INPUT_BASE}/fcst_tiles')
config.set('config','OBS_EXTRACT_TILES_INPUT_DIR', '{INPUT_BASE}/obs_tiles')

config.set('config','FCST_VAR1_NAME','TMP')
config.set('config','FCST_VAR1_LEVELS', 'Z2')
config.set('config','OBS_VAR1_NAME', 'TMP')
config.set('config','OBS_VAR1_LEVELS', 'Z2')

wrapper = ExtractTilesWrapper(config)
return wrapper


def get_storm_lines(wrapper):
filter_file = os.path.join(wrapper.config.getdir('METPLUS_BASE'),
'internal', 'tests',
'data',
'stat_data',
'fake_filter_20141214_00.tcst')
return get_input_lines(filter_file)
def get_test_file(wrapper, input_type):
if input_type == "mtd":
return os.path.join(wrapper.config.getdir('METPLUS_BASE'),
'internal', 'tests',
'data',
'mtd',
'fake_mtd_2d.txt')
if input_type == "storm":
return os.path.join(wrapper.config.getdir('METPLUS_BASE'),
'internal', 'tests',
'data',
'stat_data',
'fake_filter_20141214_00.tcst')

def get_storm_lines(wrapper):
return get_input_lines(get_test_file(wrapper, 'storm'))


def get_mtd_lines(wrapper):
input_file = os.path.join(wrapper.config.getdir('METPLUS_BASE'),
'internal', 'tests',
'data',
'mtd',
'fake_mtd_2d.txt')
return get_input_lines(input_file)
return get_input_lines(get_test_file(wrapper, 'mtd'))


def get_input_lines(filepath):
Expand Down Expand Up @@ -197,11 +212,79 @@ def test_get_grid_info(metplus_config, lat, lon, expected_result):
@pytest.mark.parametrize(
'lat, lon, expected_result', [
(-54.9, -168.6, 'latlon 60 60 -70.0 -183.5 0.5 0.5'),
(-37.8136, 144.9631, 'latlon 60 60 -53.0 130.0 0.5 0.5'),
]
)
@pytest.mark.wrapper
def test_get_grid(metplus_config, lat, lon, expected_result):
wrapper = extract_tiles_wrapper(metplus_config)
storm_data = {'ALAT': lat, 'ALON': lon}
storm_data = {
'ALAT': lat,
'ALON': lon,
'BLAT': lat,
'BLON': lon,
'CENTROID_LAT': lat,
'CENTROID_LON': lon,
}
assert(wrapper.get_grid('FCST', storm_data) == expected_result)
assert(wrapper.get_grid('OBS', storm_data) == expected_result)
assert(wrapper.get_grid('foo', storm_data,'MTD') == expected_result)

# check error is raised
wrapper.get_grid('TIGER', storm_data)
last_err = wrapper.logger.error.call_args_list[-1][0][0]
assert "Invalid data type provided to get_grid: TIGER" in last_err


@pytest.mark.parametrize(
'tool_config,input_type',[
#TC stat
({
'EXTRACT_TILES_TC_STAT_INPUT_TEMPLATE': 'filter_{init?fmt=%Y%m%d}.tcst',
'EXTRACT_TILES_TC_STAT_INPUT_DIR': '{INPUT_BASE}',
},
"storm"),
#MTD
({
'EXTRACT_TILES_MTD_INPUT_TEMPLATE': 'filter_{init?fmt=%Y%m%d}.tcst',
'EXTRACT_TILES_MTD_INPUT_DIR': '{INPUT_BASE}'
},
"mtd"),
]
)
def test_run_extract_tiles(metplus_config, tool_config, input_type):
config = metplus_config
for key, value in tool_config.items():
config.set('config', key, value)

with mock.patch.object(os.path, "exists", return_value=True):
wrapper = extract_tiles_wrapper(config)
test_file = get_test_file(wrapper, input_type)
with mock.patch.object(wrapper,
"get_location_input_file",
return_value=test_file):
wrapper.run_all_times()
assert wrapper.isOK


def test_get_location_input_file(metplus_config):
config = metplus_config
config.set('config', 'EXTRACT_TILES_TC_STAT_INPUT_TEMPLATE', 'filter_{init?fmt=%Y%m%d}.tcst')
config.set('config', 'EXTRACT_TILES_TC_STAT_INPUT_DIR', '/input/base/')

wrapper = extract_tiles_wrapper(config)
time_info = {
'loop_by': 'init',
'init': datetime.datetime(2014, 12, 14, 0, 0),
}

# Check error on missing file
path = wrapper.get_location_input_file(time_info, 'TC_STAT')
last_err = wrapper.logger.error.call_args_list[-1][0][0]
assert 'Could not find TC_STAT file: /input/base/filter_20141214.tcst' in last_err
assert path == None

with mock.patch.object(os.path, "exists", return_value=True):
path = wrapper.get_location_input_file(time_info, 'TC_STAT')
assert path == '/input/base/filter_20141214.tcst'

0 comments on commit 2aef7ef

Please sign in to comment.