Skip to content

Instantly share code, notes, and snippets.

@GrantTrebbin
Created May 10, 2015 09:20
Show Gist options
  • Save GrantTrebbin/b8997a5bc512b6fede23 to your computer and use it in GitHub Desktop.
Save GrantTrebbin/b8997a5bc512b6fede23 to your computer and use it in GitHub Desktop.
Create a series of images to demonstrate weather related events using python and matplotlib
#!/usr/bin/python
import csv
import sys
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.image as image
import matplotlib.lines as mlines
import os.path
csv.field_size_limit(10000000)
# read in log file
logFileName = "main_si470x_clean.log"
try:
with open(logFileName) as logfile:
logfilereader = csv.reader(logfile, delimiter=',')
try:
logfilelist = list(logfilereader)
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (logFileName, ' ', logfilereader.line_num, e))
except IOError:
sys.exit('''Problem with ''' + logFileName + ''' - can't read file''')
output = 3493*[0]
xaxisdata = 3493*[0]
for x in range(0,3493):
xaxisdata[x] = mdates.epoch2num((x + 3969981) * 360)
for x in logfilelist:
a = (int(x[0])//360 - 3969981)
output[a] = output[a] + 1
terrain = image.imread('IDR663.png')
imerror = image.imread('error.png')
fig = plt.gcf()
for frame in range(3000,3493):
#frame = 1001
plt.gcf().clear()
l1 = mlines.Line2D([0.839,0.837], [0.802,0.815], color='magenta', linewidth=3, transform=fig.transFigure, figure=fig, zorder=4)
fig.lines.extend([l1])
plt.gcf().canvas.draw()
radarframe = frame
noRadar = False
a= mdates.num2date(xaxisdata[radarframe])
radarfile = a.strftime("%Y\\%m\\%d\\") +'IDR663.T.' + a.strftime("%Y%m%d%H%M") + '.png'
while not os.path.isfile(radarfile):
radarframe = radarframe - 1
noRadar = True
a=mdates.num2date(xaxisdata[radarframe])
radarfile = a.strftime("%Y\\%m\\%d\\") +'IDR663.T.' + a.strftime("%Y%m%d%H%M") + '.png'
imradar = image.imread(radarfile)
plt.figure(1)
plt.gcf().set_size_inches(16,9)
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.gca().spines["bottom"].set_color('grey')
plt.gca().spines["left"].set_color('grey')
plt.gca().tick_params(axis='y', colors='grey')
plt.gca().tick_params(axis='x', colors='grey')
plt.gca().get_xaxis().tick_bottom()
plt.gca().get_yaxis().tick_left()
plt.plot_date(xaxisdata, output, marker="o", color =(31/255., 119/255., 180/255.), markersize=4, markeredgecolor='none', linewidth=0.05, linestyle='-', zorder = 0)
plt.gca().set_title('RDS Messages per 6 Minute Radar Sweep \n', color='grey', fontsize=20)
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
[i.set_color("grey") for i in plt.gca().get_xticklabels()]
[i.set_fontsize(13) for i in plt.gca().get_xticklabels()]
[i.set_color("grey") for i in plt.gca().get_yticklabels()]
[i.set_fontsize(13) for i in plt.gca().get_yticklabels()]
plt.subplots_adjust(left=0.03, right=0.72, top=0.9, bottom =0.12)
plt.axvspan(xaxisdata[0], xaxisdata[frame], facecolor='grey', alpha=0.07)
plt.gcf().figimage(terrain, 1408, 568, zorder = 1)
plt.gcf().figimage(imradar, 1408, 568, zorder = 2)
if noRadar:
plt.gcf().figimage(imerror, 1408, 568, alpha = 0.2, zorder = 3)
plt.gcf().text(0.79,0.5,a.strftime("%Y_%m_%d %H:%M") + ' UTC', fontsize=15, color='grey', family='monospace')
plt.gcf().text(0.735,0.05,'RDS messages from Brisbane FM radio\nstation Triple M (104.5 MHz) were\nlogged over a two week period. To\ndemonstrate a correlation between\nrainfall and message frequency, the\nnumber of messages in a 6 minute\nperiod is compared to historical data\nfrom the Mt Stapylton 128 km weather\nradar.\n\nThe small magenta line on the radar\nimage indicates the path from the\ntransmitter to the receiver\n\nGrant Trebbin\n@GPTreb 2015',fontsize=15, color='#444444')
plt.gcf().savefig('Output\\'+ str(frame) +'.png',dpi=120)
plt.show()
print (radarfile)
print (logfilelist[0][0])
print (len(logfilelist))
k=input("press close to exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment