## Example 1: daily variation in sun elevation
dayplot <- function(day="2009-12-22", lat=44+39/60, lon=-(63+34/60),
location="Halifax, Nova Scotia", add=FALSE, ...)
{
t0 <- as.POSIXct(paste(day, "00:00:00"), tz="UTC")
t <- seq(from=t0, to=t0+86400, by="15 min")
a <- sunAngle(t, lat, lon)
day <- a$elevation > 0
a$azimuth[!day] <- NA
if (add) {
points(a$azimuth, a$elevation, cex=0.5*par("cex"), ...)
} else {
plot(a$azimuth, a$elevation, type='p', xlab='Azimuth',
ylab='Elevation', ylim=c(0, 90), main=location,
cex=0.5*par("cex"), ...)
}
h <- which(trunc(t,"hours")==t)
points(a$azimuth[h], a$elevation[h], pch=16, ...)
hour <- as.POSIXlt(t[h])$hour
text(a$azimuth[h], a$elevation[h], paste(hour,"h",sep=""), pos=1,
offset=0.7, cex=0.7*par("cex"), ...)
}
dayplot("2009-06-22")
dayplot("2009-12-22", add=TRUE)
dayplot(trunc(Sys.time(),"days"), add=TRUE, col="red")
## Example 2: infer location from sunrise and sunset times
rise <- as.POSIXct("2011-03-03 06:49:00", tz="UTC") + 4*3600
set <- as.POSIXct("2011-03-03 18:04:00", tz="UTC") + 4*3600
mismatch <- function(latlon)
{
sunAngle(rise, latlon[1], latlon[2])$elevation^2 +
sunAngle(set, latlon[1], latlon[2])$elevation^2
}
result <- optim(c(1,1), mismatch)
lat.hfx <- 44.65
lon.hfx <- (-63.55274)
dist <- geodDist(result$par[1], result$par[2], lat.hfx, lon.hfx)
cat(sprintf("Infer Halifax latitude %.2f and longitude %.2f; distance mismatch %.0f km",
result$par[1], result$par[2], dist))
Run the code above in your browser using DataLab