简体   繁体   中英

how to convert longitude from 0 - 360 to -180 - 180

The longitude in CMIP5 future climate data is in 0 - 360 degree. How can I convert it to -180 - 180 degree using the raster package?

I tried with shift(r0,-180) and shift(r0,-360) . It does not work. Any help will be appreciated. r0 here is a raster.

Try 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. Longitude between 0 and 360 is frequently used in data from global climate models.

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. 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. The marmap package can do the back and forth switching for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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