def writeAtiMelt(selectedList, dList, aList, mList): # print '\n#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#' # print '#Begin Creating ATI and Melt-CUM DSS paths' # print '#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#' for ab, d, a, m in zip(selectedList, dList, aList, mList): aPart = ab.split("/")[1] b = ab.split("/")[2] # print 'aPart: ', aPart # ATI-MELT DSS path Created tsATI = TimeSeriesContainer() tsATI.watershed = aPart tsATI.location = b tsATI.parameter = 'ATI' # F-part will be 'CALC' tsATI.version = 'CALC' # Interval is hard coded as 1Day = 1440 minutes. tsATI.interval = 1440 tsATI.fullName = '/%s/%s/%s//1DAY/%s/' % \ (aPart, b, 'ATI', 'CALC') tsATI.values = a times = [] hecTime = HecTime() for i, v in enumerate(d): hecTime.set(d[i]) times.append(hecTime.value()) tsATI.times = times tsATI.startTime = times[0] tsATI.endTime = times[-1] tsATI.numberValues = len(a) tsATI.units = 'DEGF-DAY' tsATI.type = 'INST-VAL' dssFile.put(tsATI) # MELT-CUM DSS path Created tsMelt = TimeSeriesContainer() tsMelt.watershed = aPart tsMelt.location = b tsMelt.parameter = 'MELT-CUM' tsMelt.version = 'CALC' # Interval and FullName are hard coded as 1Day = 1440 minutes. tsMelt.interval = 1440 tsMelt.fullName = '/%s/%s/%s//1DAY/%s/' % \ (aPart, b, 'MELT-CUM', 'CALC') tsMelt.values = m tsMelt.times = times tsMelt.startTime = times[0] tsMelt.endTime = times[-1] tsMelt.numberValues = len(m) tsMelt.units = 'IN' tsMelt.type = 'INST-VAL' dssFile.put(tsMelt)
def makeTimeSeriesContainer(tsData, timeZone, pathname=None): ''' Construct a TimeSeriesContainer object from a python dictionary that was created from a single "time-series" returned from the CWMS RADAR web service ''' #---------------# # initial setup # #---------------# tsc = None try: tz = TimeZone.getTimeZone(timeZone) sdf8601 = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") sdfHecTime = SimpleDateFormat("ddMMMyyyy, HH:mm") cal = Calendar.getInstance() for obj in sdf8601, sdfHecTime, cal: obj.setTimeZone(tz) ht = HecTime() times, values, qualities = [], [], [] #------------------# # process the data # #------------------# if tsData.has_key("regular-interval-values"): #----------------------------------------# # regular time series (a lot to process) # #----------------------------------------# rts = tsData["regular-interval-values"] intvlStr = rts["interval"] unit = rts["unit"].split()[0] if intvlStr.startswith("PT"): intvlNum, intvlUnit = int(intvlStr[2:-1]), intvlStr[-1] try: factor, field = { "M": (1, Calendar.MINUTE), "H": (60, Calendar.HOUR_OF_DAY) }[intvlUnit] except KeyError: raise Exception("Unexpected interval: %s" % intvlStr) else: intvlNum, intvlUnit = int(intvlStr[1:-1]), intvlStr[-1] try: factor, field = { "Y": (1440 * 365, Calendar.YEAR), "M": (1440 * 30, Calendar.MONTH), "D": (1440, Calendar.DATE) }[intvlUnit] except KeyError: raise Exception("Unexpected interval: %s" % intvlStr) intvl = intvlNum * factor segmentCount = rts["segment-count"] cal.setTimeInMillis( sdf8601.parse(rts["segments"][0]["first-time"]).getTime()) for i in range(segmentCount): for j in range(rts["segments"][i]["value-count"]): ht.set(sdfHecTime.format(cal.getTimeInMillis())) v, q = rts["segments"][i]["values"][j] times.append(ht.value()) values.append(v) qualities.append(q) cal.add(field, intvlNum) if i < segmentCount - 1: nextBegin = sdf8601.parse( rts["segments"][i + 1]["first-time"]).getTime() time = cal.getTimeInMillis() while time < nextBegin: ht.set(sdfHecTime.format(time)) times.append(ht.value()) values.append(Constants.UNDEFINED) qualities.append(0) cal.add(field, intvlNum) time = cal.getTimeInMillis() elif tsData.has_key("irregular-interval-values"): #------------------------------# # irregular time series (easy) # #------------------------------# its = tsData["irregular-interval-values"] unit = its["unit"].split()[0] intvl = 0 for t, v, q in its["values"]: ht.set(sdfHecTime.format(sdf8601.parse(t))) times.append(ht.value()) values.append(v) qualities.append(q) else: raise Exception("Time series has no values") #--------------------------------------------------# # code common to regular and irregular time series # #--------------------------------------------------# tsc = TimeSeriesContainer() tsc.times = times tsc.values = values tsc.quality = qualities tsc.numberValues = len(times) tsc.startTime = times[0] tsc.endTime = times[-1] tsc.interval = intvl tsc.units = unit tsc.timeZoneID = timeZone tsc.timeZoneRawOffset = tz.getRawOffset() name = tsData["name"] loc, param, paramType, intv, dur, ver = name.split(".") if pathname: #---------------------------# # use pathname if specified # #---------------------------# A, B, C, D, E, F = 1, 2, 3, 4, 5, 6 parts = pathname.split("/") parts[D] = '' tsc.fullName = "/".join(parts) tsc.watershed = parts[A] try: tsc.location, tsc.subLocation = parts[B].split("-", 1) except: tsc.location = parts[B] try: tsc.parameter, tsc.subParameter = parts[C].split("-", 1) except: tsc.parameter = parts[C] try: tsc.version, tsc.subVersion = parts[F].split("-", 1) except: tsc.version = parts[F] else: #--------------------------------------# # no pathname, use CWMS time series id # #--------------------------------------# try: tsc.location, tsc.subLocation = loc.split("-", 1) except: tsc.location = loc try: tsc.parameter, tsc.subParameter = param.split("-", 1) except: tsc.parameter = param try: tsc.version, tsc.subVersion = ver.split("-", 1) except: tsc.version = ver tsc.type = { "Total": "PER-CUM", "Max": "PER-MAX", "Min": "PER-MIN", "Const": "INST-VAL", "Ave": "PER-AVER", "Inst": ("INST-VAL", "INST-CUM")[param.startswith("Precip")] }[paramType] except: output(traceback.format_exc()) return tsc
from hec.heclib.dss import HecDss, HecTimeSeries from hec.io import TimeSeriesContainer from hec.heclib.util import HecTime watershed = "Green River" loc = "OAKVILLE" param = "STAGE" ver = "OBS" startTime = "12Oct2003 0100" values = [12.36, 12.37, 12.42, 12.55, 12.51, 12.47, 12.43, 12.39] hecTime = HecTime() tsc = TimeSeriesContainer() tsc.watershed = watershed tsc.location = loc tsc.parameter = param tsc.version = ver tsc.fullName = "/%s/%s/%s//1HOUR/%s/" % (watershed, loc, param, ver) tsc.interval = 60 hecTime.set(startTime) times=[] for value in values: times.append(hecTime.value()) hecTime.add(tsc.interval) tsc.values = values tsc.times = times tsc.startTime = times[0] tsc.endTime = times[-1]
print '#End Calculations' print '#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#' print '\n#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#' print '#Begin Creating ATI and Melt-CUM DSS paths' print '#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#' for b, d, a, m in zip(bList, dList, aList, mList): #locDict contains a-parts with b used as the key aPart = locDict[b] # print aPart # print a #F-part will be 'CALC' #ATI-MELT DSS path Created tsATI = TimeSeriesContainer() tsATI.watershed = aPart tsATI.location = b tsATI.parameter = 'ATI' tsATI.version = 'CALC' tsATI.interval = 1440 tsATI.fullName = '/%s/%s/%s//1DAY/%s/' % \ (aPart, b, 'ATI', 'CALC') tsATI.values = a times = [] hecTime = HecTime() for i, v in enumerate(d): hecTime.set(d[i]) times.append(hecTime.value()) tsATI.times = times tsATI.startTime = times[0]