-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from xylar/fix-decimal-time
Allow decimal time steps in `isomip_plus`
- Loading branch information
Showing
3 changed files
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |