sunpy / sunkit-spex

:construction: Under Development :construction: A package for solar X-ray spectroscopy
BSD 3-Clause "New" or "Revised" License
22 stars 26 forks source link

RHESSI fits sum_flag=0 breaks code #132

Open ianan opened 9 months ago

ianan commented 9 months ago

Describe the bug

At the moment the code returns an error and breaks when trying to load a RHESSI srm/spec fits file with sum_flag != 1 in the header. I guess @KriSun95 did this is to avoid issues of multiple detectors that aren't summed, i.e. expecting always to have a single srm and single spectrogram in each fits file. But even if there is a single srm/spec in there (say only using a single RHESSI detector) will get the error/break if sum_flag=0 in the fits header.

Short term solution is just to make sure sum_flag=1 when generating RHESSI srm/spec fits from the hsi_spectrum() in sswidl, whether summed over multiple detectors or "summed" over a single detector.

Longer term might want a smarter way to deal with this? Maybe @settwi might have some ideas given your work on loading in RHESSI spec/srm.

To Reproduce

Load in a RHESSI spec/srm fits file where have a single detector spectrogram/srm but sum_flag=0 in the header, i.e. https://github.com/ianan/rhessi_spectra/blob/main/mfstats9_fits/20030725_082642_srm_org_d1.fits https://github.com/ianan/rhessi_spectra/blob/main/mfstats9_fits/20030725_082642_spec_org_d1.fits

from sunkit_spex.sunxspex_fitting.fitter import Fitter spec = Fitter(pha_file="20030725_082642_spec_org_d1.fits", srm_file="20030725_082642_srm_org_d1")

Gives error/breaks in sunxspex_fitting/instruments.py:662 with warning: "Apparently spectrum file's SUMFLAG should be one and I don't know what to do otherwise at the moment."

Screenshots

No response

System Details

No response

Installation method

No response

settwi commented 9 months ago

I believe OSPEX only allows spectroscopy to be performed when SUMFLAG=1 (see here):

sum_flag = fxpar (hdr, 'SUMFLAG', count=count)
if count gt 0 and sum_flag eq 0 then begin
  if n_elements(str2arr(detused, ' ')) gt 1 then begin   ; check for > 1 det
      err_code = 1
      err_msg = 'OSPEX can not handle SRM files with >1 detectors that are not summed (sum_flag=0). Aborting'
      return
    endif
endif

Summing counts from the different RHESSI detectors is kind of fraught so that makes sense. I guess we could just add a special case if there is only one detector. OSPEX does that a few lines above.

-WS

KriSun95 commented 9 months ago

The spex_hessi_fits2drm.pro file that @settwi has linked is likely why my error very helpfully reads: "Apparently spectrum file's SUMFLAG should be one and I don't know what to do otherwise at the moment.".

Looking at it again, would this not most likely be because OSPEX can't do simultaneous fitting? I.e., it would have no idea what to do if the detectors weren't summed into a single spectrum with only one SRM. Obviously, if this is the only reason why SUMFLAG must be 1, then there would be no issue on our side by supporting SRM files with >1 detectors that are not summed.

settwi commented 9 months ago

Good idea!