# Calculating Yields and Prices

# Rule of Yields:
# Yield = GROF/LEVL (if GROF and LEVL >0, unit t/ha)

# Rule of Prices:
# Step 1; based on Dimitrio's R script
#  1: for all farms where UVAG >UVSA =0 and UVAG != infinite, PRICE = UVAG
#  2: for all farms where UVAG >UVSA >0 and UVAG != infinite, PRICE = UVSA
#  3: for all farms where UVSA >UVAG =0 and UVSA != infinite, PRICE = UVSA
#  4: for all farms where UVSA =UVAG >0 and UVSA != infinite, PRICE = UVSA
#  5: for all farms where UVSA >UVAG >0 and UVSA != infinite, PRICE = UVAG
#  6: others PRICE = NA
# Step 2: comparsion PRICE and EAAP/GROF
# for all farms where EAAP >0 and GROF >0, PRICE2 = EAAP/GROF,
# if PRICE is NA, but PRICE2 is not NA, PRICE =PRICE2.

# Convert NUTS ------------------------------------------------------------
fadn.str.crops_NUTS <-
  fadnUtils::NUTS.convert.all(str_dat$crops, "all", 2016)

# reshape data 
crops_dat <- dcast(fadn.str.crops_NUTS,...~VARIABLE, fun.aggregate = sum,value.var = "VALUE")

# Calculation Yield -------------------------------------------------------
crops_dat[GROF>0 & LEVL>0,YIELD:= GROF/LEVL]

# Calculation Price -------------------------------------------------------
# price = EAAP/GROF = UVAG = UVSA

# Rule to compute Prices of Dimitrio:
#
#   Step 1:
#   For all farms where there is SQ>0.01 and SV>0, PRICE1=SV/SQ  UVSA
#
#   Step 2:
#   For all farms where PRQ>0 & TO>0, PRICE2 = TO/PRQ. UVAG
#
#   Step 3:
#   If only PRICE1 or only PRICE2 has been set, set PRICE=PRICE1 or PRICE=PRICE2 respectively
#
#   Step 4:
#   For all farms where PRICE1 and PRICE2 has been set, take the MIN of PRICE\@step1 and PRICE2
#
#   Step 5:
#   If for a farm, there is SV>0 but SQ=0, then calculate SQ=SV/PRICE
#  question? wofr SQ?


# Step 1; based on Dimitrio's rule
#  1: for all farms where UVAG >UVSA =0 and UVAG != infinite, PRICE = UVAG
#  2: for all farms where UVAG >UVSA >0 and UVAG != infinite, PRICE = UVSA
#  3: for all farms where UVSA >UVAG =0 and UVSA != infinite, PRICE = UVSA
#  4: for all farms where UVSA =UVAG >0 and UVSA != infinite, PRICE = UVSA
#  5: for all farms where UVSA >UVAG >0 and UVSA != infinite, PRICE = UVAG
#  6: others PRICE = NA

# Step 2: comparsion PRICE and EAAP/GROF
# for all farms where EAAP >0 and GROF >0, PRICE2 = EAAP/GROF,
# if PRICE is NA, but PRICE2 is not NA, PRICE =PRICE2.


crops_dat <- crops_dat %>%
  mutate(PRICE = case_when(UVAG > UVSA & UVSA == 0 & !is.infinite(UVAG) ~ UVAG,
                           UVSA > UVAG & UVAG == 0 & !is.infinite(UVSA) ~ UVSA,
                           UVSA == UVAG & UVAG > 0 & !is.infinite(UVSA) ~ UVSA,
                           UVSA > UVAG & UVAG >0 & !is.infinite(UVSA) ~ UVAG,
                           UVSA < UVAG & UVSA >0 & !is.infinite(UVAG)~ UVSA,
                           TRUE ~ NA_real_))

crops_dat <- crops_dat %>%
  mutate(PRICE2 = case_when(EAAP>0 &GROF>0 ~EAAP/GROF,
                            TRUE ~ NA_real_))

crops_dat <- crops_dat %>%
  mutate(PRICE_final = if_else( is.na(PRICE) & !is.na(PRICE2), PRICE2, PRICE ))


# Save gdx: organic/Conventional ----------------------------------------------------------------
# 1: calculate the organic and conventional and group by different variables 
# 2: 
{
  EU_list <- c("AUT", "BEL", "BGR", "HRV", "CYP", "CZE", "DNK",
               "EST", "FIN", "FRA", "DEU", "GRC", "HUN", "IRL",
               "ITA", "LVA", "LTU", "LUX", "MLT", "NLD", "POL",
               "PRT", "ROU", "SVK", "SVN", "ESP", "SWE")
  
  fadn.filter.yield_price <- function(crops_dat, group.by, type,EU_list){
    data <- crops_dat %>% mutate_at(vars(YIELD,
                                         PRICE_final), ~ replace(., is.na(.) |
                                                                   is.infinite(.), 0)) %>%
      mutate(ORGANIC = case_when(ORGANIC == "org-2" ~ "Organic",
                                 TRUE ~ "Conventional")) %>%
      select(-(TF8:SIZ6), -ALTITUDE)
    
    if (group.by == "EU") {
      filtered <- data %>% filter(COUNTRY %in% EU_list) %>%
        group_by(YEAR, .data[[type]], ORGANIC) %>%
        summarise(
          # yield in kg/ha
          Yield = sum(GROF * WEIGHT) / sum(LEVL * WEIGHT) * 1000,
          # price in euro/tonne
          PRICE_final_fw = weighted.mean(x = PRICE_final, w =
                                           WEIGHT, na.rm = TRUE),
          UVAG_r = sum(EAAP * WEIGHT) / sum(GROF * WEIGHT),
          UVAG_fw = weighted.mean(x = UVAG, w = WEIGHT, na.rm = TRUE),
          PRICE_1_fw = weighted.mean(x = PRICE, w = WEIGHT, na.rm = TRUE),
          PRICE_2_fw = weighted.mean(x = PRICE2, w = WEIGHT, na.rm = TRUE)
        ) %>%
        mutate_at(
          vars(PRICE_final_fw,
               UVAG_r,
               UVAG_fw,
               PRICE_1_fw,
               PRICE_2_fw),
          ~ replace(., is.nan(.) | is.infinite(.), 0)
        ) %>%
        as.data.table() %>%
        mutate(REGION = group.by,
               REG_TYPE = group.by)
      
    }
    else {
      filtered <- data %>%
        group_by(.data[[group.by]],
                 YEAR,
                 .data[[type]],
                 ORGANIC) %>%
        summarise(
          # yield in kg/ha
          Yield = sum(GROF * WEIGHT) / sum(LEVL * WEIGHT) * 1000,
          # price in euro/tonne
          PRICE_final_fw = weighted.mean(x = PRICE_final, w =
                                           WEIGHT, na.rm = TRUE),
          UVAG_r = sum(EAAP * WEIGHT) / sum(GROF * WEIGHT),
          UVAG_fw = weighted.mean(x = UVAG, w = WEIGHT, na.rm = TRUE),
          PRICE_1_fw = weighted.mean(x = PRICE, w = WEIGHT, na.rm = TRUE),
          PRICE_2_fw = weighted.mean(x = PRICE2, w = WEIGHT, na.rm = TRUE)
        ) %>%
        mutate_at(
          vars(PRICE_final_fw,
               UVAG_r,
               UVAG_fw,
               PRICE_1_fw,
               PRICE_2_fw),
          ~ replace(., is.nan(.) | is.infinite(.), 0)
        ) %>%
        as.data.table() %>%
        rename(REGION = .data[[group.by]]) %>%
        mutate(REG_TYPE = group.by)
      
    }
    
    return(filtered)
    
    
  }
  
  group_by_lst <- c("COUNTRY","REGION","NUTS1","NUTS2","NUTS1_final", "NUTS2_final", "EU")
  crops.groupby <-  lapply(seq_along(group_by_lst),
                           function(i) fadn.filter.yield_price(crops_dat,
                                                               group_by_lst[i],
                                                               "CROP",
                                                               EU_list)) %>%
    bind_rows() %>%
    mutate(REG_TYPE = case_when(REG_TYPE == "COUNTRY" ~"MS",
                                REG_TYPE == "REGION" ~ "FADN_REGION",
                                REG_TYPE == "NUTS1" ~ "NUTS1_ORG",
                                REG_TYPE =="NUTS2"~ "NUTS2_ORG",
                                REG_TYPE =="NUTS1_final"~ "Xinxin_NUTS1",
                                REG_TYPE =="NUTS2_final" ~"Xinxin_NUTS2",
                                TRUE ~ REG_TYPE)) %>%
    mutate_if(is.factor, as.character)
}

#reshape 

gdx.dat <- crops.groupby %>% pivot_longer(cols = Yield:PRICE_2_fw, names_to = "variable", values_to = "value")


# * yield -----------------------------------------------------------------
col_names_crops <- colnames(gdx.dat)
cat("Export the gdx: ",  paste0(gdx.results_out,"/crops_",fadn.countries,"_","yield/price.gdx") ,sep = "", "\n")

writegdx(dt = gdx.dat %>% filter(variable == "Yield") %>% as.data.table() ,
         gdx = paste0(gdx.results_out, "/crops_",fadn.countries,"_", "yield",".gdx"),
         name = "DataOut",
         valcol= "value",
         uelcols= col_names_crops[!col_names_crops %in% "value"],
         type="parameter")

# * price -----------------------------------------------------------------
writegdx(dt = gdx.dat %>% filter(variable != "Yield") %>% as.data.table() ,
         gdx = paste0(gdx.results_out, "/crops_", fadn.countries,"_","price",".gdx"),
         name = "DataOut",
         valcol= "value",
         uelcols= col_names_crops[!col_names_crops %in% "value"],
         type="parameter")