【问题标题】:R- plotting 2.5 grid netcdf data with country contourR- 用国家等高线绘制 2.5 网格 netcdf 数据
【发布时间】:2018-06-20 07:58:26
【问题描述】:

我正在尝试绘制具有 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

【问题讨论】:

  • 你能把你看到的所有错误信息都贴出来吗?
  • 我强烈建议使用包raster 来处理这样的简单netCDF。它非常方便。即将推出的stars 包会更好,但它仍在大力开发中。
  • 我用光栅试试,谢谢!

标签: r plot netcdf


【解决方案1】:

有几件事需要解决:

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

【讨论】:

  • 你说得对,我忘了把它复制在这里,它是 35。谢谢你的链接,我会检查出来。
猜你喜欢
  • 2018-07-10
  • 1970-01-01
  • 2017-01-15
  • 2013-05-16
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多