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) #write classified track areas
#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))
rownames(all_tracks) = all_tracks$ID
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(hex_with_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()
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)
library(sf)
library(dplyr)
hex_clip2_sf = st_read(dsn = "hexland.shp") #polygons
hex_sf = hex_clip2_sf
tm_shape(hex_clip2) +
tm_polygons(col="size") +
tm_scale_bar()
tm_shape(hex_clip2_sf) +
tm_polygons(col="size") +
tm_scale_bar()
#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_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
plot(hex_sf)
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
plot(hex_sf)
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)
clip_line = st_bbox(hex_sf)
clip_line
outer_box
outer_box_poly = SpatialPolygons(list(Polygons(list(Polygon(clip_line)), "x")))
clip_line =
st_as_sfc(st_bbox(hex_sf))
st_as_sfc(st_bbox(hex_sf))
clip_line = st_cast(st_as_sf(clip_line), to="LINESTRING")
hex_sf <- st_difference(hex_sf, clip_line)  #remove outer boundary (clipping line)
plot(hex_sf)
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)
#st_write(hex_sf, "hexlines_unique.shp", append=FALSE)
# buffer borders for getting track extents ####
tracks = data.frame(tracktype=1:3, width = c(0, 2, 4), material=c("none", "sand", "tarmac")) #define tracktypes
all_tracks = hex_sf %>% filter(1==2)  #create empty object
#all_tracks = st_cast(all_tracks, to = "POLYGON")
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)) #order by width to make the wide tracks dominate in the overlay eliminiation
all_tracks <- st_difference(all_tracks)  #remove overlaps
#st_write(all_tracks, "hexlines_tracks.shp", append=FALSE) #write classified track areas
# overlay track buffers with original hexagons ####
#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))
rownames(all_tracks) = all_tracks$ID
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(hex_with_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()
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland/raw/generate_hexland.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
# add tracks to polygonal pattern
# classify borders for tracks and export ####
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)
library(sf)
library(dplyr)
hex_clip2_sf = st_read(dsn = "../../hexland/pre_processed/pattern.shp") #polygons
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
#table(hex_sf$size)
hex_sf <- st_difference(hex_sf)  #discard duplicates that had resulted from adjacent hexagons
clip_line = st_as_sfc(st_bbox(hex_sf)) #remove outer margin, as it is no real boundary
clip_line = st_cast(st_as_sf(clip_line), to="LINESTRING")
hex_sf <- st_difference(hex_sf, clip_line)  #remove outer boundary (clipping line)
plot(hex_sf)
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)
#st_write(hex_sf, "hexlines_unique.shp", append=FALSE)
# buffer borders for getting track extents ####
tracks = data.frame(tracktype=1:3, width = c(0, 2, 4), material=c("none", "sand", "tarmac")) #define tracktypes
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
}
library(dplyr)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
selected_tracks = hex_sf %>%
filter(angle==angle_i)
selected_tracks = hex_sf %>%
filter(angle==angle_i) %>%
select(size, angle, geometry)
selected_tracks = hex_sf %>%
filter(angle==angle_i)
selected_tracks
all_tracks = hex_sf %>% filter(1==2)  #create empty object
#all_tracks = st_cast(all_tracks, to = "POLYGON")
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)) #order by width to make the wide tracks dominate in the overlay eliminiation
all_tracks <- st_difference(all_tracks)  #remove overlaps
#st_write(all_tracks, "hexlines_tracks.shp", append=FALSE) #write classified track areas
# overlay track buffers with original hexagons ####
#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))
rownames(all_tracks) = all_tracks$ID
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(hex_with_tracks, "../pre_processed/hpattern.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()
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex/raw/generate_hexland_veg.R", echo=TRUE)
install.packages("fasterize")
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex_track_inwork/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex_track_inwork/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex_track_inwork/raw/generate_hexland_veg.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/vegetation/hex_track_inwork/raw/generate_hexland_veg.R", echo=TRUE)
unique(pattern_poly$vegetat)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
all_tracks
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
tmap_mode("view")
tmap_options(check.and.fix = TRUE)
tm_shape(hex_with_tracks) +
tm_polygons(col="size") +
tm_scale_bar()
st_write(hex_with_tracks, "../pre_processed/pattern.shp", append=FALSE)
hex_with_tracks
all_tracks
View(all_tracks)
View(all_tracks)
all_tracks = hex_sf %>% filter(1==2)  #create empty object
all_tracks
for (i in 1:nrow(codes))
{
if (codes$width..m.[i]==0) next #skip codes 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 = codes$width..m.[i]/2, endCapStyle="FLAT")
buffered$tracktype = codes$ID[i]
all_tracks = rbind(all_tracks, buffered)
#st_union(all_tracks, buffered) #geht nicht
}
all_tracks
plot(all_tracks)
all_tracks = merge(all_tracks, codes, by.x="tracktype", by.y="ID") #add track attributes
all_tracks
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
hex_with_tracks
rast_temp = raster(hex_with_tracks, origin=0, resolution=c(dx, dy))
source("../../../../settings/settings.R") #load desired extents
rast_temp = raster(hex_with_tracks, origin=0, resolution=c(dx, dy))
pattern_rast = fasterize(sf=hex_with_tracks, field = "ID", raster = rast_temp)
library(fasterize)
pattern_rast = fasterize(sf=hex_with_tracks, field = "ID", raster = rast_temp)
hex_with_tracks
tt = st_cast(st_as_sf(clip_poly), to="POLYGON")
pattern_rast = fasterize(sf=tt, field = "ID", raster = rast_temp)
tt = st_cast(st_as_sf(hex_with_tracks), to="POLYGON")
tt = st_cast(st_as_sf(hex_with_tracks), to="MULTIPOLYGON")
pattern_rast = fasterize(sf=tt, field = "ID", raster = rast_temp)
plot(pattern_rast, col=palette("R3"))
write.table(file= "hexland_tracks.asc", patter_rast, sep="\t")
write.table(file= "hexland_tracks.asc", pattern_rast, sep="\t")
write.table(file= "hexland_tracks.asc", pattern_rast@data, sep="\t")
pattern_rast@data
write.table(file= "hexland_tracks.asc", as.matrix(pattern_rast), sep="\t")
write.table(file= "../pre_processed/hexland_tracks.dat", as.matrix(pattern_rast), sep="\t", row.names=FALSE, header=FALSE)
write.table(file= "../pre_processed/hexland_tracks.dat", as.matrix(pattern_rast), sep="\t", row.names=FALSE, col.names=FALSE)
plot(tt)
plot(pattern_rast)
plot(hex_with_tracks)
library(tmap)
tmap_mode("view")
tmap_options(check.and.fix = TRUE)
tm_shape(hex_with_tracks) +
#tm_shape(all_tracks) +
tm_polygons(col="size") +
# tm_shape(clip_line) +
#   tm_lines(col="black") +
tm_scale_bar()
source("E:/till/uni/crns/vJFC/0_compartments/patterns/hexland_tracks/raw/add_tracks.R", echo=TRUE)
dat =read.table("./allResults.csv", header = TRUE, sep=",")
setwd("E:/till/uni/crns/vJFC/gravimetric_simulations/Hexland_tracks")
dat =read.table("./allResults.csv", header = TRUE, sep=",")
dat$origin = NULL
dat$topoFactor = NULL
dat$g_sm = NULL
#summary(dat)
dat = as.matrix(dat)
dat[,3:4] = ceiling(dat[,3:4]) #convert "grid-centers" to indices
dat2matrix <- function(dat, src_col)
{
#src_col = 1 #column to use the data from
outmat = array(NA, c(1000,1000))
for (i in 1:nrow(dat))
{
coords = dat[i,4:3, drop=FALSE]
outmat[coords] = dat[i, src_col]
}
return(outmat)
}
gwet = dat2matrix(dat,1)
gdry = dat2matrix(dat,2)
gdry=gdry[rev(1:nrow(gdry)),] #dry matrix was stored "wrong way"
g_sm= gdry-gwet
rot4plot <-  function(x){t(apply(x, 2, rev))} #rotate matrix for correct plotting
windows()
image(z=rot4plot(gdry), useRaster = TRUE)
save_ncdf <- function(outmat, dest) {
# Create a netCDF file with this variable
library(ncdf4)
# Define spatial dimensions of netcdf
n_x=n_y=1000
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 = 0.5)
n_z = length(dim_z$vals)
dim_zmin <- ncdim_def(name = "zmin_", units = "m", vals = 0.5) #this is for the lower boundary of the lowermost layer
zmin = 0.5
# 3D-grid (actually, its only 2-D, but we store it as 3D to allow handling with the standard methods)
#define variable
varZmin       <- ncvar_def(name = "zmin", units = "m",
dim= list(dim_zmin), prec="float")
varGravity <- ncvar_def(name = "gravity", units = "mgal",
dim= list(dim_y, dim_x, dim_z), compression = 9,
longname="gravity", prec="double", missval = -9999)
#dest_dir="./"
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest), list(varGravity, varZmin))
ncvar_put(nc = ncnew, varid = varZmin, vals = zmin, start=1, count=1) #this is for the lower boundary of the lowermost layer
ncvar_put( nc = ncnew, varid = varGravity, vals = outmat, start=c(1,1,1), count=c(n_y, n_x, n_z ))
nc_close(ncnew)
}
save_ncdf(gdry, "../Hexland_tracks_dry/gravity.nc")
write.table(file="../Hexland_tracks_dry/gravity.dat", gwet, row.names = FALSE, col.names = FALSE, sep="\t", quote=FALSE)
savePlot("../Hexland_tracks_dry/gravity.png", type = "png")
save_ncdf(gdry, "../Hexland_tracks_dry/gravity.nc")
write.table(file="../Hexland_tracks_dry/gravity.dat", gdry, row.names = FALSE, col.names = FALSE, sep="\t", quote=FALSE)
savePlot("../Hexland_tracks_dry/gravity.png", type = "png")
dat =read.table("../Hexland_tracks_0/allResults.csv", header = TRUE, sep=",")
#dat =read.table("./allResults.csv", header = TRUE, sep=",")
dat$origin = NULL
dat$topoFactor = NULL
dat$g_sm = NULL
#summary(dat)
dat = as.matrix(dat)
dat[,3:4] = ceiling(dat[,3:4]) #convert "grid-centers" to indices
dat2matrix <- function(dat, src_col)
{
#src_col = 1 #column to use the data from
outmat = array(NA, c(1000,1000))
for (i in 1:nrow(dat))
{
coords = dat[i,4:3, drop=FALSE]
outmat[coords] = dat[i, src_col]
}
return(outmat)
}
gwet = dat2matrix(dat,1)
gdry = dat2matrix(dat,2)
gdry=gdry[rev(1:nrow(gdry)),] #dry matrix was stored "wrong way"
g_sm= gdry-gwet
rot4plot <-  function(x){t(apply(x, 2, rev))} #rotate matrix for correct plotting
windows()
image(z=rot4plot(gdry), useRaster = TRUE)
save_ncdf <- function(outmat, dest) {
# Create a netCDF file with this variable
library(ncdf4)
# Define spatial dimensions of netcdf
n_x=n_y=1000
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 = 0.5)
n_z = length(dim_z$vals)
dim_zmin <- ncdim_def(name = "zmin_", units = "m", vals = 0.5) #this is for the lower boundary of the lowermost layer
zmin = 0.5
# 3D-grid (actually, its only 2-D, but we store it as 3D to allow handling with the standard methods)
#define variable
varZmin       <- ncvar_def(name = "zmin", units = "m",
dim= list(dim_zmin), prec="float")
varGravity <- ncvar_def(name = "gravity", units = "mgal",
dim= list(dim_y, dim_x, dim_z), compression = 9,
longname="gravity", prec="double", missval = -9999)
#dest_dir="./"
# Create a netCDF file with this variable
ncnew <- nc_create(paste0(dest), list(varGravity, varZmin))
ncvar_put(nc = ncnew, varid = varZmin, vals = zmin, start=1, count=1) #this is for the lower boundary of the lowermost layer
ncvar_put( nc = ncnew, varid = varGravity, vals = outmat, start=c(1,1,1), count=c(n_y, n_x, n_z ))
nc_close(ncnew)
}
save_ncdf(gwet, "../Hexland_tracks_0/gravity.nc")
write.table(file="../Hexland_tracks_0/gravity.dat", gwet, row.names = FALSE, col.names = FALSE, sep="\t", quote=FALSE)
save_ncdf(gdry, "../Hexland_tracks_0_dry/gravity.nc")
write.table(file="../Hexland_tracks_0_dry/gravity.dat", gdry, row.names = FALSE, col.names = FALSE, sep="\t", quote=FALSE)
savePlot("../Hexland_tracks_0_dry/gravity.png", type = "png")
