Home > Back-end >  How to create a loop to read raster layer for each month of one year R?
How to create a loop to read raster layer for each month of one year R?

Time:04-16

I want to create a loop for each month of 2021 in order to read different raster layer over one year

month <- list(01,02,03,04,05,06,07,08,09,10,11,12)
for (i in month){
   month <- month
   tile <- "T39LTF"
   name_im <- "AMBARO_T39LTF" 
   S2_B4[[i]] = raster(glue("D:/Tuiles_José/2021/{month}/{name_im}/images/SENTINEL2X_2021{month}01-000000-000_L3A_{tile}_C_V2-2/SENTINEL2X_2021{month}01-000000-000_L3A_{tile}_C_V2-2_FRC_B4.tif",values=T)
   crs(S2_B4[[i]])=crs(" init=epsg:32739")
}

The problem is that I am also using my variable 'month' to my filepath. To make things clear, R have to pick for each month the right file ex: if I only do it on april for my actual tile I would have :

S2_B4 = raster(glue("D:/Tuiles_José/2021/**04**/**AMBARO_T39LTF**/images/SENTINEL2X_2021**04**01-000000-000_L3A_**T39LTF**_C_V2-2/SENTINEL2X_2021**04**01-000000-000_L3A_**T39LTF**_C_V2-2_FRC_B4.tif",values=T))

I thought that maybe the 'raster' function was the problem...

CodePudding user response:

Welcome to Stack Overflow, Sarah!

A couple of quick points:

  1. In the raster:: package, the raster() function can only read in one layer at a time. If you want to read all rasters to a single object from different file paths, use the stack() function.

  2. The terra:: package has now largely replaced the raster:: package. In terra:: you no longer have to worry about whether you are reading in a multiband raster, a single raster, or many rasters from different files, they can all be read in with the rast() function.

  3. In your loop, one issue that is likely causing problems is that you didn't include your index value when you try to write month. In other word as you have it now, each iteration of the loop is writing the entire month vector. If you rewrite that line of your loop as month <- month[i] that should solve the problem.

  4. At least from your example code, you never initiate the S2_B4 as a list. This is necessary to ensure you are saving the iteration of the loop to the correct spot.

I think perhaps the best solution would be to use the loop first to create the filenames, then save each file path name to a list. You can then use the lapply() function to read in the rasters as save that as S2_B4.

month <- list(01,02,03,04,05,06,07,08,09,10,11,12)

fpaths<-list()
for (i in 1Llength(month)){
month <- month[i]
tile <- "T39LTF"
name_im <- "AMBARO_T39LTF"

fpaths[[i]]<-paste("D:/Tuiles_José/2021/", month,"/", name_im, "/images/SENTINEL2X_2021", month, "01-000000-000_L3A_", tile, "_C_V2-2/SENTINEL2X_2021",month,"01-000000-000_L3A_",tile,"_C_V2-2_FRC_B4.tif",sep="")
}

S2_B4<-terra::rast(fpath)# Using the terra:: package
S2_B4<-raster::stack(fpath)# Using the raster package

CodePudding user response:

Your question is really about the basics of writing a loop, and not directly related to raster data. It is important to set up a loop step by step so that you can see what is and what is not working.

First, month should be characters if you want to keep the leading zeros (and use a vector rather then a list)

month <- c("01","02","03","04","05","06","07","08","09","10","11","12")

or like this

month <- formatC(1:12, width=2, flag="0")
month
#[1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12"

Also, do not put things in the loop that are constant such as tile and name_im.

tile <- "T39LTF"
name_im <- "AMBARO_T39LTF" 
months <- formatC(1:12, width=2, flag="0")

You cannot do for i in month because in your example you want i to be a numeric iterator (you use it to index a list). So instead

for (i in 1:length(months)) {
   # months is the vector, m is a single month
   m <- months[i]

   # first create the filenames and check if they are correct
   # I am using base-R but you can use the glue package instead
   filename <- paste0("D:/Tuiles_José/2021/", m, "/", name_im, "/images/SENTINEL2X_2021", m, "01-000000-000_L3A_", tile, "_C_V2-2/SENTINEL2X_2021", m, "01-000000-000_L3A_", tile, "_C_V2-2_FRC_B4.tif")
   print(filename)

   ## when the above works, add more, step by step
   ## after trying this for a single file

   #r <- raster(filename)
   #crs(r) = " init=epsg:32739"
}

#[1] "D:/Tuiles_José/2021/01/AMBARO_T39LTF/images/SENTINEL2X_20210101-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210101-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/02/AMBARO_T39LTF/images/SENTINEL2X_20210201-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210201-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/03/AMBARO_T39LTF/images/SENTINEL2X_20210301-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210301-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/04/AMBARO_T39LTF/images/SENTINEL2X_20210401-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210401-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/05/AMBARO_T39LTF/images/SENTINEL2X_20210501-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210501-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/06/AMBARO_T39LTF/images/SENTINEL2X_20210601-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210601-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/07/AMBARO_T39LTF/images/SENTINEL2X_20210701-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210701-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/08/AMBARO_T39LTF/images/SENTINEL2X_20210801-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210801-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/09/AMBARO_T39LTF/images/SENTINEL2X_20210901-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20210901-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/10/AMBARO_T39LTF/images/SENTINEL2X_20211001-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20211001-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/11/AMBARO_T39LTF/images/SENTINEL2X_20211101-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20211101-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
#[1] "D:/Tuiles_José/2021/12/AMBARO_T39LTF/images/SENTINEL2X_20211201-000000-000_L3A_T39LTF_C_V2-2/SENTINEL2X_20211201-000000-000_L3A_T39LTF_C_V2-2_FRC_B4.tif"
 

But not that because R is "vectorized", you can also get these file names like this, without a loop

tile <- "T39LTF"
name_im <- "AMBARO_T39LTF" 
months <- formatC(1:12, width=2, flag="0")
filenames <- paste0("D:/Tuiles_José/2021/", months, "/", name_im, "/images/SENTINEL2X_2021", months, "01-000000-000_L3A_", tile, "_C_V2-2/SENTINEL2X_2021", months, "01-000000-000_L3A_", tile, "_C_V2-2_FRC_B4.tif")

And then perhaps do

s <- stack(filenames)
  • Related