;+
;NAME:
;	rdtbl
;PURPOSE:
;	Call the SXT table and search the longest exposures
;	for each X-ray filters.
;
;	<< CAUTION >>
;	   Now this procedure is put in /home/flare1/sxt/idlpro.
;	   Thus only "sxt1@flare1" uses this directly.
;	   Because it uses the reformatted data, please wait
;          until the reformat has finished.
;
;CALLING SEQUENCE:
;	rdtbl                       ; for The latest SXT table
;	rdtbl,893                   ; by  Table Number
;	rdtbl,'21-Mar-94 14:00'     ; by  Date/Time
;	rdtbl,index                 ; by  Index
;	rdtbl,/ffi                  ;     FFI only
;	rdtbl,/all,/hc              ;     All SXT table
;INPUT:
;	itab0  - Table number to display
;	               (or)
;	         A string time representation. If this value is not
;	         present, the present UT-time is used.
;OPTIONAL KEYWORD
;	ffi    - If present, ENTRY table and FFI tables for both QT-HI
;	         and QT-ME are displayed with the list of the longest
;	         exposures.
;	all    - If present, all of SXT table is called.
;	hc     - If present, the resolt is printed out. You can select
;	         the printer in KSC or SSOC by menu.
;HISTORY:
;	Written 25-Mar-94 by R.Kano
;
pro rdtbl,itab0,hc=hc,ffi=ffi,all=all
;-

spawn,'hostname',host
if host(0) eq 'flare5' then begin
   print
   print,'CAUTION  : Please run in "flare1".'
   print,'           User in flare1 must be "sxt".'
   return
endif

if n_elements(itab0) eq 0 then begin
   itab0=fmt_tim(addtime(syst2ex(),delta= -9.*60.),day_str,time_str)
   print,'--- Now, calling the latest SXT table ---'
   print
endif

tablefile=gtab_file(itab0,table=table,headline=headline)

LUN=-1 ; standard output
printf,LUN,headline+['    <page 1>','',''],form='(A)'

if keyword_set(hc) then begin
   openw,LUN,'~/rdtbl.hc',/get_lun 
   printf,LUN,headline+['    <page 1>','',''],form='(A)'
   ffi=1
endif

if (keyword_set(ffi) or keyword_set(all)) then begin
   printf,LUN,(gtab_entry(itab0))(3:*)             ; Entry table
   printf,LUN
   printf,LUN,(gtab_ffi(itab0,fix(table(0))))(3:*) ; FFI table for QT-HI
   printf,LUN
   printf,LUN,(gtab_ffi(itab0,fix(table(2))))(3:*) ; FFI table for QT-ME
   printf,LUN
endif

longest=intarr(2,7)
for rate=0,1 do begin
   st=8+(64+32)+(24*13+8)*table(2*rate)
   filt_A = table(st+3+24*indgen(13)) mod 16   ; See 'print,gt_filta()'
   filt_B = table(st+3+24*indgen(13))  /  16   ; See 'print,gt_filtb()'
   DPE    = table(st+7+24*indgen(13))          ; DPE
   Img_typ= table(st+8+24*indgen(13))          ; Normal=1, Dark=2, Cal=3
   for B=1,6 do begin
      sel=where( (filt_A eq 1)*(Img_typ eq 1)*(filt_B eq B) )
      if total(sel) ge 0 then longest(rate,B)=max(DPE(sel))
   endfor
endfor

Expo=[ .08,   .24,   .8,   1.8,   2.4,   3.8,   5.8,   7.8,  11.8,  18, $
     28,    38,    58,    78,   118,   168,   238,   338,   468,   668, $
    948,  1338,  1888,  2668,  3778,  5338,  7548, 10678, 15108, 21358, $
  30208, 42718, 60418, 85438,120828,170878,241668]/1e3
                                           ; DPE -> exposure [sec]

printf,LUN,format='(10X," __________ Longest Exposures in FFI Table ________ ")'
printf,LUN,format='(10X,"|                                                  |")'
printf,LUN,format='(10X,"|  FilterB   QT-H   QT-M       Longest Exposures   |")'
for B=1,6 do printf,LUN $
	,format = '(10X,"|",   A8,     2I7,"   -> ",I4,"   = ",F6.2," sec   |")' $
	,gt_filtb(B,/str),longest(*,B),max(longest(*,B)),Expo(max(longest(*,B)))
printf,LUN,format='(10X,"|__________________________________________________|")'


if keyword_set(all) then begin
   tmp=fix(table([1,3,5,7])) & tmp=tmp(sort(tmp)) & tmp=tmp(uniq(tmp))
   printf,LUN,['','','']+headline+['    <page 2>','',''],form='(A)'
   for i=0,n_elements(tmp)-1 do begin
      printf,LUN,(gtab_pfi(itab0,tmp(i)))(3:*)
      printf,LUN
   endfor
   printf,LUN,['','','']+headline+['    <page 3>','',''],form='(A)'
   printf,LUN,(gtab_comm(itab0))(3:*)              ; COMMON table
   printf,LUN
   printf,LUN,(gtab_roi(itab0))(3:*)               ; ROI table
endif



if keyword_set(hc) then begin
  free_lun,LUN
  read,'Which printer do you want to use? [0: flare5(KSC), 1: flare1(SSOC), 2: local, 3: cancel] '$
      ,answer
  case answer of
    0 : begin
           print,'Printing out to flare5'
           spawn,'(cat ~/rdtbl.hc | rsh flare5 -l yohkoh lpr);\rm ~/rdtbl.hc'
        end
    1 : begin
           print,'Printing out to flare1'
           spawn,'(cat ~/rdtbl.hc | rsh flare1 -l sxt lpr);\rm ~/rdtbl.hc'
        end
    2 : begin
           print,'Printing out to local host'
           spawn,'lpr -r ~/rdtbl.hc'
        end
    else : print,'FFI table was not printed out. See "~/rdtbl.hc".'
  endcase
endif

;print ; empty line

end


