-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon-prep.Rmd
189 lines (151 loc) · 9.54 KB
/
polygon-prep.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
---
title: "Study area and regions"
author: "Robert Schlegel"
date: "2019-08-22"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
csl: FMars.csl
bibliography: MHWNWA.bib
---
```{r global_options, include = FALSE}
knitr::opts_chunk$set(fig.width = 8, fig.align = 'center',
echo = TRUE, warning = FALSE, message = FALSE,
eval = TRUE, tidy = FALSE)
```
## Introduction
This markdown file contains all of the code that prepares the polygons used to define the different regions in the Northwest Atlantic. These different regions then have the NOAA OISST pixels found therein spatially averaged to create a single time series per region in the [SST preparation](https://robwschlegel.github.io/MHWNWA/sst-prep.html) vignette. This is done so that the MHW detection algorithm may then be run on these individual time series as a general representation of the SST in those regions, rather than running the algorithm on each pixel individually, which would introduce a host of technical and philosophical problems that I won't go into here.
```{r libraries}
# Packages used in this vignette\
library(tidyverse) # Base suite of functions
library(R.matlab) # For dealing with MATLAB files
# library(marmap) # For bathymetry, but not currently used
```
## Coastal region polygons
The first step in this analysis is to broadly define the coastal regions based on previous research into thermally relevant boundaries. We have chosen to use a paper by @Richaud2016 to do this (https://www.sciencedirect.com/science/article/pii/S0278434316303181#f0010). Being the wonderful person that he is, Benjamin forwarded us the polygons [@Richaud2016; Figure 2] from this paper. The only hiccup being that they are a MATLAB file so we must first convert them to an R format. It should be noted that these areas were designed to not encompass depths deeper than 600 m as the investigators were interested in characterising the climatologies for the shelf and upper slope regions of the north east coast of North America. This works for our research purposes as well.
```{r mat-R}
# Load the file
NWA_polygons <- readMat("data/boundaries.mat")
# Remove index list items and attributes
NWA_polygons[grepl("[.]",names(NWA_polygons))] <- NULL
# attributes(NWA_polygons) <- NULL
# Function for neatly converting list items into a dataframe
mat_col <- function(vec){
df <- as.data.frame(vec)
df$region <- substr(colnames(df)[1], 2, nchar(colnames(df)[1]))
colnames(df)[1] <- strtrim(colnames(df)[1], 1)
df <- df[c(2,1)]
return(df)
}
# Create multiple smaller data.frames
coords_1 <- cbind(mat_col(NWA_polygons[1]), mat_col(NWA_polygons[2])[2])
coords_2 <- cbind(mat_col(NWA_polygons[3]), mat_col(NWA_polygons[4])[2])
coords_3 <- cbind(mat_col(NWA_polygons[5]), mat_col(NWA_polygons[6])[2])
coords_4 <- cbind(mat_col(NWA_polygons[7]), mat_col(NWA_polygons[8])[2])
coords_5 <- cbind(mat_col(NWA_polygons[9]), mat_col(NWA_polygons[10])[2])
coords_6 <- cbind(mat_col(NWA_polygons[11]), mat_col(NWA_polygons[12])[2])
# Combine them into one full dataframe and save
NWA_coords_base <- rbind(coords_1, coords_2, coords_3, coords_4, coords_5, coords_6)
colnames(NWA_coords_base) <- c("region", "lon", "lat")
```
With our polygons switched over from MATLAB to R we now want to visualise them to ensure that everything has gone smoothly.
```{r poly-vis}
# The base map
map_base <- ggplot2::fortify(maps::map(fill = TRUE, col = "grey80", plot = FALSE)) %>%
dplyr::rename(lon = long) %>%
mutate(group = ifelse(lon > 180, group+9999, group),
lon = ifelse(lon > 180, lon-360, lon)) %>%
dplyr::select(-region, -subregion)
# saveRDS(map_base, "data/map_base.Rda")
# Quick map
ggplot(data = NWA_coords_base, aes(x = lon, y = lat)) +
geom_polygon(aes(colour = region, fill = region), size = 1.5, alpha = 0.2) +
geom_polygon(data = map_base, aes(group = group), show.legend = F) +
coord_cartesian(xlim = c(min(NWA_coords_base$lon)-2, max(NWA_coords_base$lon)+2),
ylim = c(min(NWA_coords_base$lat)-2, max(NWA_coords_base$lat)+2)) +
labs(x = NULL, y = NULL, colour = "Region", fill = "Region") +
theme(legend.position = "bottom")
```
The region abbreviations are: "gm" for Gulf of Maine, "gls" for Gulf of St. Lawrence, "ls" for Labrador Shelf, "mab" for Mid-Atlantic Bight, "nfs" for Newfoundland Shelf and "ss" for Scotian Shelf.
### Cabot Strait
It was decided that because we are interested in the geography of the regions, and not just their temperature regimes, the Cabot Strait needed to be defined apart from the Gulf of St. Lawrence region. To do this we will simply snip the "gsl" polygon into two pieces at its narrowest point.
```{r cabot-strait-1}
# Extract the gsl region only
gsl_sub <- NWA_coords_base[NWA_coords_base$region == "gsl",]
# Add a simple integer column for ease of plotting
gsl_sub$row_count <- 1:nrow(gsl_sub)
ggplot(data = gsl_sub, aes(x = lon, y = lat)) +
geom_polygon(aes(fill = region)) +
geom_label(aes(label = row_count)) +
labs(x = NULL, y = NULL)
```
It appears from the crude figure above that we should pinch the polygon off into two separate shapes at row 6 and 10.
```{r cabot-strait-2}
# Create smaller gsl polygon
gsl_new <- NWA_coords_base[NWA_coords_base$region == "gsl",] %>%
slice(-c(7:9))
# Create new cbs (Cabot Strait) polygon
cbs <- NWA_coords_base[NWA_coords_base$region == "gsl",] %>%
slice(6:10) %>%
mutate(region = "cbs")
# Attach the new polygons to the original polygons
NWA_coords_cabot <- NWA_coords_base %>%
filter(region != "gsl") %>%
rbind(., gsl_new, cbs)
# saveRDS(NWA_coords_cabot, "data/NWA_coords_cabot.Rda")
# Plot the new areas to ensure everything worked
ggplot(data = NWA_coords_cabot, aes(x = lon, y = lat)) +
geom_polygon(aes(colour = region, fill = region), size = 1.5, alpha = 0.2) +
geom_polygon(data = map_base, aes(group = group), show.legend = F) +
coord_cartesian(xlim = c(min(NWA_coords_cabot$lon)-2, max(NWA_coords_cabot$lon)+2),
ylim = c(min(NWA_coords_cabot$lat)-2, max(NWA_coords_cabot$lat)+2)) +
labs(x = NULL, y = NULL, colour = "Region", fill = "Region") +
theme(legend.position = "bottom")
```
### Labrador Shelf
After running through a series of slef-roganising map (SOM) experiments in the previous iteration of this project it was decided that the Labrador Shelf (ls) region needs to be excluded from the study. This is predominantly because the inclusion of this region into the SOM study makes it too difficult for the machine to make sense of the patterns it is seeing. We concluded that this was because of the strong, sometimes unrelated processes happening in the Gulf Stream vs. the Labrador Sea. Because we are primarily concerned with the Atlantic coast, we prioritised the more southern regions over the Labrador shelf region. The code below shows what these final regions look like.
```{r no-ls}
# FIlter out the ls region
NWA_coords <- NWA_coords_cabot %>%
filter(region != "ls")
# Save these final study region coordinates
# saveRDS(NWA_coords, "data/NWA_coords.Rda")
# Plot the new areas to ensure everything worked
NWA_study_area <- ggplot(data = NWA_coords, aes(x = lon, y = lat)) +
geom_polygon(aes(colour = region, fill = region), size = 1.5, alpha = 0.2) +
geom_polygon(data = map_base, aes(group = group), show.legend = F) +
coord_cartesian(xlim = c(min(NWA_coords$lon)-2, max(NWA_coords$lon)+2),
ylim = c(min(NWA_coords$lat)-2, max(NWA_coords$lat)+0.5),
expand = FALSE) +
scale_x_continuous(breaks = seq(-70, -50, 10),
labels = c("70°W", "60°W", "50°W"),
position = "top") +
scale_y_continuous(breaks = c(40, 50),
labels = scales::unit_format(suffix = "°N", sep = "")) +
labs(x = NULL, y = NULL, colour = "Region", fill = "Region") +
theme_bw() +
theme(legend.position = c(0.6, 0.2),
legend.background = element_rect(colour = "black"),
legend.direction = "horizontal")
# ggsave(NWA_study_area, filename = "output/NWA_study_area.pdf", height = 5, width = 6)
# ggsave(NWA_study_area, filename = "output/NWA_study_area.png", height = 5, width = 6)
# ggsave(NWA_study_area, filename = "talk/graph/NWA_study_area.png", height = 5, width = 6)
# Visualise
NWA_study_area
```
## Study area extent
The final step in this vignette is to establish a consistent study area for this project based on our regions. We'll simply extend the study area by the nearest 2 whole degrees of longitude and latitude from the furthest edges of the polygons, as seen in the figure above. This will encompass broad synoptic scale variables that may be driving MHWs in our study regions, but should not be so broad as to begin to account for teleconnections, which are currently beyond the scope of this project. The exception to this being that because we want to exclude as much of the Labrador Sea as possible, we will only extend the northern edge of the study area by 0.5 degrees of latitude from the northernmost point of our study regions.
```{r study-area-coords}
# Set the max/min lon/at values
lon_min <- round(min(NWA_coords$lon)-2)
lon_max <- round(max(NWA_coords$lon)+2)
lat_min <- round(min(NWA_coords$lat)-2)
lat_max <- round(max(NWA_coords$lat)+0.5)
# Combine and save
NWA_corners <- c(lon_min, lon_max, lat_min, lat_max)
# saveRDS(NWA_corners, file = "data/NWA_corners.Rda")
# Pront the values
NWA_corners
```
We will now go about creating SST time series for each of the regions and calculate marine heatwaves (MHWs) from them. This work is continued in the [SST preparation and MHW detection](https://robwschlegel.github.io/MHWNWA/sst-prep.html) vignette.
## References