def transfomActiveTableToThrift(table, mode='PRACTICE'): from dynamicserialize.dstypes.com.raytheon.uf.common.activetable.PracticeActiveTableRecord \ import PracticeActiveTableRecord from dynamicserialize.dstypes.com.raytheon.uf.common.activetable.OperationalActiveTableRecord \ import OperationalActiveTableRecord # TODO: Eliminate use of Calendar from dynamicserialize.dstypes.java.util import Date tableList = [] if mode.upper() == 'PRACTICE': recordCtor = PracticeActiveTableRecord else: recordCtor = OperationalActiveTableRecord for template in table: atr = recordCtor() atr.setVtecstr(template['vtecstr']) atr.setEtn(str(template['etn'])) atr.setSig(template['sig']) atr.setPhen(template['phen']) if template.has_key('segText'): atr.setSegText(template['segText']) if template.has_key('overviewText'): atr.setOverviewText(template['overviewText']) atr.setPhensig(template['phensig']) atr.setAct(template['act']) atr.setSeg(template['seg']) start = Date(template['startTime'] * 1000) atr.setStartTime(start) end = Date(template['endTime'] * 1000) atr.setEndTime(end) purge = Date(template['purgeTime'] * 1000) atr.setPurgeTime(purge) issue = Date(template['issueTime'] * 1000) atr.setIssueTime(issue) atr.setUfn(template['ufn']) atr.setOfficeid(template['officeid']) # template['state'] = "Decoded" atr.setXxxid(template['xxxid']) atr.setPil(template['pil']) atr.setProductClass(template['productClass']) atr.setUgcZone(template['id']) atr.setRawmessage(template['rawMessage']) tableList.append(atr) return tableList
def __init__(self, refTime=None, fcstTime=None, validPeriod=None): self.fcstTime = int(fcstTime) if fcstTime is not None else 0 self.refTime = refTime if refTime is not None else None if validPeriod is not None and type(validPeriod) is not TimeRange: ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod if validPeriod is not None else None self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: self.refTime = int(refTime) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED")
def main(args): # if no args other than script add --help so usage is displayed if len(args) < 2: args.extend(["--help"]) # if --help in args add dummy --site arg so we can display # full script usage, not just the gfeClient.py usage if "--help" in args: args.extend(["--site", "XXX"]) parser, gfeClientArgs, scriptArgs = validateArgs(args) # add config and user option to scriptArgs scriptArgs.extend(["-c", gfeClientArgs.configFile, "-u", gfeClientArgs.userName]) # add drt option if specified if gfeClientArgs.drt: timeString = time.strftime(TIME_FORMAT, gfeClientArgs.drt) scriptArgs.extend(["-z", timeString]) # add startTime option if specified if gfeClientArgs.startTime: scriptArgs.extend(["-s", gfeClientArgs.startTime]) # shutdown isn't a real script and has no gfeClientArgs to validate if gfeClientArgs.script.lower() != "shutdown": # call the validateArgs() method in the target script scriptGlobals = {} scriptLocals = {} execfile(gfeClientArgs.script, scriptGlobals, scriptLocals) scriptLocals["validateArgs"](args, [parser]) elif "--help" in args: # Don't do shutdown if --help specified # this is only for ifpIMAGE since it's calling shutdown until # PngWriter can be fixed to run more than once in a session sys.exit(0) request = GfeClientRequest(gfeClientArgs.script, gfeClientArgs.site, gfeClientArgs.configFile, gfeClientArgs.userName, scriptArgs) if gfeClientArgs.drt: import calendar timeInMillis = calendar.timegm(gfeClientArgs.drt) * 1000 request.setTime(Date(timeInMillis)) thriftClient = ThriftClient.ThriftClient(gfeClientArgs.host, gfeClientArgs.port, "/services") thriftClient.sendRequest(request)
def __init__(self, refTime=None, fcstTime=None, validPeriod=None): self.fcstTime = int(fcstTime) if fcstTime is not None else 0 self.refTime = refTime if refTime is not None else None if validPeriod is not None and type(validPeriod) is not TimeRange: ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod if validPeriod is not None else None self.utilityFlags = EnumSet( 'com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int( calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: self.refTime = int(refTime) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int( self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED")
def __init__(self, refTime=None, fcstTime=None, validPeriod=None): """ Construct a new DataTime. May also be called as DataTime(str) to parse a string and create a DataTime from it. Some examples of valid DataTime strings: '2016-08-02 01:23:45.0' '2016-08-02 01:23:45.123' '2016-08-02 01:23:45.0 (17)', '2016-08-02 01:23:45.0 (17:34)' '2016-08-02 01:23:45.0[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' """ if fcstTime is not None: self.fcstTime = int(fcstTime) else: self.fcstTime = 0 self.refTime = refTime if validPeriod is not None and type(validPeriod) is not TimeRange: raise ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: try: self.refTime = int(self.refTime) except ValueError: # Assume first arg is a string. Attempt to parse. match = STR_PATTERN.match(self.refTime) if match is None: raise ValueError('Could not parse DataTime info from ' + str(refTime)) groups = match.groups() rDate = groups[0] rTime = groups[1] rMillis = groups[2] or 0 fcstTimeHr = groups[3] fcstTimeMin = groups[4] periodStart = groups[5], groups[6], (groups[7] or 0) periodEnd = groups[8], groups[9], (groups[10] or 0) self.refTime = self._getTimeAsEpochMillis(rDate, rTime, rMillis) if fcstTimeHr is not None: self.fcstTime = int(fcstTimeHr) * 3600 if fcstTimeMin is not None: self.fcstTime += int(fcstTimeMin) * 60 if periodStart[0] is not None: self.validPeriod = TimeRange() periodStartTime = self._getTimeAsEpochMillis(*periodStart) self.validPeriod.setStart(periodStartTime / 1000) periodEndTime = self._getTimeAsEpochMillis(*periodEnd) self.validPeriod.setEnd(periodEndTime / 1000) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if self.fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED")
class DataTime(object): def __init__(self, refTime=None, fcstTime=None, validPeriod=None): """ Construct a new DataTime. May also be called as DataTime(str) to parse a string and create a DataTime from it. Some examples of valid DataTime strings: '2016-08-02 01:23:45.0' '2016-08-02 01:23:45.123' '2016-08-02 01:23:45.0 (17)', '2016-08-02 01:23:45.0 (17:34)' '2016-08-02 01:23:45.0[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' """ if fcstTime is not None: self.fcstTime = int(fcstTime) else: self.fcstTime = 0 self.refTime = refTime if validPeriod is not None and type(validPeriod) is not TimeRange: raise ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: try: self.refTime = int(self.refTime) except ValueError: # Assume first arg is a string. Attempt to parse. match = STR_PATTERN.match(self.refTime) if match is None: raise ValueError('Could not parse DataTime info from ' + str(refTime)) groups = match.groups() rDate = groups[0] rTime = groups[1] rMillis = groups[2] or 0 fcstTimeHr = groups[3] fcstTimeMin = groups[4] periodStart = groups[5], groups[6], (groups[7] or 0) periodEnd = groups[8], groups[9], (groups[10] or 0) self.refTime = self._getTimeAsEpochMillis(rDate, rTime, rMillis) if fcstTimeHr is not None: self.fcstTime = int(fcstTimeHr) * 3600 if fcstTimeMin is not None: self.fcstTime += int(fcstTimeMin) * 60 if periodStart[0] is not None: self.validPeriod = TimeRange() periodStartTime = self._getTimeAsEpochMillis(*periodStart) self.validPeriod.setStart(periodStartTime / 1000) periodEndTime = self._getTimeAsEpochMillis(*periodEnd) self.validPeriod.setEnd(periodEndTime / 1000) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if self.fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED") def getRefTime(self): return self.refTime def setRefTime(self, refTime): self.refTime = refTime def getFcstTime(self): return self.fcstTime def setFcstTime(self, fcstTime): self.fcstTime = fcstTime def getValidPeriod(self): return self.validPeriod def setValidPeriod(self, validPeriod): self.validPeriod = validPeriod def getUtilityFlags(self): return self.utilityFlags def setUtilityFlags(self, utilityFlags): self.utilityFlags = utilityFlags def getLevelValue(self): return self.levelValue def setLevelValue(self, levelValue): self.levelValue = numpy.float64(levelValue) def __str__(self): buffer = StringIO() if self.refTime is not None: refTimeInSecs = self.refTime.getTime() / 1000 micros = (self.refTime.getTime() % 1000) * 1000 dtObj = datetime.datetime.utcfromtimestamp(refTimeInSecs) dtObj = dtObj.replace(microsecond=micros) # This won't be compatible with java or string from java since its to microsecond buffer.write(dtObj.isoformat(' ')) if "FCST_USED" in self.utilityFlags: hrs = int(self.fcstTime / 3600) mins = int((self.fcstTime - (hrs * 3600)) / 60) buffer.write(" (" + str(hrs)) if mins != 0: buffer.write(":" + str(mins)) buffer.write(")") if "PERIOD_USED" in self.utilityFlags: buffer.write("[") buffer.write(self.validPeriod.start.isoformat(' ')) buffer.write("--") buffer.write(self.validPeriod.end.isoformat(' ')) buffer.write("]") strVal = buffer.getvalue() buffer.close() return strVal def __repr__(self): return "<DataTime instance: " + str(self) + " >" def __hash__(self): hashCode = hash(self.refTime) ^ hash(self.fcstTime) if self.validPeriod is not None and self.validPeriod.isValid(): hashCode ^= hash(self.validPeriod.getStart()) hashCode ^= hash(self.validPeriod.getEnd()) hashCode ^= hash(self.levelValue) return hashCode def __eq__(self, other): if type(self) != type(other): return False if other.getRefTime() is None: return self.fcstTime == other.fcstTime dataTime1 = (self.refTime, self.fcstTime, self.validPeriod, self.levelValue) dataTime2 = (other.refTime, other.fcstTime, other.validPeriod, other.levelValue) return dataTime1 == dataTime2 def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime < otherValidTime: return True if self.fcstTime < other.fcstTime: return True if self.levelValue < other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() < otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() < otherValidPeriod.getStartInMillis() return False def __le__(self, other): if type(self) != type(other): return NotImplemented return self.__lt__(other) or self.__eq__(other) def __gt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime > otherValidTime: return True if self.fcstTime > other.fcstTime: return True if self.levelValue > other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() > otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() > otherValidPeriod.getStartInMillis() return False def __ge__(self, other): if type(self) != type(other): return NotImplemented return self.__gt__(other) or self.__eq__(other) def _getTimeAsEpochMillis(self, dateStr, timeStr, millis): t = time.strptime(dateStr + ' ' + timeStr, '%Y-%m-%d %H:%M:%S') epochSeconds = calendar.timegm(t) return int(epochSeconds * 1000) + int(millis)
def deserialize(context): result = Date() result.setTime(context.readI64()) return result
def __init__(self, refTime=None, fcstTime=None, validPeriod=None): """ Construct a new DataTime. May also be called as DataTime(str) to parse a string and create a DataTime from it. Some examples of valid DataTime strings: '2016-08-02 01:23:45.0' '2016-08-02 01:23:45.123' '2016-08-02 01:23:45.0 (17)', '2016-08-02 01:23:45.0 (17:34)' '2016-08-02 01:23:45.0[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' """ if fcstTime is not None: self.fcstTime = int(fcstTime) else: self.fcstTime = 0 self.refTime = refTime if validPeriod is not None and type(validPeriod) is not TimeRange: raise ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = long(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = long(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = long(self.refTime.getTime()) else: try: self.refTime = long(self.refTime) except ValueError: # Assume first arg is a string. Attempt to parse. match = STR_PATTERN.match(self.refTime) if match is None: raise ValueError('Could not parse DataTime info from ' + str(refTime)) groups = match.groups() rDate = groups[0] rTime = groups[1] rMillis = groups[2] or 0 fcstTimeHr = groups[3] fcstTimeMin = groups[4] periodStart = groups[5], groups[6], (groups[7] or 0) periodEnd = groups[8], groups[9], (groups[10] or 0) self.refTime = self._getTimeAsEpochMillis(rDate, rTime, rMillis) if fcstTimeHr is not None: self.fcstTime = long(fcstTimeHr) * 3600 if fcstTimeMin is not None: self.fcstTime += long(fcstTimeMin) * 60 if periodStart[0] is not None: self.validPeriod = TimeRange() periodStartTime = self._getTimeAsEpochMillis(*periodStart) self.validPeriod.setStart(periodStartTime / 1000) periodEndTime = self._getTimeAsEpochMillis(*periodEnd) self.validPeriod.setEnd(periodEndTime / 1000) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + long(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if self.fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED")
class DataTime(object): def __init__(self, refTime=None, fcstTime=None, validPeriod=None): """ Construct a new DataTime. May also be called as DataTime(str) to parse a string and create a DataTime from it. Some examples of valid DataTime strings: '2016-08-02 01:23:45.0' '2016-08-02 01:23:45.123' '2016-08-02 01:23:45.0 (17)', '2016-08-02 01:23:45.0 (17:34)' '2016-08-02 01:23:45.0[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' """ if fcstTime is not None: self.fcstTime = int(fcstTime) else: self.fcstTime = 0 self.refTime = refTime if validPeriod is not None and type(validPeriod) is not TimeRange: raise ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = long(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = long(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = long(self.refTime.getTime()) else: try: self.refTime = long(self.refTime) except ValueError: # Assume first arg is a string. Attempt to parse. match = STR_PATTERN.match(self.refTime) if match is None: raise ValueError('Could not parse DataTime info from ' + str(refTime)) groups = match.groups() rDate = groups[0] rTime = groups[1] rMillis = groups[2] or 0 fcstTimeHr = groups[3] fcstTimeMin = groups[4] periodStart = groups[5], groups[6], (groups[7] or 0) periodEnd = groups[8], groups[9], (groups[10] or 0) self.refTime = self._getTimeAsEpochMillis(rDate, rTime, rMillis) if fcstTimeHr is not None: self.fcstTime = long(fcstTimeHr) * 3600 if fcstTimeMin is not None: self.fcstTime += long(fcstTimeMin) * 60 if periodStart[0] is not None: self.validPeriod = TimeRange() periodStartTime = self._getTimeAsEpochMillis(*periodStart) self.validPeriod.setStart(periodStartTime / 1000) periodEndTime = self._getTimeAsEpochMillis(*periodEnd) self.validPeriod.setEnd(periodEndTime / 1000) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + long(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if self.fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED") def getRefTime(self): return self.refTime def setRefTime(self, refTime): self.refTime = refTime def getFcstTime(self): return self.fcstTime def setFcstTime(self, fcstTime): self.fcstTime = fcstTime def getValidPeriod(self): return self.validPeriod def setValidPeriod(self, validPeriod): self.validPeriod = validPeriod def getUtilityFlags(self): return self.utilityFlags def setUtilityFlags(self, utilityFlags): self.utilityFlags = utilityFlags def getLevelValue(self): return self.levelValue def setLevelValue(self, levelValue): self.levelValue = numpy.float64(levelValue) def __str__(self): buffer = StringIO.StringIO() if self.refTime is not None: refTimeInSecs = self.refTime.getTime() / 1000 micros = (self.refTime.getTime() % 1000) * 1000 dtObj = datetime.datetime.utcfromtimestamp(refTimeInSecs) dtObj = dtObj.replace(microsecond=micros) # This won't be compatible with java or string from java since its to microsecond buffer.write(dtObj.isoformat(' ')) if "FCST_USED" in self.utilityFlags: hrs = int(self.fcstTime / 3600) mins = int((self.fcstTime - (hrs * 3600)) / 60) buffer.write(" (" + str(hrs)) if mins != 0: buffer.write(":" + str(mins)) buffer.write(")") if "PERIOD_USED" in self.utilityFlags: buffer.write("[") buffer.write(self.validPeriod.start.isoformat(' ')) buffer.write("--") buffer.write(self.validPeriod.end.isoformat(' ')) buffer.write("]") strVal = buffer.getvalue() buffer.close() return strVal def __repr__(self): return "<DataTime instance: " + str(self) + " >" def __hash__(self): hashCode = hash(self.refTime) ^ hash(self.fcstTime) if self.validPeriod is not None and self.validPeriod.isValid(): hashCode ^= hash(self.validPeriod.getStart()) hashCode ^= hash(self.validPeriod.getEnd()) hashCode ^= hash(self.levelValue) return hashCode def __eq__(self, other): if type(self) != type(other): return False if other.getRefTime() is None: return self.fcstTime == other.fcstTime dataTime1 = (self.refTime, self.fcstTime, self.validPeriod, self.levelValue) dataTime2 = (other.refTime, other.fcstTime, other.validPeriod, other.levelValue) return dataTime1 == dataTime2 def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime < otherValidTime: return True if self.fcstTime < other.fcstTime: return True if self.levelValue < other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() < otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() < otherValidPeriod.getStartInMillis() return False def __le__(self, other): if type(self) != type(other): return NotImplemented return self.__lt__(other) or self.__eq__(other) def __gt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime > otherValidTime: return True if self.fcstTime > other.fcstTime: return True if self.levelValue > other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() > otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() > otherValidPeriod.getStartInMillis() return False def __ge__(self, other): if type(self) != type(other): return NotImplemented return self.__gt__(other) or self.__eq__(other) def _getTimeAsEpochMillis(self, dateStr, timeStr, millis): t = time.strptime(dateStr + ' ' + timeStr, '%Y-%m-%d %H:%M:%S') epochSeconds = calendar.timegm(t) return long(epochSeconds * 1000) + long(millis)
class DataTime(object): def __init__(self, refTime=None, fcstTime=None, validPeriod=None): self.fcstTime = int(fcstTime) if fcstTime is not None else 0 self.refTime = refTime if refTime is not None else None if validPeriod is not None and type(validPeriod) is not TimeRange: ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod if validPeriod is not None else None self.utilityFlags = EnumSet( 'com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int( calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: self.refTime = int(refTime) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int( self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED") def getRefTime(self): return self.refTime def setRefTime(self, refTime): self.refTime = refTime def getFcstTime(self): return self.fcstTime def setFcstTime(self, fcstTime): self.fcstTime = fcstTime def getValidPeriod(self): return self.validPeriod def setValidPeriod(self, validPeriod): self.validPeriod = validPeriod def getUtilityFlags(self): return self.utilityFlags def setUtilityFlags(self, utilityFlags): self.utilityFlags = utilityFlags def getLevelValue(self): return self.levelValue def setLevelValue(self, levelValue): self.levelValue = numpy.float64(levelValue) def __str__(self): buffer = StringIO() if self.refTime is not None: refTimeInSecs = self.refTime.getTime() / 1000 micros = (self.refTime.getTime() % 1000) * 1000 dtObj = datetime.datetime.utcfromtimestamp(refTimeInSecs) dtObj = dtObj.replace(microsecond=micros) buffer.write(dtObj.isoformat(' ')) if "FCST_USED" in self.utilityFlags: hrs = int(self.fcstTime / 3600) mins = int((self.fcstTime - (hrs * 3600)) / 60) buffer.write(" (" + str(hrs)) if mins != 0: buffer.write(":" + str(mins)) buffer.write(")") if "PERIOD_USED" in self.utilityFlags: buffer.write("[") buffer.write(self.validPeriod.start.isoformat(' ')) buffer.write("--") buffer.write(self.validPeriod.end.isoformat(' ')) buffer.write("]") strVal = buffer.getvalue() buffer.close() return strVal def __repr__(self): return "<DataTime instance: " + str(self) + " >" def __hash__(self): hashCode = hash(self.refTime) ^ hash(self.fcstTime) if self.validPeriod is not None and self.validPeriod.isValid(): hashCode ^= hash(self.validPeriod.getStart()) hashCode ^= hash(self.validPeriod.getEnd()) hashCode ^= hash(self.levelValue) return hashCode def __eq__(self, other): if type(self) != type(other): return False if other.getRefTime() is None: return self.fcstTime == other.fcstTime dataTime1 = (self.refTime, self.fcstTime, self.validPeriod, self.levelValue) dataTime2 = (other.refTime, other.fcstTime, other.validPeriod, other.levelValue) return dataTime1 == dataTime2 def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime < otherValidTime: return True if self.fcstTime < other.fcstTime: return True if self.levelValue < other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() < otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis( ) < otherValidPeriod.getStartInMillis() return False def __le__(self, other): if type(self) != type(other): return NotImplemented return self.__lt__(other) or self.__eq__(other) def __gt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime > otherValidTime: return True if self.fcstTime > other.fcstTime: return True if self.levelValue > other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() > otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis( ) > otherValidPeriod.getStartInMillis() return False def __ge__(self, other): if type(self) != type(other): return NotImplemented return self.__gt__(other) or self.__eq__(other)
class DataTime(object): def __init__(self, refTime=None, fcstTime=None, validPeriod=None): self.fcstTime = int(fcstTime) if fcstTime is not None else 0 self.refTime = refTime if refTime is not None else None if validPeriod is not None and type(validPeriod) is not TimeRange: ValueError("Invalid validPeriod object specified for DataTime.") self.validPeriod = validPeriod if validPeriod is not None else None self.utilityFlags = EnumSet('com.raytheon.uf.common.time.DataTime$FLAG') self.levelValue = numpy.float64(-1.0) if self.refTime is not None: if isinstance(self.refTime, datetime.datetime): self.refTime = int(calendar.timegm(self.refTime.utctimetuple()) * 1000) elif isinstance(self.refTime, time.struct_time): self.refTime = int(calendar.timegm(self.refTime) * 1000) elif hasattr(self.refTime, 'getTime'): # getTime should be returning ms, there is no way to check this # This is expected for java Date self.refTime = int(self.refTime.getTime()) else: self.refTime = int(refTime) self.refTime = Date(self.refTime) if self.validPeriod is None: validTimeMillis = self.refTime.getTime() + int(self.fcstTime * 1000) self.validPeriod = TimeRange() self.validPeriod.setStart(validTimeMillis / 1000) self.validPeriod.setEnd(validTimeMillis / 1000) # figure out utility flags if fcstTime: self.utilityFlags.add("FCST_USED") if self.validPeriod and self.validPeriod.isValid(): self.utilityFlags.add("PERIOD_USED") def getRefTime(self): return self.refTime def setRefTime(self, refTime): self.refTime = refTime def getFcstTime(self): return self.fcstTime def setFcstTime(self, fcstTime): self.fcstTime = fcstTime def getValidPeriod(self): return self.validPeriod def setValidPeriod(self, validPeriod): self.validPeriod = validPeriod def getUtilityFlags(self): return self.utilityFlags def setUtilityFlags(self, utilityFlags): self.utilityFlags = utilityFlags def getLevelValue(self): return self.levelValue def setLevelValue(self, levelValue): self.levelValue = numpy.float64(levelValue) def __str__(self): buffer = StringIO() if self.refTime is not None: refTimeInSecs = self.refTime.getTime() / 1000 micros = (self.refTime.getTime() % 1000) * 1000 dtObj = datetime.datetime.utcfromtimestamp(refTimeInSecs) dtObj = dtObj.replace(microsecond=micros) buffer.write(dtObj.isoformat(' ')) if "FCST_USED" in self.utilityFlags: hrs = int(self.fcstTime / 3600) mins = int((self.fcstTime - (hrs * 3600)) / 60) buffer.write(" (" + str(hrs)) if mins != 0: buffer.write(":" + str(mins)) buffer.write(")") if "PERIOD_USED" in self.utilityFlags: buffer.write("[") buffer.write(self.validPeriod.start.isoformat(' ')) buffer.write("--") buffer.write(self.validPeriod.end.isoformat(' ')) buffer.write("]") strVal = buffer.getvalue() buffer.close() return strVal def __repr__(self): return "<DataTime instance: " + str(self) + " >" def __hash__(self): hashCode = hash(self.refTime) ^ hash(self.fcstTime) if self.validPeriod is not None and self.validPeriod.isValid(): hashCode ^= hash(self.validPeriod.getStart()) hashCode ^= hash(self.validPeriod.getEnd()) hashCode ^= hash(self.levelValue) return hashCode def __eq__(self, other): if type(self) != type(other): return False if other.getRefTime() is None: return self.fcstTime == other.fcstTime dataTime1 = (self.refTime, self.fcstTime, self.validPeriod, self.levelValue) dataTime2 = (other.refTime, other.fcstTime, other.validPeriod, other.levelValue) return dataTime1 == dataTime2 def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime < otherValidTime: return True if self.fcstTime < other.fcstTime: return True if self.levelValue < other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() < otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() < otherValidPeriod.getStartInMillis() return False def __le__(self, other): if type(self) != type(other): return NotImplemented return self.__lt__(other) or self.__eq__(other) def __gt__(self, other): if type(self) != type(other): return NotImplemented myValidTime = self.getRefTime().getTime() + self.getFcstTime() otherValidTime = other.getRefTime().getTime() + other.getFcstTime() if myValidTime > otherValidTime: return True if self.fcstTime > other.fcstTime: return True if self.levelValue > other.levelValue: return True myValidPeriod = self.validPeriod otherValidPeriod = other.validPeriod if myValidPeriod != otherValidPeriod: if myValidPeriod.duration() > otherValidPeriod.duration(): return True return myValidPeriod.getStartInMillis() > otherValidPeriod.getStartInMillis() return False def __ge__(self, other): if type(self) != type(other): return NotImplemented return self.__gt__(other) or self.__eq__(other)