def _removeOldGrids(self, weName): # get the inventory trList = self._getWEInventory(weName) for tr in trList: if tr.endTime().unixTime() < current().unixTime(): self.deleteCmd([weName], tr) return
def _removeOldGrids(self, weName): # get the inventory trList = self._getWEInventory(weName) for tr in trList: if tr.endTime().unixTime() < current().unixTime(): self.deleteCmd([weName], tr) return
def _getWEInventory(self, WEName, timeRange=None, asJava=False): # set up a timeRange if it is None if timeRange is None: now = current() yesterday = now - (24 * 3600) # one day ago later = now + 10 * 24 * 3600 # 10 days from now timeRange = self._makeTimeRange(yesterday.unixTime(), later.unixTime()) parm = self.getParm(MODEL, WEName, LEVEL) trList = [] if parm is not None: if isinstance(timeRange, JavaWrapperClass): timeRange = timeRange.toJavaObj() gridInventory = parm.getGridInventory(timeRange) for g in gridInventory: gridTimeRange = g.getGridTime() tr = gridTimeRange.clone() if not asJava: tr = TimeRange(tr) trList.append(tr) return trList
def _getWEInventory(self, WEName, timeRange=None, asJava=False): # set up a timeRange if it is None if timeRange is None: now = current() yesterday = now - (24 * 3600) # one day ago later = now + 10 * 24 * 3600 # 10 days from now timeRange = self._makeTimeRange(yesterday.unixTime(), later.unixTime()) parm = self.getParm(MODEL, WEName, LEVEL) trList = [] if parm is not None: if isinstance(timeRange, JavaWrapperClass): timeRange = timeRange.toJavaObj() gridInventory = parm.getGridInventory(timeRange) for g in gridInventory: gridTimeRange = g.getGridTime() tr = gridTimeRange.clone() if not asJava: tr = TimeRange(tr) trList.append(tr) return trList
def _setupHazardsInventory(self, weName, trList): # see if the element exists yet, if not, make a new grid # This is a painful way just to see if the grid exists # but all other techniques fail for temporary weather elements now = current() yesterday = now - (24 * 3600) # one day ago later = now + 10 * 24 * 3600 # 10 days from now timeRange = TimeRange(yesterday, later).toJavaObj() try: gridInfo = self.getGridInfo(MODEL, weName, LEVEL, timeRange) except: # this means the WE does not exist, so make a grid if len(trList) <= 0: return for tr in trList: self._makeEmptyHazardGrid(weName, tr) return # fill any gaps in the inventory gapList = self._getGaps(weName, trList) for g in gapList: self._makeEmptyHazardGrid(weName, g) # Split the grids at the timeRange boundaries unix_now = now.unixTime() for tr in trList: # If tr is a java timerange, convert it to a python TimeRange if not isinstance(tr, TimeRange): tr = TimeRange(tr) end = tr.endTime().unixTime() if end > unix_now: # parm.splitTR() will split timeRanges with non-zero minutes # to the next hour. So, truncate start and end times to the # previous hour and then split start = tr.startTime().unixTime() start = int(start / 3600) * 3600 end = int(end / 3600) * 3600 roundedTR = TimeRange(AbsTime(start), AbsTime(end)).toJavaObj() parm = self.getParm(MODEL, weName, LEVEL) self.splitCmd([weName], roundedTR) return
def _setupHazardsInventory(self, weName, trList): # see if the element exists yet, if not, make a new grid # This is a painful way just to see if the grid exists # but all other techniques fail for temporary weather elements now = current() yesterday = now - (24 * 3600) # one day ago later = now + 10 * 24 * 3600 # 10 days from now timeRange = TimeRange(yesterday, later).toJavaObj() try: gridInfo = self.getGridInfo(MODEL, weName, LEVEL, timeRange) except: # this means the WE does not exist, so make a grid if len(trList) <= 0: return for tr in trList: self._makeEmptyHazardGrid(weName, tr) return # fill any gaps in the inventory gapList = self._getGaps(weName, trList) for g in gapList: self._makeEmptyHazardGrid(weName, g) # Split the grids at the timeRange boundaries unix_now = now.unixTime() for tr in trList: # If tr is a java timerange, convert it to a python TimeRange if not isinstance(tr, TimeRange): tr = TimeRange(tr) end = tr.endTime().unixTime() if end > unix_now: # parm.splitTR() will split timeRanges with non-zero minutes # to the next hour. So, truncate start and end times to the # previous hour and then split start = tr.startTime().unixTime() start = int(start / 3600) * 3600 end = int(end / 3600) * 3600 roundedTR = TimeRange(AbsTime(start), AbsTime(end)).toJavaObj() parm = self.getParm(MODEL, weName, LEVEL) self.splitCmd([weName], roundedTR) return
def _lockHazards(self): "Flag the hazards parm as being edited. Return the hazards parm and its grid." hazParm = self.getParm(MODEL, ELEMENT, LEVEL) startAbsTime = AbsTime(int(current().unixTime() /3600)*3600) endAbsTime = startAbsTime + LOCK_HOURS() * HOUR_SECONDS() timeRange = TimeRange(startAbsTime, endAbsTime) inventory = self._getWEInventory(ELEMENT, timeRange, asJava=True) startTimes = jep.jarray(len(inventory), Date) for trNum in range(len(inventory)): startTimes[trNum] = inventory[trNum].getStart() gridData = None try: # startParmEdit() refreshes the grids and sets up the times that endParmEdit() will lock. gridData = hazParm.startParmEdit(startTimes) except RuntimeError, runtimeErr: if runtimeErr.message is None: raise if runtimeErr.message.startswith("com.raytheon.viz.gfe.GFEOperationFailedException:"): self.statusBarMsg("There are conflicting locks. " + \ "Please resolve these before adding any hazards", "S") hazParm = None else: raise
def _lockHazards(self): "Flag the hazards parm as being edited. Return the hazards parm and its grid." hazParm = self.getParm(MODEL, ELEMENT, LEVEL) startAbsTime = AbsTime(int(current().unixTime() /3600)*3600) endAbsTime = startAbsTime + LOCK_HOURS() * HOUR_SECONDS() timeRange = TimeRange(startAbsTime, endAbsTime) inventory = self._getWEInventory(ELEMENT, timeRange, asJava=True) startTimes = jep.jarray(len(inventory), Date) for trNum in range(len(inventory)): startTimes[trNum] = inventory[trNum].getStart() gridData = None try: # startParmEdit() refreshes the grids and sets up the times that endParmEdit() will lock. gridData = hazParm.startParmEdit(startTimes) except RuntimeError, runtimeErr: if runtimeErr.message is None: raise if runtimeErr.message.startswith("com.raytheon.viz.gfe.GFEOperationFailedException:"): self.statusBarMsg("There are conflicting locks. " + \ "Please resolve these before adding any hazards", "S") hazParm = None else: raise
def _addHazard(self, weName, timeRange, addHaz, mask, combine=1): # Python TimeRanges are easy to compare. # Java methods require Java TimeRanges. # Make sure we have one of each. if isinstance(timeRange, JavaWrapperClass): pyTimeRange = timeRange timeRange = timeRange.toJavaObj() else: pyTimeRange = TimeRange(timeRange) # refuse to make new grids that are more than one hour in the past if pyTimeRange.endTime().unixTime() < current().unixTime() - HOUR_SECONDS(): msg = "skipped time range creation: %s < %s" % (pyTimeRange.endTime().string(), current().string()) return # set up the inventory first self._setupHazardsInventory(weName, [timeRange]) # get the inventory trList = self._getWEInventory(weName, timeRange, asJava=True) # coerce mask into a boolean array if it isn't already if not (isinstance(mask, numpy.ndarray) and mask.dtype==numpy.bool): mask = numpy.array(mask, dtype=numpy.bool) for tr in trList: # get the grid of index values and list of keys those indices select byteGrid, hazKey = self.getGrids(MODEL, weName, LEVEL, tr, mode="First", cache=0) if isinstance(hazKey, str): hazKey = eval(hazKey) # Eliminate keys that aren't in the grid from the list. uniqueKeys = self._getUniqueKeys(byteGrid, hazKey, mask) for uKey in uniqueKeys: # Figure out what the new key is if combine: newKey = self._makeNewKey(uKey, addHaz) else: #replace newKey = addHaz # Find the index number for the old key oldIndex = self.getIndex(uKey, hazKey) # Find the index number for the new key (newKey is added if not in hazKey) newIndex = self.getIndex(newKey, hazKey) # calculate the mask - intersection of mask and oldIndex values editMask = (byteGrid==oldIndex) & mask # poke in the new values byteGrid[editMask] = newIndex # Save the updated byteGrid and hazKey if weName == ELEMENT: self.createGrid(MODEL, ELEMENT, "DISCRETE", (byteGrid, hazKey), tr, discreteOverlap=1, discreteAuxDataLength=4) else: # it's a temporary WE - special key hazKey = ["<None>", addHaz] hazKeyDesc = self._addHazardDesc(hazKey) self.createGrid(MODEL, weName, "DISCRETE", (byteGrid, hazKey), tr, discreteOverlap=0, discreteAuxDataLength=4, discreteKeys=hazKeyDesc, defaultColorTable="YesNo") # remove any grids that are completely in the past self._removeOldGrids(weName) return
def testAbsTimeZero(): start = AbsTime(0) end = current() timeRange = TimeRange(start, end) javaTR = timeRange.toJavaObj() return javaTR
def _addHazard(self, weName, timeRange, addHaz, mask, combine=1): # Python TimeRanges are easy to compare. # Java methods require Java TimeRanges. # Make sure we have one of each. if isinstance(timeRange, JavaWrapperClass): pyTimeRange = timeRange timeRange = timeRange.toJavaObj() else: pyTimeRange = TimeRange(timeRange) # refuse to make new grids that are more than one hour in the past if pyTimeRange.endTime().unixTime() < current().unixTime() - HOUR_SECONDS(): msg = "skipped time range creation: %s < %s" % (pyTimeRange.endTime().string(), current().string()) return # set up the inventory first self._setupHazardsInventory(weName, [timeRange]) # get the inventory trList = self._getWEInventory(weName, timeRange, asJava=True) # coerce mask into a boolean array if it isn't already if not (isinstance(mask, numpy.ndarray) and mask.dtype==numpy.bool): mask = numpy.array(mask, dtype=numpy.bool) for tr in trList: # get the grid of index values and list of keys those indices select byteGrid, hazKey = self.getGrids(MODEL, weName, LEVEL, tr, mode="First", cache=0) if isinstance(hazKey, str): hazKey = eval(hazKey) # Eliminate keys that aren't in the grid from the list. uniqueKeys = self._getUniqueKeys(byteGrid, hazKey, mask) for uKey in uniqueKeys: # Figure out what the new key is if combine: newKey = self._makeNewKey(uKey, addHaz) else: #replace newKey = addHaz # Find the index number for the old key oldIndex = self.getIndex(uKey, hazKey) # Find the index number for the new key (newKey is added if not in hazKey) newIndex = self.getIndex(newKey, hazKey) # calculate the mask - intersection of mask and oldIndex values editMask = (byteGrid==oldIndex) & mask # poke in the new values byteGrid[editMask] = newIndex # Save the updated byteGrid and hazKey if weName == ELEMENT: self.createGrid(MODEL, ELEMENT, "DISCRETE", (byteGrid, hazKey), tr, discreteOverlap=1, discreteAuxDataLength=4) else: # it's a temporary WE - special key hazKey = ["<None>", addHaz] hazKeyDesc = self._addHazardDesc(hazKey) self.createGrid(MODEL, weName, "DISCRETE", (byteGrid, hazKey), tr, discreteOverlap=0, discreteAuxDataLength=4, discreteKeys=hazKeyDesc, defaultColorTable="YesNo") # remove any grids that are completely in the past self._removeOldGrids(weName) return
def testAbsTimeZero(): start = AbsTime(0) end = current() timeRange = TimeRange(start, end) javaTR = timeRange.toJavaObj() return javaTR