Giter VIP home page Giter VIP logo

daybreak's Introduction

Project Status: Active – The project has reached a stable, usable state and is being actively developed. Signed by Signed commit % Linux build Status
Minimal R Version License

daybreak

Compute Sun Rise/Set Times, Start/End of Twilight, and the Length of the Day at Any Date and Latitude

Description

A wrapper for Paul Schlyter’s C-based library for computing sunrise, sunset, twilight start and end, plus the length of day for a given data and coordinates.

What’s Inside The Tin

The following functions are implemented:

  • astronomical_twilight: Astronomical twilight
  • civil_twilight: Civil twilight
  • day_astronomical_twilight_length: Length of astronomical twilight
  • day_civil_twilight_length: Length of civil twilight
  • day_length: Length of day
  • day_nautical_twilight_length: Length of nautical twilight
  • nautical_twilight: Nautical twilight
  • sun_rise_set: Sun rise/set times

Installation

install.packages("daybreak", repos = "https://cinc.rud.is")
# or
remotes::install_git("https://git.rud.is/hrbrmstr/daybreak.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/daybreak")
# or
remotes::install_gitlab("hrbrmstr/daybreak")
# or
remotes::install_bitbucket("hrbrmstr/daybreak")
# or
remotes::install_github("hrbrmstr/daybreak")

NOTE: To use the ‘remotes’ install options you will need to have the {remotes} package installed.

Usage

library(daybreak)

# current version
packageVersion("daybreak")
## [1] '0.1.0'

Near me:

day_length("2019-12-31", -70.8636, 43.2683)
## [1] 9.031444
day_civil_twilight_length("2019-12-31", -70.8636, 43.2683)
## [1] 10.10834
day_nautical_twilight_length("2019-12-31", -70.8636, 43.2683)
## [1] 11.30569
day_astronomical_twilight_length("2019-12-31", -70.8636, 43.2683)
## [1] 12.45998
sun_rise_set("2019-12-31", lon = -70.8636, lat = 43.2683)
## $rise
## [1] 12.25761
## 
## $set
## [1] 21.28906
civil_twilight("2019-12-31", -70.8636, 43.2683)
## $start
## [1] 11.71917
## 
## $end
## [1] 21.82751
nautical_twilight("2019-12-31", -70.8636, 43.2683)
## $start
## [1] 11.12049
## 
## $end
## [1] 22.42618
astronomical_twilight("2019-12-31", -70.8636, 43.2683)
## $start
## [1] 10.54335
## 
## $end
## [1] 23.00332

Tromsø, Norway (Winter)

day_length("2019-12-31", 18.9553, 69.6492)
## [1] 0
day_civil_twilight_length("2019-12-31", 18.9553, 69.6492)
## [1] 4.613116
day_nautical_twilight_length("2019-12-31", 18.9553, 69.6492)
## [1] 8.000323
day_astronomical_twilight_length("2019-12-31", 18.9553, 69.6492)
## [1] 10.5871
sun_rise_set("2019-12-31", 18.9553, 69.6492)
## $rise
## [1] NA
## 
## $set
## [1] NA
civil_twilight("2019-12-31", 18.9553, 69.6492)
## $start
## [1] 8.476866
## 
## $end
## [1] 13.08998
nautical_twilight("2019-12-31", 18.9553, 69.6492)
## $start
## [1] 6.783262
## 
## $end
## [1] 14.78359
astronomical_twilight("2019-12-31", 18.9553, 69.6492)
## $start
## [1] 5.489872
## 
## $end
## [1] 16.07698

Tromsø, Norway (Summer)

day_length("2019-06-01", 18.9553, 69.6492)
## [1] 24
day_civil_twilight_length("2019-06-01", 18.9553, 69.6492)
## [1] 24
day_nautical_twilight_length("2019-06-01", 18.9553, 69.6492)
## [1] 24
day_astronomical_twilight_length("2019-06-01", 18.9553, 69.6492)
## [1] 24
sun_rise_set("2019-06-01", 18.9553, 69.6492)
## $rise
## [1] NA
## 
## $set
## [1] NA
civil_twilight("2019-06-01", 18.9553, 69.6492)
## $start
## [1] NA
## 
## $end
## [1] NA
nautical_twilight("2019-06-01", 18.9553, 69.6492)
## $start
## [1] NA
## 
## $end
## [1] NA
astronomical_twilight("2019-06-01", 18.9553, 69.6492)
## $start
## [1] NA
## 
## $end
## [1] NA

daybreak Metrics

Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
C 3 0.33 267 0.66 110 0.60 245 0.60
R 4 0.44 65 0.16 16 0.09 84 0.20
C/C++ Header 1 0.11 39 0.10 35 0.19 44 0.11
Rmd 1 0.11 32 0.08 21 0.12 37 0.09

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

daybreak's People

Contributors

hrbrmstr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

daybreak's Issues

Enhancement idea: Adjust sun rise/set hours to match timezone and return `POSIXlt` object

Thanks for the package! I had two ideas that could be a helpful enhancement for sun_rise_set() along with astronomical_twilight, civil_twilight, and nautical_twilight.

  • The first idea is to match the hours returned to the time zone of the date provided (if the date provided includes a time zone).
  • The second idea is to optionally return a "POSIXlt" object using the same date as the one provided but the time adjusted to match the sunrise and sunset times (or twilight start and end times).

If you're interested, I put together a helper function that gets this working although I'm open to trying other approaches if you have any suggestions.

sun_rise_set <- function(date, lon, lat, return = "hours", adj_tz = TRUE) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  times <- .Call("r_sun_rise_set", date$year+1900L, date$mon+1L, date$mday, lon, lat)
  
  if (adj_tz) {
    times$rise <- adjust_tz(date, times$rise, return)
    times$set <- adjust_tz(date, times$set, return)
  }
  times
}

adjust_tz <- function(date, x, return) {
  if ("POSIXt" %in% class(date)) {
    x <- x - as.double(difftime(date, as.POSIXct(date, tz = "UTC"), units = "hours"))
  }
  
  if (return == "hours") {
    x
  } else if (return == "POSIXlt") {
    day <- strftime(date, format="%Y-%m-%d")
    tz <- strftime(date, format="%Z")
    hr <- floor(x)
    min <- (x %% 1) * 60
    sec <- floor((min %% 1) * 60)
    min <- floor(min)
    as.POSIXlt(paste(day, paste0(c(hr, min, sec), collapse = ":"), tz), format = "%Y-%m-%d %H:%M:%S")
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.