def testFromStrRefTimeOnlyZeroMillis(self): s = '2016-08-02 01:23:45.0' # result of str() will always drop trailing .0 milliseconds expected = '2016-08-02 01:23:45' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testDataTimeReconstructItselfFromString(self): times = [ '2016-08-02 01:23:45', '2016-08-02 01:23:45.0', '2016-08-02 01:23:45.1', '2016-08-02 01:23:45.123000', '2016-08-02 01:23:45 (17)', '2016-08-02 01:23:45.0 (17)', '2016-08-02 01:23:45.1 (17)', '2016-08-02 01:23:45 (17:34)', '2016-08-02 01:23:45.0 (17:34)', '2016-08-02 01:23:45.1 (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.0[2016-08-02_02:34:45.123--2016-08-02_03:45:56.456]', '2016-08-02 01:23:45.456_(17:34)[2016-08-02_02:34:45.0--2016-08-02_03:45:56.0]' ] for time in times: self.assertEqual(DataTime(time), DataTime(str(DataTime(time))), time)
def __sanitizeInputs(modelName, weatherElements, levels, samplePoint, refTime, timeRange): locationNames = [str(modelName)] parameters = __buildStringList(weatherElements) levels = __buildStringList(levels) envelope = Point(samplePoint) if refTime is not None: refTime = DataTime(refTime=DateTimeConverter.convertToDateTime(refTime)) if timeRange is not None: timeRange = DateTimeConverter.constructTimeRange(*timeRange) return (locationNames, parameters, levels, envelope, refTime, timeRange)
def __determineForecastHours(request, refTime, timeRange): dataTimes = DataAccessLayer.getAvailableTimes(request, False) timesGen = [(DataTime(refTime=dataTime.getRefTime()), dataTime) for dataTime in dataTimes] dataTimesMap = defaultdict(list) for baseTime, dataTime in timesGen: dataTimesMap[baseTime].append(dataTime) if refTime is None: refTime = max(dataTimesMap.keys()) forecastHours = dataTimesMap[refTime] if timeRange is None: return forecastHours else: return [forecastHour for forecastHour in forecastHours if timeRange.contains(forecastHour.getValidPeriod())]
def _adjustValueType(self, value): """ Try to take part of a constraint value, encoded as a string, and return it as its 'true type'. _adjustValueType('3.0') -> 3.0 _adjustValueType('3') -> 3.0 _adjustValueType('a string') -> 'a string' """ try: return float(value) except ValueError: pass try: return DataTime(value) except ValueError: pass return value
def getStationData(self): """ Sends ThriftClient request and writes out received files.""" dtime = datetime.strptime(self.refTime, "%y%m%d/%H%M") trange = TimeRange() trange.setStart(dtime) trange.setEnd(dtime) dataTime = DataTime(refTime=dtime, validPeriod=trange) req = StationDataRequest() req.setPluginName(self.pluginName) req.setStationId(self.stationId) req.setRefTime(dataTime) req.setParmList(self.parmList) req.setPartNumber(self.partNumber) resp = self.client.sendRequest(req) for i, rec in enumerate(resp): resp[i] = { key.decode() if isinstance(key, bytes) else key: val.decode() if isinstance(val, bytes) else val for key, val in rec.items() } return resp
def __init__(self, parmId=None, timeRange=None): self.gridHistory = [] self.dataURI = None self.pluginName = "gfe" self.insertTime = None self.messageData = None self.identifier = None self.dataTime = None self.parmId = None if timeRange is not None: if type(timeRange) is TimeRange: self.dataTime = DataTime(refTime=timeRange.getStart(), validPeriod=timeRange) else: raise TypeError("Invalid TimeRange object specified.") if parmId is not None: if type(parmId) is ParmID.ParmID: self.parmId = parmId self.parmName = parmId.getParmName() self.parmLevel = parmId.getParmLevel() self.dbId = parmId.getDbId() else: raise TypeError("Invalid ParmID object specified. Type:" + str(type(parmId)))
def getDataTime(self, dataURI): dataTimeStr = dataURI.split('/')[2] return DataTime(dataTimeStr)
def testFromStrWithEverything(self): s = '2016-08-02 01:23:45.0_(17:34)[2016-08-02 02:34:45.0--2016-08-02 03:45:56.0]' expected = '2016-08-02 01:23:45 (17:34)[2016-08-02 02:34:45--2016-08-02 03:45:56]' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrWithPeriodZeroMillis(self): s = '2016-08-02 01:23:45.0[2016-08-02 02:34:45.0--2016-08-02 03:45:56.0]' expected = '2016-08-02 01:23:45[2016-08-02 02:34:45--2016-08-02 03:45:56]' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrWithFcstTimeHrMinZeroMillis(self): s = '2016-08-02 01:23:45.0 (17:34)' expected = '2016-08-02 01:23:45 (17:34)' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrWithFcstTimeHrAndMillis(self): s = '2016-08-02 01:23:45.1 (17)' expected = '2016-08-02 01:23:45.001000 (17)' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrWithFcstTimeHr(self): s = '2016-08-02 01:23:45 (17)' expected = s self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrRefTimeOnlyWithMillis(self): s = '2016-08-02 01:23:45.1' expected = '2016-08-02 01:23:45.001000' self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def testFromStrRefTimeOnly(self): s = '2016-08-02 01:23:45' expected = s self.assertEqual(expected, str(DataTime(s))) s = s.replace(' ', '_') self.assertEqual(expected, str(DataTime(s)))
def do_request(user_args): if user_args.host: DataAccessLayer.changeEDEXHost(user_args.host) srcId = user_args.srcId varAbrev = user_args.varAbrev if not srcId or not varAbrev: raise Exception("srcId or varAbrev not provided") return date = user_args.date hour = user_args.hour fcst = user_args.fcst if not date or not hour or not fcst: raise Exception("date, hour, or fcst not provided") return dt = datetime.strptime( str(date) + " " + str(hour) + ":00:00.0", "%Y-%m-%d %H:%M:%S.%f") # check for and build date range if necessary daterange = None if varAbrev.endswith("hr"): import re matches = re.findall(r'\d+', varAbrev) if matches: from datetime import timedelta hourRange = int(matches[-1]) endDate = dt + timedelta(hours=int(fcst)) beginDate = endDate - timedelta(hours=hourRange) daterange = TimeRange(beginDate, endDate) # convert hours to seconds because DataTime does the reverse time = DataTime(dt, int(fcst) * 3600, daterange) req = DataAccessLayer.newDataRequest("grid") req.setParameters(varAbrev) req.addIdentifier("info.datasetId", srcId) # To handle partial level matches, use identifiers instead of a Level. if user_args.lvlName is not None: req.addIdentifier("info.level.masterLevel.name", user_args.lvlName) if user_args.lvlOne is not None: req.addIdentifier("info.level.levelonevalue", numpy.float64(user_args.lvlOne)) if user_args.lvlTwo is not None: req.addIdentifier("info.level.leveltwovalue", numpy.float64(user_args.lvlTwo)) times = [time] # If fcst is 0, also query for times with FCST_USED flag if fcst == '0': time = DataTime(dt, int(fcst) * 3600, daterange) time.utilityFlags.add("FCST_USED") times.append(time) grids = DataAccessLayer.getGridData(req, times) if not grids: # print "Data not available" raise Exception("") grid = grids[0] rawData = grid.getRawData() yLen, xLen = rawData.shape return grid, xLen, yLen