Skip to content

Commit

Permalink
x2
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-seaice committed Sep 10, 2024
1 parent 4f1d5bb commit f54da51
Showing 1 changed file with 88 additions and 23 deletions.
111 changes: 88 additions & 23 deletions test/test_payu_conf/test_mom6_filenames.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pytest
import xarray as xr
import numpy as np
import pandas as pd

from os import makedirs, chdir
Expand All @@ -23,25 +21,20 @@ def assert_f_not_exists(p):
raise AssertionError("File exists and should not: %s" % str(p))


def monthly_files(dir_name, nmonths, tmp_path):
def yearly_files(dir_name, n, tmp_path):
"""
Make 12 months of empty data files data, and then write it into 12 files
request = (path, ndays)
e.g. request = ("archive/output000", "365")
Make empty data files
"""

times = pd.date_range("2010-01-01", freq="ME", periods=nmonths)
times = pd.date_range("2010-01-01", freq="YE", periods=n)

out_dir = str(tmp_path) + "/" + dir_name + "/"
paths = [f"{out_dir}{DIAG_BASE}_{str(t)[0:4]}_{str(t)[5:7]}.nc" for t in times]
paths = [f"{out_dir}{DIAG_BASE}._{str(t)[0:4]}.nc" for t in times]

makedirs(out_dir)

for p in paths:
with open(p, "w") as f:
# f.write("blank")
f.close()

for p in paths:
Expand All @@ -51,23 +44,22 @@ def monthly_files(dir_name, nmonths, tmp_path):


@pytest.mark.parametrize(
"hist_dir, use_dir, nmonths",
"hist_dir, use_dir, n",
[
("Default", False, 12),
("archive/output000", False, 12),
("archive/output999", False, 1),
("archive/output9999", False, 1),
("archive/output574", True, 12),
],
) # run this test with a several folder names and lengths, provide the directory as an argument sometimes
def test_true_case(hist_dir, use_dir, nmonths, tmp_path):
def test_true_case(hist_dir, use_dir, n, tmp_path):

monthly_paths = monthly_files(hist_dir, nmonths, tmp_path)
yearly_paths = yearly_files(hist_dir, n, tmp_path)
chdir(tmp_path)
output_dir = Path(monthly_paths[0]).parents[0]
output_dir = Path(yearly_paths[0]).parents[0]

if not use_dir: # default path
run([run_str])
expected_months = pd.date_range("2010-01-01", freq="ME", periods=nmonths + 1)
else: # provide path
run(
[
Expand All @@ -76,19 +68,92 @@ def test_true_case(hist_dir, use_dir, nmonths, tmp_path):
output_dir,
],
)
expected_months = pd.date_range("2010-01-01", freq="ME", periods=nmonths + 1)

expected_years = pd.date_range("2010-01-01", freq="YE", periods=n + 1)

# valid output filenames
expected_paths = [
f"{output_dir}/{DIAG_BASE}_{str(t)[0:4]}-{str(t)[5:7]}.nc"
for t in expected_months
f"{output_dir}/{DIAG_BASE}.{str(t)[0:4]}.nc" for t in expected_years
]

for p in expected_paths[0:nmonths]:
for p in expected_paths[0:n]:
assert_file_exists(p)

for p in expected_paths[nmonths]:
for p in expected_paths[n]:
assert_f_not_exists(p)

for p in monthly_paths:
for p in yearly_paths:
assert_f_not_exists(p)


@pytest.mark.parametrize(
"hist_dir, use_dir, n",
[
("archive/output000", False, 12),
],
)
def test_dont_override(hist_dir, use_dir, n, tmp_path):
"""
make some empty data files, and make some files where the files should be renamed to,
and confirm it doesn't delete any of them
"""

yearly_paths = yearly_files(hist_dir, n, tmp_path)
chdir(tmp_path)
output_dir = Path(yearly_paths[0]).parents[0]

# write the expected output too
expected_years = pd.date_range("2010-01-01", freq="YE", periods=n)

expected_paths = [
f"{output_dir}/{DIAG_BASE}.{str(t)[0:4]}.nc" for t in expected_years
]

for p in expected_paths:
with open(p, "w") as f:
f.close()

if not use_dir: # default path
run([run_str])
else: # provide path
run(
[
run_str,
"-d",
output_dir,
],
)

for p in expected_paths:
assert_file_exists(p)

for p in yearly_paths:
assert_file_exists(p)


# @pytest.mark.parametrize("hist_dir, ndays", [("Default", 31), ("Default", 27)])
# def test_no_override(hist_dir, ndays, hist_base, tmp_path):
# """
# Run the script to convert the daily data into monthly files, but the output filename already exists, and check nothing happens.
# """

# daily_paths = daily_files(hist_dir, hist_base, ndays, tmp_path)

# chdir(tmp_path)
# output_dir = Path(daily_paths[0]).parents[0]

# expected_months = pd.date_range("2010-01-01", freq="ME", periods=1)

# monthly_paths = [
# f"{output_dir}/{hist_base}.{str(t)[0:7]}.nc" for t in expected_months
# ]
# for p in monthly_paths:
# Path(p).touch()

# run([run_str])

# for p in daily_paths:
# assert_file_exists(p)

# for p in monthly_paths:
# assert_file_exists(p)

0 comments on commit f54da51

Please sign in to comment.