Skip to content

Commit

Permalink
Do not write intermediate shapefile
Browse files Browse the repository at this point in the history
without emissions. Instead just export one final shapefile containing ABIMO + emissions calculation
  • Loading branch information
mrustl committed Mar 23, 2021
1 parent 313b6b9 commit b5c1976
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 49 deletions.
28 changes: 10 additions & 18 deletions R/abimo.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,38 +202,30 @@ makeVG <- function(rawdir,
return(vg)
}

# read dbf results file, join with input shapefile and write output shapefile
# including ABIMO results
# read dbf results file, join with input shapefile
#' abimo: postprocess
#' @description read dbf results file, join with input shapefile and write
#' output shapefile including ABIMO results
#' @param rawdir rawdir
#' @param nameABIMOin nameABIMOin
#' @param nameABIMOout nameABIMOout
#' @param ABIMOjoinedName ABIMOjoinedName
#' @return write shapefile with name "ABIMOjoinedName" to "rawdir"
#' @description read dbf results file and joins with input shapefile
#' @param path_input path of ABIMO input shapefile
#' @param abimo_output path of ABIMO output DBF file
#' @return
#' @export
#'
#' @importFrom foreign read.dbf
#' @importFrom raster shapefile
#' @importFrom dplyr left_join
#' @importFrom magrittr "%>%"
#'
postProcessABIMO <- function(rawdir, nameABIMOin,
nameABIMOout, ABIMOjoinedName){
postProcessABIMO <- function(path_input,
path_output){

# read files
ABIMOin <- raster::shapefile(x=file.path(rawdir, nameABIMOin), stringsAsFactors=FALSE)
ABIMOout <- foreign::read.dbf(file=file.path(rawdir, nameABIMOout), as.is=TRUE)

## set CODE in subcatchment data to padded CODE
#ABIMOin$CODE <- padCODE(ABIMOin@data$CODE)
ABIMOin <- raster::shapefile(x = path_input, stringsAsFactors=FALSE)
ABIMOout <- foreign::read.dbf(file = path_output, as.is=TRUE)

# join
ABIMOjoined <- ABIMOin
ABIMOjoined@data <- ABIMOin@data %>%
dplyr::left_join(ABIMOout, by='CODE')

# write out joined table output as shapefile
raster::shapefile(x=ABIMOjoined, filename=file.path(rawdir, ABIMOjoinedName), overwrite=TRUE)
ABIMOjoined
}
15 changes: 5 additions & 10 deletions man/postProcessABIMO.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 16 additions & 21 deletions vignettes/workflow_beijing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -278,35 +278,27 @@ message(cat(sprintf("Run ABIMO executable manually:
abimo_inp_path,
abimo_out))
)
kwb.utils::hsOpenWindowsExplorer(paths$abimo_exe)
abimo_out_joined <- sprintf('abimo_%sout_joined.dbf', paths$site)
}
kwb.utils::hsOpenWindowsExplorer(dirname(paths$abimo_exe))
# postprocessing ----------------------------------------------------------------
# post-process ABIMO output file -> join it with input shape file for
# visualization in GIS
urbanAnnualRunoff::postProcessABIMO(rawdir = paths$abimo,
nameABIMOin = abimo_inp,
nameABIMOout = abimo_out,
ABIMOjoinedName = abimo_out_joined
abimo_inpout <- urbanAnnualRunoff::postProcessABIMO(
path_input = file.path(paths$abimo, abimo_inp),
path_output = file.path(paths$abimo, abimo_out)
)
abimo_inpout <- kwb.abimo::abimo_comb_in_out(
file_ABIMO_out = file.path(paths$abimo, abimo_out),
file_ABIMO_in = abimo_inp_dbf_path
)
}
}
# kwb.abimo::abimo_comb_in_out() works only with DBF as input
# abimo_inpout <- kwb.abimo::abimo_comb_in_out(
# file_ABIMO_out = file.path(paths$abimo, abimo_out),
# file_ABIMO_in = abimo_inp_dbf_path
# )
# compute emissions Tongzhou with OgRe AMC (annual mean concentration) database
cDataFile <- file.path(paths$emissions_input, "annual_mean_conc.csv")
outFile <- file.path(paths$emissions_output, "emissionsTz.shp")
# grab AMC from OgRe
x_conc <- read.table(file = cDataFile,
Expand Down Expand Up @@ -345,12 +337,15 @@ loads <- sapply(X = c_heavy_metals_means$conc_mean,
})
# add computed loads to ABIMO dataset
abimo_inpout <- cbind(abimo_inpout, loads)
abimo_inpout_emissions <- cbind(abimo_inpout, loads)
# write out joined table output as shapefile
raster::shapefile(x = abimo_inpout,
outFile <- file.path(paths$emissions_output,
sprintf("abimo_%s_emissions.shp", paths$site))
raster::shapefile(x = abimo_inpout_emissions,
filename = outFile,
overwrite = TRUE)
}
```

0 comments on commit b5c1976

Please sign in to comment.