-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0_update_cold_pool_index.Rmd
319 lines (245 loc) · 13.7 KB
/
0_update_cold_pool_index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
---
title: "Update the Cold Pool Index and Average Temperature"
author:
- affiliation: RACE
description: Research Fish Biologist
email: [email protected]
name: Sean Rohan
output: word_document
fontsize: 12pt
addr:
l1: 7600 Sand Point Way NE
l2: NMFS RACE Division, Groundfish Assessment Program
l3: Seattle, WA 98115
---
# Introduction
This document provides code for updating the annual summer Cold Pool Index and average summer bottom temperature for the eastern Bering Sea continental shelf. The data used are collected during AFSC/RACE/GAP bottom trawl surveys of the eastern Bering Sea continental shelf and the code herein is used to interpolate bottom temperature (i.e., gear temperature in RACEBASE), calculate cold pool area and average bottom temperature, produce GeoTIFF rasters of temperature with a 5x5 km resolution, and update data sets that are included with the coldpool package.
# 1. Setup project options
```{r setup, include=TRUE, message=FALSE, warning=FALSE, echo=FALSE}
library(coldpool)
library(akgfmaps)
# Set global options ----
fig_res <- 600
proj_crs <- coldpool:::ebs_proj_crs
# Should data included in the package be updated with new data (i.e. for annual update)? ----
update_sysdata <- TRUE
# Filepath to csv containing data to use for temperature interpolation ----
ebs_csv_path <- here::here("data", paste0("index_hauls_temperature_data.csv"))
nbs_ebs_csv_path <- here::here("data", paste0("ebs_nbs_temperature_full_area.csv"))
nbs_bt_years <- c(2010, 2017, 2018, 2019, 2021, 2022, 2023)
nbs_sst_years <- c(2010, 2017, 2018, 2019, 2021, 2022, 2023)
```
# 2. Retrieve temperature data
Retrieve temperature data and write to:
- /data/[date]_all_temperature_data.csv: Temperature data from all hauls.
- /data/[date]_index_hauls_temperature_data.csv: Temperature data for haul type 3 with good performance. This is the data set that is used for cold pool index calculations.
```{r query_data, include=TRUE, message=FALSE, warning=FALSE}
# Update system data with new cold pool area estimates
if(update_sysdata) {
library(getPass)
# Connect RODBC ----
channel <- get_connected()
# Get temperature data and write csvs to data directory ----
coldpool:::get_data(channel = channel, include_preliminary_data = NULL)
}
```
# 3. Interpolate bottom and surface temperatures using candidate interpolation methods ----
Conduct interpolation using candidate interpolation methods.
Writes:
--GeoTIFF rasters of interpolated gear temperature are saved to output/raster/[variable] for each method and year.
```{r interpolate_sebs, include=TRUE, message=FALSE, warning=FALSE, echo = FALSE}
# Interpolate gear temperature and write rasters for SEBS
interpolation_wrapper(temp_data_path = ebs_csv_path,
proj_crs = proj_crs,
cell_resolution = 5000, # 5x5 km grid resolution
select_years = 1982:2024,
interp_variable = "gear_temperature",
select_region = "sebs",
methods = "Ste")
# Interpolate surface temperature and write rasters for SEBS
interpolation_wrapper(temp_data_path = ebs_csv_path,
proj_crs = proj_crs,
cell_resolution = 5000, # 5x5 km grid resolution
select_years = 1982:2024,
interp_variable = "surface_temperature",
select_region = "sebs",
methods = "Ste")
```
```{r interpolate_full_sebs, include=TRUE, message=FALSE, warning=FALSE, echo = FALSE}
# Interpolate gear temperature and write rasters for full EBS
interpolation_wrapper(temp_data_path = nbs_ebs_csv_path,
proj_crs = proj_crs,
cell_resolution = 5000, # 5x5 km grid resolution
select_years = nbs_bt_years,
interp_variable = "gear_temperature",
select_region = "ebs",
methods = "Ste") # Full EBS+NBS
# Interpolate surface temperature and write rasters for full EBS
interpolation_wrapper(temp_data_path = nbs_ebs_csv_path,
proj_crs = proj_crs,
cell_resolution = 5000, # 5x5 km grid resolution
select_years = nbs_sst_years,
interp_variable = "surface_temperature",
select_region = "ebs",
methods = "Ste") # Full EBS+NBS
```
# 4. Calculate cold pool area, mean bottom temperature, surface temperature
Use surface and bottom temperature rasters to calculate cold pool area, mean bottom temperature, and mean surface temperature.
Writes:
- /output/raster/[METHOD]_[YEAR]_gear_temperature.tif: GeoTIFF rasters of gear temperature, by method and year, with 5x5 km grid resolution.
```{r ebs_mean_temp}
# Calculate cold pool area and mean bottom temperature from SEBS rasters
bottom_temp_files <- list.files(here::here("output", "raster", "sebs", "gear_temperature"),
full.names = TRUE,
pattern = "ste_")
bt_df <- data.frame(YEAR = numeric(length = length(bottom_temp_files)),
AREA_LTE2_KM2 = numeric(length = length(bottom_temp_files)),
AREA_LTE1_KM2 = numeric(length = length(bottom_temp_files)),
AREA_LTE0_KM2 = numeric(length = length(bottom_temp_files)),
AREA_LTEMINUS1_KM2 = numeric(length = length(bottom_temp_files)),
MEAN_GEAR_TEMPERATURE = numeric(length = length(bottom_temp_files)),
MEAN_BT_LT100M = numeric(length = length(bottom_temp_files)))
# Setup mask to calculate mean bottom temperature from <100 m strata
ebs_layers <- akgfmaps::get_base_layers(select.region = "sebs", set.crs = proj_crs)
lt100_strata <- ebs_layers$survey.strata |>
dplyr::filter(Stratum %in% c(10, 20, 31, 32, 41, 42, 43)) |>
dplyr::group_by(SURVEY) |>
dplyr::summarise()
for(i in 1:length(bottom_temp_files)) {
bt_raster <- terra::rast(bottom_temp_files[i])
bt_df$YEAR[i] <- as.numeric(gsub("[^0-9.-]", "", names(bt_raster))) # Extract year
bt_df$AREA_LTE2_KM2[i] <- bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 2)
bt_df$AREA_LTE1_KM2[i] <- bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 1)
bt_df$AREA_LTE0_KM2[i] <- bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 0)
bt_df$AREA_LTEMINUS1_KM2[i] <- bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = -1)
bt_df$MEAN_GEAR_TEMPERATURE[i] <- mean(terra::values(bt_raster), na.rm = TRUE)
lt100_temp <- terra::mask(bt_raster,
lt100_strata,
touches = FALSE)
bt_df$MEAN_BT_LT100M[i] <- mean(terra::values(lt100_temp), na.rm = TRUE)
}
# Calculate mean surface temperature
surface_temp_files <- list.files(here::here("output", "raster", "sebs", "surface_temperature"),
full.names = TRUE,
pattern = "ste_")
sst_df <- data.frame(YEAR = numeric(length = length(bottom_temp_files)),
MEAN_SURFACE_TEMPERATURE = numeric(length = length(surface_temp_files)))
for(i in 1:length(surface_temp_files)) {
sst_raster <- terra::rast(surface_temp_files[i])
sst_df$YEAR[i] <- as.numeric(gsub("[^0-9.-]", "", names(sst_raster))) # Extract year
sst_df$MEAN_SURFACE_TEMPERATURE[i] <- mean(terra::values(sst_raster), na.rm = TRUE)
}
output_df <- dplyr::inner_join(bt_df, sst_df)
output_df$LAST_UPDATE <- Sys.Date()
```
```{r nbs_mean_temp}
nbs_area <- akgfmaps::get_base_layers(select.region = "ebs",
set.crs = coldpool:::ebs_proj_crs)$survey.area |>
dplyr::filter(SURVEY == "NBS_SHELF")
nbs_ebs_bt_files <- list.files(here::here("output", "raster", "ebs", "gear_temperature"),
full.names = TRUE,
pattern = "ste_")
nbs_ebs_sst_files <- list.files(here::here("output", "raster", "ebs", "surface_temperature"),
full.names = TRUE,
pattern = "ste_")
nbs_mean_temperature <- data.frame(YEAR = numeric(length = length(nbs_ebs_bt_files)),
MEAN_GEAR_TEMPERATURE = numeric(length = length(nbs_ebs_bt_files)),
MEAN_SURFACE_TEMPERATURE = numeric(length = length(nbs_ebs_bt_files)))
for(i in 1:length(nbs_ebs_bt_files)) {
nbs_bt_raster <- terra::rast(nbs_ebs_bt_files[i]) |>
terra::mask(nbs_area, touches = FALSE)
nbs_mean_temperature$YEAR[i] <- as.numeric(gsub("[^0-9.-]", "", names(nbs_bt_raster))) # Extract year
nbs_mean_temperature$MEAN_GEAR_TEMPERATURE[i] <- mean(terra::values(nbs_bt_raster), na.rm = TRUE)
nbs_mean_temperature$AREA_LTE2_KM2[i] <- nbs_bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 2)
nbs_mean_temperature$AREA_LTE1_KM2[i] <- nbs_bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 1)
nbs_mean_temperature$AREA_LTE0_KM2[i] <- nbs_bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = 0)
nbs_mean_temperature$AREA_LTEMINUS1_KM2[i] <- nbs_bt_raster |>
cpa_from_raster(raster_units = "m", temperature_threshold = -1)
# Don't calculate SST if NBS data haven't been finalized
if(file.exists(nbs_ebs_sst_files[i])) {
nbs_sst_raster <- terra::rast(nbs_ebs_sst_files[i]) |>
terra::mask(nbs_area, touches = FALSE)
nbs_mean_temperature$MEAN_SURFACE_TEMPERATURE[i] <- mean(terra::values(nbs_sst_raster), na.rm = TRUE)
} else {
nbs_mean_temperature$MEAN_SURFACE_TEMPERATURE[i] <- NA
}
}
nbs_mean_temperature <- nbs_mean_temperature |>
dplyr::filter(YEAR != 2018)
nbs_mean_temperature$LAST_UPDATE <- Sys.Date()
```
```{r cpa_change}
cpa_previous_year <- tail(coldpool:::cold_pool_index$AREA_LTE2_KM2,2)[1]
cpa_this_year <- tail(coldpool:::cold_pool_index$AREA_LTE2_KM2,1)
(cpa_previous_year - cpa_this_year) /cpa_previous_year
lte1_previous_year <- tail(coldpool:::cold_pool_index$AREA_LTE1_KM2,2)[1]
lte1_this_year <- tail(coldpool:::cold_pool_index$AREA_LTE1_KM2,1)
(lte1_previous_year - lte1_this_year) / lte1_previous_year
lte0_previous_year <- tail(coldpool:::cold_pool_index$AREA_LTE0_KM2,2)[1]
lte0_this_year <- tail(coldpool:::cold_pool_index$AREA_LTE0_KM2,1)
(lte0_previous_year - lte0_this_year) / lte0_previous_year
lteminus1_previous_year <- tail(coldpool:::cold_pool_index$AREA_LTEMINUS1_KM2,2)[1]
lteminus1_this_year <- tail(coldpool:::cold_pool_index$AREA_LTEMINUS1_KM2,1)
(lteminus1_previous_year - lteminus1_this_year) / lteminus1_previous_year
```
# 5. Add data to R/sysdata.rda ----
Update sysdata.rda with cold pool area, mean bottom temperature, mean surface temperature, and and 5-km resolution rasters of surface and bottom temperature.
```{r cpa_to_sysdata}
# Write cold pool index and average gear temperature table to R/sysdata.R ----
# Need to update documentation and build/install after running this code.
if(update_sysdata) {
# Make surface temperature raster stack (multiplying by 1 resets the source)
ebs_surface_temperature <- coldpool::make_raster_stack(file_path = here::here("output", "raster", "sebs", "surface_temperature"),
file_name_contains = "ste_",
file_type = ".tif",
wrap = TRUE)
# Make bottom temperature raster stack (multiplying by 1 resets the source)
ebs_bottom_temperature <- coldpool::make_raster_stack(file_path = here::here("output", "raster", "sebs", "gear_temperature"),
file_name_contains = "ste_",
file_type = ".tif",
wrap = TRUE)
# Make surface temperature raster stack (multiplying by 1 resets the source)
nbs_ebs_surface_temperature <- coldpool::make_raster_stack(file_path = here::here("output", "raster", "ebs", "surface_temperature"),
file_name_contains = "ste_",
file_type = ".tif",
wrap = TRUE)
# Make bottom temperature raster stack (multiplying by 1 resets the source)
nbs_ebs_bottom_temperature <- coldpool::make_raster_stack(file_path = here::here("output", "raster", "ebs", "gear_temperature"),
file_name_contains = "ste_",
file_type = ".tif",
wrap = TRUE)
cpa_pre2021 <- read.csv(file = here::here("inst", "extdata", "old_method_cpa_temperature_2021.csv"))
ebs_proj_crs <- coldpool:::ebs_proj_crs
cold_pool_index <- output_df
save(cpa_pre2021,
ebs_proj_crs,
cold_pool_index,
nbs_mean_temperature,
ebs_bottom_temperature,
ebs_surface_temperature,
nbs_ebs_bottom_temperature,
nbs_ebs_surface_temperature,
file = here::here("R", "sysdata.rda"))
}
```
# 5. Update documentation, install, and restart
Update version number in DESCRIPTION and update documentation using:
```{r updater, eval=FALSE, include=TRUE}
devtools::document(roclets = c('rd', 'collate', 'namespace'))
```
After updating, reinstall the coldpool package:
```{r install, eval=FALSE, include = TRUE}
Rcmd.exe INSTALL --no-multiarch --with-keep.source cold_pool
```
#6. Check that data appears correctly
Check that the updated cold pool index data is included in the package.
```{r qaqc, eval = TRUE, include = TRUE}
print(coldpool:::cold_pool_index)
```