[R-sig-Geo] Suggestions for irregular grid netCDF data?

Michael Treglia mtreglia at gmail.com
Thu Mar 2 04:53:24 CET 2017


​Hi all,

I've been trying to work with a somewhat complex netCDF dataset, and wanted
to see if anybody has suggestions.

The data are for marine variables in the form of a remote netCDF served
through OPENDAP. One of the complexities is that it comes in an irregular
grid, as fine as 25m and as coarse as 400m (coarser as you move further
from the coast) - this variable spacing makes it tough to work with in the
raster package, which seems like it would be the easiet.  Is there a
straightforward, relatively simple way to work with a dataset like this,
getting entire layers at a time? The data have a slew of different
variables, some of which also have time steps and different depth horizons.

So far, I’ve been able to connect to the data using the ncdf4 package in R,
and load simple, 3 dimensional data (e.g., depth). I can also force layers
to load using the raster package, adding the argument
‘stopIfNotEqualSpaced=FALSE', though that obviously has issues with the
irregular grid (though if it worked, would make it easier to pull
information by date and depth horizons). I'm having a tough time getting
data via the ncdf4 package for specific dates/depth horizons though. [note:
I end up having to use R in a linux environment for this, as I understand
the windows implementation for is more limited, and throws an ‘Unknown file
format’ error.]

The data are here:
http://colossus.dl.stevens-tech.edu/thredds/dodsC/LISS/Hindcast/all_nb_mon_81.nc,
and here’s the web-interface for the data:
http://colossus.dl.stevens-tech.edu/thredds/dodsC/LISS/Hindcast/all_nb_mon_81.nc.html.
I've included some sample R code below - and am totally open to alternative
ways that might be better for working with these data [still new-ish to
netCDFs, and have mainly dealt with much simpler, soil or climate datasets].

Thanks!
Mike


Sample R Code:
#Load and work w/ data in ncdf4
library(ncdf4)

nyhops.nc <- nc_open("
http://colossus.dl.stevens-tech.edu/thredds/dodsC/LISS/Hindcast/all_nb_mon_81.nc
")

#Get Point Data with Depth Measurements
depth.nc <- ncvar_get(nyhops.nc, 'depth')
lat.nc <- ncvar_get(nyhops.nc, 'lat')
long.nc <- ncvar_get(nyhops.nc, 'lon')

depth.df <- data.frame(lat=as.vector(lat.nc), long=as.vector(long.nc),
depth=as.vector(depth.nc)) #Can then be exported, or converted to spatial
object

#Trying to get the entirety of a single time point and depth horizon, but
not quite getting the start/count args - this gets a single data point it
seems
temp.nc <- ncvar_get(nyhops.nc, 'temp', start=c(10,10,1,1),
count=c(1,1,1,1))



#Load layers w/ raster package.
library(raster)
nyhops.url <- "
http://colossus.dl.stevens-tech.edu/thredds/dodsC/LISS/Hindcast/all_nb_mon_81.nc
"
depth <- raster(nyhops.url, varname="depth", lvar=1, level=1,
stopIfNotEqualSpaced=FALSE) #Forcing it to read layer despite unequal
spacing of coords
plot(depth) #Plot looks distorted, as expected given irregular grid
spacing. [I realize the data aren't projected at this point]

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list