<p>The code below will give you the alt-az coordinates for any specified location for any specified time.  From there, I think the easiest method to determine which side of the house it will hit is to subtract 145deg from the AZ, so that 0 means it's hitting the front dead on.  I'll call this the &quot;front angle&quot;.</p>
<p>If the front angle is between 270 and 90 (passing through 0), it's hitting the front.  Between 0 and 180 the left, between 90 and 270 the back, and between 180 and 0 the right.</p>
<p>You'll also want to validate the sun's altitude is &gt; 0</p>
<pre><code>#!/usr/bin/python
import astropy.units as u
from astropy.time import Time
from astropy.coordinates import EarthLocation, AltAz
from astropy.coordinates import get_sun

louisville = EarthLocation.from_geodetic(-85.7636,38.2464000,0)
utcoffset = -4*u.hour  # Eastern Daylight Time
time = Time('2012-7-12 20:00:00') - utcoffset
aaframe=AltAz(obstime=time,location=louisville)

sun=get_sun(time)

sunaa=sun.transform_to(aaframe)
print(f&quot;Sun Alt, Az = {sunaa.alt} {sunaa.az}&quot;)

frontAngle=sunaa.az-145*u.deg

if(frontAngle&lt;0): frontAngle=frontAngle+360*u.deg
print(f&quot;Front Angle = {frontAngle}&quot;)

if (frontAngle.is_within_bounds('270d', '360d') or frontAngle.is_within_bounds('0d', '90d')): print(&quot;front&quot;)
if (frontAngle.is_within_bounds('0d', '180d')): print(&quot;left&quot;)
if (frontAngle.is_within_bounds('90d', '270d')): print(&quot;back&quot;)
if (frontAngle.is_within_bounds('180d', '360d')): print(&quot;right&quot;)
</code></pre>

CC BY-SA 4.0
2022-04-26T20:46:57.213Z
Thanks! It's not clear to me how to check for the case where it "passes through zero". Can you modify the above code with four if statements showing how to check for front/left/right/back?
cayblood
CC BY-SA 4.0
2022-04-26T21:13:19.633Z
Never mind, I think I understand now. I just suggested an edit to your code. Let me know if that is what you were thinking.
cayblood
CC BY-SA 4.0
2022-04-26T23:59:49.200Z
Yep, that's it.
Greg Miller
CC BY-SA 4.0
2022-04-27T09:41:00.730Z
Maybe change those 4 final `if` statements into an `if... elif ... else` block.
PM 2Ring
CC BY-SA 4.0
2022-04-27T13:04:25.827Z
You don't want these to be elif's because there's overlap.
Greg Miller
CC BY-SA 4.0
2022-04-25T15:15:06.467Z
2022-04-26T23:58:58.010Z
2022-04-26T23:58:58.010Z
Greg Miller
<p>I recently installed motorized solar blinds on various windows in our house to reduce solar gain during the summer months. I have integrated the blinds with my hubitat home automation system, and am writing a python script to control the blinds. I've got lat/lon coordinates for my house, and I've determined that my house faces 145 degrees SE. I'm trying to figure out how to use astropy to determine which side(s) of the house the sun is shining on (if any) at the current time. My house shape is basically square, so the windows are basically either parallel or perpendicular to the direction the house faces.</p>
<p>I've found <a href="https://stackoverflow.com/questions/47663082/how-can-we-compute-solar-position-at-a-given-place-on-a-given-day-and-time">this example</a> for getting the solar zenith. I'm assuming I need to use something like this example, plus the azimuth, adjusted somehow for the direction my house faces, to figure out which windows are getting sun at a given time, but I'm still working out the math. Thanks in advance for your help.</p>

<the-sun><python><astropy>
Determining when the sun shines on different sides of a building using astropy
cayblood
# Determining when the sun shines on different sides of a building using astropy

(Asked by: username_0 on 2022-04-25)

I recently installed motorized solar blinds on various windows in our house to reduce solar gain during the summer months. I have integrated the blinds with my hubitat home automation system, and am writing a python script to control the blinds. I've got lat/lon coordinates for my house, and I've determined that my house faces 145 degrees SE. I'm trying to figure out how to use astropy to determine which side(s) of the house the sun is shining on (if any) at the current time. My house shape is basically square, so the windows are basically either parallel or perpendicular to the direction the house faces.

I've found [this example](https://stackoverflow.com/questions/47663082/how-can-we-compute-solar-position-at-a-given-place-on-a-given-day-and-time) for getting the solar zenith. I'm assuming I need to use something like this example, plus the azimuth, adjusted somehow for the direction my house faces, to figure out which windows are getting sun at a given time, but I'm still working out the math. Thanks in advance for your help.

**Tags:** the-sun, python, astropy


---

## 1 answers

### Answer by username_1 on 2022-04-25. Score: 3 | Accepted answer

The code below will give you the alt-az coordinates for any specified location for any specified time. From there, I think the easiest method to determine which side of the house it will hit is to subtract 145deg from the AZ, so that 0 means it's hitting the front dead on. I'll call this the "front angle".

If the front angle is between 270 and 90 (passing through 0), it's hitting the front. Between 0 and 180 the left, between 90 and 270 the back, and between 180 and 0 the right.

You'll also want to validate the sun's altitude is > 0

```
#!/usr/bin/python
import astropy.units as u
from astropy.time import Time
from astropy.coordinates import EarthLocation, AltAz
from astropy.coordinates import get_sun

louisville = EarthLocation.from_geodetic(-85.7636,38.2464000,0)
utcoffset = -4*u.hour  # Eastern Daylight Time
time = Time('2012-7-12 20:00:00') - utcoffset
aaframe=AltAz(obstime=time,location=louisville)

sun=get_sun(time)

sunaa=sun.transform_to(aaframe)
print(f"Sun Alt, Az = {sunaa.alt} {sunaa.az}")

frontAngle=sunaa.az-145*u.deg

if(frontAngle<0): frontAngle=frontAngle+360*u.deg
print(f"Front Angle = {frontAngle}")

if (frontAngle.is_within_bounds('270d', '360d') or frontAngle.is_within_bounds('0d', '90d')): print("front")
if (frontAngle.is_within_bounds('0d', '180d')): print("left")
if (frontAngle.is_within_bounds('90d', '270d')): print("back")
if (frontAngle.is_within_bounds('180d', '360d')): print("right")

```

> **Comments:**
> **username_0**: Thanks! It's not clear to me how to check for the case where it "passes through zero". Can you modify the above code with four if statements showing how to check for front/left/right/back?  
> **username_0**: Never mind, I think I understand now. I just suggested an edit to your code. Let me know if that is what you were thinking.  
> **username_1**: Yep, that's it.  
> **username_2**: Maybe change those 4 final `if` statements into an `if... elif ... else` block.  
> **username_1**: You don't want these to be elif's because there's overlap.  

---

