afsc-gap-products / data-requests

An empty repo for tracking requests for GAP data
0 stars 0 forks source link

Pollock CPUE and isotherm map #81

Closed sean-rohan-NOAA closed 6 months ago

sean-rohan-NOAA commented 6 months ago

Data product requested: CPUE and isotherm (0 and 2 degrees) map for pollock in the EBS and NBS during all years the EBS survey was conducted. Example from 2019 shown below.

Output type: Plots

Species: walleye pollock

Region : EBS/NBS

Research team making the request: Ebett Siddon

Screen Shot 2023-12-31 at 8 24 20 AM
sean-rohan-NOAA commented 6 months ago

Filled using the code below. Maps at the bottom.

# Install GAP packages ----
# remotes::install_github(repo = "afsc-gap-products/gapindex")
# remotes::install_github(repo = "afsc-gap-products/akgfmaps")
# remotes::install_github(repo = "afsc-gap-products/coldpool")

# Load GAP packages
library(gapindex)
library(akgfmaps)
library(coldpool)

# Prepare CPUE in kg/ha ----
channel <- gapindex::get_connected()

ebs_dat <- gapindex::get_data(year_set = c(2010, 2017:2019, 2021:2023),
                              survey_set = "EBS",
                              spp_codes = 21740,
                              haul_type = 3,
                              abundance_haul = "Y",
                              sql_channel = channel
                              )

ebs_2018 <- gapindex::get_data(year_set = 2018,
                               survey_set = "EBS",
                               spp_codes = 21740,
                               haul_type = 13,
                               abundance_haul = "N",
                               sql_channel = channel
)

nbs_dat <- gapindex::get_data(year_set = c(2010, 2017:2019, 2021:2023),
                              survey_set = "NBS",
                              spp_codes = 21740,
                              haul_type = 3,
                              abundance_haul = "Y",
                              sql_channel = channel
                              )

ebs_cpue <- gapindex::calc_cpue(racebase_tables = ebs_dat)

ebs_cpue_2018 <- gapindex::calc_cpue(racebase_tables = ebs_2018)

nbs_cpue <- gapindex::calc_cpue(racebase_tables = nbs_dat)

all_cpue <- rbind(ebs_cpue, nbs_cpue, ebs_cpue_2018)

all_cpue$CPUE_KGHA <- all_cpue$CPUE_KGKM2/100

all_cpue$LONGITUDE <- (all_cpue$LONGITUDE_DD_START + all_cpue$LONGITUDE_DD_END)/2
all_cpue$LATITUDE <- (all_cpue$LATITUDE_DD_START + all_cpue$LATITUDE_DD_END)/2
all_cpue$COMMON_NAME <- "walleye pollock"

# Make CPUE sf layers ----
cpue_stack <- akgfmaps::make_idw_stack(x = all_cpue, 
                                       region = "ebs", 
                                       set.breaks = c(0, 50, 100, 200, 300, Inf),
                                       grouping.vars = "YEAR",
                                       extrapolation.grid.type = "sf"
)

# Setup temperature layers ----
year_vec <- names(terra::rast(coldpool:::nbs_ebs_bottom_temperature))

for(ii in year_vec) {

  if(ii == 2020) {next}

  sel_layer_df <- as.data.frame(terra::rast(coldpool::nbs_ebs_bottom_temperature)[[as.character(ii)]], 
                                na.rm = FALSE, 
                                xy = TRUE) |>
    tidyr::pivot_longer(cols = 3) |>
    sf::st_as_sf(coords = c("x", "y"),
                 crs = coldpool:::ebs_proj_crs) |>
    dplyr::rename(YEAR = name,
                  temperature = value)  |>
    stars::st_rasterize()

  sel_layer_df$temperature <- cut(sel_layer_df$temperature, 
                                  breaks = c(-2, 0, 2))

  sel_layer_df <- sel_layer_df |>
    sf::st_as_sf() |>
    dplyr::group_by(temperature) |>
    dplyr::summarise(n = n()) |>
    dplyr::select(-n) |>
    dplyr::ungroup() |>
    sf::st_intersection(cpue_stack$map_layers$survey.area)

  sel_layer_df$YEAR <- as.numeric(ii)

  if(ii == year_vec[1]) {
    bt_year_df <- sel_layer_df
  } else{
    bt_year_df <- dplyr::bind_rows(bt_year_df, sel_layer_df)
  }
}

lt_2 <- bt_year_df |>
  dplyr::group_by(YEAR) |>
  summarise(do_union = TRUE)

lt_0 <- bt_year_df |>
  dplyr::filter(temperature == "(-2,0]") |>
  dplyr::group_by(YEAR) |>
  summarise(do_union = TRUE)

# Mask for 2018
mask_2018 <- akgfmaps::get_base_layers(select.region = "nbs", set.crs = "EPSG:3338")$survey.grid |>
  dplyr::filter(
    STATIONID %in% c("V-01", "U-01", "T-01", "S-01", "R-01",
                     "V-02", "U-02", "T-02", "S-02", "R-02",
                     "DD-03", "ZZ-03", "Y-03", "X-03", "W-03", "V-03", "U-03", "T-03", "S-03", "R-03",
                     "DD-04", "CC-04", "BB-04", "AA-04", "ZZ-04", "Y-04", "X-04", "W-04", "V-04", "U-04", "T-04", "S-04", "R-04",
                     "DD-05", "CC-05", "BB-05", "AA-05", "ZZ-05", "Y-05", "X-05", "W-05", "V-05", "U-05", "T-05", "S-05", "R-05",
                     "DD-06", "CC-06", "BB-06", "AA-06", "ZZ-06", "Y-06", "X-06", "W-06", "V-06", "U-06", "T-06", "S-06", "R-06",
                     "DD-07", "CC-07", "BB-07", "AA-07", "ZZ-07", "Y-07", "X-07", "W-07", "V-07", "U-07", "T-07", "S-07", "R-07",
                     "DD-08", "CC-08", "BB-08", "AA-08", "ZZ-08", "Y-08", "X-08", "W-08", "V-08", "U-08", "T-08", "S-08", "R-08",
                     "DD-09", "CC-09", "BB-09", "AA-09", "ZZ-09", "Y-09", "X-09", "W-09", "V-09", "U-09", "T-09", "S-09", "R-09",
                     "DD-10", "CC-10", "BB-10", "AA-10", "ZZ-10", "Y-10", "X-10", "W-10", "V-10", "U-10", "T-10", "S-10", "R-10")
    ) |>
  dplyr::group_by() |>
  summarise(do_union = TRUE) |>
  sf::st_make_valid() |>
  dplyr::mutate(YEAR = 2018)

# Make maps
plot_cpue <- ggplot() +
  geom_sf(data = cpue_stack$extrapolation.stack,
          mapping = aes(fill = var1.pred), color = NA) +
  geom_sf(data = mask_2018, fill = "white", color = NA) +
  geom_sf(data = cpue_stack$map_layers$survey.strata, fill = NA, color = "black") +
  geom_sf(data = cpue_stack$map_layers$akland, color = NA, fill = "grey50") +
  scale_fill_manual(name = expression(CPUE~(kg/ha)), 
                    values = c("gray90",
                                                                viridis::viridis(
                                                                  option = "mako",
                                                                  direction = -1,
                                                                  n = 5,
                                                                  begin = 0.20,
                                                                  end = 0.80)),
                    labels = c("No catch", ">0–50", ">50–100", ">100–200", ">200–300", ">300")) +
  coord_sf(xlim = cpue_stack$map_layers$plot.boundary$x,
           ylim = cpue_stack$map_layers$plot.boundary$y) +
  facet_wrap(~YEAR, ncol = 4) +
  theme_bw() +
  theme(axis.title = element_text(color = "black", face = "bold"),
        axis.text = element_text(color = "black", size = 5),
        axis.ticks = element_line(color = "black"),
        legend.position = "right",
        strip.text = element_text(size = 9,
                                  color = "white",
                                  face = "bold",
                                  margin = margin(0.5, 0, 0.5, 0, "mm")),
        strip.background = element_rect(fill = "#0055a4",
                                        color = NA))

plot_cpue_lt0 <- plot_cpue +
  geom_sf(data = lt_0, fill = "pink3", color = NA, alpha = 0.6) +
  geom_sf(data = lt_0, fill = NA, color = "pink3") +
  coord_sf(xlim = cpue_stack$map_layers$plot.boundary$x,
           ylim = cpue_stack$map_layers$plot.boundary$y) +
    scale_x_continuous(breaks = cpue_stack$map_layers$lon.breaks) +
    scale_y_continuous(breaks = cpue_stack$map_layers$lat.breaks)

plot_cpue_lt2 <- plot_cpue +
  geom_sf(data = lt_2, fill = "pink3", color = NA, alpha = 0.6) +
  geom_sf(data = lt_2, fill = NA, color = "pink3") +
  coord_sf(xlim = cpue_stack$map_layers$plot.boundary$x,
           ylim = cpue_stack$map_layers$plot.boundary$y) +
  scale_x_continuous(breaks = cpue_stack$map_layers$lon.breaks) +
  scale_y_continuous(breaks = cpue_stack$map_layers$lat.breaks)

png(filename = "pollock_cpue_lt0_wide.png", width = 8, height = 4, units = "in", res = 300)
print(plot_cpue_lt0)
dev.off()

png(filename = "pollock_cpue_lt2_wide.png", width = 8, height = 4, units = "in", res = 300)
print(plot_cpue_lt2)
dev.off()

png(filename = "pollock_cpue_lt0_tall.png", width = 7, height = 7.5, units = "in", res = 300)
print(plot_cpue_lt0 +
        facet_wrap(~YEAR, ncol = 3) +
        theme(legend.position = c(0.66, 0.15),
              legend.direction = "horizontal",
              axis.text = element_text(color = "black", size = 7)) +
        guides(fill = guide_legend(title.position = "top", title.hjust = 0.5))
)
dev.off()

png(filename = "pollock_cpue_lt2_tall.png", width = 7, height = 7.5, units = "in", res = 300)
print(plot_cpue_lt2 +
        facet_wrap(~YEAR, ncol = 3) +
        theme(legend.position = c(0.66, 0.15),
              legend.direction = "horizontal",
              axis.text = element_text(color = "black", size = 7)) +
        guides(fill = guide_legend(title.position = "top", title.hjust = 0.5))
)
dev.off()

pollock_cpue_lt0_tall pollock_cpue_lt0_wide pollock_cpue_lt2_tall pollock_cpue_lt2_wide