From af0435e460d47b9efc346c10f6682ccb2a943ce7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 4 May 2026 11:43:29 +0000 Subject: [PATCH 1/5] Initial plan From af2da2be5fe1cf30d73904b7699094ee5fe0ffde Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 4 May 2026 11:47:43 +0000 Subject: [PATCH 2/5] Check if spectrum/SRM files exist before writing; append counter to avoid silent overwrite Agent-Logs-Url: https://github.com/i4Ds/STIX-GSW/sessions/678aa8dc-f761-4c9a-9f08-e898b2c36990 Co-authored-by: ennosigaeus <649330+ennosigaeus@users.noreply.github.com> --- .../stx_convert_science_data2ospex.pro | 14 +++++ .../spectrogram/stx_unique_filename.pro | 61 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 stix/idl/processing/spectrogram/stx_unique_filename.pro diff --git a/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro b/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro index b844d411..cb48c19f 100644 --- a/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro +++ b/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro @@ -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, $ @@ -283,6 +284,19 @@ 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 + 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 + 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: diff --git a/stix/idl/processing/spectrogram/stx_unique_filename.pro b/stix/idl/processing/spectrogram/stx_unique_filename.pro new file mode 100644 index 00000000..90385cb2 --- /dev/null +++ b/stix/idl/processing/spectrogram/stx_unique_filename.pro @@ -0,0 +1,61 @@ +;--------------------------------------------------------------------------- +;+ +; :project: +; STIX +; +; :name: +; stx_unique_filename +; +; :purpose: +; Return a unique 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 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 file path (must end in a recognised extension +; such as '.fits'). +; +; :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 + + ; Return immediately if the file does not yet exist + if ~file_test(filename) then return, filename + + ; Identify the extension boundary (last '.' in the name) + dot_pos = strpos(filename, '.', /reverse_search) + if dot_pos lt 0 then begin + ; No extension found – append counter directly to the end + base = filename + ext = '' + endif else begin + base = strmid(filename, 0, dot_pos) + ext = strmid(filename, dot_pos) + endelse + + 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 From 82e1881b4bd62e1bf5cd414403d2f796e1d09c31 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 4 May 2026 12:05:23 +0000 Subject: [PATCH 3/5] Simplify stx_unique_filename using file_basename/file_dirname instead of manual strpos Agent-Logs-Url: https://github.com/i4Ds/STIX-GSW/sessions/aa2ca532-d099-4c14-84c6-13c7ed4ce926 Co-authored-by: ennosigaeus <649330+ennosigaeus@users.noreply.github.com> --- .../spectrogram/stx_unique_filename.pro | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/stix/idl/processing/spectrogram/stx_unique_filename.pro b/stix/idl/processing/spectrogram/stx_unique_filename.pro index 90385cb2..ea0b5006 100644 --- a/stix/idl/processing/spectrogram/stx_unique_filename.pro +++ b/stix/idl/processing/spectrogram/stx_unique_filename.pro @@ -7,20 +7,19 @@ ; stx_unique_filename ; ; :purpose: -; Return a unique file path that does not yet exist on disk. +; 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 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 +; 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 file path (must end in a recognised extension -; such as '.fits'). +; Desired output FITS file path. ; ; :returns: ; String containing a file path that does not exist on disk. If @@ -35,19 +34,10 @@ ;- function stx_unique_filename, filename - ; Return immediately if the file does not yet exist if ~file_test(filename) then return, filename - ; Identify the extension boundary (last '.' in the name) - dot_pos = strpos(filename, '.', /reverse_search) - if dot_pos lt 0 then begin - ; No extension found – append counter directly to the end - base = filename - ext = '' - endif else begin - base = strmid(filename, 0, dot_pos) - ext = strmid(filename, dot_pos) - endelse + ext = '.fits' + base = file_dirname(filename, /mark_directory) + file_basename(filename, ext) counter = 1 candidate = base + '_' + strtrim(counter, 2) + ext From 6062f822a0badeba9fa318292e5af788170e1270 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 4 May 2026 12:15:51 +0000 Subject: [PATCH 4/5] Move stx_unique_filename.pro to stix/idl/util (more generic location) Agent-Logs-Url: https://github.com/i4Ds/STIX-GSW/sessions/2fd4495a-54f7-40c6-9e21-56fca5e5f97b Co-authored-by: ennosigaeus <649330+ennosigaeus@users.noreply.github.com> --- stix/idl/{processing/spectrogram => util}/stx_unique_filename.pro | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename stix/idl/{processing/spectrogram => util}/stx_unique_filename.pro (100%) diff --git a/stix/idl/processing/spectrogram/stx_unique_filename.pro b/stix/idl/util/stx_unique_filename.pro similarity index 100% rename from stix/idl/processing/spectrogram/stx_unique_filename.pro rename to stix/idl/util/stx_unique_filename.pro From ebdb04af36e1d9d2d281beee8efeb7064fdb5229 Mon Sep 17 00:00:00 2001 From: "Laszlo I. Etesi" Date: Mon, 4 May 2026 14:18:35 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../stx_convert_science_data2ospex.pro | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro b/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro index cb48c19f..577fd32f 100644 --- a/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro +++ b/stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro @@ -285,16 +285,19 @@ pro stx_convert_science_data2ospex, spectrogram = spectrogram, specpar = specpar 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 - 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 + ; 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 + 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')))