Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions R/secondary_weights.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
#' @export
secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full"){

## check to make sure climate raster is a spatraster, change if not
if (!inherits(grid, "SpatRaster")) {
clim_raster <- terra::rast(grid)
} else {

clim_raster <- grid

}


## Return error if terra::extent can't inherit from the value supplied
## won't be able to check if secondary raster fully overlaps if
## this input isn't compatible with terra::extent()
Expand Down Expand Up @@ -72,14 +82,6 @@ secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full

}

## check to make sure climate raster is a spatraster, change if not
if (!inherits(grid, "SpatRaster")) {
clim_raster <- terra::rast(grid)
} else {

clim_raster <- grid

}

## check if SpatRaster is in geographic coodrinates
if(!terra::is.lonlat(clim_raster)) {
Expand Down Expand Up @@ -131,7 +133,8 @@ secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full

extent <- extent

}
}


## If an extent was included, crop it to the extent to save ram
## -----------------------------------------------
Expand Down Expand Up @@ -208,6 +211,15 @@ secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full

}

# Reproject if necessary
if(terra::crs(secondary_raster, TRUE) != terra::crs(grid, TRUE)){

message(crayon::yellow("Warning: reprojecting secondary_raster to match grid, which may affect data"))
secondary_raster <- terra::project(secondary_raster, clim_raster)

}



## crop the ERA/climate raster to the appropriate extent
## use the extent of the previously user-cropped secondary raster
Expand All @@ -216,9 +228,6 @@ secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full
# Find the difference between the climate raster resolution and secondary raster resolution
clim_raster <- terra::crop(clim_raster, terra::ext(secondary_raster), snap="out")

## set crs of secondary raster to match climate data
## -----------------------------------------------
terra::crs(secondary_raster) <- terra::crs(clim_raster)

## check if the cropped secondary raster contains NA values
if(isTRUE(any(is.na(terra::values(secondary_raster, na.rm=FALSE))))) {
Expand Down
43 changes: 22 additions & 21 deletions R/staggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,14 @@ polygon_aggregation <- function(clim_dt, weights_dt, list_names, time_agg, weigh
#' unless `daily_agg = 'none'`)
#' @param start_date the date (and time, if applicable) of the first layer in
#' the stack. To be input in a format compatible with
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
#' The default is `NA` since the spatRasters usually already contain temporal
#' information in the layer names and they do not need to be manually supplied.
#' @param time_interval the time interval between layers in the spatRaster to be
#' aggregated. To be input in a format compatible with seq(), e.g.
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
#' `NA`.
#' aggregated. To be input in a format compatible with
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
#' not `'none'` or if the `start_date` argument is not `NA`.
#' @param weights_join_tolerance the tolerance to use when joining
#' overlay_weights with the climate data by the x and y columns. This is useful
#' when the height/width of your data cells expressed in degrees is a very long
Expand Down Expand Up @@ -588,14 +588,14 @@ staggregate_polynomial <- function(data, overlay_weights, daily_agg, time_agg =
#' unless `daily_agg = 'none'`)
#' @param start_date the date (and time, if applicable) of the first layer in
#' the stack. To be input in a format compatible with
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
#' The default is `NA` since the spatRasters usually already contain temporal
#' information in the layer names and they do not need to be manually supplied.
#' @param time_interval the time interval between layers in the spatRaster to be
#' aggregated. To be input in a format compatible with seq(), e.g.
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
#' `NA`.
#' aggregated. To be input in a format compatible with
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
#' not `'none'` or if the `start_date` argument is not `NA`.
#' @param weights_join_tolerance the tolerance to use when joining
#' overlay_weights with the climate data by the x and y columns. This is useful
#' when the height/width of your data cells expressed in degrees is a very long
Expand Down Expand Up @@ -773,14 +773,14 @@ staggregate_spline <- function(data, overlay_weights, daily_agg, time_agg = "mon
#' unless `daily_agg = 'none'`)
#' @param start_date the date (and time, if applicable) of the first layer in
#' the raster. To be input in a format compatible with
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
#' The default is `NA` since the rasters usually already contain temporal
#' information in the layer names and they do not need to be manually supplied.
#' @param time_interval the time interval between layers in the raster to be
#' aggregated. To be input in a format compatible with seq(), e.g.
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
#' `NA`.
#' @param time_interval the time interval between layers in the spatRaster to be
#' aggregated. To be input in a format compatible with
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
#' not `'none'` or if the `start_date` argument is not `NA`.
#' @param weights_join_tolerance the tolerance to use when joining
#' overlay_weights with the climate data by the x and y columns. This is useful
#' when the height/width of your data cells expressed in degrees is a very long
Expand Down Expand Up @@ -958,13 +958,14 @@ staggregate_bin <- function(data, overlay_weights, daily_agg, time_agg = "month"
#' `'month'`, or `'year'`
#' @param start_date the date (and time, if applicable) of the first layer in
#' the raster. To be input in a format compatible with
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
#' The default is `NA` since the rasters usually already contain temporal
#' information in the layer names and they do not need to be manually supplied.
#' @param time_interval the time interval between layers in the raster to be
#' aggregated. To be input in a format compatible with seq(), e.g.
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
#' required if the `start_date` argument is not `NA`.
#' @param time_interval the time interval between layers in the spatRaster to be
#' aggregated. To be input in a format compatible with
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
#' not `'none'` or if the `start_date` argument is not `NA`.
#' @param weights_join_tolerance the tolerance to use when joining
#' overlay_weights with the climate data by the x and y columns. This is useful
#' when the height/width of your data cells expressed in degrees is a very long
Expand Down
24 changes: 8 additions & 16 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ polynomial_output <- staggregate_polynomial(

data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
# typically but not necessarily climate
# data. For now, data must start at
# midnight and be hourly. We're
# converting from Kelvin to Celsius
# here.
# data. We're converting from Kelvin to
# Celsius here.

overlay_weights = county_weights, # Output from Step 2, determined here by
# area-normalized cropland weights for grid
Expand Down Expand Up @@ -197,10 +195,8 @@ spline_output <- staggregate_spline(

data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
# typically but not necessarily climate
# data. For now, data must start at
# midnight and be hourly. We're
# converting from Kelvin to Celsius
# here.
# data. We're converting from Kelvin to
# Celsius here.

overlay_weights = county_weights, # Output from Step 2, determined here by
# area-normalized cropland weights for grid
Expand Down Expand Up @@ -235,10 +231,8 @@ bin_output <- staggregate_bin(

data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
# typically but not necessarily climate
# data. For now, data must start at
# midnight and be hourly. We're
# converting from Kelvin to Celsius
# here.
# data. We're converting from Kelvin to
# Celsius here.

overlay_weights = county_weights, # Output from Step 2, determined here by
# area-normalized cropland weights for grid
Expand Down Expand Up @@ -272,10 +266,8 @@ The final transformation offered is degree days, which measures the degrees over
staggregate_degree_days(
data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
# typically but not necessarily climate
# data. For now, data must start at
# midnight and be hourly. We're
# converting from Kelvin to Celsius
# here.
# data. We're converting from Kelvin to
# Celsius here.

overlay_weights = county_weights, # Output from Step 2, determined here by
# area-normalized cropland weights for grid
Expand Down
Loading
Loading