as.data.frame.R 2.91 KB
Newer Older
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
as.data.frame.PrepGR <- function(x, row.names = NULL, ...) {

  # if (!(inherits(x, "PrepGR"))) {
  #   stop("'InputsCrit' must be of class 'PrepGR'")
  # }

  TMGR <- .TypeModelGR(x)
  myGR <- list()

  myGR$FracSolid <- NA
  myGR$TempMean  <- NA

  if (TMGR$CemaNeige) {
    PrecipSol <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip) * as.data.frame(x$InputsModel$LayerFracSolidPrecip), na.rm = TRUE)
    PrecipSim <- rowMeans(as.data.frame(x$InputsModel$LayerPrecip), na.rm = TRUE)
    FracSolid <- PrecipSol / PrecipSim
    FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid)
    myGR$FracSolid <- FracSolid
    TempMean  <- rowMeans(as.data.frame(x$InputsModel$LayerTempMean), na.rm = TRUE)
    myGR$TempMean  <- TempMean
  }

  myGR$DatesR  <- x$InputsModel$DatesR
  myGR$PotEvap <- x$InputsModel$PotEvap
  myGR$Precip  <- x$InputsModel$Precip
  myGR$Qobs    <- x$Qobs
  myGR$Qsim    <- NA

  TabSim <- data.frame(Dates                     = myGR$DatesR,
                       PotEvap                   = myGR$PotEvap,
                       PrecipObs                 = myGR$Precip,
                       PrecipFracSolid_CemaNeige = myGR$FracSolid,
                       TempMeanSim_CemaNeige     = myGR$TempMean,
                       Qobs                      = myGR$Qobs,
                       Qsim                      = myGR$Qsim)
  as.data.frame(x = TabSim, row.names = row.names, ...)
}


as.data.frame.CalGR <- function(x, row.names = NULL, ...) {

  # if (inherits(x, "CalGR") | inherits(x, "SimGR"))) {
  #   stop("'InputsCrit' must be of class 'CalGR', 'SimGR'")
  # }

  TMGR <- .TypeModelGR(x)
  myGR <- list()

  myGR$FracSolid <- NA
  myGR$TempMean  <- NA

  if (TMGR$CemaNeige) {
    PrecipSol <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Psol"), na.rm = TRUE)
    PrecipSim <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Pliq"), na.rm = TRUE) + PrecipSol
    FracSolid <- PrecipSol / PrecipSim
    FracSolid <- ifelse(is.na(FracSolid) & PrecipSol == 0 & PrecipSim == 0, 0, FracSolid)
    myGR$FracSolid <- FracSolid
    TempMean  <- rowMeans(sapply(x$OutputsModel$CemaNeigeLayers, "[[", "Temp"), na.rm = TRUE)
    myGR$TempMean  <- TempMean
  }

  myGR$DatesR  <- x$OutputsModel$DatesR
  myGR$PotEvap <- x$OutputsModel$PotEvap
  myGR$Precip  <- x$OutputsModel$Precip
  myGR$Qobs    <- x$Qobs
  myGR$Qsim    <- x$OutputsModel$Qsim

  TabSim <- data.frame(Dates                     = myGR$DatesR,
                       PotEvap                   = myGR$PotEvap,
                       PrecipObs                 = myGR$Precip,
                       PrecipFracSolid_CemaNeige = myGR$FracSolid,
                       TempMeanSim_CemaNeige     = myGR$TempMean,
                       Qobs                      = myGR$Qobs,
                       Qsim                      = myGR$Qsim)
  as.data.frame(x = TabSim, row.names = row.names, ...)
}


as.data.frame.SimGR  <- as.data.frame.CalGR