Skip to content

Commit 31cb72a

Browse files
committed
Merge branch 'dev' of https://github.com/KWB-R/kwb.rabimo into dev
2 parents 8fefb3b + ae9a8f6 commit 31cb72a

4 files changed

Lines changed: 171 additions & 11 deletions

File tree

R/natural-scenario.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ data_to_natural <- function(data, type = "undeveloped")
3232
nat_data <- data
3333
nat_data[urban_columns] <- 0
3434

35+
# vegetation class 50
36+
nat_data["veg_class"] <- 50L
37+
3538
if (type == "undeveloped") {
3639
return(nat_data)
3740
}
@@ -143,7 +146,7 @@ calculate_delta_w_1 <- function(
143146
calculate_delta_w_2 <- function(
144147
natural,
145148
urban,
146-
columns_water_balance = c("surface_runoff", "infiltration", "evaporation"),
149+
columns_water_balance = c("runoff", "infiltr", "evapor"),
147150
column_code = "code"
148151
)
149152
{
@@ -173,7 +176,7 @@ calculate_delta_w_2 <- function(
173176
calculate_delta_w_3 <- function(
174177
natural,
175178
urban,
176-
columns_water_balance = c("surface_runoff", "infiltration", "evaporation"),
179+
columns_water_balance = c("runoff", "infiltr", "evapor"),
177180
column_code = "code"
178181
)
179182
{

inst/extdata/column-names.csv

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
rabimo_berlin,abimo_berlin,by_100,meaning,unit,type,data_type,default
22
code,CODE,,Unique identifier of block,-,required,character,
33
prec_yr,REGENJA,,Long-term average precipitation per year,mm,required,integer,600
4-
prec_s,REGENSO,,Long-term average precipitation per summer half year (May to October!),mm,required,integer,300
4+
prec_s,REGENSO,,Long-term average precipitation per summer half year (May to October),mm,required,integer,300
55
epot_yr,,,Long-term average potential evapotranspiration per year,mm,required,integer,700
6-
epot_s,,,Long-term average potential evapotranspiration per summer half year (May to October!),mm,required,integer,500
7-
district,BEZIRK,,Specific to Berlin: name of city district,-,required?,character,0
6+
epot_s,,,Long-term average potential evapotranspiration per summer half year (May to October),mm,required,integer,500
7+
district,BEZIRK,,Specific to Berlin: identifier of city district,-,required?,character,0
88
total_area,,,Total block area,m2,required,numeric,100
99
area_main,FLGES,,Total non-road area within the block,m2,required?,numeric,100
1010
area_road,STR_FLGES,,Total road area within the block,m2,required?,numeric,0.0
1111
main_frac,,,Non-road fraction of total block area (area_main/total_area),0..1,required,numeric,
1212
roof,PROBAU,x,Roof fraction of non-road built area,0..1,required,numeric,0.2
1313
green_roof,,,Green roof fraction of roof area,0..1,required,numeric,0.0
14-
swg_roof,KAN_BEB,x,Fraction of ... ,0..1,required,numeric,1.0
15-
pvd,PROVGU,x,Fraction of ...,0..1,required,numeric,0.6
16-
swg_pvd,KAN_VGU,x,Fraction of ...,0..1,required,numeric,0.7
14+
swg_roof,KAN_BEB,x,Fraction of roof area connected to the sewer ,0..1,required,numeric,1.0
15+
pvd,PROVGU,x, Paved fraction of non-road area,0..1,required,numeric,0.6
16+
swg_pvd,KAN_VGU,x,Fraction of paved area connected to the sewer,0..1,required,numeric,0.7
1717
srf1_pvd,BELAG1,x,Fraction of paved area belonging to surface class 1,0..1,required,numeric,0.5
1818
srf2_pvd,BELAG2,x,Fraction of paved area belonging to surface class 2,0..1,required,numeric,0.2
1919
srf3_pvd,BELAG3,x,Fraction of paved area belonging to surface class 3,0..1,required,numeric,0.1
2020
srf4_pvd,BELAG4,x,Fraction of paved area belonging to surface class 4,0..1,required,numeric,0.1
2121
srf5_pvd,BELAG5,x,Fraction of paved area belonging to surface class 5,0..1,required,numeric,0.1
2222
road_frac,,,Road fraction of total block area (area_road/total_area),0..1,required,numeric,0.0
23-
pvd_r,VGSTRASSE,x,Fraction of ...,0..1,required,numeric,0.9
24-
swg_pvd_r,KAN_STR,x,Fraction of ...,0..1,required,numeric,1.0
23+
pvd_r,VGSTRASSE,x,Paved fraction of road area,0..1,required,numeric,0.9
24+
swg_pvd_r,KAN_STR,x,Fraction of paved road area connected to the sewer,0..1,required,numeric,1.0
2525
srf1_pvd_r,STR_BELAG1,x,Fraction of road area belonging to surface class 1,0..1,required,numeric,0.9
2626
srf2_pvd_r,STR_BELAG2,x,Fraction of road area belonging to surface class 2,0..1,required,numeric,0.1
2727
srf3_pvd_r,STR_BELAG3,x,Fraction of road area belonging to surface class 3,0..1,required,numeric,0.0
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
library(terra)
2+
library(stringr)
3+
library(R.utils)
4+
5+
# Set the directory where files are stored
6+
data_dir <- "C:/Users/fdpunt/Downloads/dwd/grids_germany/monthly/evapo_p"
7+
output_dir <- "C:/Users/fdpunt/Documents/Projekte/AMAREX/Daten/Raster/epot"
8+
unzipped_dir <- file.path(data_dir, "unzipped")
9+
10+
# download and save data
11+
if(FALSE) {
12+
from <- "199101"
13+
to <- "202312"
14+
pot_evaporation_monthly <- kwb.dwd::load_potential_evaporation_berlin(from, to)
15+
}
16+
17+
# List all .asc.gz files
18+
files <- list.files(data_dir, pattern = "grids_germany_monthly_evapo_p_\\d{6}\\.asc\\.gz$", full.names = TRUE)
19+
file_names <- basename(files)
20+
21+
grid <- kwb.dwd::read_asc_gz_file(files[1])
22+
23+
# Extract year and month information
24+
timestamp <- str_extract(file_names, "\\d{6}")
25+
year <- as.numeric(substr(timestamp, 1, 4))
26+
month <- as.numeric(substr(timestamp, 5, 6))
27+
28+
# Create the data frame
29+
file_info <- data.frame(
30+
file = files,
31+
year = year,
32+
month = month
33+
)
34+
35+
districts <- sf::read_sf("C:/Users/fdpunt/Documents/Projekte/AMAREX/Daten/Bezirke/Bezirke/ISU5_UA_FL_2020_Bezirke.shp")
36+
37+
# Function to check and handle raster values before saving
38+
process_raster <- function(raster, scale = 0.1) {
39+
40+
raster <- raster * scale
41+
return(raster)
42+
}
43+
44+
# Process yearly total for all months
45+
years <- 1991:2020
46+
raster_list_year <- list()
47+
raster_list_summer <- list()
48+
49+
# Loop over each year
50+
for (i in seq_along(years)) {
51+
# Get files for the current year
52+
yr <- years[i]
53+
year_files <- file_info$file[file_info$year == yr]
54+
summer_files <- file_info$file[file_info$year == yr &
55+
file_info$month %in% 5:10]
56+
57+
# Read all monthly rasters for the current year
58+
rasters_year <- lapply(year_files, kwb.dwd::read_asc_gz_file)
59+
rasters_year_stack <- raster::stack(rasters_year)
60+
61+
# Read all summer rasters for the current year
62+
rasters_summer <- lapply(summer_files, kwb.dwd::read_asc_gz_file)
63+
rasters_summer_stack <- raster::stack(rasters_summer)
64+
65+
# Sum rasters to get the yearly total
66+
yearly_sum <- sum(rasters_year_stack, na.rm = TRUE)
67+
summer_sum <- sum(rasters_summer_stack, na.rm = TRUE)
68+
69+
# Process raster (data scaling)
70+
yearly_sum <- process_raster(yearly_sum)
71+
summer_sum <- process_raster(summer_sum)
72+
73+
# Save raster to list
74+
raster_list_year[[as.character(yr)]] <- yearly_sum
75+
raster_list_summer[[as.character(yr)]] <- summer_sum
76+
print(paste("Stored rasters for year:", yr))
77+
78+
# Define the output file path
79+
output_file_year <- file.path(output_dir, paste0("evapo_p_total_", yr, ".tif"))
80+
output_file_summer <- file.path(output_dir, paste0("evapo_p_summer_", yr, ".tif"))
81+
82+
# Save the raster to a .tif file
83+
terra::writeRaster(yearly_sum, output_file_year, overwrite = TRUE)
84+
terra::writeRaster(summer_sum, output_file_summer, overwrite = TRUE)
85+
86+
# visualize the result (for verification)
87+
plot(yearly_sum, main = paste("Yearly Sum of Potential Evaporation", yr))
88+
plot(summer_sum, main = paste("Summer Sum of Potential Evaporation", yr))
89+
}
90+
91+
# Calculate average raster
92+
years_stack <- raster::stack(raster_list_year)
93+
average_epot_year <- mean(rast(years_stack), na.rm = TRUE)
94+
95+
summers_stack <- raster::stack(raster_list_summer)
96+
average_epot_summer <- mean(rast(summers_stack), na.rm = TRUE)
97+
98+
# Reproject the shapefile to match the raster's CRS
99+
average_epot_crs <- sf::st_crs(average_epot_year)$proj4string
100+
berlin_reprojected <- sf::st_transform(districts, average_epot_crs)
101+
102+
# Save the rasters
103+
output_file <- file.path(output_dir, paste0("yearly-average_", min(years), "-", max(years), ".tif"))
104+
terra::writeRaster(average_epot_year, output_file, overwrite = TRUE)
105+
106+
output_file <- file.path(output_dir, paste0("summer-average_", min(years), "-", max(years), ".tif"))
107+
terra::writeRaster(average_epot_summer, output_file, overwrite = TRUE)
108+
109+
# Visualize the raster with the reprojected shapefile
110+
plot(average_epot_year, , main = paste0("average epot ", min(years), "-", max(years)))
111+
plot(average_epot_summer, , main = paste0("average summer epot ", min(years), "-", max(years)))
112+
113+
# mask and crop Berlin
114+
berlin_epot_avg_year <- (raster::mask(average_epot_year, berlin_reprojected[,1]))
115+
berlin_epot_avg_year_crop <- raster::crop(berlin_epot_avg_year, berlin_reprojected[,1])
116+
plot(berlin_epot_avg_year_crop,
117+
main = "mean yearly potential evaporation berlin", axes = NA)
118+
plot(berlin_reprojected[,1], col = NA, border = "white", add = TRUE)
119+
district_centroids <- sf::st_centroid(berlin_reprojected)
120+
text(sf::st_coordinates(district_centroids),
121+
labels = berlin_reprojected$bezneu, col = 'white', cex = 0.4)
122+
123+
berlin_epot_avg_summer <- (raster::mask(average_epot_summer, berlin_reprojected[,1]))
124+
berlin_epot_avg_summer_crop <- raster::crop(berlin_epot_avg_summer, berlin_reprojected[,1])
125+
plot(berlin_epot_avg_summer_crop,
126+
main = "mean summer potential evaporation berlin", axes = NA)
127+
plot(berlin_reprojected[,1], col = NA, border = "white", add = TRUE)
128+
district_centroids <- sf::st_centroid(berlin_reprojected)
129+
text(sf::st_coordinates(district_centroids),
130+
labels = berlin_reprojected$bezneu, col = 'white', cex = 0.4)
131+
132+
# Convert the sf object to a SpatVector
133+
districts_spatvector <- terra::vect(berlin_reprojected)
134+
135+
# Extract mean raster values for each district
136+
district_means_year <- terra::extract(berlin_epot_avg_year_crop,
137+
districts_spatvector,
138+
fun = mean,
139+
na.rm = TRUE)
140+
141+
district_means_summer <- terra::extract(berlin_epot_avg_summer_crop,
142+
districts_spatvector,
143+
fun = mean,
144+
na.rm = TRUE)
145+
146+
epot_berlin_1991_2020 <- berlin_reprojected %>%
147+
dplyr::select(ID = bezneu, district = bezirk) %>%
148+
dplyr::mutate(ID = as.integer(ID)) %>%
149+
sf::st_drop_geometry() %>%
150+
dplyr::left_join(district_means_year, by = "ID") %>%
151+
dplyr::mutate(total_mean = as.integer(round(mean))) %>%
152+
dplyr::select(-mean) %>%
153+
dplyr::left_join(district_means_summer, by = "ID") %>%
154+
dplyr::mutate(summer_mean = as.integer(round(mean))) %>%
155+
dplyr::select(-mean)
156+
157+

inst/scripts/test-rabimo.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if (FALSE)
5555

5656
rabimo_inputs_2020 <- kwb.rabimo::prepare_berlin_inputs(
5757
data = berlin_2020_data,
58-
config = kwb.abimo::read_config()
58+
config = kwb.abimo:::read_config()
5959
)
6060

6161
# Fehler: Column 'gw_dist' must not contain missing values (NA, found 4

0 commit comments

Comments
 (0)