;                          GS.pro
;+
;  Routine to display test_rd's DATA array as a grey scale image.
;  The DATA array is trimmed in case any long records exist and plotted
;  to give reasonable representation of channel 4 (and hopefully the others)
;  The data can be decompressed before plotting if requested.
;
;  The maximum number of test_rd read datasets that it is sensible to try
;  and display in one go is 900.  The greyscale image is displayed within
;  a window having a 10 pixel wide border.
;
;  By-products    GSA  2-d array containing only valid spectral area of 
;                      original DATA array.
;                 GSA_TEMP a 2-d array internally scaled for each channel's
;                      data - for display only, not to be used for data
;                      analysis
;            
;                This array is displayed such that the value which is 
;                GS_CUT times the mean of the channel's data is scaled to
;                white. The scaling can be changed by changing this variable
;                or by the usual look-up table manipulations.
;
;  Use:
;        IDL>  .run gs
;
;                
;   CDP   Oct 91
;         Feb 92   Include gs_decomp flag
;         Mar 92   Allow individual scaling of channels and allow cutoff
;                  set by GS_CUT
;         Mar 92   Variable bins/channel
;         Mar 92   Major readjustment to cater for test_rd, set max display
;                  size of 900.
;         May 92   Fix scaling bug    CDP
;
;-
;
;  is cut off level set - if not default to 2.5*mean
;
if n_elements(gs_cut) lt 1 then gs_cut = 2.5
;
;   and then pick up number of bins per spectrum (omit duff one at end)
;
chan_r = bcs_chan_rng(index,0) 
c1s = chan_r(0)
c1e = chan_r(1)-1
c2s = chan_r(2)
c2e = chan_r(3)-1
c3s = chan_r(4)
c3e = chan_r(5)-1
c4s = chan_r(6)
c4e = chan_r(7)-1
chan_range = chan_r(7) - chan_r(0) + 2

;
;  set up border
;
gs_off = [10,20]
;
;  how many dsets to display ndset?
;
ndset = n_elements(dset_str.dset) 
if ndset gt 900 then begin
   print,' '
   print,'Data array selected is too large. Extract a smaller subset'
   print,'if you want to be able to display it.'
   go_disp = 0
endif else begin
   go_disp = 1
endelse
;
;  set up window required
;
wy = max([400,ndset+2*gs_off(1)])
if !d.name eq 'X' and go_disp eq 1 then begin
   window,0,retain=2,title='      BCS spectra - Time increases bottom to top',$
             xs=chan_range+2*gs_off(0),ys=wy
endif

;
;  check for consistent modeid 
;
if (total(index.bcs.modeid-index(0).bcs.modeid) ne 0) then begin
    print,' '
    print,'Warning: not all MODEIDs in the extracted data are the same.'
endif

;
;  allocate output array
;
big = min([768,chan_range])
gsa = bytarr(big,ndset)
gsa = data(0:big-1,*)
gsa_temp = gsa

;
;  decompress?
;
ans = ' '
if ndset gt 900 then begin
   print,' '
   read,'Decompress data before storage in GSA array? > ',ans
endif else begin
   print,' '
   read,'Decompress data before display? > ',ans
endelse

if strupcase(ans) eq 'Y' then begin
   gsa = bcs_decomp(gsa)
   gs_decomp = 1
endif else begin
   gs_decomp = 0
endelse

;
;  calculate mean of Sulphur area
;
if !d.name eq 'X' and go_disp eq 1 then begin
   print,'Scaling data for display...'
   if c4e - c4s gt 1 then begin
      g1 = gsa(c4s:c4e,*)
      m4 = max([gs_cut*total(g1)/n_elements(g1),1.])
      if max(g1) gt m4 then g1(where(g1 gt m4)) = m4
      gsa_temp(c4s:c4e,*) = g1*250./m4
   endif
   if c3e - c3s gt 1 then begin
      g1 = gsa(c3s:c3e,*)
      m3 = max([gs_cut*total(g1)/n_elements(g1),1.])
      if max(g1) gt m3 then g1(where(g1 gt m3)) = m3
      gsa_temp(c3s:c3e,*) = g1*250./m3
   endif
   if c2e - c2s gt 1 then begin
      g1 = gsa(c2s:c2e,*)
      m2 = max([gs_cut*total(g1)/n_elements(g1),1.])
      if max(g1) gt m2 then g1(where(g1 gt m2)) = m2
      gsa_temp(c2s:c2e,*) = g1*250./m2
   endif
   if c1e - c1s gt 1 then begin
      g1 = gsa(c1s:c1e,*)
      m1 = max([gs_cut*total(g1)/n_elements(g1),1.])
      if max(g1) gt m1 then g1(where(g1 gt m1)) = m1
      gsa_temp(c1s:c1e,*) = g1*250./m1
   endif
endif
;
;  and display
;
if !d.name eq 'X' and go_disp eq 1 then begin
   tvscl,gsa_temp,gs_off(0),gs_off(1)
   ypos = 1.0 - (float(gs_off(1))*.9)/float(ndset)
   xyouts,(c1s+c1e)/2.5/float(chan_range),ypos,'Fe XXVI',/normal,size=1.5
   xyouts,(c2s+c2e)/2.2/float(chan_range),ypos,'Fe XXV',/normal,size=1.5
   xyouts,(c3s+c3e)/2.1/float(chan_range),ypos,'Ca XIX',/normal,size=1.5
   xyouts,(c4s+c4e)/2.05/float(chan_range),ypos,'S XV',/normal,size=1.5
   xyouts,0.0,0.0,'Start time is '+fmt_tim(index(0)),size=1.2,/normal
endif  else begin
   if go_disp eq 1 then begin
      print,' '
      print, 'Grey scale would have been plotted on the correct device.'
      print, 'But in any case, the GSA array has been set up.'
   endif else begin
     print,' '
     print,'Array was too large to plot but arrays have been set up as normal.'
     print,'It should be OK to go ahead with other GS routines.'
   endelse
endelse

end
