|
| 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 | +} |
0 commit comments