@@ -10,6 +10,7 @@ module nc4fortran
1010 nf90_def_dim, nf90_put_att, nf90_def_var, nf90_get_var, nf90_put_var, nf90_float, nf90_double, nf90_int, nf90_int64, &
1111 nf90_inq_libvers
1212
13+ use pathlib, only : unlink, get_tempdir, is_absolute_path
1314use string_utils, only : toLower, strip_trailing_null, truncate_string_null
1415
1516implicit none (type, external )
@@ -28,6 +29,8 @@ module nc4fortran
2829integer :: comp_lvl = 0 ! < compression level (1-9) 0: disable compression
2930logical :: verbose = .false.
3031logical :: debug = .false.
32+ logical :: is_scratch = .false.
33+ ! ! will be auto-deleted on close
3134character (80 ) :: libversion
3235
3336contains
@@ -251,6 +254,10 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
251254 end select
252255case (' new' ,' replace' )
253256 ier = nf90_create(self% filename, ior (NF90_CLOBBER, NF90_NETCDF4), self% ncid)
257+ case (' scratch' )
258+ ier = nf90_create(self% filename, ior (NF90_CLOBBER, NF90_NETCDF4), self% ncid)
259+ self% is_scratch = .true.
260+ if (.not. is_absolute_path(filename)) self% filename = get_tempdir() // ' /' // filename
254261case default
255262 write (stderr,* ) ' Unsupported status -> ' // lstatus
256263 error stop 128
@@ -279,6 +286,11 @@ subroutine nc_finalize(self, ierr)
279286 if (present (ierr)) return
280287 error stop
281288endif
289+
290+ if (self% is_scratch) then
291+ if (unlink(self% filename)) write (stderr,* ) ' WARNING: could not delete scratch file: ' // self% filename
292+ endif
293+
282294end subroutine nc_finalize
283295
284296
0 commit comments