This is the time of year at our house where the sun starts peeking back over Miller Hill in the early afternoon, and direct sun starts hitting the house again. There’s a week where we get sunlight in the upstairs windows, but none downstairs and it feels momentous when we finally get direct sun throughout the house again.
The position of the sun at any location on a smooth, hypothetical representation of the earth is well known, but so much of when the sun actually rises at a particular place depends on the local topography in relation to the sun in the sky. This is a particular issue when camping in the mountains because at elevation the temperatures drop quickly as soon as there is no longer direct sunlight where you are. When we’re backpacking, we use an app called PeakFinder, which does these calculations and shows you the location and estimated time the sun (and moon) will rise and set at your location.
Last summer’s campsite at Titcomb Basin in Wyoming’s Wind River Range is a good example. There were pretty much mountains in all directions.
Sunrise and Sunset
The first step in figuring out when the sun rises and sets is getting the position of the sun. We’re using the suncalc R package to calculate the solar parameters for sunrise, sunset, and solar noon (when the sun is highest in the sky).
You can click the “Code” arrow below to see the R code.
The highlighted row is the data for today, at our house. Solar noon happens just after 1 PM, and at that point the sun is 3.8 degrees above the horizon of a hypothetical, smooth geoid. Sunrise and sunset on this ellipsoid are at 10:33 and 15:30, giving us a hypothetical day length of four hours and 57 minutes.
But Miller Hill is south of our house, so if the angle between our front window and the top of Miller Hill is higher than 3.8 degrees, we won’t get any direct sun through that window at solar noon. The other issue is that this calculation assumes our elevation above the geoid is zero, so it can’t account for the height of our first floor, or what the sun’s angle will be from the second floor.
Horizon angle
Horizon angle is the angle above the horizon that comes from topography. We can get an estimate of horizon angle by analyzing the data from a digital elevation model. These are raster images where each pixel of the image contains the elevation at that location. It’s only an estimate, and on the ground each pixel represents a rectangular area several meters on a side.
The process is to read in the DEM, draw a series of lines radiating out from our location in all directions, then intersect the lines with the DEM to get a snapshot of the hills and mountains in all directions. Along each of these lines, we calculate the angle between our location (the starting point of the line) and the ground elevation. The maximum angle we find is the horizon angle for that direction at our location. We can also correct for observer height in this calculation by moving the elevation of the start point up.
Here’s a diagram of one such elevation line and the horizon angle to the highest hill. The x-axis is distance from our location (on the left), the y-axis is the elevation, and the squiggly line is the topography in that direction. If you move the base of the triangle up slightly and recalculate the angles to the ground, you can see how we can calculate horizon angle at different heights above the ground.
A few important notes about the code. The DEM needs to be converted from the latitude/longitude projection of the original data into a projection that flattens the area into a Cartesian coordinate system where the x and y coordinates are square. This allows us to create lines radiating out from our location at all angles using simple trigonometry (see the st_project_cartesian function in the code). These calculations can also be done using spherical math (see the destPoint function from the geosphere package), but we didn’t do that here.
We also perform the calculations starting 100 meters from the center location because minor elevation changes between the center location raster pixel and pixels nearby would result in the same horizon angle for unrealistic ranges of directions. I also smoothed the horizon angles slightly.
Here’s a look at the horizon angles for our house (at the ground level). The plot has north on the left (0 degrees) and right (360 degrees) sides of the plot, and south (180 degrees) in the middle.
You can see Miller Hill between 60 and 210 degrees, and the other peaks are Ester Dome (255 degrees) and Murphy Dome (330). At solar noon on today’s date, the sun is supposed to be 3.8 degrees above the horizon, and the horizon angle at 180 degrees is 2.9 degrees, so theoretically we should get direct sunlight at solar noon. The sun itself is 0.833 degrees “tall”, which means that the entire area of the sun will be just above the horizon at that time.
Here’s the horizon angles for our campsite at Titcomb Basin. Look at how steep the horizon angles are!
Sun over the horizon
Now let’s intersect the sun’s position against the horizon angle. To do that, we need to calculate the sun angles and compass directions from sunrise to sunset and plot these against the horizon angles.
Here’s the plot with the same horizon angles, and the sun’s position plotted in orange.
Code
today_sun <-getSunlightTimes(ymd("2024-01-14"),lat = lat, lon = lon,tz ="US/Alaska")sun_times <-seq( today_sun$sunrise +minutes(5), today_sun$sunset -minutes(5),15*60)sun_position <-map( sun_times,function(ts)getSunlightPosition(ts, lat =local(lat), lon =local(lon))) |>list_rbind() |>filter(altitude >0) |>mutate(altitude =rad2deg(altitude),azimuth = (rad2deg(azimuth) -180) %%360 ) |>as_tibble() ggplot(data = smoothed_horizon_angles,aes(x = azimuth, y = horizon_angle_smooth)) +theme_bw() +geom_point(data = sun_position,aes(x = azimuth, y = altitude),color ="darkorange",size =4 ) +geom_area(color ="black", fill ="grey50") +scale_x_continuous(name ="Azimuth (° from North)",breaks =seq(0, 360, 30) ) +scale_y_continuous(name ="Horizon Angle (° above horizon)",breaks =pretty_breaks(n =5) ) +labs(title ="Sunrise over Miller Hill")
From this, we can calculate when the sun is above the horizon for today’s date. The highlighted rows are where the full disc of the sun is above the horizon.