; $Id: taspl.pro,v 1.9 2003/10/28 19:54:41 jmspark Exp $
pro taspl,d,x,y,noerase=noerase,nsym=nsy,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,$
font=font $
   , davePtr = davePtr
;+
; NAME:         TASPL
; PURPOSE:
;               Plot a TASCOM file read in with TASRD
; CATEGORY:
; CALLING SEQUENCE:
;               TASPL,tasdata,'x-axis index','y-axis index'
; INPUTS:
;               tasdata = a tascom data structure read in with TASRD
; OPTIONAL INPUT PARAMTERS:
;               x,y-axis index= string defining parameter field for x,y-axis
;                        e.g.  taspl,d,'h','i' or taspl,d,'phm','mon'
;                  if y index is ommitted 'i' is the default e.g. taspl,d,'em'
;                  if x index is also ommitted 'h','i' is the default x,y
; KEYWORD PARAMETERS:
;   noerase   Doesn't erase the screen
;   nsym   symbol number for the user defined symbol routine:
;         symbols. IF -1 THEN draw lines
;   xmin, xmax  min. and max. range for x axis
;   ymin, ymax  ditto
;   font   font number to use for text. default is 5

; OUTPUTS:
;               a graph of x-index vs y-index for data structure d
; OPTIONAL OUTPUT PARAMETERS: xmin, xmax, ymin, ymax (if they are zero, but assinged to a variable)
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
;               this routine uses the user contributed (windt) macros symbols
;               and eplot to set the symbol to a solid circle and plot error
;               bars (if i or mon is the y-axis)
; PROCEDURE:
; MODIFICATION HISTORY:
;               Thom Mason, Riso National Lab, 1993
;   Sept. 1993: Added the font keyword parameter to choose a font
;   Oct 1993: Added some quasi-smart autoscaling options. Paul Schleger, Riso
;   Oct 1993: Now can cope with just one column of data existing
;-
COMMON axwidgets,mbase,xcol,ycol
common plot,xrs,yrs

message, 'Starts...', /continue


IF NOT keyword_set(noerase) THEN noerase=0     ;don't erase old plot?
IF NOT keyword_set(nsy) then nsy=1      ;default plotting symbol is open circle
IF NOT keyword_set(font) THEN font=5      ;default font is complex roman
IF (font LE 3) OR (font GE 20) THEN font=5     ;check that the font number is in a valid rng
fntstr = STRCOMPRESS('!'+STRING(font,format='(I)'),/REMOVE_ALL)   ;This string switches to the font
if (n_params() eq 1) then x='H'        ;No input parameters, so use 'H' for x axis
xind=tasid(x,d)
IF (xind eq -1) THEN BEGIN       ;But if 'H' isn't there, use first element
   IF (n_elements(d.prp) LT 2) THEN BEGIN     ;If there is just one column of data use
     xind=-2           ;the data point position in the array
     x='Point Number'
   END ELSE BEGIN
     xind=0
     x=d.prp(0)
   END
END
IF (n_params() le 2) THEN BEGIN        ;No specification for y axis, so try 'I'
  sind=tasid('SIGI',d)         ;Error bars?
  y='I'              ;choose the Intensity as plotting variable
ENDIF ELSE $
  IF (strupcase(y) eq 'I') then sind=tasid('SIGI',d) else $
    if (strupcase(y) eq 'MON') then sind=tasid('SIGM',d) else sind=-1

yind=tasid(y,d)            ;Try to get 'I' for y axis
IF (yind EQ -1) THEN BEGIN       ;If it isn't there
   IF (n_elements(d.prp) LT 2) THEN BEGIN     ;If there is just one column of data use
     yind=0          ;that column
     y=d.prp(0)
   END ELSE BEGIN           ;Otherwise use the second column
     yind=1
     y=d.prp(1)
   END
END

stuffy=d.data(yind,*)           ;Assign x and y axis data to a local variable

IF (xind EQ -2) THEN stuffx=FINDGEN(n_elements(stuffy)) $ ;y axis may just be index, if just one column
ELSE stuffx=d.data(xind,*)
IF n_elements(stuffx) LT 2 THEN BEGIN
   stsave=stuffx
   stuffx=fltarr(1)
   stuffx(0)=stsave
END

; If only one point exists, change it to an array of one element.
IF n_elements(stuffy) LT 2 THEN BEGIN
   stsave=stuffy
   stuffy=fltarr(1)
   stuffy(0)=stsave
END

tind=tasid('TIM',d)         ;Get the time. Useful for 'I'

IF (nsy EQ -1) THEN psym=0 ELSE BEGIN
   symbols,nsy,1.           ;Get the symbols defined
   psym=8
END

if (tind ge 0) then $
   IF n_elements(d.data(tind,*)) GT 1 THEN sd=stdev(d.data(tind,*),mean) $
   ELSE mean =  d.data(tind,0) $
else mean=0   ;Get the mean time for a data point

;Get the error bars. zero if none
if (sind ge 0) then begin
   err=d.data(sind,*)
endif ELSE begin
   err=FLTARR(n_elements(stuffy))
endelse

;calculate the plotting limits
IF keyword_set(xmin) THEN xmn=xmin ELSE xmn=min(stuffx)
IF keyword_set(xmax) THEN xmx=xmax ELSE xmx=max(stuffx)
IF keyword_set(ymin) THEN ymn=ymin ELSE ymn=min(stuffy)
IF keyword_set(ymax) THEN ymx=ymax ELSE ymx=max(stuffy)

;print,'xmin',xmn,'ymin',ymn

;Find out which points are actually within the desired window
index = WHERE((stuffx GE xmn) AND (stuffx LE xmx) AND $
         (stuffy GE ymn) AND (stuffy LE ymx),count)

;Calculate an extra amount of space around data for y axis
IF (count NE 0) THEN IF (sind ge 0) THEN msig=max(d.data(sind,index)) ELSE BEGIN
   minyr=(0.001 > 0.01*max(stuffy))
   msig=((minyr) > (0.1*(max(stuffy)-min(stuffy))))
END ELSE msig=0

;And the x axis too
IF (count NE 0) THEN BEGIN
   minxr=(0.001 > 0.01*max(stuffx))
   xdif=((minxr) > (0.1*(max(stuffx)-min(stuffx))))
END ELSE xdif=0

;Now autoscale on the y-axis, if ymax and ymin aren't set but xmin or xmax is and add the padding
IF (NOT keyword_set(ymin)) THEN BEGIN
   IF (count NE 0) THEN ymn=min(stuffy(index))-2*msig ELSE $
   ymn=min(stuffy)-3*msig
END
IF (NOT keyword_set(ymax)) THEN BEGIN
   IF (count NE 0) THEN ymx=max(stuffy(index))+2*msig ELSE $
   ymx=max(stuffy)+3*msig
END
IF (NOT keyword_set(xmin)) THEN BEGIN
   IF (count NE 0) THEN xmn=min(stuffx(index))-xdif ELSE $
   xmn=min(stuffx)-xdif
END
IF (NOT keyword_set(xmax)) THEN BEGIN
   IF (count NE 0) THEN xmx=max(stuffx(index))+xdif ELSE $
   xmx=max(stuffx)+xdif
END

IF (mean GT 0) THEN ytitle=fntstr+y+'(<Time>='+ string(mean,format='(i4)') + ' seconds)' $
ELSE ytitle=fntstr+y
title=fntstr + d.text(0)
if (noerase ne 1)  then begin
   xrs=[xmn,xmx]
   yrs=[ymn,ymx]
   IF (nsy EQ -1) THEN $
   plot, stuffx,stuffy,psym=psym,noerase=noerase,$
      ytitle=ytitle,xtitle=fntstr+x,charsize=1.2,title=title,$
      yrange=yrs,xrange=xrs,xstyle=1,ystyle=1 $
   ELSE eplot,stuffx,stuffy,err,err,psym=psym,noerase=noerase,$
      ytitle=ytitle,xtitle=fntstr+x,charsize=1.2,thick=2,title=title,$
      yrange=yrs,xrange=xrs,xstyle=1,ystyle=1
endif else IF (nsy EQ -1) THEN $
   plot,stuffx,stuffy,psym=psym,noerase=noerase,$
      ytitle=ytitle,xtitle=fntstr+x,charsize=1.2,title=title,$
      yrange=yrs,xrange=xrs,xstyle=1,ystyle=1 $
   ELSE eplot,stuffx,stuffy,err,err,psym=psym,noerase=noerase,$
      ytitle=ytitle,xtitle=fntstr+x,charsize=1.2,thick=2,title=title,$
      yrange=yrs,xrange=xrs,xstyle=1,ystyle=1
;map xmn, etc. back to the input parameters
xmin=xmn & xmax = xmx & ymin=ymn & ymax=ymx



; Generate a davePtr or modify an existing one with the new data.
widget_control, mbase, get_uvalue = pState
davePtr = (*pState).inPtr
dave_makeDavePtr, davePtr = davePtr
heap_free, (*(*davePtr).dataStrPtr).commonStr.histPtr
histPtr = ptr_new( $
   { qty:reform( stuffy ) $
   , err:reform( [ err ] ) $
   , x:reform( stuffx ) $
   , y:[0e] } $
   , /no_copy )
(*(*davePtr).dataStrPtr).commonStr.histPtr  = histPtr
(*(*davePtr).dataStrPtr).commonStr.xlabel = d.prp[ xind ]
(*(*davePtr).dataStrPtr).commonStr.ylabel = d.prp[ yind ]


return
end


















