Function CH1102_SPEC_FAST,temp,wave_edges,wstep=wstep,photon=photon, $
        erg=erg, ab_ion=ab_ion, solsp_file=solsp_file, quiet=quiet
;+
;  NAME:
;    ch1102_spec_fast (this version reads solar spectrum .genx file)
;  PURPOSE:
;    Compute a Chianti isothermal spectrum (line + continuum) in the 
;        specified wavelength bins by reading solar spectral table
;        calculated with Chianti ver.10.1., using the elemental
;        abundance set.
;    Chianti 2021 + adv ioneq(@3-oct-2025) : ab_ion=1 (default)
;    Asplund 2021 + Chianti ioneq : ab_ion=5  ; N/A
;    
;    Calculation is made for EM=1.e44 cm^-3.
;
;  CALLING SEQUENCE:
;    Flux = ch1102_spec_fast(temp,wave_edges)		; ph s-1 
;    Flux = ch1102_spec_fast(5.0e6,[1,30],/erg)       	; erg s-1
;
;  INPUTS:
;    Temp  :  Temperature (in K, may be a vector).
;    Wave_edges  : Vector of wavelengths (in Ang).    
;                  Wave_edges value, [1,8,20,30], yields the 3-element Flux,
;                    Flux(*,0) : Fluxes for  1 -  8 A wavelength range.
;                    Flux(*,1) : Fluxes for  8 - 20 A wavelength range.
;                    Flux(*,2) : Fluxes for 20 - 30 A wavelength range.
;
;  OUTPUTS:
;    Flux  : Fluxes in ph s-1 or erg s-1 (total flux with each bin)
;	     Fluxes = fltarr(N_elements(Te6),N_elements(wave))
;
;  OPTIONAL INPUT KEYWORDS:
;    Wstep :  wavelength step size of spectrum calculation (in Ang),
;               (a bin size used in the ISOTHERMAL.PRO, default=0.05)
;    Photon : If set, calculation is made in ph s-1 (default)
;    Erg    : If set, calculation is made in erg s-1
;
;  OPTIONAL INPUT KEYWORDS:
;    Ab_ion : 1 (default) : solspec_ch1102_cr2021chianti_adv_xrtirrad.genx 
;                             is read. This file is based on 
;                             sun_coronal_2021_chianti.abund &
;                             ch_adv_3-Oct-2025-14:07:28.ioneq
;           : 2          : solspec_ch1000_hybrid_chianti_xrtirrad.genx is read.
;                          sun_coronal_2012_schmelz_ext.abund & chianti.ioneq
;           : 3          : solspec_ch1000_photos_chianti_xrtirrad.genx is read.
;                          sun_photospheric_2015_scott.abund & chianti.ioneq
;           : 4          : solspec_ch1102_photos_chianti_xrtirrad.genx is read.
;                          sun_photospheric_2015_scott.abund & chianti.ioneq
;;(previous setting)
;;    Ab_ion : 1          : solspec_ch800_corona_chianti_irrad.genx  is read.
;;                          sun_coronal_1992_feldman_ext.abund & chianti.ioneq
;;           : 2 (default): solspec_ch800_hybrid_chianti_irrad.genx  is read.
;;                          sun_coronal_2012_schmelz_ext.abund & chianti.ioneq
;;           : 3          : solspec_ch800_photos_chianti_irrad.genx  is read.
;;                          sun_photospheric_2011_caffau.abund & chianti.ioneq
;;(previous setting)
;;    Ab_ion : 1          : sun_coronal_ext.abund & chianti.ioneq are assumed.
;;           : 2 (default): sun_hybrid_ext.abund  & chianti.ioneq are assumed.
;;           : 3          : sun_photospheric_grevesse07.abund & chianti.ioneq.
;    Solsp_file: file name of the solar spectrum file to be read.
;
;  METHOD:
;    Instead of calculate spectrum with isothermal.pro, read solar isothermal
;       spectrum file, and return the flux a given temperature(s) and
;       included specified wavelength range for EM=1.e44 cm^-3.
;
;  MODIFICATION HISTORY:
;     1-Jul-2010, Aki Takeda : Modified version of mewe_spec and
;                                mewe_spec_lwa.pro  
;     3-Apr-2011, Aki T.: Default sre file, corona --> hybrid. 
;    28-Jan-2018, Aki T.: Change abundance keyword binding. 
;       /hybrid : sun_coronal_2012_schmelz_ext.abund (from Fludra 1999)    
;       /photos : sun_photospheric_2011_caffau.abund (from Grevesse 2007)    
;       /corona : sun_coronal_1992_feldman_ext.abund (change only file name)
;    19-Jul-2018, Aki T.: Modify to read a solar spectrum file instead of
;                 calculating it with isothermal.pro, then interpolate
;                 spectra regarding input temperatures.
;     5-Feb-2023, Aki T.: Update the spectrum file from chianti 8.0 version
;                 to the 10.0 version.
;                 Unified with CH1000_SPEC_FAST4XRT.PRO as there is no
;                 difference in using with SXT or XRT data.
;    20-Aug-2023, Aki T.: Update the spectrum file to chianti 10.1 version.
;    15-Sep-2024, Aki T.: fixed a bug in selecting wavelength range to 
;                 integrate (was NG for the case of multiple bins).
;    25-May-2026, Aki T.: Update the spectrum file to chianti 11.2 version.
;
; ab_ion=1 (default): solspec_ch1102_cr2021chianti_adv_xrtirrad.genx
;-
;****************************************************************
; With no calling arguments, display the calling sequence:
;****************************************************************

; temp=5.0e6 & wave_edges=[1,30] & erg=1  ; for testing

if n_params() eq 0 then begin
  return,'   Flux= chianti_spec(Temp,wave_edges[,/erg]) ; for EM=1.e44 cm-3'
endif

if keyword_set(photon) and keyword_set(erg) then begin
  print,' **** Error in chianti_spec ****',string(7b),string(7b)
  print,'      You cannot specify both /photon and /erg'
  return,-1
endif else if keyword_set(erg) then Units=1 else Units=0

if n_elements(wave_edges) eq 0 then begin
  print,' **** Error in chianti_spec ****',string(7b),string(7b)
  print,'      Wave_edges or Temp is undefined'
  help,Temp,Wave_edges
  return,-1
endif

if keyword_set(quiet) then q_quiet=1 else q_quiet=0

if not keyword_set(Wstep) then begin
  Wstep=0.05
  if q_quiet eq 0 then print,' *** Wstep set to 0.05 (A). ***'
endif

; ************************************************************
;   Set up the solar spectrum file to be read. 
; ************************************************************
;---------------------------------
if keyword_set(ab_ion) then begin
;---------------------------------
case ab_ion of
1 : begin  ;  (corona chianti_2021 + chianti adv@3-oct-2025) 
      solsp_file='solspec_ch1102_cr2021chianti_adv_xrtirrad'
    end
;5 : begin       ;  (photospheric + chianti)
;      solsp_file='solspec_ch1010_ph2021asplund_chianti_xrtirrad'
;    end
;1 : begin       ;  (corona + chianti)
;      solsp_file='solspec_ch1000_corona_chianti_xrtirrad'
;      solsp_file='solspec_ch800_corona_chianti'
;      solsp_file='solspec_ch800_corona_chianti_irrad'
;      ab_file='sun_coronal_1992_feldman_ext.abund'
;      ion_file='chianti.ioneq'
;    end
;2 : begin       ;  (hybrid + chianti : Default)
;      solsp_file='solspec_ch1000_hybrid_chianti_xrtirrad'
;      solsp_file='solspec_ch800_hybrid_chianti'
;      solsp_file='solspec_ch800_hybrid_chianti_irrad'
;      ab_file='sun_coronal_2012_schmelz_ext.abund'
;      ion_file='chianti.ioneq'
;    end
;3 : begin       ;  (photospheric + chianti)
;      solsp_file='solspec_ch1000_photos_chianti_xrtirrad'
;      solsp_file='solspec_ch800_photos_chianti'
;      solsp_file='solspec_ch800_photos_chianti_irrad'
;      ab_file='sun_photospheric_2011_caffau.abund'
;      ion_file='chianti.ioneq'
;    end
else : begin    ; (invalid number --> default)
        solsp_file='solspec_ch1102_cr2021chianti_adv_xrtirrad'
        print, $
   "*** 'solspec_ch1102_cr2021chianti_adv_xrtirrad.genx' is assumed."
       end
endcase
;
;------------------
endif else begin
;------------------
; set default
        solsp_file='solspec_ch1102_cr2021chianti_adv_xrtirrad'
        print, $
   "*** 'solspec_ch1102_cr2021chianti_adv_xrtirrad.genx' is assumed."
;
;------------------
endelse
;------------------
;
;***************************************************** 
; Compute the line + cont flux (for EM=1.e44 cm-3):
;***************************************************** 
nn_temp=n_elements(temp)
wmin = min(wave_edges) & wmax = max(wave_edges)
n_wave=fix((wmax-wmin)/wstep)+1
wave=findgen(n_wave)*wstep+wmin
em=replicate(1.d44,nn_temp)

;isothermal,wmin,wmax,wstep,temp,edensity=1.e9,erg=erg,wave,spec, $
;  em=em, abund_name=abund_name, ioneq_name=ioneq_name, /cont
;tot_flux=spec*4*!pi

;***** read spectral table *****
dir_spec='/disk/yla/software/yla_ssw/recal_resp/'
restgen,file=dir_spec+solsp_file, total_flux0,logte0,wave0
te0=10^logte0
if q_quiet eq 0 then print,solsp_file
if q_quiet eq 0 then help,total_flux0,logte0,wave0
;
;***** find the flux for the closest Te to the input *****
nn_wave=n_elements(wave0)
total_flux=dblarr(nn_temp,nn_wave)      ; prepare a box
;
if wmax gt wave0(nn_wave-1) then begin
  print,'!!! wave_edge goes higher than that of solspec file. !!!'
  print,wmax,'   >',wave0(nn_wave-1)
  flux=0
  goto, FIN
endif
if wmin lt wave0(0) then begin $
  print,'!!! wave_edge goes lower than that of solspec file. !!!'
  print,wmin,'   <',wave0(0)
endif
;
ss_nearest=intarr(nn_temp)
for i=0,nn_temp-1 do begin
  if temp(i) eq 1. then ss_nearest(i)=-1 $ 
  else begin
    chk=(te0-temp(i))^2
    work=min(chk,ss_min)
    ss_nearest(i)=ss_min
  endelse
endfor
;
for i=0,nn_temp-1 do begin
   if ss_nearest(i) ne -1 then $
      total_flux(i,*)=total_flux0(ss_nearest(i),*) $
   else total_flux(i,*)=0
endfor
if q_quiet eq 0 then help,total_flux
;
;*************************************************
; Rebin into the user-supplied wavelength bins.
;*************************************************
nbin=n_elements(wave_edges)-1
flux=dblarr(nbin,nn_temp)
;
for i=0,nbin-1 do begin
;  ss=where(wave ge wave_edges(i) and wave le wave_edges(i+1))
;  wave le wave_edges(i+1) does not work because wave - wave_edges(i+1)
;      is not exactly zero (~1e-7).  
  ss=where(wave0 - wave_edges(i) ge -0.01 and $ 
           wave0 - wave_edges(i+1) le 0.01)
  flux(i,*)=total(total_flux(*,ss),2)
endfor
;
FIN:
return,reform(flux)
;
end
