def toreg(irts,tw=None,ti='1hour'): """ toreg(irts,tw=None,ti='1hour'): Converts irregular time series to regular time series with a time window and time interval. """ ti = vutils.timeinterval(ti) if isinstance(irts,vutils.DataReference): irts = irts.getData() # initialize time window accurately if tw == None: tw = irts.getTimeWindow() else: tw = vutils.timewindow(tw) st = vutils.time(tw.getStartTime().ceiling(ti)) et = vutils.time(tw.getEndTime().floor(ti)) #print 'Start time: ',st #print 'End time: ',et nvals = st.getNumberOfIntervalsTo(et,ti)+1 path = irts.getName() parts = string.split(path,'/') if len(parts) == 8: parts[6] = parts[6]+'-REG' name = string.join(parts,'/') #print name yvals = jarray.zeros(nvals,'d') flags = jarray.zeros(nvals,'i') index=0 iterator = irts.getIterator() last_val = vutils.Constants.MISSING_VALUE last_flag = 0 # get first time value in irregular time series next_time = vutils.time(long(iterator.getElement().getX())) # get starting time of regular time series time_val = vutils.time(st) # loop to fill values while index < nvals: #print index,time_val #print next_time,last_val,last_flag # if time value of rts is >= irts then update values if not iterator.atEnd() and time_val.compare(next_time) >= 0: last_val = iterator.getElement().getY() last_flag = iterator.getElement().getFlag() # advance by one & update next time value iterator.advance() if not iterator.atEnd(): next_time = vutils.time(long(iterator.getElement().getX())) yvals[index] = last_val flags[index] = last_flag time_val.incrementBy(ti) index=index+1 attr = irts.getAttributes().createClone() attr.setType(vutils.DataType.REGULAR_TIME_SERIES) rts = vutils.RegularTimeSeries(name,str(st),str(ti),yvals,flags,attr) return rts
def toreg(irts, tw=None, ti='1hour'): """ toreg(irts,tw=None,ti='1hour'): Converts irregular time series to regular time series with a time window and time interval. """ ti = vutils.timeinterval(ti) if isinstance(irts, vutils.DataReference): irts = irts.getData() # initialize time window accurately if tw == None: tw = irts.getTimeWindow() else: tw = vutils.timewindow(tw) st = vutils.time(tw.getStartTime().ceiling(ti)) et = vutils.time(tw.getEndTime().floor(ti)) #print 'Start time: ',st #print 'End time: ',et nvals = st.getNumberOfIntervalsTo(et, ti) + 1 path = irts.getName() parts = string.split(path, '/') if len(parts) == 8: parts[6] = parts[6] + '-REG' name = string.join(parts, '/') #print name yvals = jarray.zeros(nvals, 'd') flags = jarray.zeros(nvals, 'i') index = 0 iterator = irts.getIterator() last_val = vutils.Constants.MISSING_VALUE last_flag = 0 # get first time value in irregular time series next_time = vutils.time(long(iterator.getElement().getX())) # get starting time of regular time series time_val = vutils.time(st) # loop to fill values while index < nvals: #print index,time_val #print next_time,last_val,last_flag # if time value of rts is >= irts then update values if not iterator.atEnd() and time_val.compare(next_time) >= 0: last_val = iterator.getElement().getY() last_flag = iterator.getElement().getFlag() # advance by one & update next time value iterator.advance() if not iterator.atEnd(): next_time = vutils.time(long(iterator.getElement().getX())) yvals[index] = last_val flags[index] = last_flag time_val.incrementBy(ti) index = index + 1 attr = irts.getAttributes().createClone() attr.setType(vutils.DataType.REGULAR_TIME_SERIES) rts = vutils.RegularTimeSeries(name, str(st), str(ti), yvals, flags, attr) return rts
def daysPerMonthToITS(dxc, value, allThirty): """ Computational routine converts a monthly time series of days per month to an irregular time series of opening and closing operations. Converts Delta Cross Channel gate operation from a CALSIM file containing fraction of days open Args: dxc input time series with days per month value the value the output time series should take when 'on' allThirty set true if all months are assumed 30 day length. """ from vutils import timewindow, timeinterval, time from vista.time import TimeWindow import vista.time.Time from vista.set import IrregularTimeSeries import config debug = 0 daysinmonth = { 'jan': 31, 'feb': 28, 'mar': 31, 'apr': 30, 'may': 31, 'jun': 30, 'jul': 31, 'aug': 31, 'sep': 30, 'oct': 31, 'nov': 30, 'dec': 31 } x = [] y = [] lastopen = 0 # indicator that gate ended previous month open first = 1 i = 0 maxYVal = 0 ti = timeinterval("1MON") # to subtract 1 month from CALSIM DSS time stamp for el in dxc: if el: xval = el.getX() xstr = time(long(xval)).toString() xdate = time(long(xval) - 1) xdate = xdate.floor(ti) if debug: print "Processing date %s" % time(long(xval)) yval = el.getY() maxYVal = max(maxYVal, yval) month = string.lower(xstr[2:5]) year = int(string.lower(xstr[5:9])) # for year we need int val if allThirty and yval > 30: raise "Argument allThirty appears to be wrong, element found with value>30" ndays_month = daysinmonth[month] if allThirty: ndays_month = 30 if month == 'feb' and year % 4 == 0: ndays_month = 29 if debug: print "processesing date: %s %s / %s" % (xdate, yval, ndays_month) if yval == 0.0: if lastopen or first: # must close the gate x.append(time(xdate.getTimeInMinutes())) y.append(0.0) lastopen = 0 else: if (not lastopen) or first: # closed, must open it if debug: print "opening on: %s ndays=%s days/mo=%s" % ( xdate, yval, ndays_month) x.append(time(xdate.getTimeInMinutes())) y.append(value) if (ndays_month - yval) > 1E-5: # some days open in month ti_sub = timeinterval('%sday' % int(yval)) xdate2 = time(xdate.getTimeInMinutes()) xdate2 = xdate2 + ti_sub x.append(xdate2) y.append(0.0) lastopen = 0 else: lastopen = 1 first = 0 import jarray xarr = jarray.array(x, vista.time.Time) yarr = jarray.array(y, 'd') if debug: print x if (maxYVal == 30 and not allThirty == 1): print "Maximum open days in all months was 30. Argument allThirty=0 is probably wrong" dxcITS = IrregularTimeSeries("/dxc/pos/", xarr, yarr) return dxcITS
def daysPerMonthToITS(dxc,value,allThirty): """ Computational routine converts a monthly time series of days per month to an irregular time series of opening and closing operations. Converts Delta Cross Channel gate operation from a CALSIM file containing fraction of days open Args: dxc input time series with days per month value the value the output time series should take when 'on' allThirty set true if all months are assumed 30 day length. """ from vutils import timewindow, timeinterval, time from vista.time import TimeWindow import vista.time.Time from vista.set import IrregularTimeSeries import config debug=0 daysinmonth = {'jan': 31, 'feb': 28, 'mar': 31, 'apr': 30, 'may': 31, 'jun': 30, 'jul': 31, 'aug': 31, 'sep': 30, 'oct': 31, 'nov': 30, 'dec': 31} x=[] y=[] lastopen=0 # indicator that gate ended previous month open first=1 i=0 maxYVal=0 ti=timeinterval("1MON") # to subtract 1 month from CALSIM DSS time stamp for el in dxc: if el: xval = el.getX() xstr=time(long(xval)).toString() xdate=time(long(xval)-1) xdate=xdate.floor(ti) if debug: print "Processing date %s" % time(long(xval)) yval = el.getY() maxYVal=max(maxYVal,yval) month = string.lower(xstr[2:5]) year = int(string.lower(xstr[5:9])) # for year we need int val if allThirty and yval > 30: raise "Argument allThirty appears to be wrong, element found with value>30" ndays_month=daysinmonth[month] if allThirty: ndays_month=30 if month=='feb' and year%4 == 0: ndays_month = 29 if debug: print "processesing date: %s %s / %s" % (xdate,yval,ndays_month) if yval == 0.0: if lastopen or first: # must close the gate x.append(time(xdate.getTimeInMinutes())) y.append(0.0) lastopen=0 else: if (not lastopen) or first: # closed, must open it if debug: print "opening on: %s ndays=%s days/mo=%s" % (xdate,yval,ndays_month) x.append(time(xdate.getTimeInMinutes())) y.append(value) if (ndays_month-yval)>1E-5: # some days open in month ti_sub = timeinterval('%sday' % int(yval)) xdate2=time(xdate.getTimeInMinutes()) xdate2=xdate2+ti_sub x.append(xdate2) y.append(0.0) lastopen=0 else: lastopen=1 first=0 import jarray xarr=jarray.array(x,vista.time.Time) yarr=jarray.array(y,'d') if debug: print x if (maxYVal==30 and not allThirty==1): print "Maximum open days in all months was 30. Argument allThirty=0 is probably wrong" dxcITS=IrregularTimeSeries("/dxc/pos/",xarr,yarr) return dxcITS