How to handle negative cosine of Solar Zenith Angle while calculating clear-sky UV Index?
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)&gt;=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