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

The longitude in the CMIP5 climate data is 0 - 360 degrees. How to convert it to -180 - 180 degrees using a raster package?

I tried with shift(r0,-180) and shift(r0,-360) . This does not work. Any help would be appreciated. r0 here is the raster.

+6
source share
3 answers

Try rotate() . The help page even mentions its usefulness with the type of data you are dealing with:

Rotate the Raster * object, which has x (longitude) coordinates from 0 to 360, to standard coordinates from -180 to 180 degrees. Longitudes between 0 and 360 are often used in global climate model data.

Here is 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) 
+12
source

It's kind of a hack, and it's probably much easier to do it in raster , but here's an option. First, you need to create a matrix from your raster object, and then change some values ​​of longitude (only those that are> 180) and return to the raster. The marmap package can do the reverse and fourth switch 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) 
+1
source

It is pretty simple:

 ifelse(r0 > 180, -360 + r0, r0) 
0
source

Source: https://habr.com/ru/post/974993/


All Articles