Skip to content

Commit

Permalink
Bugfix for negative perturbations and handle time offset
Browse files Browse the repository at this point in the history
  • Loading branch information
trygveasp committed May 31, 2023
1 parent 6f06c82 commit 3947ea7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 44 deletions.
5 changes: 5 additions & 0 deletions pysurfex/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def as_datetime_string(dtg):
return dtg.strftime(fmt)


def offsetaware(dtg):
"""Make offset aware."""
return dtg.replace(tzinfo=timezone.utc)


def as_timedelta(seconds=0):
"""Convert seconds to timedelta."""
return timedelta(seconds=seconds)
Expand Down
24 changes: 0 additions & 24 deletions pysurfex/namelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@ def __init__(self, program, config, definitions, assemble=None, consistency=True
self.program = program
self.config = config
self.nldict = definitions
"""
macros_defs = {
"CPGDFILE": "SURFEX#IO#CPGDFILE",
"CPREPFILE": "SURFEX#IO#CPREPFILE",
"CSURFFILE": "SURFEX#IO#CSURFFILE",
"CSURF_FILETYPE": "SURFEX#IO#CSURF_FILETYPE",
"CFORCING_FILETYPE": "SURFEX#IO#CFORCING_FILETYPE",
"CTIMESERIES_FILETYPE": "SURFEX#IO#CTIMESERIES_FILETYPE",
"XRIMAX": "SURFEX#PARAMETERS#XRIMAX",
"LSPLIT_PATCH": "SURFEX#IO#LSPLIT_PATCH",
"LFAKETREE": "SURFEX#TREEDRAG#FAKETREES",
"LECOSG": "SURFEX#COVER#SG",
"XTSTEP": "SURFEX#IO#XTSTEP",
"XTSTEP_OUTPUT ": "SURFEX#IO#XTSTEP_OUTPUT",
}
macros = {}
for macro, setting in macros_defs.items():
vmacro = config.get_setting(setting)
logging.debug("Mapping macro %s = %s", macro, vmacro)
if vmacro is not None:
if isinstance(vmacro, tuple):
vmacro = list(vmacro)
macros.update({macro: vmacro})
"""

nobstype = 0
if program == "soda" or program == "offline":
Expand Down
5 changes: 3 additions & 2 deletions pysurfex/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cfunits = None


from .datetime_utils import fromtimestamp, isdatetime, utcfromtimestamp
from .datetime_utils import fromtimestamp, isdatetime, offsetaware, utcfromtimestamp
from .geo import ConfProj, Geo
from .interpolation import Interpolation

Expand Down Expand Up @@ -102,6 +102,7 @@ def nc_slice(
logging.debug("Time provided in call as datetime objects")
times_in_var = var.datetimes
for i, times_in_var_val in enumerate(times_in_var):
times_in_var_val = offsetaware(times_in_var_val)
logging.debug(
"i %s times_in_var %s times %s", i, times_in_var_val, times
)
Expand All @@ -110,7 +111,7 @@ def nc_slice(
logging.debug(
"i=%s, times_in_var_val=%s tval=%s", i, times_in_var_val, tval
)
if times_in_var[i] == tval:
if times_in_var_val == tval:
times_to_read.append(i)
if i > 0:
prev_time_steps.append(i - 1)
Expand Down
28 changes: 10 additions & 18 deletions pysurfex/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,19 @@ def __init__(
negpert (bool, optional): _description_. Defaults to False.
"""
self.pert_number = pert_number
pert_number = int(pert_number)
settings["nam_io_varassim"]["LPRT"] = True
settings["nam_var"]["nivar"] = int(pert_number)
settings["nam_var"]["nivar"] = pert_number
# Handle negative pertubations
if negpert:
nvar = int(settings["nam_var"]["nvar"])
ipert = 0
npert = 1
for nvi in range(0, nvar):
key = "nncv(" + str(nvi + 1) + ")"
val = int(settings["nam_var"][key])
# Check if active
if val == 1:
npert = 1
else:
npert = npert + 1
for __ in range(0, npert):
ipert = ipert + 1
key = "xtprt_m(" + str(ipert) + ")"
val = settings["nam_var"][key]
settings["nam_var"][key] = -val
nncv = settings["nam_var"]["nncv"]
nind = 0
for nvi, nval in enumerate(nncv):
if nval == 1:
nind += 1
if nind == pert_number:
val = settings["nam_var"]["xtprt_m"][nvi]
settings["nam_var"]["xtprt_m"][nvi] = -val
SURFEXBinary.__init__(
self,
binary,
Expand Down

0 comments on commit 3947ea7

Please sign in to comment.