<p><strong>This is only to address the title question</strong>.</p>

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

<p><code>if cos(sza)&gt;=0:
   raw_uvi = 12.50*pow(cos(sza),2.42)*pow(float(ozone)/300,-1.23)
else: 
   raw_uvi=0.0</code></p>

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

CC BY-SA 3.0
2017-07-20T15:43:13.180Z
2017-07-21T15:32:00.237Z
2017-07-21T15:32:00.237Z
BarocliniCplusplus
<p>I'm using <a href="https://users.cs.jmu.edu/bernstdh/Web/CS159/pa1/Background_UltravioletIndex.pdf" rel="nofollow noreferrer">this</a> source to calculate the UVI value for every hour in a day (From 7am to 7pm). And using <a href="https://pysolar.readthedocs.io/en/latest/" rel="nofollow noreferrer">pysolar</a> module to get the <code>Solar Zenith Angle</code>. The equation is below:</p>

<pre><code>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)
</code></pre>

<p>The output is:
    0.4850283419701262+1.8890606693266203j</p>

<p>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 <code>9.232276464148745</code> which is wrong. Am I missing something ? Or my interpretation of the algorithm is wrong  ?</p>

<clouds><ozone><python>
How to handle negative cosine of Solar Zenith Angle while calculating clear-sky UV Index?
Rahul
# How to handle negative cosine of Solar Zenith Angle while calculating clear-sky UV Index?

(Asked by: username_0 on 2017-07-20)

I'm using [this](https://users.cs.jmu.edu/bernstdh/Web/CS159/pa1/Background_UltravioletIndex.pdf) source to calculate the UVI value for every hour in a day (From 7am to 7pm). And using [pysolar](https://pysolar.readthedocs.io/en/latest/) 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 ?

**Tags:** clouds, ozone, python


---

## 1 answers

### Answer by username_1 on 2017-07-20. Score: 2

**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


---

