Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

raster

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.

like image 564
user1617676 Avatar asked Sep 08 '14 18:09

user1617676


2 Answers

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)
like image 198
Josh O'Brien Avatar answered Sep 17 '22 02:09

Josh O'Brien


It's pretty simple:

ifelse(r0 > 180, -360 + r0, r0)
like image 22
Señor O Avatar answered Sep 17 '22 02:09

Señor O