📄 stripchart2.py
字号:
def doXAxis(self, x0, x1, y0, y1, tag):
intlist, label = self.XAxisData[self.displayItems]
ltag = translate_spaces(tag,'_')
self.canvas.delete(ltag) # Remove previous scales
incr = self.axisWidth / (len(intlist)-1)
xt = x0
for tick in intlist:
if xt > x0:
self.canvas.create_line(xt,y1,xt,y1-4,
fill=self.Axes, width=2,
tags=ltag)
self.canvas.create_line(xt,y0,xt,y1-4,
fill=self.CrossHairsV,
width=1, tags=ltag)
self.canvas.create_text(xt,y1+5, text=tick,
font=('Verdana', 8),
fill=self.Axes, anchor='n',
tags=ltag)
xt = xt + incr
self.canvas.create_text(xt-(incr*2)+(incr/2),y1+2,
text=label, font=('Verdana', 12),
fill=self.Titles, anchor='n',
tags=ltag)
def doThresholds(self):
for xf, yf, af, label, low, high, dataType, datalist, \
yAxisLabel, ticklist in self.cells:
for data in datalist:
tag = '%sThreshold' % data
self.canvas.delete(tag) # Remove previous data...
threshold, action = getattr(self, '%sThreshold'%data)
if not threshold == BLANK_VALUE:
x0=(self.cellWidth*xf)+self.xindent
x1=(self.cellWidth*xf)+self.xindent+self.axisWidth
y0=(self.cellHeight*yf)+self.yindent+self.titlespace+1
xa1 = x0 + (x1-x0)/10
xa2 = x0 + (x1-x0)/2
xa3 = x1 - (x1-x0)/10
if low <= 0:
spread=abs(low)+abs(high)
else:
spread = high-low
negcomp=self.calculate_negative_component(low, high)
factor = -2
if action in [WARNOVER, ALARMOVER]:
factor = 2
yt=((float(spread-(threshold+negcomp))/float(spread))*\
(self.axisHeight*af))+y0-1
self.canvas.create_line(x0,yt,x1,yt, fill=self.Threshold,
width=2, tags=tag)
self.canvas.create_line(x0,yt+factor,x1,yt+factor,
fill=self.Warning, width=2, tags=tag)
for x in [xa1, xa2, xa3]:
self.canvas.create_line(x,yt+factor, x,yt+(factor*10),
fill=self.Warning, width=2,
tags=tag, arrow='first',
arrowshape=(8,8,3))
self.thresholdChanged = FALSE
def doPlot(self):
# Check for threshold value....
if self.thresholdChanged:
self.doThresholds()
if self.gotData:
self.gotData = FALSE
self.threadRunning = FALSE
else:
if self.threadRunning:
self.frame.after(1000, self.doPlot) # Spin
return
else:
self.threadRunning = TRUE
self.gotData = FALSE
thread.start_new(self.getMETAR, (noaa_url,metar_dir,
self.wStation))
self.frame.after(1000, self.doPlot) # Spin
return
if self.report:
self.decodeMETAR(self.report)
self.pollTime = time.time()
for xf, yf, af, label, low, high, dataType, datalist, \
yAxisLabel, ticklist in self.cells:
x0=(self.cellWidth*xf)+self.xindent
x1=(self.cellWidth*xf)+self.xindent+self.axisWidth
y0=(self.cellHeight*yf)+self.yindent+self.titlespace+1
y1=(self.cellHeight*yf)+self.yindent+self.titlespace+\
(self.axisHeight*af)+1
if low <= 0:
spread=abs(low)+abs(high)
else:
spread = high-low
negcomp=self.calculate_negative_component(low, high)
for data in datalist:
dataPoint = getattr(self, '%s' % data)
if dataPoint or dataPoint == 0.0:
if self.pollTime > self.maxSpan:
self.canvas.delete(self.plottedData) # Remove previous
self.baseTime = self.pollTime
self.setDisplayRange(self.displayItems)
xfactor = float(self.axisWidth) / (self.maxSpan - \
self.baseTime)
xv = self.pollTime - self.baseTime
if not dataPoint == float(BLANK_VALUE):
centerx = x0 + ((xv * xfactor)-1) +2
centery=((float(spread-(dataPoint+negcomp))/\
float(spread))*(self.axisHeight*af))+y0-1
lastX, lastY = getattr(self, 'last%s' % data)
if not lastY == -999.0:
if not lastX >= centerx:
self.canvas.create_line(lastX,lastY,centerx,
centery, width=2,
fill=self.Line,
tags=self.plottedData)
pstr = 'self.last%s = (%d,%d)' % (data,centerx,centery)
else:
pstr = 'self.last%s = (-999.0,-999.0)' % data
#### Now, check for thresholds
if self.monitor: # have to be present to win...
threshold, action = eval('self.%sThreshold' % data)
if not threshold == BLANK_VALUE:
if dataPoint == float(BLANK_VALUE):
self.monitor.set(data, OFF_STATUS, 0)
else:
takeAction = FALSE
if action in [WARNUNDER, ALARMUNDER]:
if dataPoint <= threshold:
takeAction = TRUE
else:
if dataPoint >= threshold:
takeAction = TRUE
if takeAction:
if action in [WARNUNDER, WARNOVER]:
self.monitor.set(data,WARNING_STATUS,0)
else:
self.monitor.set(data,ALARM_STATUS,1)
self.monitor.frame.bell()
else:
self.monitor.set(data, ON_STATUS, 0)
else:
pstr = 'self.last%s = (-999.0,-999.0)' % data
exec(pstr)
drawinterval = 1000*self.refreshRate
self.frame.after(drawinterval, self.doPlot)
def setDisplayRange(self, display):
doSetup = FALSE
if not self.displayItems == display:
doSetup = TRUE
self.displayItems = display
[value, units] = string.splitfields(display, ' ')
mult = 1
if units[:1] == 'M':
mult = 60
if units[:1] == 'H':
mult = 3600
self.maxSpan = self.baseTime + (string.atoi(value) * mult)
self.canvas.delete(self.plottedData) # Remove previous data...
if doSetup:
self.setup_plot(restart=0)
def setDisplayRate(self, rate):
newRate = 10
if not rate == 'Default':
[value, units] = string.splitfields(rate, ' ')
mult = 1
if units[:1] == 'M':
mult = 60
newRate = string.atoi(value) * mult
self.set_refreshRate(newRate)
self.baseTime = time.time()
self.setDisplayRange(self.displayItems)
def setStation(self, station):
self.wStation = station[:4]
self.master.title("Weather Monitor (%s)" % self.wStation)
def set_hardware_data(self, vendor):
SSMIHW.set_hardware_data(vendor)
def calculate_negative_component(self, low, high):
if low < 0:
negcomp = abs(low)
elif low > 0:
negcomp = -low
else:
negcomp = low
return negcomp
def set_refreshRate(self, rate):
self.refreshRate = rate
def setTValues(self):
self.tw = SetThresholdsScreen()
self.tw.setParent(self)
self.tw.setCurrentValues()
self.tw.run()
def getMETAR(self, url=noaa_url, directory=metar_dir,
station='KPVD'):
# UNCOMMENT the next line to inject test data (saves the bandwidth!)
## return self.getTestData()
tmp = '%s/wtemp' % temp_dir
if os.path.exists(tmp) == 1:
os.remove(tmp)
data = open(tmp,'w')
try:
ftp = FTP(url)
ftp.login()
ftp.cwd(directory)
ftp.retrbinary('RETR %s.TXT' % station, data.write)
ftp.quit
except:
print 'FTP failed...'
data.close()
report = open(tmp, 'r')
self.report = report.read()
self.gotData = TRUE
def decodeMETAR(self, report):
self.Temp = None
self.Press = None
self.WSpeed = None
self.Humidity = None
self.Visibility = None
self.WDir = None
self.CLow = None
self.CHigh = None
station = dtime = wind = visib = runway = weather = temp = alt = '--'
lines = string.split(report, '\n')
data = string.split(lines[1], ' ')
station = data[0]
dtime = data[1]
if not data[2][0] in 'CSA': # COR, AUTO, SPEC
wind = data[2]
next = 3
else:
wind = data[3]
next = 4
if data[next][-2:] == 'SM':
visib = data[next]
else:
next = next + 1 #SKIP
visib = data[next]
next = next + 1
if data[next][0] == 'R':
runway = data[next]
next = next + 1
if data[next][:2] in ['MI','BC','PR','TS','BL','SH','DR','FZ','DZ',
'IC','UP','RA','PE','SN','GR','SG','GS','BR',
'SA','FG','HZ','FU','PY','VA','DU','SQ','FC',
'SS','DS','PO']:
weather = data[next]
next = next + 1
cloud = []
while 1:
if data[next][:3] in ['SKC','SCT','FEW','BKN','OVC','TCC','CLR']:
cloud.append(data[next])
next = next + 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -