Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
; pass through structure of info parameters to write in FITS file
; 16-Aug-2022 - ECMD (Graz), pass out background data structure for plotting
; 16-Jun-2023 - ECMD (Graz), for a source location dependent response estimate, the location in HPC and the auxiliary ephemeris file must be provided.
; 04-May-2026 - (Copilot), check if spectrum and SRM output files already exist before writing; append '_N' counter to avoid silent overwrite.
;
;-
pro stx_convert_science_data2ospex, spectrogram = spectrogram, specpar = specpar, time_shift = time_shift, data_level = data_level, $
Expand Down Expand Up @@ -283,6 +284,22 @@ pro stx_convert_science_data2ospex, spectrogram = spectrogram, specpar = specpar
fits_info_params.specfile = (cur_spec_fn eq '') ? specfilename : cur_spec_fn
fits_info_params.srmfile = (cur_srm_fn eq '') ? srmfilename : cur_srm_fn

; Check if output files already exist and generate unique filenames to avoid overwriting
; only when FITS generation is enabled.
if keyword_set(generate_fits) or keyword_set(fits_info_params.generate_fits) then begin
new_specfile = stx_unique_filename(fits_info_params.specfile)
if new_specfile ne fits_info_params.specfile then begin
if ~keyword_set(silent) then print, 'Spectrum file ' + fits_info_params.specfile + ' already exists. Saving as: ' + new_specfile
fits_info_params.specfile = new_specfile
endif

new_srmfile = stx_unique_filename(fits_info_params.srmfile)
if new_srmfile ne fits_info_params.srmfile then begin
if ~keyword_set(silent) then print, 'SRM file ' + fits_info_params.srmfile + ' already exists. Saving as: ' + new_srmfile
fits_info_params.srmfile = new_srmfile
endif
endif

transmission = read_csv(loc_file( 'stix_transmission_highres_20251110.csv', path = getenv('STX_GRID')))
phe = transmission.(0)
; Select photon energies slightly beyond the nominal 500 keV range:
Expand Down
51 changes: 51 additions & 0 deletions stix/idl/util/stx_unique_filename.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;---------------------------------------------------------------------------
;+
; :project:
; STIX
;
; :name:
; stx_unique_filename
;
; :purpose:
; Return a unique FITS file path that does not yet exist on disk.
; If the requested path already exists an integer counter suffix
; is appended to the base name (before the .fits extension) and
; incremented until a free name is found, e.g.
; stx_spectrum_12345.fits -> stx_spectrum_12345_1.fits
; stx_spectrum_12345_1.fits -> stx_spectrum_12345_2.fits
;
; :category:
; helper methods
;
; :params:
; filename : in, required, type="string"
; Desired output FITS file path.
;
; :returns:
; String containing a file path that does not exist on disk. If
; filename itself does not exist it is returned unchanged.
;
; :calling sequence:
; IDL> safe_name = stx_unique_filename('stx_spectrum_12345.fits')
;
; :history:
; 04-May-2026 - (Copilot), initial release
;
;-
function stx_unique_filename, filename

if ~file_test(filename) then return, filename

ext = '.fits'
base = file_dirname(filename, /mark_directory) + file_basename(filename, ext)

counter = 1
candidate = base + '_' + strtrim(counter, 2) + ext
while file_test(candidate) do begin
counter = counter + 1
candidate = base + '_' + strtrim(counter, 2) + ext
endwhile

return, candidate

end