#!/usr/bin/python

import h5py, numpy, os, datetime

co2points = {}

filedir = "/data/ACOS_L2S.3.3"
for filename in os.listdir(filedir):
    if filename.endswith(".h5"):
        infile = h5py.File(os.path.join(filedir,filename),"r")
        time_string = infile["RetrievalHeader"]["sounding_time_string"][0:] # [2015-03-23T01:33:25.724Z]
        latitude = infile["SoundingGeometry"]["sounding_latitude"][0:]
        longitude = infile["SoundingGeometry"]["sounding_longitude"][0:]
        xco2 = infile["RetrievalResults"]["xco2"][0:]

        for x in xrange(xco2.shape[0]):
            if xco2[x] == -999999.0 or latitude[x] == -999999.0 or longitude[x] == -999999.0:
                # Ignore bad data
                continue
            else:                
                point_name = time_string[x][0:10]

                if (point_name) in co2points:
                    co2points[point_name].append(xco2[x])
                else:
                    co2points[point_name] = [xco2[x]]
        infile.close()

filedir = "/data/OCO2_L2_Standard.7"
for filename in os.listdir(filedir):
    if filename.endswith(".h5"):
        infile = h5py.File(os.path.join(filedir,filename),"r")
        time_string = infile["RetrievalHeader"]["retrieval_time_string"][0:]
        latitude = infile["RetrievalGeometry"]["retrieval_latitude"][0:]
        longitude = infile["RetrievalGeometry"]["retrieval_longitude"][0:]
        xco2 = infile["RetrievalResults"]["xco2"][0:]

        for x in xrange(xco2.shape[0]):
            if xco2[x] == -999999.0 or latitude[x] == -999999.0 or longitude[x] == -999999.0:
                # Ignore bad data
                continue
            else:         
                point_name = time_string[x][0:10]

                if (point_name) in co2points:
                    co2points[point_name].append(xco2[x])
                else:
                    co2points[point_name] = [xco2[x]]
        infile.close()

outfile = file("acos_oco2_daily.csv","w")
outfile.write("Date, Average Xco2, Reading Count\n")

for co2point in sorted(co2points.iterkeys()):
    count = str(len(co2points[co2point]))
    average = str(numpy.mean(co2points[co2point]))
    outfile.write(co2point+", "+average+", "+count+"\n")

outfile.close()
