R-用国家轮廓绘制2.5网格netcdf数据

问题描述 投票:-1回答:1

我正在尝试绘制具有2.5 x 2.5网格的国家轮廓的降水数据,数据可在此链接中找到:https://www.esrl.noaa.gov/psd/data/gridded/data.cmap.html“Mean(Enhanced Monthly)”

我使用的答案是:R - Plotting netcdf climate data。但是我收到了一个错误。

这就是我所做的:

library(ncdf4)
ncpath <- "C:/Users/"
ncname <- "precip.mon.mean"
ncfname <- paste(ncpath,ncname,".nc",sep="")
ncin <- nc_open(ncfname)

lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)

lat <- ncvar_get(ncin, "lat")
nlat <- dim(lat)

dname <-"precip"
ppt_array <- ncvar_get(ncin,dname)
dim(ppt_array)

pres <- ppt_array[ , ,25:444] 
precip <- array(pres, , dim=c(nlon, nlat, 12, ano)) 
prec <- precip[97:115,21:34, ,1:ano] #I just want a piece of the map

这是我遇到问题的地方:

latlat <- rev(lat)
precipit <- prec[ , ,1,1] %Just to see if it works
lonlon <- lon-180
image(lonlon,latlat,precipit) 
library(maptools)
data(wrld_simpl) 

#however I don't know if this will work to plot just a portion of the map  
plot(wrld_simpl,add=TRUE)

我有几个错误,有人可以帮忙吗?

编辑:我得到的错误是这些:

> image(lonlon,latlat,precipit)
Error in image.default(lonlon, latlat, precipit) : 
  increasing 'x' and 'y' values expected
> library(maptools)
> data(wrld_simpl)
> plot(wrld_simpl,add=TRUE)
Error in polypath(x = mcrds[, 1], y = mcrds[, 2], border = border, col = col,  : 
  plot.new has not been called yet
r plot netcdf
1个回答
0
投票

有几件事需要修复:

1)ano似乎没有在任何地方定义。也许它是以交互方式定义的?

precip <- array(pres, , dim=c(nlon, nlat, 12, ano)) 

2)看起来你打算添加一条评论但是使用infix operator代替 - 用#替换它,如下所示:

precipit <- prec[ , ,1,1] # Just to see if it works

3)如果你只想拥有地图的一部分,你可以确保latlon数组都匹配你想要显示的区域(基本上裁剪世界地图)或者在要突出显示的区域之外定义NA(这将显示类似于地图here

© www.soinside.com 2019 - 2024. All rights reserved.