I am trying to determine if date/times are during dawn, dusk, night, or day. I am using the maptools library's sunriset() and crepuscule() functions. All of the sunrise and sunset calculations are working, but when I have it do dawn/dusk, I get NA's for some of the calculations, while for others it gives me the time of dawn/dusk. Anyone have any ideas?
I've posted some reproducible example of my code below. Note, there doesn't seem to be a particular year, or date that this happens for!
library(lubridate)
library(maptools)
library(sp)
# Build example data set to show issues with dawn/dusk calculations;
df <- data.frame(Lon=c(-136.1740, -136.1683, -136.1670, -136.1683, -136.1775, -136.1913, -136.2070, -137.7000, -137.7022, -137.7232, -137.7332, -137.7390, -137.7434, -137.7483, -137.7554, -136.0803, -136.0835), Lat=c(57.03300, 57.08083, 57.09700, 57.13115, 57.15701, 57.17111, 57.17961, 57.84775, 57.84896, 57.85831, 57.85919, 57.85680, 57.85320, 57.84934, 57.84586, 56.90012, 56.90763), date.time=ymd_hms("2007-07-08 09:00:00", "2007-07-08 10:00:00", "2007-07-08 11:00:00", "2007-07-08 12:00:00", "2007-07-08 13:00:00", "2007-07-09 14:00:00", "2007-07-09 15:00:00", "2009-07-30 04:00:00", "2009-07-30 05:00:00", "2009-07-30 06:00:00", "2009-07-30 07:00:00", "2009-07-30 08:00:00", "2009-07-30 09:00:00", "2009-07-30 10:00:00", "2009-07-30 11:00:00","2007-07-18 06:00:00","2007-07-18 07:00:00"))
#Make a new column for local date/time (Alaska time):
df$date.time.local <- as.POSIXct(df$date.time, tz="GMT", format="%Y-%m-%d %H:%M:%S")
df$date.time.local<- format(df$date.time.local, tz="us/alaska")
df$date.time.local <- as.POSIXct(df$date.time.local, tz="us/alaska",format="%Y-%m-%d %H:%M:%S")
head(df)
#Create vector of locations in Spatial Points format:
stcalc<-subset(df, select=c("Lon","Lat"))
stcalc <- SpatialPoints(stcalc, proj4string=CRS("+proj=longlat +datum=WGS84"))
head(stcalc@coords)
#Create vector of local date/time & use it to calculate sunrise & sunset
dt_local <- as.POSIXct(df$date.time, tz="GMT", format="%Y-%m-%d %H:%M:%S")
dt_local<- format(dt_local, tz="us/alaska")
dt_local <- as.POSIXct(dt_local, tz="us/alaska",format="%Y-%m-%d %H:%M:%S")
head(dt_local)
#Calculate sunrise and sunset using local time
sr<-sunriset(stcalc, dt_local, direction = "sunrise", POSIXct.out = TRUE)
st<-sunriset(stcalc, dt_local, direction="sunset", POSIXct.out=TRUE)
# calculate dawn & dusk times with local date/time
dawn<-crepuscule(stcalc@coords, dt_local, solarDep=12, direction="dawn", POSIXct.out=TRUE) #nautical dawn = 12 degrees for angle of sun below horizon
dawn #some are NA, some are not?
dusk<-crepuscule(stcalc, dt_local, solarDep=12, direction="dusk", POSIXct.out=TRUE)
dusk # some are NA, some are not?
df$Sunrise<-sr$time
df$Sunset<-st$time
df$Dawn<-dawn$time
df$Dusk<-dusk$time
# use sunrise/sunset/dawn/dusk to determine light levels;
df$Light = ifelse(df$date.time.local < df$Dawn, "Night", ifelse(df$date.time.local < df$Sunrise, "Dawn", ifelse(df$date.time.local < df$Sunset, "Day", ifelse(df$date.time.local < df$Dusk, "Dusk", "Night"))))
df$Light = factor(df$Light)
df # here some of the light column are NA, some give a dawn/day/dusk/night calculation
Thank you in advance for any advice ... what am I missing?