dnr_month_data.html
001| """MN DNR Monthly Data.
001|
001| Classes:
001| DataRow (list)
001|
001| Functions:
001| main ~task dispatch function
001| month_data_print_json ~[json] print the monthly data in JSON format
001| month_data_hdd_cdd_chart ~[dd] chart of heating/cooling degree days
001| month_data_dep_chart ~[dep] chart of temp departures from 30-year average
001| month_data_snow_chart ~[snow] make monthly snow fall and depth chart
001|
001| [Site]
001| https://www.dnr.state.mn.us/climate/historical/lcd.html?loc=msp
001|
001| [Curr]
001| https://www.weather.gov/media/mpx/Climate/MSP/jan2026.pdf
001|
001| [Fields]
001| https://www.weather.gov/grr/climateF6explain
001|
001| """
002|
003| from os import path
004| from calendar import month_name
005| import matplotlib.pyplot as plt
006| import matplotlib.ticker as mlticks
007| import matplotlib.dates as mldates
008| from matplotlib.patches import Patch
009| from goodies import makefilename, roundup,rounddn, str2int,str2float
010|
011| BasePath = r'C:\CJS\prj\Python\app\weather'
012| ImgsPath = path.join(BasePath, 'img')
013| DataPath = path.join(BasePath, 'dat')
014|
015|
016| MonthCode = (2026, 11)
017| MonthData = """\
017| 1 71 53 62 20 3 0 0.00 0.0 0 8.1 20 170 M M 8 1 26 180
017| 2 66 57 62 21 3 0 0.57 0.0 0 14.6 22 160 M M 10 18 35 180
017| 3 66 53 60 19 5 0 0.74 0.0 0 9.0 17 330 M M 10 1 27 360
017| 4 53 47 50 10 15 0 0.56 0.0 0 8.2 16 360 M M 10 1 22 360
017| 5 51 37 44 4 21 0 0.03 0.0 0 13.2 24 340 M M 10 1 33 350
017| 6 48 31 40 0 25 0 0.00 0.0 M 3.1 8 280 M M 2 12 270
017| 7 56 32 44 5 21 0 0.00 0.0 0 7.1 16 240 M M 3 23 230
017| 8 57 42 50 12 15 0 0.00 0.0 0 8.8 21 290 M M 2 26 300
017| 9 61 38 50 12 15 0 0.00 0.0 0 7.6 20 250 M M 3 30 250
017| 10 61 38 50 12 15 0 0.00 0.0 0 4.5 10 270 M M 4 16 270
017| 11 56 38 47 10 18 0 0.00 0.0 0 9.6 23 360 M M 7 32 350
017| 12 40 27 34 -2 31 0 T 0.0 0 8.3 18 30 M M 6 30 30
017| 13 40 24 32 -4 33 0 0.00 0.0 0 5.0 10 30 M M 0 8 16 40
017| 14 44 25 35 -1 30 0 0.00 0.0 0 1.3 7 150 M M 0 11 10
017| 15 51 27 39 4 26 0 0.00 0.0 0 6.1 13 160 M M 2 17 170
017| 16 57 32 45 11 20 0 0.00 0.0 0 8.9 17 210 M M 2 29 210
017| 17 56 43 50 16 15 0 0.00 0.0 0 12.1 21 200 M M 8 31 200
017| 18 59 43 51 17 14 0 0.00 0.0 0 13.0 24 180 M M 8 35 180
017| 19 55 43 49 16 16 0 0.00 0.0 0 11.2 26 250 M M 2 40 240
017| 20 65 40 53 20 12 0 0.00 0.0 0 14.6 29 170 M M 7 42 190
017| 21 63 43 53 21 12 0 0.13 0.0 0 10.9 22 200 M M 10 18 33 190
017| 22 43 30 37 5 28 0 0.01 0.0 0 16.1 25 310 M M 8 35 280
017| 23 30 22 26 -5 39 0 0.00 0.0 0 9.9 21 330 M M 5 28 320
017| 24 34 18 26 -5 39 0 0.00 0.0 0 4.6 10 220 M M 5 14 220
017| 25 39 26 33 3 32 0 0.00 0.0 0 4.9 12 140 M M 6 16 100
017| 26 40 22 31 1 34 0 0.00 0.0 0 3.7 10 320 M M 3 16 30
017| 27 43 21 32 3 33 0 T 0.0 0 4.2 9 310 M M 4 12 210
017| 28 54 31 43 14 22 0 0.00 0.0 0 8.1 22 330 M M 2 1 29 340
017| """
018|
019|
020|
021| class DataRow (list):
022|
023| def __init__ (self, line:str):
024| super().__init__()
025| self.append(int(line[ 0: 2].strip()))
026| self.append(int(line[ 2: 6].strip()))
027| self.append(int(line[ 6:10].strip()))
028| self.append(int(line[10:14].strip()))
029| self.append(int(line[14:18].strip()))
030| self.append(int(line[18:22].strip()))
031| self.append(int(line[22:26].strip()))
032| self.append(line[26:31].strip())
033| self.append(line[31:36].strip())
034| self.append(line[36:41].strip())
035| self.append(float(line[41:46].strip()))
036| self.append(int(line[46:49].strip()))
037| self.append(int(line[49:53].strip()))
038| self.append(line[53:57].strip())
039| self.append(line[57:63].strip())
040| self.append(line[63:67].strip())
041| self.append(line[67:74].strip())
042| self.append(line[74:77].strip())
043| self.append(line[77:81].strip())
044|
045| def __repr__ (self):
046| return f'<{type(self).__name__} {self.day:2d} @{id(self):012x}>'
047|
048| def __str__ (self):
049| return f'{self.day:2d}: lo: {self.lo:+4d}, hi: {self.hi:+4d}, precip: {self.rain:>4s}/{self.snow:>3s}'
050|
051| @property
052| def day (self): return self[0]
053|
054| @property
055| def hi (self): return self[1]
056|
057| @property
058| def lo (self): return self[2]
059|
060| @property
061| def tavg (self): return self[3]
062|
063| @property
064| def dep (self): return self[4]
065|
066| @property
067| def hdd (self): return self[5]
068|
069| @property
070| def cdd (self): return self[6]
071|
072| @property
073| def rain (self): return self[7]
074|
075| @property
076| def snow (self): return self[8]
077|
078| @property
079| def snowdepth (self): return self[9]
080|
081| @property
082| def wind_speed (self): return self[10]
083|
084| @property
085| def wind_max (self): return self[11]
086|
087| @property
088| def wind_angle (self): return self[12]
089|
090| @property
091| def sun_mins (self): return self[13]
092|
093| @property
094| def sun_poss (self): return self[14]
095|
096|
097|
098| def month_data_print_json (month_data:list, ncols:int=5):
099| '''Print the monthly data (in JSON file format).'''
100| days = list(month_data)
101| indent = " "*4
102|
103| hi_temp = max(d.hi for d in month_data)
104| lo_temp = min(d.lo for d in month_data)
105|
106| print(f'Last Date: {month_data[-1].day}')
107| print(f'Max Lo: {lo_temp} (ymin: {((lo_temp//10)+0)*10})')
108| print(f'Max Hi: {hi_temp} (ymax: {((hi_temp//10)+1)*10})')
109| print()
110|
111| while len(days):
112| data = days[0:ncols]
113| cols = [f'[{d.day:2d},{d.lo:3d},{d.hi:3d}]' for d in data]
114| line = ', '.join(cols)
115| del days[0:ncols]
116|
117| print(f'{indent}{line}{"," if len(days) else ""}')
118| print()
119|
120|
121| def month_data_hdd_cdd_chart (month_data:list, month_code:tuple[int,int]):
122| '''Make a chart of Heating/Cooling Degree Days (HDD/CDD).'''
123| yr,mon = month_code
124| mon_nm = month_name[mon]
125| filename = makefilename(BasePath, f'hdd_cdd_{yr:04d}-{mon:02d}.png')
126|
127| xs = [d.day for d in month_data]
128| ys1 = [str2int(d.hdd) for d in month_data]
129| ys2 = [str2int(d.cdd) for d in month_data]
130|
131| xmax = 0.5 + len(xs)
132| ymax = 5 + roundup(ys1+ys2, 5)
133|
134| fig, ax = plt.subplots()
135| fig.suptitle(f'{mon_nm} {yr} Heating and Cooling Degree Days', fontsize=14, fontweight='bold')
136| fig.set_figwidth(8.8)
137| fig.set_figheight(3.0)
138| fig.subplots_adjust(left=0.075, right=0.996, bottom=0.18, top=0.90)
139|
140| ax.set_xlabel('Day', fontsize=11, fontweight='bold')
141| ax.set_xlim(0.5, xmax)
142| ax.xaxis.set_major_locator(mlticks.MultipleLocator(1))
143| ax.xaxis.set_minor_locator(mlticks.NullLocator())
144| ax.xaxis.set_major_formatter(mlticks.FormatStrFormatter('%2d'))
145| ax.xaxis.set_minor_formatter(mlticks.NullFormatter())
146|
147| for tick in ax.xaxis.get_major_ticks():
148| tick.label1.set_rotation('vertical')
149| tick.label1.set_size(9)
150|
151| ax.set_ylabel('Heating/Cooling Degrees', fontsize=11, fontweight='bold')
152| ax.set_ylim(0.0, ymax)
153| ax.yaxis.set_major_locator(mlticks.MultipleLocator(5.0))
154| ax.yaxis.set_minor_locator(mlticks.MultipleLocator(1.0))
155| ax.yaxis.set_major_formatter(mlticks.FormatStrFormatter('%.0f'))
156| ax.yaxis.set_minor_formatter(mlticks.NullFormatter())
157|
158| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
159| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
160| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
161| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
162|
163| ax.bar(xs,ys1, label='HDD', color='#ff0000', alpha=0.75, zorder=11)
164| ax.bar(xs,ys2, label='CDD', color='#0000ff', alpha=0.75, zorder=12)
165|
166| fig.legend(ncols=2, fontsize=10, loc='upper left', bbox_to_anchor=(0.10,0.90))
167|
168| fig.savefig(filename)
169| print('wrote: %s' % filename)
170| print()
171|
172|
173| def month_data_dep_chart (month_data:list, month_code:tuple[int,int]):
174| '''Make a chart of temperature departures from the 30-year average.'''
175| yr,mon = month_code
176| mon_nm = month_name[mon]
177| filename = makefilename(BasePath, f'dep_{yr:04d}-{mon:02d}.png')
178|
179| xs = [d.day for d in month_data]
180| ys = [str2int(d.dep) for d in month_data]
181| cs = ['#ff0000' if 0<y else '#0000ff' for y in ys]
182|
183| xmax = 0.5 + len(xs)
184| ymax = roundup(ys, 5)
185| ymin = rounddn(ys, 5)
186|
187| fig, ax = plt.subplots()
188| fig.suptitle(f'{mon_nm} {yr} Daily Departure from Average', fontsize=14, fontweight='bold')
189| fig.set_figwidth(8.8)
190| fig.set_figheight(3.0)
191| fig.subplots_adjust(left=0.075, right=0.996, bottom=0.18, top=0.90)
192|
193| ax.set_xlabel('Day', fontsize=11, fontweight='bold')
194| ax.set_xlim(0.5, xmax)
195| ax.xaxis.set_major_locator(mlticks.MultipleLocator(1))
196| ax.xaxis.set_minor_locator(mlticks.NullLocator())
197| ax.xaxis.set_major_formatter(mlticks.FormatStrFormatter('%2d'))
198| ax.xaxis.set_minor_formatter(mlticks.NullFormatter())
199|
200| for tick in ax.xaxis.get_major_ticks():
201| tick.label1.set_rotation('vertical')
202| tick.label1.set_size(9)
203|
204| ax.set_ylabel('Temp Differance', fontsize=11, fontweight='bold')
205| ax.set_ylim(ymin, ymax)
206| ax.yaxis.set_major_locator(mlticks.MultipleLocator(5.0))
207| ax.yaxis.set_minor_locator(mlticks.MultipleLocator(1.0))
208| ax.yaxis.set_major_formatter(mlticks.FormatStrFormatter('%.0f'))
209| ax.yaxis.set_minor_formatter(mlticks.NullFormatter())
210|
211| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
212| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
213| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
214| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
215|
216| ax.bar(xs,ys, color=cs, label='Difference', alpha=0.85, zorder=11)
217|
218| fig.savefig(filename)
219| print('wrote: %s' % filename)
220| print()
221|
222|
223| def month_data_rain_chart (month_data:list, month_code:tuple[int,int]):
224| '''Make a chart of monthly percipitation. (And sunshine?)'''
225| yr,mon = month_code
226| mon_nm = month_name[mon]
227| filename = makefilename(BasePath, f'rain_{yr:04d}-{mon:02d}.png')
228|
229| xs = [d.day for d in month_data]
230| ys1 = [str2float(d.rain) for d in month_data]
231| ys2 = [str2float(d.sun_mins) for d in month_data]
232| ys3 = [str2float(d.sun_poss) for d in month_data]
233|
234| print(ys2); print()
235| print(ys3); print()
236|
237| xmax = 0.5 + len(xs)
238| ymax = roundup(ys1, 1)
239|
240| fig, ax = plt.subplots()
241| fig.suptitle(f'{mon_nm} Rain', fontsize=14, fontweight='bold')
242| fig.set_figwidth(8.8)
243| fig.set_figheight(3.0)
244| fig.subplots_adjust(left=0.075, right=0.996, bottom=0.18, top=0.90)
245|
246| ax.set_xlabel('Day', fontsize=11, fontweight='bold')
247| ax.set_xlim(0.5, xmax)
248| ax.xaxis.set_major_locator(mlticks.MultipleLocator(1))
249| ax.xaxis.set_minor_locator(mlticks.NullLocator())
250| ax.xaxis.set_major_formatter(mlticks.FormatStrFormatter('%2d'))
251| ax.xaxis.set_minor_formatter(mlticks.NullFormatter())
252|
253| for tick in ax.xaxis.get_major_ticks():
254| tick.label1.set_rotation('vertical')
255| tick.label1.set_size(9)
256|
257| ax.set_ylabel('Inches', fontsize=11, fontweight='bold')
258| ax.set_ylim(0.0, ymax)
259| ax.yaxis.set_major_locator(mlticks.MultipleLocator(1.0))
260| ax.yaxis.set_minor_locator(mlticks.MultipleLocator(0.1))
261| ax.yaxis.set_major_formatter(mlticks.FormatStrFormatter('%.2f'))
262| ax.yaxis.set_minor_formatter(mlticks.NullFormatter())
263|
264| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
265| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
266| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
267| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
268|
269|
270| ax.bar(xs,ys1, color='#0000ff', lw=3.0, alpha=1.00, zorder=12)
271|
272|
273|
274| fig.savefig(filename)
275| print('wrote: %s' % filename)
276| print()
277|
278|
279| def month_data_snow_chart (month_data:list, month_code:tuple[int,int]):
280| '''Make a chart of monthly snow fall and depth.'''
281| yr,mon = month_code
282| mon_nm = month_name[mon]
283| filename = makefilename(BasePath, f'snow_{yr:04d}-{mon:02d}.png')
284|
285| xs = [d.day for d in month_data]
286| ys1 = [str2float(d.snow) for d in month_data]
287| ys2 = [str2int(d.snowdepth) for d in month_data]
288|
289| xmax = 0.5 + len(xs)
290| ymax = 5 + roundup(ys1+ys2, 5)
291|
292| for dt,sf,sd in zip(xs, ys1, ys2):
293| print(f'{dt}: {sf} {sd}')
294| print()
295|
296| fig, ax = plt.subplots()
297| fig.suptitle(f'{mon_nm} Snow Fall and Depth', fontsize=14, fontweight='bold')
298| fig.set_figwidth(8.8)
299| fig.set_figheight(3.0)
300| fig.subplots_adjust(left=0.075, right=0.996, bottom=0.18, top=0.90)
301|
302| ax.set_xlabel('Day', fontsize=11, fontweight='bold')
303| ax.set_xlim(0.5, xmax)
304| ax.xaxis.set_major_locator(mlticks.MultipleLocator(1))
305| ax.xaxis.set_minor_locator(mlticks.NullLocator())
306| ax.xaxis.set_major_formatter(mlticks.FormatStrFormatter('%2d'))
307| ax.xaxis.set_minor_formatter(mlticks.NullFormatter())
308|
309| for tick in ax.xaxis.get_major_ticks():
310| tick.label1.set_rotation('vertical')
311| tick.label1.set_size(9)
312|
313| ax.set_ylabel('Depth', fontsize=11, fontweight='bold')
314| ax.set_ylim(0.0, ymax)
315| ax.yaxis.set_major_locator(mlticks.MultipleLocator(5.0))
316| ax.yaxis.set_minor_locator(mlticks.MultipleLocator(1.0))
317| ax.yaxis.set_major_formatter(mlticks.FormatStrFormatter('%.1f'))
318| ax.yaxis.set_minor_formatter(mlticks.NullFormatter())
319|
320| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
321| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
322| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
323| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
324|
325|
326| ax.fill_between(xs,ys2, color='#007fff', lw=0, alpha=0.50, zorder=11)
327| ax.plot(xs,ys2, color='#0000ff', lw=3.0, alpha=0.75, zorder=11)
328|
329|
330| ax.bar(xs,ys1, color='#993300', lw=3.0, alpha=1.00, zorder=12)
331|
332|
333| patchs = [
334| Patch(facecolor='#0000ff', edgecolor='#000000', lw=0.75),
335| Patch(facecolor='#993300', edgecolor='#000000', lw=0.75),
336| ]
337| labels = ['Snow Depth', 'Daily Snowfall']
338| fig.legend(patchs, labels, ncols=2, fontsize=10, loc='upper right', bbox_to_anchor=(1.00,0.90))
339|
340| fig.savefig(filename)
341| print('wrote: %s' % filename)
342| print()
343|
344|
345|
346| def main (source, month_code, tasks='all'):
347|
348|
349| month_data = [DataRow(line) for line in source.splitlines() if line]
350|
351|
352| if ('all' in tasks) or ('prn' in tasks):
353| for rcd in month_data:
354| print(rcd)
355| print()
356|
357|
358| if ('all' in tasks) or ('json' in tasks):
359| month_data_print_json(month_data)
360|
361|
362| if ('all' in tasks) or ('dd' in tasks):
363| month_data_hdd_cdd_chart(month_data, month_code)
364|
365|
366| if ('all' in tasks) or ('dep' in tasks):
367| month_data_dep_chart(month_data, month_code)
368|
369|
370| if ('all' in tasks) or ('rain' in tasks):
371| month_data_rain_chart(month_data, month_code)
372|
373|
374| if ('all' in tasks) or ('snow' in tasks):
375| month_data_snow_chart(month_data, month_code)
376|
377| return month_data
378|
379|
380| if __name__ == '__main__':
381| print()
382| rv = main(MonthData, MonthCode, tasks='all')
383| print()
384|
385| '''eof'''
386|