from lxml import etree
import zipfile
from datetime import date, time, datetime, timedelta
import calendar

# user defined variables 
station = "C040"
y0 = 2008
yN = 2012

# define the sequence of times (every 10 minutes) 
# to be used with potential missing files and data
allHours = [''] * 144
tlast = datetime.combine(date.today(), time(0, 0))
allHours[0] = tlast.strftime("%H:%M")
for i in range(1, 144):
  tlast += timedelta(minutes = 10)
  allHours[i] = tlast.strftime("%H:%M")

# main loop
fout = open('%s.csv' % station, 'w')

for yy in range(y0, yN + 1):
  print yy
  for imonth in range(1, 13):
    zipf = zipfile.ZipFile("../%s/%s_%s.zip" % (yy, station, yy))
    isNA = False
    try:      
      xmlf = zipf.open("%s/%s_%s_%s.xml" % (station, station, yy, imonth))
    except KeyError:
      try: # first try if month '1' is denoted '01'
        xmlf = zipf.open("%s/%s_%s_0%s.xml" % (station, station, yy, imonth))
      except KeyError: # the file does not exist (missing values)
	isNA = True
	temp = ''
    if isNA == False: # if file exists
      xmlData = etree.parse(xmlf)
      monthDays = xmlData.findall("//dia")
      print len(monthDays)
      for day in monthDays:
	dayLabel = day.attrib['Dia']
	hours = day.findall("hora")
	for hour in hours:
	  temp = hour.findtext("Meteoros/Tem.Aire._a_620cm")
	  #print "%s; %s; %s" % (dayLabel, hour.attrib['Hora'], temp)
	  #print hour.find("Meteoros/Tem.Aire._a_620cm")
	  fout.write("%s;%s;%s\n" % (dayLabel, hour.attrib['Hora'], temp))
      xmlf.close()
    else: # if file does not exist (missing data for that year and month)
      ndays = calendar.monthrange(yy, imonth)[1]
      monthDays = range(1, ndays + 1)
      print "%s (missing)" % len(monthDays)
      for day in monthDays:
	d = "0%s" % day if day < 10 else day
        month = "0%s" % imonth if imonth < 10 else imonth
	dayLabel = "%s-%s-%s" % (yy, month, d)
	for hour in allHours:
	  temp = '' # NA
	  fout.write("%s;%s;%s\n" % (dayLabel, hour, temp))
    zipf.close()

fout.close()
