Skip to content

Commit

Permalink
revert back broken code
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimishr committed Sep 7, 2023
1 parent aa67400 commit c9f9b01
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 640 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '233715440'
ValidationKey: '233735047'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'magpie4: MAgPIE outputs R package for MAgPIE version 4.x'
version: 1.192.0
version: 1.192.1
date-released: '2023-09-07'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: magpie4
Title: MAgPIE outputs R package for MAgPIE version 4.x
Version: 1.192.0
Version: 1.192.1
Date: 2023-09-07
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
Expand Down
78 changes: 9 additions & 69 deletions R/emisCO2.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ emisCO2 <- function(gdx, file = NULL, level = "cell", unit = "gas",

# forestry land
####################
p32_land <- landForestry(gdx, level = "cell")
p32_land <- landForestry(gdx, level = "cell")
if (is.null(p32_land)) {
b[, , "forestry_plant"] <- 0
} else {
Expand Down Expand Up @@ -261,14 +261,14 @@ emisCO2 <- function(gdx, file = NULL, level = "cell", unit = "gas",
timestep_length <- readGDX(gdx, "im_years", react = "silent")
if (is.null(timestep_length)) timestep_length <- timePeriods(gdx)
stock <- carbonstock(gdx, level = "cell", sum_cpool = FALSE, sum_land = FALSE)
# a <- new.magpie(getCells(stock),getYears(stock),getNames(stock),fill = 0); a[,1,] <- NA; dummy <- a;
# a <- new.magpie(getCells(stock),getYears(stock),getNames(stock),fill = 0); a[,1,] <- NA; dummy <- a;
a <- stock
a[, , ] <- 0
a[, 1, ] <- NA
dummy <- a
a[, , ] <- 0
a[, 1, ] <- NA
dummy <- a
t <- 2:length(timestep_length)
a[, t, ] <- (setYears(stock[, t - 1, ], getYears(a[, t, ])) - stock[, t, ]) /
timestep_length[, t, ]
timestep_length[, t, ]
emis_total <- a

### emis_cc
Expand Down Expand Up @@ -311,60 +311,6 @@ dummy <- a
emis_degrad <- emis_degrad / timestep_length
emis_degrad[, 1, ] <- NA

## Emission deforestation ----
emis_deforestation <- emis_other_land <- dummy

### Find changes in secdf ---
secdforest <- collapseNames(readGDX(gdx, "ov35_secdforest")[,, "level"])
secdforest_change <- secdforest[, 1,]
secdforest_change[secdforest_change!=0] <- 0
for (t in 2:nyears(secdforest)) {
temp <- setYears(secdforest[, t - 1, ], getYears(secdforest[, t, ])) - secdforest[, t, ]
temp[temp<0] <- 0 # age-classes which increased in future, it does not count as "deforestation"
secdforest_change <- mbind(secdforest_change,temp)
}

### Find changes in primf ----
primforest <- collapseNames(readGDX(gdx, "ov_land")[,,"primforest"][,, "level"])
primforest_change <- primforest[, 1,]
primforest_change[primforest_change!=0] <- 0
for (t in 2:nyears(primforest)) {
temp <- setYears(primforest[, t - 1, ], getYears(primforest[, t, ])) - primforest[, t, ]
temp[temp<0] <- 0 # if primf increased in future, it does not count as "deforestation"
primforest_change <- mbind(primforest_change,temp)
}

### Find changes in other land ----
other <- collapseNames(readGDX(gdx, "ov35_other")[,, "level"])
other_change <- other[, 1,]
other_change[other_change!=0] <- 0
for (t in 2:nyears(other)) {
temp <- setYears(other[, t - 1, ], getYears(other[, t, ])) - other[, t, ]
temp[temp<0] <- 0 # age-classes which increased in future, it does not count as "other land conversion"
other_change <- mbind(other_change,temp)
}

if (all(!is.null(secdforest_change),
!is.null(primforest_change))) {
agPools <- c("vegc", "litc")
pm_carbon_density_ac <- readGDX(gdx, "pm_carbon_density_ac")[, getYears(timestep_length), ]
emis_deforestation[, , "secdforest"][, , agPools] <- dimSums(pm_carbon_density_ac[, , agPools] * secdforest_change, dim = "ac")
fm_carbon_density <- readGDX(gdx, "fm_carbon_density")[, getYears(emis_deforestation), ]
names(dimnames(fm_carbon_density))[2] <- "t"
emis_deforestation[, , "primforest"][, , agPools] <- primforest_change * fm_carbon_density[, , "primforest"][, , agPools]
}
emis_deforestation <- emis_deforestation / timestep_length
emis_deforestation[, 1, ] <- NA

if (all(!is.null(other_change))) {
agPools <- c("vegc", "litc")
pm_carbon_density_ac <- readGDX(gdx, "pm_carbon_density_ac")[, getYears(timestep_length), ]
emis_other_land[, , "other"][, , agPools] <- dimSums(pm_carbon_density_ac[, , agPools] * other_change, dim = "ac")
}
emis_other_land <- emis_other_land / timestep_length
emis_other_land[, 1, ] <- NA


# ### emis_harvest (wood)
# emis_harvest <- dummy
# ov_hvarea_forestry <- readGDX(gdx,"ov_hvarea_forestry",select = list(type="level"),react = "silent")
Expand Down Expand Up @@ -411,14 +357,8 @@ dummy <- a
# emis_harvest <- add_dimension(emis_harvest, dim = 3.3,
# nm = "lu_harvest", add = "type")

emis_deforestation <- add_dimension(emis_deforestation, dim = 3.3,
nm = "lu_deforestation", add = "type")

emis_other_land <- add_dimension(emis_other_land, dim = 3.3,
nm = "lu_other_conversion", add = "type")

# bind together
a <- mbind(emis_total, emis_cc, emis_lu, emis_luc, emis_regrowth, emis_degrad, emis_deforestation, emis_other_land)
a <- mbind(emis_total, emis_cc, emis_lu, emis_luc, emis_regrowth, emis_degrad)

# calc emis_total
# calc emis_cc #vegc,litc,soilc #natural + indirect human effects
Expand Down Expand Up @@ -453,8 +393,8 @@ dummy <- a

# unit conversion
if (unit == "gas") {
a <- a * 44 / 12
} # from Mt C/yr to Mt CO2/yr
a <- a * 44 / 12
} # from Mt C/yr to Mt CO2/yr

# years
years <- getYears(a, as.integer = TRUE)
Expand Down
Loading

0 comments on commit c9f9b01

Please sign in to comment.