library(lumpR)
bulk_density
bulk_density=0.4
1-(bulk_density/2.85)
bulk_density=1.2
1-(bulk_density/2.85)
# Relief of Sierra_neutronica, fading to plain in the Eastern half
# set working directory to source file location ####
rm(list=ls())
if (!require("rstudioapi", quietly = TRUE)) install.packages("rstudioapi")
require(rstudioapi)
wd <- rstudioapi::getActiveDocumentContext()$path
wd <- sub(x=wd, pattern="(.*/)[^/]+$","\\1")
setwd(wd)
# import DEM ####
library(raster)
dem_grid <- raster("../../sierra_neutronica/raw/dem.tif")
n_x=ncol(dem_grid)
n_y=ncol(dem_grid)
dem_z_resolution = 1 #min(diff(unique(dem_grid)))
# plot DEM   ####
plot(dem_grid)
# create transition grid ###
trans_grid = dem_grid *0
trans_width = 50 #width of transition zone
trans_zone = seq( from=round(500 -trans_width/2), to=500+round(trans_width/2))
trans_vals = seq(from=1, to=0, length.out=trans_width)
for (r in trans_zone)
trans_grid[,r] = trans_vals[which(r==trans_zone)]
trans_grid[,1:trans_zone[1]] = 1
trans_grid[,trans_zone[trans_width]: n_x] = 0
plot(trans_grid)
# overlay
dem_grid2 = dem_grid * trans_grid
plot(dem_grid2)
# convert to raster and save ####
library(raster)
writeRaster(dem_grid2,'dem.tif',options=c('TFW=YES'), overwrite=TRUE)
library(rgl)
persp3d(dem_grid2, col = "lightblue", x=1:n_x, y=1:n_y)
persp3d(flip(dem_grid2, direction="x"), col = "lightblue", x=1:n_x, y=1:n_y)
dem_grid = dem_grid2
#save as netcdfs
library(ncdf4)
#round DEM values to multiples of z-resolution
range_dem = cellStats(dem_grid, stat=range)
dem_grid_reduced = dem_grid - range_dem[1] #shift minimum elevation to zero
range_dem_reduced = range_dem - range_dem[1]
#dem_grid2 = round (dem_grid2 / dem_z_resolution) * dem_z_resolution
# Define spatial dimensions of netcdf
y_ = n_y:1 #y-axis points towards north, so it is reversed here
dim_x <- ncdim_def(name = "x", units = "m", vals = 1:n_x )
dim_y <- ncdim_def(name = "y", units = "m", vals = y_ )
dim_z <- ncdim_def(name = "z", units = "m", vals = seq(from=range_dem_reduced[1], to=range_dem_reduced[2], by=dem_z_resolution))
n_z = length(dim_z$vals)
dim_zmin <- ncdim_def(name = "zmin_", units = "m", vals = 0) #this is for the lower boundary of the lowermost layer
# 2D-grid
#define variable
varElev <- ncvar_def(name = "elevation", units = "m",
dim= list(dim_y, dim_x), compression=1,
longname="Elevation asl", prec="double")
# Create a netCDF file with this variable
ncnew <- nc_create( ".././relief_2D.nc", varElev )
ncvar_put( nc = ncnew, varid = varElev, vals = as.matrix(dem_grid), start=c(1,1), count=c(n_y, n_x ))
nc_close(ncnew)
# 3D-grid
#define variable
varZmin       <- ncvar_def(name = "zmin", units = "m",
dim= list(dim_zmin), prec="float")
varMaterial <- ncvar_def(name = "material_id", units = "id",
dim= list(dim_y, dim_x, dim_z), compression = 1,
longname="material_id", prec="short", missval = 255)
# Create a netCDF file with this variable
ncnew <- nc_create( ".././relief_3D.nc", list(varMaterial, varZmin))
ncvar_put(nc = ncnew, varid = varZmin, vals = 0, start=1, count=1) #this is for the lower boundary of the lowermost layer
# set all to NA, see MaterialCodes.txt
ncvar_put( nc = ncnew, varid = varMaterial, vals = rep(1, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_y, n_x, n_z ))
#step through slices of the DEM and fill blocks accordingly, setting code for 211 = Soil with 10%Vol moisture
#step through slices of the DEM and fill blocks accordingly, setting code for 212 =  Quartz (2.5 g/cm^3)
for (i in 1:dim_z$len)
{
print(paste0(i, " / ", dim_z$len))
slice_grid = dem_grid_reduced >= dim_z$vals[i]
ncvar_put( nc = ncnew, varid = varMaterial, vals = ifelse(as.matrix(slice_grid), 212, NA), start=c(1,1,i), count=c(n_y, n_x, 1 ))
}
nc_close(ncnew)
# Create a netCDF file with this variable
ncnew <- nc_create( ".././relief_3D.nc", list(varMaterial, varZmin))
ncvar_put(nc = ncnew, varid = varZmin, vals = 0, start=1, count=1) #this is for the lower boundary of the lowermost layer
# set all to NA, see MaterialCodes.txt
ncvar_put( nc = ncnew, varid = varMaterial, vals = rep(1, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_y, n_x, n_z ))
#step through slices of the DEM and fill blocks accordingly, setting code for 211 = Soil with 10%Vol moisture
#step through slices of the DEM and fill blocks accordingly, setting code for 212 =  Quartz (2.5 g/cm^3)
for (i in 1:dim_z$len)
{
print(paste0(i, " / ", dim_z$len))
slice_grid = dem_grid_reduced >= dim_z$vals[i]
ncvar_put( nc = ncnew, varid = varMaterial, vals = ifelse(as.matrix(slice_grid), 212, NA), start=c(1,1,i), count=c(n_y, n_x, 1 ))
}
nc_close(ncnew)
#compute slope map and export
library(raster)
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
extent(dem)=c(0,1000,0,1000)
res(dem)=c(1,1)
image(dem)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8)
image(slope)
summary(slope)
range(as.matrix(slope), na.rm=TRUE)
writeRaster(slope,'slope_rise_run.tif', overwrite=TRUE)
mm = as.matrix(slope)
max(mm[,2], na.rm=TRUE)
slope2 = raster("slope_rise_run.tif")
image(slope2)
summary(slope2)
#sierra_half
source_dem =  "../../../relief/sierra_half/./relief_3D.nc"
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_sierra/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2and30_31/raw/generate_detector_layer.R", echo=TRUE)
boundaries
ncnew <- nc_open(paste0(dest_dir,  "detector_3D.nc"))
mat = ncvar_get(nc = ncnew, varid = varMaterial)
mat
str(mat)
mat[1,1,]
ncvar_get(nc = ncnew, varid = "z_")
ncvar_get(nc = ncnew, varid = "z")
dim_zmin
ncvar_get(nc = ncnew, varid = "zmin")
