Skip to content

Commit

Permalink
Bugfix #2137 develop PointStat -obs_valid_beg/end (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemccabe committed Apr 26, 2023
1 parent cc01e88 commit 2637ca3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
23 changes: 8 additions & 15 deletions internal/tests/pytests/util/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def test_getseconds(metplus_config, input_value, result):
seconds = conf.getseconds('config', 'TEST_SECONDS')
assert seconds == result
except NoOptionError:
if result is None:
assert True
assert result is None


# value = None -- config variable not set
Expand Down Expand Up @@ -64,8 +63,7 @@ def test_getstr(metplus_config, input_value, default, result):
try:
assert result == conf.getstr('config', 'TEST_GETSTR', default)
except NoOptionError:
if default is None:
assert True
assert default is None


# value = None -- config variable not set
Expand All @@ -87,13 +85,11 @@ def test_getdir(metplus_config, input_value, default, result):

# catch NoOptionError exception and pass test if default is None
try:
assert result == conf.getdir('TEST_GETSTR', default=default)
assert result == conf.getdir('TEST_GETDIR', default=default)
except NoOptionError:
if result is 'NoOptionError':
assert True
assert result == 'NoOptionError'
except ValueError:
if result is 'ValueError':
assert True
assert result == 'ValueError'


# value = None -- config variable not set
Expand Down Expand Up @@ -151,8 +147,7 @@ def test_getbool(metplus_config, input_value, default, result):
try:
assert result == conf.getbool('config', 'TEST_GETBOOL', default)
except NoOptionError:
if result is None:
assert True
assert result is None


# value = None -- config variable not set
Expand Down Expand Up @@ -195,8 +190,7 @@ def test_getfloat(metplus_config, input_value, default, result):
try:
assert result == conf.getfloat('config', 'TEST_GETFLOAT', default)
except ValueError:
if result is None:
assert True
assert result is None


# value = None -- config variable not set
Expand Down Expand Up @@ -226,8 +220,7 @@ def test_getint(metplus_config, input_value, default, result):
try:
assert result == conf.getint('config', 'TEST_GETINT', default)
except ValueError:
if result is None:
assert True
assert result is None


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
import pytest

import os
from datetime import datetime, timedelta

from metplus.wrappers.point_stat_wrapper import PointStatWrapper

fcst_dir = '/some/path/fcst'
obs_dir = '/some/path/obs'

inits = ['2005080700', '2005080712']
time_fmt = '%Y%m%d%H'
lead_hour = 12
lead_hour_str = str(lead_hour).zfill(3)
valids = []
for init in inits:
valid = datetime.strptime(init, time_fmt) + timedelta(hours=lead_hour)
valid = valid.strftime(time_fmt)
valids.append(valid)

def set_minimum_config_settings(config):
# set config variables to prevent command from running and bypass check
Expand All @@ -19,11 +29,11 @@ def set_minimum_config_settings(config):
# set process and time config variables
config.set('config', 'PROCESS_LIST', 'PointStat')
config.set('config', 'LOOP_BY', 'INIT')
config.set('config', 'INIT_TIME_FMT', '%Y%m%d%H')
config.set('config', 'INIT_BEG', '2005080700')
config.set('config', 'INIT_END', '2005080712')
config.set('config', 'INIT_TIME_FMT', time_fmt)
config.set('config', 'INIT_BEG', inits[0])
config.set('config', 'INIT_END', inits[-1])
config.set('config', 'INIT_INCREMENT', '12H')
config.set('config', 'LEAD_SEQ', '12H')
config.set('config', 'LEAD_SEQ', f'{lead_hour}H')

config.set('config', 'POINT_STAT_CONFIG_FILE',
'{PARM_BASE}/met_config/PointStatConfig_wrapped')
Expand Down Expand Up @@ -517,6 +527,10 @@ def test_met_dictionary_in_var_options(metplus_config):
{'METPLUS_FCST_FILE_TYPE': 'file_type = NETCDF_PINT;'}),
({'POINT_STAT_SEEPS_P1_THRESH': 'ge0.1&&le0.85', },
{'METPLUS_SEEPS_P1_THRESH': 'seeps_p1_thresh = ge0.1&&le0.85;'}),
({'POINT_STAT_OBS_VALID_BEG': '{valid?fmt=%Y%m%d_%H?shift=-6H}', }, {}),
({'POINT_STAT_OBS_VALID_END': '{valid?fmt=%Y%m%d_%H?shift=6H}', }, {}),
({'POINT_STAT_OBS_VALID_BEG': '{valid?fmt=%Y%m%d_%H?shift=-6H}',
'POINT_STAT_OBS_VALID_END': '{valid?fmt=%Y%m%d_%H?shift=6H}'}, {}),
]
)
@pytest.mark.wrapper_a
Expand Down Expand Up @@ -590,15 +604,26 @@ def test_point_stat_all_fields(metplus_config, config_overrides,
verbosity = f"-v {wrapper.c_dict['VERBOSITY']}"
config_file = wrapper.c_dict.get('CONFIG_FILE')
out_dir = wrapper.c_dict.get('OUTPUT_DIR')
expected_cmds = [(f"{app_path} {verbosity} "
f"{fcst_dir}/2005080700/fcst_file_F012 "
f"{obs_dir}/2005080712/obs_file "
f"{config_file} -outdir {out_dir}/2005080712"),
(f"{app_path} {verbosity} "
f"{fcst_dir}/2005080712/fcst_file_F012 "
f"{obs_dir}/2005080800/obs_file "
f"{config_file} -outdir {out_dir}/2005080800"),
]
extra_args = [' '] * len(inits)
for beg_end in ('BEG', 'END'):
if f'POINT_STAT_OBS_VALID_{beg_end}' in config_overrides:
for index in range(0, len(inits)):
valid_dt = datetime.strptime(valids[index], time_fmt)
if beg_end == 'BEG':
value = valid_dt - timedelta(hours=6)
else:
value = valid_dt + timedelta(hours=6)
value = value.strftime('%Y%m%d_%H')
extra_args[index] += f'-obs_valid_{beg_end.lower()} {value} '

expected_cmds = []
for index in range(0, len(inits)):
expected_cmds.append(
f"{app_path} {verbosity}{extra_args[index]}"
f"{fcst_dir}/{inits[index]}/fcst_file_F{lead_hour_str} "
f"{obs_dir}/{valids[index]}/obs_file "
f"{config_file} -outdir {out_dir}/{valids[index]}"
)

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")
Expand All @@ -610,6 +635,7 @@ def test_point_stat_all_fields(metplus_config, config_overrides,
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

assert len(all_cmds) == len(expected_cmds)
for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd
Expand Down
12 changes: 12 additions & 0 deletions metplus/wrappers/compare_gridded_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,24 @@ def process_fields(self, time_info):
is_directory=True):
return

# set command line arguments
self.set_command_line_arguments(time_info)

# set environment variables needed by MET config file
self.set_environment_variables(time_info)

# run the MET command
self.build()

def set_command_line_arguments(self, time_info):
"""!Set command line arguments in self.args to add to command to run.
Nothing is done for CompareGridded wrapper. This function can be
overwritten in subclasses.
@param time_info dictionary with time information
"""
return

def get_command(self):
"""! Builds the command to run the MET application
@rtype string
Expand Down
8 changes: 7 additions & 1 deletion metplus/wrappers/point_stat_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,13 @@ def create_c_dict(self):

return c_dict

def add_obs_valid_args(self, time_info):
def set_command_line_arguments(self, time_info):
"""!Set command line arguments in self.args to add to command to run.
This function is overwritten from CompareGridded wrapper.
@param time_info dictionary with time information
"""
# set optional obs_valid_beg and obs_valid_end arguments
for ext in ['BEG', 'END']:
if self.c_dict[f'OBS_VALID_{ext}']:
obs_valid = do_string_sub(self.c_dict[f'OBS_VALID_{ext}'],
Expand Down

0 comments on commit 2637ca3

Please sign in to comment.