From 75b09d62c83c9ca7c06f31e8aff0d0b0d14c9d77 Mon Sep 17 00:00:00 2001 From: mjreno Date: Tue, 14 Jan 2025 08:56:29 -0500 Subject: [PATCH] baseline tests check ascii parity --- autotest/regression/test_mf6_netcdf.py | 98 ++++++++++++++----- .../gwf_sto01/gwf_sto01.nam | 16 +-- .../gwf_sto01/gwf_sto01.oc | 10 +- .../gwf_sto01/gwf_sto01.nam | 16 +-- .../gwf_sto01/gwf_sto01.oc | 10 +- flopy/utils/model_netcdf.py | 2 +- 6 files changed, 99 insertions(+), 53 deletions(-) diff --git a/autotest/regression/test_mf6_netcdf.py b/autotest/regression/test_mf6_netcdf.py index eac430125..e414482e9 100644 --- a/autotest/regression/test_mf6_netcdf.py +++ b/autotest/regression/test_mf6_netcdf.py @@ -84,11 +84,10 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path): next(file2) for line1, line2 in zip(file1, file2): - assert line1 == line2 + assert line1.lower() == line2.lower() else: # TODO compare nc files assert os.path.exists(gen) - continue @requires_pkg("xarray") @@ -96,8 +95,21 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path): @pytest.mark.regression def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path): xr = import_optional_dependency("xarray") - - cases = ["structured", "mesh2d"] + data_path_base = example_data_path / "mf6" / "netcdf" + tests = { + "test_gwf_sto01_mesh": TestInfo( + "gwf_sto01", + "gwf_sto01_write", + "gwf_sto01.ugrid.nc", + ), + "test_gwf_sto01_structured": TestInfo( + "gwf_sto01", + "gwf_sto01_write", + "gwf_sto01.structured.nc", + ), + } + name = "gwf_sto01" + cases = ["mesh2d", "structured"] # static model data # temporal discretization @@ -174,9 +186,17 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path): ske = [6e-4, 3e-4, 6e-4] # build - for name in cases: - # name = cases[0] - ws = function_tmpdir / name + ws = function_tmpdir / "ws" + for idx, (base_folder, test_info) in enumerate(tests.items()): + print(f"RUNNING TEST: {base_folder}") + netcdf = cases[idx] + data_path = os.path.join( + data_path_base, base_folder, test_info.original_simulation_folder + ) + # copy example data into working directory + base_model_folder = os.path.join(ws, f"{base_folder}_base") + test_model_folder = os.path.join(ws, f"{base_folder}_test") + shutil.copytree(data_path, base_model_folder) # build MODFLOW 6 files sim = flopy.mf6.MFSimulation( @@ -184,7 +204,11 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path): ) # create tdis package tdis = flopy.mf6.ModflowTdis( - sim, time_units="DAYS", nper=nper, perioddata=tdis_rc + sim, + time_units="DAYS", + start_date_time="2041-01-01t00:00:00-05:00", + nper=nper, + perioddata=tdis_rc, ) # create gwf model @@ -276,21 +300,43 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path): printrecord=[("HEAD", "LAST"), ("BUDGET", "ALL")], ) - sim.write_simulation(netcdf=name) - - try: - success, buff = flopy.run_model( - "mf6", - ws / "mfsim.nam", - model_ws=ws, - report=True, - ) - except Exception: - warn( - "MODFLOW 6 serial test", - name, - f"failed with error:\n{format_exc()}", - ) - success = False - - assert success + sim.set_sim_path(test_model_folder) + sim.write_simulation(netcdf=netcdf) + + # compare generated files + gen_files = [ + f + for f in os.listdir(test_model_folder) + if os.path.isfile(os.path.join(test_model_folder, f)) + ] + base_files = [ + f + for f in os.listdir(base_model_folder) + if os.path.isfile(os.path.join(base_model_folder, f)) + ] + # assert len(gen_files) == len(base_files) + for f in base_files: + print(f"cmp => {f}") + base = os.path.join(base_model_folder, f) + gen = os.path.join(test_model_folder, f) + if f != test_info.netcdf_output_file: + with open(base, "r") as file1, open(gen, "r") as file2: + # Skip first line + next(file1) + next(file2) + + for line1, line2 in zip(file1, file2): + if "netcdf filein" in line1.lower(): + if netcdf == "mesh2d": + suffix = "ugrid" + else: + suffix = netcdf + assert line1 == f" NETCDF FILEIN gwf_sto01.{suffix}.nc\n" + assert line2 == " NETCDF FILEIN gwf_sto01.in.nc\n" + else: + assert line1 == line2 + else: + # TODO compare nc files + assert os.path.exists( + os.path.join(test_model_folder, "gwf_sto01.in.nc") + ) diff --git a/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.nam b/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.nam index d406ec93b..eb2ff4ca8 100644 --- a/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.nam +++ b/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.nam @@ -6,12 +6,12 @@ BEGIN options END options BEGIN packages - dis6 gwf_sto01.dis dis - ic6 gwf_sto01.ic ic - npf6 gwf_sto01.npf npf - sto6 gwf_sto01.sto sto - rch6 gwf_sto01.rcha rcha_0 - wel6 gwf_sto01.wel wel_0 - chd6 gwf_sto01.chd chd_0 - oc6 gwf_sto01.oc oc + DIS6 gwf_sto01.dis dis + IC6 gwf_sto01.ic ic + NPF6 gwf_sto01.npf npf + STO6 gwf_sto01.sto sto + RCH6 gwf_sto01.rcha rcha_0 + WEL6 gwf_sto01.wel wel_0 + CHD6 gwf_sto01.chd chd_0 + OC6 gwf_sto01.oc oc END packages diff --git a/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.oc b/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.oc index c04b78fba..3041e7820 100644 --- a/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.oc +++ b/examples/data/mf6/netcdf/test_gwf_sto01_mesh/gwf_sto01/gwf_sto01.oc @@ -2,13 +2,13 @@ BEGIN options BUDGET FILEOUT gwf_sto01.cbc HEAD FILEOUT gwf_sto01.hds - HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 general + HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 GENERAL END options BEGIN period 1 - SAVE head all - SAVE budget all - PRINT head all - PRINT budget all + SAVE HEAD ALL + SAVE BUDGET ALL + PRINT HEAD LAST + PRINT BUDGET ALL END period 1 diff --git a/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.nam b/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.nam index b11cafa07..e0f8a2a84 100644 --- a/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.nam +++ b/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.nam @@ -6,12 +6,12 @@ BEGIN options END options BEGIN packages - dis6 gwf_sto01.dis dis - ic6 gwf_sto01.ic ic - npf6 gwf_sto01.npf npf - sto6 gwf_sto01.sto sto - rch6 gwf_sto01.rcha rcha_0 - wel6 gwf_sto01.wel wel_0 - chd6 gwf_sto01.chd chd_0 - oc6 gwf_sto01.oc oc + DIS6 gwf_sto01.dis dis + IC6 gwf_sto01.ic ic + NPF6 gwf_sto01.npf npf + STO6 gwf_sto01.sto sto + RCH6 gwf_sto01.rcha rcha_0 + WEL6 gwf_sto01.wel wel_0 + CHD6 gwf_sto01.chd chd_0 + OC6 gwf_sto01.oc oc END packages diff --git a/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.oc b/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.oc index 8d7376f36..233b5286d 100644 --- a/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.oc +++ b/examples/data/mf6/netcdf/test_gwf_sto01_structured/gwf_sto01/gwf_sto01.oc @@ -2,13 +2,13 @@ BEGIN options BUDGET FILEOUT gwf_sto01.cbc HEAD FILEOUT gwf_sto01.hds - HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 general + HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 GENERAL END options BEGIN period 1 - SAVE head all - SAVE budget all - PRINT head last - PRINT budget all + SAVE HEAD ALL + SAVE BUDGET ALL + PRINT HEAD LAST + PRINT BUDGET ALL END period 1 diff --git a/flopy/utils/model_netcdf.py b/flopy/utils/model_netcdf.py index 5dfa1c9db..3f4a6aee8 100644 --- a/flopy/utils/model_netcdf.py +++ b/flopy/utils/model_netcdf.py @@ -177,7 +177,7 @@ def _create_layered_var( ): try: for layer in range(data.shape[0]): - layer_vname = f"{varname}_l{layer+1}" + layer_vname = f"{varname}_l{layer + 1}" var_d = {layer_vname: (nc_shape, data[layer].flatten())} self._dataset = self._dataset.assign(var_d) self._dataset[layer_vname].attrs["modflow6_input"] = tag