Skip to content

Instantly share code, notes, and snippets.

@monkeycycle
Last active July 5, 2018 02:33
Show Gist options
  • Save monkeycycle/d94cfc97865e0a386ffca36395efffbc to your computer and use it in GitHub Desktop.
Save monkeycycle/d94cfc97865e0a386ffca36395efffbc to your computer and use it in GitHub Desktop.
##################################################
# Get sunrise, sunset and dusk times for a location
##################################################
days <- seq(from = as.POSIXct("2017-01-01"), to = as.POSIXct("2017-12-31"), by = "days")
# coordinates for Toronto
lat <- 43.6532
lon <- -79.3832
coord <- matrix(c(lon, lat), nrow = 1)
# Daily sunrise and sunet times
df_sunrise_sunset <- data.frame(
date = days,
sunrise <- sunriset(coord, days, direction = "sunrise", POSIXct.out = TRUE)$time,
sunset <- sunriset(coord, days, direction = "sunset", POSIXct.out = TRUE)$time,
dawn_civil <- crepuscule(coord, days, solarDep = 6, direction = "dawn", POSIXct.out = TRUE)$time,
dusk_civil <- crepuscule(coord, days, solarDep = 6, direction = "dusk", POSIXct.out = TRUE)$time,
dawn_naut <- crepuscule(coord, days, solarDep = 12, direction = "dawn", POSIXct.out = TRUE)$time,
dusk_naut <- crepuscule(coord, days, solarDep = 12, direction = "dusk", POSIXct.out = TRUE)$time,
dawn_astro <- crepuscule(coord, days, solarDep = 18, direction = "dawn", POSIXct.out = TRUE)$time,
dusk_astro <- crepuscule(coord, days, solarDep = 18, direction = "dusk", POSIXct.out = TRUE)$time
)
# Clean up time columns
df_sunrise_sunset$sunrise <- hms(as.character(df_sunrise_sunset$sunrise, format = "%H:%M:%S"))
df_sunrise_sunset$sunset <- hms(as.character(df_sunrise_sunset$sunset, format = "%H:%M:%S"))
df_sunrise_sunset$dawn_civil <- hms(as.character(df_sunrise_sunset$dawn_civil, format = "%H:%M:%S"))
df_sunrise_sunset$dusk_civil <- hms(as.character(df_sunrise_sunset$dusk_civil, format = "%H:%M:%S"))
df_sunrise_sunset$dawn_naut <- hms(as.character(df_sunrise_sunset$dawn_naut, format = "%H:%M:%S"))
df_sunrise_sunset$dusk_naut <- hms(as.character(df_sunrise_sunset$dusk_naut, format = "%H:%M:%S"))
df_sunrise_sunset$dawn_astro <- hms(as.character(df_sunrise_sunset$dawn_astro, format = "%H:%M:%S"))
df_sunrise_sunset$dusk_astro <- hms(as.character(df_sunrise_sunset$dusk_astro, format = "%H:%M:%S"))
df_sunrise_sunset <- df_sunrise_sunset %>%
dplyr:::select(date, sunrise, sunset, dawn_civil, dusk_civil, dawn_naut, dusk_naut, dawn_astro, dusk_astro)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment