Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RSO9192 committed Apr 29, 2024
1 parent 463ed83 commit ed9eca5
Show file tree
Hide file tree
Showing 28 changed files with 1,114 additions and 983 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Imports:
patchwork,
ggridges,
magrittr,
cli
Depends:
R(>= 3.5.0),
cli,
loadeR.java
Depends:
R(>= 3.5.0)
Suggests:
knitr,
rmarkdown
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export(agreement)
export(climate_change_signal)
export(common_dates)
export(convert_vector_to_month_initials)
export(ens_trends)
export(extract_raster)
export(load_data)
export(load_data_and_climate_change_signal)
Expand Down
18 changes: 1 addition & 17 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,5 @@ output:
---

## CAVAanalytics 3.0.0
Version 3.0.0 of CAVAanalytics bring several updates:

- the trends function was removed. Linear regression can now be applied to observation only by specifying trends=T in the observations function
- model agreement in the sign of climate change signal has been added. This substitute the calculation of trends for future projections
- temporal and spatiotemporal patterns can now be visualized by directly specifying temporal=T or spatiotemporal=T in plotting where allowed

## CAVAanalytics 2.1.0
Version 2.1.0 of CAVAanalytics bring several updates:

- the country argument in load_data can now take an sf object
- the memory efficient functions (load_data_and_projections and so forth) can now take a country name or sf object in the country argument
- load_data_and_model_biases function has been added
- Better handling of messages and errors in the plotting function
- xarray option was removed from the load_data function. This is because CORDEX data is now stored in new THREDDS servers which allow fast ad multi-thread data download
- Improved facets layout in plotting results

## CAVAanalytics 2.0.4
first stable release
Version 3.0.0 of CAVAanalytics is the first stable release. Follow the tutorial to learn how to use it.
2 changes: 1 addition & 1 deletion R/climate_change_signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ climate_change_signal <- function(data,
"SparRaster stack for ccs sd",
"SpatRaster stack for individual members",
"SpatRaster stack for ccs agreement",
"dataframe for spatially aggregated data"
"dataframe for spatially and annually aggregated data"

)
))
Expand Down
68 changes: 47 additions & 21 deletions R/load_data_and_climate_change_signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ load_data_and_climate_change_signal <-
frequency = F,
bias.correction = F,
domain = NULL,
threshold=0.6,
threshold = 0.6,
n.sessions = 6) {
# calculate number of chunks based on xlim and ylim
if (missing(chunk.size) | missing(season)) {
Expand All @@ -60,7 +60,9 @@ load_data_and_climate_change_signal <-
)
}


cli::cli_alert_warning(
"Interpolation may be required to merge the rasters. Be aware that interpolation can introduce slight discrepancies in the data, potentially affecting the consistency of results across different spatial segments."
)

country_shp = if (!is.null(country) &
!inherits(country, "sf")) {
Expand Down Expand Up @@ -89,6 +91,11 @@ load_data_and_climate_change_signal <-
sf::st_set_crs(., NA)
}

lon_range <-
c(sf::st_bbox(country_shp)[[1]], sf::st_bbox(country_shp)[[3]])
lat_range <-
c(sf::st_bbox(country_shp)[[2]], sf::st_bbox(country_shp)[[4]])

x_chunks <- seq(from = xlim[1], to = xlim[2], by = chunk.size)
y_chunks <- seq(from = ylim[1], to = ylim[2], by = chunk.size)

Expand All @@ -100,6 +107,12 @@ load_data_and_climate_change_signal <-
ylim
else
y_chunks

#making sure the whole area is loaded
if (x_chunks[length(x_chunks)] < lon_range[2])
x_chunks[length(x_chunks) + 1] = lon_range[2]
if (y_chunks[length(y_chunks)] < lat_range[2])
y_chunks[length(y_chunks) + 1] = lat_range[2]
# create empty list to store output
out_list <- list()

Expand Down Expand Up @@ -163,52 +176,65 @@ load_data_and_climate_change_signal <-
}
}
cli::cli_progress_step("Merging rasters")
# Extract the first, second, and third elements of each list in `out_list`
# Extract elements of each list in `out_list`
rst_mean <- lapply(out_list, `[[`, 1)
rst_sd <- lapply(out_list, `[[`, 2)
rst_mbrs <- lapply(out_list, `[[`, 3)
rst_agree <- lapply(out_list, `[[`, 4)
# Merge the extracted rasters using `Reduce` and set their names
df_temp <-
do.call(rbind, lapply(out_list, `[[`, 5)) %>% # spatial average of all chunks
dplyr::group_by(date, experiment, Var1, season) %>%
dplyr::summarise(value = median(value, na.rm = T))

merge_rasters <- function(rst_list) {
# Determine the smallest (finest) resolution among all rasters
resolutions <- sapply(rst_list, function(r)
terra::res(r))
common_res <- min(resolutions)

# Resample all rasters to the common resolution
resampled_rasters <- lapply(rst_list, function(r) {
terra::resample(r,
terra::rast(
terra::ext(r),
resolution = common_res,
crs = terra::crs(r)
),
method = "bilinear")
})

# Merge the resampled rasters
merged_raster <-
Reduce(function(x, y)
terra::merge(x, y), resampled_rasters)

#Set names from the first raster in the list
names <- names(rst_list[[1]])
Reduce(function(...)
terra::merge(...), rst_list) %>% setNames(names)
setNames(merged_raster, names)
}

cli::cli_process_done()

rasters_mean <- tryCatch(
expr = merge_rasters(rst_mean),
warning = function(w) {
# Translate the warning into something more understandable
translated_warning <-
"Terra had to interpolate your SpatRasters to merge them. You can ignore this warning if you used sensible values for overalp and chunk_size arguments"
# ... handle the warning ...
# You can print the translated warning, log it, or perform any other action
cli::cli_alert_warning(translated_warning)
}
)
rasters_mean <- merge_rasters(rst_mean)
rasters_sd <- merge_rasters(rst_sd)
rasters_mbrs <- merge_rasters(rst_mbrs)
rasters_agree <- merge_rasters(rst_agree)



invisible(structure(
list(
rasters_mean %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
rasters_sd %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
rasters_mbrs %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
rasters_agree %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
NULL
df_temp
),
class = "CAVAanalytics_ccs",
components = list(
"SpatRaster for ensemble mean",
"SpatRaster for ensemble sd",
"SpatRaster for individual members",
"SpatRaster stack for ccs agreement",
"dataframe for spatially aggregated data"
"dataframe for spatially and annually aggregated data"
)
))

Expand Down
54 changes: 38 additions & 16 deletions R/load_data_and_model_biases.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ load_data_and_model_biases <-
)
}


cli::cli_alert_warning(
"Interpolation may be required to merge the rasters. Be aware that interpolation can introduce slight discrepancies in the data, potentially affecting the consistency of results across different spatial segments."
)
country_shp = if (!is.null(country) &
!inherits(country, "sf")) {
suppressMessages(
Expand Down Expand Up @@ -87,6 +89,11 @@ load_data_and_model_biases <-
sf::st_set_crs(., NA)
}

lon_range <-
c(sf::st_bbox(country_shp)[[1]], sf::st_bbox(country_shp)[[3]])
lat_range <-
c(sf::st_bbox(country_shp)[[2]], sf::st_bbox(country_shp)[[4]])

x_chunks <- seq(from = xlim[1], to = xlim[2], by = chunk.size)
y_chunks <- seq(from = ylim[1], to = ylim[2], by = chunk.size)
x_chunks <- if (length(x_chunks) < 2)
Expand All @@ -97,6 +104,12 @@ load_data_and_model_biases <-
ylim
else
y_chunks

#making sure the whole area is loaded
if (x_chunks[length(x_chunks)] < lon_range[2])
x_chunks[length(x_chunks) + 1] = lon_range[2]
if (y_chunks[length(y_chunks)] < lat_range[2])
y_chunks[length(y_chunks) + 1] = lat_range[2]
# create empty list to store output
out_list <- list()

Expand Down Expand Up @@ -164,28 +177,37 @@ load_data_and_model_biases <-
rst_mbrs <- lapply(out_list, `[[`, 2)
# Merge the extracted rasters using `Reduce` and set their names
merge_rasters <- function(rst_list) {
# Determine the smallest (finest) resolution among all rasters
resolutions <- sapply(rst_list, function(r)
terra::res(r))
common_res <- min(resolutions)

# Resample all rasters to the common resolution
resampled_rasters <- lapply(rst_list, function(r) {
terra::resample(r,
terra::rast(
terra::ext(r),
resolution = common_res,
crs = terra::crs(r)
),
method = "bilinear")
})

# Merge the resampled rasters
merged_raster <-
Reduce(function(x, y)
terra::merge(x, y), resampled_rasters)

#Set names from the first raster in the list
names <- names(rst_list[[1]])
Reduce(function(...)
terra::merge(...), rst_list) %>% setNames(names)
setNames(merged_raster, names)
}

cli::cli_process_done()

rasters_mean <- tryCatch(
expr = merge_rasters(rst_mean),
warning = function(w) {
# Translate the warning into something more understandable
translated_warning <-
"Terra had to interpolate your SpatRasters to merge them. You can ignore this warning if you used sensible values for overalp and chunk_size arguments"
# ... handle the warning ...
# You can print the translated warning, log it, or perform any other action
cli::cli_alert_warning(translated_warning)
}
)
rasters_mean <- merge_rasters(rst_mean)
rasters_mbrs <- merge_rasters(rst_mbrs)



invisible(structure(
list(
rasters_mean %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
Expand Down
59 changes: 45 additions & 14 deletions R/load_data_and_projections.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ load_data_and_projections <- function(variable,
)
}

cli::cli_alert_warning(
"Interpolation may be required to merge the rasters. Be aware that interpolation can introduce slight discrepancies in the data, potentially affecting the consistency of results across different spatial segments."
)

country_shp = if (!is.null(country) & !inherits(country, "sf")) {
suppressMessages(
Expand Down Expand Up @@ -84,6 +87,11 @@ load_data_and_projections <- function(variable,
sf::st_set_crs(., NA)
}

lon_range <-
c(sf::st_bbox(country_shp)[[1]], sf::st_bbox(country_shp)[[3]])
lat_range <-
c(sf::st_bbox(country_shp)[[2]], sf::st_bbox(country_shp)[[4]])

x_chunks <- seq(from = xlim[1], to = xlim[2], by = chunk.size)
y_chunks <- seq(from = ylim[1], to = ylim[2], by = chunk.size)
x_chunks <- if (length(x_chunks) < 2)
Expand All @@ -94,6 +102,12 @@ load_data_and_projections <- function(variable,
ylim
else
y_chunks

#making sure the whole area is loaded
if (x_chunks[length(x_chunks)] < lon_range[2])
x_chunks[length(x_chunks) + 1] = lon_range[2]
if (y_chunks[length(y_chunks)] < lat_range[2])
y_chunks[length(y_chunks) + 1] = lat_range[2]
# create empty list to store output
out_list <- list()

Expand Down Expand Up @@ -162,26 +176,43 @@ load_data_and_projections <- function(variable,
rst_mean <- lapply(out_list, `[[`, 1)
rst_sd <- lapply(out_list, `[[`, 2)
rst_mbrs <- lapply(out_list, `[[`, 3)
df_temp <-
do.call(rbind, lapply(out_list, `[[`, 4)) %>% # spatial average of all chunks
dplyr::group_by(date, experiment, Var1, season) %>%
dplyr::summarise(value = median(value, na.rm = T))
# Merge the extracted rasters using `Reduce` and set their names

merge_rasters <- function(rst_list) {
# Determine the smallest (finest) resolution among all rasters
resolutions <- sapply(rst_list, function(r)
terra::res(r))
common_res <- min(resolutions)

# Resample all rasters to the common resolution
resampled_rasters <- lapply(rst_list, function(r) {
terra::resample(r,
terra::rast(
terra::ext(r),
resolution = common_res,
crs = terra::crs(r)
),
method = "bilinear")
})

# Merge the resampled rasters
merged_raster <-
Reduce(function(x, y)
terra::merge(x, y), resampled_rasters)

#Set names from the first raster in the list
names <- names(rst_list[[1]])
Reduce(function(...)
terra::merge(...), rst_list) %>% setNames(names)
setNames(merged_raster, names)
}


cli::cli_process_done()

rasters_mean <- tryCatch(
expr = merge_rasters(rst_mean),
warning = function(w) {
# Translate the warning into something more understandable
translated_warning <-
"Terra had to interpolate your SpatRasters to merge them. You can ignore this warning if you used sensible values for overalp and chunk_size arguments"
# ... handle the warning ...
# You can print the translated warning, log it, or perform any other action
cli::cli_alert_warning(translated_warning)
}
)
rasters_mean <- merge_rasters(rst_mean)
rasters_sd <- merge_rasters(rst_sd)
rasters_mbrs <- merge_rasters(rst_mbrs)

Expand All @@ -191,7 +222,7 @@ load_data_and_projections <- function(variable,
rasters_mean %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
rasters_sd %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
rasters_mbrs %>% terra::crop(., country_shp) %>% terra::mask(., country_shp),
NULL
df_temp
),
class = "CAVAanalytics_projections",
components = list(
Expand Down
Loading

0 comments on commit ed9eca5

Please sign in to comment.