gpt4 book ai didi

r - 在不规则网格上绘制气候数据的正确方法

转载 作者:行者123 更新时间:2023-12-04 02:16:30 24 4
gpt4 key购买 nike

我在 Efficient way to plot data on an irregular grid 中提出了这个问题问题,但一般的反馈是将原始问题分成更易于管理的块。因此,这个新问题。

我使用组织在不规则二维网格上的卫星数据,其维度是扫描线(沿轨道维度,即 Y 轴)和地面像素(跨轨道维度,即 X 轴)。每个中心像素的经纬度信息存储在辅助坐标变量中,以及四个角坐标对(经纬度坐标在 WGS84 引用椭球上给出)。

让我们构建一个玩具数据集,其中包含一个 12x10 的潜在不规则网格和相关的表面温度测量值。

library(pracma) # for the meshgrid function
library(ggplot2)

num_sl <- 12 # number of scanlines
num_gp <- 10 # number of ground pixels
l <- meshgrid(seq(from=-20, to=20, length.out = num_gp),
seq(from=30, to=60, length.out = num_sl))

lon <- l[[1]] + l[[2]]/10
lat <- l[[2]] + l[[1]]/10

data <- matrix(seq(from = 30, to = 0, length.out = num_sl*num_gp),
byrow = TRUE, nrow = num_sl, ncol = num_gp) +
matrix(runif(num_gp*num_sl)*6, nrow = num_sl, ncol = num_gp)

df <- data.frame(lat=as.vector(lat), lon=as.vector(lon), temp=as.vector(data))
lonlat数据包含我正在使用的原始产品中提供的中心像素坐标,存储为二维矩阵,其轴是ground_pixel(X 轴)和扫描线(Y 轴)。 data矩阵——相同的维度——包含我的测量值。然后我将三个矩阵展平并将它们存储在一个数据框中。

我想在 map 上绘制地面像素(作为四边形),并相应地填充温度测量值。

使用瓷砖我得到:
ggplot(df, aes(y=lat, x=lon, fill=temp)) + 
geom_tile(width=2, height=2) +
geom_point(size=.1) +
borders('world', colour='gray50', size=.2) +
coord_quickmap(xlim=range(lon), ylim=range(lat)) +
scale_fill_distiller(palette='Spectral') +
theme_minimal()

Using tiles

但这不是我所追求的。我可以玩 widthheight使瓷砖彼此“接触”,但当然这甚至不会接近我想要的目标,即在 map 上绘制实际投影的地面像素。
例如,Python 的 xarray 可以根据像素中心坐标自动推断像素边界:

Xarray solution



有没有办法在 R 中实现相同的结果,即:从像素中心自动推断像素边界,并将像素绘制为 map 上的填充多边形?也许使用 sf包裹?

我可以在回答中看到它完成 question但答案是指使用 sf对我来说有点不清楚,因为它处理不同的投影和潜在的常规网格,而在我的情况下,我想我不必重新投影我的数据,而且,我的数据不在常规网格上。

如果这是不可能的,我想我可以在我的产品中使用像素边界信息,但如果这个问题被证明不容易解决,那也许这是另一个问题的主题。

最佳答案

这是执行此操作的一种方法。可能有更简单的东西,但这是有效的。

首先,我将使用 raster 包来操作坐标。我在这里创建的栅格是“非常规的”,因为它们包含的值是位置数据。但是为此使用栅格而不是矩阵可以访问一些有用的功能,例如 extend最有用的是 resample ,我将使用它的双线性插值函数来查找顶点

library(raster)
latr = raster(lat)
lonr = raster(lon)

find.vertices = function(m){
r = raster(m)
vertices = raster(matrix(nrow = dim(r)[1]+1, ncol = dim(r)[2]+1))
x = extend(r, c(1,1))
x[1,] = 2*x[2,] - x[3,]
x[dim(x)[1],] = 2*x[dim(x)[1]-1,] - x[dim(x)[1]-2,]
x[,1] = 2*x[,2] - x[,3]
x[,dim(x)[2]] = 2*x[,dim(x)[2]-1] - x[,dim(x)[2]-2,]
extent(vertices) = extent(r) + res(r)
vertices = resample(x, vertices)
}

latv = find.vertices(lat)
lonv = find.vertices(lon)
df2 = data.frame(xc = lonv[], yc = latv[])

让我们绘制这些顶点以检查我们是否在正轨上:
ggplot(df, aes(y=lat, x=lon, fill=temp)) + 
geom_tile(width=2, height=2) +
geom_point(size=.1) +
geom_point(aes(xc, yc), data=df2, inherit.aes =F) +
borders('world', colour='gray50', size=.2) +
coord_quickmap(xlim=range(lon), ylim=range(lat)) +
scale_fill_distiller(palette='Spectral') +
theme_minimal()

enter image description here

现在我们创建一些 Polygon从这些顶点
nx = NCOL(latv)
ny = NROW(lonv)
polys = list()
for (i in 1:length(data)) {
x = col(data)[i]
y = row(data)[i]
polys[[i]] = Polygon(cbind(
lonv[c((x-1)*ny + y, (x-1)*ny + y + 1, x*ny + y + 1, x*ny + y, (x-1)*ny + y)],
latv[c((x-1)*ny + y, (x-1)*ny + y + 1, x*ny + y + 1, x*ny + y, (x-1)*ny + y)]
))
}

转换列表 PolygonSpatialPolygonsDataFrame
Polys = sapply(1:length(polys), function(i) Polygons(polys[i], i))
SPolys = sapply(1:length(polys), function(i) SpatialPolygons(Polys[i], i))
SPolys = do.call(bind, SPolys)
SPolysdf = SpatialPolygonsDataFrame(SPolys, data.frame(data=as.vector(data)))

要在 ggplot 中绘制此对象,我们需要转换为常规 data.frame .传统上大多数人都使用过 fortify为了这。但是 ggplot 文档警告说这可能会被弃用,并建议改用 broom 包。我对扫帚不太熟悉,但我决定遵循以下建议:
library(broom)
ggSPolysdf = tidy(SPolysdf)
ggSPolysdf = cbind(ggSPolysdf, data = rep(as.vector(data), each=5))

最后我们可以绘制:
ggplot(df, aes(y=lat, x=lon, fill=temp)) + 
geom_polygon(aes(long,lat,fill=data, group = id), data=ggSPolysdf) +
borders('world', colour='gray50', size=.2) +
coord_quickmap(xlim=range(lon), ylim=range(lat)) +
scale_fill_distiller(palette='Spectral') +
theme_minimal()

enter image description here

关于r - 在不规则网格上绘制气候数据的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com