Adding support for time (HH:MM:SS.MICROSEC) in time_vector. Updating tests accordingly.
This commit is contained in:
parent
dc207ae8c5
commit
fe5004685c
@ -1,4 +1,6 @@
|
||||
import datetime
|
||||
from pandas import to_datetime
|
||||
|
||||
from operator import attrgetter
|
||||
try:
|
||||
from StringIO import StringIO
|
||||
@ -43,7 +45,17 @@ def _make_datetime(dates_record):
|
||||
month = dates_record[1].get_str(0)
|
||||
year = dates_record[2].get_int(0)
|
||||
|
||||
return datetime.datetime(year, ecl_month[month], day)
|
||||
date_dt = datetime.datetime(year, ecl_month[month], day)
|
||||
if len(dates_record) < 4:
|
||||
return date_dt
|
||||
else:
|
||||
time_str = dates_record[3].get_str(0)
|
||||
dt = to_datetime('1900-01-01 '+time_str)
|
||||
if dt.time():
|
||||
return datetime.datetime(year, ecl_month[month], day, dt.hour, dt.minute, dt.second, dt.microsecond)
|
||||
else:
|
||||
return date_dt
|
||||
|
||||
|
||||
class TimeStep(object):
|
||||
|
||||
@ -92,7 +104,13 @@ class TimeStep(object):
|
||||
day = self.dt.day
|
||||
month = self.dt.month
|
||||
year = self.dt.year
|
||||
string.write("DATES\n {day} '{month}' {year}/\n/\n\n".format( day=day, month = inv_ecl_month[month], year=year))
|
||||
if not self.dt.time():
|
||||
string.write("DATES\n {day} '{month}' {year}/\n/\n\n".format( day=day, month = inv_ecl_month[month], year=year))
|
||||
else:
|
||||
hour = self.dt.hour
|
||||
minute = self.dt.minute
|
||||
second = self.dt.second + self.dt.microsecond*1.0e-6
|
||||
string.write("DATES\n {day} '{month}' {year} {hour:02d}:{minute:02d}:{second:2.6f} /\n/\n\n".format( day=day, month = inv_ecl_month[month], year=year, hour=hour, minute=minute, second=second))
|
||||
|
||||
for kw in self.keywords:
|
||||
string.write(str(kw))
|
||||
@ -238,14 +256,15 @@ class TimeVector(object):
|
||||
def __getitem__(self, index):
|
||||
"""Will look up a timestep in the vector.
|
||||
|
||||
The index argument can either be an integer or a datetime instance.
|
||||
The index argument can either be an integer or a datetime.date/datetime instance.
|
||||
|
||||
"""
|
||||
if isinstance(index,int):
|
||||
return self.time_steps_list[index]
|
||||
else:
|
||||
if isinstance(index,datetime.date):
|
||||
if not isinstance(index,datetime.datetime) and isinstance(index,datetime.date):
|
||||
index = datetime.datetime(index.year, index.month, index.day)
|
||||
|
||||
return self.time_steps_dict[index]
|
||||
|
||||
|
||||
|
@ -66,7 +66,7 @@ class TestTimeVector(unittest.TestCase):
|
||||
tv.load(test_path("data/schedule/part2.sch"))
|
||||
|
||||
self.assertEqual(tv.dates, [datetime.datetime(1997, 11, 6),
|
||||
datetime.datetime(1997, 11, 14),
|
||||
datetime.datetime(1997, 11, 14, 0, 0, 0, 1000),
|
||||
datetime.datetime(1997, 12, 1),
|
||||
datetime.datetime(1997, 12, 17),
|
||||
datetime.datetime(1998, 1, 1),
|
||||
@ -136,12 +136,12 @@ class TestTimeVector(unittest.TestCase):
|
||||
with self.assertRaises(KeyError):
|
||||
tv.delete(datetime.datetime(2019,1,1))
|
||||
|
||||
ts = tv[datetime.datetime(1997,11,14)]
|
||||
ts = tv[datetime.datetime(1997,11,14,0,0,0,1000)]
|
||||
self.assertTrue("WTEST" in ts)
|
||||
tv.delete(datetime.datetime(1997,11,14))
|
||||
tv.delete(datetime.datetime(1997,11,14,0,0,0,1000))
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
tv.delete(datetime.datetime(1997,11,14))
|
||||
tv.delete(datetime.datetime(1997,11,14,0,0,0,1000))
|
||||
|
||||
for ts in tv:
|
||||
self.assertFalse("WTEST" in ts)
|
||||
|
Loading…
Reference in New Issue
Block a user