Skip to content

Commit 52bb15b

Browse files
committed
Added tested function 'hres_ic.set_replace_function()' to define which replacement function to use, based on the value of the command line argument '--type'.
1 parent fbe6b3f commit 52bb15b

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/hres_ic.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,30 @@ def parse_arguments():
7878
parser.add_argument('--type', default="era5land")
7979
return parser.parse_args()
8080

81-
def main():
81+
def set_replace_function(data_type):
82+
"""
83+
Set what replace function to use, based on the command line argument '--type'.
8284
85+
Parameters
86+
----------
87+
data_type: str
88+
Type of data source for the replacement. The value of the command line argument '--type'.
89+
90+
Returns
91+
-------
92+
callable or None
93+
The function to be called for the replacement, or None if no replacement needs to take place.
94+
"""
95+
if data_type == "era5land":
96+
replace_function = replace_landsurface_with_ERA5land_IC.swap_land_era5land
97+
elif data_type == "barra":
98+
replace_function = replace_landsurface_with_BARRA2R_IC.swap_land_barra
99+
else:
100+
replace_function = None
101+
return replace_function
102+
103+
104+
def main():
83105
"""
84106
The main function that creates a worker pool and generates single GRIB files
85107
for requested date/times in parallel.
@@ -102,12 +124,10 @@ def main():
102124
print(f"replacement_file = {args.file}")
103125
print(f"start_time = {t}")
104126

105-
# If necessary replace ERA5 land/surface fields with higher-resolution options
106-
if "era5land" in args.type:
107-
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t)
108-
replace_input_file_with_tmp_input_file(args.file)
109-
elif "barra" in args.type:
110-
replace_landsurface_with_BARRA2R_IC.swap_land_barra(args.mask, args.file, t)
127+
# If necessary replace land/surface fields with higher-resolution options
128+
replace_function = set_replace_function(args.type)
129+
if replace_function is not None:
130+
replace_function(args.mask, args.file, t)
111131
replace_input_file_with_tmp_input_file(args.file)
112132
else:
113133
print("No need to swap out IC")

tests/test_hres.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
srcpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'src') #To delete when src is a package
1313
sys.path.insert(0,srcpath) #To delete when src is a package
1414

15-
from hres_ic import get_start_time, replace_input_file_with_tmp_input_file, parse_arguments
15+
from hres_ic import get_start_time, replace_input_file_with_tmp_input_file, parse_arguments, set_replace_function
1616

1717
del sys.path[0] #To delete when src is a package
1818

@@ -48,7 +48,7 @@ def test_parse_arguments_with_type():
4848
args = parse_arguments()
4949
assert args.type == 'newtype'
5050

51-
@patch('sys.argv', ['program_name', '--file', 'file_path', '--start', '2024-08-12'])
51+
@patch('sys.argv', ['program_name', '--file', 'file_path', '--start', '202408121230'])
5252
def test_parse_arguments_missing_mask():
5353
with pytest.raises(SystemExit):
5454
parse_arguments()
@@ -61,4 +61,18 @@ def test_parse_arguments_missing_file():
6161
@patch('sys.argv', ['program_name', '--mask', 'mask_path', '--file', 'file_path'])
6262
def test_parse_arguments_missing_start():
6363
with pytest.raises(SystemExit):
64-
parse_arguments()
64+
parse_arguments()
65+
66+
@patch('replace_landsurface_with_ERA5land_IC.swap_land_era5land')
67+
def test_set_replace_function_era5land(mock_era5land):
68+
result = set_replace_function("era5land")
69+
assert result == mock_era5land
70+
71+
@patch('replace_landsurface_with_BARRA2R_IC.swap_land_barra')
72+
def test_set_replace_function_barra(mock_barra):
73+
result = set_replace_function("barra")
74+
assert result == mock_barra
75+
76+
def test_set_replace_function_unknown():
77+
result = set_replace_function("unknown")
78+
assert result is None

0 commit comments

Comments
 (0)