Extracting data from .nc file

480 Views Asked by At

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.

2

There are 2 best solutions below

5
On BEST ANSWER

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()

enter image description here

1
On

The variable name is sst array with 3 dimensions.

sstVar = ncvar_get(sst, "sst") 
dim(sstVar)
# [1] 360 180  12

And for date, we need origin:

as.Date(date, origin = "1800-01-01")
# [1] "0000-12-30" "0001-01-30" "0001-02-27" "0001-03-30" "0001-04-29"
# [6] "0001-05-30" "0001-06-29" "0001-07-30" "0001-08-30" "0001-09-29"
# [11] "0001-10-30" "0001-11-29"