
FUNCTION eis_fit_scatter, fitdata, xrange=xrange, yrange=yrange, nx=nx, ny=ny, $
                          xparam=xparam, yparam=yparam, image=image, $
                          maximage=maximage, dens=dens, line=line, $
                          im_xrange=im_xrange, im_yrange=im_yrange, _extra=extra, $
                          scatter=scatter, heatmap=heatmap

;+
; NAME:
;     EIS_FIT_SCATTER
;
; PURPOSE:
;     This routine takes a FITDATA structure (produced by
;     EIS_AUTO_FIT) and creates a scatter plot for two of the
;     parameters (e.g., intensity vs. velocity). If the number of
;     points is greater than 5000, then a "heatmap" is plotted
;     instead. 
;
; CATEGORY:
;     HINODE/EIS; Gauss fitting; display.
;
; CALLING SEQUENCE:
;     Result = EIS_FIT_SCATTER( FitData )
;
; INPUTS:
;     Fitdata:  A fitdata structure in the format returned by
;               EIS_AUTO_FIT.  
;
; OPTIONAL INPUTS:
;     Line:     If the fit structure contains multiple lines, then
;               this allows you to choose the line. If not specified
;               then you will be given a list of options.
;     Xrange:   A 2 element array that directly specifies the X-range
;               for the plot. 
;     Yrange:   A 2 element array that directly specifies the Y-range
;               for the plot. 
;     Nx:       The number of X-bins (or pixels) for the displayed
;               heatmap image. The default is 40.
;     Ny:       The number of Y-bins (or pixels) for the displayed
;               heatmap image. The default is 40.
;     Xparam:   The name for the parameter to be used for the
;               X-axis. Options include 'int', 'vel', 'wid',
;               'chi'. The default value is 'vel'.
;     Yparam:   The name for the parameter to be used for the
;               X-axis. Options include 'int', 'vel', 'wid',
;               'chi'. The default value is 'wid'.
;     MaxImage :By default the displayed heatmap has a maximum that is
;               the maximum of the image. By specifying MAXIMAGE this
;               maximum can be manually adjusted. This can be useful
;               for "saturating" the image to reveal low signal
;               pixels. 
;     Dens:     A density structure in the form output by the routine
;               EIS_DENSITY.PRO (allows density to be added as one of
;               the parameters).
;     Im_Xrange: Specifies the X-range for the image arrays in
;                FITDATA. 
;     Im_Xrange: Specifies the Y-range for the image arrays in
;                FITDATA.
;     _Extra:   The routine also takes standard inputs for the IDL
;               IMAGE plot object. For example, font_size, color,
;               thick, etc.
;	
; KEYWORD PARAMETERS:
;     SCATTER:  If set, then force the routine to do a standard
;               scatter plot.
;     HEATMAP:  If set, then force the routine to do a heatmap plot. 
;
; OUTPUTS:
;     Returns an IDL plot object containing the scatter plot. If
;     there are more than 5000 points then a heatmap is plotted
;     instead. 
;
; OPTIONAL OUTPUTS:
;     IMAGE:  A 2D image containing the heatmap image that was
;             plotted. 
;
; CALLS:
;     EIS_GET_FITDATA
;
; EXAMPLES:
;     IDL> eis_auto_fit, windata, fitdata
;     IDL> p=eis_fit_scatter(fitdata)
;     IDL> p=eis_fit_scatter(fitdata, xparam='wid', yparam='int')
;     IDL> p=eis_fit_scatter(fitdata, im_xrange=[0,10], im_yrange=[0,10])
;
; MODIFICATION HISTORY:
;     Ver.1, 27-Mar-2020, Peter Young
;-


ng=fitdata.ngauss
IF ng EQ 1 THEN line=0

IF ng GT 1 AND n_elements(line) EQ 0 THEN BEGIN
  print,''
  print,'% EIS_FIT_SCATTER: The fit structure contains '+trim(ng)+' emission lines. Please select'
  print,'                   which line to use by specifying the keyword LINE=.'
  form1=trim(ng)+'i9'
  form2=trim(ng)+'f9.3'
  print,''
  print,format='("   LINE: ",'+form1+')',indgen(ng)
  print,format='(" REFWVL: ",'+form2+')',fitdata.refwvl
  print,''
  return,''
ENDIF


IF n_elements(xparam) EQ 0 THEN BEGIN
  xparam='vel'
  print,"% EIS_FIT_SCATTER:  XPARAM not specified, so using xparam='vel'"
ENDIF 

IF n_elements(yparam) EQ 0 THEN BEGIN
  yparam='wid'
  print,"% EIS_FIT_SCATTER:  YPARAM not specified, so using yparam='wid'"
ENDIF 

CASE xparam OF 
  'vel': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/vel,/quiet)
    xtitle='Velocity / km s!u-1!n'
  END 
  'wid': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/wid,/quiet)
    xtitle='Non-thermal width / '+string(197b)
    print,"% EIS_FIT_SCATTER: Note: instrumental width has been removed"
  END 
  'int': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/int,/quiet)
    xtitle='Intensity / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  'chi': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/chi,/quiet)
    xtitle='Chi!u2!n [no units]'
  END 
  'bg1': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/bg1,/quiet)
    xtitle='Background intensity 1 / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  'bg2': BEGIN
    xx=eis_get_fitdata(fitdata,line=line,/bg2,/quiet)
    xtitle='Background intensity 2 / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  'den': BEGIN
    IF n_tags(dens) EQ 0 THEN BEGIN
      print,'% EIS_FIT_SCATTER: please specify a density structure with the DENS keyword.'
      print,'                  Returning...'
      return,''
    ENDIF 
    xx=dens.ldens
    xtitle='Log!d10!n (Density / cm!u-3!n)'
  END 
  ELSE: BEGIN
    print,'% EIS_FIT_SCATTER: please specify one of  the following options for XPARAM:'
    print,"   xparam='vel'  - velocity"
    print,"   xparam='wid'  - width"
    print,"   xparam='int'  - intensity"
    print,"   xparam='chi'  - chi^2"
    print,"   xparam='bg1'  - background intensity 1"
    print,"   xparam='bg2'  - background intensity 2"
    print,"   xparam='den'  - electron density (only if the density structure is input)"
    return,''
  END
ENDCASE


CASE yparam OF 
  'vel': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/vel,/quiet)
    ytitle='Velocity / km s!u-1!n'
  END 
  'wid': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/wid,/quiet)
    ytitle='Width / '+string(197b)
  END 
  'int': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/int,/quiet)
    ytitle='Intensity / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  'chi': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/chi,/quiet)
    ytitle='Chi!u2!n [no units]'
  END 
  'bg1': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/bg1,/quiet)
    ytitle='Background intensity 1 / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  'bg2': BEGIN
    yy=eis_get_fitdata(fitdata,line=line,/bg2,/quiet)
    ytitle='Background intensity 2 / erg cm!u-2!n s!u-1!n sr!u-1!n'
  END 
  ELSE: BEGIN
    print,'% EIS_FIT_SCATTER: please specify one of  the following options for YPARAM:'
    print,"   yparam='vel'  - velocity"
    print,"   yparam='wid'  - width"
    print,"   yparam='int'  - intensity"
    print,"   yparam='chi'  - chi^2"
    print,"   xparam='bg1'  - background intensity 1"
    print,"   xparam='bg2'  - background intensity 2"
    return,''
  END
ENDCASE


IF n_elements(im_xrange) NE 0 THEN BEGIN
  xx=xx[im_xrange[0]:im_xrange[1],*]
  yy=yy[im_xrange[0]:im_xrange[1],*]
ENDIF 
IF n_elements(im_yrange) NE 0 THEN BEGIN
  xx=xx[*,im_yrange[0]:im_yrange[1]]
  yy=yy[*,im_yrange[0]:im_yrange[1]]
ENDIF 

IF n_elements(xrange) EQ 0 THEN BEGIN 
  k=where(xx NE -100.)
  xx_mean=mean(xx[k])
  xx_sig=stdev(xx[k])
  xrange=[xx_mean-3.*xx_sig,xx_mean+3.*xx_sig]
ENDIF 

IF n_elements(yrange) EQ 0 THEN BEGIN 
  k=where(yy NE -100.)
  yy_mean=mean(yy[k])
  yy_sig=stdev(yy[k])
  yrange=[yy_mean-3.*yy_sig,yy_mean+3.*yy_sig]
ENDIF

IF n_elements(font_size) EQ 0 THEN font_size=12

;
; swtch determines if a scatter plot is created or a heatmap.
;
swtch=0
IF n_elements(xx) LT 5000 OR keyword_set(scatter) THEN swtch=1
IF keyword_set(heatmap) THEN swtch=0


;
; Now make the plot (either scatter or heatmap).
;
IF swtch EQ 1 THEN BEGIN
 ;
  IF n_elements(symbol) EQ 0 THEN symbol='X'
  p=plot(xx,yy,symbol=symbol, $
         xrange=xrange, yrange=yrange, $
         _extra=extra,linestyle='none', $
         xtitle=xtitle, ytitle=ytitle)
 ;
ENDIF ELSE BEGIN
 ;
  IF n_elements(nx) EQ 0 THEN nx=40
  IF n_elements(ny) EQ 0 THEN ny=40

  image=intarr(nx,ny)

  xbin=(xrange[1]-xrange[0])/float(nx-1)
  ybin=(yrange[1]-yrange[0])/float(ny-1)

  xaxis=findgen(nx)*xbin + xrange[0]
  yaxis=findgen(ny)*ybin + yrange[0]

  FOR i=0,nx-2 DO BEGIN
    FOR j=0,ny-2 DO BEGIN
      k=where(xx GE xaxis[i] AND xx LT xaxis[i+1] $
              AND yy GE yaxis[j] AND yy LT yaxis[j+1],nk)
      image[i,j]=nk
    ENDFOR
  ENDFOR

  IF n_elements(maximage) NE 0 THEN image=image<maximage
  IF n_elements(rgb_table) EQ 0 THEN rgb_table=3

 ;
 ; By default the routine forces the plot to be square. The keyword
 ; 'aspect_ratio' can be used to change this.
 ;
  aspect_ratio=(xrange[1]-xrange[0])/(yrange[1]-yrange[0])
  IF n_tags(extra) NE 0 THEN BEGIN
    IF tag_exist(extra,'aspect_ratio') EQ 1 THEN noprint=1 ELSE noprint=0
  ENDIF 
  IF NOT keyword_set(noprint) THEN print,'% EIS_FIT_SCATTER: aspect_ratio='+trim(string(format="(f10.3)",aspect_ratio))

  p=image(image,xaxis,yaxis,axis_style=2,rgb_table=rgb_table, $
          xtitle=xtitle, ytitle=ytitle, margin=0.12, $
          _extra=extra, font_size=font_size, aspect_ratio=aspect_ratio )

  IF n_elements(maximage) EQ 0 THEN print,'% EIS_FIT_SCATTER: The maximum of the image is: '+trim(max(image))
 ;
ENDELSE


return,p

END
