Question

how can I do a inverse coordinate transformation in the netcdf file ? I have a grid with 75 longitude values and 36 latitude values:

nc<-create.n("filename.nc")
#Dimentions
dim.def.nc(nc,"lon",75)
dim.def.nc(nc,"lat",36)
dim.def.nc(nc,"time",365)
#Vars
var.def.nc(nc,"Observation","NC_FLOAT", c(1,0,2))
var.def.nc(nc,"lon","NC_FLOAT", c(0))
var.def.nc(nc,"lat","NC_FLOAT", c(1))
var.def.nc(nc,"time","NC_FLOAT", c(2))
(...)

According to the documentation in unidata, it should be possible to have the netcdf doing the inverse transformation from (lat,lon) to (x,y), but I don't have any idea how I can perform this. I want to transfor my lat long grid to a Lambert Conformal grid.

Was it helpful?

Solution

This is the way I proceed myself to reproject a netCDF file: basically I grab the longitude, the latitude and the data I want and create a shapefile that I can then work with with packages rgdal, sp and maptools. (The example here uses data from NOAA downloaded here)

library(ncdf)
library(rgdal)
library(sp)
library(maptools)
nc <- open.ncdf("20130128-ABOM-L4HRfnd-AUS-v01-fv01_0-RAMSSA_09km.nc")

# Grab the longitude, latitude and data
lon <- nc$dim$lon$vals
lat <- nc$dim$lat$vals
sst <- get.var.ncdf(nc,"analysed_sst")

# Create a SpatialPointsDataFrame object
lonlat <- expand.grid(lon,lat)
sst <- as.data.frame(matrix(sst,ncol=1))
dat <- SpatialPointsDataFrame(lonlat, data=sst,
                              proj4string=CRS("+proj=longlat +datum=WGS84 "))

# And then reproject
dat2 <- spTransform(dat,CRS("+proj=lcc"))  
# Of course you have to write the proj4 string that corresponds exactly to the desired projection.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top