Skip to content

Commit

Permalink
Merge pull request #775 from xylar/fix-decimal-time
Browse files Browse the repository at this point in the history
Allow decimal time steps in `isomip_plus`
  • Loading branch information
xylar authored Feb 22, 2024
2 parents beb4cf7 + 77f99e5 commit e32eab1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
9 changes: 3 additions & 6 deletions compass/ocean/tests/isomip_plus/forward.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import shutil
import time

import numpy as np
import xarray

from compass.model import run_model
from compass.ocean.tests.isomip_plus.evap import update_evaporation_flux
from compass.ocean.tests.isomip_plus.viz.plot import MoviePlotter
from compass.ocean.time import get_time_interval_string
from compass.step import Step


Expand Down Expand Up @@ -148,11 +148,8 @@ def run(self):
dt_per_km = config.getfloat('isomip_plus', 'dt_per_km')
dt_btr_per_km = config.getfloat('isomip_plus', 'dt_btr_per_km')

# https://stackoverflow.com/a/1384565/7728169
# Note: this will drop any fractional seconds, which is usually okay
dt = time.strftime('%H:%M:%S', time.gmtime(dt_per_km * resolution))
btr_dt = time.strftime(
'%H:%M:%S', time.gmtime(dt_btr_per_km * resolution))
dt = get_time_interval_string(seconds=dt_per_km * resolution)
btr_dt = get_time_interval_string(seconds=dt_btr_per_km * resolution)

options = dict(config_dt=f"'{dt}'",
config_btr_dt=f"'{btr_dt}'")
Expand Down
14 changes: 5 additions & 9 deletions compass/ocean/tests/isomip_plus/ssh_adjustment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time

from compass.ocean.iceshelf import adjust_ssh
from compass.ocean.time import get_time_interval_string
from compass.step import Step


Expand Down Expand Up @@ -86,14 +85,11 @@ def run(self):
dt_per_km = config.getfloat('isomip_plus', 'dt_per_km')
dt_btr_per_km = config.getfloat('isomip_plus', 'dt_btr_per_km')

# https://stackoverflow.com/a/1384565/7728169
# Note: this will drop any fractional seconds, which is usually okay
dt = time.strftime('%H:%M:%S', time.gmtime(dt_per_km * resolution))
btr_dt = time.strftime(
'%H:%M:%S', time.gmtime(dt_btr_per_km * resolution))
dt = get_time_interval_string(seconds=dt_per_km * resolution)
btr_dt = get_time_interval_string(seconds=dt_btr_per_km * resolution)

options = dict(config_dt="'{}'".format(dt),
config_btr_dt="'{}'".format(btr_dt))
options = dict(config_dt=f"'{dt}'",
config_btr_dt=f"'{btr_dt}'")
self.update_namelist_at_runtime(options)

iteration_count = config.getint('ssh_adjustment', 'iterations')
Expand Down
38 changes: 38 additions & 0 deletions compass/ocean/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time

import numpy as np


def get_time_interval_string(days=None, seconds=None):
"""
Convert a time interval in days and/or seconds to a string for use in a
model config option. If both are provided, they will be added
Parameters
----------
days : float, optional
A time interval in days
seconds : float, optional
A time interval in seconds
Returns
-------
time_str : str
The time as a string in the format "DDDD_HH:MM:SS.SS"
"""
sec_per_day = 86400
total = 0.
if seconds is not None:
total += seconds
if days is not None:
total += sec_per_day * days

day_part = int(total / sec_per_day)
sec_part = total - day_part * sec_per_day
sec_decimal = sec_part - np.floor(sec_part)
# https://stackoverflow.com/a/1384565/7728169
seconds_str = time.strftime('%H:%M:%S', time.gmtime(sec_part))
time_str = f'{day_part:04d}_{seconds_str}.{int(sec_decimal * 1e3):03d}'
return time_str

0 comments on commit e32eab1

Please sign in to comment.