This is my first attempt working with data in a .nc file. I manually downloaded the dataset (https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2/sst.ltm.1961-1990.nc) and saved it to my computer. However, I'm having difficulty extracting the data for analysis.
# open data file
sst <- nc_open("./Data_Covariates/Raw/sst.ltm.1961-1990.nc")
# view metadata
print(sstOld)
# extract data
lat = ncvar_get(sst, "lat")
lon = ncvar_get(sst, "lon")
date = ncvar_get(sst, "time")
sstVar = ncvar_get(sst, "short sst")
# from the metadata, I assumed that "short sst" was the name of the variable for sea surface temperature, however, I get the following error:
Error in vobjtovarid4(nc, varid, verbose = verbose, allowdimvar = TRUE) :
Variable not found
Additionally, the metadata states that unit for time is days since 1800-01-01 00:00:0.0. However, the values are all negative numbers and I'm unsure how to convert these to actual dates.
The data starts in 1961, so I think we need to use the ltm_range
attribute. Here's a full reprex, including plots:
library(ncdf4)
sst <- nc_open("sst.ltm.1961-1990.nc")
lat = ncvar_get(sst, "lat")
lon = ncvar_get(sst, "lon")
date = cumsum(c(0, diff(ncvar_get(sst, "time")))) +
ncatt_get(sst, "time")$ltm_range[1]
date = as.Date(date, origin = "1800-01-01")
sstVar = ncvar_get(sst, "sst")
df <- data.frame(lat = rep(rep(lat, each = length(lon)), length(date)),
lon = rep(rep(lon, length(lat)), length(date)),
date = rep(date, each = length(lat) * length(lon)),
sst = as.vector(sstVar))
library(ggplot2)
ggplot(df, aes(lon, lat, fill = sst)) +
geom_raster() +
scale_fill_viridis_c(na.value = "gray50") +
facet_wrap(.~date) +
theme_minimal()