install.packages("gWidgets")
source("E:/till/uni/crns/vJFC/0_compartments/relief/synthetic/dem_synth.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/synthetic/dem_synth.R", echo=TRUE)
dem = array(NA, c(n_y, n_x))
source("E:/till/uni/crns/vJFC/0_compartments/relief/synthetic/dem_synth.R", echo=TRUE)
library(rgl)
library(rgl)
install.packages("rgl")
plot3d(dem4)
library(rgl)
plot3d(dem4)
persp3d(dem4)
persp(dem4)
persp(dem4, col = "lightblue")
persp(dem4, col = "lightblue")
persp3d(dem4, col = "lightblue")
range(dem)
range(dem4)
lambda_max
a_max
dem4 = dem4 * a_max
persp3d(dem4, col = "lightblue")
persp(dem4, col = "lightblue")
persp3d(dem3, col = "lightblue")
dem3 = cos(inner)  #"drop ripples"
dem3 = (dem3+1)/2 #scale to 0..1
persp3d(dem3, col = "lightblue")
dem3 = cos(inner)  #"drop ripples"
#range(lambda)
inner = ((radius)-1) * 2*pi/lambda
dem3 = cos(inner)  #"drop ripples"
dem3 = (dem3+1)/2 #scale to 0..1
persp3d(dem3, col = "lightblue")
image(dem4)
# introduce gradient from south to north, shrinking northern peaks ####
scaling = dem*0 + a_func(row(dem))
range(scaling)
lambda_max
a_max
source("E:/till/uni/crns/vJFC/0_compartments/relief/synthetic/dem_synth.R", echo=TRUE)
persp3d(scaling, col = "lightblue")
dem5 = dem4 * scaling/a_max #scale with amplitude gradient
persp3d(dem5, col = "lightblue")
scaling = dem*0 + a_func(col(dem))
# range(scaling)
# persp3d(scaling, col = "lightblue")
dem5 = dem4 * scaling/a_max #scale with amplitude gradient
persp3d(dem5, col = "lightblue")
scaling = dem*0 + a_func(n_x+1 - col(dem))
# range(scaling)
# persp3d(scaling, col = "lightblue")
dem5 = dem4 * scaling/a_max #scale with amplitude gradient
persp3d(dem5, col = "lightblue")
image(dem5)
par(mfrow=c(1,3))
image(dem3)
image(dem4)
image(dem5)
# convert to raster and save ####
library(raster)
r <- raster(dem5)
setwd("E:/till/uni/crns/vJFC/0_compartments/relief/synthetic")
writeRaster(r,'dem.tif',options=c('TFW=YES'))
persp3d(dem4, col = "lightblue", x=1:n_x, y=1:n_y)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y)
.libPaths()
searchpaths()
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, theta = 1, phi=NULL, r=NULL, d=1)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, theta = 1) #, phi=NULL, r=NULL, d=1)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 1) #, phi=NULL, r=NULL, d=1)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 10) #, phi=NULL, r=NULL, d=1)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180) #, phi=NULL, r=NULL, d=1)
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
pdf("shadeplot.png")
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
dev.off()
pdf("shadeplot.png")
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
dev.off()
windows()
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
savePlot("shadeplot.png")
windows()
persp(dem5, col = "lightblue", x=1:n_x, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
savePlot("shadeplot.png", type = "png")
dev.off()
n_x
a=1:n_x
persp(dem5, col = "lightblue", x=a, y=1:n_y, border = NA, theta = 180, shade=0.6) #, phi=NULL, r=NULL, d=1)
#save shaded view as png
windows()
windows(21, 7)
windows(width = 12, height = 4)
par(mfrow=c(1,3))
image(dem3)
image(dem4)
image(dem5)
savePlot("dem_stages.png", type = "png")
windows(width = 12, height = 4)
par(mfrow=c(1,3))
image(dem3)
image(dem4)
image(dem5)
savePlot("dem_stages.png", type = "png")
dev.off()
image(dem5, xlab="x")
raster::image(dem5)
raster::image(dem5, x=1:n_x, y=1:n_y)
image(dem5, x=1:n_x, y=1:n_y)
dem_grid = dem5
#round DEM values to multiples of z-resolution
range_dem = cellStats(dem_grid, stat=range)
dem_grid <- raster(dem5)
writeRaster(dem_grid,'dem.tif',options=c('TFW=YES'))
# plot DEM   ####
plot(dem_grid)
dem_grid = dem5
dem_grid <- raster(dem5)
#round DEM values to multiples of z-resolution
range_dem = cellStats(dem_grid, stat=range)
range_dem
dem_grid_reduced = dem_grid - range_dem[1] #shift minimum elevation to zero
range_dem_reduced = range_dem - range_dem[1]
# Define spatial dimensions of netcdf
dim_x <- ncdim_def(name = "x", units = "m", vals = 1:n_x )
dim_y <- ncdim_def(name = "y", units = "m", vals = 1:n_y )
#save as netcdfs
library(ncdf4)
# Define spatial dimensions of netcdf
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 = seq(from=range_dem_reduced[1], to=range_dem_reduced[2], by=dem_z_resolution))
dem_z_resolution = 0.5 #vertical resolution of DEM to achieve
dem_z_resolution = 1 #vertical resolution of DEM to achieve
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
# Define spatial dimensions of netcdf
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 = 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( "../pre_processed/relief_2D.nc", varElev )
# Create a netCDF file with this variable
ncnew <- nc_create( "../pre_processed/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)
#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 ))
}
# create relief as constant slope for vJFC
dem_z_resolution = 1 #vertical resolution of DEM to achieve
#source("../../../../settings/settings.R") #load desired extents
n_x=n_y=1000
dem = array(NA, c(n_y, n_x))
a_min = 0
a_max = 10
for (r in 1:n_y)
{
index = (1:n_x)-1
dem[,r] = a_min + index * max(index) * (a_max-a_min)
}
image(dem)
summary(dem)
range(dem)
for (r in 1:n_y)
{
index = (1:n_x)-1
dem[,r] = a_min + index / max(index) * (a_max-a_min)
}
image(dem)
range(dem)
# convert to raster and save ####
library(raster)
dem_grid <- raster(dem5[nrow(dem5):1,])
# convert to raster and save ####
library(raster)
dem_grid <- raster(dem5[nrow(dem):1,])
# convert to raster and save ####
library(raster)
dem_grid <- raster(dem[nrow(dem):1,])
writeRaster(dem_grid,'dem.tif',options=c('TFW=YES'), overwrite=TRUE)
# plot DEM   ####
plot(dem_grid)
# convert to raster and save ####
library(raster)
dem_grid <- raster(dem[nrow(dem):1,])
writeRaster(dem_grid,'dem.tif',options=c('TFW=YES'), overwrite=TRUE)
# plot DEM   ####
plot(dem_grid)
#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
#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]
# Define spatial dimensions of netcdf
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 = 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( "../pre_processed/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( "../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)
{
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), 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))
#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/slope/raw/dem_synth.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/slope/raw/dem_synth.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/slope/raw/dem_synth.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/slope/raw/dem_synth.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/relief/slope/raw/dem_synth.R", echo=TRUE)
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_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( "../pre_processed/relief_3D.nc", list(varMaterial, varZmin))
# Create a netCDF file with this variable
ncnew <- nc_create( "../pre_processed/relief_3D_2.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 100 = Soil with soil moisture from GUI
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), 100, 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)
