Skip to content

Commit

Permalink
Merge pull request #3 from emolinab/main
Browse files Browse the repository at this point in the history
calcPricesProducer update
  • Loading branch information
caviddhen authored Aug 21, 2024
2 parents 4b0ae2d + 38633a9 commit 7982a1c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions R/calcPricesProducer.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ calcPricesProducer <- function(products = "kcr", calculation = "VoP", weighting
if (products == "kcr") {
if (calculation == "FAO") {
# items for aggregation

pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD2017MER per ton wet matter


getNames(pricesProdFAO)[getNames(pricesProdFAO) == "254|Oil palm fruit"] <- "254|Oil, palm fruit"

# weight: Production
Expand All @@ -37,10 +37,10 @@ calcPricesProducer <- function(products = "kcr", calculation = "VoP", weighting
names <- intersect(getNames(weightProd), getNames(pricesProdFAO[, , itemsIntersect]))
years <- intersect(getYears(weightProd), getYears(pricesProdFAO[, , itemsIntersect]))
mappingFAO <- mappingFAO[mappingFAO$ProductionItem %in% names, ]

pricesProdFAOkcr <- toolAggregate(pricesProdFAO[, years, names], rel = mappingFAO,
from = "ProductionItem", to = "kcr", weight = weightProd[, years, names],
dim = 3, wdim = 3)[, , "remaining", invert = TRUE]
dim = 3, wdim = 3, zeroWeight="allow")[, , "remaining", invert = TRUE]
} else if (weighting == "consumption") {
# map prices to FoodBalanceITems first
mappingFAO <- toolGetMapping("FAOitems_online.csv", type = "sectoral", where = "mappingfolder")
Expand All @@ -53,7 +53,7 @@ calcPricesProducer <- function(products = "kcr", calculation = "VoP", weighting
pricesProdFAO <- toolAggregate(pricesProdFAO[, wyears, wnames], rel = mappingFAO,
from = "ProductionItem", to = "FoodBalanceItem",
weight = weightProd[, wyears, wnames],
partrel = TRUE, dim = 3, wdim = 3)
partrel = TRUE, dim = 3, wdim = 3, zeroWeight="allow")

weightPrice <- collapseNames(calcOutput("FAOharmonized", aggregate = FALSE)[, , "food"])
itemsIntersect <- intersect(getNames(pricesProdFAO), unique(mappingFAO$FoodBalanceItem))
Expand All @@ -63,7 +63,7 @@ calcPricesProducer <- function(products = "kcr", calculation = "VoP", weighting

pricesProdFAOkcr <- toolAggregate(pricesProdFAO[, years, names], rel = mappingFAO,
from = "FoodBalanceItem", to = "k", weight = weightPrice[, years, names],
dim = 3, wdim = 3)
dim = 3, wdim = 3, zeroWeight="allow")
} else {
stop("only production and consumption weights")
}
Expand Down Expand Up @@ -100,7 +100,7 @@ stop("only production and consumption weights")
x <- x[, years, names]

} else if (calculation == "VoP") {
# Value of production (USD05MER -> USD05PPP)
# Value of production (USD17MER)
vop <- calcOutput("VoPcrops", aggregate = FALSE)

production <- collapseNames(calcOutput("Production", products = "kcr", aggregate = FALSE, attributes = "dm"))
Expand Down Expand Up @@ -130,27 +130,29 @@ stop("only production and consumption weights")

pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD2017 per ton


# get mapping
mappingFAO <- toolGetMapping("FAOitems_online.csv", type = "sectoral", where = "mappingfolder") # Reads mapping

if (weighting == "production") {
itemsIntersect <- intersect(getNames(pricesProdFAO), unique(mappingFAO$ProductionItem))

weightProd <- collapseNames(readSource("FAO", "LivePrim")[, , "production"]) # Prod. of livestock primary prod
weightProd <- collapseNames(readSource("FAO_online", "LivePrim")[, , "production"]) # Prod. of livestock primary prod
# subseting of items
names <- intersect(getNames(weightProd), getNames(pricesProdFAO[, , itemsIntersect]))
years <- intersect(getYears(weightProd), getYears(pricesProdFAO[, , itemsIntersect]))
mappingFAO <- mappingFAO[mappingFAO$ProductionItem %in% names, ]

# Aggregation to magpie objects
pricesProdFAOkli <- toolAggregate(pricesProdFAO[, years, names], rel = mappingFAO, from = "ProductionItem",
to = "k", weight = weightProd[, years, names], dim = 3, wdim = 3)
to = "k", weight = weightProd[, years, names], dim = 3, wdim = 3,
zeroWeight="allow")

} else if (weighting == "consumption") {
# map prices to FoodBalanceITems first
mappingFAO <- toolGetMapping("FAOitems_online.csv", type = "sectoral", where = "mappingfolder")

weightProd <- collapseNames(readSource("FAO", "LivePrim")[, , "production"])
weightProd <- collapseNames(readSource("FAO_online", "LivePrim")[, , "production"])
witemsIntersect <- intersect(getNames(pricesProdFAO), unique(mappingFAO$ProductionItem))

wnames <- intersect(getNames(weightProd), getNames(pricesProdFAO[, , witemsIntersect]))
Expand All @@ -159,7 +161,7 @@ stop("only production and consumption weights")
pricesProdFAO <- toolAggregate(pricesProdFAO[, wyears, wnames], rel = mappingFAO,
from = "ProductionItem", to = "FoodBalanceItem",
weight = weightProd[, wyears, wnames],
partrel = TRUE, dim = 3, wdim = 3)
partrel = TRUE, dim = 3, wdim = 3, zeroWeight="allow")

weightPrice <- collapseNames(calcOutput("FAOharmonized", aggregate = FALSE)[, , "food"])
itemsIntersect <- intersect(getNames(pricesProdFAO), unique(mappingFAO$FoodBalanceItem))
Expand All @@ -169,7 +171,7 @@ stop("only production and consumption weights")

pricesProdFAOkli <- toolAggregate(pricesProdFAO[, years, names], rel = mappingFAO,
from = "FoodBalanceItem", to = "k", weight = weightPrice[, years, names],
dim = 3, wdim = 3)
dim = 3, wdim = 3, zeroWeight="allow")
} else {
stop("only production and consumption weights")
}
Expand Down Expand Up @@ -205,6 +207,7 @@ stop("invalid type")

units <- "2017USDMER/tDM"


return(list(x = x,
weight = weight,
mixed_aggregation = NULL,
Expand Down

0 comments on commit 7982a1c

Please sign in to comment.