Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #2137 develop PointStat -obs_valid_beg/end #2141

Merged
merged 5 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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