Hi everyone,
i am encountering a similar error. I donwnloaded the monthly era5 data, merged them using Kris's function and the regular micro_era5 runs are sensible (validated on some weather station data) although i do keep getting some rainfall warnings. They also give good predictions with ectotherm (deb=1).
Now, when i try to run the scenario = 2 or 4 things get out of hand, both TREF and TALOC are way too high, im talking about 70 degrees in the summer (values are almost double of what one would expect). Please find attached the regular scenario=0 test runs with observed temp data i used to validate the micro_era5.
If anyone has any advice im very much open to suggestions. I think i still might have some issues with compatibility between the emrged era5 data and the terracliamte scenarios data.
Here is my code to generate the micro objects in a loop, due to issues with snowfall and model crashing at low ERR values im permiting quite a high ERR but most locations work with the default value.
Cheers
Urban
minshade <- 0
maxshade <- 90
Thcond <- 2.5
SpecHeat <- 870
Density <- 2.56
BulkDensity <- 2.55
windfac <- 1
REFL <- 0.2
cap <- 0
SLE <- 0.95
warm <- 0
Usrhyt <- 0.01
clearsky <- 0
ERR <- 1.5
DEP <- c(0, 3, 5, 10, 15, 20, 30, 50, 100, 200)
scenario <- 2
dstart <- "01/01/1990"
dfinish <- "31/12/2010"
getwd()
# Loop over each location
for (i in 1:nrow(locations_df)) {
ERR <- 1.5
# Extract location details
name <- locations_df$Location[i]
lat <- locations_df$Latitude[i]
lon <- locations_df$Longitude[i]
# Define the save path for the location
save_path <- file.path(base_path, name)
# Create the directory if it doesn't exist
if (!dir.exists(save_path)) {
dir.create(save_path, recursive = TRUE)
}
# Change working directory to the new folder
setwd(save_path)
# Get DEM data
dem <- microclima::get_dem(r = NA, lat = lat, lon = lon, resolution = 30, zmin = -20, xdims = 100, ydims = 100)
# Convert coordinates to a SpatVector and transform to match DEM CRS
xy <- terra::vect(data.frame(x = lon, y = lat), geom = c("x", "y"), crs = "EPSG:4326")
xy <- terra::project(xy, terra::crs(dem))
# Extract elevation using the same method as slope
elev <- terra::extract(dem, xy, method = "simple")[,2]
# Compute slope and extract value at the location
slope <- terra::extract(terra::terrain(dem, v = "slope", unit = "degrees"), xy)[,2]
# Compute aspect and extract value at the location
aspect <- terra::extract(terra::terrain(dem, v = "aspect", unit = "degrees"), xy)[,2]
# Compute horizon angles
ha36 <- numeric(36)
for (j in 0:35) {
har <- microclima::horizonangle(dem, j * 10, terra::res(dem)[1])
ha36[j + 1] <- atan(terra::extract(har, xy)[,2]) * (180/pi)
}
# Interpolate horizon angles and constrain to [0,90]
hori <- spline(x = ha36, n = 24, method = 'periodic')$y
hori[hori < 0] <- 0
hori[hori > 90] <- 90
# Run micro_era5
micro <- micro_era5(
SLE = SLE,
warm = warm,
soilgrids = 0,
dstart = dstart,
dfinish = dfinish,
Usrhyt = Usrhyt,
DEP = DEP,
REFL = REFL,
minshade = minshade,
maxshade = maxshade,
loc = c(lon, lat),
runshade = 1,
run.gads = 1,
snowmodel = 1,
BulkDensity = BulkDensity,
cap = cap,
runmoist = 1,
scenario = scenario,
Density = Density,
Thcond = Thcond,
SpecHeat = SpecHeat,
slope = slope,
aspect = aspect,
hori = hori,
dem = dem,
windfac = windfac,
ERR = ERR,
save = 1,
spatial = '../../../Micro_era5/Urban_downloads_era5/ERA5_yearly/ERA5',
cad.effects = FALSE
)
while(min(micro$metout[,1]) == 0 & ERR <= 3.5){
cat("model crashed, trying a higher error tolerance \n")
ERR <- ERR + 0.5
micro <- micro_era5(
SLE = SLE,
warm = warm,
soilgrids = 0,
dstart = dstart,
dfinish = dfinish,
Usrhyt = Usrhyt,
DEP = DEP,
REFL = REFL,
minshade = minshade,
maxshade = maxshade,
loc = c(lon, lat),
runshade = 1,
run.gads = 1,
snowmodel = 1,
BulkDensity = BulkDensity,
cap = cap,
runmoist = 1,
scenario = scenario,
Density = Density,
Thcond = Thcond,
SpecHeat = SpecHeat,
slope = slope,
aspect = aspect,
hori = hori,
dem = dem,
windfac = windfac,
ERR = ERR,
save = 2,
spatial = '../../../Micro_era5/Urban_downloads_era5/ERA5_yearly/ERA5',
cad.effects = FALSE
)
gc()
}
setwd(base_path)
if(!(min(micro$metout[,1]) == 0 & ERR <= 3)){
# Return to main working directory to save results
# Save the output file in the newly created directory
save(micro, file = file.path(base_path, paste0("micro_era5_2_", name, ".RData")))
# Print progress
message("Finished processing ", name, ". Results saved in: ", base_path)
}else{
message("Failed processing ", name)
}
}