I would like to calculate the position of the sun at a given time, latitude and longitude. I found this wonderful question and answer here: The position of the sun, a given time of day, latitude and longitude . However, when I evaluate a function, I get incorrect results. Given the quality of the answer, Iโm pretty sure that something is wrong on my part, but I ask this question as a record of an attempt to solve the problem.
Here's the function code reprinted below for convenience:
astronomersAlmanacTime <- function(x) {
If I run:
sunPosition(when = Sys.time(),lat = 43, long = -89)
Result:
elevation azimuthJ azimuthC 1 -24.56604 55.26111 55.26111
Sys.time () gives:
> Sys.time() [1] "2016-09-08 09:09:05 CDT"
It's 9am and the sun is up. Using http://www.esrl.noaa.gov/gmd/grad/solcalc/ I get an azimuth of 124 and a height of 38, which, in my opinion, is correct.
I thought maybe this is a problem with the code, but I also tried out the original SunPosition function from Josh from the answer above and got the same results. My next thought is that there is a problem with my time or time zone.
testing the winter solstice, as done in the above question, still gives the same results that they found and looks right:
testPts <- data.frame(lat = c(-41,-3,3, 41), long = c(0, 0, 0, 0)) time <- as.POSIXct("2012-12-22 12:00:00") sunPosition(when = time, lat = testPts$lat, long = testPts$long) elevation azimuthJ azimuthC 1 72.43112 359.0787 359.0787 2 69.56493 180.7965 180.7965 3 63.56539 180.6247 180.6247 4 25.56642 180.3083 180.3083
When I do the same test, but change the longitude (-89), I get a negative increase at noon.
testPts <- data.frame(lat = c(-41,-3,3, 41), long = c(-89, -89, -89, -89)) time <- as.POSIXct("2012-12-22 12:00:00 CDT") sunPosition(when = time, lat = testPts$lat, long = testPts$long) elevation azimuthJ azimuthC 1 16.060136563 107.3420 107.3420 2 2.387033692 113.3522 113.3522 3 0.001378426 113.4671 113.4671 4 -14.190786786 108.8866 108.8866