;-----------------------------
fname='sig_xrt_amap_20230219'
;-----------------------------
;--- read XRT qsbp info structure ---
in_dir='/disk/yla/takeda/REU_2026/qsbp_sig/'
restgen,file=in_dir+fname,sig_xrt
yyyymmdd=strmid(fname,13,8)
sig_fov=sig_xrt.sig_fov
sig_qs=sig_xrt.sig_qs
sig_bps=sig_xrt.sig_bps
nn_bp=n_elements(sig_bps(0,*))
;
;---------- read XRT data to get index ---------
file_am=sig_xrt.file_am
file_ap=sig_xrt.file_ap
mreadfits,file_am,index_am,/nodata
mreadfits,file_ap,index_ap,/nodata
;
;***** prepare an array of open fraction *****
sl_ph=check_sl_phase(index_am)
ofrac=get_pf_openfrac(index_am)
fctm_am=get_fctm_fact9_01(index_am)
fctm_ap=get_fctm_fact9_10(index_ap)
;
;***** apply XRT_TEEM4SYN  with specified solar spectrum *****
iiam_dmy=index_am
iiam_dmy.exptime=1.0
iiap_dmy=index_ap
iiap_dmy.exptime=1.0
ddam_dmy=fltarr(2,2+nn_bp)
ddap_dmy=fltarr(2,2+nn_bp)
;
;--- whole FOV and QS ---
ddam_dmy(*,0)=sig_fov(0)
ddap_dmy(*,0)=sig_fov(1)
bps_am=sig_bps(0,*)
bps_ap=sig_bps(1,*)
ddam_dmy(*,1)=sig_qs(0)
ddap_dmy(*,1)=sig_qs(1)
;--- BPs ---
for i=0,nn_bp-1 do begin
  ddam_dmy(*,2+i)=bps_am(i)
  ddap_dmy(*,2+i)=bps_ap(i)
endfor
;
xrt_teem4syn_ch1102_slmd_fctm, $
  iiap_dmy,ddap_dmy,iiam_dmy,ddam_dmy, te_dmy,em_dmy, te_unc,em_unc,$
  /corona, frac_open=ofrac, f1ctm=fctm_ap,f2ctm=fctm_am
te_fov=te_dmy(0,0) & em_fov=em_dmy(0,0)
te_qs=te_dmy(0,1) & em_qs=em_dmy(0,1)
te_bps=te_dmy(0,2:*) & em_bps=em_dmy(0,2:*)
teunc_fov=te_unc(0,0) & emunc_fov=em_unc(0,0)
teunc_qs=te_unc(0,1) & emunc_qs=em_unc(0,1)
teunc_bps=te_unc(0,2:*) & emunc_bps=em_unc(0,2:*)
;
help,te_fov,em_fov, te_qs,em_qs, te_bps,em_bps
for i=0,nn_bp-1 do print,te_bps(0,i), em_bps(0,i)
help,teunc_fov,emunc_fov, teunc_qs,emunc_qs, teunc_bps,emunc_bps
for i=0,nn_bp-1 do print,teunc_bps(0,i), emunc_bps(0,i)
help,ofrac,fctm_am,fctm_ap
;
;--- make final info structure ---
teem_fov=[te_fov,em_fov]
teem_qs=[te_qs,em_qs]
teem_bps=[te_bps,em_bps]
unc_fov=[teunc_fov,emunc_fov]
unc_qs=[teunc_qs,emunc_qs]
unc_bps=[teunc_bps,emunc_bps]
;
teem_xrt=create_struct('file_am',file_am,'file_ap',file_ap, $
              'ofrac',ofrac, 'fctm_am',fctm_am, 'fctm_ap',fctm_ap, $
                        'teem_fov',teem_fov, $
                        'teem_qs',teem_qs, $
                        'teem_bps',teem_bps, $
                        'unc_fov',unc_fov, $
                        'unc_qs',unc_qs, $
                        'unc_bps',unc_bps)
help,teem_xrt,/str
;
yn='n'
read,prompt='*** save info_structure? [y/n] ',yn
if yn eq 'y' then begin $
  outdir='/disk/yla/takeda/REU_2026/qsbp_teem/'
  fname='teem_amap_'+yyyymmdd
  text='Created by SIG2TEEM_QSBP.PRO. '+ $
       'A structure contains signals of '+ $
       'file_am(Al_mesh filename), file_ap(Al_poly filename), '+ $
       'ofrac(prefilter open fraction assumed), '+ $
       'fctm_am(Alms filter contam. ratio assumed), '+ $
       'fctm_ap(Apol filter contam. ratio assumed), '+ $
       'teem_fov(Te and EM obtained for FOV data), '+ $
       'teem_qs(Te and EM obtained for QS data), '+ $
       'teem_bps(Te and EM obtained for BPs data), '+ $
       'unc_fov(Te & EM uncertainty for FOV data), '+ $
       'unc_qs(Te & EM uncertainty for QS data), '+ $
       'unc_bps(Te & EM uncertainty for BPs data). '
  name=['teem_xrt']
  savegen,teem_xrt,file=outdir+fname,text=text,name=name
endif
;
FIN:
END
