Why do I get different results in plotting a NetCDF layer with “image(x,y,z)” and “plot (raster)” using R-package Raster?

社会主义新天地 提交于 2019-12-24 15:46:36

问题


This might be something really simple to fix.

I want to plot over a map a data layer from a NetCDF file using the function plot(raster). I don't know why I'm getting a raster skewed/offset (My guess is that the problem is in the transformation, resolution?) as shown in the following image.

Incorrect map

If I use the function image(x,y,z...) with the lat,lng, value matrices I get the correct display as is shown here:

Correct map

This is the code in R that I'm using:

library(ncdf)
library(raster)

# This is opening the NetCDF layer as a raster
varRaster<-raster("SMOS_File.nc", varname="Soil_Moisture")

# Showing the information of the raster
varRaster
class       : RasterLayer 
dimensions  : 586, 1383, 810438  (nrow, ncol, ncell)
resolution  : 0.2603037, 0.2916659  (x, y)
extent      : -180, 180, -85.4581, 85.4581  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : SMOS_File.nc 
names       : Retrieved.soil.moisture.value 
zvar        : Soil_Moisture 

plot(varRaster)
data(wrld_simpl)
plot(wrld_simpl, add = TRUE) #This produces the incorrect map 

# If I use the "image" function I can get the right overlay of the data

ex.nc = open.ncdf("SMOS_File.nc")
print(ex.nc)
summary(ex.nc)
y = get.var.ncdf( ex.nc, "lat")  
x = get.var.ncdf( ex.nc, "lon")
z = get.var.ncdf( ex.nc, "Soil_Moisture")

image(x,y,z, zlim=c(-0.9,1), col = heat.colors(37))
plot(wrld_simpl, add = TRUE) #This produces the correct map 

Any idea on why this could be happening? I'd like to use the raster version and save it as geotiff.


回答1:


I was assuming that the netcdf layer was a regular grid and it's not. So coordinate system + transformation is required (thanks to Spacedman at gis.stackexchange.com)

Data provider confirmed EASE coordinate system - EPSG code 3410

The post that helped me with this from Spacedman at https://gis.stackexchange.com/questions/48518/how-to-re-project-ease-equal-area-scalable-earth-grid-with-a-25-km-cylindrica

Now the following seems to work...

library(ncdf)
library(raster)
library(maptools)
source("http://dl.dropboxusercontent.com/u/3394649/R_libs/ConversionFunctions.R") #Thanks to Spacedman at  https://gis.stackexchange.com/questions/48518/how-to-re-project-ease-equal-area-scalable-earth-grid-with-a-25-km-cylindrica

data(wrld_simpl)

varRaster <- ConvertGrid("SMOS_File.nc", "Soil_Moisture")

varRaster[varRaster < 0 ] <- NA
varRaster <- TransformTo(varRaster)

#With Plot
plot(varRaster)
plot(wrld_simpl, add = TRUE)

#Now with 'image'
image(varRaster, useRaster=TRUE)
plot(wrld_simpl, add = TRUE)

Thanks for the help...

Guillermo



来源:https://stackoverflow.com/questions/19330710/why-do-i-get-different-results-in-plotting-a-netcdf-layer-with-imagex-y-z-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!