Functions for calculating the timing of solar positions by means of function sun_angles, given geographical coordinates and dates. They can be also used to find the time for an arbitrary solar elevation between 90 and -90 degrees by supplying "twilight" angle(s) as argument.

day_night(date = lubridate::now(tzone = "UTC"),
  tz = lubridate::tz(date), geocode = data.frame(lon = 0, lat = 51.5,
  address = "Greenwich"), twilight = "none", unit.out = "hours")

day_night_fast(date, tz, geocode, twilight, unit.out)

noon_time(date = lubridate::today(), tz = Sys.timezone(),
  geocode = data.frame(lon = 0, lat = 51.5, address = "Greenwich"),
  twilight = "none", unit.out = "datetime")

sunrise_time(date = lubridate::today(), tz = Sys.timezone(),
  geocode = data.frame(lon = 0, lat = 51.5, address = "Greenwich"),
  twilight = "sunlight", unit.out = "datetime")

sunset_time(date = lubridate::today(), tz = Sys.timezone(),
  geocode = data.frame(lon = 0, lat = 51.5, address = "Greenwich"),
  twilight = "sunlight", unit.out = "datetime")

day_length(date = lubridate::now(), tz = "UTC",
  geocode = data.frame(lon = 0, lat = 51.5, address = "Greenwich"),
  twilight = "sunlight", unit.out = "hours")

night_length(date = lubridate::now(), tz = "UTC",
  geocode = data.frame(lon = 0, lat = 51.5, address = "Greenwich"),
  twilight = "sunlight", unit.out = "hours")

Arguments

date

"vector" of POSIXct times or Date objects, any valid TZ is allowed, default is current date

tz

character vector indicating time zone to be used in output.

geocode

data frame with one or more rows and variables lon and lat as numeric values (degrees). If present, address will be copied to the output.

twilight

character string, one of "none", "civil", "nautical", "astronomical", or a numeric vector of length one, or two, giving solar elevation angle(s) in degrees (negative if below the horizon).

unit.out

character string, One of "datetime", "day", "hour", "minute", or "second".

Value

A data.frame with variables day, tz, twilight.rise, twilight.set, longitude, latitude, address, sunrise, noon, sunset, daylength, nightlength.

noon_time, sunrise_time and sunset_time return a vector of POSIXct times

day_length and night_length return numeric a vector giving the length in hours

Details

Twilight names are interpreted as follows. "none": solar elevation = 0 degrees. "refraction": solar elevation = 0 degrees + refraction correction. "sunlight": upper rim of solar disk corrected for refraction. "civil": -6 degrees, "naval": -12 degrees, and "astronomical": -18 degrees. Unit names for output are as follows: "hours" times for sunrise and sunset are returned as times-of-day in hours since midnight. "date" or "datetime" return the same times as datetime objects with TZ set (this is much slower than the "hours"). Day length and night length are returned as numeric values expressed in hours when `"datetime"' is passed as argument to unit.out. If twilight is a numeric vector of length two, the element with index 1 is used for sunrise and that with index 2 for sunset.

Note

This function is an implementation of Meeus equations as used in NOAAs on-line web calculator, which are very precise and valid for a very broad range of dates. For sunrise and sunset the times are affected by refraction in the atmosphere, which does in turn depend on weather conditions. The effect of refraction on the apparent position of the sun is only an estimate based on "typical" conditions. The more tangential to the horizon is the path of the sun, the larger the effect of refraction is on the times of visual occlusion of the sun behind the horizon---i.e. the largest timing errors occur at high latitudes. The computation is not defined for latitudes 90 and -90 degrees, i.e. at the poles.

There exists a different R implementation of the same algorithms called "AstroCalcPureR" available as function astrocalc4r in package 'fishmethods'. Although the equations used are almost all the same, the function signatures and which values are returned differ. In particular, the present implementation splits the calculation into two separate functions, one returning angles at given instants in time, and a separate one returning the timing of events for given dates.

night_length returns the length of night-time conditions in one day (00:00:00 to 23:59:59), rather than the length of the night between two consecutive days.

Warning

Be aware that R's Date class does not save time zone metadata. This can lead to ambiguities in the current implementation as based on time instants. The argument passed to date should be of class POSIXct, in other words an instant in time, from which the correct date will be computed based on the tz argument.

See also

Examples

library(lubridate)
#> #> Attaching package: 'lubridate'
#> The following object is masked from 'package:base': #> #> date
my.geocode <- data.frame(lat = 60, lon = 25) day_night(ymd("2015-05-30"), geocode = my.geocode)
#> day tz twilight.rise twilight.set longitude latitude address sunrise #> 1 2015-05-30 UTC 0 0 25 60 <NA> 1.368398 #> noon sunset daylength nightlength #> 1 10.29246 19.21651 17.84812 6.151883
day_night(ymd("2015-05-30") + days(1:10), geocode = my.geocode, twilight = "civil")
#> day tz twilight.rise twilight.set longitude latitude address #> 1 2015-05-31 UTC -6 -6 25 60 <NA> #> 2 2015-06-01 UTC -6 -6 25 60 <NA> #> 3 2015-06-02 UTC -6 -6 25 60 <NA> #> 4 2015-06-03 UTC -6 -6 25 60 <NA> #> 5 2015-06-04 UTC -6 -6 25 60 <NA> #> 6 2015-06-05 UTC -6 -6 25 60 <NA> #> 7 2015-06-06 UTC -6 -6 25 60 <NA> #> 8 2015-06-07 UTC -6 -6 25 60 <NA> #> 9 2015-06-08 UTC -6 -6 25 60 <NA> #> 10 2015-06-09 UTC -6 -6 25 60 <NA> #> sunrise noon sunset daylength nightlength #> 1 23.80018 10.29485 20.78952 20.98934 3.010664 #> 2 23.75166 10.29736 20.84305 21.09140 2.908601 #> 3 23.70388 10.29997 20.89606 21.19219 2.807812 #> 4 23.65693 10.30269 20.94845 21.29152 2.708483 #> 5 23.61092 10.30551 21.00009 21.38918 2.610823 #> 6 23.56595 10.30842 21.05088 21.48493 2.515070 #> 7 23.52217 10.31142 21.10067 21.57851 2.421495 #> 8 23.47970 10.31450 21.14930 21.66960 2.330400 #> 9 23.43873 10.31766 21.19660 21.75787 2.242131 #> 10 23.39944 10.32090 21.24236 21.84293 2.157074
sunrise_time(ymd("2015-05-30"), geocode = my.geocode)
#> [1] "2015-05-30 04:11:55 EEST"
noon_time(ymd("2015-05-30"), geocode = my.geocode)
#> [1] "2015-05-30 13:17:32 EEST"
sunset_time(ymd("2015-05-30"), geocode = my.geocode)
#> [1] "2015-05-30 22:23:09 EEST"
day_length(ymd("2015-05-30"), geocode = my.geocode)
#> [1] 18.18727
day_length(ymd("2015-05-30"), geocode = my.geocode, unit.out = "day")
#> [1] 0.7578028