Skip to content

Instantly share code, notes, and snippets.

@mattparrilla
Created April 8, 2014 20:03
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 mattparrilla/10182451 to your computer and use it in GitHub Desktop.
Save mattparrilla/10182451 to your computer and use it in GitHub Desktop.
Converts USHCN daily temperature data to a CSV with a row per year and date's as columns
#!/usr/local/bin/python
import csv
def date_to_winter():
f = csv.reader(open('vt-temp.csv', 'rU'))
dates = [l for l in f]
del dates[0:2]
date_row = []
all_dates = []
header_row = ['year']
for date in dates[:365]:
if date[0] == '1':
header_row.append(date[0])
else:
header_row.append(0)
header_row[-1] = '1'
all_dates.append(header_row)
for date in dates:
if date[0] == '29' and date[2] == '2':
continue
if date[0] == '1' and date[2] == '1':
all_dates.append(date_row)
if date[4][-1] == '0':
date_row = [date[4]]
else:
date_row = ['']
try:
date_row.append(int(date[5]))
except ValueError:
date_row.append(date_row[-1])
# Gets rid of incomplete years
list_to_write = []
for year in all_dates:
if len(year) < 365:
continue
else:
list_to_write.append(year)
writer = csv.writer(open('winters.csv', 'w'))
writer.writerows(list_to_write)
date_to_winter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment