Sunrise and sunset in R

Asked

Viewed 81 times

2

I’m trying to create a script that shows the sunrise and sunset with the latitudes set, but the time is not compatible. OBS: this latitude corresponds to the time of São Paulo.

library(maptools)

library(dplyr)

dateInt <- c("2017-10-11","2017-10-01")

  lon <- -46.6821862
  lat <- -23.5977319
  timezone <- 'UTC+3'
  date <- dateInt[2]

  if (length(dateInt)==1) {
    span <- 1
  }else { 
    span <- difftime(strptime(dateInt[1], format = "%Y-%m-%d")
                 ,strptime(dateInt[2], format = "%Y-%m-%d"),units="days")

    span <- as.numeric(span)
    span <- span
  }

  lon.lat <- matrix(c(lon, lat), nrow=1)

  day <- as.POSIXct(date, tz=timezone)
  sequence <- seq(from=day, length.out=span , by="days")

  #Getting datas
  sunrise <- sunriset(lon.lat, sequence, direction="sunrise", POSIXct.out=TRUE)
  sunset <- sunriset(lon.lat, sequence, direction="sunset", POSIXct.out=TRUE)
  solar_noon <- solarnoon(lon.lat, sequence, POSIXct.out=TRUE)
  day_length <- round(as.numeric(sunset$time-sunrise$time),2)

  r <- data.frame(Data=as.Date(sunrise$time),
              Nascer=format(sunrise$time, "%H:%M"),
              `Meio Dia`=format(solar_noon$time, "%H:%M"),
              Pôr=format(sunset$time, "%H:%M"),
              `Duração(Horas)` = day_length)

  #sorting dataframe by Data
  r <- r[order(r, decreasing = TRUE),]

  r <- data.frame(lapply(r, as.character), stringsAsFactors=FALSE)

  #Removing N/A
  r <- na.omit(r)

  r
  • Why is the schedule not compatible? What time should appear?

  • Sp is UTC -3, but for the time to be really similar, I have to put UTC+3, even so, there is the delay around 30 minutes.

1 answer

0

library(suncalc)
library(dplyr)
lon <- -46.6821862
lat <- -23.5977319
timezone <- base::grep("Brazil", base::OlsonNames(), value = T)
# tz de SP = timezone[3]
# Sys.Date() = data de hoje
df <- data.frame(date = seq.Date(Sys.Date()-30, # Data de inicio 
                                 Sys.Date(),    # Data final
                                 by = 1),       # Passo
                 lat = rep(lat, 31),  # 30+1
                 lon = rep(lon, 31))

df2 <- suncalc::getSunlightTimes(data = df, 
                                 keep = c("sunrise", "sunset"), 
                                 tz   = timezone[3]) %>% 
  dplyr::rename("Nascer do sol" = sunrise, 
                "Pôr do sol"    = sunset)

__

> df2
         date       lat       lon       Nascer do sol          Pôr do sol
1  2019-03-30 -23.59773 -46.68219 2019-03-30 06:15:15 2019-03-30 18:09:38
2  2019-03-31 -23.59773 -46.68219 2019-03-31 06:15:37 2019-03-31 18:08:39
3  2019-04-01 -23.59773 -46.68219 2019-04-01 06:15:59 2019-04-01 18:07:39
4  2019-04-02 -23.59773 -46.68219 2019-04-02 06:16:21 2019-04-02 18:06:40
5  2019-04-03 -23.59773 -46.68219 2019-04-03 06:16:44 2019-04-03 18:05:42
6  2019-04-04 -23.59773 -46.68219 2019-04-04 06:17:06 2019-04-04 18:04:43
7  2019-04-05 -23.59773 -46.68219 2019-04-05 06:17:28 2019-04-05 18:03:45
8  2019-04-06 -23.59773 -46.68219 2019-04-06 06:17:51 2019-04-06 18:02:48
9  2019-04-07 -23.59773 -46.68219 2019-04-07 06:18:13 2019-04-07 18:01:50
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.