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

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.

# 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)
}

# Temporarily add unfinalized NBS data -- COMMENT OUT WHEN 2023 NBS DATA ARE FINALIZED

# nbs_layers <- get_base_layers(select.region = "nbs", set.crs = "EPSG:4269")
# 
# nbs_centroid <- nbs_layers$survey.grid |>
# sf::st_make_valid() |> 
# sf::st_centroid()
# 
# nbs_centroid <- cbind(nbs_centroid,
#                       sf::st_coordinates(nbs_centroid)) |>
#   as.data.frame() |>
#   dplyr::select(-geometry) |>
#   dplyr::rename(longitude = X,
#                 latitude = Y,
#                 stationid = STATIONID)
# 
# read.csv(file = here::here("data", "unfinalized_nbs_2023.csv")) |>
#   dplyr::inner_join(nbs_centroid) |>
#   dplyr::bind_rows(
#   read.csv(file = nbs_ebs_csv_path)
#   ) |>
#   write.csv(file = nbs_ebs_csv_path)

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.

# 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:2023,
                      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:2023,
                      interp_variable = "surface_temperature",
                      select_region = "sebs",
                      methods = "Ste") 
# 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.

# 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()
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)

      # 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()

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.

# 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:

devtools::document(roclets = c('rd', 'collate', 'namespace'))

After updating, reinstall the coldpool package:

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.

print(coldpool:::cold_pool_index)


afsc-gap-products/coldpool documentation built on Feb. 25, 2024, 9:44 p.m.