-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path03-El-Nino-Southern-Oscillation.qmd
252 lines (203 loc) · 9.38 KB
/
03-El-Nino-Southern-Oscillation.qmd
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
---
title: "El_Nino_Southern_Oscillation"
author: "[email protected]"
format:
docx:
reference-doc: SAFE-Reference-Doc.docx
---
## EL NIÑO – SOUTHERN OSCILLATION
```{r}
#| include: false
### Load libraries
library(tidyverse)
library(lubridate)
library(here)
library(stringr)
# remotes::install_github("nmfs-fish-tools/nmfspalette")
library(nmfspalette)
library(plotly)
library(reticulate)
reticulate::use_miniconda('r-reticulate')
```
```{r}
#| include: false
# Set report year (RptYr), to make things easier
RptYr <- 2023
# Set path to variable: El_Nino_Southern_Oscillation
# This is where the data are and where the plots will go
Dir <- here("El_Nino_Southern_Oscillation")
```
```{r}
#| include: false
### Load data
ONI_full <- read_csv(file = paste(Dir, '/ONI_', RptYr, '.csv', sep = ""))
```
```{r}
#| include: false
### Round data to the tenth of a degree
ONI_rounded <- round(ONI_full$ANOM, digits = 1)
```
```{r}
#| include: false
### Identifying El Niños and La Niñas, based on the criterion that they persist for at least 5 consecutive seasons
ElNino_idx = which(ONI_rounded >= 0.5);
LaNina_idx = which(ONI_rounded <= -0.5);
Neutral_idx = which(ONI_rounded > -0.5 & ONI_rounded < 0.5);
ElNino <- array(0, dim = c(length(ONI_rounded), 3))
LaNina <- array(0, dim = c(length(ONI_rounded), 3))
Neutral <- array(0, dim = c(length(ONI_rounded), 3))
ElNino[ElNino_idx] = ONI_rounded[ElNino_idx]
LaNina[LaNina_idx] = ONI_rounded[LaNina_idx]
Neutral[Neutral_idx] = ONI_rounded[Neutral_idx]
if (ElNino[1,1] > 0) {
ElNino[1,2] = 1
ElNino[1,3] = 1
}
if (LaNina[1,1] < 0) {
LaNina[1,2] = 1
LaNina[1,3] = 1
}
if (Neutral[1,1] != 0) {
Neutral[1,2] = 1
Neutral[1,3] = 1
}
for (r in seq(2, length(ONI_rounded), 1)) {
if (ElNino[r,1] > 0) {
ElNino[r,2] = 1
ElNino[r,3] = ElNino[r,2] + ElNino[r - 1,3]
}
if (LaNina[r,1] < 0) {
LaNina[r,2] = 1
LaNina[r,3] = LaNina[r,2] + LaNina[r - 1,3]
}
if (Neutral[r,1] != 0) {
Neutral[r,2] = 1
Neutral[r,3] = Neutral[r,2] + Neutral[r - 1,3]
}
}
for (l in seq(4, 1, -1)) {
for (r in seq(2, length(ONI_rounded), 1)) {
if (ElNino[r,3] == 0 && ElNino[r - 1,3] <= l) {
ElNino[r - 1,3] = 0
}
if (LaNina[r,3] == 0 && LaNina[r - 1,3] <= l) {
LaNina[r - 1,3] = 0
}
if (Neutral[r,3] == 0 && Neutral[r - 1,3] <= l) {
Neutral[r - 1,3] = 0
}
}
}
pos_idx = which(ElNino[,3] == 0)
ElNino[pos_idx,1] = NA
neg_idx = which(LaNina[,3] == 0)
LaNina[neg_idx,1] = NA
neu_idx = which(Neutral[,3] == 0)
Neutral[neu_idx,1] = NA
```
```{r}
#| echo: false
# Note that the above needs to be 'echo' and not 'include' so that the error checks print.
# Pull out the values we need for the report
yr_of_int <- which(ONI_full$YR == RptYr)
prev_yrs <- which(ONI_full$YR < RptYr)
all_yrs <- which(ONI_full$YR <= RptYr)
monthly_min_RptYr <- min(ONI_full$ANOM[yr_of_int])
monthly_min_PrevYrs <- min(ONI_full$ANOM[prev_yrs])
monthly_max_RptYr <- max(ONI_full$ANOM[yr_of_int])
monthly_max_PrevYrs <- max(ONI_full$ANOM[prev_yrs])
if (monthly_max_RptYr > monthly_max_PrevYrs) {
print('The highest monthly value was in the report year.')
}
if (monthly_max_RptYr > monthly_max_PrevYrs) {
print('The lowest monthly value was in the report year.')
}
```
```{r}
#| include: false
# Write csv for dashboard
# Note that dashboard output has its own folder
# Thanks to Emily Conklin for this chunk of code!
month_list <- c("DJF", "JFM", "FMA", "MAM", "AMJ", "MJJ",
"JJA", "JAS", "ASO", "SON", "OND", "NDJ")
ENSO_dashboard <- ONI_full %>%
rowwise() %>%
mutate(Month = which(month_list == SEAS)) %>%
mutate(Date_Time = paste0("15-", Month, "-", as.character(YR), " 00:00")) %>%
mutate(Value_lm = NA, Anom = NA, Anom_lm = NA, ID = "ENSO", Units = "Oceanic Nino Index") %>%
select(Date_Time, Year = YR, Month, Value = ANOM, Anom, ID, Value_lm, Anom_lm, Units) |>
filter(Year <= RptYr)
write_csv(ENSO_dashboard, file = here("Indicator_Dashboard", "Data", paste('ENSO_Dashboard_Data_', RptYr, '.csv', sep = "")))
```
```{r}
#| include: false
# Borrowing code from the dashboard for this chunk
# so that figures look the same across products
indicator_data <- ENSO_dashboard |>
filter(Year <= RptYr)
# Create color palette for easy reference
oceans <- nmfs_palette("oceans")(3) # 1 = report_year, 2 = La Niña, 3 = previous years
crustacean <- nmfs_palette("crustacean")(4) # 1 = linear trend
coral <- nmfs_palette("coral")(3) # 2 = El Niño, 3 = annual average for Rpt Yr
ann_grey <- "#D0D0D0" # annual means; in NMFS branding guide but not in package
waves <- nmfs_palette("waves")(3) # annual means; in NMFS branding guide but not in package
seagrass <- nmfs_palette("seagrass")(3)
pal <- c(oceans[3], coral[2], waves[2], coral[3], crustacean[2])
# Formatting
plot_title_font <- list(size = 14)
plot_height <- 350 #in pixels
#calculate pos/neg/neutral ENSO values
#needs to be greater than 0.5 or less than -0.5 for at least five consecutive seasons
indicator_data <- indicator_data |> mutate(Sign = sign(Value)) |>
mutate(Sign = ifelse(test = (Value < 0.5 & Value > -0.5), yes = 0, no = Sign)) |> #positive or negative?
group_by(Sign, Group = with(rle(Sign), rep(seq_along(lengths), lengths))) |> #how many seasons in a row?
ungroup() |>
add_count(Group) |>
mutate(ENSO = "Neutral") |>
mutate(Units = "Oceanic Niño Index (ONI)")
#add ENSO category
indicator_data[which(indicator_data$Sign == -1 & indicator_data$n >= 5),]$ENSO <- "La Niña"
indicator_data[which(indicator_data$Sign == 1 & indicator_data$n >= 5),]$ENSO <- "El Niño"
#color-coded bar chart for ENSO
p1 <- plot_ly(indicator_data, x = dmy_hm(indicator_data$Date_Time), y = ~Value, type = "bar",
height = plot_height, name = ~ENSO, color = ~ENSO,
colors = c(coral[2], oceans[2], ann_grey))
#apply same layout parameters for all plots
#custom x axis (min, every decade, report year)
all_years <- unique(indicator_data$Year)
first_date <- as.character(parse_date_time(indicator_data$Date_Time[1], orders = "d-b-Y H:M"))
date_axis <- c(first_date,
all_years[which(all_years %% 10 == 0)],
RptYr)
p1 <- p1 |> layout(title = list(text = "Indicator Time Series", x = 0.01, font = plot_title_font), #add title
xaxis = list(type = "date", tickformat = "%Y", tickmode = "array", tickvals = date_axis, tickangle = 90, showgrid = TRUE),
#xaxis = list(tick0 = min(indicator_data$Date_Time), dtick = "M24"),
yaxis = list(title = indicator_data$Units[1], hoverformat = '.3f', range = list(-3, 3), tickvals = list(-3, -2, -1, 0, 1, 2, 3)), #add units and round values in hover display; not sure of a better way to set axis limits and ticks...
paper_bgcolor = 'transparent', plot_bgcolor = 'transparent', #transparent background
hovermode = "x unified", #show data for all traces simultaneously
hoverlabel = list(namelength = -1)) #don't cutoff hoverinfo due to length
#return plot
save_image(p1, paste(Dir, '/ONI_ts_', RptYr, '.pdf', sep = ""))
# Handy reference, in case this doesn't work in the future
# from: https://stackoverflow.com/questions/64028289/export-plot-from-plotly-into-pdf
# install.packages('reticulate')
# reticulate::install_miniconda()
# reticulate::conda_install('r-reticulate', 'python-kaleido')
# reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')
# reticulate::use_miniconda('r-reticulate')
# To export:
# library(plotly)
# kaleido(FINAL_plot, "FINAL_plot.pdf")
```
Rationale: The El Niño – Southern Oscillation (ENSO) cycle is known to have impacts on Pacific fisheries including tuna fisheries. The ONI focuses on ocean temperature, which has the most direct effect on these fisheries.
Status: The Oceanic Niño Index (ONI) indicated a transition from La Niña to El Niño conditions in `r RptYr`. In `r RptYr`, the ONI ranged from `r monthly_min_RptYr` to `r monthly_max_RptYr`. This is within the range of values observed previously in the time series.
Description: The three-month running mean (referred to as a season) of satellite remotely-sensed sea surface temperature (SST) anomalies in the Niño 3.4 region (5°S – 5°N, 120° – 170°W). The Oceanic Niño Index (ONI) is a measure of the El Niño – Southern Oscillation (ENSO) phase. Warm and cool phases, termed El Niño and La Niña respectively, are based in part on an ONI threshold of ± 0.5 °C being met for a minimum of five consecutive overlapping seasons. Additional atmospheric indices are needed to confirm an El Niño or La Niña event, as the ENSO is a coupled ocean-atmosphere phenomenon. The atmospheric half of ENSO is measured using the Southern Oscillation Index.
Timeframe: Three-month running average.
Region/Location: Niño 3.4 region, 5°S – 5°N, 120° – 170°W.
Measurement Platform: *In-situ* station, satellite, model.
Data available at: <https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt>.
Sourced from NOAA CPC (2024).
Graphics produced in part using Stawitz (2023).
## Additional Information
Data were manually copied and pasted into a .csv. These data have precision to the hundredth of a degree. They are subsequently rounded in this script to match the data on the more commonly used [CPC site](https://origin.cpc.ncep.noaa.gov/products/analysis_monitoring/ensostuff/ONI_v5.php), which are rounded to the tenth of a degree.
No data are saved for the Council portal because ONI isn't examined as an annual mean.