diff --git a/pyposeidon/telemac.py b/pyposeidon/telemac.py index 4fdd3f7..edab23a 100644 --- a/pyposeidon/telemac.py +++ b/pyposeidon/telemac.py @@ -588,6 +588,7 @@ def __init__(self, **kwargs): self.end_date = pd.to_datetime(end_date) self.time_frame = self.end_date - self.start_date + # if it is the first computation, set origin time (important for restart) if not hasattr(self, "rdate"): self.rdate = get_value(self, kwargs, "rdate", self.start_date) diff --git a/pyposeidon/utils/cast.py b/pyposeidon/utils/cast.py index 50bb02e..e95de3f 100644 --- a/pyposeidon/utils/cast.py +++ b/pyposeidon/utils/cast.py @@ -553,19 +553,19 @@ def run(self, **kwargs): info["config_file"] = os.path.join(ppath, self.tag + "_model.json") - self.start = pd.Timestamp(info["start_date"]) # TO FIX! - # THIS SHOULD NOT BE IMPLEMENTED THIS WAY + self.time_origin = pd.Timestamp(info["rdate"]) # update the properties info["params"]["rdate"] = self.sdate info["params"]["start_date"] = self.sdate + info["start_date"] = self.sdate info["params"]["time_frame"] = self.end_date - self.sdate info["params"]["end_date"] = self.end_date if info["tag"] == "telemac2d": - info["params"]["datestart"] = self.start.strftime("%Y;%m;%d") - info["params"]["timestart"] = self.start.strftime("%H;%M;%S") + info["params"]["datestart"] = self.rdate.strftime("%Y;%m;%d") + info["params"]["timestart"] = self.rdate.strftime("%H;%M;%S") elif info["tag"] == "tomawac": - info["params"]["datestart"] = self.start.strftime("%Y%m%d%H%M") + info["params"]["datestart"] = self.rdate.strftime("%Y%m%d%H%M") info["params"]["duration"] = pd.to_timedelta(info["params"]["time_frame"]).total_seconds() info["rpath"] = rpath diff --git a/tests/test_telemac_cast.py b/tests/test_telemac_cast.py index 4d2b109..5ccacc5 100644 --- a/tests/test_telemac_cast.py +++ b/tests/test_telemac_cast.py @@ -33,7 +33,7 @@ "mesh_file": MESH_FILE, "tag": "telemac2d", "start_date": "2018-10-1 0:0:0", - "time_frame": "12H", + "time_frame": "12h", "dem_source": DEM_FILE, "meteo_source": METEO_FILES_1, "meteo_merge": "last", # combine meteo @@ -51,7 +51,7 @@ "mesh_file": MESH_FILE, "tag": "telemac2d", "start_date": "2018-10-1 0:0:0", - "time_frame": "36H", + "time_frame": "36h", "dem_source": DEM_FILE, "meteo_source": METEO_FILES_2, "meteo_merge": "last", # combine meteo @@ -79,7 +79,6 @@ def test_telemac_cast(tmpdir, copy): original_model_info.update( { "rpath": original_rpath, - "update": ["meteo"], }, ) original_model = pyposeidon.model.set(**original_model_info) @@ -134,12 +133,16 @@ def test_telemac_cast_workflow(tmpdir): b = pyposeidon.model.set(**test_case) - b.execute() + b.create() + b.mesh.Dataset.type[:] = "closed" + b.output() + b.save() + b.run() # creating a time sequence of the runs start_date = pd.to_datetime("2018-10-1 0:0:0") end_date = pd.to_datetime("2018-10-2 0:0:0") - date_list = pd.date_range(start_date, end_date, freq="12H") + date_list = pd.date_range(start_date, end_date, freq="12h") # creating a sequence of folder to store the runs. In this case we name them after the date attribute. # NOTE that the first folder is the fisrt run already perfomed!! @@ -148,9 +151,9 @@ def test_telemac_cast_workflow(tmpdir): # creating a sequence of folder from which we read the meteo. meteo = [] for date in date_list: - end_date = pd.to_datetime(date) + pd.to_timedelta("12H") + end_date = pd.to_datetime(date) + pd.to_timedelta("12h") end_date = end_date.strftime(format="%Y-%m-%d %H:%M:%S") - dr = pd.date_range(date, end_date, freq="12H") + dr = pd.date_range(date, end_date, freq="12h") names = ["uvp_" + datetime.datetime.strftime(x, "%Y%m%d%H") + ".grib" for x in dr] dur = [(DATA_DIR / name).as_posix() for name in names] meteo.append(dur) @@ -162,11 +165,14 @@ def test_telemac_cast_workflow(tmpdir): model=b, ppath=rpaths[l], cpath=rpaths[l + 1], - meteo=METEO_FILES_2, + meteo=meteo[l + 1], sdate=date_list[l + 1], + end_date=date_list[l + 1] + pd.to_timedelta("12h"), + start=date_list[l + 1], ) - h.run(execute=True) # execute + b2 = h.run(execute=False) + b2.run() # Run check case - Total duration check.update({"rpath": rpath + "check/"}) # use tmpdir for running the model @@ -176,17 +182,18 @@ def test_telemac_cast_workflow(tmpdir): c.create() c.mesh.Dataset.type[:] = "closed" c.output() - c.set_obs() c.save() c.run() # COMPARE - ds = xr.open_mfdataset(rpaths + "results_2D.slf") - ds_check = xr.open_dataset(rpath + "check/results_2D.slf") + ds = xr.open_mfdataset([p + "/results_2D.slf" for p in rpaths]) + ds = ds.drop_duplicates("time", keep="first") # remove duplicate time stamps + ds_check = xr.open_dataset(rpath + "/check/results_2D.slf") for var in ["S"]: flag = False - mdif = np.abs(ds[var].values - ds_check[var].values).max() + inode = np.random.randint(len(ds.x)) + mdif = np.abs(ds[var].isel(node=inode).values - ds_check[var].isel(node=inode).values).max() if mdif < 1.0e-14: flag = True