# 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 ?

## 1 Answer

This is only to address the title question.

You are right, in a way. I would recommend that you set an if statement up such that

if cos(sza)>=0: raw_uvi = 12.50*pow(cos(sza),2.42)*pow(float(ozone)/300,-1.23) else: raw_uvi=0.0

This makes it so that when the sun is beneath the horizon, the sun is not 'taking back' the energy