Comparison between packages RnetCDF, ncdf4 and ncdf
Let use the same nc file obtained from NOAA (http://www.esrl.noaa.gov/psd/data/gridded/data.noaa.oisst.v2.highres.html) be used.
Here I show the 3 ways to get annual data depending on the package used.
In conclusion, RnetCDF does not scale data using scale_factor and add_offset automatically. Take care.
utcal.nc from package RnetCDF is very useful.
dest <- "sst.day.mean.2014.v2.nc"
For all the package, I will use the same coordinates: lat=35, lon=28
# Note that the function ind_long_lat can be used data from any of these packages
library(RNetCDF)
dat.RNetCDF <- open.nc(dest)
lat=35
lon=28
# it will be used for all data
library(phenology)
indice <- ind_long_lat(ncdf=dat.RNetCDF, long=lon, lat=lat)
Packages RnetCDF
# take care, it does not scale automatically the data
point_SST.RNetCDF_wrong <- var.get.nc(ncfile=dat.RNetCDF, variable="sst",
start=c(indice ,1), count=c(1, 1, 1))
# correct method
point_SST.RNetCDF_corr <- var.get.nc(ncfile=dat.RNetCDF, variable="sst",
start=c(indice ,1), count=c(1, 1, 1))*
att.get.nc(ncfile=dat.RNetCDF, variable="sst", attribute="scale_factor")+
att.get.nc(ncfile=dat.RNetCDF, variable="sst", attribute="add_offset")
# Get the time
label.time <- dim.inq.nc(dat.RNetCDF, 0)$name
date.char <- utcal.nc(att.get.nc(dat.RNetCDF, label.time, "units"),
var.get.nc(dat.RNetCDF, label.time,
start=1,
count=dim.inq.nc(dat.RNetCDF, 0)$length),
type="s")
date.POSIXlt <- strptime(date.char, format="%Y-%m-%d %H:%M:%S", tz="UTC")
Package ncdf4
library(ncdf4)
dat.ncdf4 <- nc_open(dest)
point_SST.ncdf4 <- ncvar_get(dat.ncdf4, varid="sst", start=c(indice ,1),count=c(1, 1, 1))
# Get the time
# We must check what is the number of time element using names(dat.ncdf$dim); here it is the first one
label.time <- names(dat.ncdf4$dim)[1]
library(RNetCDF)
date.char <- utcal.nc(dat.ncdf4$dim[[label.time]]$units, dat.ncdf4$dim[[label.time]]$vals, type="s")
date.POSIXlt <- strptime(date.char, format="%Y-%m-%d %H:%M:%S", tz="UTC")
Package ncdf
library(ncdf)
dat.ncdf <- open.ncdf(dest)
point_SST.ncdf <- get.var.ncdf(dat.ncdf, varid="sst", start=c(indice ,1),count=c(1, 1, 1))
# Get the time
# We must check what is the number of time element using names(dat.ncdf$dim); here it is the first one
label.time <- names(dat.ncdf$dim)[1]
library(RNetCDF)
date.char <- utcal.nc(dat.ncdf$dim[[label.time]]$units, dat.ncdf$dim[[label.time]]$vals, type="s")
date.POSIXlt <- strptime(date.char, format="%Y-%m-%d %H:%M:%S", tz="UTC")
# Alternative without utcal() function from RNetCDF package:
ref.date <- strptime(gsub("days since ", "", dat.ncdf$dim[[label.time]]$units), format="%Y-%m-%d %H:%M:%S", tz="UTC")
if (substr(dat.ncdf$dim[[label.time]]$units, 1, 4)=="days")
{date.POSIXlt <- ref.date+dat.ncdf$dim[[label.time]]$vals*24*3600} else
{if (substr(dat.ncdf$dim[[label.time]]$units, 1, 5)=="hours")
{date.POSIXlt <- ref.date+dat.ncdf$dim[[label.time]]$vals*3600} else
{date.POSIXlt <- rep(NA, length(dat.ncdf$dim[[label.time]]$vals))}}
Commentaires
Enregistrer un commentaire