debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
str(veg_grid_reduced2)
str(veg_grid_reduced)
str(veg_grid_reduced)
str(veg_grid_reduced2)
r2 = raster(veg_grid_reduced2[,,above_ground_layer],  xmn=1, xmx=1000, ymn=1, ymx=1000)
r2
plot(r2, main="material_codes (above_ground_layer)", col=palette("R3")) #correct
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
#ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_x, n_y )) #gibt 500 x 1000
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_y, n_x )) #gibt
str(code_matrix)
n_y
n_x
n_x
pattern_rast
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(-1,-1)) #
nc_close(ncnew)
nc_close(ncnew)
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_x, n_y )) #gibt 500 x 1000
#ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_y, n_x )) #gibt fehler
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(-1,-1)) #
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_y, n_x )) #gibt fehler
nc_close(ncnew)
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
#ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_y, n_x )) #gibt fehler
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(-1,-1)) #
nc_close(ncnew)
# generate plots of ground layer for verification ####
nc = nc_open(filename = paste0(dest_dir, "/vegetation_2D.nc"))
code_matrix2 = ncvar_get(nc=nc, varid = "material_id", collapse_degen = FALSE) #load content of NetCDF into array
code_matrix2 = ncvar_get(nc=nc, varid = "vegetation", collapse_degen = FALSE) #load content of NetCDF into array
str(code_matrix)
str(code_matrix2)
ncdim_def(nc)
nc
nc$dim
nc$ndims
nc$dims
nc$dims
nc$dim
str(nc$dim)
nc_close(nc)
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
ncvar_put( nc = ncnew, varid = varVegetation, vals = t(code_matrix), start=c(1,1), count=c(-1,-1)) #gibt 500 x 1000
nc_close(ncnew)
# generate plots of ground layer for verification ####
nc = nc_open(filename = paste0(dest_dir, "/vegetation_2D.nc"))
code_matrix2 = ncvar_get(nc=nc, varid = "vegetation", collapse_degen = FALSE) #load content of NetCDF into array
str(code_matrix)
str(code_matrix2)
varVegetation <- ncvar_def(name = "vegetation", units = "m",
dim= list(dim_y, dim_x), compression=1,
longname="vegetation codes", prec="integer")
varVegetation
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
nc_close(ncnew)
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest_dir, "/vegetation_2D.nc"), varVegetation )
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_x, n_y )) #gibt 500 x 1000
#ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_x, n_y )) #gibt 500 x 1000
ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(n_y, n_x )) #gibt fehler
#ncvar_put( nc = ncnew, varid = varVegetation, vals = code_matrix, start=c(1,1), count=c(-1,-1)) #gibt 500 x 1000
#ncvar_put( nc = ncnew, varid = varVegetation, vals = t(code_matrix), start=c(1,1), count=c(-1,-1)) #gibt
nc_close(ncnew)
# generate plots of ground layer for verification ####
nc = nc_open(filename = paste0(dest_dir, "/vegetation_2D.nc"))
code_matrix2 = ncvar_get(nc=nc, varid = "vegetation", collapse_degen = FALSE) #load content of NetCDF into array
str(code_matrix)
str(code_matrix2)
identical(code_matrix2, code_matrix)
identical(code_matrix2, as.integer(code_matrix))
identical(code_matrix2, round(code_matrix))
image(code_matrix)
raster(code_matrix)
plot(raster(code_matrix))
plot(raster(code_matrix2))
plot(raster(code_matrix2))
plot(raster(code_matrix))
# Create a netCDF file with this variable
ncnew <-nc_create(paste0(dest_dir, "/vegetation_3D.nc"), list(varMaterial, varDensity, varZmin))
ncvar_put(nc = ncnew, varid = varZmin, vals = 0, start=1, count=1) #this is for the lower boundary of the lowermost layer
ncvar_put( nc = ncnew, varid = varMaterial, vals = veg_grid_reduced, start=c(1,1,1), count=c(n_y, n_x, n_z ))
ncvar_put( nc = ncnew, varid = varDensity,  vals = density,          start=c(1,1,1), count=c(n_y, n_x, n_z ))
veg_grid_reduced
str(veg_grid_reduced)
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)
nc_close(ncnew)
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)
varDensity <- ncvar_def(name = "density", units = "g/cm3",
dim= list(dim_y, dim_x, dim_z), compression = 1,
longname="density", prec="float", missval = 255)
ncnew <-nc_create(paste0(dest_dir, "/vegetation_3D.nc"), list(varMaterial, varDensity, 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(NA, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_x, n_y, n_z ))
ncvar_put( nc = ncnew, varid = varMaterial, vals = veg_grid_reduced, start=c(1,1,1), count=c(n_y, n_x, n_z ))
ncvar_put( nc = ncnew, varid = varDensity,  vals = density,          start=c(1,1,1), count=c(n_y, n_x, n_z ))
#replace NA values back to NA (undocumented side-effect of "ncvar_put")
veg_grid_reduced[veg_grid_reduced==255] = NA
density         [density==255]          = NA
nc_close(ncnew)
library(tmap)
tmap_mode("view")
# tm_view(set.zoom.limits=c(1,50)) #geht nicht
tmap_options(check.and.fix = TRUE)
# tmap_options()$zoom.limits #geht nicht
#rast_layer_plot = pattern_rast #initialize raster representation
#unique(as.vector(density))
above_ground_layer = which(upper_bounds > 0)[1]
#tt = veg_grid_reduced[,,2]
#tt = density[,,1]
#tt [1:10,1:100]=   3
#raster::values(rast_layer_plot)=   t(tt)
rast_layer_plot = raster(veg_grid_reduced[,,above_ground_layer],  xmn=1, xmx=1000, ymn=1, ymx=1000)
summary(rast_layer_plot)
tm_shape(pattern_poly) +
tm_polygons(col="black", alpha=0) +
tm_shape(rast_layer_plot) +
tm_raster() +
tm_scale_bar()
# re-load from ncdf
nc = nc_open(filename = paste0(dest_dir, "/vegetation_3D.nc"))
veg_grid_reduced2 = ncvar_get(nc=nc, varid = "material_id", collapse_degen = FALSE) #load content of NetCDF into array
density2          = ncvar_get(nc=nc, varid = "density",     collapse_degen = FALSE) #load content of NetCDF into array
str(veg_grid_reduced2)
str(veg_grid_reduced)
above_ground_layer = which(upper_bounds > 0)[1]
r2 = raster(veg_grid_reduced2[,,above_ground_layer],  xmn=1, xmx=1000, ymn=1, ymx=1000)
plot(r2, main="material_codes (above_ground_layer)", col=palette("R3")) #correct
r2 = raster(density2[,,above_ground_layer], xmn=1, xmx=1000, ymn=1, ymx=1000)
plot(r2, main="density (above_ground_layer)") #correct
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
# settings ####
#veg_z_resolution = 0.3 #vertical resolution of vegetation to achieve
source("../../../../settings/settings.R") #load desired extents
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
dim(code_matrix)
x_res
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
# set all to NA, see MaterialCodes.txt
ncvar_put( nc = ncnew, varid = varMaterial, vals = rep(NA, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_x, n_y, n_z ))
varMaterial
# set all to NA, see MaterialCodes.txt
ncvar_put( nc = ncnew, varid = varMaterial, vals = rep(NA, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_x, n_y, n_z ))
dim_y
n_x
n_y
n_z
# set all to NA, see MaterialCodes.txt
ncvar_put( nc = ncnew, varid = varMaterial, vals = rep(NA, n_x*n_y*n_z ), start=c(1,1,1), count=c(n_y, n_x, n_z ))
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/atmosphere/homogenous/raw/generate_atm_homogen.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/snow/homogenous/raw/generate_snow_05.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/none/raw/generate_relief_0.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/realistic/raw/process_DEM.R", echo=TRUE)
dem_grid
source("E:/till/uni/crns/vJFC/0_compartments/snow/none/raw/generate_snow_0.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2and30_31/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/SM_distribution/hexland_tracks/raw/generate_hexland_soil_moisture.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
library(ncdf4)
detach("package:ncdf4", unload = TRUE)
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
windows()
r2 = raster(veg_grid_reduced2[,,above_ground_layer],  xmn=1, xmx=1000, ymn=1, ymx=1000)
plot(r2, main="material_codes (above_ground_layer)", col=palette("R3")) #correct
savePlot(paste0(dest_dir, "/material_codes.png"), type="png")
r2 = raster(density2[,,above_ground_layer], xmn=1, xmx=1000, ymn=1, ymx=1000)
plot(r2, main="density (above_ground_layer)") #correct
savePlot(paste0(dest_dir, "/density.png"), type="png")
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
r2
r2 = raster(bulk_density[,,ground_layer], xmn=1, xmx=x_extent, ymn=1, ymx=y_extent)
plot(r2, main="bulk density (ground layer)") #correct
savePlot(paste0(dest_dir, "/bulk_density.png"), type="png")
x_extent
y_extent
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
n_y
n_x
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
ny
n_y
n_x
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
bulk_density
str(bulk_density)
str(code_matrix)
dx
dy
pattern_poly
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/soil_physics/hexland_tracks/raw/generate_hexland_soil_phys.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/SM_distribution/hexland_tracks/raw/generate_hexland_soil_moisture.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/SM_distribution/hexland_tracks/raw/generate_hexland_soil_moisture.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2and30_31/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
r2 = raster(veg_grid_reduced2[,,above_ground_layer], xmn=1, xmx=x_extent, ymn=1, ymx=y_extent)
plot(r2, main="material_codes (above_ground_layer)", col=palette("R3")) #correct
savePlot(paste0(dest_dir, "/material_codes.png"), type="png")
windows()
r2 = raster(veg_grid_reduced2[,,above_ground_layer], xmn=1, xmx=x_extent, ymn=1, ymx=y_extent)
plot(r2, main="material_codes (above_ground_layer)", col=palette("R3")) #correct
savePlot(paste0(dest_dir, "/material_codes.png"), type="png")
#density[1,1,]
r2 = raster(density2[,,above_ground_layer], xmn=1, xmx=x_extent, ymn=1, ymx=y_extent)
plot(r2, main="density (above_ground_layer)") #correct
savePlot(paste0(dest_dir, "/density.png"), type="png")
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2and30_31/raw/generate_detector_layer.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/snow/none/raw/generate_snow_0.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/realistic/raw/process_DEM.R", echo=TRUE)
ncvar_put( nc = ncnew, varid = varElev, vals = dem_grid, start=c(1,1), count=c(n_y, n_x ))
dem_grid
ncvar_put( nc = ncnew, varid = varElev, vals = dem_grid, start=c(1,1), count=c(n_y, n_x ))
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( "../pre_processed/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 214 = Soil with soil moisture from GUI
for (i in 1:dim_z$len)
{
slice_grid = dem_grid_reduced >= dim_z$vals[i]
ncvar_put( nc = ncnew, varid = varMaterial, vals = ifelse(as.matrix(slice_grid), 214, NA), start=c(1,1,i), count=c(n_y, n_x, 1 ))
}
#plot(slice_grid )
# i = 45
# slice_grid = ncvar_get(nc = ncnew, varid = varMaterial, verbose = TRUE,
#             start=c(1,1,i), count=c(n_y, n_x, 1 ), collapse_degen=TRUE)
# image(t(slice_grid))
nc_close(ncnew)
source("E:/till/uni/crns/vJFC/0_compartments/relief/none/raw/generate_relief_0.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/snow/homogenous/raw/generate_snow_05.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/atmosphere/homogenous/raw/generate_atm_homogen.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw/generate_detector_layer.R", echo=TRUE)
setwd("E:/till/uni/crns/vJFC/0_compartments/detector/1_2/raw")
# settings ####
source("../../../../settings/settings.R") #load desired extents
#save as netcdfs
library(ncdf4)
# verify ####
nc <- nc_open( "../pre_processed/detector_3D.nc", list(varMaterial, varZmin))
# verify ####
nc <- nc_open( "../pre_processed/detector_3D.nc")
tt = ncvar_get(nc, varid="material_id")
str(tt)
# Detector layer of 1 m height parallel to the ground. Additionally, a dummy layer consisting of air is inserted very deeply, that formally
# constituted the required detector layer in URANOS but doesn't record any true counts
# Till Francke, Jan 2022
# 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)
# settings ####
source("../../../../settings/settings.R") #load desired extents
# create homogenous detector layer
codes = read.table("codes.txt", sep="\t", header=TRUE, comment.char = "#") #concrete definition of map-IDs
codes$upper_boundary = codes$lower_boundary + codes$height
#assemble boundaries for (multiple) detector layers
boundaries = sort(unique(c(codes$upper_boundary, codes$lower_boundary)))
#detector_grid_reduced = array(0.3, dim = c(n_x, n_y))
#save as netcdfs
library(ncdf4)
# Define spatial dimensions of netcdf. These are the upper boundaries of the layers
dim_x <- ncdim_def(name = "x", units = "m", vals = 1:n_x )
dim_y <- ncdim_def(name = "y", units = "m", vals = 1:n_y )
dim_z <- ncdim_def(name = "z", units = "m", vals = boundaries[-1])
n_z = length(dim_z$vals)
dim_zmin <- ncdim_def(name = "zmin_", units = "m", vals = min(boundaries)) #this is for the lower boundary of the lowermost layer
# 2D-grid
#define variable
#...
# 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)
dim_y
dim_x
dim_z
varZmin
dim_z$len
varMaterial
boundaries
nc <- nc_open( "../../../../1_realizations/hexland_tracks/fulldomain_3D.nc")
tt = ncvar_get(nc, varid="material_id")
str(tt)
nc_close(ncnew)
nc_close(nc)
nc <- nc_open( "../../../../1_realizations/sierra_neutronica/fulldomain_3D.nc")
tt = ncvar_get(nc, varid="material_id")
str(tt)
tt[:,1,1]
tt[,1,1]
tt[1:10,1,1]
tt[1:10,2,2]
tt = ncvar_get(nc, varid="density")
str(tt)
tt[1:10,1,1]
nc_close(nc)
nc <- nc_open( "../../../relief/sierra_neutronica/pre_processed/relief_3D_2.nc")
tt = ncvar_get(nc, varid="material_id")
#tt = ncvar_get(nc, varid="density")
str(tt)
tt[1:10,1,1]
tt[1:10,2,2]
tt[,2,2]
tt[1,2,]
tt = ncvar_get(nc, varid="material_id", count = c(10,10,1000) )
#tt = ncvar_get(nc, varid="density")
str(tt)
tt[1:10,1,1]
tt[1,2,]
nc_close(nc)
nc <- nc_open( "../../../relief/sierra_neutronica/pre_processed/relief_3D_2.nc")
nc <- nc_open( "../../../relief/sierra_neutronica/pre_processed/relief_3D_2.nc")
print(nc)
tt = ncvar_get(nc, varid="material_id", count = c(10,10,-1) )
#tt = ncvar_get(nc, varid="density")
str(tt)
tt[1,2,]
nc <- nc_open( "../../../relief/sierra_neutronica/pre_processed/relief_3D.nc")
print(nc)
tt = ncvar_get(nc, varid="material_id", count = c(10,10,-1) )
tt[1,2,]
source("E:/till/uni/crns/vJFC/0_compartments/relief/sierra_neutronica/raw/dem_synth.R", echo=TRUE)
# Create a netCDF file with this variable
ncnew <- nc_create( "../pre_processed/relief_3D3.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 ))
}
#plot(slice_grid )
# i = 45
# slice_grid = ncvar_get(nc = ncnew, varid = varMaterial, verbose = TRUE,
#             start=c(1,1,i), count=c(n_y, n_x, 1 ), collapse_degen=TRUE)
# image(t(slice_grid))
nc_close(ncnew)
#compute slope map
library(raster)
dem = raster("dem.tif")
res(dem)
res(dem)=1
res(dem)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3.png")
dem
crs(dem)
proj4string()
dem = raster("dem.tif")
res(dem)=1
crs(dem) = crs(r) <- CRS('+init=EPSG:6172')
crs(dem) = CRS('+init=EPSG:6172')
crs(dem)
res(dem)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3.png")
dem
extent(dem)
extent(dem)=c(1:4)
extent(dem)
extent(dem)=c(0,1000,0,1000)
dem
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3.png")
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3.png")
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
crs(dem)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3")
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3", overwrite=TRUE)
#extent(dem)=c(0,1000,0,1000)
res(dem)=1
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3", overwrite=TRUE)
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
#extent(dem)=c(0,1000,0,1000)
res(dem)=1
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
res(dem)
#extent(dem)=c(0,1000,0,1000)
res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3", overwrite=TRUE)
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
res(dem)
#extent(dem)=c(0,1000,0,1000)
res(dem)=c(1,1)
extent(dem)=c(0,1000,0,1000)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3", overwrite=TRUE)
crs(dem)
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
#extent(dem)=c(0,1000,0,1000)
#res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="radians", neighbors=8, filename="slope3", overwrite=TRUE)
image(slope)
summary(slope)
#extent(dem)=c(0,1000,0,1000)
#res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8, filename="slope3", overwrite=TRUE)
image(slope)
summary(slope)
dem = raster("dem.tif")
crs(dem) = CRS('+init=EPSG:6172')
extent(dem)
res(dem)
extent(dem)=c(0,1000,0,1000)
res(dem)
res(dem)=c(1,1)
image(dem)
#extent(dem)=c(0,1000,0,1000)
#res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8, filename="slope3", overwrite=TRUE)
res(dem)
extent(dem)
#extent(dem)=c(0,1000,0,1000)
#res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8, filename="slope3", overwrite=TRUE)
res(dem)=c(1,1)
#extent(dem)=c(0,1000,0,1000)
#res(dem)=c(1,1)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8, filename="slope3", overwrite=TRUE)
image(slope)
writeRaster(slope,'slope.tif',options=c('TFW=YES'), overwrite=TRUE)
summary(slope)
slope = terrain(dem, opt="slope", unit="degrees", neighbors=8)
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)
writeRaster(slope,'slope.tif', overwrite=TRUE)
max(diff(slope[,1]))
slope[,1]
matrix(slope)[,1]
slope@data
image(slope)
slope@data
slope@data[,1]
slope@data
slope@data@values[,1]
as.matrix(slope)
mm = as.matrix(slope)
m[,1]
mm[,1]
mm[,2]
summary(slope)
max(mm[,2], na.rm=TRUE)
writeRaster(slope,'slope_rise_run.tif', overwrite=TRUE)
summary(slope)
summary(as.matrix(slope))
range(as.matrix(slope))
range(as.matrix(slope), na.rm=TRUE)
slope2 = raster("slope2")
slope2 = raster("slope_rise_run.tif")
image(slope2)
summary(slope2)
