PE_Oudin.R 4.51 KB
Newer Older
1
PE_Oudin <- function(JD, Temp,
2
                     Lat, LatUnit = c("rad", "deg"),
3
                     TimeStepIn = "daily", TimeStepOut = "daily",
4
                     RunFortran = FALSE) {
5
6


7
  ## ---------- check arguments
8

9
10
11
12
13
14
15
  if (!(inherits(JD, "numeric") | inherits(JD, "integer"))) {
    stop("'JD' must be of class 'numeric'")
  }
  if (!(inherits(Temp, "numeric") | inherits(Temp, "integer"))) {
    stop("'Temp' must be of class 'numeric'")
  }
  if (length(JD) != length(Temp)) {
16
    stop("'JD' and 'Temp' must have the same length")
17
  }
18
  if (!RunFortran & (!inherits(Lat, "numeric") | length(Lat) != 1)) {
19
20
    stop("'Lat' must be a 'numeric' of length one")
  }
21
22
23
24
25
  if (RunFortran & (!inherits(Lat, "numeric") | (!length(Lat) %in% c(1, length(Temp))))) {
    stop("'Lat' must be a 'numeric' of length one or of the same length as 'Temp'")
  }
  LatUnit <- match.arg(LatUnit, choices = c("rad", "deg"))
  if (LatUnit[1L] == "rad" & (all(Lat >= pi/2) | all(Lat <= -pi/2))) {
26
27
    stop("'Lat' must be comprised between -pi/2 and +pi/2 degrees")
  }
28
  if (LatUnit[1L] == "deg" & (all(Lat >= 90) | all(Lat <= -90))) {
29
    stop("'Lat' must be comprised between -90 and +90 degrees")
30
  }
31
  if (LatUnit[1L] == "rad") {
32
33
    FI <- Lat
  }
34
  if (LatUnit[1L] == "deg") {
35
36
37
38
39
    FI <- Lat / (180 / pi)
  }
  if (any(JD < 0) | any(JD > 366)) {
    stop("'JD' must only contain integers from 1 to 366")
  }
40
41
42
  TimeStep <- c("daily", "hourly")
  TimeStepIn  <- match.arg(TimeStepIn , choices = TimeStep)
  TimeStepOut <- match.arg(TimeStepOut, choices = TimeStep)
43
44
45
46
47
48
  rleJD <- rle(JD)
  if (TimeStepIn == "daily" & any(rleJD$lengths != 1)) {
    stop("each day must have only one identical value of julian days")
  }
  if (TimeStepIn == "hourly" & any(rleJD$lengths != 24)) {
    stop("each day must have 24 identical values of julian days (one for each hour)")
49
  }
50
51


52
  ## ---------- hourly inputs aggregation
53

54
55
56
57
58
  if (TimeStepIn == "hourly") {
    JD <- rleJD$values
    idJD <- rep(seq_along(JD), each = rleJD$lengths[1L])
    Temp <- as.vector(tapply(X = Temp, INDEX = idJD, FUN = mean))
  }
59
60


61
  ## ---------- Oudin's formula
62

63
  if (RunFortran) {
64

65
    LInputs = as.integer(length(Temp))
66
67

    if (length(FI) == 1) {
68
      FI <- rep(FI, LInputs)
69
    }
70

71
    RESULTS <- .Fortran("frun_pe_oudin", PACKAGE = "airGR",
72
73
                        ##inputs
                        LInputs = LInputs,
74
                        InputsLAT = as.double(FI),
75
76
                        InputsTT = as.double(Temp),
                        InputsJJ = as.double(JD),
77
                        ##outputs
78
79
80
                        PE_Oudin_D = rep(as.double(-999.999), LInputs)
    )
    PE_Oudin_D = RESULTS$PE_Oudin_D
81

82
  } else {
83

84
85
    PE_Oudin_D <- rep(NA, length(Temp))
    COSFI <- cos(FI)
86

87
    for (k in seq_along(Temp)) {
88

89
90
91
92
93
      TETA <- 0.4093 * sin(JD[k] / 58.1 - 1.405)
      COSTETA <- cos(TETA)
      COSGZ <- max(0.001, cos(FI - TETA))
      GZ <- acos(COSGZ)
      COSOM <- 1 - COSGZ / COSFI / COSTETA
94

95
96
97
98
      if (COSOM < -1) {
        COSOM <- -1
      }
      if (COSOM > 1) {
99
        COSOM <- 1
100
      }
101

102
      COSOM2 <- COSOM * COSOM
103

104
105
      if (COSOM2 >= 1) {
        SINOM <- 0
106
      } else {
107
        SINOM <- sqrt(1 - COSOM2)
108
      }
109

110
111
      OM <- acos(COSOM)
      COSPZ <- COSGZ + COSFI * COSTETA * (SINOM/OM - 1)
112

113
114
115
      if (COSPZ < 0.001) {
        COSPZ <- 0.001
      }
116

117
118
      ETA <- 1 + cos(JD[k] / 58.1) / 30
      GE <- 446 * OM * COSPZ * ETA
119

120
121
122
123
124
125
126
127
128
      if (is.na(Temp[k])) {
        PE_Oudin_D[k] <- NA
      } else {
        if (Temp[k] >= -5.0) {
          PE_Oudin_D[k] <- GE * (Temp[k] + 5) / 100 / 28.5
        } else {
          PE_Oudin_D[k] <- 0
        }
      }
129

130
    }
131

132
  }
133

134
  ## ---------- disaggregate PE from daily to hourly
135

136
  if (TimeStepOut == "hourly") {
137
    parab_D2H <- c(0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
138
139
                   0.035, 0.062, 0.079, 0.097, 0.110, 0.117,
                   0.117, 0.110, 0.097, 0.079, 0.062, 0.035,
140
                   0.000, 0.000, 0.000, 0.000, 0.000, 0.000)
141
    PE_Oudin_H <- rep(PE_Oudin_D, each = 24) * rep(parab_D2H, times = length(PE_Oudin_D))
142
  }
143
144


145
  ## ---------- outputs warnings
146

147
148
149
150
151
152
153
154
155
156
  if (any(is.na(Temp))) {
    if (any(is.na(PE_Oudin_D))) {
      warning("'Temp' time series, and therefore the returned 'PE' time series, contain missing value(s)")
    } else {
      warning("'Temp' time series contains missing value(s)")
    }
  }
  if (!any(is.na(Temp)) & any(is.na(PE_Oudin_D))) {
    warning("returned 'PE' time series contains missing value(s)")
  }
157

158
159
160
161
162
163
  if (TimeStepOut == "daily") {
    PE_Oudin <- PE_Oudin_D
  } else {
    PE_Oudin <- PE_Oudin_H
  }
  return(PE_Oudin)
164

165
}