herdStatisticsOutPut <- function(stats){
  
  herdStatsFix <- list(
    Description = c("annual female calves", 
                    "number of female calves needed", 
                    "number of female calves exceeding the quantity needed for replacement", 
                    "annual male calves", 
                    "number of replacement heifers needed ", 
                    "lactation length", 
                    "lactation length of heifers", 
                    "fat and protein correction", 
                    "corrected milk (L to kg)", 
                    "pasture area", 
                    "area cultivated for supplementation "),
    Unit = c("calves/year",
             "heads/year",
             "heads/year",
             "calves/year",
             "heads/year",
             "months",
             "months",
             "dimensionless",
             "kg",
             "ha",
             "ha"))
  
  df <- data.frame(Statistic = names(stats), Description = herdStatsFix$Description, Unit = herdStatsFix$Unit, Value = as.numeric(stats))
  
  return(df)
}

herdStructureOutPut <- function(struct, allInp){
  
  herdStructFix <- list(
    Unit = rep("amount of animals (annual average)", 6)
  )
  
  df <- data.frame(Category = names(struct), Unit = herdStructFix$Unit, Equilibrium = as.numeric(struct), Current = allInp$currentHerd[["Num"]]) 
  
  return(df)
}

emissionListOutPut <- function(emissions){
  
  emissionFix <- list(
    FI = c(YearlyPerHead        = "kg/head/year",
           DailyPerHead         = "kg/head/day",
           TotalEquilibriumHerd = "Mg/year",
           TotalCurrentHerd     = "Mg/year"),
    
    CH4 = c(YearlyPerHead        = "kg/head/year",
            DailyPerHead         = "kg/head/day",
            TotalEquilibriumHerd = "kg/year",
            TotalCurrentHerd     = "kg/year"),
    
    N2O = c(YearlyPerHead        = "kg/head/year",
            DailyPerHead         = "kg/head/day",
            TotalEquilibriumHerd = "kg/year",
            TotalCurrentHerd     = "kg/year"))
  
  for(i in names(emissions)){
   nms <- paste0(names(emissionFix[[i]]), " (", emissionFix[[i]], ")")
   colnames(emissions[[i]]) <- c("Category", nms)
  }
  
  return(emissions)
}

emissionsStatsOutPut <- function(stats){
 
  emissionStatsFix <- list(
    Description = c(
      "annual total excretion N rate",
      "total herd volatile solid",
      "total herd volatilized N",
      "CH4 emission factor",
      "waste management total emission",
      "total N2O direct emissions",
      "total N2O indirect emissions",
      "CO2 emissions associated with the application of limestone in pasture and roughage production areas",
      "CO2 emissions associated with the application of urea",
      "N2O emissions associated with the application of fertilizers in pasture areas and forage production",
      "CH4 emissions in pasture and roughage production soils",
      "total N2O emissions for food production",
      "total CO2 emissions for food production",
      "CH4 emissions from herd enteric fermentation",
      "N2O emissions from livestock waste",
      "CO2 emissions from livestock waste",
      "total CH4 emissions in the production system",
      "total N2O emissions in the production system",
      "total CO2 emissions in the production system",
      "total CH4 emissions in the production system converted into CO2 equivalent",
      "total N2O emissions in the production system converted into CO2 equivalent",
      "total CO2 emissions in the production system converted into CO2 equivalent",
      "total CH4 emissions for livestock and fertilizers (except manure)",
      "total N2O emissions for livestock and fertilizers (except manure)",
      "total CO2 emissions for livestock and fertilizers (except manure)",
      "total emissions in CO2 equivalent per unit of product (FPCM)",
      "total emissions in CO2 equivalent per unit of product (FPCM)"),
    Unit = c(
      "kg N/year", 
      "kg VS/day",      
      "kg N/year",
      "kg CH4/year",
      "Mg CH4/year", 
      "Mg N2O/year", 
      "Mg N2O/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg/year", 
      "Mg CO2eq/kg FPCM", 
      "Mg CO2eq/kg FPCM"))

  stats$Description <- emissionStatsFix$Description
  stats$Unit        <- emissionStatsFix$Unit
  
  return(stats)
}

emissionListOutPut <- function(emissions){
  
  emissionFix <- list(
    FI = c(YearlyPerHead        = "kg/head/year",
           DailyPerHead         = "kg/head/day",
           TotalEquilibriumHerd = "Mg/year",
           TotalCurrentHerd     = "Mg/year"),
    
    CH4 = c(YearlyPerHead        = "kg/head/year",
            DailyPerHead         = "kg/head/day",
            TotalEquilibriumHerd = "kg/year",
            TotalCurrentHerd     = "kg/year"),
    
    N2O = c(YearlyPerHead        = "kg/head/year",
            DailyPerHead         = "kg/head/day",
            TotalEquilibriumHerd = "kg/year",
            TotalCurrentHerd     = "kg/year"))
  
  for(i in names(emissions)){
    nms <- paste0(names(emissionFix[[i]]), " (", emissionFix[[i]], ")")
    colnames(emissions[[i]]) <- c("Category", nms)
  }
  
  return(emissions)
}


transitionMatrixOutPut <- function(stocks){
  
  transMatrixFix <- list(
    Description = c("Annual change of C stocks per area",
                    "Annual change of C stocks",
                    "Annual change of CO2 stocks"),
    Unit = c("Mg/ha/year",
             "Mg C/year",
             "Mg CO2/year")
  )
  
  stocks <- as.data.frame(c(transMatrixFix, stocks))
  
  mtxDF <- cbind(data.frame(Statistic = rownames(stocks)), stocks)
  rownames(mtxDF) <- NULL
  
  stats <- data.frame(Total = c(NA, as.numeric(rowSums(stocks[,-c(1,2)])[-1])))
  mtxDF <- cbind(mtxDF, stats)
  
  return(list(PlotsStatistics = mtxDF))
}

