簡體   English   中英

使用R將GRIB數據繪制為具有超過360度經度的地圖

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

我正在嘗試自動獲取波浪高度預測的數據並構建類似於此的圖。

使用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)

但是,如果你仔細看看我上面發布的鏈接,你會發現一個微妙的差異:鏈接的圖表經度超過360度。

這對我正在做的事情非常重要,因為它允許我輕松檢查同一地塊內所有海洋上的膨脹 - 如果一次只顯示360度,這就更難了,因為這會強制導致其中一個海洋切。

盡管我付出了最大努力,但我找不到繪制超過360度的方法,因為GRIB格式“太聰明”不允許(這不僅僅是偏移數據,而是重復其中的一部分)。

任何見解將不勝感激。 干杯

我會將數據從raster包加載到柵格堆棧中,然后使用mergecrop功能。 基本上你復制光柵,將其移動360度,然后將其與自身合並,然后裁剪它。 這是一個功能:

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))
}

以下是一些用法:

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

首先移動和裁剪光柵可能效率更高,然后合並,但這是一個開始,只需幾秒鍾就可以在光柵上運行。

如果你所關心的只是繪圖,那么編寫一個繪制兩次的函數可能會更容易,一次調整范圍。 但是,必須為光柵中的每個波段完成....

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)
}

然后你做:

wraplot(gstack[[1]])

查看raster包的所有功能。

更天真的方法是創建第二個數據集,偏移量為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)

在此輸入圖像描述

您可以使用xlim以您想要的方式居中:

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

在此輸入圖像描述

這是有效的,因為數據在常規網格上,因此不需要插值,否則,@ Spacedman解決方案是首選。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM