plot(pattern_poly)
# convert to raster ####
dx = 1 #resolution
dy = 1
#pattern_rast = raster(pattern_poly, origin=0, layer=1, resolution=c(dx, dy))
rast_temp = raster(pattern_poly, origin=0, resolution=c(dx, dy))
pattern_rast = fasterize(sf=pattern_poly, field = "vegetat", raster = rast_temp)
windows()
plot(pattern_rast, col=palette("R3"))
savePlot(paste0(dest_dir, "/vegetation.png"), type="png")
#str(pattern_rast)
codes_in_rast = unique(pattern_poly$vegetat) #find all unique raster codes occuring in the raster
# replace raster values with URANOS codes ####
# settings ####
#veg_z_resolution = 0.3 #vertical resolution of vegetation to achieve
source("../../../../settings/settings.R") #load desired extents
#codes = read.table(paste0(dest_dir, "/../raw/codes.txt"), sep="\t", header=TRUE) #concrete definition of map-IDs
codes = read.table("codes.txt", sep="\t", header=TRUE) #concrete definition of map-IDs
codes = codes[codes$ID %in% codes_in_rast, ] #discard codes not needed
missing = setdiff(codes_in_rast, codes$ID)
if (length(missing)>0) stop(paste0("Unspecified code(s) found in raster: ", paste0(missing, collapse=", ")))
codes$height..m. = codes$top..m. - codes$bottom..m.
upper_bounds = sort(unique(c(codes$top..m., codes$bottom..m.)))[-1] #get upper limits of required vegetation layers
n_z = length(upper_bounds)
# create 3D-grid
veg_grid_reduced = array(NA, dim = c(n_y, n_x, n_z)) #x and y reversed because of matrix-raster conversion #for holding the material-IDs
density          = array(NA, dim = c(n_y, n_x, n_z)) #x and y reversed because of matrix-raster conversion #density
range_veg_reduced = upper_bounds
#code_matrix = matrix(pattern_rast@data@values, ncol=n_y) #2-D matrix of IDs
code_matrix = as.matrix(pattern_rast) #2-D matrix of IDs
if (!identical(dim(code_matrix), c(n_y, n_x))) stop("Provided pattern has different extent than expected.")
# select most suitable URANOS-IDs to keep correction errors by scaling density to a minimum ####
library(xlsx)  #load Yulia config-file to extract URANOS IDs and densities ####
URANOS_materialcodes = read.xlsx(file=path_to_cfg_file, sheetName = "material_codes_distributed")
URANOS_materialcodes = URANOS_materialcodes[grepl(URANOS_materialcodes$name, pattern = "^vegetation|^plants"),] #only keep "vegetation" entries
codes$biomass_desired = codes$total.aboveground.dry.biomass..kg.m2. * (1 + codes$watercontent.g.g.) #the total (wet) biomass inventory [kg/m2] that is specified in the table
codes$density         = codes$biomass_desired / codes$height..m.  #biomass density desired (kg/m3)
codes$density[is.nan(codes$density)]= 0 #fix division by zero where no height was specified
closest_match = function(target, table){return (which.min(abs(target-table)))} #return index of closest match
closest_ix = sapply(X = codes$density, FUN=closest_match, table=URANOS_materialcodes$density) # get indices of URANOScodes closest to desired density
closest_IDs = as.numeric(URANOS_materialcodes$code[closest_ix])
if (any(closest_IDs != codes$URANOS_codes))
warnings("URANOS-IDs corrected to ", paste0(closest_IDs, collapse=", "), " for better matching required biomass inventories.")
codes$URANOS_codes = closest_IDs #use best-matching URANOS codes
codes$URANOSdensity= URANOS_materialcodes$density[closest_ix] #get corresponding default densities for the chosen URANOS codes [kg/m3]
##materialCodes.txt: 239: Plants (1 kg/m^3) with Air
#codes$URANOSdensity= NA
#codes$URANOSdensity[codes$URANOS_codes==239]= 1*1e-3 #density [g/cm?] corresponding to URANOS-code 239
#codes$URANOSdensity[codes$URANOS_codes==220]= 5*1e-3 #density [g/cm?] corresponding to URANOS-code 220
#if (any(is.na(codes$URANOSdensity))) stop("For URANOS-codes other than 239, please provide density in the source code.")
#codes$biomass_uncorrected = codes$height..m.* codes$URANOSdensity*1e3 #the total (wet) biomass inventory [kg/m2] URANOS would use without density correction
codes$density_multiplicator = codes$density  / codes$URANOSdensity
codes$density_multiplicator[is.nan(codes$density_multiplicator)]= 1 #fix division by zero where no biomass was specified
codes$density = codes$density / 1000 #convert kg/m3 to  (g/cm3)
for (code in codes_in_rast)
{
cur_row = which(code == codes$ID)
if (length(cur_row)==0) stop(paste0("ID ", cur_row, "not found in codes.txt."))
veg_height  = codes$height..m.  [cur_row]
if (veg_height == 0) next #nothing to assign
layers2assign = which(codes$top..m.   [cur_row] >= upper_bounds &
codes$bottom..m.[cur_row]  < upper_bounds
) #find layers to be assigned
#str(code_matrix)
cells2assign = which(code_matrix == code, arr.ind = TRUE)
for (ll in layers2assign)
{
veg_grid_reduced[cbind(cells2assign[,1], cells2assign[,2], ll)] = codes$URANOS_codes[cur_row]
density         [cbind(cells2assign[,1], cells2assign[,2], ll)] = codes$density     [cur_row]
}
}
#
#range(veg_grid_reduced[,,2], na.rm=TRUE)
#range(density         [,,2], na.rm=TRUE)
#unique(as.vector(density))
#save as netcdf ####
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 = upper_bounds)
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
varVegetation <- ncvar_def(name = "vegetation", units = "m",
dim= list(dim_x, dim_y), compression=1,
longname="vegetation codes", prec="integer")
# 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 ))
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_x, dim_y, dim_z), compression = 1,
longname="material_id", prec="short", missval = 255)
varDensity <- ncvar_def(name = "density", units = "g/cm3",
dim= list(dim_x, dim_y, dim_z), compression = 1,
longname="density", prec="float", missval = 255)
# 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
# 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_x, n_y, n_z ))
ncvar_put( nc = ncnew, varid = varDensity,  vals = density,          start=c(1,1,1), count=c(n_x, n_y, n_z ))
#replace NA values back to NA (undocumented sife-effect of "ncvar_put")
veg_grid_reduced[veg_grid_reduced==255] = NA
density         [density==255]          = NA
nc_close(ncnew)
#unique(as.vector(density))
# veg_grid_reduced[1,1,]
# density[1,1,]
# veg_grid_reduced[600,100,]
# density[600,100,]
# veg_grid_reduced[600,400,]
# density[600,400,]
#
# range(density, na.rm=TRUE)
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))
#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[,,2], , 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()
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R")
undebug()
debuggingState()
debuggingState(FALSE)
Q
debuggingState()
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/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
summary(rast_layer_plot)
veg_grid_reduced
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()
above_ground_layer = which(upper_bounds > 0)[1]
r2 = raster(veg_grid_reduced[,,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(density[,,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")
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
graphics.off()
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
# generate plots of ground layer for verification ####
nc = nc_open(filename = paste0(dest_dir, "/SM_distribution_3D.nc"))
# generate plots of ground layer for verification ####
nc = nc_open(filename = paste0(dest_dir, "/vegetation_3D.nc"))
veg_grid_reduced = ncvar_get(nc=nc, varid = "material_id", collapse_degen = FALSE) #load content of NetCDF into array
density          = ncvar_get(nc=nc, varid = "density",     collapse_degen = FALSE) #load content of NetCDF into array
#zmin_bd         = ncvar_get(nc=nc, varid = "zmin", collapse_degen = TRUE) #load content of NetCDF into array
#if (zmin_bd != compartment_boundaries_reduced$soil_moisture[1])
#stop("Non-equal values of zmin for soil-moisture and bulk density. Adjust either.")
# compartment_boundaries_reduced[["bulk_density"]] = c(zmin_bd, nc$dim$z$vals) #get boundaries from netcdf-file
nc_close(nc)
above_ground_layer = which(upper_bounds > 0)[1]
r2 = raster(veg_grid_reduced[,,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(density[,,above_ground_layer], xmn=1, xmx=1000, ymn=1, ymx=1000)
plot(r2, main="density (above_ground_layer)") #correct
density
unique(as.vector(density))
upper_bounds
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
n_y
n_x
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
n_x
n_y
dim(code_matrix)
c(n_y, n_x)
identical(dim(code_matrix), c(n_y, n_x))
dim(code_matrix)
str(dim(code_matrix))
str(c(n_y, n_x)))
str(c(n_y, n_x))
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
density
str(density)
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
veg_grid_reduced
# load and plot pattern ####
pattern_poly = st_read(patternfile)
plot(pattern_poly)
plot(pattern_poly)
plot(pattern_rast, col=palette("R3"))
dx
dy
rast_temp
rast_temp = raster(pattern_poly, origin=0, ncols=n_x, nrows=n_y)
rast_temp
n_x
str(as.matrix(rast_temp))
str(matrix(rast_temp))
rast_temp
str(as.matrix(rast_temp))
pattern_rast
as.matrix(rast_temp)
rast_temp = raster(pattern_poly, origin=0, ncols=n_x, nrows=n_y)
rast_temp
str(as.matrix(rast_temp))
pattern_rast = fasterize(sf=pattern_poly, field = "vegetat", raster = rast_temp)
str(as.matrix(pattern_rast))
pattern_rast
rast_temp
n_y
debugSource("E:/till/uni/crns/vJFC/0_compartments/vegetation/hexland_tracks/raw/generate_hexland_veg.R", echo=TRUE)
str(veg_grid_reduced)
veg_grid_reduced
str(veg_grid_reduced)
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)
