Search code examples
rautomationraster

R: iteratively apply some analysis and save the output based on a number used for the analysis


I have 1 raster which I am resampling it multiple times (15 times). Each time I resample it, I am saving the result using as a name the name of the raster and some factor (see the code below). For example, if my raster's name is r, the output, after I resample it using a std of 0.2, should be r2. If the std is 0.25, the output should be r25 and so on. So far, I am doing this resampling manually but I would like to automate the process. Here is an example:

library(raster)
r = raster("path/r1.tif")
m = as.matrix(r)
psf = down_sample_image(m,
   factor = 4.66,
   gaussian_blur = T,
   gauss_sigma = 0.2 * 450) # 0.2 to 0.9 with step 0.05
e <- extent(r)
m2r = raster(psf)
extent(m2r) <- e
plot(m2r)
raster::crs(m2r) <- "EPSG:7767"
writeRaster(m2r, "path/r2.tif")

The output name of the raster is determined based on the function gauss_sigma. So in the end, I should get the following rasters: r2, r25, r3, r35, r4, r45......r9. All in all, I am looking for a repeated process that: reads the same raster and performs the above analysis 15 times (only the value of gauss_sigma is changing from 0.2 to 0.9 with step 0.05. The *450 should stay the same). At every iteration, the output should be saved with a name based on the gauss_sigma value.

Here is my attempt (based on the answer below):

library(terra)
library(OpenImageR)
        
setwd("C:/Users/Geography/Desktop/multipolygon_test")
    
    
fname <- file.path(tempdir(), "a.tif")
r <- rast(ext(583400, 594200, 1005800, 1025900), res = 100, crs = "EPSG:7767")
values(r) <- sample(46:550, ncell(r), replace = T)
writeRaster(r, fname, overwrite = T)

dsi <- function(m, f) {
  down_sample_image(m,
                    factor = 4.66,
                    gaussian_blur = T,
                    gauss_sigma = 0.20 * 450)
  return(m / f)
}

ff <- c(0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90)
outf <- paste0(gsub("\\.tif$", "", fname), "_", 1:15, ".tif")
nms <- paste0("f_", ff)
m <- as.matrix(r)

for (i in seq_along(ff)) {
  d <- dsi(m, ff[i])
  m2r <- setValues(r, d)
  writeRaster(m2r, outf[i], overwrite=TRUE, names=nms[i])
}
rast(outf)

Here is the output:

class       : SpatRaster 
dimensions  : 201, 108, 15  (nrow, ncol, nlyr)
resolution  : 100, 100  (x, y)
extent      : 583400, 594200, 1005800, 1025900  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / Maharashtra (EPSG:7767) 
sources     : a_1.tif  
              a_2.tif  
              a_3.tif  
              ... and 12 more source(s)
names       : f_0.2, f_0.25,     f_0.3,    f_0.35, f_0.4,    f_0.45, ... 
min values  :   230,    184,  153.3333,  131.4286,   115,  102.2222, ... 
max values  :  2750,   2200, 1833.3334, 1571.4286,  1375, 1222.2222, ...

The issue is I don't know how to export those raster files. I tried using the writeRaster function but I am getting this error (one of my many tries):

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘writeRaster’ for signature ‘"character", "character"’

Any ideas why that might be?

Here is a small raster:

r = raster(extent(583400, 594200, 1005800, 1025900), res=100, crs="EPSG:7767")
values(r) <- sample(46:550, ncell(r), replace=T)

Solution

  • Here is what I did, based on the answer above:

    library(gridkernel)
    library(gridprocess)
    library(raster)
    
    tirs = raster("path/tirs.tif")
    cr = raster("path/ntl.tif")
    
    for (i in seq(from = 0.2, to = 0.9, by = 0.05)) {
       g = as.grid(tirs)
       smoothed = gaussiansmooth(g, sd = i * 460, max.r = 100)
       r <- raster(smoothed)
       
       raster::crs(r) <- "EPSG:7755"
       
       print(i)
       
      r = resample(r, cr, method = "ngb")
      stringedi = gsub("0\\.", "", toString(format(i, nsmall = 2)))
      file_name <- paste0("tirs", stringedi, ".txt")
      print(file_name)
    
       # Make sure here you supply full path to folder without the last "/"
       full_file_path = paste0("path", "/", file_name)
       writeRaster(r, full_file_path, format = "GTiff", overwrite = T)
    }