Sunrise and sunset times in R

2

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

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
    
asked by anonymous 10.11.2017 / 20:13

0 answers