Transforming raster with odd number of rows and unusual extent without resampling - r

89 Views Asked by At

I have a raster with unusual dimensions and extent, these are global maps of ocean data.

rodd=rast(ymin=-91,ymax=91,xmin=-1,xmax=359,nrows=91,ncols=180,crs='WGS84')

and I would like to get it into this format so I can stack it with well-formatted data

r=rast(ymin=-90,ymax=90,xmin=-180,xmax=180,nrows=90,ncols=180,crs='WGS84')

I have tried different approaches but in all cases the data gets shifted or modified and my data is very sensitive to the location. For example data that was 100km from the coast is now 50km from it.

I have tried different function from terra: first rotate and then resample. I also tried to remove the bottom row as it contains NA values and then crop. But in all cases I experience this geographic shift of data that makes the data not usable. I also tried resample with "mean" but I get the following error "Error: [resample] not a valid warp method". Why is resample blurring data to adjacent cells?

Is there a way to minimize and data shift and loss of information?

1

There are 1 best solutions below

5
On

How about this?

library(terra)
#> terra 1.6.33

r <- rast(ymin = -91, ymax = 91, xmin = -1, xmax = 359, 
          nrows = 91, ncols = 180, 
          crs = "epsg:4326")

# `?rotate`:
# Rotate a SpatRaster that has longitude coordinates from 0 to 360, 
# to standard coordinates between -180 and 180 degrees (or vice-versa).
r2 <- rotate(r, left = TRUE) 
r2
#> class       : SpatRaster 
#> dimensions  : 91, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -181, 179, -91, 91  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

# adjust for shift in x and y and dimension
r3 <- shift(r2, dx = 1, dy = -1)
r3
#> class       : SpatRaster 
#> dimensions  : 91, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -180, 180, -92, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

# now you can crop the bottom row (-92° to -90°) consisting of `NA`
r4 <- crop(r3, ext(-180, 180, -90, 90), snap = "in")
r4
#> class       : SpatRaster 
#> dimensions  : 90, 180, 1  (nrow, ncol, nlyr)
#> resolution  : 2, 2  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

Created on 2022-12-16 with reprex v2.0.2