Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #175

Merged
merged 13 commits into from
Apr 8, 2024
48 changes: 32 additions & 16 deletions pyposeidon/utils/cast.py
Original file line number Diff line number Diff line change
@@ -279,6 +279,8 @@ def run(self, **kwargs):

copy = get_value(self, kwargs, "copy", False)

ihot = get_value(self, kwargs, "ihot", 1)

pwd = os.getcwd()

self.origin = self.model.rpath
@@ -336,7 +338,6 @@ def run(self, **kwargs):
info["config_file"] = os.path.join(ppath, "param.nml")

# update the properties

info["rdate"] = self.rdate
info["start_date"] = self.sdate
info["time_frame"] = self.time_frame
@@ -362,7 +363,10 @@ def run(self, **kwargs):
logger.debug("create restart file")

# check for combine hotstart
hotout = int((self.sdate - self.rdate).total_seconds() / info["params"]["core"]["dt"])
if ihot == 2:
hotout = int((self.sdate - self.rdate).total_seconds() / info["params"]["core"]["dt"])
elif ihot == 1:
hotout = self.parameters["nhot_write"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should (almost) always add an else clase to an if/elif.

What if the user adds an invalid ihot value? Then hotout will not be defined and you will get a NameError which will confusing.

if instead we have:

else:
    raise ValueError("Acceptable values for `ihot` are 1 and 2, not: %s", ihot)

it will be immediately apparent what the issue is.

For the record, ideally, we should be validating the user input as soon as we get it, but to do that properly will require a major restructure so let's keep it simple for now.

logger.debug("hotout_it = {}".format(hotout))

# link restart file
@@ -430,20 +434,32 @@ def run(self, **kwargs):
logger.warning("meteo files present\n")

# modify param file
rnday_new = (self.sdate - self.rdate).total_seconds() / (3600 * 24.0) + pd.to_timedelta(
self.time_frame
).total_seconds() / (3600 * 24.0)
hotout_write = int(rnday_new * 24 * 3600 / info["params"]["core"]["dt"])
info["parameters"].update(
{
"ihot": 2,
"rnday": rnday_new,
"start_hour": self.rdate.hour,
"start_day": self.rdate.day,
"start_month": self.rdate.month,
"start_year": self.rdate.year,
}
)
if ihot == 2:
rnday_new = (self.sdate - self.rdate).total_seconds() / (3600 * 24.0) + pd.to_timedelta(
self.time_frame
).total_seconds() / (3600 * 24.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is ugly here. Either introduce new variables or maybe try some variation of this (I didn't test it):

            rnday_new = (self.sdate - self.rdate) + pd.to_timedelta(self.time_frame)
            rnday_new = rnday_new.total_seconds() / (3600 * 24.0)
            ...

hotout_write = int(rnday_new * 24 * 3600 / info["params"]["core"]["dt"])
info["parameters"].update(
{
"ihot": 2,
"rnday": rnday_new,
"start_hour": self.rdate.hour,
"start_day": self.rdate.day,
"start_month": self.rdate.month,
"start_year": self.rdate.year,
}
)
elif ihot == 1:
info["parameters"].update(
{
"ihot": 1,
"start_hour": self.sdate.hour,
"start_day": self.sdate.day,
"start_month": self.sdate.month,
"start_year": self.sdate.year,
}
)
# else:

m.config(output=True, **info) # save param.nml