Temporary File Management (scratch)¶
Module: utils_scratch
Overview¶
Provides utilities for managing temporary scratch files in SPEX, primarily for logging and system operations. The scratch module creates temporary log files that are automatically cleaned up when SPEX exits.
Key Features¶
Automatic temporary log file management
Session-based scratch file tracking
Automatic cleanup on SPEX exit
Integration with SPEX logging system
Error handling and reporting
API Reference¶
Subroutines¶
init
- scr%init(ier)
Initialize scratch file system
- Parameters:
ier – Error status (integer, output)
- Returns:
None
Sets up temporary scratch files for logging and system operations.
Status Codes: -
ier = 0: Success -ier ≠ 0: Failure (cannot create scratch files)
addlog
- scr%addlog(string)
Add message to scratch log file
- Parameters:
string – Message to add (character(*))
- Returns:
None
Writes a string to the scratch log file.
addsys
- scr%addsys(string)
Add message to scratch system file
- Parameters:
string – Message to add (character(*))
- Returns:
None
Writes a string to the scratch system file.
final
- scr%final()
Clean up scratch files
- Returns:
None
Closes and deletes all scratch files. This should only happen at successful exit of SPEX.
Usage Examples¶
Basic Scratch File Initialization
use utils_scratch
integer :: ier
! Initialize scratch file system
call scr%init(ier)
if (ier == 0) then
write(*,*) 'Scratch files initialized successfully'
else
write(*,*) 'Error: Cannot create scratch files'
endif
Adding Messages to Scratch Files
use utils_scratch
integer :: ier
! Initialize scratch system
call scr%init(ier)
if (ier == 0) then
! Add messages to log and system files
call scr%addlog('Starting data processing...')
call scr%addsys('System initialized')
! ... perform operations ...
call scr%addlog('Data processing completed')
call scr%addsys('System shutting down')
! Clean up scratch files
call scr%final()
endif
Error Handling with Scratch Files
use utils_scratch
use utils_message
integer :: ier
type(message) :: mesg
! Initialize with error handling
call scr%init(ier)
if (ier /= 0) then
call mesg%sper('Failed to initialize scratch files')
call mesg%sper('Check directory write permissions')
else
! Add log messages
call scr%addlog('Application started')
! ... perform operations ...
! Add system messages
call scr%addsys('Operation completed successfully')
! Clean up
call scr%final()
endif
Integration with SPEX Workflow
use utils_scratch
use utils_message
integer :: ier
type(message) :: mesg
! Initialize scratch files at start of SPEX session
call scr%init(ier)
if (ier == 0) then
call scr%addlog('SPEX session started')
call scr%addsys('System ready')
! ... SPEX operations ...
! Log important events
call scr%addlog('Data loaded successfully')
call scr%addsys('Memory allocated')
! ... more operations ...
! Final cleanup when SPEX exits
call scr%final()
else
call mesg%sper('SPEX cannot start: scratch file initialization failed')
endif
Notes¶
Scratch File Management
Scratch files are automatically created with unique names based on process ID
Files are stored in the current working directory with names like
spexsysXXXXXXXXXX.dumandspexlogXXXXXXXXXX.dumAll scratch files are automatically deleted on
scr%final()Scratch files are used primarily for logging and system operations
Error Handling
Check
ierafter initializationier = 0indicates successNon-zero values indicate errors (usually permission issues)
Use
mesg%sper()for error reporting
Performance Considerations
Scratch files use disk I/O for logging
Minimal performance impact for normal operations
Files are small and temporary
Automatic cleanup prevents disk space issues
Best Practices
Always initialize scratch system at SPEX startup
Always clean up with
scr%final()at SPEX exitUse for logging important events and system messages
Check initialization errors and handle gracefully
Use with message system for consistent error reporting
Common Use Cases
Use Case |
Description |
Example |
|---|---|---|
SPEX Session Logging |
Track SPEX operations and events |
Startup, data loading, processing steps |
System Monitoring |
Record system state and operations |
Memory allocation, file operations |
Error Diagnosis |
Capture debug information |
Problem analysis and troubleshooting |
Session Tracking |
Monitor SPEX workflow progression |
Step-by-step operation logging |
See Also¶
File Operations (files) - General file operations
Message System (message) - Error handling and reporting
System Interface (sys) - System operations