From 825372c0a2a5e42d1afc7da9467c18c11dc747a7 Mon Sep 17 00:00:00 2001 From: George Breyiannis Date: Sat, 23 Sep 2023 12:11:32 +0200 Subject: [PATCH] schism: do output merging only if files are present If we define nc_out = 0 in the param.nml there is no map output. This way we avoid waiting for the geometry reindexing. --- pyposeidon/schism.py | 47 ++++++++++++++++++++++++++------------------ tests/test_schism.py | 28 +++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/pyposeidon/schism.py b/pyposeidon/schism.py index 9d1f44b7..427da1a3 100644 --- a/pyposeidon/schism.py +++ b/pyposeidon/schism.py @@ -1312,10 +1312,16 @@ def results(self, **kwargs): path = get_value(self, kwargs, "rpath", "./schism/") logger.info("get combined 2D netcdf files \n") + # check for new IO output hfiles = glob.glob(os.path.join(path, "outputs/out2d_*.nc")) hfiles.sort() + # check for old IO output + ofiles = glob.glob(os.path.join(path, "outputs/schout_*_*.nc")) - if hfiles: + if not (ofiles or hfiles): + logger.warning("no output netcdf files, moving on") + + elif hfiles: x2d = xr.open_mfdataset(hfiles, data_vars="minimal") # set timestamp @@ -1350,7 +1356,7 @@ def results(self, **kwargs): # save 2D variables to file x2d.to_netcdf(os.path.join(path, "outputs/schout_1.nc")) - else: + elif ofiles: if len(self.misc) == 0: logger.info("retrieving index references ... \n") self.global2local(**kwargs) @@ -1719,6 +1725,9 @@ def results(self, **kwargs): xc.to_netcdf(os.path.join(path, f"outputs/schout_{val}.nc")) + else: + raise Exception("This should never happen") + logger.info("done with output netCDF files \n") def set_obs(self, **kwargs): @@ -1871,25 +1880,25 @@ def get_station_sim_data(self, **kwargs): try: # get the station flags flags = pd.read_csv(os.path.join(path, "station.in"), header=None, nrows=1, delim_whitespace=True).T - flags.columns = ["flag"] - flags["variable"] = [ - "elev", - "air_pressure", - "windx", - "windy", - "T", - "S", - "u", - "v", - "w", - ] - - vals = flags[flags.values == 1] # get the active ones - except OSError as e: - if e.errno == errno.EEXIST: - logger.error("no station.in file present") + except FileNotFoundError: + logger.error("no station.in file present") return + flags.columns = ["flag"] + flags["variable"] = [ + "elev", + "air_pressure", + "windx", + "windy", + "T", + "S", + "u", + "v", + "w", + ] + + vals = flags[flags.values == 1] # get the active ones + dstamp = kwargs.get("dstamp", self.rdate) dfs = [] diff --git a/tests/test_schism.py b/tests/test_schism.py index c55ebd9f..20d154a1 100644 --- a/tests/test_schism.py +++ b/tests/test_schism.py @@ -84,6 +84,32 @@ } +case4 = { + "solver_name": "schism", + "mesh_file": MESH_FILE, + "manning": 0.12, + "windrot": 0.00001, + "tag": "test", + "start_date": "2011-1-1 0:0:0", + "time_frame": "12H", + "meteo_source": [(DATA_DIR / "era5.grib").as_posix()], # meteo file + "dem_source": DEM_FILE, + "monitor": True, + "update": ["all"], # update only meteo, keep dem + "parameters": { + "dt": 400, + "rnday": 0.3, + "nhot": 0, + "ihot": 0, + "nspool": 9, + "ihfskip": 36, + "nhot_write": 108, + "nc_out": 0, + }, + "scribes": 2, +} + + def schism(tmpdir, dic): # initialize a model rpath = str(tmpdir) + "/" @@ -105,7 +131,7 @@ def schism(tmpdir, dic): @pytest.mark.schism -@pytest.mark.parametrize("case", [case1, case2, case3]) +@pytest.mark.parametrize("case", [case1, case2, case3, case4]) def test_answer(tmpdir, case): assert schism(tmpdir, case) == True