;+
; Project     : SOHO - CDS     
;                   
; Name        : SHOW_SYNOPTIC
;               
; Purpose     : Displays data from a single CDS synoptic scan. 
;               
; Explanation : On a regular basis (daily as operations allow), CDS performs
;               a series of 4'x4' rasters along the solar meridian.  These 
;               rasters are put together in a single image and stored in 
;               an IDL save file for easy retrieval.  This routine provides
;               a simple display of the data taken from one such series of
;               rasters.  Note that the rasters overlap slightly in Solar Y 
;               and no account is taken of solar rotation when the rasters 
;               are performed.
;               
; Use         : IDL> show_synoptic, file_number
;    
; Inputs      : file_number - the date 'number' in format yyyymmdd
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: status - success at finding file.
;               
; Keywords    : PUBLIC	= If set, then only the three strongest lines are
;			  shown.
;
;		CLEAN	= If set, then CDS_CLEAN_IMAGE is called.
;               DEBUG   = print files to be used to screen
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: Needs CDS_SUMMARY_DATA to be defined to point to where the  
;               FITS files are.
;               
; Side effects: None
;               
; Category    : Data analysis
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 7-May-96
;               
; Modified    : Use FITS files instead of IDL save files.  08-May-96
;		Version 3, 18-Jun-1996, William Thompson
;			Use FXPAR instead of STR_PICK to read observation date
;			from FITS header.
;		Version 4, 22-Jul-1996, William Thompson, GSFC
;			Modified to use SETVIEW for multiple plots.
;			Added keyword /CLEAN
;               Version 5, 13-Jun-1997, CDP, added status return variable.
;               Version 6, 7-Jul-1997, CDP, Trap incomplete set of FITS files.
;		Version 7, 17-Oct-1997, JP, Modified to handle either 4 or 8 lines 
;		Version 8, 23-Dec-1997, William Thompson, GSFC
;			Also handle only 1 line, for SYNOP_LP case
;		Version 9, 29-Jun-1998, William Thompson, GSFC
;			Handle version 4 of SYNOP_F
;               Version 10, handle slit 5 raster with 4" steps.  CDP, 25-Aug-99
;               Version 11, Fix multiple copies of .fts files.  CDP, 25-Mar-03
;               version 12, Added DEBUG keyword to avoid problems in cron job
;
; Version     : Version 12, 15-Apr-2003
;-            

pro show_synoptic, fnum, public=public, clean=clean, status=status, debug=debug

;
;  check env. var.
;
if getenv('CDS_SUMMARY_DATA') eq '' then begin
   print,'$CDS_SUMMARY_DATA is not defined.'
   return
endif

;
;  if no parameters supplied, then help a little
;
if n_params() eq 0 then begin
   print,'Use:  IDL> show_synoptic, file_date_number'
   return
endif

;
;  success assumed
;
status = 1

;
;  compose expected file name
;
save_file = concat_dir('$CDS_SUMMARY_DATA','scds*'+trim(fnum)+'*.fts')
save_file = findfile(save_file)
if save_file(0) eq ''  then begin
   print,'Files for that date do not exist'
   status = 0
   return
endif
 
print,n_elements(save_file),' files found'
if (n_elements(save_file) lt 4) and (n_elements(save_file) ne 1) then begin
   print,'Incomplete set of FITS files'
   status = 0
   return
endif

;
;  cater for repeated creation of .fts files with different time stamps
;
if keyword_set(debug) then print_str,save_file
sf = str_pick(save_file,trim(fnum)+'_','.fts')
n = bsort(sf)
sf = sf(n)
save_file = save_file(n)

last = last_item(sf)
nn = where(sf eq last)
save_file = save_file(nn)
if keyword_set(debug) then begin
   print,'-------- using only --------'
   print_str,save_file
   print,' '
   print,' '
endif
;
;  figure out which version
;

if (n_elements(save_file) lt 8) AND (n_elements(save_file) gt 5) then begin
   print,'Incomplete set of FITS files'
   status = 0
   return
endif


;
;  set missing value
;
missing = -100

;
;  read individual images
;
if n_elements(save_file) eq 1 then begin
	image1 = readfits(save_file(0),header,/silent,nan=missing)
endif else if n_elements(save_file) eq 4 then begin
	image1 = readfits(save_file(2),header,/silent,nan=missing)
	image2 = readfits(save_file(3),/silent,nan=missing)
	image3 = readfits(save_file(1),/silent,nan=missing)
	image4 = readfits(save_file(0),/silent,nan=missing)
endif else if n_elements(save_file) eq 5 then begin
	image1 = readfits(save_file(2),header,/silent,nan=missing)
	image2 = readfits(save_file(4),/silent,nan=missing)
	image3 = readfits(save_file(1),/silent,nan=missing)
	image4 = readfits(save_file(0),/silent,nan=missing)
	image5 = readfits(save_file(3),/silent,nan=missing)
endif else begin
	image1 = readfits(save_file(4),header,/silent,nan=missing)
	image2 = readfits(save_file(5),/silent,nan=missing)
	image3 = readfits(save_file(7),/silent,nan=missing)
	image4 = readfits(save_file(3),/silent,nan=missing)
	image5 = readfits(save_file(1),/silent,nan=missing)
	image6 = readfits(save_file(6),/silent,nan=missing)
	image7 = readfits(save_file(2),/silent,nan=missing)
	image8 = readfits(save_file(0),/silent,nan=missing)
endelse

ss = size(image1)
if ss(1) eq 60 then xsc = 4.064 else xsc = 2.032

syn_start = anytim2cal(fxpar(header,'date_obs'))


;
;  find average for later scaling
;
av1 = average(image1,miss=missing)
if n_elements(save_file) gt 1 then begin
	av2 = average(image2,miss=missing)
	av3 = average(image3,miss=missing)
	av4 = average(image4,miss=missing)
endif
if n_elements(save_file) gt 4 then begin
	av5 = average(image5,miss=missing)
endif
if n_elements(save_file) gt 5 then begin
	av6 = average(image6,miss=missing)
	av7 = average(image7,miss=missing)
	av8 = average(image8,miss=missing)
endif

;
;  if possible create correct sized window
;
if !d.name eq 'X' then begin
if  n_elements(save_file) le 4 then  window,10,xs=650,ys=800 $
		else window,10,xs=1300,ys=800
   wset,10
   erase
endif
;
;  Start the plot.  The number of windows plotted depends on whether    
;  data is from SYNOP or HI_SYNOP and if the /PUBLIC keyword was used.
;
n_windows = n_elements(save_file)
if keyword_set(public) then n_windows = n_windows < 3
erase
xticks = !x.ticks
xtickv = !x.tickv
!x.ticks = 3
!x.tickv = [-120,0,120]
;
if (n_windows eq 1) then begin
	xlabel='O V 630 A'
endif else if (n_windows le 5) and (n_windows ge 3) then begin
	xlabel=['He I 584 A','O V 630 A','Mg IX 368 A','Fe XVI 361 A', $
		'Mg X 625 A']
endif else begin
	xlabel=['He I 584 A','O III 600 A','O V 630 A','Ne VI 563 A', $
	     'Mg IX 368 A','Mg X 625 A','Si XII 521 A','Fe XVI 361 A']
endelse

setview, 1, n_windows, 1, 1, .5

if keyword_set(clean) then cds_clean_image, image1
if n_dimensions(image1) eq 2 then begin
   plot_image, sigrange(image1,missing=missing,frac=0.99), missing=missing, $
	       xtitle = xlabel(0), ytitle='Solar Y (arcsec)',	$
	       origin=[-122,-1000], scale=[xsc,1.56]
endif

xmin = !x.window(0)
ymax = !y.window(1)
xmax = !x.window(1)
;
if n_windows gt 1 then begin
   setview, 2, n_windows, 1, 1, .5
   if keyword_set(clean) then cds_clean_image, image2
   if n_dimensions(image2) eq 2 then begin
      plot_image, sigrange(image2,missing=missing,frac=0.99), missing=missing, $
	          xtitle = xlabel(1), ytitle='Solar Y (arcsec)',	$
	          origin=[-122,-1000], scale=[xsc,1.56]
   endif
;
   setview, 3, n_windows, 1, 1, .5
   if keyword_set(clean) then cds_clean_image, image3
   if n_dimensions(image3) eq 2 then begin
      plot_image, sigrange(image3,missing=missing,frac=0.99), missing=missing, $
	          xtitle = xlabel(2), ytitle='Solar Y (arcsec)',	$
	          origin=[-122,-1000], scale=[xsc,1.56]
   endif
   xmax = !x.window(1)
;
   if n_windows gt 3 then begin
      if keyword_set(clean) then cds_clean_image, image4
      setview, 4, n_windows, 1, 1, .5
      if n_dimensions(image4) eq 2 then begin
         plot_image, sigrange(image4,missing=missing,frac=0.99), missing=missing, $
	             xtitle = xlabel(3), ytitle='Solar Y (arcsec)',	$
	             origin=[-122,-1000], scale=[xsc,1.56]
      endif

      xmax = !x.window(1)
;
      if n_windows eq 5 then begin
         if keyword_set(clean) then cds_clean_image, image5
	 setview, 5, n_windows, 1, 1, .5
         if n_dimensions(image5) eq 2 then begin
            plot_image, sigrange(image5,missing=missing,frac=0.99), missing=missing, $
	                xtitle = xlabel(4), ytitle='Solar Y (arcsec)',	$
	                origin=[-122,-1000], scale=[xsc,1.56]
         endif
	 
         xmax = !x.window(1)
	
      endif
   endif
;
   if n_windows eq 8 then begin
      if keyword_set(clean) then cds_clean_image, image5
      setview, 5, n_windows, 1, 1, .5
      if n_dimensions(image5) eq 2 then begin
         plot_image, sigrange(image5,missing=missing,frac=0.99), missing=missing, $
	             xtitle = xlabel(4), ytitle='Solar Y (arcsec)',	$
	             origin=[-122,-1000], scale=[xsc,1.56]
      endif
      
      xmax = !x.window(1)
;
      if keyword_set(clean) then cds_clean_image, image6
	setview, 6, n_windows, 1, 1, .5
        if n_dimensions(image6) eq 2 then begin
        plot_image, sigrange(image6,missing=missing,frac=0.99), missing=missing, $
	            xtitle = xlabel(5), ytitle='Solar Y (arcsec)',	$
	            origin=[-122,-1000], scale=[xsc,1.56]
        endif

	xmax = !x.window(1)
;
	if keyword_set(clean) then cds_clean_image, image7
	setview, 7, n_windows, 1, 1, .5
        if n_dimensions(image7) eq 2 then begin
           plot_image, sigrange(image7,missing=missing,frac=0.99), missing=missing, $
	               xtitle = xlabel(6), ytitle='Solar Y (arcsec)',	$
	               origin=[-122,-1000], scale=[xsc,1.56]
        endif

	xmax = !x.window(1)
;
	if keyword_set(clean) then cds_clean_image, image8
	setview, 8, n_windows, 1, 1, .5
        if n_dimensions(image8) eq 2 then begin
           plot_image, sigrange(image8,missing=missing,frac=0.99), missing=missing, $
	               xtitle = xlabel(7), ytitle='Solar Y (arcsec)',	$
	               origin=[-122,-1000], scale=[xsc,1.56]
        endif

	xmax = !x.window(1)
    endif
endif
;
setview
!x.ticks = xticks
!x.tickv = xtickv
;
;  Display the title across the top.
;
xcen = (xmin + xmax) / 2.
ycen = ymax + 0.9*!d.y_ch_size/!d.y_size
xyouts, xcen, ycen, 'CDS Synoptic scan started at  ' + syn_start,	$
	charsize=1.5, /normal, alignment=0.5
end
