def test_diffusehorizontal():
    """py.test for diffusehorizontal"""
    data = ((0.289, 2.641, 90, 21, 100.490533), # taub, taud, alt, daynum, Eb
    )
    for taub, taud, alt, daynum, Eb in data:
        result = clearskyrad.diffusehorizontal(taub, taud, alt, daynum=daynum)
        assert almostequal(result, Eb)
def test_weatherdata():
    """py.test with real weather data"""
    # from USA_AZ_Phoenix.722780_TMY2.ddy
    # datetime = month, date, hour = 1, 24, 13
    # ET Horizontal Radiation = 852
    # ET Direct Normal radiation = 1412
    # Direct Normal Radiation = 969
    # Diffuse horizontal radiation = 71
    # Total Sky cover = 0
    # Opaque sky cover = 0
    # from USA_AZ_Phoenix.722780_TMY2.stat
    # Jan taub = 0.306
    # Jan taud = 2.534
    # Feb taub = 0.317
    # Feb taud = 2.463
    # from <http://www.esrl.noaa.gov/gmd/grad/solcalc/azel.html>
    # solar azimuth = 185.8 degrees
    # solar altitude = 37.36 degrees

    Eb = 969
    taub = 0.306
    taud = 2.534
    alt = 37.36
    daynum = 24
    result = clearskyrad.directnormal(taub, taud, alt, daynum=daynum)
    # assert result == Eb
    
    Ed = 71
    result = clearskyrad.diffusehorizontal(taub, taud, alt, daynum=daynum)
    # assert result == Ed

    # other days 
    # 1975, 2, 16, 14
    Eb = 816 
    taub = 0.317
    taud = 2.463
    alt = 40.67
    daynum = 24
    thedate = datetime(1975, 2, 16)
    result = clearskyrad.directnormal(taub, taud, alt, thedate=thedate)
    # assert result == Eb
    
    Ed = 152
    result = clearskyrad.diffusehorizontal(taub, taud, alt, thedate=thedate)
    assert result == Ed
spadict['azm_rotation'] = 0
spadict['atmos_refract'] = 0.5667 # typical value

statfilename = './weatherfiles/USA_AZ_Phoenix/USA_AZ_Phoenix.722780_TMY2.stat'
taubs, tauds = clearskyrad.tau(open(statfilename))

# for m in range(1, 13):
for m in range(1, 13):
    for d in range(1, calendar.monthrange(spadict['year'], m)[1] + 1):
    # for d in range(1, 25):
        for h in range(1, 25):
            spadict['month'] = m
            spadict['day'] = d
            spadict['hour'] = h
            try:
                sun = spabase.spacalc(spadict)
                alt = 90 - sun['zenith']
                taub, taud = taubs[m -1], tauds[m -1]
                thedate = datetime(spadict['year'], m, d)
                if alt <= 0:
                    Eb = 0
                    Ed = 0
                else:
                    Eb = clearskyrad.directnormal(taub, taud, alt, thedate=thedate)
                    Ed = clearskyrad.diffusehorizontal(taub, taud, alt, thedate=thedate)
                print '%s, %s, %s, %s, %s, %s' % (m, d, h, alt, Eb, Ed)
                # print m, d, h, 90 - sun['zenith']
                # print sun
            except spabase.UnknownError:
                raise spabase.DataError