~hrbrmstr/daybreak

ref: af8ff3a5efbe1173d1a188e6d8d884c2bc809e9c daybreak/R/daybreak-wrappers.R -rw-r--r-- 4.3 KiB
af8ff3a5hrbrmstr plain C version 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' Length of day
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) length of day
#' @export
#' @examples
#' day_length("2019-12-31", -70.8636, 43.2683)
day_length <- function(date = Sys.Date(), lon, lat) {

  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)

  .Call("r_day_length", date$year+1900L, date$mon+1L, date$mday, lon, lat)

}


#' Length of civil twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) day civil twilight length
#' @export
#' @examples
#' day_length("2019-12-31", -70.8636, 43.2683)
day_civil_twilight_length <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_day_civil_twilight_length", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Length of nautical twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) day nautical twilight length
#' @export
#' @examples
#' day_nautical_twilight_length("2019-12-31", -70.8636, 43.2683)
day_nautical_twilight_length <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_day_nautical_twilight_length", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Length of astronomical twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) astronomical twilight length
#' @export
#' @examples
#' day_astronomical_twilight_length("2019-12-31", -70.8636, 43.2683)
day_astronomical_twilight_length <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_day_astronomical_twilight_length", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Sun rise/set times
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) sunrise/sunset
#' @export
#' @examples
#' sun_rise_set("2019-12-31", -70.8636, 43.2683)
sun_rise_set <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_sun_rise_set", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Civil twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) civil twilight
#' @export
#' @examples
#' civil_twilight("2019-12-31", -70.8636, 43.2683)
civil_twilight <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_civil_twilight", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Nautical twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) nautical twilight
#' @export
#' @examples
#' nautical_twilight("2019-12-31", -70.8636, 43.2683)
nautical_twilight <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_nautical_twilight", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}

#' Astronomical twilight
#'
#' @param date The date to compute the length for. An R [DateTimeClasses] object
#'     or something that can be coerced into one by [as.POSIXlt()].
#' @param lon,lat longitude & latitude
#' @return (dbl) astronomical twilight
#' @export
#' @examples
#' astronomical_twilight("2019-12-31", -70.8636, 43.2683)
astronomical_twilight <- function(date, lon, lat) {
  date <- valid_date(date)
  lon <- valid_lon(lon)
  lat <- valid_lat(lat)
  .Call("r_astronomical_twilight", date$year+1900L, date$mon+1L, date$mday, lon, lat)
}