Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Created December 9, 2022 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsnell/6ac75c44d0bc8dbd9c649f5211b77934 to your computer and use it in GitHub Desktop.
Save jasonsnell/6ac75c44d0bc8dbd9c649f5211b77934 to your computer and use it in GitHub Desktop.
#! /usr/local/bin/python3
# Recent daily rain script
# For WeatherCat - https://trixology.com
# By Jason Snell <jsnell@sixcolors.com>
import re
import matplotlib.pyplot as plt
from collections import defaultdict
from datetime import date
# Path to folder containing WeatherCat data files in year-folder format
# It's usually '/Users/[your-username]/Library/WeatherCatData/[your-location]/'
weatherCatPath = '/Users/admin/Library/WeatherCatData/Location1/'
# Path to folder where you want image to be saved
savePath = '/Library/Webserver/Documents/snellzone/weather/'
# mm or in switch; in is default
useMM = False
maxprecip = defaultdict(lambda: -99999)
diff = {}
today = date.today()
month = int(today.strftime('%m'))
day = today.strftime('%d')
year = today.strftime('%Y')
datelist = []
daysList = ['S', 'M', 'T', 'W', 'Th', 'F', 'S', 'S']
todaysday = int(today.strftime('%w'))
chartlist = []
for i in range(0, 7):
if todaysday == 0:
todaysday = 7
chartlist.append(daysList[todaysday])
todaysday -= 1
chartlist.reverse()
datapoint = re.compile(r't:([0-9]{2})[0-9]{4}')
rainpoint = re.compile(r'P:([0-9]{1,3}\.[0-9]{2})')
for i in range(month-1, month+1, 1):
if i < 1:
monthcode = 12
yearcode = str(int(year) - 1)
else:
monthcode = i
yearcode = year
filepath = (weatherCatPath + f'{yearcode}/{monthcode}_WeatherCatData.cat')
file = open(filepath, 'r')
monthstring = str(i)
if i < 10:
monthstring = "0" + monthstring
for count, line in enumerate(file):
if count > 10:
theday = datapoint.search(line)
todayDay = monthstring + (theday.group(1))
therain = rainpoint.search(line)
rainfloat = float(therain.group(1))
if useMM is True:
maxprecip[todayDay] = rainfloat
else:
maxprecip[todayDay] = round(rainfloat * 0.03937007874, 2)
file.close()
# now loop through from monthstring + day
# plucking values for days and putting them in a new list or dict
theRain = list(maxprecip.values())
theRainOne = theRain[-14:-7]
theRainTwo = theRain[-7:]
theDates = list(maxprecip.keys())
theDatesOne = theDates[-14:-7]
theDatesTwo = theDates[-7:]
# Graphing
fig, ax = plt.subplots()
font = {'fontname': 'Helvetica Neue'}
ax.bar(theDatesOne, theRainOne, color="blue")
ax.bar_label(ax.containers[0], color="black")
ax.set_aspect(aspect=0.4)
ax.set_yticks([])
plt.box(False)
plt.xticks(theDatesOne, chartlist, color="black", **font)
plt.savefig(f'{savePath}latestrain1.png', bbox_inches='tight',
dpi=300, pad_inches=0.05)
fig, ax = plt.subplots()
font = {'fontname': 'Helvetica Neue'}
ax.bar(theDatesTwo, theRainTwo, color="blue")
ax.bar_label(ax.containers[0], color="black")
ax.set_aspect(aspect=0.4)
ax.set_yticks([])
plt.box(False)
plt.xticks(theDatesTwo, chartlist, color="black", **font)
plt.savefig(f'{savePath}latestrain2.png', bbox_inches='tight',
dpi=300, pad_inches=0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment