Use of lapply to execute a process and save the data set using specific name in R

I have two raster layers and I want to perform some analysis and save the result (a raster) based on the name of the input raster + some value. The value is determined by a part of a function (see code bellow).

For example if my input is called tirs, the output should be tirs2, if the number I used for sigma is 0.2. If I use sigma = 0.65, the output should be tirs65. As you can see, although the sigma is a decimal number, the output should not include the "0." part. (Note that I do sd = a number * 460, I do not want the * 460 to be included in the output name).

Lastly, the sigma value is from 0.2 to 0.9 with step 0.05.

The analysis I am using is the following:

library(gridkernel)
library(gridprocess)
library(raster)

tirs = raster("path/tirs.tif") # raster to be upscaled
ntl = raster("path/ntl.tif") # raster to be resampled with

g = as.grid(tirs)
smoothed = gaussiansmooth(g,  
sd = 0.2 * 460,  
max.r = 100)
r <- raster(smoothed)

resample(r, 
ntl, 
method = "ngb", 
filename = "path/nn/tirs2.tif", 
overwrite = T)

I have tried to do this:

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)
  resample(r, 
           ntl, 
           method = "ngb")
  file_name <- paste0("tirs", i, ".txt")
  full_file_path = paste0("path/nn",  "/", file_name)
  writeRaster(r, full_file_path, format = "GTiff")
}

but unsuccessfully. It does not smooth and resample the images.

The question is similar to this one, but whenever I am trying to to add the resample function, the output's raster pixel size is the same as my input (no smoothing and no resampling, just the name it changes).

Here are my raster layers:

tirs = raster(ncols=1818, nrows=372, xmn=32400, xmx=214200, ymn=262100, ymx=299300, crs='+proj=tmerc +lat_0=4 +lon_0=4.5 +k=0.99975 +x_0=230738.26 +y_0=0 +a=6378249.145 +rf=293.465 +units=m +no_defs')

ntl = raster(ncols=363, nrows=75, xmn=32618.9705113234, xmx=214118.970511323, ymn=261831.765752868, ymx=299331.765752868, crs='+proj=tmerc +lat_0=4 +lon_0=4.5 +k=0.99975 +x_0=230738.26 +y_0=0 +a=6378249.145 +rf=293.465 +units=m +no_defs')

Hi @nikos_geo,
Try this (untested):

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)
  s <- resample(r, ntl, method = "ngb")
  j <- i*100
  file_name <- paste0("tirs", j, ".txt")
  full_file_path = paste0("path/nn",  "/", file_name)
  writeRaster(s, full_file_path, format = "GTiff")
}

I am getting this error: Error in kernelsmoothc(x = x, k = kernel) : Not a matrix.

This is another problem which can't be solved with the given informations. Assuming that all inputs you have are indeed correct, assigned and can be used inside the functions you call, you could do this:

compute_and_save_raster <- function(sigma){
  g <- as.grid(tirs)
  smoothed <- gaussiansmooth(g, sd = sigma * 460, max.r = 100)
  r <- raster(smoothed)
  resample(r, 
           ntl, 
           method = "ngb")
  file_name <- paste0("tirs", 10 * sigma, ".txt")
  full_file_path = paste0("path/nn",  "/", file_name)
  writeRaster(r, full_file_path, format = "GTiff")
}

lapply(X = seq.default(0.2, 0.9, 0.05), compute_and_save_raster)

Kind regards

for (i in seq(from = 0.2, to = 0.9, by = 0.05)) {
   g = as.grid(tirs)
  # kern = makegaussiankernel(sd = 0.2 * 460, cellsize = 100, max.r = 100)
   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 I supply full path to folder without the last "/"
   full_file_path = paste0("path", "/", file_name)
   writeRaster(r, full_file_path, format = "GTiff", overwrite = T)
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.