[英]how to convert longitude from 0 - 360 to -180 - 180
The longitude in CMIP5 future climate data is in 0 - 360 degree. CMIP5未来气候数据的经度为0 - 360度。 How can I convert it to -180 - 180 degree using the raster package?
如何使用光栅包将其转换为-180 - 180度?
I tried with shift(r0,-180)
and shift(r0,-360)
. 我尝试使用
shift(r0,-180)
和shift(r0,-360)
。 It does not work. 这是行不通的。 Any help will be appreciated.
任何帮助将不胜感激。
r0
here is a raster. 这里的
r0
是一个栅格。
Try rotate()
. 尝试
rotate()
。 Its help page even mentions its utility with the type of data you're dealing with: 它的帮助页面甚至提到了它与你正在处理的数据类型的效用:
Rotate a Raster* object that has x coordinates (longitude) from 0 to 360, to standard coordinates between -180 and 180 degrees.
将x坐标(经度)从0到360的Raster *对象旋转到-180到180度之间的标准坐标。 Longitude between 0 and 360 is frequently used in data from global climate models.
全球气候模型的数据经常使用0到360之间的经度。
Here's a simple reproducible example to show what it does: 这是一个简单的可重现的例子来展示它的作用:
library(raster)
r <- raster(matrix(1:100, ncol=10), 0, 360, -90, 90, crs="+proj=merc")
r2 <- rotate(r)
r2
# class : RasterLayer
# dimensions : 10, 10, 100 (nrow, ncol, ncell)
# resolution : 36, 18 (x, y)
# extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=merc
# data source : in memory
# names : layer
# values : 1, 100 (min, max)
这很简单:
ifelse(r0 > 180, -360 + r0, r0)
This is kind of a hack and there's probably a much easier way to do that in raster
, but here is an option. 这是一种破解,在
raster
可能有更简单的方法,但这是一个选项。 First, you need to create a matrix from your raster object, then modify some longitude values (only the ones that are > 180) and switch back to a raster. 首先,您需要从栅格对象创建矩阵,然后修改一些经度值(仅限大于180的值)并切换回栅格。 The
marmap
package can do the back and forth switching for you: marmap
包可以为您进行来回切换:
# Switching from a raster to a matrix of class 'bathy'
library(marmap)
temp <- as.bathy(r0)
summary(temp)
# Changing the relevant longitude
names <- as.numeric(row.names(temp))
names[names > 180] <- names[names > 180] - 360
# Renaming the longitudes and switching back from a 'bathy' object to a raster
rownames(temp) <- names
r0.modified <- as.raster(temp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.