Check out my main page
Below is code to automate the extraction of a time series from the high resolution NOAA OISST v2 data set. This code is designed to work for a specific subset of the 0.25x0.25 version from 1982 to 2022. However, it can easily be modified to work for other regions and time frames. First, download the subset from the Red Sea here. After you have the data, all you have to do is input the path to the file on your computer and then add the latitude and longitude for the area you would like to extract the time series from.
#If it is your first time using this code, remove the # symbol from all the lines that say "install.packages"
#install.packages("ncdf4")
#install.packages("raster")
#install.packages("ggplot2")
#install.packages("dplyr")
#install.packages("tidyverse")
#install.packages("patchwork")
library(ncdf4) # package for netcdf manipulation
library(raster) # package for raster manipulation
library(dbplyr)
library(tidyverse)
library(ggplot2)
library(patchwork)
#Some of these packages may give you a hard time with warning messages, luckily, as long as they download they should work perfectly
rs_sst<- brick("/Users/ip367316/Desktop/rs_sst.nc", ncols = 493, varname = "sst") #INPUT YOUR PATHNAME HERE
site_lon <- 42.1353312 #INPUT LONGITUDE
site_lat <- 15.095242 #INPUT LATITUDE
site_series <- raster::extract(rs_sst, SpatialPoints(cbind(site_lon,site_lat))) #extracts the time series
site_series<- t(site_series) #Transforms the matrix received from the above code
site_series<- as.data.frame(site_series) #Converts above matrix into a data frame
month<- rep(c(1,2,3,4,5,6,7,8,9,10,11,12), times = 41) #creates a column of months
year<- rep(c(1982:2022), each = 12) #creates a column of years
yr<- seq(from = 1982.0833, to = 2023, by = 0.0833) #creates a column of years and months in decimal format
yr<- yr - 0.0833 #final formatting on the above column
#nest three lines take the columns we made earlier and stitch them into the data set
site_series$Year<- year
site_series$Month<- month
site_series$YR<- yr
site_series<- site_series %>% rename(SST = V1) #renames the data column
#creating the annual SST time series
site_annual<- site_series %>%
group_by(Year) %>% #groups monthly data by year
summarize(SST = mean(SST)) #takes the mean of the data per year
#creating the cold month SST time series
site_winter<- site_series %>%
group_by(Year) %>%
slice_min(SST) #selects the values in each year that have the lowest SST
#creating the warm month SST time series
site_summer<- site_series %>%
group_by(Year) %>%
slice_max(SST, n = 2) %>% #selects the 2 warmest values each year
summarize(SST = mean(SST))
Save the time series to nice and friendly CSV files. :)
write.csv(site_series, "sitesst.csv")
write.csv(site_annual, "annualsst.csv")
write.csv(site_winter, "wintersst.csv")
write.csv(site_summer, "summersst.csv")