我正在尝试确定日期/时间是在黎明,黄昏,夜晚还是白天。我正在使用maptools库的Sunriset()和crepuscule()函数。 所有的日出和日落计算都可以正常工作,但是当我进行黎明/黄昏计算时,对于某些计算,我会得到NA,而对于其他计算,它给出了黎明/黄昏的时间。 有人有想法么?
我在下面发布了一些可复制的代码示例。请注意,似乎没有特定的年份或日期!
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.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,format="%Y-%m-%d %H:%M:%S")
dt_local<- format(dt_local,tz="us/alaska")
dt_local <- as.POSIXct(dt_local,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,direction="sunset",POSIXct.out=TRUE)
# calculate dawn & dusk times with local date/time
dawn<-crepuscule(stcalc@coords,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,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
在此先感谢您提供任何建议...我想念的是什么?