correspondence,
location_data[, c("location_id..group.id..counter..", "soil", "landuse", "class",
"surface_val_om",   "subsurface_val_om",   "break_depth_om",
"surface_val_bd",   "subsurface_val_bd",   "break_depth_bd",
"surface_val_clay_perc", "subsurface_val_clay_perc", "break_depth_clay_perc"
)],
by.x=c("location_id_fdr"),
by.y=c("location_id..group.id..counter..")
)
#join 'composite' data (OM-content)
composite_data = read.xlsx(fdr_fielddata_file, sheetName = "composite_lab_results",
stringsAsFactors=FALSE )  # read the sheet holding the composite data
correspondence$depth_rounded10 = trunc(correspondence$depth.of.hole..cm./10) * 10 #round depth values to 10-cm-increments (as in the composite analysis)
correspondence = merge(
correspondence,
composite_data[, c("included.location.IDs", "depth..cm.", "SOM....")],
by.x=c("location_id_grav", "depth_rounded10"),
by.y=c("included.location.IDs", "depth..cm."),
all.x=TRUE
)
#names(correspondence)[grepl(x=names(correspondence), pattern="SOM")] = "om_perc" #rename column containing organic matter fraction
nrow(correspondence)
eps_cols = c("epsilon1","epsilon2","epsilon3")
# #transform to square root of epsilon
# correspondence[, c("epsilon1_sqrt","epsilon2_sqrt","epsilon3_sqrt")] =
#   sqrt(correspondence[,eps_cols])
# eps_cols = paste0(c("epsilon1","epsilon2","epsilon3"), "_sqrt")
#
#
# #compute clay fraction as mean of class and topsoil/subsoil
#   tt = merge (gravi_data[, c("location_id", "clay...", "depth..cm.")], location_data[, c("location_id..group.id..counter..", "class")],
#               by.x="location_id", by.y="location_id..group.id..counter..")
#   tt$topsoil = tt$depth..cm. < 15 #indicator topsoil/subsoil
#
#   tt2 = aggregate(tt$clay..., by=tt[,c("class", "topsoil")], FUN=mean, na.rm=TRUE)
#   names(tt2)[names(tt2)=="x"] = "clay_perc" #rename column
#   write.table(x=tt2, file="clay_cont_classes.txt", row.names = FALSE, quote = FALSE, sep="\t")
#
#   correspondence$topsoil = correspondence$depth..cm. < 15 #indicator topsoil/subsoil
#   correspondence = merge(correspondence, tt2) #merge mean clay content
correspondence$depth_rounded = round(correspondence$depth.of.hole..cm./5) * 5 #round depth values to 5-cm-increments
#compute OM-content, BD, clay_perc from (interpolated) piecewise linear coefficients
source("f_piece_linear.R")
correspondence$om_perc   = piece_linear(z=correspondence$depth.of.hole..cm., surface_val=correspondence$surface_val_om,   subsurface_val=correspondence$subsurface_val_om,   break_depth=correspondence$break_depth_om)
correspondence$bd        = piece_linear(z=correspondence$depth.of.hole..cm., surface_val=correspondence$surface_val_bd,   subsurface_val=correspondence$subsurface_val_bd,   break_depth=correspondence$break_depth_bd)
correspondence$clay_perc = piece_linear(z=correspondence$depth.of.hole..cm., surface_val=correspondence$surface_val_clay, subsurface_val=correspondence$subsurface_val_clay, break_depth=correspondence$break_depth_clay)
# relate values of epsilon to theta ####
#assemble all three replicates of FDR-measurements to allow common regression
common_set=NULL
eps_cols = c("epsilon1","epsilon2","epsilon3")
for (cc in eps_cols)
{
tt = correspondence[, c(cc, "theta","landuse","soil","BD", "device", "om_perc", "clay_perc", "depth.of.hole..cm.", "dist")]
names(tt)[1]="epsilon"
common_set=rbind(common_set, tt)
}
common_set = common_set[is.finite(common_set$epsilon),] #discard NA
#names(common_set) = c("epsilon_sqrt","wc_vol","landuse","soil", "BD")
names(common_set)[grepl(names(common_set), pattern = "depth")] = "depth"
#common_set$part_dens = ifelse(common_set$soil=="organic", 1, 2.6) #estimated
common_set$part_dens = common_set$om_perc/100 * 1 + (1-common_set$om_perc/100)* 2.6 #estimated particle density
common_set$n = 1 - (common_set$BD / common_set$part_dens)
#if (is.null(common_set$soil))
common_set$soil = ifelse(common_set$om_perc>30, "organic", "mineral") #guess soils type based on OM content
common_set$soil = as.factor(common_set$soil)
common_set$landuse = as.factor(common_set$landuse)
#modifications to allow merging with PR2 dataset
common_set$ring_no = 1
common_set$type = "Theta Probe"
common_set$source="campaign"
# save results ####
common_set_theta_probes = common_set #rename to allow merging with Profile data
# make specific adjustments
warning("Specific adjustments for Treuenbrietzen made")
common_set_theta_probes$clay_perc = as.numeric(NA) #no clay information available
common_set_theta_probes$cohesive = FALSE
save(file="common_set_theta_probes.RData", list="common_set_theta_probes")
save(file="correspondence.RData", list=c("correspondence", "fdr_data")) #for later use in 3_apply_conversion.R
load(file="common_set_theta_probes.RData")
# simple plot ####
palette(rainbow(10))
plot(1,1, xlim=sqrt(range(common_set$epsilon, na.rm=TRUE)), ylim=range(common_set$theta, na.rm=TRUE),
xlab="sqrt(eps)", ylab="theta_vol [-]", type="n", main=tstring)
points(sqrt(common_set$epsilon), common_set$theta,
#col=common_set$BD..g.cmÂ³./2*10
col=common_set$depth
#col=ifelse(common_set$soil=="mineral","black","red")
#col=ifelse(common_set$landuse=="forest","green","red")
)
abline(lm(common_set$theta ~ sqrt(common_set$epsilon)))
# if you want to include PR2-data in the calibration, process until 4_1_assemble_epsilon_thetadata.R
# otherwise, continue with 2_choose_conversion.R
source("P:/nieplitz/Waldbrand/Bodenfeuchte/theta_processing/thetaprobes_konrad/3_apply_conversion.R", echo=TRUE)
# do comparison with prior version
prior_version = "e:/till/uni/lehre/Diplomarbeiten_Gutachten/21/Petzold_Konrad/daten/220419_theta_Konrad_Lisa_converted.xlsx"
fdr_data2_old = read.xlsx(prior_version, sheetName = "theta_from_FDR",
stringsAsFactors=FALSE )  # read the second sheet
setdiff(unique(fdr_data2_old$location_id), unique(fdr_data2$location_id))
#          fdr_data2_new = fdr_data2
#   load(file="before_improvement.RData")
names(fdr_data2_old)[names(fdr_data2_old)=="theta_mean"] = "theta_mean_old"
library(lubridate)
fdr_data2$day = round_date(fdr_data2$time, unit = "1 day")
#tail(fdr_data2_new)
corresp_both = merge(fdr_data2, fdr_data2_old, by=c("location_id", "time"))
corresp_both$loc =substr(corresp_both$location_id, 1, stop=1)
corresp_both$op = ifelse(corresp_both$time < ISOdate(2021, 11, 12), "Konrad", "Lisa")
palette("R3");
plot( corresp_both$theta_mean_old, corresp_both$theta_mean,
pch=ifelse(corresp_both$op=="Konrad", 20, 21),
xlab="theta_alt",
ylab="theta_neu",
xlim=range(0,0.2), ylim=range(0,0.2),
col=as.factor(corresp_both$loc))
abline(b=1, a=0)
legend("topleft", legend=unique(as.factor(corresp_both$loc)), col=unique(as.factor(corresp_both$loc)), pch=20)
fdr_data2$loc=substr(fdr_data2$location_id, 1, 1)
fdr_data2_old$loc=substr(fdr_data2_old$location_id, 1, 1)
table(corresp_both$loc)
table(fdr_data2$loc)
table(fdr_data2_old$loc)
tt = fdr_data2[fdr_data2$loc=="I",]
unique(round(tt$time, unit="day"))
nrow(tt)
}
source("P:/nieplitz/Waldbrand/Bodenfeuchte/theta_processing/smt100_series/6_epsilon2theta.R", echo=TRUE)
df_temp$BD
df_temp$om_perc
df_temp$clay_perc
ring_depth
probe_locations$surface_val_om
probe_locations$subsurface_val_om
source("P:/nieplitz/Waldbrand/Bodenfeuchte/theta_processing/smt100_series/7_compare2previous.R", echo=TRUE)
source("P:/nieplitz/Waldbrand/Bodenfeuchte/theta_processing/smt100_series/7_compare2previous.R", echo=TRUE)
files
ii
length(files)
for (ii in 10:length(files))
{
print(paste0("treating  ", files[ii], " ..."))
cont      = read.table(paste0(base_dir,      files[ii]), nrow=-1, sep="\t", stringsAsFactors = FALSE, header=TRUE, na.strings = "NA", strip.white = TRUE)
cont_prev = read.table(paste0(base_dir_prev, files[ii]), nrow=-1, sep="\t", stringsAsFactors = FALSE, header=TRUE, na.strings = "NA", strip.white = TRUE)
probe_id = sub(files[ii], pattern = ".*(\\d{2}[-_]\\d{2})_eps.txt$", repl="\\1")
probe_id = sub(probe_id, pattern = "^(\\d+).*", repl="\\1")
sensors_cols = which(grepl(names(cont), pattern = "^theta"))
max_rings = length(sensors_cols)
plot(1, xlab="theta old", ylab="theta_new", xlim=plot_range, ylim=plot_range)
for(ring_no in 1:max_rings)
{
points(cont_prev[,ring_no+1], cont[,ring_no+1], col=ring_no)
abline(a=0, b=1)
text(mean(plot_range), mean(plot_range), labels = probe_id)
}
}
source("P:/nieplitz/Waldbrand/Bodenfeuchte/theta_processing/smt100_series/6_epsilon2theta.R", echo=TRUE)
7.8/4
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland/raw/generate_hexland.R", echo=TRUE)
#save as shp
st_write(hex_clip2_sf, "hexland.shp", append=FALSE)
# classify borders for tracks and export ####
library(sf)
hex_sf = hex_clip2_sf
#function to split polygons into segments (not possible with "st_cast()")
stdh_cast_substring <- function(x, to = "MULTILINESTRING") {
ggg <- st_geometry(x)
if (!unique(st_geometry_type(ggg)) %in% c("POLYGON", "LINESTRING")) {
stop("Input should be  LINESTRING or POLYGON")
}
for (k in 1:length(st_geometry(ggg))) {
sub <- ggg[k]
geom <- lapply(
1:(length(st_coordinates(sub)[, 1]) - 1),
function(i)
rbind(
as.numeric(st_coordinates(sub)[i, 1:2]),
as.numeric(st_coordinates(sub)[i + 1, 1:2])
)
) %>%
st_multilinestring() %>%
st_sfc()
if (k == 1) {
endgeom <- geom
}
else {
endgeom <- rbind(endgeom, geom)
}
}
endgeom <- endgeom %>% st_sfc(crs = st_crs(x))
if (class(x)[1] == "sf") {
endgeom <- st_set_geometry(x, endgeom)
}
if (to == "LINESTRING") {
endgeom <- endgeom %>% st_cast("LINESTRING")
}
return(endgeom)
}
hex_sf = stdh_cast_substring(hex_sf, to = "LINESTRING")  # split polygons into segments
hex_sf = hex_sf[, "size", drop=FALSE] #keep only size column
hex_sf$size = factor(hex_sf$size, levels = c("other", "quarter", "half",    "full"), ordered = TRUE) #convert to factor to allow sorting according to minimum
hex_sf = hex_sf %>% arrange(size) #sort by size of polygons
library(dplyr)
hex_sf = hex_clip2_sf
#function to split polygons into segments (not possible with "st_cast()")
stdh_cast_substring <- function(x, to = "MULTILINESTRING") {
ggg <- st_geometry(x)
if (!unique(st_geometry_type(ggg)) %in% c("POLYGON", "LINESTRING")) {
stop("Input should be  LINESTRING or POLYGON")
}
for (k in 1:length(st_geometry(ggg))) {
sub <- ggg[k]
geom <- lapply(
1:(length(st_coordinates(sub)[, 1]) - 1),
function(i)
rbind(
as.numeric(st_coordinates(sub)[i, 1:2]),
as.numeric(st_coordinates(sub)[i + 1, 1:2])
)
) %>%
st_multilinestring() %>%
st_sfc()
if (k == 1) {
endgeom <- geom
}
else {
endgeom <- rbind(endgeom, geom)
}
}
endgeom <- endgeom %>% st_sfc(crs = st_crs(x))
if (class(x)[1] == "sf") {
endgeom <- st_set_geometry(x, endgeom)
}
if (to == "LINESTRING") {
endgeom <- endgeom %>% st_cast("LINESTRING")
}
return(endgeom)
}
hex_sf = stdh_cast_substring(hex_sf, to = "LINESTRING")  # split polygons into segments
hex_sf = hex_sf[, "size", drop=FALSE] #keep only size column
hex_sf$size = factor(hex_sf$size, levels = c("other", "quarter", "half",    "full"), ordered = TRUE) #convert to factor to allow sorting according to minimum
hex_sf = hex_sf %>% arrange(size) #sort by size of polygons
hex_sf <- st_difference(hex_sf)  #discard duplicates that had resulted from adjacent hexagons
clip_line = st_cast(st_as_sf(outer_box_poly), to="LINESTRING")
hex_sf <- st_difference(hex_sf, clip_line)  #remove outer boundary (clipping line)
line_bearing <- function(line) {
delta_xy = apply(st_coordinates(line)[,1:2], MAR=2, diff)
alpha = atan(delta_xy[2]/delta_xy[1])* 180/pi
if(alpha < 0)   alpha= alpha + 180 #constrain to 0-180 degrees
if(alpha > 180) alpha= alpha - 180
return(as.numeric(alpha))
}
hex_sf$angle = round(sapply(hex_sf$geometry, line_bearing))
plot(hex_sf)
#define tracktypes
tracks = data.frame(tracktype=1:3, width = c(0, 2, 4), material=c("none", "sand", "tarmac"))
all_tracks = hex_sf %>% filter(1==2)  #create empty object
for (i in 1:nrow(tracks))
{
if (tracks$width[i]==0) next #skip tracks with zero width
angle_i = unique(hex_sf$angle)[i]
selected_tracks = hex_sf %>%
filter(angle==angle_i) %>%
select(size, angle, geometry)
buffered = st_buffer( selected_tracks, dist = tracks$width[i]/2, endCapStyle="FLAT")
buffered$tracktype = i
all_tracks = rbind(all_tracks, buffered)
#st_union(all_tracks, buffered) #geht nicht
}
all_tracks = merge(all_tracks, tracks) #add track attributes
all_tracks = all_tracks %>% arrange(desc(width))
all_tracks <- st_difference(all_tracks)  #remove overlaps
st_write(all_tracks, "hexlines_tracks.shp", append=FALSE)
# overlay track buffers with original hexagons ####
#homogenize column names to allow merging
all_tracks$ID = seq(from=max(hex_clip2_sf$ID), by=1, length.out=nrow(all_tracks))
missing_cols = setdiff(names(hex_clip2_sf), names(all_tracks))
all_tracks[, missing_cols] = NA
hex_clip2_sf$tracktype = NA
obsolete_cols = setdiff(names(all_tracks), names(hex_clip2_sf))
all_tracks[, obsolete_cols] = NULL
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
st_write(all_tracks, "hexland_tracks.shp", append=FALSE)
library(tmap)
tmap_mode("view")
tm_shape(hex_with_tracks) +
tm_polygons(col="size")
tmap_options(check.and.fix = TRUE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size")
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
st_write(all_tracks, "hexland_tracks.shp", append=FALSE)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = TRUE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
sf::st_is_valid(hex_with_tracks)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
all(sf::st_is_valid(hex_with_tracks))
nc = st_read(system.file("shape/nc.shp", package="sf"))
plot(nc)
st_combine(nc)
plot(st_combine(nc))
plot(st_union(nc))
str(st_combine(nc))
st_combine(nc)
nc
st_combine(nc)
plot(st_union(nc))
plot(nc)
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
st_write(all_tracks, "hexland_tracks.shp", append=FALSE)
hex_with_tracks
View(hex_with_tracks)
all_tracks
hex_clip2_sf
hex_with_tracks
#homogenize column names to allow merging
all_tracks$ID = seq(from=max(hex_clip2_sf$ID)+1, by=1, length.out=nrow(all_tracks))
missing_cols = setdiff(names(hex_clip2_sf), names(all_tracks))
all_tracks[, missing_cols] = NA
hex_clip2_sf$tracktype = NA
obsolete_cols = setdiff(names(all_tracks), names(hex_clip2_sf))
all_tracks[, obsolete_cols] = NULL
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
View(hex_with_tracks)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
all_tracks
rownames(all_tracks)
rownames(all_tracks) = all_tracks$ID
rownames(all_tracks)
all_tracks[, obsolete_cols] = NULL
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
View(hex_with_tracks)
hex_with_tracks = st_union(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
hex_with_tracks
View(hex_with_tracks)
View(hex_with_tracks)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
a = hex_clip2_sf[1:3]
b = hex_clip2_sf[4:6]
tm_shape(a) +
tm_polygons(col="size") +
tm_scale_bar()
a
hex_clip2_sf
hex_clip2_sf[1:3]
a = hex_clip2_sf[1:3,]
a
b = hex_clip2_sf[4:6,]
tm_shape(a) +
tm_polygons(col="size") +
tm_scale_bar()
tm_shape(b) +
tm_polygons(col="size") +
tm_scale_bar()
tm_shape(a+b) +
tm_polygons(col="size") +
tm_scale_bar()
tm_shape(rbind(a, b)) +
tm_polygons(col="size") +
tm_scale_bar()
tmap_options(check.and.fix = FALSE)
tm_shape(rbind(a, b)) +
tm_polygons(col="size") +
tm_scale_bar()
c = rbind(a, b)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
a = all_tracks[1:3, names(hex_clip2_sf)]
b = hex_clip2_sf[1:3,]
a
b
c = rbind(a, b)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
a = all_tracks[1:30, names(hex_clip2_sf)]
b = hex_clip2_sf[1:30,]
c = rbind(a, b)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
b = hex_clip2_sf[1:50,]
c = rbind(a, b)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
a = all_tracks[1:30, names(hex_clip2_sf)]
b = hex_clip2_sf[,]
c = rbind(a, b)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
a = all_tracks[, names(hex_clip2_sf)]
b = hex_clip2_sf[,]
c = rbind(a, b)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
c = hex_with_tracks
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
c = hex_with_tracks
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
c = hex_with_tracks
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
st_write(all_tracks, "hexland_tracks.shp", append=FALSE)
hex_with_tracks = st_union(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
a = all_tracks[, names(hex_clip2_sf)]
b = hex_clip2_sf[,]
c = rbind(a, b)
c = hex_with_tracks
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
st_write(hex_with_tracks, "hexland_tracks.shp", append=FALSE)
all_tracks
hex_with_tracks
hex_with_tracks = rbind(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
hex_with_tracks <- st_difference(hex_with_tracks)  #remove overlaps
st_write(hex_with_tracks, "hexland_tracks.shp", append=FALSE)
#hex_with_tracks = st_union(all_tracks[, names(hex_clip2_sf)], hex_clip2_sf)
a = all_tracks[, names(hex_clip2_sf)]
b = hex_clip2_sf[,]
c = rbind(a, b)
c = hex_with_tracks
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = FALSE)
tm_shape(c) +
tm_polygons(col="size") +
tm_scale_bar()
