def test_range(self): d1 = date(2000, 1, 1) dt1 = datetime(2000, 1, 1, 0, 0, 0) c1 = CTime(d1) d0 = date(1999, 1, 1) dt0 = datetime(1999, 1, 1, 0, 0, 0) c0 = CTime(d0) d2 = date(2001, 1, 1) dt2 = datetime(2001, 1, 1, 0, 0, 0) c2 = CTime(d2) self.assertTrue(d0 <= c1 < dt2) self.assertTrue(c0 <= c1 < d2) self.assertTrue(dt0 <= c1 < c2) self.assertFalse(d1 <= c0 < dt2) self.assertFalse(c1 <= c0 < d2) self.assertFalse(dt1 <= c0 < c2) self.assertTrue(d0 <= c0 < dt2) self.assertTrue(c0 <= c0 < d2) self.assertTrue(dt0 <= c0 < c2) self.assertFalse(d0 <= c2 < dt2) self.assertFalse(c0 <= c2 < d2) self.assertFalse(dt0 <= c2 < c2) self.assertTrue(d0 <= c2 <= dt2) self.assertTrue(c0 <= c2 <= d2) self.assertTrue(dt0 <= c2 <= c2)
def createRegular(cls, start, end, deltaString): """ The last element in the vector will be <= end; i.e. if the question of whether the range is closed in the upper end depends on the commensurability of the [start,end] interval and the delta: createRegular(0 , 10 , delta=3) => [0,3,6,9] createRegular(0 , 10 , delta=2) => [0,2,4,6,8,10] """ start = CTime(start) end = CTime(end) if start > end: raise ValueError("The time interval is invalid start is after end") (num, timeUnit) = cls.parseTimeUnit(deltaString) timeVector = TimeVector() currentTime = start while currentTime <= end: ct = CTime(currentTime) timeVector.append(ct) currentTime = timeVector.nextTime(num, timeUnit) return timeVector
def iget_restart_sim_time(self, index): """ Will locate restart block nr @index and return the true time as a datetime instance. """ ct = CTime(cfunc.iget_restart_time(self, index)) return ct.datetime()
def iget_date(self , time_index): """ Returns the simulation date for element nr @time_index. """ long_time = self._iget_sim_time( time_index ) ct = CTime(long_time) return ct.datetime()
def iget_date(self, time_index): """ Returns the simulation date for element nr @time_index. """ long_time = self._iget_sim_time(time_index) ct = CTime(long_time) return ct.datetime()
def iget_restart_sim_time( self , index ): """ Will locate restart block nr @index and return the true time as a datetime instance. """ ct = CTime(cfunc.iget_restart_time( self , index )) return ct.datetime()
def iget_date(self, time_index): """ Returns the simulation date for element nr @t ime_index. """ long_time = EclSum.cNamespace().iget_sim_time(self, time_index) ct = CTime(long_time) return ct.datetime()
def iget_date(self , time_index): """ Returns the simulation date for element nr @t ime_index. """ long_time = EclSum.cNamespace().iget_sim_time( self , time_index ) ct = CTime(long_time) return ct.datetime()
def test_c_time(self): c_time = CTime(0) self.assertEqual(str(c_time), "1970-01-01 01:00:00") date_time = CTime(datetime(1970, 1, 1, 1, 0, 0)) self.assertEqual(c_time, date_time) date_time_after = CTime(datetime(1970, 1, 1, 1, 0, 5)) self.assertTrue(date_time_after > date_time)
def __init__(self, default_value=None, initial_size=0): if default_value is None: super(TimeVector, self).__init__(CTime(0), initial_size) else: try: default = CTime( default_value ) except: raise ValueError("default value invalid - must be type ctime() or date/datetime") super(TimeVector, self).__init__(default , initial_size)
def test_creation(self): t0 = CTime(0) t2 = CTime(datetime(1970, 1, 1)) self.assertEqual(t0, t2) t3 = CTime(date(1970, 1, 1)) self.assertEqual(t0, t3) with self.assertRaises(NotImplementedError): CTime("string")
def test_c_time(self): delta = timezoneOffsetInSeconds(datetime(1970, 1, 1)) c_time = CTime(-delta) py_time = datetime(1970, 1, 1) self.assertEqual(str(c_time), py_time.strftime("%Y-%m-%d %H:%M:%S%z")) date_time = CTime(py_time) self.assertEqual(c_time, date_time) date_time_after = CTime(datetime(1970, 1, 1, 1, 0, 5)) self.assertTrue(date_time_after > date_time)
def test_math(self): c1 = CTime(date(2000, 1, 1)) c2 = CTime(date(2000, 1, 1)) c3 = CTime(date(2000, 1, 1)) c3 += c1 self.assertTrue(isinstance(c3, CTime)) c4 = c1 * 1.0 self.assertTrue(isinstance(c4, CTime)) self.assertTrue(isinstance(c1 + c2, CTime)) self.assertEqual((c1 + c2) * 0.5, date(2000, 1, 1))
def test_c_time(self): delta = 0 c_time = CTime(0) py_time = datetime(1970, 1, 1) self.assertEqual(str(c_time), py_time.strftime("%Y-%m-%d %H:%M:%S%z")) date_time = CTime(py_time) self.assertEqual(c_time, date_time) date_time_after = CTime(datetime(1970, 1, 1, 1, 0, 5)) self.assertTrue(date_time_after > date_time)
def test_creation(self): delta = timezoneOffsetInSeconds(datetime(1970, 1, 1)) t0 = CTime(-delta) t1 = CTime(t0) self.assertEqual(t0, t1) t2 = CTime(datetime(1970, 1, 1)) self.assertEqual(t0, t2) t3 = CTime(date(1970, 1, 1)) self.assertEqual(t0, t3) with self.assertRaises(NotImplementedError): CTime("string")
def lookupTime(self, time): index = TimeMap.cNamespace().lookup_time(self, CTime(time)) if index >= 0: return index else: raise ValueError( "The time:%s was not found in the time_map instance" % time)
def restart_block( cls , filename , dtime = None , report_step = None): """ Load one report step from unified restart file. Unified restart files can be prohibitively large; with this class method it is possible to load only one report step. Which report step you are interested in must be specified with either one of the optional arguments @report_step or @dtime. If present @dtime should be a normal python datetime instance: block1 = EclFile.restart_block( "ECLIPSE.UNRST" , dtime = datetime.datetime( year , month , day )) block2 = EclFile.restart_block( "ECLIPSE.UNRST" , report_step = 67 ) If the block you are asking for can not be found the method will return None. """ obj = EclFile( filename ) if dtime: OK = obj._restart_block_time( CTime( dtime )) elif not report_step is None: OK = obj._restart_block_step( report_step ) else: raise TypeError("restart_block() requires either dtime or report_step argument - none given.") if not OK: if dtime: raise ValueError("Could not locate date:%02d/%02d/%4d in restart file: %s." % (dtime.day , dtime.month , dtime.year , filename)) else: raise ValueError("Could not locate report step:%d in restart file: %s." % (report_step , filename)) return obj
def blockedProduction(self , totalKey , timeRange): node = self.smspec_node(totalKey) if node.is_total: total = DoubleVector() for t in timeRange: if t < CTime(self.start_time): total.append( 0 ) elif t >= CTime( self.end_time): total.append( self.get_last_value( totalKey )) else: total.append( self.get_interp( totalKey , date = t )) tmp = total << 1 total.pop() return tmp - total else: raise TypeError("The blockedProduction method must be called with one of the TOTAL keys like e.g. FOPT or GWIT")
def getStartTime(self): """ A Python datetime instance with the start time. See start_date() for further details. """ return CTime( EclSum.cNamespace().get_start_date( self ) ).datetime()
def check_sim_time(self, date): """ Will check if the input date is in the time span [sim_start , sim_end]. """ if not isinstance(date, CTime): date = CTime(date) return self._check_sim_time(date)
def getStartTime(self): """ A Python datetime instance with the start time. See start_date() for further details. """ return CTime(self._get_start_date()).datetime()
def get_interp(self, key, days=None, date=None): """ Will lookup vector @key at time given by @days or @date. Requiers exactly one input argument @days or @date; will raise exception ValueError if this is not satisfied. The method will check that the time argument is within the time limits of the simulation; if else the method will raise exception ValueError. Also available as method get_interp() on the EclSumVector class. """ self.assertKeyValid(key) if days is None and date is None: raise ValueError("Must supply either days or date") if days is None: t = CTime(date) if self.check_sim_time(t): return self._get_general_var_from_sim_time(t, key) else: raise ValueError( "date:%s is outside range of simulation data" % date) elif date is None: if self._check_sim_days(days): return self._get_general_var_from_sim_days(days, key) else: raise ValueError( "days:%s is outside range of simulation: [%g,%g]" % (days, self.first_day, self.sim_length)) else: raise ValueError("Must supply either days or date")
def timeRange(self, start=None, end=None, interval="1Y", extend_end=True): (num, timeUnit) = TimeVector.parseTimeUnit(interval) if start is None: start = self.getDataStartTime() if isinstance(start, datetime.date): start = datetime.datetime(start.year, start.month, start.day, 0, 0, 0) if end is None: end = self.end_time if isinstance(end, datetime.date): end = datetime.datetime(end.year, end.month, end.day, 0, 0, 0) if end < start: raise ValueError("Invalid time interval start after end") if not timeUnit == "d": year1 = start.year year2 = end.year month1 = start.month month2 = end.month day1 = start.day day2 = end.day if extend_end: if timeUnit == 'm': if day2 > 1: month2 += 1 if month2 == 13: year2 += 1 month2 = 1 elif timeUnit == "y": month1 = 1 if year2 > 1 or day2 > 1: year2 += 1 month2 = 1 day1 = 1 day2 = 1 range_start = datetime.date(year1, month1, day1) range_end = datetime.date(year2, month2, day2) trange = TimeVector.createRegular(range_start, range_end, interval) # If the simulation does not start at the first of the month # the start value will be before the simulation start; we # manually shift the first element in the trange to the start # value; the same for the end of list. if trange[-1] < end: if extend_end: trange.appendTime(num, timeUnit) else: trange.append(end) if trange[0] < start: trange[0] = CTime(start) return trange
def update(self, index, time): if TimeMap.cNamespace().try_update(self, index, CTime(time)): return True else: if self.isStrict(): raise Exception("Tried to update with inconsistent value") else: return False
def dumpCSVLine(self, time, keywords, pfile): """ Will dump a csv formatted line of the keywords in @keywords, evaluated at the intertpolated time @time. @pfile should point to an open Python file handle. """ cfile = CFILE(pfile) ctime = CTime(time) EclSum._dump_csv_line(self, ctime, keywords, cfile)
def headers(self): """ Returns a list of two tuples (well_name , date) for the whole file. """ header_list = [] for i in (range(cfunc_file.get_size(self, None, CTime(-1)))): rft = self.iget(i) header_list.append((rft.well, rft.date)) return header_list
def getDataStartTime(self): """The first date we have data for. Thiw will mostly be equal to getStartTime(), but in the case of restarts, where the case we have restarted from is not found, this time will be later than the true start of the field. """ return CTime(self._get_data_start()).datetime()
def getHeaders(self): """ Returns a list of two tuples (well_name , date) for the whole file. """ header_list = [] for i in (range(self._get_size(None, CTime(-1)))): rft = self.iget(i) header_list.append((rft.getWellName(), rft.getDate())) return header_list
def get(self, well_name, date): """ Will look up the RFT object corresponding to @well and @date. Returns None if no matching RFT can be found. """ c_ptr = cfunc_file.get_rft(self, well_name, CTime(date)) if c_ptr: return EclRFT(c_ptr, self) else: return None
def start_date(self): """ A Python date instance with the start date. The start time is taken from the SMSPEC file, and in case not all timesteps have been loaded, e.g. for a restarted case, the returned start_date might be different from the datetime of the first (loaded) timestep. """ ct = self._get_start_date() return CTime(ct).date()
def __init__(self, filename, start_time): if os.path.isfile(filename): c_ptr = self._parse(filename, CTime(start_time)) super(SchedFile, self).__init__(c_ptr) if not c_ptr: err_msg = 'start_time = "%s", filename = "%s"' % ( str(start_time), str(filename)) raise ValueError('Unable to construct SchedFile with %s.' % err_msg) else: raise IOError('No such file "%s"' % filename)
def has_sim_time(self, dtime): """ Checks if the current EclFile has data for time @dtime. The implementation goes through all the INTEHEAD headers in the EclFile, i.e. it can be fooled (and probably crash and burn) if the EclFile instance in question is has INTEHEAD keyword(s), but is still not a restart file. The @dtime argument should be a normal python datetime instance. """ return cfunc.has_sim_time(self, CTime(dtime))
def get(self, well_name, date): """ Will look up the RFT object corresponding to @well and @date. Raise Exception if not found. """ if self.size(well=well_name, date=date) == 0: raise KeyError("No RFT for well:%s at %s" % (well_name, date)) rft = self._get_rft(well_name, CTime(date)) rft.setParent(self) return rft
def test_conversion(self): t = CTime(0) self.assertEqual(t.value(), 0) self.assertEqual(t.ctime(), 0) self.assertEqual(t.time(), time.localtime(0)) # These conversions depend on timezone utc = timezone('utc') local_timezone = timezone(CTime.timezone()) localized_dt = local_timezone.localize(t.datetime()) self.assertEqual(localized_dt.astimezone(utc), datetime(1970, 1, 1, 0, tzinfo=utc)) localized_d = datetime(1970, 1, 1, 0, tzinfo=utc).astimezone(local_timezone) self.assertEqual(t.date(), localized_d.date())
def timezoneOffsetInSeconds(dt): local_timezone = timezone(CTime.timezone()) return int(local_timezone.utcoffset(dt).total_seconds())
def date(self): """ The date when this RFT/PLT/... was recorded. """ ct = CTime(cfunc_rft.get_date( self )) return ct.date()
def getDate(self): """ The date when this RFT/PLT/... was recorded. """ ct = CTime(self._get_date( )) return ct.date()
def getSimDate(self): ct = CTime( self._get_sim_time( ) ) return ct.datetime( )
def getSimDate(self): ct = CTime( EclRestartHead.cNamespace().get_sim_time( self ) ) return ct.datetime( )
def test_conversion(self): t = CTime(0) self.assertEqual(t.value(), 0) self.assertEqual(t.ctime(), 0) self.assertEqual(t.time(), time.gmtime(0))