Export Multiple Files as Months in Output Directory – Raster and GeoTIFF

geotiff-tiffmeanrrasterrename

I have various .tif files (3 files per month) from 2006-2010. Nomenclature of files are as following:

20060103
20060113
20060124
20060203
20060213
20060221
...
...
...
20100624

I have calculated monthly mean spatial plots of the files using the code given below: (with raster, rgdal and sp packages)

rast <- list.files(path="D:/Test",pattern='.*tif',full.names=TRUE)
s <- stack(rast)
grp <- rep(1:5, rep(3,5))
grp
ab_mean <- stackApply(s, grp, mean)
plot(ab_mean) 

I want to export the monthly mean spatial rasters to an output directory and at the same time, I want to rename the raster files in a way that the output file should take the month and year from the input files (such as 01/2006 or Jan 2006…) to avoid any confusion in case of missing data.

I know month.name function but it is not serving the purpose.

I am coding in R.

Best Answer

I've made a folder with 162 tiffs in it that should roughly replicate your data. Here's the first 10:

> rast <- list.files(path="./data/",pattern='.*tif',full.names=TRUE)
> rast[1:10]
 [1] "./data//20060103.tif" "./data//20060113.tif" "./data//20060121.tif"
 [4] "./data//20060203.tif" "./data//20060213.tif" "./data//20060221.tif"
 [7] "./data//20060303.tif" "./data//20060313.tif" "./data//20060321.tif"
[10] "./data//20060403.tif"

When I make the stack, I get the year-month-date from the filename in the names:

> s <- stack(rast)
> names(s)[1:10]
 [1] "X20060103" "X20060113" "X20060121" "X20060203" "X20060213" "X20060221"
 [7] "X20060303" "X20060313" "X20060321" "X20060403"

with an X to make it non-numeric. Now my grouping is going to be based on the year-month, which is the first 7 characters of the name (including the X, we'll keep that):

> grp = substr(names(s),1,7)
> grp[1:10]
 [1] "X200601" "X200601" "X200601" "X200602" "X200602" "X200602" "X200603"
 [8] "X200603" "X200603" "X200604"

Now apply means over the groupings in the stack. The resulting stack has useful names:

> ab_mean <- stackApply(s, grp, mean)
> names(ab_mean)[1:10]
 [1] "index_X200601" "index_X200602" "index_X200603" "index_X200604"
 [5] "index_X200605" "index_X200606" "index_X200607" "index_X200608"
 [9] "index_X200609" "index_X200610"

So we can loop over the names, extract the layer, and write it using the name:

> for(n in names(ab_mean)){writeRaster(ab_mean[[n]], paste0(n,".tif"))}
>

and that gives me 54 individual single-layer TIFF files output:

> list.files(path=".",pattern=".tif")
 [1] "index_X200601.tif" "index_X200602.tif" "index_X200603.tif"
 [4] "index_X200604.tif" "index_X200605.tif" "index_X200606.tif"
 [7] "index_X200607.tif" "index_X200608.tif" "index_X200609.tif"
[10] "index_X200610.tif" "index_X200611.tif" "index_X200612.tif"
[13] "index_X200701.tif" "index_X200702.tif" "index_X200703.tif"
[16] "index_X200704.tif" "index_X200705.tif" "index_X200706.tif"
[19] "index_X200707.tif" "index_X200708.tif" "index_X200709.tif"
[22] "index_X200710.tif" "index_X200711.tif" "index_X200712.tif"
[25] "index_X200801.tif" "index_X200802.tif" "index_X200803.tif"
[28] "index_X200804.tif" "index_X200805.tif" "index_X200806.tif"
[31] "index_X200807.tif" "index_X200808.tif" "index_X200809.tif"
[34] "index_X200810.tif" "index_X200811.tif" "index_X200812.tif"
[37] "index_X200901.tif" "index_X200902.tif" "index_X200903.tif"
[40] "index_X200904.tif" "index_X200905.tif" "index_X200906.tif"
[43] "index_X200907.tif" "index_X200908.tif" "index_X200909.tif"
[46] "index_X200910.tif" "index_X200911.tif" "index_X200912.tif"
[49] "index_X201001.tif" "index_X201002.tif" "index_X201003.tif"
[52] "index_X201004.tif" "index_X201005.tif" "index_X201006.tif"

Use whatever string processing you want to create according to whatever naming convention you want to use.

Also, try and use the terra package.