Skip to content

Commit

Permalink
formatted files with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
royagrace committed Aug 1, 2024
1 parent bd64137 commit 4e7b657
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 352 deletions.
47 changes: 24 additions & 23 deletions test/test_HRRR_ztd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@


def test_scenario_1(tmp_path, data_for_hrrr_ztd, mocker):

with pushd (tmp_path):

with pushd(tmp_path):
dct_group = {
'aoi_group': {'bounding_box': [36, 37, -92, -91]},
'date_group': {'date_start': '20200101'},
'time_group': {'time': '12:00:00', 'interpolate_time': 'none'},
'weather_model': 'HRRR',
'height_group': {'height_levels': [0, 50, 100, 500, 1000]},
'look_dir': 'right',
'runtime_group': {'output_directory': 'test/scenario_1'}
"aoi_group": {"bounding_box": [36, 37, -92, -91]},
"date_group": {"date_start": "20200101"},
"time_group": {"time": "12:00:00", "interpolate_time": "none"},
"weather_model": "HRRR",
"height_group": {"height_levels": [0, 50, 100, 500, 1000]},
"look_dir": "right",
"runtime_group": {"output_directory": "test/scenario_1"},
}

cfg = update_yaml(dct_group, os.path.join(tmp_path,'temp.yaml'))
cfg = update_yaml(dct_group, os.path.join(tmp_path, "temp.yaml"))

SCENARIO_DIR = os.path.join(tmp_path, TEST_DIR, "scenario_1")
mocker.patch('RAiDER.processWM.prepareWeatherModel',
side_effect=[str(data_for_hrrr_ztd)])
calcDelays([os.path.join(tmp_path,'temp.yaml')])

new_data = xr.load_dataset(os.path.join(tmp_path, 'test', 'scenario_1', 'HRRR_tropo_20200101T120000_ztd.nc'))
new_data1 = new_data.sel(x=-91.84, y=36.84, z=0, method='nearest')
golden_data = 2.2622863, 0.0361021 # hydro|wet

np.testing.assert_almost_equal(golden_data[0], new_data1['hydro'].data)
np.testing.assert_almost_equal(golden_data[1], new_data1['wet'].data)


mocker.patch(
"RAiDER.processWM.prepareWeatherModel", side_effect=[str(data_for_hrrr_ztd)]
)
calcDelays([os.path.join(tmp_path, "temp.yaml")])

new_data = xr.load_dataset(
os.path.join(
tmp_path, "test", "scenario_1", "HRRR_tropo_20200101T120000_ztd.nc"
)
)
new_data1 = new_data.sel(x=-91.84, y=36.84, z=0, method="nearest")
golden_data = 2.2622863, 0.0361021 # hydro|wet

np.testing.assert_almost_equal(golden_data[0], new_data1["hydro"].data)
np.testing.assert_almost_equal(golden_data[1], new_data1["wet"].data)
133 changes: 70 additions & 63 deletions test/test_checkArgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,169 +19,176 @@
SCENARIO_1 = os.path.join(TEST_DIR, "scenario_1")
SCENARIO_2 = os.path.join(TEST_DIR, "scenario_2")


@pytest.fixture(autouse=True)
def args():
d = DEFAULT_DICT
d['date_list'] = [datetime.datetime(2018, 1, 1)]
d['time'] = datetime.time(12,0,0)
d['aoi'] = BoundingBox([38, 39, -92, -91])
d['los'] = Zenith()
d['weather_model'] = GMAO()

for f in 'weather_files weather_dir'.split():
shutil.rmtree(f) if os.path.exists(f) else ''
d["date_list"] = [datetime.datetime(2018, 1, 1)]
d["time"] = datetime.time(12, 0, 0)
d["aoi"] = BoundingBox([38, 39, -92, -91])
d["los"] = Zenith()
d["weather_model"] = GMAO()

for f in "weather_files weather_dir".split():
shutil.rmtree(f) if os.path.exists(f) else ""
return d


def isWriteable(dirpath):
'''Test whether a directory is writeable'''
"""Test whether a directory is writeable"""
try:
filehandle = open(os.path.join(dirpath, 'tmp.txt'), 'w')
filehandle = open(os.path.join(dirpath, "tmp.txt"), "w")
filehandle.close()
return True
except IOError:
return False


def test_checkArgs_outfmt_1(args):
'''Test that passing height levels with hdf5 outformat works'''
"""Test that passing height levels with hdf5 outformat works"""
args = args
args.file_format = 'h5'
args.file_format = "h5"
args.heightlvls = [10, 100, 1000]
checkArgs(args)
assert os.path.splitext(args.wetFilenames[0])[-1] == '.h5'
assert os.path.splitext(args.wetFilenames[0])[-1] == ".h5"


def test_checkArgs_outfmt_2(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
args.heightlvs = [10, 100, 1000]
args.file_format = 'GTiff'
args.file_format = "GTiff"
args = checkArgs(args)
assert os.path.splitext(args.wetFilenames[0])[-1] == '.nc'
assert os.path.splitext(args.wetFilenames[0])[-1] == ".nc"


def test_checkArgs_outfmt_3(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
with pytest.raises(FileNotFoundError):
args.aoi = StationFile(os.path.join('fake_dir', 'stations.csv'))
args.aoi = StationFile(os.path.join("fake_dir", "stations.csv"))


def test_checkArgs_outfmt_4(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
args.aoi = RasterRDR(
lat_file = os.path.join(SCENARIO_1, 'geom', 'lat.dat'),
lon_file = os.path.join(SCENARIO_1, 'geom', 'lon.dat'),
lat_file=os.path.join(SCENARIO_1, "geom", "lat.dat"),
lon_file=os.path.join(SCENARIO_1, "geom", "lon.dat"),
)
argDict = checkArgs(args)
assert argDict.aoi.type()=='radar_rasters'
assert argDict.aoi.type() == "radar_rasters"


def test_checkArgs_outfmt_5(args, tmp_path):
with pushd(tmp_path):
args = args
args.aoi = StationFile(os.path.join(SCENARIO_2, 'stations.csv'))
args.aoi = StationFile(os.path.join(SCENARIO_2, "stations.csv"))
argDict = checkArgs(args)
assert pd.read_csv(argDict['wetFilenames'][0]).shape == (8, 4)
assert pd.read_csv(argDict["wetFilenames"][0]).shape == (8, 4)


def test_checkArgs_outloc_1(args):
'''Test that the default output and weather model directories are correct'''
"""Test that the default output and weather model directories are correct"""
args = args
argDict = checkArgs(args)
out = argDict['output_directory']
wmLoc = argDict['weather_model_directory']
out = argDict["output_directory"]
wmLoc = argDict["weather_model_directory"]
assert os.path.abspath(out) == os.getcwd()
assert os.path.abspath(wmLoc) == os.path.join(os.getcwd(), 'weather_files')
assert os.path.abspath(wmLoc) == os.path.join(os.getcwd(), "weather_files")


def test_checkArgs_outloc_2(args, tmp_path):
'''Tests that the correct output location gets assigned when provided'''
"""Tests that the correct output location gets assigned when provided"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
argDict = checkArgs(args)
out = argDict['output_directory']
out = argDict["output_directory"]
assert out == tmp_path


def test_checkArgs_outloc_2b(args, tmp_path):
''' Tests that the weather model directory gets passed through by itself'''
"""Tests that the weather model directory gets passed through by itself"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
args.weather_model_directory = 'weather_dir'
args.weather_model_directory = "weather_dir"
argDict = checkArgs(args)
assert argDict['weather_model_directory'] == 'weather_dir'
assert argDict["weather_model_directory"] == "weather_dir"


def test_checkArgs_outloc_3(args, tmp_path):
'''Tests that the weather model directory gets created when needed'''
"""Tests that the weather model directory gets created when needed"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
argDict = checkArgs(args)
assert os.path.isdir(argDict['weather_model_directory'])
assert os.path.isdir(argDict["weather_model_directory"])


def test_checkArgs_outloc_4(args):
'''Tests for creating writeable weather model directory'''
"""Tests for creating writeable weather model directory"""
args = args
argDict = checkArgs(args)

assert isWriteable(argDict['weather_model_directory'])
assert isWriteable(argDict["weather_model_directory"])


def test_filenames_1(args):
'''tests that the correct filenames are generated'''
"""tests that the correct filenames are generated"""
args = args
argDict = checkArgs(args)
assert 'Delay' not in argDict['wetFilenames'][0]
assert 'wet' in argDict['wetFilenames'][0]
assert 'hydro' in argDict['hydroFilenames'][0]
assert '20180101' in argDict['wetFilenames'][0]
assert '20180101' in argDict['hydroFilenames'][0]
assert len(argDict['hydroFilenames']) == 1
assert "Delay" not in argDict["wetFilenames"][0]
assert "wet" in argDict["wetFilenames"][0]
assert "hydro" in argDict["hydroFilenames"][0]
assert "20180101" in argDict["wetFilenames"][0]
assert "20180101" in argDict["hydroFilenames"][0]
assert len(argDict["hydroFilenames"]) == 1


def test_filenames_2(args):
'''tests that the correct filenames are generated'''
"""tests that the correct filenames are generated"""
args = args
args['output_directory'] = SCENARIO_2
args.aoi = StationFile(os.path.join(SCENARIO_2, 'stations.csv'))
args["output_directory"] = SCENARIO_2
args.aoi = StationFile(os.path.join(SCENARIO_2, "stations.csv"))
argDict = checkArgs(args)
assert '20180101' in argDict['wetFilenames'][0]
assert len(argDict['wetFilenames']) == 1
assert "20180101" in argDict["wetFilenames"][0]
assert len(argDict["wetFilenames"]) == 1


def test_makeDelayFileNames_1():
assert makeDelayFileNames(None, None, "h5", "name", "dir") == \
("dir/name_wet_ztd.h5", "dir/name_hydro_ztd.h5")
assert makeDelayFileNames(None, None, "h5", "name", "dir") == (
"dir/name_wet_ztd.h5",
"dir/name_hydro_ztd.h5",
)


def test_makeDelayFileNames_2():
assert makeDelayFileNames(None, (), "h5", "name", "dir") == \
("dir/name_wet_std.h5", "dir/name_hydro_std.h5")
assert makeDelayFileNames(None, (), "h5", "name", "dir") == (
"dir/name_wet_std.h5",
"dir/name_hydro_std.h5",
)


def test_makeDelayFileNames_3():
assert makeDelayFileNames(datetime.datetime(2020, 1, 1, 1, 2, 3), None, "h5", "model_name", "dir") == \
(
"dir/model_name_wet_20200101T010203_ztd.h5",
"dir/model_name_hydro_20200101T010203_ztd.h5"
assert makeDelayFileNames(
datetime.datetime(2020, 1, 1, 1, 2, 3), None, "h5", "model_name", "dir"
) == (
"dir/model_name_wet_20200101T010203_ztd.h5",
"dir/model_name_hydro_20200101T010203_ztd.h5",
)


def test_makeDelayFileNames_4():
assert makeDelayFileNames(datetime.datetime(1900, 12, 31, 1, 2, 3), "los", "h5", "model_name", "dir") == \
(
"dir/model_name_wet_19001231T010203_std.h5",
"dir/model_name_hydro_19001231T010203_std.h5"
assert makeDelayFileNames(
datetime.datetime(1900, 12, 31, 1, 2, 3), "los", "h5", "model_name", "dir"
) == (
"dir/model_name_wet_19001231T010203_std.h5",
"dir/model_name_hydro_19001231T010203_std.h5",
)


def test_get_raster_ext():
with pytest.raises(ValueError):
get_raster_ext('dummy_format')

get_raster_ext("dummy_format")
73 changes: 36 additions & 37 deletions test/test_datelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,60 @@
from test import TEST_DIR, WM, update_yaml
from RAiDER.cli.raider import read_run_config_file


def test_datelist():
SCENARIO_DIR = os.path.join(TEST_DIR, 'datelist')
SCENARIO_DIR = os.path.join(TEST_DIR, "datelist")
if os.path.exists(SCENARIO_DIR):
shutil.rmtree(SCENARIO_DIR)
os.makedirs(SCENARIO_DIR, exist_ok=False)

dates = ['20200124', '20200130']
true_dates = [
datetime.datetime(2020,1,24), datetime.datetime(2020,1,30)
]
dates = ["20200124", "20200130"]
true_dates = [datetime.datetime(2020, 1, 24), datetime.datetime(2020, 1, 30)]

dct_group = {
'aoi_group': {'bounding_box': [28, 28.3, -116.3, -116]},
'date_group': {'date_list': dates},
'time_group': {'time': '00:00:00', 'interpolate_time': 'none'},
'weather_model': WM,
'runtime_group': {
'output_directory': SCENARIO_DIR,
'weather_model_directory': os.path.join(SCENARIO_DIR, 'weather_files')
}
}
cfg = update_yaml(dct_group, 'temp.yaml')
file_to_del = 'temp.yaml'
"aoi_group": {"bounding_box": [28, 28.3, -116.3, -116]},
"date_group": {"date_list": dates},
"time_group": {"time": "00:00:00", "interpolate_time": "none"},
"weather_model": WM,
"runtime_group": {
"output_directory": SCENARIO_DIR,
"weather_model_directory": os.path.join(SCENARIO_DIR, "weather_files"),
},
}

cfg = update_yaml(dct_group, "temp.yaml")
file_to_del = "temp.yaml"
param_dict = read_run_config_file(cfg)
assert param_dict['date_list'] == true_dates
assert param_dict["date_list"] == true_dates

if os.path.exists(file_to_del):
os.remove(file_to_del)


def test_datestep():
SCENARIO_DIR = os.path.join(TEST_DIR, 'scenario_5')
st, en, step = '20200124', '20200130', 3
SCENARIO_DIR = os.path.join(TEST_DIR, "scenario_5")
st, en, step = "20200124", "20200130", 3
true_dates = [
datetime.datetime(2020,1,24),
datetime.datetime(2020,1,27),
datetime.datetime(2020,1,30)
datetime.datetime(2020, 1, 24),
datetime.datetime(2020, 1, 27),
datetime.datetime(2020, 1, 30),
]

dct_group = {
'aoi_group': {'bounding_box': [28, 39, -123, -112]},
'date_group': {'date_start': st, 'date_end': en, 'date_step': step},
'time_group': {'time': '00:00:00', 'interpolate_time': 'none'},
'weather_model': WM,
'runtime_group': {
'output_directory': SCENARIO_DIR,
'weather_model_directory': os.path.join(SCENARIO_DIR, 'weather_files')
}
}
cfg = update_yaml(dct_group, 'temp.yaml')
file_to_del = 'temp.yaml'
"aoi_group": {"bounding_box": [28, 39, -123, -112]},
"date_group": {"date_start": st, "date_end": en, "date_step": step},
"time_group": {"time": "00:00:00", "interpolate_time": "none"},
"weather_model": WM,
"runtime_group": {
"output_directory": SCENARIO_DIR,
"weather_model_directory": os.path.join(SCENARIO_DIR, "weather_files"),
},
}

cfg = update_yaml(dct_group, "temp.yaml")
file_to_del = "temp.yaml"
param_dict = read_run_config_file(cfg)
assert param_dict['date_list'] == true_dates
assert param_dict["date_list"] == true_dates

if os.path.exists(file_to_del):
os.remove(file_to_del)
os.remove(file_to_del)
Loading

0 comments on commit 4e7b657

Please sign in to comment.