Site icon R-bloggers

Sun and moon paths in daily sky

[This article was first published on Dan Kelley Blog/R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

This blog started because I was interested in the sunrise position on the winter solstice of 2013. With the Spring equinox, I’m trying something different: plotting the paths of the sun and moon through the day.

The code shown here produces a daily graph, and I have a cron job running on a machine so that this graph is visible to anyone at my website.

Procedure

The Oce package has functions called moonAngle() and sunAngle() that do the calculations. The rest of this code sets up and graphs the results. The horizon is on the outer edge of the circle. Hours (local time) are marked along the sun and moon paths. East-west and North-south lines are shown, and they intersect at the zenith.

1
library(oce)
## Loading required package: methods
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
angles <- function(day = Sys.Date(), lon = -63.61, lat = 44.67, tz = "America/Halifax", 
    sun = TRUE) {
    tUTC <- t <- seq(as.POSIXct(paste(day, "00:00:00"), tz = tz), length.out = 240, 
        by = "6 min")
    attributes(tUTC)$tzone <- "UTC"
    a <- if (sun) 
        sunAngle(tUTC, lon = lon, lat = lat) else moonAngle(tUTC, lon = lon, lat = lat)
    invisible <- a$altitude < 0
    a$altitude[invisible] <- NA
    a$azimuth[invisible] <- NA
    list(t = t, altitude = a$altitude, azimuth = a$azimuth)
}

day <- Sys.Date()
tz <- "America/Halifax"
s <- angles()
m <- angles(sun = FALSE)
max <- 1.04 * max(c(s$altitude, m$altitude), na.rm = TRUE)

par(mar = rep(0.5, 4))
theta <- seq(0, 2 * pi, length.out = 24 * 10)
radiusx <- cos(theta)
radiusy <- sin(theta)

# Horizon and labels+lines for EW and NS
plot(radiusx, radiusy, type = "l", col = "gray", asp = 1, axes = FALSE, xlab = "", 
    ylab = "")
lines(c(-1, 1), c(0, 0), col = "gray")
lines(c(0, 0), c(-1, 1), col = "gray")
D <- 1.06
text(0, -D, "S", xpd = TRUE)  # xpd so can go in margin
text(-D, 0, "W", xpd = TRUE)
text(0, D, "N", xpd = TRUE)
text(D, 0, "E", xpd = TRUE)

## Moon
mx <- (90 - m$altitude)/90 * cos(pi/180 * (90 - m$azimuth))
my <- (90 - m$altitude)/90 * sin(pi/180 * (90 - m$azimuth))
lines(mx, my, col = "blue", lwd = 3)
t <- s$t
mlt <- as.POSIXct(sprintf("%s %02d:00:00", day, 1:24), tz = tz)
ti <- unlist(lapply(mlt, function(X) which.min(abs(X - t))))
points(mx[ti], my[ti], pch = 20, cex = 3, col = "white")
text(mx[ti], my[ti], 1:24, cex = 3/4)

## Sun
sx <- (90 - s$altitude)/90 * cos(pi/180 * (90 - s$azimuth))
sy <- (90 - s$altitude)/90 * sin(pi/180 * (90 - s$azimuth))
lines(sx, sy, col = "red", lwd = 3)
slt <- as.POSIXct(sprintf("%s %02d:00:00", day, 1:24), tz = tz)
si <- unlist(lapply(slt, function(X) which.min(abs(X - t))))
points(sx[ti], sy[ti], pch = 20, cex = 3, col = "white")
text(sx[ti], sy[ti], 1:24, cex = 3/4)

mtext(paste("Halifax NS", day, sep = "\n"), side = 3, adj = 0, line = -2)
mtext("Red sun\nBlue moon", side = 3, adj = 1, line = -2)

Resources

To leave a comment for the author, please follow the link and comment on their blog: Dan Kelley Blog/R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.