0

If I have this raster with 40 x 40 resolution.

 library(raster)
 #get some sample data
 data(meuse.grid)
 gridded(meuse.grid) <- ~x+y
 meuse.raster <- raster(meuse.grid)
 res(meuse.raster)
 #[1] 40 40

I would like to downscale this raster to 4 x 4. If a pixel of 40x40 = 125, so use the same values for all pixels of 4x4x within this pixel.

just divide each pixel of 40 x40 into 4x4 with keeping its value.

I am open to CDO solutions as well.

temor
  • 935
  • 2
  • 10
  • 26
  • Perhaps you want to [resample](https://stackoverflow.com/questions/72044284/how-to-change-a-raster-to-a-specific-spatial-resolution) using `terra`. – Chris Aug 04 '22 at 11:26

1 Answers1

3

We can use raster::disaggregate

library(raster)

#get some sample data
data(meuse.grid)
gridded(meuse.grid) <- ~x+y
meuse.raster <- raster(meuse.grid)

#assign geographic coordinate system (from coordinates and location (Meuse) it seems like the standard projection for the Netherlands (Amersfoort, ESPG:28992)
crs(meuse.raster) <- "EPSG:28992"

#disaggregate
meuse.raster.dissaggregated <- disaggregate(meuse.raster, c(10,10))

I used c(10,10) to disaggregated from a 40x40 to 4x4 resolution (10 times more detailed).

res(meuse.raster.dissaggregated)
[1] 4 4

In the comments Chris mentioned the terra package. I also recommend shifting from raster to terra. I believe its the newest package and will eventually replaces packages like raster and stars.

terra also has a disaggregation function terra::disagg() which works in a similar way.

maarvd
  • 1,254
  • 1
  • 4
  • 14
  • Thanks, so the values will not be changed? – temor Aug 04 '22 at 11:48
  • Indeed, you can save the rasters and open them in for example for a quick check. Documentation of ```terra::disagg()```: Create a SpatRaster with a higher resolution (smaller cells). The values in the new SpatRaster are the same as in the larger original cells. Documentation of ```raster::disaggregate()```: Disaggregate a RasterLayer to create a new RasterLayer with a higher resolution (smaller cells). The values in the new RasterLayer are the same as in the larger original cells unless you specify method="bilinear", in which case values are locally interpolated (using the resample function). – maarvd Aug 04 '22 at 13:15