Skip to content

Commit fb05003

Browse files
committed
Added write_fire_grids function and option to main func, creates WMFire fire grids.
1 parent 4a63527 commit fb05003

5 files changed

Lines changed: 120 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(update_template)
1515
export(update_world)
1616
export(world_gen)
1717
export(world_redefine)
18+
export(write_fire_grids)
1819
importFrom(methods,as)
1920
importFrom(stats,aggregate)
2021
importFrom(utils,setTxtProgressBar)

R/RHESSysPreprocess.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#' @param unique_strata_ID Takes input map or value for canopy strata ID and appends either a 1 or 2 dpending on which canopy it is. Defaults to TRUE.
2929
#' @param seq_patch_IDs TRUE/FALSE should patch map IDs be overwritten with sequential integers.
3030
#' @param output_patch_map TRUE/FALSE should the new patch map with sequential IDs be output to file.
31+
#' @param fire_grid_out TRUE/FALSE should the writefire_grids headerless ascii grid outputs of DEM, patch, zone, and hillslope be output. An additional fire grid info
32+
#' file will be created specifying the ncol/nrow and other header info. Will use the input name as the base prefix for the grid files.
3133
#' @param parallel TRUE/FALSE flag to build a flowtable for use in the hilllslope parallelized version of RHESSys. Console may output warnings of
3234
#' automated actions taken to make hillslope parallelization possible, or errors indicating fatal problems in hillslope parallelization.
3335
#' @param make_stream The maximum distance (cell lengths) away from an existing stream that a patch can be automatically coerced to be a stream.
@@ -54,6 +56,7 @@ RHESSysPreprocess = function(template,
5456
unique_strata_ID = TRUE,
5557
seq_patch_IDs = FALSE,
5658
output_patch_map = FALSE,
59+
fire_grid_out = FALSE,
5760
parallel = TRUE,
5861
make_stream = 4,
5962
wrapper = TRUE) {
@@ -167,4 +170,10 @@ RHESSysPreprocess = function(template,
167170
# )
168171
# }
169172

173+
if (fire_grid_out) {
174+
cat("Writing fire grid files")
175+
write_fire_grids(name = name, template = template, map_dir = typepars, seq_patch_IDs = seq_patch_IDs)
176+
}
177+
178+
170179
} # end function

R/write_fire_grids.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#' Write fire grids
2+
#'
3+
#' Writes ascii formatted, headerless, fire grid files for use with the RHESSys integrated WMFire model. Also outputs a info file
4+
#' containing the grid dimensions and cell size.
5+
#' @param name Basename for the output fire grids. This will be the fire grid prefix given to rhessys to specify the fire grid files.
6+
#' @param template Input template file referencing the DEM, patch, zone, and hillslope spatial inputs.
7+
#' @param map_dir Directory containing the input maps (referenced as 'typepars' elsewhere)
8+
#' @param seq_patch_IDs TRUE/FALSE should sequential patch IDs be created (only needed if they were when worldfile was created)
9+
#' @author Will Burke
10+
#' @export
11+
12+
# name = "BC_h2"
13+
# template = "Preprocessing/template/coupling_test.template"
14+
# map_dir = "Preprocessing/BC_spatial/hillID_2/"
15+
16+
write_fire_grids = function(name, template, map_dir, seq_patch_IDs = FALSE) {
17+
18+
template_in = template_read(template)
19+
maps = template_in[[5]]
20+
maps = maps[maps[, 1] %in% c("world", "basin", "hillslope", "zone", "patch", "z"),]
21+
maps = unique(maps)
22+
23+
# could kinda skip this but this makes sure the same cropping/clipping happens the same as it does for the worldfile
24+
maps_in = GIS_read(
25+
maps_in = unique(maps[, 2]),
26+
type = "raster",
27+
typepars = map_dir,
28+
map_info = maps,
29+
seq_patch_IDs = seq_patch_IDs,
30+
output_patch_map = FALSE
31+
)
32+
33+
#maps_rast = methods::as(maps_in, "RasterStack")
34+
35+
# write.asciigrid needs even cell sizes and projections can lead to very slight differences
36+
if (length(unique(maps_in@grid@cellsize)) != 1) {
37+
maps_in@grid@cellsize = round(maps_in@grid@cellsize, 5)
38+
if (length(unique(maps_in@grid@cellsize)) != 1) {
39+
warning("Cell sizes are not square - using mean of cell sizes so write.asciigrid will work")
40+
maps_in@grid@cellsize = mean(maps_in@grid@cellsize)
41+
}
42+
}
43+
44+
files_in = unname(c(maps[maps[, 1] == "z", 2],
45+
maps[maps[, 1] == "hillslope", 2],
46+
maps[maps[, 1] == "zone", 2],
47+
maps[maps[, 1] == "patch", 2]))
48+
file_types = c("dem", "hillslope", "zone", "patch")
49+
files_out = file.path(map_dir, file_types)
50+
51+
write_rast = function(X) {
52+
write.asciigrid(
53+
x = maps_in[files_in[X]],
54+
fname = files_out[X]
55+
)
56+
}
57+
shh = lapply(seq_along(files_out), write_rast)
58+
59+
linesin = lapply(files_out, readLines)
60+
shhh = file.remove(files_out)
61+
heads = lapply(linesin, "[", c(1:6))
62+
linesout = lapply(linesin, function(x) {
63+
x[7:length(x)]
64+
})
65+
write_files = file.path(map_dir, paste0(name, ".", file_types))
66+
shhhh = mapply(writeLines, linesout, write_files)
67+
cat(
68+
"Wrote fire grid files to map dir:",
69+
map_dir,
70+
"with levels appended as extensions (.dem, .hillslope, .zone, .patch)"
71+
)
72+
73+
# write header(s) info
74+
writeLines(text = unlist(unique(heads)),
75+
con = file.path(map_dir, "grid_info.txt"))
76+
cat(
77+
"Wrote header info to:",
78+
file.path(map_dir, "grid_info.txt"),
79+
"duplicate entries indicate varying header info"
80+
)
81+
82+
}

man/RHESSysPreprocess.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/write_fire_grids.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)