Skip to content

Instantly share code, notes, and snippets.

@yinyin
Created December 19, 2012 18:52
Show Gist options
  • Save yinyin/4339340 to your computer and use it in GitHub Desktop.
Save yinyin/4339340 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
""" Parsing timing information of sunrise and sunset from CWB """
import sys
import csv
import re
import pprint
if 4 != len(sys.argv):
print "Argument: [INPUT_CSV] [OUTPUT_TXT] [YEAR]"
sys.exit(1)
sun_rise = [[] for i in range(12)]
sun_set = [[] for i in range(12)]
time_regex = re.compile("^([0-9]{1,2})\:([0-9]{1,2})$")
fp = open(sys.argv[1], "rb")
csvreader = csv.reader(fp)
month_offset = 0
for row in csvreader:
if 13 > len(row):
continue
try:
day = int(row[0])
except Exception as e:
print "ERR: %r" % (e,)
continue
for i in range(6):
row_offset = i*2+1
time_sr = row[row_offset+0]
time_ss = row[row_offset+1]
if (time_regex.match(time_sr) is not None) and (time_regex.match(time_ss) is not None):
sun_rise[i+month_offset].append(time_sr)
sun_set[i+month_offset].append(time_ss)
assert(sun_rise[i+month_offset][day-1] == time_sr)
assert(sun_set[i+month_offset][day-1] == time_ss)
if 31 == day:
month_offset = 6
fp.close()
y = int(sys.argv[3])
fp = open(sys.argv[2], "wb")
for m in range(12):
tsun_r = sun_rise[m]
tsun_s = sun_set[m]
assert(len(tsun_r) == len(tsun_s))
for i in range(len(tsun_r)):
d = i + 1
v = "%d/%d/%d\t%s\t%s\n" % ((m+1), d, y, tsun_r[i], tsun_s[i],)
fp.write(v)
fp.close()
pprint.pprint(sun_rise)
# vim: ts=4 sw=4 ai nowrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment