简体   繁体   English

使用R将GRIB数据绘制为具有超过360度经度的地图

[英]Plotting GRIB data as a map with more than 360 degrees of longitude using R

I am trying to automatically grab data on wave height forecast and build plots similar this one . 我正在尝试自动获取波浪高度预测的数据并构建类似于此的图。

Using R, I could download the data and plot it using: 使用R,我可以下载数据并使用以下方法绘制它:

library(rgdal)
library(fields)

ftp.string <- "ftp://polar.ncep.noaa.gov/pub/waves//20130205.t00z/nww3.HTSGW.grb"
#this link may become broken with time, as folders are removed after some time. just edit the date to reflect the most recent day at the time you run these lines

download.file(ftp.string, "foo.grb", mode="wb")

grib <- readGDAL("foo.grb")
is.na(grib$band1) <- grib$band1 > 100
image(grib, col=(tim.colors(15)), attr=1)

However, if you take a closer look at the link I posted above, you'll notice a subtile difference: the plot from the link spans more than 360 degrees in longitude. 但是,如果你仔细看看我上面发布的链接,你会发现一个微妙的差异:链接的图表经度超过360度。

This is important for what I'm doing as it allows me to easily check for swells on all oceans within the same plot - this is much harder if only 360 degrees are shown at a time, as that forcibly results in one of the oceans being cut. 这对我正在做的事情非常重要,因为它允许我轻松检查同一地块内所有海洋上的膨胀 - 如果一次只显示360度,这就更难了,因为这会强制导致其中一个海洋切。

Despite all my best efforts, I can't find a way to plot more than 360 degrees, as the GRIB format is 'too smart' to allow that (this is not just offsetting the data, but instead repeating a part of it). 尽管我付出了最大努力,但我找不到绘制超过360度的方法,因为GRIB格式“太聪明”不允许(这不仅仅是偏移数据,而是重复其中的一部分)。

Any insights will be greatly appreciated. 任何见解将不胜感激。 Cheers 干杯

I would load your data into a raster stack from the raster package and then use the merge and crop functions. 我会将数据从raster包加载到栅格堆栈中,然后使用mergecrop功能。 Basically you duplicate the raster, shift it 360 degrees, then merge that with itself, then crop it to taste. 基本上你复制光栅,将其移动360度,然后将其与自身合并,然后裁剪它。 Here's a function: 这是一个功能:

require(raster)
wwrap <- function(g,xmax=720){
  gE = extent(g)

  shiftE = extent(360,720,gE@ymin, gE@ymax)
  g2 = g
  extent(g2)=shiftE

  gMerge = merge(g,g2)

  crop(gMerge,extent(0,xmax,gE@ymin, gE@ymax))
}

And here's some usage: 以下是一些用法:

> gstack = stack("foo.grb")
> gstack[gstack>100] = NA
> gstack2 = wwrap(gstack,xmax=460)
> plot(gstack2)
> plot(gstack2[[1]])
> plot(gstack2[[61]])

Probably more efficient to shift and crop the raster first, then merge, but this is a start and it only takes a few seconds to run on your raster. 首先移动和裁剪光栅可能效率更高,然后合并,但这是一个开始,只需几秒钟就可以在光栅上运行。

If all you care about is plotting then it might be easier to write a function that plots it twice, once with a shift in extent. 如果你所关心的只是绘图,那么编写一个绘制两次的函数可能会更容易,一次调整范围。 But that would have to be done for each band in your raster.... 但是,必须为光栅中的每个波段完成....

wraplot <- function(g,xmax=720){
  gE = extent(g)
  ## to setup the plot
  worldWrap = extent(0,xmax,gE@ymin, gE@ymax)
  rWrap = raster(nrows=1,ncols=1,ext=worldWrap)
  rWrap[]=NA
  plot(rWrap)
  ## first part
  plot(g,add=TRUE)
  ## setup and plot it again
  shiftE = extent(360,720,gE@ymin, gE@ymax)
  cropE = extent(360,xmax,gE@ymin, gE@ymax)
  extent(g)=shiftE
  g=crop(g,cropE)
  plot(g,add=TRUE)
}

Then you do: 然后你做:

wraplot(gstack[[1]])

Check out all the features of the raster package. 查看raster包的所有功能。

A more naive approach would be to create a second dataset, with an offset of 360°: 更天真的方法是创建第二个数据集,偏移量为360°:

grib2 <- grib
grib2@bbox[1, ] <- grib2@bbox[1, ] - 360
image(grib, col=(tim.colors(15)), attr=1, xlim=c(-360,360))
image(grib2, add=TRUE, col=(tim.colors(15)), attr=1)

在此输入图像描述

And you can play with xlim to center it the way you want: 您可以使用xlim以您想要的方式居中:

image(grib, col=(tim.colors(15)), attr=1, xlim=c(-270,90))
image(grib2, add=TRUE, col=(tim.colors(15)), attr=1)

在此输入图像描述

Here it works because the data is on a regular grid so there is no need for interpolation, otherwise of course, @Spacedman solution is to be preferred. 这是有效的,因为数据在常规网格上,因此不需要插值,否则,@ Spacedman解决方案是首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM