Search code examples
pythonpython-3.xozone

How to handle negative cosine of Solar Zenith Angle while calculating clear-sky UV Index?


I'm using this source to calculate the UVI value for every hour in a day (From 7am to 7pm). And using pysolar module to get the Solar Zenith Angle. The equation is below:

from pysolar.solar import *
from math import cos
import datetime

dobj = datetime.datetime(2017,7,1,8,0)
sza = float(90)-get_altitude(42.57952, 1.65362, dobj)
ozone = 300
raw_uvi = 12.50*pow(cos(sza),2.42)*pow(float(ozone)/300,-1.23)
print (raw_uvi)

The output is: 0.4850283419701262+1.8890606693266203j

My question is when cos(sza) becomes negative it will give a complex number as output, how can I handle this ? Also, at 7 am the uvi results in 9.232276464148745 which is wrong. Am I missing something ? Or my interpretation of the algorithm is wrong ?


Solution

  • math.cos expects radians but you seem to pass in degrees.

    Convert from degrees to radians with sza * math.pi / 180. Then you should not get negative values for angles between -90 and +90 degrees (which I guess makes sense, assuming that 0 degrees means the sun is in the zenith and 90 degrees is the horizon).