Skip to content

Commit

Permalink
Refactoring project into package.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeslabreche committed Nov 15, 2019
1 parent 00549ea commit d1ebd4d
Show file tree
Hide file tree
Showing 165 changed files with 2,782 additions and 1,229 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
^data-raw$
17 changes: 17 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Package: mars
Title: Mars Solar Radiation
Version: 0.0.0.9000
Authors@R:
person(given = "Georges",
family = "Labreche",
role = c("aut", "cre"),
email = "[email protected]",
comment = structure("YOUR-ORCID-ID", .Names = "ORCID"))
Description: A set of functions to calculate solar irradiance and insolation on Mars horizontal and inclined surfaces.
License: What license it uses
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.0
Suggests:
testthat,
covr
49 changes: 49 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by roxygen2: do not edit by hand

export(G_b)
export(G_bh)
export(G_bi)
export(G_dh)
export(G_di)
export(G_h)
export(G_i)
export(G_ob)
export(G_obh)
export(H_ali)
export(H_bi)
export(H_dh)
export(H_di)
export(H_h)
export(H_hbh)
export(H_i)
export(H_obh)
export(I_ali)
export(I_bh)
export(I_bi)
export(I_dh)
export(I_di)
export(I_h)
export(I_i)
export(I_obh)
export(Z)
export(albedo)
export(constrain_solar_time_range)
export(declination)
export(f)
export(f_all_Zs)
export(f_all_taus)
export(is_irradiated)
export(is_polar_day)
export(is_polar_night)
export(sunrise)
export(sunrise_for_horizontal_surface)
export(sunrise_for_inclined_surface)
export(sunrise_for_inclined_surface_oriented_east)
export(sunrise_for_inclined_surface_oriented_equator)
export(sunrise_for_inclined_surface_oriented_west)
export(sunset)
export(sunset_for_horizontal_surface)
export(sunset_for_inclined_surface)
export(sunset_for_inclined_surface_oriented_east)
export(sunset_for_inclined_surface_oriented_equator)
export(sunset_for_inclined_surface_oriented_west)
19 changes: 19 additions & 0 deletions R/G_ali.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Title
#'
#' @param Ls
#' @param phi
#' @param longitude
#' @param T_s
#' @param Z
#' @param tau
#' @param al
#' @param beta
#' @param nfft
#'
#' @return
G_ali = function(Ls, phi, longitude, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, al=albedo(latitude=phi, longitude=longitude, tau=tau), beta, nfft){

Gali = al * Gh_eq(Ls=Ls, phi=phi, longitude=longitude, Z=Z, tau=tau, al=al, nfft=nfft) * sin((beta*pi/180) / 2)^2
return(Gali)
}

29 changes: 15 additions & 14 deletions functions/G_b.R → R/G_b.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@
# Appelbaum, Joseph & Flood, Dennis. (1990). Solar radiation on Mars. Solar Energy. 45. 353–363. 10.1016/0038-092X(90)90156-7.
# https://ntrs.nasa.gov/?R=19890018252

library(here)

# Equation 4 (1990): Beam irridiance at the top of the Martian atmosphere [W/m2].
Gob_eq = dget(here("functions", "G_ob.R"))

# Equation 6: Zenith angle of the incident solar radiation [deg].
Z_eq = dget(here("functions", "Z.R"))

# Check if there is irradiance based on the givent moment.
is_irradiated = dget(here("utils", "is_irradiated.R"))

# Equation 14 (1990): Beam irradiance on Mars surface [W/m2]
# Ls - Areocentric Longitude.
# Z - Sun Zenith Angle.
# tau - Optical Depth.
function(Ls, phi=NULL, T_s=NULL, Z=Z_eq(Ls, T_s, phi, nfft), tau, nfft){
if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, Z=Z, nfft=nfft)){
#' Title
#'
#' @param Ls
#' @param phi
#' @param T_s
#' @param z
#' @param tau
#' @param nfft
#'
#' @return
#' @export
G_b = function(Ls, phi=NULL, T_s=NULL, z=Z(Ls, T_s, phi, nfft), tau, nfft){

if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, z=z, nfft=nfft)){
return(0)

}else{
Gob_eq(Ls) * exp(-tau / cos(Z*pi/180))
G_ob(Ls) * exp(-tau / cos(z*pi/180))
}
}
31 changes: 17 additions & 14 deletions functions/G_bh.R → R/G_bh.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@

# FIXME: Update this function so that it figures out if its a polar night or day.

library(here)

# Equation 4: Beam irridiance at the top of the Martian atmosphere [W/m2].
Gob_eq = dget(here("functions", "G_ob.R"))

# Equation 6: Zenith angle of the incident solar radiation [deg].
Z_eq = dget(here("functions", "Z.R"))

# Check if there is irradiance based on the givent moment.
is_irradiated = dget(here("utils", "is_irradiated.R"))

# Equation 18: Beam irradiance on Mars horizontal surface [W/m2].
#
# Ls - Areocentric longitude [deg].
Expand All @@ -26,11 +15,25 @@ is_irradiated = dget(here("utils", "is_irradiated.R"))
# tau - Optical depth.
# al - NOT NEEDED - Included for looping convenience with other functions.
# nfft - Net flux calculation type.
function(Ls, phi, longitude=NULL, T_s, Z=Z_eq(Ls, T_s, phi, nfft), tau, al=NULL, nfft){
if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, Z=Z, nfft=nfft)){
#' Title
#'
#' @param Ls
#' @param phi
#' @param longitude
#' @param T_s
#' @param z
#' @param tau
#' @param al
#' @param nfft
#'
#' @return
#' @export
G_bh = function(Ls, phi, longitude=NULL, T_s, z=Z(Ls, T_s, phi, nfft), tau, al=NULL, nfft){

if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, z=z, nfft=nfft)){
return(0)

}else{
Gob_eq(Ls) * cos(Z * pi/180) * exp(-tau / cos(Z*pi/180))
G_ob(Ls) * cos(z * pi/180) * exp(-tau / cos(z*pi/180))
}
}
48 changes: 23 additions & 25 deletions functions/G_bi.R → R/G_bi.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@

# FIXME: Update this function so that it figures out if its a polar night or day.

library(here)

# Equation 4 (1990): Beam irridiance at the top of the Martian atmosphere [W/m2].
Gob_eq = dget(here("functions", "G_ob.R"))

# Equation 6 (1990): Zenith angle of the incident solar radiation [deg].
Z_eq = dget(here("functions", "Z.R"))

# Equation 14 (1990): Direct beam irradiance on the Mars surface normal to the solar rays [W/m2].
Gb_eq = dget(here("functions", "G_b.R"))

# Equation 7 (1990): The declination angle.
source(here("utils", "declination.R"))


function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, gamma_c, nfft){
#' Title
#'
#' @param Ls
#' @param phi
#' @param T_s
#' @param z
#' @param tau
#' @param beta
#' @param gamma_c
#' @param nfft
#'
#' @return
#' @export
G_bi = function(Ls, phi, T_s, z=Z(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, gamma_c, nfft){

if(gamma_c > 180 || gamma_c < -180){
stop("Surface azimuth angle gamma_c must between -180 and 180 degress with zero south, east negative, and west positive.")
Expand Down Expand Up @@ -53,7 +51,7 @@ function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, ga
# Equation 6 (1993): Solar Azimuth Angle [deg]
x = sin(phi*pi/180) * cos(delta) * cos(omega_deg*pi/180)
y = cos(phi*pi/180) * sin(delta)
z = sin(Z*pi/180)
z = sin(z*pi/180)

# From (32) in (1993): It is solar noon when omega is 0 deg. This translates to gamma_s = 0 deg.
# The following operation will result in a value very close to 1 but not exactly 1 if it is
Expand All @@ -64,8 +62,8 @@ function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, ga
one = function(x){
return(
ifelse(x > 0,
ifelse(x > 1 && x < 1+1e-5, 1, x), # If x is positive.
ifelse(x < -1 && x > -1-1e-5, 1, x)) # If x is negative.
ifelse(x > 1 && x < 1+1e-5, 1, x), # If x is positive.
ifelse(x < -1 && x > -1-1e-5, 1, x)) # If x is negative.
)
}

Expand All @@ -76,8 +74,8 @@ function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, ga
gamma_s = acos(op) * 180/pi # [deg]

# Alternatively, Equation 7 (1993): Solar Azimuth Angle [deg]
# x = sin(phi*pi/180) * cos(Z*pi/180) - sin(delta)
# y = cos(phi*pi/180) * sin(Z*pi/180)
# x = sin(phi*pi/180) * cos(z*pi/180) - sin(delta)
# y = cos(phi*pi/180) * sin(z*pi/180)
#
# gamma_s = acos(x / y) * 180/pi # [deg]

Expand All @@ -91,13 +89,13 @@ function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, ga
#TODO: Double check this, why wouldn't the paper use squared instead of multiply the by the same value?
# Check with (24) in (1993).
j = sin(gamma_s * pi/180) * sin(gamma_s * pi/180)
teta = acos(sin(Z * pi/180) * (i + j))# [rad]
teta = acos(sin(z * pi/180) * (i + j))# [rad]

}else{
# (13) in (1993): Inclined surface.
# (27) in (1993): Surface facing the equator.
i = cos(beta * pi/180) * cos(Z * pi/180)
j = sin(beta * pi/180) * sin(Z * pi/180) * cos((gamma_s - gamma_c) * pi/180) # Does not matter when beta = 0 because it leads to j = 0.
i = cos(beta * pi/180) * cos(z * pi/180)
j = sin(beta * pi/180) * sin(z * pi/180) * cos((gamma_s - gamma_c) * pi/180) # Does not matter when beta = 0 because it leads to j = 0.
teta = acos(i + j) # [rad]
}

Expand All @@ -107,7 +105,7 @@ function(Ls, phi, T_s, Z=Z_eq(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, beta, ga
# Sun Angle of Incidence [rad] on an inclined surface.
teta = sun_angle_of_incidence()

Gbi = Gb_eq(Ls=Ls, Z=Z, tau=tau) * cos(teta)
Gbi = G_b(Ls=Ls, z=z, tau=tau) * cos(teta)

return(Gbi)
}
38 changes: 18 additions & 20 deletions functions/G_dh.R → R/G_dh.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@

# FIXME: Update this function so that it figures out if its a polar night or day.

library(here)

# Equation 17 (1990): Global irradiance on Mars horizontal surface [W/m2].
Gh_eq = dget(here("functions", "G_h.R"))

# Equation 6: Zenith angle of the incident solar radiation [deg].
Z_eq = dget(here("functions", "Z.R"))

# Equation 18 (1990): Beam irradiance on Mars horizontal surface [W/m2].
Gbh_eq = dget(here("functions", "G_bh.R"))

# Check if there is irradiance based on the givent moment.
is_irradiated = dget(here("utils", "is_irradiated.R"))

source(here("functions", "albedo.R"))

# Equation 16 (1990): The solar irradiance components on a horizontal Martian surface [W/m2].
#
# Ls - Areocentric longitude [deg].
Expand All @@ -35,12 +19,26 @@ source(here("functions", "albedo.R"))
# - 1 for f_89.
# - 2 for f_90.
# - 3 for f_analytical.
function(Ls, phi, longitude, T_s=NULL, Z=Z_eq(Ls, T_s, phi, nfft), tau, al=albedo(latitude=phi, longitude=longitude, tau=tau), nfft){
if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, Z=Z, nfft=nfft)){
#' Title
#'
#' @param Ls
#' @param phi
#' @param longitude
#' @param T_s
#' @param z
#' @param tau
#' @param al
#' @param nfft
#'
#' @return
#' @export
G_dh = function(Ls, phi, longitude, T_s=NULL, z=Z(Ls, T_s, phi, nfft), tau, al=albedo(latitude=phi, longitude=longitude, tau=tau), nfft){

if(!is_irradiated(Ls=Ls, phi=phi, T_s=T_s, z=z, nfft=nfft)){
return(0)

}else{
G_dh = Gh_eq(Ls=Ls, phi=phi, longitude=longitude, T_s=T_s, Z=Z, tau=tau, al=al, nfft=nfft) - Gbh_eq(Ls=Ls, phi=phi, T_s=T_s, Z=Z, tau=tau, nfft=nfft)
return(G_dh)
Gdh = G_h(Ls=Ls, phi=phi, longitude=longitude, T_s=T_s, z=z, tau=tau, al=al, nfft=nfft) - G_bh(Ls=Ls, phi=phi, T_s=T_s, z=z, tau=tau, nfft=nfft)
return(Gdh)
}
}
19 changes: 19 additions & 0 deletions R/G_di.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Title
#'
#' @param Ls
#' @param phi
#' @param longitude
#' @param T_s
#' @param z
#' @param tau
#' @param al
#' @param beta
#' @param nfft
#'
#' @return
#' @export
G_di = function(Ls, phi, longitude, T_s, z=Z(Ls=Ls, T_s=T_s, phi=phi, nfft=nfft), tau, al=albedo(latitude=phi, longitude=longitude, tau=tau), beta, nfft){

G_di = Gdh_eq(Ls=Ls, phi=phi, longitude=longitude, z=z, tau=tau, al=al, nfft=nfft) * cos((beta*pi/180) / 2)^2
return(G_di)
}
Loading

0 comments on commit d1ebd4d

Please sign in to comment.