"""Count tornadoes by month for given states in the SPC 1950-2024 tornado CSV (argument 1). Counts one record per tornado (sg == 1), assigned to the state where the record places it (st). Run with python3 -I -X utf8.""" import sys, csv, collections path = sys.argv[1] states = sys.argv[2:] or ['AZ', 'NM'] by_month = {s: collections.Counter() for s in states} years_oct = {s: collections.Counter() for s in states} oct_days = {s: set() for s in states} oct_mag = {s: collections.Counter() for s in states} total = {s: 0 for s in states} n_rows = 0 with open(path, newline='', encoding='utf-8', errors='replace') as f: r = csv.DictReader(f) cols = r.fieldnames for row in r: n_rows += 1 if row['sg'].strip() != '1': continue st = row['st'].strip() if st not in by_month: continue mo = int(row['mo']) by_month[st][mo] += 1 total[st] += 1 if mo == 10: years_oct[st][int(row['yr'])] += 1 oct_days[st].add(row['date']) oct_mag[st][row['mag'].strip()] += 1 print('columns:', cols) print('rows:', n_rows) for s in states: print('\n==', s, 'tornadoes 1950-2024 (sg=1):', total[s]) print(' by month:', [(m, by_month[s][m]) for m in range(1, 13)]) print(' October:', by_month[s][10], '= %.1f%% of all' % (100.0 * by_month[s][10] / total[s])) print(' years with an October tornado:', len(years_oct[s]), 'of 75;', sorted(years_oct[s].items())) print(' October tornado days:', len(oct_days[s])) print(' October ratings (mag):', sorted(oct_mag[s].items())) print(' average per year, all months: %.1f; October average: %.2f' % (total[s] / 75.0, by_month[s][10] / 75.0)) last30 = sum(1 for y in years_oct[s] if y >= 1995) print(' 1995-2024: October tornadoes', sum(c for y, c in years_oct[s].items() if y >= 1995), 'in', last30, 'years')