Skip to content

Commit 05ffa37

Browse files
committed
Update process_temperature
1 parent 34e5994 commit 05ffa37

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

Diff for: R/hdcd.R

+34-7
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,42 @@ hdcd <- function(ncdf = NULL,
143143
print('.........................................')
144144
print(paste0('Running hdcd for file: ', ncdf_i))
145145

146-
ncdf_list <- helios::process_temperature(ncdf = ncdf_i,
146+
ncdf_grid <- helios::process_temperature(ncdf = ncdf_i,
147147
model = model,
148148
ncdf_var = ncdf_var,
149149
time_periods = time_periods,
150-
spatial = spatial)
151-
ncdf_pivot <- ncdf_list$ncdf_pivot
152-
years <- ncdf_list$ncdf_years
153-
index_subset <- ncdf_list$index_subset
150+
spatial = spatial,
151+
reference_temp_F = reference_temp_F)
152+
153+
154+
# get the actual datetime from the ncdf
155+
ncdf_times <- names(ncdf_grid)[
156+
!names(ncdf_grid) %in% c('lat', 'lon', 'region', 'subRegion', 'ID')]
157+
158+
indices <- as.integer(grepl(paste0(time_periods, collapse = '|'), ncdf_times))
159+
index_subset <- c(1:length(ncdf_times)) * indices
160+
index_subset <- index_subset[!index_subset %in% 0]
161+
years <- unique(substr(ncdf_times, 1, 4))
162+
163+
if (model == 'wrf') {
164+
165+
ncdf_pivot <- ncdf_grid %>%
166+
tidyr::pivot_longer(cols = dplyr::all_of(ncdf_times), names_to = 'datetime') %>%
167+
dplyr::mutate(datetime = as.POSIXct(datetime,
168+
format = '%Y-%m-%d_%H:%M:%S',
169+
tz = 'UTC')) %>%
170+
dplyr::mutate(year = lubridate::year(datetime))
171+
172+
} else if (model == 'cmip') {
173+
174+
ncdf_pivot <- ncdf_grid %>%
175+
tidyr::pivot_longer(cols = dplyr::all_of(ncdf_times), names_to = 'datetime') %>%
176+
dplyr::mutate(datetime = as.POSIXct(datetime,
177+
format = '%Y-%m-%d',
178+
tz = 'UTC')) %>%
179+
dplyr::mutate(year = lubridate::year(datetime))
180+
181+
}
154182

155183

156184
#......................
@@ -183,8 +211,7 @@ hdcd <- function(ncdf = NULL,
183211
dplyr::left_join(population_j_weighted %>%
184212
dplyr::select(-value, -subRegion_total_value),
185213
by = c('ID', 'region', 'subRegion', 'lat', 'lon', 'year')) %>%
186-
dplyr::mutate(value = (((value - 273.15) * 9/5) + 32) - reference_temp_F,
187-
value = dplyr::if_else(is.na(pop_weight), value, value * pop_weight))
214+
dplyr::mutate(value = dplyr::if_else(is.na(pop_weight), value, value * pop_weight))
188215

189216
# aggregated to region, but leave the datetime for later segment, monthly, annual aggregation
190217
hdcd_region_i <- ncdf_hdcd_pop_weighted %>%

Diff for: R/process_temperature.R

+8-30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#' @param model Default = NULL. String for climate model that generates the ncdf file. Options: 'wrf' or 'cmip'.
88
#' @param spatial Default = NULL. String for spatial aggregation boundaries. Options: check helios::spatial_options. 'gcam_us49', 'gcam_regions32', 'gcam_regions31_us52', 'gcam_countries', 'gcam_basins'.
99
#' @param time_periods Default = NULL. Integer vector for selected time periods to process. If not specified, set to GCAM periods seq(2020, 2100, 5).
10+
#' @param reference_temp_F Default = 65. Integer for comfort temperature in degree F. 65 degree F is the comfort baseline temperature typically used by NOAA. The comfort temperature can vary by regions.
1011
#' @importFrom magrittr %>%
1112
#' @importFrom data.table :=
1213
#' @export
@@ -15,7 +16,8 @@ process_temperature <- function(ncdf = NULL,
1516
ncdf_var = NULL,
1617
model = NULL,
1718
spatial = NULL,
18-
time_periods = NULL){
19+
time_periods = NULL,
20+
reference_temp_F = 65){
1921

2022
# read ncdf file
2123
ncdf_grid <- helios::read_ncdf(ncdf = ncdf,
@@ -27,37 +29,13 @@ process_temperature <- function(ncdf = NULL,
2729
ncdf_grid <- helios::find_mapping_grid(data = ncdf_grid,
2830
spatial = spatial)
2931

30-
# get the actual datetime from the ncdf
31-
ncdf_times <- names(ncdf_grid)[
32-
!names(ncdf_grid) %in% c('lat', 'lon', 'region', 'subRegion', 'ID')]
32+
# calculate heating and cooling degrees
33+
ncdf_grid <- ncdf_grid %>%
34+
dplyr::mutate(across(c(-lat, -lon, -region, -subRegion, -ID),
35+
~ round((((. - 273.15) * 9/5) + 32) - reference_temp_F, 2)))
3336

34-
indices <- as.integer(grepl(paste0(time_periods, collapse = '|'), ncdf_times))
35-
index_subset <- c(1:length(ncdf_times)) * indices
36-
index_subset <- index_subset[!index_subset %in% 0]
37-
years <- unique(substr(ncdf_times, 1, 4))
3837

39-
if (model == 'wrf') {
4038

41-
ncdf_pivot <- ncdf_grid %>%
42-
tidyr::pivot_longer(cols = dplyr::all_of(ncdf_times), names_to = 'datetime') %>%
43-
dplyr::mutate(datetime = as.POSIXct(datetime,
44-
format = '%Y-%m-%d_%H:%M:%S',
45-
tz = 'UTC')) %>%
46-
dplyr::mutate(year = lubridate::year(datetime))
47-
48-
} else if (model == 'cmip') {
49-
50-
ncdf_pivot <- ncdf_grid %>%
51-
tidyr::pivot_longer(cols = dplyr::all_of(ncdf_times), names_to = 'datetime') %>%
52-
dplyr::mutate(datetime = as.POSIXct(datetime,
53-
format = '%Y-%m-%d',
54-
tz = 'UTC')) %>%
55-
dplyr::mutate(year = lubridate::year(datetime))
56-
57-
}
58-
59-
return(list(ncdf_pivot = ncdf_pivot,
60-
ncdf_years = years,
61-
index_subset = index_subset))
39+
return(ncdf_grid)
6240

6341
}

Diff for: inst/extras/dev_tests.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ncdf_grid <- helios::read_ncdf(ncdf = path_to_climate_ncdf,
2828

2929
pop <- helios::read_population(path_to_population, time_periods = 2020)
3030

31-
ncdf_pivot <- helios::process_temperature(ncdf = path_to_climate_ncdf,
31+
ncdf_grid <- helios::process_temperature(ncdf = path_to_climate_ncdf,
3232
model = 'wrf',
3333
ncdf_var = 'T2',
3434
time_periods = 2020,

Diff for: man/process_temperature.Rd

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)