From a07aab955542c9d2a6a4bc9a8db3d234e2732480 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Mon, 15 Jul 2024 12:18:23 -0400 Subject: [PATCH 01/15] adds filepath and data tests for get runscripts --- tests/test_parflow_run.py | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 3ca588ce..abc6d639 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -14,6 +14,81 @@ def test_get_ref_yaml_path(tmp_path): assert run.GeomInput.domaininput.InputType == "Box" +@pytest.mark.parametrize( + "grid, mode, input_file_type", + [ + pytest.param("conus1", "spinup", "box"), + pytest.param("conus2", "spinup", "box"), + pytest.param("conus1", "transient", "box"), + pytest.param("conus2", "transient", "box"), + pytest.param("conus1", "spinup", "solid"), + pytest.param("conus2", "spinup", "solid"), + pytest.param("conus1", "transient", "solid"), + pytest.param("conus2", "transient", "solid"), + + ] +) +def test_get_template_runscript_filepaths(grid, mode, input_file_type, tmp_path): + test_dir = tmp_path / "test_runscript" + test_dir.mkdir() + runscript_path = st.get_template_runscript( + grid=grid, + mode=mode, + input_file_type=input_file_type, + write_dir=test_dir, +) + output=f"{test_dir}/{grid}_{mode}_{input_file_type}.yaml" + print(os.path.exists(runscript_path)) + assert runscript_path == output + assert os.path.exists(runscript_path) + + +@pytest.mark.parametrize( + "grid, mode, input_file_type", + [ + pytest.param("conus1", "spinup", "box"), + pytest.param("conus2", "spinup", "box"), + pytest.param("conus1", "transient", "box"), + pytest.param("conus2", "transient", "box"), + pytest.param("conus1", "spinup", "solid"), + pytest.param("conus2", "spinup", "solid"), + pytest.param("conus1", "transient", "solid"), + pytest.param("conus2", "transient", "solid"), + + ] +) +def test_get_template_runscript_data(grid, mode, input_file_type, tmp_path): + test_dir = tmp_path / "test_runscript_data" + test_dir.mkdir() + runscript_path = st.get_template_runscript( + grid=grid, + mode=mode, + input_file_type=input_file_type, + write_dir=test_dir +) + run = Run.from_definition(runscript_path) + + #check mode + if mode == "transient": + assert run.Solver.LSM == "CLM" + else: + assert run.Solver.LSM == "none" + #check grid + if grid == "conus1": + assert run.ComputationalGrid.NX == 3342 + assert run.ComputationalGrid.NY == 1888 + else: + assert run.ComputationalGrid.NX == 4442 + assert run.ComputationalGrid.NY == 3256 + #check input_file_type + if input_file_type == "box": + assert run.Geom.domain.Upper.X == run.ComputationalGrid.NX*1000.0 + assert run.Geom.domain.Upper.Y == run.ComputationalGrid.NY*1000.0 + else: + assert run.Geom.domain.Upper.X == None + assert run.Geom.domain.Upper.Y == None + + def test_edit_runscript_for_subset_1(tmp_path): """Check the edited fiedls of the runscript file.""" test_dir = tmp_path / "test" From 97b49f854a10074ef5c5816445669dc9b41b8204 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Tue, 16 Jul 2024 10:15:25 -0400 Subject: [PATCH 02/15] adds filename and data tests for edit parflow run --- tests/test_parflow_run.py | 92 +++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index abc6d639..e10f160e 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -4,16 +4,6 @@ import subsettools as st -def test_get_ref_yaml_path(tmp_path): - test_dir = tmp_path / "test" - test_dir.mkdir() - runscript = st.get_template_runscript("conus1", "transient", "box", test_dir) - assert runscript == os.path.join(test_dir, "conus1_transient_box.yaml") - run = Run.from_definition(runscript) - assert run.ComputationalGrid.NX == 3342 - assert run.GeomInput.domaininput.InputType == "Box" - - @pytest.mark.parametrize( "grid, mode, input_file_type", [ @@ -37,9 +27,7 @@ def test_get_template_runscript_filepaths(grid, mode, input_file_type, tmp_path) input_file_type=input_file_type, write_dir=test_dir, ) - output=f"{test_dir}/{grid}_{mode}_{input_file_type}.yaml" - print(os.path.exists(runscript_path)) - assert runscript_path == output + assert runscript_path == os.path.join(test_dir, os.listdir(test_dir)[0]) assert os.path.exists(runscript_path) @@ -61,11 +49,11 @@ def test_get_template_runscript_data(grid, mode, input_file_type, tmp_path): test_dir = tmp_path / "test_runscript_data" test_dir.mkdir() runscript_path = st.get_template_runscript( - grid=grid, - mode=mode, - input_file_type=input_file_type, - write_dir=test_dir -) + grid=grid, + mode=mode, + input_file_type=input_file_type, + write_dir=test_dir + ) run = Run.from_definition(runscript_path) #check mode @@ -89,6 +77,74 @@ def test_get_template_runscript_data(grid, mode, input_file_type, tmp_path): assert run.Geom.domain.Upper.Y == None +def test_edit_runscript_filepaths(tmp_path): + test_dir = tmp_path / "test" + test_dir.mkdir() + write_dir = tmp_path / "write" + write_dir.mkdir() + forcing_dir = tmp_path / "forcing" + forcing_dir.mkdir() + runscript_path = st.get_template_runscript("conus1", "transient", "box",test_dir) + ij_bounds = (10, 10, 25, 25) + runname = "test-edit-runscript" + + new_runscript_path = st.edit_runscript_for_subset( + ij_bounds=ij_bounds, + runscript_path=runscript_path, + write_dir=write_dir, + runname=runname, + forcing_dir=forcing_dir, + ) + assert os.path.exists(new_runscript_path) + assert os.path.join(write_dir, os.listdir(write_dir)[0]) == new_runscript_path + +@pytest.mark.parametrize( + "runname, ij_bounds", + [ + pytest.param("new_runscript", (10,10,25,25)), + pytest.param(None, (10,10,25,23)) + ] +) +def test_edit_runscript_data(runname, ij_bounds, tmp_path): + test_dir = tmp_path / "test" + test_dir.mkdir() + write_dir = tmp_path / "write" + write_dir.mkdir() + forcing_dir = tmp_path / "forcing" + forcing_dir.mkdir() + old_runscript_path = st.get_template_runscript("conus1", "transient", "box", test_dir) + new_runscript_path = st.edit_runscript_for_subset( + ij_bounds=ij_bounds, + runscript_path=old_runscript_path, + write_dir=write_dir, + runname=runname, + forcing_dir=str(forcing_dir), + ) + i_min, j_min, i_max, j_max = ij_bounds + nx, ny = (i_max-i_min, j_max-j_min) + new_run = Run.from_definition(new_runscript_path) + old_run = Run.from_definition(old_runscript_path) + + if runname: + assert new_run.get_name() == runname + else: + assert new_run.get_name() == old_run.get_name() + assert new_run.ComputationalGrid.NX == nx + assert new_run.ComputationalGrid.NY == ny + assert new_run.Geom.domain.Upper.X == nx*1000 + assert new_run.Geom.domain.Upper.Y == ny*1000 + + +def test_get_ref_yaml_path(tmp_path): + test_dir = tmp_path / "test" + test_dir.mkdir() + runscript = st.get_template_runscript("conus1", "transient", "box", test_dir) + assert runscript == os.path.join(test_dir, "conus1_transient_box.yaml") + run = Run.from_definition(runscript) + assert run.ComputationalGrid.NX == 3342 + assert run.GeomInput.domaininput.InputType == "Box" + + def test_edit_runscript_for_subset_1(tmp_path): """Check the edited fiedls of the runscript file.""" test_dir = tmp_path / "test" From f0a7f476fa344a96a4f8aa2e117f9492f67b3cf7 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 13:35:06 -0400 Subject: [PATCH 03/15] adds tests for copy_files --- tests/test_parflow_run.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index e10f160e..77f1c0d7 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -2,6 +2,7 @@ import pytest from parflow import Run import subsettools as st +import filecmp @pytest.mark.parametrize( @@ -27,8 +28,8 @@ def test_get_template_runscript_filepaths(grid, mode, input_file_type, tmp_path) input_file_type=input_file_type, write_dir=test_dir, ) - assert runscript_path == os.path.join(test_dir, os.listdir(test_dir)[0]) assert os.path.exists(runscript_path) + assert runscript_path == os.path.join(test_dir, os.listdir(test_dir)[0]) @pytest.mark.parametrize( @@ -135,6 +136,40 @@ def test_edit_runscript_data(runname, ij_bounds, tmp_path): assert new_run.Geom.domain.Upper.Y == ny*1000 +def test_copy_single_file(tmp_path): + read_dir = tmp_path / "read" + read_dir.mkdir() + write_dir = tmp_path / "write" + write_dir.mkdir() + runscript_path = st.get_template_runscript( + grid="conus1", + mode="transient", + input_file_type="box", + write_dir=read_dir, + ) + st.copy_files(read_dir, write_dir) + f1 = runscript_path + f2 = f"{write_dir}/{os.path.basename(runscript_path)}" + assert os.path.exists(f2) + assert filecmp.cmp(f1, f2, shallow=False) + + +def test_copy_multiple_files(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + correct_output_dir = os.path.join( + os.getcwd(), "tests", "correct_output", "conus1_upper_verde" + ) + st.copy_files(read_dir=correct_output_dir, write_dir=write_dir) + match, mismatch, errors = filecmp.cmpfiles( + write_dir, correct_output_dir, ["mask.pfb", "solidfile.pfsol", "drv_clmin.dat"], shallow=True + ) + assert len(match) == 3 + assert len(mismatch) == 0 + assert len(errors) == 0 + + + def test_get_ref_yaml_path(tmp_path): test_dir = tmp_path / "test" test_dir.mkdir() From 2b6789e60106349c93a0aff7e2f5f78b20b6c698 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 13:51:22 -0400 Subject: [PATCH 04/15] adds check for forcing_dir in edit_runscript_data --- tests/test_parflow_run.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 77f1c0d7..08d52723 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -100,36 +100,46 @@ def test_edit_runscript_filepaths(tmp_path): assert os.path.join(write_dir, os.listdir(write_dir)[0]) == new_runscript_path @pytest.mark.parametrize( - "runname, ij_bounds", + "runname, ij_bounds, forcing", [ - pytest.param("new_runscript", (10,10,25,25)), - pytest.param(None, (10,10,25,23)) + pytest.param("new_runscript", (10,10,25,25), False), + pytest.param(None, (10,10,25,23), True) ] ) -def test_edit_runscript_data(runname, ij_bounds, tmp_path): +def test_edit_runscript_data(runname, ij_bounds, forcing, tmp_path): test_dir = tmp_path / "test" test_dir.mkdir() write_dir = tmp_path / "write" write_dir.mkdir() - forcing_dir = tmp_path / "forcing" - forcing_dir.mkdir() + if forcing: + forcing_dir = tmp_path / "forcing" + forcing_dir.mkdir() + forcing_dir = str(forcing_dir) + else: + forcing_dir = None old_runscript_path = st.get_template_runscript("conus1", "transient", "box", test_dir) new_runscript_path = st.edit_runscript_for_subset( ij_bounds=ij_bounds, runscript_path=old_runscript_path, write_dir=write_dir, runname=runname, - forcing_dir=str(forcing_dir), + forcing_dir=forcing_dir, ) i_min, j_min, i_max, j_max = ij_bounds nx, ny = (i_max-i_min, j_max-j_min) new_run = Run.from_definition(new_runscript_path) old_run = Run.from_definition(old_runscript_path) - + # check runname if runname: assert new_run.get_name() == runname else: assert new_run.get_name() == old_run.get_name() + # check forcing_dir + if forcing: + assert new_run.Solver.CLM.MetFilePath == str(forcing_dir) + else: + assert new_run.Solver.CLM.MetFilePath == old_run.Solver.CLM.MetFilePath + # check ij_bounds assert new_run.ComputationalGrid.NX == nx assert new_run.ComputationalGrid.NY == ny assert new_run.Geom.domain.Upper.X == nx*1000 From 9f69dab59ebc48effaa871a1ab40009317a34477 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 14:41:12 -0400 Subject: [PATCH 05/15] checks all files are copied in test_copy_multiple_files --- tests/test_parflow_run.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 08d52723..a080abac 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -16,7 +16,6 @@ pytest.param("conus2", "spinup", "solid"), pytest.param("conus1", "transient", "solid"), pytest.param("conus2", "transient", "solid"), - ] ) def test_get_template_runscript_filepaths(grid, mode, input_file_type, tmp_path): @@ -56,7 +55,6 @@ def test_get_template_runscript_data(grid, mode, input_file_type, tmp_path): write_dir=test_dir ) run = Run.from_definition(runscript_path) - #check mode if mode == "transient": assert run.Solver.LSM == "CLM" @@ -83,22 +81,19 @@ def test_edit_runscript_filepaths(tmp_path): test_dir.mkdir() write_dir = tmp_path / "write" write_dir.mkdir() - forcing_dir = tmp_path / "forcing" - forcing_dir.mkdir() runscript_path = st.get_template_runscript("conus1", "transient", "box",test_dir) ij_bounds = (10, 10, 25, 25) runname = "test-edit-runscript" - new_runscript_path = st.edit_runscript_for_subset( ij_bounds=ij_bounds, runscript_path=runscript_path, write_dir=write_dir, runname=runname, - forcing_dir=forcing_dir, ) assert os.path.exists(new_runscript_path) assert os.path.join(write_dir, os.listdir(write_dir)[0]) == new_runscript_path + @pytest.mark.parametrize( "runname, ij_bounds, forcing", [ @@ -172,12 +167,11 @@ def test_copy_multiple_files(tmp_path): ) st.copy_files(read_dir=correct_output_dir, write_dir=write_dir) match, mismatch, errors = filecmp.cmpfiles( - write_dir, correct_output_dir, ["mask.pfb", "solidfile.pfsol", "drv_clmin.dat"], shallow=True + write_dir, correct_output_dir, os.listdir(correct_output_dir), shallow=True ) - assert len(match) == 3 + assert len(match) == len(os.listdir(correct_output_dir)) assert len(mismatch) == 0 assert len(errors) == 0 - def test_get_ref_yaml_path(tmp_path): From 71cde12be163c6e8d54d8fc2888d7bd3764acaab Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 14:50:50 -0400 Subject: [PATCH 06/15] empty commit to trigger build From 9a50c0a8a41f0e1c1853cd522d2a1f0dda27a91b Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 15:59:59 -0400 Subject: [PATCH 07/15] adds dummy files for copy_files test --- tests/test_parflow_run.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index a080abac..ee08c765 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -71,9 +71,12 @@ def test_get_template_runscript_data(grid, mode, input_file_type, tmp_path): if input_file_type == "box": assert run.Geom.domain.Upper.X == run.ComputationalGrid.NX*1000.0 assert run.Geom.domain.Upper.Y == run.ComputationalGrid.NY*1000.0 + assert run.GeomInput.domaininput.InputType == "Box" else: assert run.Geom.domain.Upper.X == None assert run.Geom.domain.Upper.Y == None + assert run.GeomInput.domaininput.InputType == "SolidFile" + def test_edit_runscript_filepaths(tmp_path): @@ -146,15 +149,11 @@ def test_copy_single_file(tmp_path): read_dir.mkdir() write_dir = tmp_path / "write" write_dir.mkdir() - runscript_path = st.get_template_runscript( - grid="conus1", - mode="transient", - input_file_type="box", - write_dir=read_dir, - ) + test_file = read_dir / "myfile" + test_file.write_text("file contents") st.copy_files(read_dir, write_dir) - f1 = runscript_path - f2 = f"{write_dir}/{os.path.basename(runscript_path)}" + f1 = test_file + f2 = f"{write_dir}/{os.path.basename(test_file)}" assert os.path.exists(f2) assert filecmp.cmp(f1, f2, shallow=False) From d7ed8f16f5c4d62252d30d6e2e9d1e08c788a5f4 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Fri, 19 Jul 2024 10:25:36 -0400 Subject: [PATCH 08/15] adds dummy files for test_copy_multiple_files --- tests/test_parflow_run.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index ee08c765..2b1340a1 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -161,14 +161,18 @@ def test_copy_single_file(tmp_path): def test_copy_multiple_files(tmp_path): write_dir = tmp_path / "write" write_dir.mkdir() - correct_output_dir = os.path.join( - os.getcwd(), "tests", "correct_output", "conus1_upper_verde" - ) - st.copy_files(read_dir=correct_output_dir, write_dir=write_dir) + read_dir = tmp_path / "read" + read_dir.mkdir() + test_file1 = read_dir / "myfile1" + test_file1.write_text("this is the first file in this folder") + test_file2 = read_dir / "myfile2" + test_file2.write_text("this is the second file in this folder") + + st.copy_files(read_dir, write_dir) match, mismatch, errors = filecmp.cmpfiles( - write_dir, correct_output_dir, os.listdir(correct_output_dir), shallow=True + write_dir, read_dir, os.listdir(read_dir), shallow=False ) - assert len(match) == len(os.listdir(correct_output_dir)) + assert len(match) == 2 assert len(mismatch) == 0 assert len(errors) == 0 From 6dc8a6e8b107a7acc4f3f639726238cb0cb6d595 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Mon, 22 Jul 2024 15:25:59 -0400 Subject: [PATCH 09/15] adds filename tests for change_filename_values --- tests/test_parflow_run.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 2b1340a1..88da58f9 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -177,6 +177,40 @@ def test_copy_multiple_files(tmp_path): assert len(errors) == 0 +def test_change_filename_values_filepaths1(tmp_path): +# saves new runscript in new dir with new runname + working_dir = tmp_path / "working" + working_dir.mkdir() + write_dir = tmp_path / "write" + write_dir.mkdir() + original_runscript_path = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=working_dir) + new_runscript_path = st.change_filename_values(runscript_path=original_runscript_path, write_dir=write_dir, runname="new_runscript") + assert original_runscript_path != new_runscript_path + assert os.path.dirname(new_runscript_path) != os.path.dirname(original_runscript_path) + assert os.path.basename(new_runscript_path) == "new_runscript.yaml" + + +def test_change_filename_values_filepaths2(tmp_path): +# saves new runscript in same dir with new runname + working_dir = tmp_path / "working" + working_dir.mkdir() + original_runscript_path = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=working_dir) + new_runscript_path = st.change_filename_values(runscript_path=original_runscript_path, runname="new_runscript") + assert original_runscript_path != new_runscript_path + assert os.path.dirname(new_runscript_path) == os.path.dirname(original_runscript_path) + assert os.path.basename(new_runscript_path) == "new_runscript.yaml" + + +def test_change_filename_values_filepaths3(tmp_path): +# overwrites existing runscript with new runscript + working_dir = tmp_path / "working" + working_dir.mkdir() + original_runscript_path = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=working_dir) + new_runscript_path = st.change_filename_values(runscript_path=original_runscript_path) + assert original_runscript_path == new_runscript_path + + +# ------ Previously written tests ------ # def test_get_ref_yaml_path(tmp_path): test_dir = tmp_path / "test" test_dir.mkdir() @@ -242,7 +276,7 @@ def test_edit_runscript_for_subset_3(tmp_path): def test_change_filename_values_1(tmp_path): - """Check the edited fiedls of the runscript file.""" + """Check the edited fields of the runscript file.""" test_dir = tmp_path / "test" test_dir.mkdir() old_runscript = st.get_template_runscript("conus1", "transient", "box", test_dir) From 4c8e9f1f5b2444b48cce10c930885f4f198efabe Mon Sep 17 00:00:00 2001 From: gartavanis <32808713+gartavanis@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:46:42 -0400 Subject: [PATCH 10/15] Fix HUC picker reference (#95) Co-authored-by: gartavanis --- docs/examples/definte_subset.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/examples/definte_subset.ipynb b/docs/examples/definte_subset.ipynb index 9a01a8d8..c155f947 100644 --- a/docs/examples/definte_subset.ipynb +++ b/docs/examples/definte_subset.ipynb @@ -125,7 +125,7 @@ "metadata": {}, "source": [ "## 4. Defining bounds for a HUC watershed\n", - "The subsettools [`define_huc_domain`](https://hydroframesubsettools.readthedocs.io/en/latest/autoapi/subsettools/subsettools/index.html#subsettools.subsettools.define_huc_domain) function returns a tuple `(imin, jmin, imax, jmax)` of grid indices that define a bounding box containing any HUC, and a mask for that domain. You can provide 2, 4, 6, 8 or 10-digit HUCs. For help finding your HUC you can refer to the [USGS HUC picker](https://water.usgs.gov/wsc/map_index.html)." + "The subsettools [`define_huc_domain`](https://hydroframesubsettools.readthedocs.io/en/latest/autoapi/subsettools/subsettools/index.html#subsettools.subsettools.define_huc_domain) function returns a tuple `(imin, jmin, imax, jmax)` of grid indices that define a bounding box containing any HUC, and a mask for that domain. You can provide 2, 4, 6, 8 or 10-digit HUCs. For help finding your HUC you can refer to the [USGS HUC picker](https://water.usgs.gov/wsc/a_api/wbd/index_wbd.html). **Make sure to use the new (up to 12-digit) USGS HUC maps rather than the legacy 8-digit ones.**" ] }, { @@ -269,7 +269,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.9" } }, "nbformat": 4, From e932459db465fae8aeb2f199db94e3dcd8bbe06e Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 18 Jul 2024 14:50:50 -0400 Subject: [PATCH 11/15] empty commit to trigger build From 932a9db2bc293930b37de021d92c0418daad9d9d Mon Sep 17 00:00:00 2001 From: egwilborn Date: Tue, 23 Jul 2024 13:07:36 -0400 Subject: [PATCH 12/15] adds individual unit tests for change_filename_values data --- tests/test_parflow_run.py | 82 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 88da58f9..5500f53b 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -209,7 +209,87 @@ def test_change_filename_values_filepaths3(tmp_path): new_runscript_path = st.change_filename_values(runscript_path=original_runscript_path) assert original_runscript_path == new_runscript_path - + +def test_change_filename_values_slopex(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", slopex=test_file) + run = Run.from_definition(new_runscript) + assert run.TopoSlopesX.FileName == test_file + + +def test_change_filename_values_slopey(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", slopey=test_file) + run = Run.from_definition(new_runscript) + assert run.TopoSlopesY.FileName == test_file + + +def test_change_filename_values_solidfile(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", solidfile=test_file) + run = Run.from_definition(new_runscript) + assert run.GeomInput.domaininput.FileName == test_file + + +def test_change_filename_values_init_press(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", init_press=test_file) + run = Run.from_definition(new_runscript) + assert run.Geom.domain.ICPressure.FileName == test_file + + +def test_change_filename_values_indicator(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", indicator=test_file) + run = Run.from_definition(new_runscript) + assert run.Geom.indi_input.FileName == test_file + + +def test_change_filename_values_depth_to_bedrock(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", depth_to_bedrock=test_file) + run = Run.from_definition(new_runscript) + assert run.Geom.domain.FBz.FileName == test_file + + +def test_change_filename_values_mannings(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", mannings=test_file) + run = Run.from_definition(new_runscript) + assert run.Mannings.FileName == test_file + + +def test_change_filename_values_evap_trans(tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + test_file = "dummy_test_file.pfb" + original_runscript = st.get_template_runscript(grid="conus1", mode="transient", input_file_type="box", write_dir=write_dir) + new_runscript = st.change_filename_values(original_runscript, write_dir, runname="new_runscript", evap_trans=test_file) + run = Run.from_definition(new_runscript) + assert run.Solver.EvapTrans.FileName == test_file + + # ------ Previously written tests ------ # def test_get_ref_yaml_path(tmp_path): test_dir = tmp_path / "test" From 38bf80f39b771bc17c89d826c66b511fd94ecb34 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Tue, 23 Jul 2024 16:18:45 -0400 Subject: [PATCH 13/15] adds dist_run tests for filepaths and data --- tests/test_parflow_run.py | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 5500f53b..abeb10ac 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -1,6 +1,9 @@ import os import pytest +import numpy as np from parflow import Run +from parflow.tools.io import write_pfb, read_pfb +from parflow import ParflowBinaryReader import subsettools as st import filecmp @@ -290,6 +293,102 @@ def test_change_filename_values_evap_trans(tmp_path): assert run.Solver.EvapTrans.FileName == test_file +def test_dist_run_filepaths_1(tmp_path): +# no working directory provided + runscript_dir = tmp_path / "runscript" + runscript_dir.mkdir() + original_runscript_path = st.get_template_runscript( + grid="conus1", + mode="transient", + input_file_type="box", + write_dir=runscript_dir + ) + new_runscript_path = st.dist_run(topo_p=4,topo_q=4,runscript_path=original_runscript_path,dist_clim_forcing=False) + assert new_runscript_path == original_runscript_path + + +def test_dist_run_filepaths_2(tmp_path): +# no working directory provided + runscript_dir = tmp_path / "runscript" + runscript_dir.mkdir() + working_dir = tmp_path / "working" + working_dir.mkdir() + original_runscript_path = st.get_template_runscript( + grid="conus1", + mode="transient", + input_file_type="box", + write_dir=runscript_dir + ) + new_runscript_path = st.dist_run(topo_p=4,topo_q=4,runscript_path=original_runscript_path,dist_clim_forcing=False, working_dir=working_dir) + assert original_runscript_path != new_runscript_path + assert os.path.basename(new_runscript_path) == os.path.basename(original_runscript_path) + assert os.path.dirname(new_runscript_path) != os.path.dirname(original_runscript_path) + + +def test_dist_run_runscript(tmp_path): + working_dir = tmp_path / "working" + working_dir.mkdir() + runscript_path = st.get_template_runscript( + grid="conus1", + mode="transient", + input_file_type="box", + write_dir=working_dir + ) + new_path = st.dist_run( + topo_p=3, + topo_q=2, + runscript_path=runscript_path, + dist_clim_forcing=False + ) + run = Run.from_definition(new_path) + assert run.Process.Topology.P == 3 + assert run.Process.Topology.Q == 2 + + +def test_dist_run_forcing_data(tmp_path): +# checks header of distributed forcing data + runscript_dir = tmp_path / "runscript" + runscript_dir.mkdir() + working_dir = tmp_path / "working" + working_dir.mkdir() + test_file = working_dir / "test_file.pfb" + data = np.ones((10,10)) + write_pfb(str(test_file), data) + topo_p = 2 + topo_q = 3 + runscript_path = st.get_template_runscript("conus1", "transient", "box", runscript_dir) + temp_run = Run.from_definition(runscript_path) + temp_run.Solver.CLM.MetFilePath = str(working_dir) + temp_run.write( + working_directory=runscript_dir, file_format="yaml" + ) + new_path = st.dist_run(topo_p, topo_q,runscript_path, dist_clim_forcing=True, working_dir=str(working_dir)) + run = Run.from_definition(new_path) + with ParflowBinaryReader(test_file) as pfb: + data = pfb.read_header() + assert data['n_subgrids'] == topo_p*topo_q + + +def test_dist_run_runfile_data(tmp_path): +# checks header of distributed forcing data + runscript_dir = tmp_path / "runscript" + runscript_dir.mkdir() + working_dir = tmp_path / "working" + working_dir.mkdir() + test_file = working_dir / "test_run_file.pfb" + data = np.ones((10,10)) + write_pfb(str(test_file), data) + topo_p = 2 + topo_q = 3 + runscript_path = st.get_template_runscript("conus1", "transient", "box", runscript_dir) + new_path = st.dist_run(topo_p, topo_q,runscript_path, dist_clim_forcing=False, working_dir=str(working_dir)) + run = Run.from_definition(new_path) + with ParflowBinaryReader(test_file) as pfb: + data = pfb.read_header() + assert data['n_subgrids'] == topo_p*topo_q + + + # ------ Previously written tests ------ # def test_get_ref_yaml_path(tmp_path): test_dir = tmp_path / "test" From 44751422ff1fdd491a2d2298304c8279010ef1a8 Mon Sep 17 00:00:00 2001 From: egwilborn Date: Wed, 24 Jul 2024 09:46:52 -0400 Subject: [PATCH 14/15] small fixes --- tests/test_parflow_run.py | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index abeb10ac..17dd4f80 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -297,28 +297,18 @@ def test_dist_run_filepaths_1(tmp_path): # no working directory provided runscript_dir = tmp_path / "runscript" runscript_dir.mkdir() - original_runscript_path = st.get_template_runscript( - grid="conus1", - mode="transient", - input_file_type="box", - write_dir=runscript_dir - ) + original_runscript_path = st.get_template_runscript("conus1", "transient", "box", runscript_dir) new_runscript_path = st.dist_run(topo_p=4,topo_q=4,runscript_path=original_runscript_path,dist_clim_forcing=False) assert new_runscript_path == original_runscript_path def test_dist_run_filepaths_2(tmp_path): -# no working directory provided +# working directory provided runscript_dir = tmp_path / "runscript" runscript_dir.mkdir() working_dir = tmp_path / "working" working_dir.mkdir() - original_runscript_path = st.get_template_runscript( - grid="conus1", - mode="transient", - input_file_type="box", - write_dir=runscript_dir - ) + original_runscript_path = st.get_template_runscript("conus1", "transient", "box", runscript_dir) new_runscript_path = st.dist_run(topo_p=4,topo_q=4,runscript_path=original_runscript_path,dist_clim_forcing=False, working_dir=working_dir) assert original_runscript_path != new_runscript_path assert os.path.basename(new_runscript_path) == os.path.basename(original_runscript_path) @@ -326,20 +316,11 @@ def test_dist_run_filepaths_2(tmp_path): def test_dist_run_runscript(tmp_path): +# checks runscript object to ensure topo_p and q changes were made working_dir = tmp_path / "working" working_dir.mkdir() - runscript_path = st.get_template_runscript( - grid="conus1", - mode="transient", - input_file_type="box", - write_dir=working_dir - ) - new_path = st.dist_run( - topo_p=3, - topo_q=2, - runscript_path=runscript_path, - dist_clim_forcing=False - ) + runscript_path = st.get_template_runscript("conus1", "transient", "box", working_dir) + new_path = st.dist_run(topo_p=3, topo_q=2, runscript_path=runscript_path, dist_clim_forcing=False) run = Run.from_definition(new_path) assert run.Process.Topology.P == 3 assert run.Process.Topology.Q == 2 @@ -370,7 +351,7 @@ def test_dist_run_forcing_data(tmp_path): def test_dist_run_runfile_data(tmp_path): -# checks header of distributed forcing data +# checks header of distributed run data runscript_dir = tmp_path / "runscript" runscript_dir.mkdir() working_dir = tmp_path / "working" From 3d93fe8c201baf4b3f19dd343754dc501788ee8c Mon Sep 17 00:00:00 2001 From: egwilborn Date: Thu, 25 Jul 2024 14:32:22 -0400 Subject: [PATCH 15/15] changes directory for forcing data --- tests/test_parflow_run.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_parflow_run.py b/tests/test_parflow_run.py index 17dd4f80..ec215190 100644 --- a/tests/test_parflow_run.py +++ b/tests/test_parflow_run.py @@ -330,20 +330,20 @@ def test_dist_run_forcing_data(tmp_path): # checks header of distributed forcing data runscript_dir = tmp_path / "runscript" runscript_dir.mkdir() - working_dir = tmp_path / "working" - working_dir.mkdir() - test_file = working_dir / "test_file.pfb" + forcing_dir = tmp_path / "forcing" + forcing_dir.mkdir() + test_file = forcing_dir / "test_file.pfb" data = np.ones((10,10)) write_pfb(str(test_file), data) topo_p = 2 topo_q = 3 runscript_path = st.get_template_runscript("conus1", "transient", "box", runscript_dir) temp_run = Run.from_definition(runscript_path) - temp_run.Solver.CLM.MetFilePath = str(working_dir) + temp_run.Solver.CLM.MetFilePath = str(forcing_dir) temp_run.write( working_directory=runscript_dir, file_format="yaml" ) - new_path = st.dist_run(topo_p, topo_q,runscript_path, dist_clim_forcing=True, working_dir=str(working_dir)) + new_path = st.dist_run(topo_p, topo_q,runscript_path, dist_clim_forcing=True) run = Run.from_definition(new_path) with ParflowBinaryReader(test_file) as pfb: data = pfb.read_header()