关于我之前的问题Read/Open Oceancolor data from MODIS with nc format。我试图使用ncdf4 package
使用此代码阅读ncdf oceancolor数据download here
library(ncdf4)
nc <- nc_open('A2014325053500.L2_LAC_SST.nc')
# Get data for each variabel name as a large matrix
sst <- ncvar_get(nc, varid = 'geophysical_data/sst')
lon <- ncvar_get(nc, varid = 'navigation_data/longitude')
lat <- ncvar_get(nc, varid = 'navigation_data/latitude')
比这个数据,我转换为栅格。但是我没有重新投射光栅figure 1
。图片应该像figure 2
library(sp)
library(raster)
r <- raster(sst,
xmn=min(lat), xmx=max(lat),
ymn=min(lon), ymx=max(lon),
crs=CRS('+proj=longlat +ellps=WGS84 +datum=WGS84'))
proj='+proj=longlat +ellps=WGS84 +datum=WGS84'
r2 <- projectRaster(r, crs=proj, method = 'ngb')
plot(r2)
根据jbaums的第一个建议,结果未正确重新投影figure 3
当我提高分辨率时,我才意识到结果有问题。在西部和东部变成白色(没有数据)。我在下面使用了以下代码:
r <- rasterize(df, raster(extent(df), res=0.0103011), 'sst', fun=mean)
答案 0 :(得分:3)
在他们当前的投影中,坐标不会落在常规网格上。
解决此问题的一种方法是将坐标表示为SpatialPoints
对象,然后,如果需要栅格,则rasterize
。
library(ncdf4)
library(raster)
# Read in the data
nc <- nc_open('~/../Downloads/A2014325053500.L2_LAC_SST.nc')
sst <- ncvar_get(nc, varid = 'geophysical_data/sst')
lon <- ncvar_get(nc, varid = 'navigation_data/longitude')
lat <- ncvar_get(nc, varid = 'navigation_data/latitude')
nc_close(nc)
# Create a SpatialPointsDataFrame
p <- data.frame(lon=c(lon), lat=c(lat), sst=c(sst))
coordinates(p) <- ~lon+lat
proj4string(p) <- '+init=epsg:4326'
# Rasterize with appropriate resolution and aggregation function
r <- rasterize(p, raster(extent(p) * 1.04, res=0.05), 'sst', fun=mean)
# for high resolution look at ?gdalUtils::gdal_rasterize for efficiency
这是一个情节:
library(rasterVis)
levelplot(r, at=seq(-75, 75, length=100), margin=FALSE, par.settings=BuRdTheme,
colorkey=list(height=0.6), main='MODIS Sea Surface Temperature')