Skip to content

Commit

Permalink
Update to calcBphEffect to fill missing data
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewin-climsci committed Nov 30, 2020
1 parent fd7bb69 commit 26835fc
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions R/calcBphEffect.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,56 @@
calcBphEffect <-function(){

x <- readSource("BphEffect", convert="onlycorrect")
k <- calcOutput("ClimateClass", aggregate = FALSE)
weight <- dimSums(calcOutput("LanduseInitialisation", aggregate=FALSE, cellular=TRUE, land="fao", input_magpie=TRUE, years="y1995", round=6), dim=3)

bph_mask_weight <- calcOutput("BphMask", aggregate="cluster", round=6)

#mapping to connect cell names with latitudes
map <- toolGetMapping(type="cell", name="CountryToCellMapping.csv")
map <- data.table(map)

#assuming 0 was NA before.
x[x==0] <- NA

#check: How many NA cells
length(x[is.na(x)])

#extract climate class names
cclass <- getNames(k)

#select dominating climate class
ccl <- apply(k,1,function(k) {
cclass[which(k==max(k))]
})
#add to mapping
map <- cbind(map,ccl)

#global mean, used in case of NA for all cells for a give lat
m_glo <- mean(x,na.rm=TRUE)
#m_glo <- mean(b,na.rm=FALSE)

#Loop over climate classes
for (sel in cclass) {
print(sel)
#get the magpie cells corresponding to cl
cells <- map[ccl %in% sel,cell]
#find cells with NA
cells_NA <- cells[is.na(x[cells,,])]
#If all cells are NA, use m_glo, otherwise calc mean based on the non NA cells.
if(identical(cells,cells_NA)) {
x[cells_NA,,] <- m_glo
print("Attention: Global mean used!")
} else {
m <- mean(x[cells,,],na.rm=TRUE)
x[cells_NA,,] <- m
}
}
#check: no more NA cells
length(x[is.na(x)])

return(list(
x=x,
weight=bph_mask_weight,
weight=weight,
unit="degC",
description="Biogeophysical temp change of afforestation in degC",
isocountries=FALSE))
Expand Down

0 comments on commit 26835fc

Please sign in to comment.