
PRO plot_eis_fits, fitdata, width=width, velocity=velocity, chisq=chisq, $
                   _extra=_extra, $
                   min=min, max=max, $
                   xrange=xrange, yrange=yrange, title=title, line=line, $
                   origin=origin, scale=scale, mask=mask, log=log
                   

;+
; NAME
;
;    PLOT_EIS_FITS
;
; PROJECT
;
;    Hinode/EIS
;
; EXPLANATION
;
;    Plots line parameter images contained in the FITDATA structure
;    output by EIS_AUTO_FIT. This routine is called by EIS_FIT_VIEWER
;    (for example).
;
; INPUTS
;
;    FITDATA   Structure returned by EIS_AUTO_FIT.
;
; KEYWORDS
;
;    VELOCITY  A velocity map is plotted.
;
;    WIDTH     A line width map is plotted. Note that the quantity
;              plotted is the FWHM in mAngstroms.
;
;    CHISQ     A chi-square map is plotted.
;
;    ORIGIN    Specifies the coordinates to be applied to the
;              lower-left corner of the image. Default is (0,0).
;
;    SCALE     Specifies the X and Y scaling factors to be applied for
;              each pixel. The default is (1,1).
;
;    LOG       This is only active if the intensity is being plotted,
;              and it makes the logarithm of the intensity be
;              plotted. 
;
; OPTIONAL INPUTS
;
;    LINE      When FITDATA contains fits for multiple lines, LINE is
;              used to select the line to be plotted. If not set, then
;              user is asked to select a line.
;
;    MIN       Minimum value to be used for plot scale.
;
;    MAX       Maximum value to be used for plot scale.
;
;    MASK      A 2D array of same size as the FITDATA line parameter
;              arrays that is used to specify a pixel mask. Pixels for
;              which MASK=0 will be flagged as missing data and so
;              displayed in the output image.
;
; OUTPUTS
;
;    An image is sent to a graphics window. By default the routine
;    plots the intensity image for the selected emission line. The
;    keywords /velocity, /width, /chisq (see above) can be used to
;    plot other quantities. 
;
; CALLS
;
;    EIS_GET_FITDATA, PLOT_VEL_IMAGE
;
; HISTORY
;
;    Ver.1, 4-Nov-2009, Peter Young
;    Ver.2, 5-Jan-2010, Peter Young
;       added /silent keyword to loadct
;    Ver.3, 6-Apr-2011, Peter Young
;       now calls eis_get_fitdata to retrieve parameter arrays;
;       _extra= keyword has been added so that additional
;       eis_get_fitdata keywords can be used (e.g.,
;       /KEEP_INST_WIDTH).
;    Ver.4, 7-Apr-2011, Peter Young
;       line= keyword had been accidentally removed with previous
;       change so has been re-instated; line= keyword is now passed to
;       eis_get_fitdata. 
;    Ver.5, 11-May-2011, Peter Young
;       added ORIGIN and SCALE keywords; now calls plot_vel_image for
;       plotting the velocity image.
;    Ver.6, 14-Aug-2012, Peter Young
;       added MASK= optional input
;    Ver.7, 23-Sep-2014, Peter Young
;       line= does not need to be set if /chisq is set.
;    Ver.8, 24-Sep-2014, Peter Young
;       implemented /log option (for intensity).
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use:  plot_eis_fits, fitdata [,/ wid, /vel, /chisq, min=, max=, xrange=, yrange=, title=,'
  print,'                        line=, origin=, scale=, mask=, /log ]'
  return
ENDIF 

                   
IF fitdata.ngauss EQ 1 THEN line=0

IF n_elements(mask) NE 0 THEN BEGIN
  siz=size(mask)
  IF siz[1] NE fitdata.nx OR siz[2] NE fitdata.ny THEN BEGIN
    print,'%PLOT_EIS_FITS:  The dimensions of the input MASK do not match those of FITDATA. Returning...'
    return
  ENDIF 
ENDIF

swtch = keyword_set(chisq)

IF fitdata.ngauss GT 1 AND n_elements(line) EQ 0 AND swtch EQ 0 THEN BEGIN
  ngauss=fitdata.ngauss
  print,'Choose a line to plot:'
  FOR i=0,ngauss-1 DO BEGIN
    cen=reform(fitdata.aa[3*i+1,*,*])
    print,format='(i3,". ",f8.2)',i+1,average(cen,missing=0.)
  ENDFOR
  read,'Type index of line: ',index
  line=index-1
ENDIF 

IF n_elements(xrange) EQ 0 THEN xrange=[0,fitdata.nx-1]
IF n_elements(yrange) EQ 0 THEN yrange=[0,fitdata.ny-1]

IF n_elements(origin) EQ 0 THEN origin=[xrange[0],yrange[0]]

IF n_elements(scale) EQ 0 THEN scale=[1.0,1.0]

IF keyword_set(velocity) THEN BEGIN
  vel=eis_get_fitdata(fitdata,/vel,_extra=_extra,line=line)
 ;
  IF n_elements(mask) NE 0 THEN BEGIN
    k=where(mask EQ 0,nk)
    IF nk NE 0 THEN vel[k]=fitdata.missing
  ENDIF 
 ;
  vel=vel[xrange[0]:xrange[1],yrange[0]:yrange[1]]
 ;
  IF n_elements(min) EQ 0 THEN min=-30
  IF n_elements(max) EQ 0 THEN max=30
 ;
  plot_vel_image,vel,velmax=max,origin=origin,title=title, scale=scale, $
             _extra=_extra, black_val=fitdata.missing
  return
ENDIF 


IF keyword_set(width) THEN BEGIN
  wid=eis_get_fitdata(fitdata,/wid,/quiet,_extra=_extra,line=line)
 ;
  IF n_elements(mask) NE 0 THEN BEGIN
    k=where(mask EQ 0,nk)
    IF nk NE 0 THEN wid[k]=fitdata.missing
  ENDIF 
 ;
  wid=wid[xrange[0]:xrange[1],yrange[0]:yrange[1]]
 ;
  IF n_elements(min) EQ 0 THEN min=0.0
  IF n_elements(max) EQ 0 THEN max=0.07
 ;
  loadct,3,/silent
  plot_image,wid,min=min,max=max,origin=origin,title=title,scale=scale, $
             _extra=_extra
  return
ENDIF 

IF keyword_set(chisq) THEN BEGIN
  chisq=eis_get_fitdata(fitdata,/chi,/quiet,_extra=_extra,line=line)
 ;
  IF n_elements(mask) NE 0 THEN BEGIN
    k=where(mask EQ 0,nk)
    IF nk NE 0 THEN chisq[k]=fitdata.missing
  ENDIF 
 ; 
  chisq=chisq[xrange[0]:xrange[1],yrange[0]:yrange[1]]
 ;
  IF n_elements(min) EQ 0 THEN min=0
  IF n_elements(max) EQ 0 THEN max=3
 ;
  loadct,3,/silent
  plot_image,chisq,min=min,max=max,origin=origin,title=title,scale=scale, $
             _extra=_extra
  return
ENDIF 

int=eis_get_fitdata(fitdata,/int,/quiet,_extra=_extra,line=line)
;
IF n_elements(mask) NE 0 THEN BEGIN
  k=where(mask EQ 0,nk)
  IF nk NE 0 THEN int[k]=fitdata.missing
ENDIF 
; 
int=int[xrange[0]:xrange[1],yrange[0]:yrange[1]]

IF keyword_set(log) THEN BEGIN
 ;
 ; Need to re-define min if it's 0 or less.
 ;
  IF n_elements(min) NE 0 AND min LE 0. THEN BEGIN
    k=where(int NE fitdata.missing,nk)
    i_sort=sort(int[k])
    i_max=min([9,nk-1])
    min=median(int[i_sort[0:i_max]])
  ENDIF 
 ;
  IF n_elements(min) EQ 0 THEN BEGIN
    k=where(int NE fitdata.missing,nk)
    i_sort=sort(int[k])
    i_max=min([9,nk-1])
    min=median(int[i_sort[0:i_max]])
  ENDIF 
 ;
  IF n_elements(max) EQ 0 THEN BEGIN
    max=max(int)
  ENDIF
 ;
  int=int>min
  int=int<max
  int_plot=alog10(int)
 ;
  dmin=alog10(min)
  dmax=alog10(max)
ENDIF ELSE BEGIN 
  IF n_elements(min) EQ 0 THEN dmin=0. ELSE dmin=min
  IF n_elements(max) EQ 0 THEN dmax=max(int) ELSE dmax=max
 ;
  int_plot=int
ENDELSE 
;
loadct,3,/silent
plot_image,int_plot,min=dmin,max=dmax,origin=origin,title=title,scale=scale, $
             _extra=_extra

END
