Skip to content

Commit 34dda92

Browse files
committed
add sunrise
1 parent 60500fe commit 34dda92

5 files changed

Lines changed: 62 additions & 21 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ License: MIT + file LICENSE
2828
Encoding: UTF-8
2929
LazyData: true
3030
Roxygen: list(markdown = TRUE)
31-
RoxygenNote: 7.3.2
31+
RoxygenNote: 7.3.3
3232
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export(soil_class)
129129
export(soil_heat_flux)
130130
export(solve_goal)
131131
export(suncalc)
132+
export(sunrise)
132133
export(theta)
133134
export(theta_wet)
134135
export(theta_wet_bolton)

R/cal_sun_angle.R

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' @name cal_ssd
32
#' @title sunset hour angle
43
#' @description Calculating sunset hour angle (`ws`) according to Allen Eq. 25.
@@ -31,21 +30,19 @@ cal_ssd <- function(lat, J) cal_sunset_angle(lat, J) %>% ws2ssd()
3130

3231
# 赤纬角
3332
#' Sun Declination angle
34-
#'
33+
#'
3534
#' @importFrom lubridate yday dhours dminutes
36-
#' @export
35+
#' @export
3736
get_sigma <- function(J) {
3837
if (inherits(J, "Date")) J %<>% yday()
3938
sigma <- 0.409 * sin(2 * pi / 365 * J - 1.39)
4039
rad2deg(sigma)
4140
}
4241

43-
local2UTC <- function() {
44-
}
4542

4643
deltaT_UTC <- function(lon, timeZone = 8) {
4744
lon_center <- timeZone * 15
48-
delta_lon <- (lon - 120)
45+
delta_lon <- (lon_center - lon)
4946
delta_minute <- round(delta_lon / 15 * 60, 2)
5047
dminutes(delta_minute)
5148
}
@@ -60,8 +57,16 @@ get_localtime <- function(time, lon, timeZone = 8) {
6057
time + deltaT_UTC(lon, timeZone)
6158
}
6259

60+
utc2local <- function(time, lon, timeZone = 8) {
61+
time + deltaT_UTC(lon, timeZone)
62+
}
63+
64+
local2utc <- function(time, lon, timeZone = 8) {
65+
time - deltaT_UTC(lon, timeZone)
66+
}
67+
6368
# 太阳高度角
64-
#' @export
69+
#' @export
6570
SunAngle_Elevation <- function(lat, sigma, omega) {
6671
lat %<>% deg2rad()
6772
sigma %<>% deg2rad()
@@ -73,7 +78,7 @@ SunAngle_Elevation <- function(lat, sigma, omega) {
7378
round(2)
7479
}
7580

76-
#' @export
81+
#' @export
7782
SunAngle_Azimuth <- function(lat, sigma, omega) {
7883
lat %<>% deg2rad()
7984
sigma %<>% deg2rad()
@@ -95,23 +100,28 @@ SunAngle_Azimuth <- function(lat, sigma, omega) {
95100
fprintf <- function(fmt, ...) cat(sprintf(fmt, ...))
96101

97102
#' suncalc
98-
#'
103+
#'
99104
#' The default location is ZuoLing.
100-
#'
105+
#'
106+
#' @examples
107+
#' suncalc(Sys.Date())
108+
#'
101109
#' @export
102-
suncalc <- function(time, lon = 114.6053, lat = 30.49694, ...,
103-
year = year(Sys.time()),
104-
verbose = TRUE)
110+
suncalc <- function(
111+
time, lon = 114.6053, lat = 30.49694, ...,
112+
year = year(Sys.time()),
113+
verbose = TRUE
114+
)
105115
{
106116
if (is.integer(time)) {
107-
J = time
108-
time_str = sprintf("%d%03d", year, J)
117+
J <- time
118+
time_str <- sprintf("%d%03d", year, J)
109119
time <- as.Date(time_str, "%Y%j")
110120
}
111121
J <- yday(time)
112122
angle_sigma <- get_sigma(J)
113123

114-
delta_minute = deltaT_UTC(lon, timeZone = 8)
124+
delta_minute <- deltaT_UTC(lon, timeZone = 8)
115125

116126
# 我们这里比北京时间晚了21.6min
117127
time_local <- get_localtime(time, lon)
@@ -145,8 +155,29 @@ suncalc <- function(time, lon = 114.6053, lat = 30.49694, ...,
145155
fprintf("%-14s: %-10s\n", "日出时间(UTC8) ", time_begin - delta_minute)
146156
fprintf("%-14s: %-10s\n", "日落时间(UTC8) ", time_end - delta_minute)
147157
}
148-
158+
149159
data.table(
150160
angle_elev, angle_azimuth, angle_sigma, delta_minute,
151-
time_local, time_begin, time_end)
161+
time_local, time_begin, time_end
162+
)
163+
}
164+
165+
#' sunrise
166+
#'
167+
#' @examples
168+
#' sunrise(Sys.Date())
169+
#'
170+
#' @rdname suncalc
171+
#' @export
172+
sunrise <- function(date = Sys.Date(), lon = 80., lat = 30., timeZone = 8) {
173+
ws <- cal_sunset_angle(lat, yday(date))
174+
ssd <- ws2ssd(ws)
175+
176+
time_noon <- format(date, "%Y-%m-%d 12:00:00") %>%
177+
as.POSIXct() %>%
178+
utc2local(lon = lon, timeZone = timeZone)
179+
180+
time_beg <- time_noon - dhours(ssd / 2)
181+
time_end <- time_noon + dhours(ssd / 2)
182+
data.table(date, time_noon, time_beg, time_end)
152183
}

man/cal_Rsi.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/suncalc.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)