I want to control where the temporary files generated by raster functions are stored. The reason is that I want to be able to remove temporary files that are specific to a process, without removing those used within other processes running in parallel. This was suggested by Luke Macaulay here.
The idea is that each process features commands that: set a process-specific tmpdir
, then run some raster function, store its process-specific output somewhere else than tmpDir()
, and finally remove the process-specific tmpDir()
.
But some raster operations keep storing their temp files in the "default" temp directory (looking like C:\Users\...\Temp\RtmpaevgEe
). Hence these temp files cannot get deleted at the end of each process and ultimately risk to fill up the hard drive.
This happens both regardless of being executed for a single process, in an iterative loop of processes or in a parallel setting.
My code to set the process-specific temporary directory is:
# Define which process we are in:
processname <- file.path(<raster_to_input_to_this_process.tif>)
# Create path to process-specific temp directory
process_tmp_dir <- file.path(paste0(processname,"_Tmp"))
# Create process-specific temp directory
dir.create(process_tmp_dir, showWarnings = FALSE)
# set temp directory
rasterOptions(tmpdir=process_tmp_dir)
rasterOptions()
or tmpDir()
indeed return process_tmp_dir
and not the default temp directory it was returning prior to the rasterOptions(tmpdir=process_tmp_dir)
command.
Then, if I run a mask
operation (Raster,Spatial-method), temp files are generated in process_tmp_dir
, as expected.
But if I run calc
, overlay
, or aggregate
my process_tmp_dir
remains empty while temp files appear in the default temp directory.
After that though, rasterOptions()
or tmpDir()
return process_tmp_dir
.
In every case I specified a filename
argument and
canProcessInMemory(processname, verbose = T)
memory stats in GB
mem available: 9.66
60% : 5.8
mem needed : 28.07
max allowed : 4.66 (if available)
[1] FALSE
I wonder why these functions do not "comply" to the new tmpdir setting while mask
does.
(Also, note that mask
generates .grd, much heavier, temp files than those, .tif, generated by calc
etc..)
I would highly appreciate any suggestions why this is so, and what could be done to make sure the temp files of any raster function are generated in the specified tmpDir()
.
I can make the data available and explicit the exact raster operations if you believe this is necessary to better understand what is happening.