Skip to content

Avoid silent overwrite of existing spectrum/SRM FITS files#249

Open
Copilot wants to merge 5 commits into
masterfrom
copilot/check-spectrum-files-existence
Open

Avoid silent overwrite of existing spectrum/SRM FITS files#249
Copilot wants to merge 5 commits into
masterfrom
copilot/check-spectrum-files-existence

Conversation

Copy link
Copy Markdown

Copilot AI commented May 4, 2026

Repeated processing of the same observation produces identical default filenames (stx_spectrum_<uid>.fits / stx_srm_<uid>.fits), silently clobbering any previously written files.

Changes

  • New stx_unique_filename.pro — helper function that returns the input path unchanged if free, or finds the next available name by appending _1, _2, … before the extension. Extension-agnostic (uses last . in the path).

  • stx_convert_science_data2ospex.pro — after the output paths are finalised in fits_info_params, both are resolved through stx_unique_filename. If a name changes, the updated path is written back to fits_info_params (so the FITS writer and OSPEX object stay in sync) and a message is printed unless silent is set.

Example behaviour

stx_spectrum_12345.fits        → already exists
stx_spectrum_12345_1.fits      → already exists
stx_spectrum_12345_2.fits      → free  ✓

; Console output:
; Spectrum file stx_spectrum_12345.fits already exists. Saving as: stx_spectrum_12345_2.fits
; SRM file stx_srm_12345.fits already exists. Saving as: stx_srm_12345_2.fits

…void 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>
Copilot AI changed the title [WIP] Check if spectrum files exist before writing Avoid silent overwrite of existing spectrum/SRM FITS files May 4, 2026
Copilot AI requested a review from ennosigaeus May 4, 2026 11:48
Copy link
Copy Markdown
Member

@ennosigaeus ennosigaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be using the IDL routine "file_basename" to check for the name of the file and extension and not do that much manual work in stx_unique_filename. I would assume the entire routine become much shorter, so you may be able to integrate it with the main routine.

… 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>
Copy link
Copy Markdown
Author

Copilot AI commented May 4, 2026

You should be using the IDL routine "file_basename" to check for the name of the file and extension and not do that much manual work in stx_unique_filename. I would assume the entire routine become mu...

Refactored in 82e1881. stx_unique_filename now uses file_basename(filename, '.fits') and file_dirname to split path/base/extension, removing all the manual strpos/strmid work. The active code body is down to ~10 lines.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents repeated processing runs from silently overwriting previously written spectrum/SRM FITS outputs by introducing a helper that selects the next available filename and applying it during OSPEX conversion.

Changes:

  • Added stx_unique_filename.pro to find an available output filename by appending an incrementing _<N> suffix before the extension.
  • Updated stx_convert_science_data2ospex.pro to run the finalized spectrum/SRM output paths through stx_unique_filename, updating fits_info_params and optionally printing a notice (unless silent is set).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
stix/idl/processing/spectrogram/stx_unique_filename.pro New helper function to pick a non-existing output filename by applying a numeric suffix.
stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro Uses stx_unique_filename to avoid clobbering existing spectrum/SRM outputs and keeps fits_info_params in sync.
Comments suppressed due to low confidence (1)

stix/idl/processing/spectrogram/stx_unique_filename.pro:56

  • stx_unique_filename always appends a new _<counter> suffix to the full base name. If the input already ends with an existing numeric suffix (e.g. foo_1.fits) and that file exists, this will generate foo_1_1.fits rather than incrementing to foo_2.fits as described in the header examples. Consider detecting a trailing _<int> in the base name and incrementing from that value (or stripping it before applying the counter).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread stix/idl/processing/spectrogram/stx_convert_science_data2ospex.pro Outdated
@ennosigaeus ennosigaeus marked this pull request as ready for review May 4, 2026 12:11
Copy link
Copy Markdown
Member

@ennosigaeus ennosigaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach, but the stx_unique_filename is in the wrong place. Maybe better in a more generic folder, like https://github.com/i4Ds/STIX-GSW/tree/master/stix/idl/util

Copy link
Copy Markdown
Author

Copilot AI commented May 4, 2026

I like the approach, but the stx_unique_filename is in the wrong place. Maybe better in a more generic folder, like https://github.com/i4Ds/STIX-GSW/tree/master/stix/idl/util

Moved to stix/idl/util/stx_unique_filename.pro in 6062f82.

Copilot AI requested a review from ennosigaeus May 4, 2026 12:16
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@ennosigaeus ennosigaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, already gave feedback to copilot.

@ennosigaeus ennosigaeus requested a review from paolomassa May 4, 2026 12:19
@ennosigaeus
Copy link
Copy Markdown
Member

@paolomassa can you have a quick look and spin in IDL to see if this is fine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check if spectrum files exist before writing

3 participants