Interpolating Gridded 3D Data to a finer scale
Asked Answered
W

1

2

I have a NetCDF file of a probability surface. It's a 30x30 grid of 0.25 degree lat/lon intervals with a probability surface described in the z dimension. I can easily import this into Panoply, a NetCDF viewer:

Raw Grid Data

And it's then a breeze (checking one box) to interpolate/smooth the raw data to a finer grid size:

Interpolated Grid Data

However, I don't just want to visualize the data, I want to plot it in R along with bathymetry and point data. That all is no problem, but I have not found a straightforward way to interpolate the gridded data in R. Here's the code I use to import and plot the data:

library(RNetCDF)

nc <- open.nc("132235-1.nc")
print.nc(nc)
tmp <- read.nc(nc)
probs<-tmp$likelihoods

xran <- range(tmp$longitude)
yran <- range(tmp$latitude)
zran <- range(probs,na.rm=T)
lon <- tmp$longitude
lat <- tmp$latitude[30:1]

z <- array(probs, dim=dim(probs))

z <- z[,rev(seq(ncol(z)))]
z <- z[,seq(ncol(z))]



prob.pal<-colorRampPalette(
  c("#C1FFC1","#8FBC8F","#2F4F4F")
)

zbreaks <- seq(0.0001, 0.063, by=0.001)

cols<- c(prob.pal(length(zbreaks)-1))

png("ProbTest.png", width=7.5, height=6, units="in", res=200)
layout(matrix(1:2, 1,2), widths=c(6,1.5), heights=c(6))

par(mar=c(2,2,1,1), ps=10)
image(lon, lat, z=z, col=cols, breaks=zbreaks, useRaster=TRUE, ylim=c(13,28), xlim=c(-115,-100))

dev.off()

And I end up with this, which is the same as using Panoply but with a different color scheme:

R-plotted Prob Surface

Is there a straightforward way to interpolate/smooth this data? I know how to create kernel utilization densities etc. using point data, but not using gridded data.

Many thanks for your assistance!

Wieche answered 29/5, 2015 at 18:48 Comment(1)
I'd use the raster package to first read in the NetCDF file using raster() or brick() and to then smooth the data to a finer-scale resolution using resample().Andyane
K
4

This is the solution I think you're looking for, which uses bilinear resampling. However this is not the only way to do such interpolation and you'd likely need to justify not using a more sophisticated approach (e.g. geostatistical, splines, etc.):

library(raster)
set.seed(2002)

##  Create an extent boundary:
ex <- extent(c(0, 20, 0, 20))

##  Simulate a coarse raster:
r.small <- raster(ex, vals=rnorm(10*10, mean=5, sd=1), nrow=10, ncol=10)

##  Simulate the grid of a finer-scale raster:
r.big <- raster(ncol=200, nrow=200, ext=ex)

##  Resample the coarser raster to match finer grid:
r.res <- resample(x=r.small, y=r.big, method="bilinear")

plot(r.small)
plot(r.res)

Coarse:

Coarse

Fine:

Fine

Klotz answered 29/5, 2015 at 19:51 Comment(2)
Hi Forrest—this most certainly did the trick, and solved a lot of other problems I was having, as I didn't realize raster could read NetCDF files. Many thanks for the solution!Wieche
You're very welcome, but I should say thank you to Robert Hijmans and the rest of the contributors to the raster package as their hard work makes this kind of thing very easy in R.Klotz

© 2022 - 2024 — McMap. All rights reserved.