Skip to content

Commit 8151a8a

Browse files
authored
Merge pull request #51 from tcarleton/50-fix_projection
50 fix projection and 49 update documentation
2 parents 7b2d351 + 5012e7b commit 8151a8a

9 files changed

Lines changed: 280 additions & 216 deletions

R/secondary_weights.R

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
#' @export
3030
secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full"){
3131

32+
## check to make sure climate raster is a spatraster, change if not
33+
if (!inherits(grid, "SpatRaster")) {
34+
clim_raster <- terra::rast(grid)
35+
} else {
36+
37+
clim_raster <- grid
38+
39+
}
40+
41+
3242
## Return error if terra::extent can't inherit from the value supplied
3343
## won't be able to check if secondary raster fully overlaps if
3444
## this input isn't compatible with terra::extent()
@@ -72,14 +82,6 @@ secondary_weights <- function(secondary_raster, grid = era5_grid, extent = "full
7282

7383
}
7484

75-
## check to make sure climate raster is a spatraster, change if not
76-
if (!inherits(grid, "SpatRaster")) {
77-
clim_raster <- terra::rast(grid)
78-
} else {
79-
80-
clim_raster <- grid
81-
82-
}
8385

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

132134
extent <- extent
133135

134-
}
136+
}
137+
135138

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

209212
}
210213

214+
# Reproject if necessary
215+
if(terra::crs(secondary_raster, TRUE) != terra::crs(grid, TRUE)){
216+
217+
message(crayon::yellow("Warning: reprojecting secondary_raster to match grid, which may affect data"))
218+
secondary_raster <- terra::project(secondary_raster, clim_raster)
219+
220+
}
221+
222+
211223

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

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

223232
## check if the cropped secondary raster contains NA values
224233
if(isTRUE(any(is.na(terra::values(secondary_raster, na.rm=FALSE))))) {

R/staggregate.R

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,14 @@ polygon_aggregation <- function(clim_dt, weights_dt, list_names, time_agg, weigh
431431
#' unless `daily_agg = 'none'`)
432432
#' @param start_date the date (and time, if applicable) of the first layer in
433433
#' the stack. To be input in a format compatible with
434-
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
434+
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
435435
#' The default is `NA` since the spatRasters usually already contain temporal
436436
#' information in the layer names and they do not need to be manually supplied.
437437
#' @param time_interval the time interval between layers in the spatRaster to be
438-
#' aggregated. To be input in a format compatible with seq(), e.g.
439-
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
440-
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
441-
#' `NA`.
438+
#' aggregated. To be input in a format compatible with
439+
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
440+
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
441+
#' not `'none'` or if the `start_date` argument is not `NA`.
442442
#' @param weights_join_tolerance the tolerance to use when joining
443443
#' overlay_weights with the climate data by the x and y columns. This is useful
444444
#' when the height/width of your data cells expressed in degrees is a very long
@@ -588,14 +588,14 @@ staggregate_polynomial <- function(data, overlay_weights, daily_agg, time_agg =
588588
#' unless `daily_agg = 'none'`)
589589
#' @param start_date the date (and time, if applicable) of the first layer in
590590
#' the stack. To be input in a format compatible with
591-
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
591+
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
592592
#' The default is `NA` since the spatRasters usually already contain temporal
593593
#' information in the layer names and they do not need to be manually supplied.
594594
#' @param time_interval the time interval between layers in the spatRaster to be
595-
#' aggregated. To be input in a format compatible with seq(), e.g.
596-
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
597-
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
598-
#' `NA`.
595+
#' aggregated. To be input in a format compatible with
596+
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
597+
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
598+
#' not `'none'` or if the `start_date` argument is not `NA`.
599599
#' @param weights_join_tolerance the tolerance to use when joining
600600
#' overlay_weights with the climate data by the x and y columns. This is useful
601601
#' when the height/width of your data cells expressed in degrees is a very long
@@ -773,14 +773,14 @@ staggregate_spline <- function(data, overlay_weights, daily_agg, time_agg = "mon
773773
#' unless `daily_agg = 'none'`)
774774
#' @param start_date the date (and time, if applicable) of the first layer in
775775
#' the raster. To be input in a format compatible with
776-
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
776+
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
777777
#' The default is `NA` since the rasters usually already contain temporal
778778
#' information in the layer names and they do not need to be manually supplied.
779-
#' @param time_interval the time interval between layers in the raster to be
780-
#' aggregated. To be input in a format compatible with seq(), e.g.
781-
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
782-
#' required if daily_agg is not `'none'` or if the `start_date` argument is not
783-
#' `NA`.
779+
#' @param time_interval the time interval between layers in the spatRaster to be
780+
#' aggregated. To be input in a format compatible with
781+
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
782+
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
783+
#' not `'none'` or if the `start_date` argument is not `NA`.
784784
#' @param weights_join_tolerance the tolerance to use when joining
785785
#' overlay_weights with the climate data by the x and y columns. This is useful
786786
#' when the height/width of your data cells expressed in degrees is a very long
@@ -958,13 +958,14 @@ staggregate_bin <- function(data, overlay_weights, daily_agg, time_agg = "month"
958958
#' `'month'`, or `'year'`
959959
#' @param start_date the date (and time, if applicable) of the first layer in
960960
#' the raster. To be input in a format compatible with
961-
#' lubridate::as_datetime(), e.g. `"1991-10-29"` or `"1991-10-29 00:00:00"`.
961+
#' lubridate::as_datetime(), e.g. `'1991-10-29'` or `'1991-10-29 00:00:00'`.
962962
#' The default is `NA` since the rasters usually already contain temporal
963963
#' information in the layer names and they do not need to be manually supplied.
964-
#' @param time_interval the time interval between layers in the raster to be
965-
#' aggregated. To be input in a format compatible with seq(), e.g.
966-
#' `'1 day'` or `'3 months'`. The default is `'1 hour'` and this argument is
967-
#' required if the `start_date` argument is not `NA`.
964+
#' @param time_interval the time interval between layers in the spatRaster to be
965+
#' aggregated. To be input in a format compatible with
966+
#' `lubridate::as.duration()` such as `'30 minutes'`, `'2 hours'`, `'1 day'`,
967+
#' etc. The default is `'1 hour'` and this argument is required if daily_agg is
968+
#' not `'none'` or if the `start_date` argument is not `NA`.
968969
#' @param weights_join_tolerance the tolerance to use when joining
969970
#' overlay_weights with the climate data by the x and y columns. This is useful
970971
#' when the height/width of your data cells expressed in degrees is a very long

README.Rmd

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ polynomial_output <- staggregate_polynomial(
153153
154154
data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
155155
# typically but not necessarily climate
156-
# data. For now, data must start at
157-
# midnight and be hourly. We're
158-
# converting from Kelvin to Celsius
159-
# here.
156+
# data. We're converting from Kelvin to
157+
# Celsius here.
160158
161159
overlay_weights = county_weights, # Output from Step 2, determined here by
162160
# area-normalized cropland weights for grid
@@ -197,10 +195,8 @@ spline_output <- staggregate_spline(
197195
198196
data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
199197
# typically but not necessarily climate
200-
# data. For now, data must start at
201-
# midnight and be hourly. We're
202-
# converting from Kelvin to Celsius
203-
# here.
198+
# data. We're converting from Kelvin to
199+
# Celsius here.
204200
205201
overlay_weights = county_weights, # Output from Step 2, determined here by
206202
# area-normalized cropland weights for grid
@@ -235,10 +231,8 @@ bin_output <- staggregate_bin(
235231
236232
data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
237233
# typically but not necessarily climate
238-
# data. For now, data must start at
239-
# midnight and be hourly. We're
240-
# converting from Kelvin to Celsius
241-
# here.
234+
# data. We're converting from Kelvin to
235+
# Celsius here.
242236
243237
overlay_weights = county_weights, # Output from Step 2, determined here by
244238
# area-normalized cropland weights for grid
@@ -272,10 +266,8 @@ The final transformation offered is degree days, which measures the degrees over
272266
staggregate_degree_days(
273267
data = temp_nj_jun_2024_era5 - 273.15, # A raster brick of our primary data,
274268
# typically but not necessarily climate
275-
# data. For now, data must start at
276-
# midnight and be hourly. We're
277-
# converting from Kelvin to Celsius
278-
# here.
269+
# data. We're converting from Kelvin to
270+
# Celsius here.
279271
280272
overlay_weights = county_weights, # Output from Step 2, determined here by
281273
# area-normalized cropland weights for grid

0 commit comments

Comments
 (0)