<p>Look at the pysolar docs (<a href="http://pysolar.readthedocs.io/en/latest/" rel="nofollow noreferrer">http://pysolar.readthedocs.io/en/latest/</a>) under &quot;Estimate of clear sky radiation&quot;. The algorithm does not return zeros at night, but instead just plugs those numbers straight in, giving nonsensical values. Filter the results so that if <code>altitude_deg</code> &lt; 0, the radiation is 0. An example just using pysolar, datetime, and pyplot (I don't use pandas, so I can't comment on that) and lists looks like:</p>
<pre><code>import datetime
import matplotlib.pyplot as plt
import pysolar
lat, lon = 39.9075, 116.39723  # Beijing, China
timezone = datetime.timezone(datetime.timedelta(hours=8))  # 0800 UTC
start = datetime.datetime(2018,1,1,tzinfo=timezone)  # 1 Jan 2018

# Calculate radiation every hour for 90 days
nhr = 24*90
dates, altitudes_deg, radiations = list(), list(), list()
for ihr in range(nhr):
    date = start + datetime.timedelta(hours=ihr)
    altitude_deg = pysolar.solar.get_altitude(lat,lon,date)
    if altitude_deg &lt;= 0:
        radiation = 0.
    else:
        radiation = pysolar.radiation.get_radiation_direct(date,altitude_deg)
    dates.append(date)
    altitudes_deg.append(altitude_deg)
    radiations.append(radiation)

days = [ihr/24 for ihr in range(nhr)]
fig, axs = plt.subplots(nrows=2,ncols=1,sharex=True)
axs[0].plot(days,altitudes_deg)
axs[0].set_title('Solar altitude, degrees')
axs[1].plot(days,radiations)
axs[1].set_title('Solar radiation, W/m2')
axs[1].set_xlabel('Days since ' + start.strftime('%Y/%m/%d %H:%M UTC'))
plt.show()
</code></pre>
<p><a href="https://i.sstatic.net/t5MAT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/t5MAT.png" alt="Altitude and radiation plot" /></a></p>

CC BY-SA 4.0
2018-06-29T07:53:06.040Z
2021-03-27T17:03:08.797Z
2021-03-27T17:03:08.797Z
Jareth Holt
<p>The solar radiation is one of the important factors controlling the formation of $O_3$, and thereby impacting the levels of various secondary species in the atmosphere. </p>

<p>However, in the campaign of ambient $PM_{2.5}$ sampling, I don't have the instrument to acquire the actual solar radiation data. Since the solar radiation can be calculated mainly by the altitude degree of the sun, I turned to find a way to calculate the ideal values in clean sky condition.  </p>

<p>I found that <a href="http://docs.pysolar.org/en/latest/#examples" rel="noreferrer">pysolar</a> is a potential tool to tackle this kind of issue.  By simply defining the location(Lon, Lat), and the date time, the solar radiation in the unit of $W/m^2$ can be generated. </p>

<blockquote>
  <p>However, the output result here seems to be strange. </p>
</blockquote>

<hr>

<p>Here is an example using Python 3.4:    </p>

<pre><code># Noted that the package can only be implemented in Python 3.4 environment
from pysolar.solar import *
import datetime

# define the location (Beijing, China)
lat, lon = 39.9075, 116.39723

# generate the time series dataset of the solar radiation
start = datetime.datetime(2018, 1, 1, 8, 0, 0, 0, tzinfo=datetime.timezone.utc)

solar_data = []
for i in range(0,24*90,1): # 24 hours x 90 days
    date = start + pd.to_timedelta(1,'H')
    altitude_deg = get_altitude(lat, lon, date)
    solar_data.append(radiation.get_radiation_direct(date, altitude_deg))

solar_ = pd.Dataframe([])
solar_['value] = solar_data

# Plotting
## Before plotting, I found there are a lot of extreme values with the radiation larger than  10e5. I don't know the reasons why they came up, and how to delete those data by conditions. 

solar_.loc[solar_.value&gt;5000, 'value'] = np.nan
plt.plot(solar_.value)
</code></pre>

<p><a href="https://i.sstatic.net/C63Xq.png" rel="noreferrer"><img src="https://i.sstatic.net/C63Xq.png" alt="enter image description here"></a>   </p>

<p>The result seems to be incorrect. In my opinion, the solar radiation should present a clear diurnal pattern with seasonal heterogeneity. </p>

<p>How to explain the bizarre results? Or is there any better solution for the solar radiation data if the monitoring equipment is unavailable. </p>

<p>Any comments or suggestions would be appreciated.</p>

<p><strong>PS</strong>: I added the result of altitude degree   the same period to response the comment of BarocliniCplusplus 
<a href="https://i.sstatic.net/zxa31.png" rel="noreferrer"><img src="https://i.sstatic.net/zxa31.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2018-06-25T17:34:37.573Z
It looks like the output of a secant function. Might I suggest checking to see if the altitude angle seems reasonable (like printing it out)?
BarocliniCplusplus
CC BY-SA 4.0
2018-06-26T10:21:26.033Z
I've barely ever done anything in Python, so may be way, way off base entirely... but noticed date is odd to not use i (perhaps somehow pd is assumed to iterate because of the loop!????), and also wondering what the ['value] is doing in the solar_['value] line (maybe making a list out of an array or some such thing, but would think you'd want the index not the value??) Anyways, probably useless input, but thoughts (figured why not, with so little reply).  Maybe print just a single value before looping/trying to reformat into a graphics package to make sure the issue isn't somewhere in between?
JeopardyTempest
CC BY-SA 4.0
2018-06-26T10:29:55.483Z
@BarocliniCplusplus, thanks for your reply, and sorry for the late response. I'll upload the altitude degree data as supplemental information.
Han Zhengzu
<meteorology><atmosphere><atmosphere-modelling><atmospheric-chemistry><atmospheric-radiation>
How to calculate the solar radiation at any place, any time
Han Zhengzu
# How to calculate the solar radiation at any place, any time

(Asked by: username_0 on 2018-06-25)

The solar radiation is one of the important factors controlling the formation of $O\_3$, and thereby impacting the levels of various secondary species in the atmosphere.

However, in the campaign of ambient $PM\_{2.5}$ sampling, I don't have the instrument to acquire the actual solar radiation data. Since the solar radiation can be calculated mainly by the altitude degree of the sun, I turned to find a way to calculate the ideal values in clean sky condition.

I found that [pysolar](http://docs.pysolar.org/en/latest/#examples) is a potential tool to tackle this kind of issue. By simply defining the location(Lon, Lat), and the date time, the solar radiation in the unit of $W/m^2$ can be generated.

> However, the output result here seems to be strange.

---

Here is an example using Python 3.4:

```
# Noted that the package can only be implemented in Python 3.4 environment
from pysolar.solar import *
import datetime

# define the location (Beijing, China)
lat, lon = 39.9075, 116.39723

# generate the time series dataset of the solar radiation
start = datetime.datetime(2018, 1, 1, 8, 0, 0, 0, tzinfo=datetime.timezone.utc)

solar_data = []
for i in range(0,24*90,1): # 24 hours x 90 days
    date = start + pd.to_timedelta(1,'H')
    altitude_deg = get_altitude(lat, lon, date)
    solar_data.append(radiation.get_radiation_direct(date, altitude_deg))

solar_ = pd.Dataframe([])
solar_['value] = solar_data

# Plotting
## Before plotting, I found there are a lot of extreme values with the radiation larger than  10e5. I don't know the reasons why they came up, and how to delete those data by conditions. 

solar_.loc[solar_.value>5000, 'value'] = np.nan
plt.plot(solar_.value)

```

[![enter image description here](https://i.sstatic.net/C63Xq.png)](https://i.sstatic.net/C63Xq.png)

The result seems to be incorrect. In my opinion, the solar radiation should present a clear diurnal pattern with seasonal heterogeneity.

How to explain the bizarre results? Or is there any better solution for the solar radiation data if the monitoring equipment is unavailable.

Any comments or suggestions would be appreciated.

**PS**: I added the result of altitude degree the same period to response the comment of username_1
[![enter image description here](https://i.sstatic.net/zxa31.png)](https://i.sstatic.net/zxa31.png)

**Tags:** meteorology, atmosphere, atmosphere-modelling, atmospheric-chemistry, atmospheric-radiation

> **Comments:**
> **username_1**: It looks like the output of a secant function. Might I suggest checking to see if the altitude angle seems reasonable (like printing it out)?  
> **username_2**: I've barely ever done anything in Python, so may be way, way off base entirely... but noticed date is odd to not use i (perhaps somehow pd is assumed to iterate because of the loop!????), and also wondering what the ['value] is doing in the solar_['value] line (maybe making a list out of an array or some such thing, but would think you'd want the index not the value??) Anyways, probably useless input, but thoughts (figured why not, with so little reply).  Maybe print just a single value before looping/trying to reformat into a graphics package to make sure the issue isn't somewhere in between?  
> **username_0**: @username_1, thanks for your reply, and sorry for the late response. I'll upload the altitude degree data as supplemental information.  

---

## 1 answers

### Answer by username_3 on 2018-06-29. Score: 7 | Accepted answer

Look at the pysolar docs (<http://pysolar.readthedocs.io/en/latest/>) under "Estimate of clear sky radiation". The algorithm does not return zeros at night, but instead just plugs those numbers straight in, giving nonsensical values. Filter the results so that if `altitude_deg` < 0, the radiation is 0. An example just using pysolar, datetime, and pyplot (I don't use pandas, so I can't comment on that) and lists looks like:

```
import datetime
import matplotlib.pyplot as plt
import pysolar
lat, lon = 39.9075, 116.39723  # Beijing, China
timezone = datetime.timezone(datetime.timedelta(hours=8))  # 0800 UTC
start = datetime.datetime(2018,1,1,tzinfo=timezone)  # 1 Jan 2018

# Calculate radiation every hour for 90 days
nhr = 24*90
dates, altitudes_deg, radiations = list(), list(), list()
for ihr in range(nhr):
    date = start + datetime.timedelta(hours=ihr)
    altitude_deg = pysolar.solar.get_altitude(lat,lon,date)
    if altitude_deg <= 0:
        radiation = 0.
    else:
        radiation = pysolar.radiation.get_radiation_direct(date,altitude_deg)
    dates.append(date)
    altitudes_deg.append(altitude_deg)
    radiations.append(radiation)

days = [ihr/24 for ihr in range(nhr)]
fig, axs = plt.subplots(nrows=2,ncols=1,sharex=True)
axs[0].plot(days,altitudes_deg)
axs[0].set_title('Solar altitude, degrees')
axs[1].plot(days,radiations)
axs[1].set_title('Solar radiation, W/m2')
axs[1].set_xlabel('Days since ' + start.strftime('%Y/%m/%d %H:%M UTC'))
plt.show()

```

[![Altitude and radiation plot](https://i.sstatic.net/t5MAT.png)](https://i.sstatic.net/t5MAT.png)


---

