;; MTA_DB_PLOT  BDS June 2001
;;  plots tag_array vs tag_array from a given structure
;;  used to plot filtered msids from mta average databases
;;
;;  Calling Sequence
;;   mta_db_plot, struct, 'xaxis', 'yaxis', [ filter='', opt='' ]
;;      where - struct is an idl structure of arrays (variable)
;;            - xaxis is the tagname of values to plot on x axis (string)
;;            - yaxis is the tagname of values to plot on y axis (string)
;;            - filter is an optional keyword to specify a where-like 
;;                        filter using the struct tagnames (string)
;;            - opt is an optional string of comma delimited 
;;                        idl plot keywords to apply (string)
;;
;;   coming soon:
;;      mta_db_oplot
;;
;;  Examples
;;      > mta_db_plot, test, 'time', '1dahacu_avg'
;;      > mta_db_plot, test, 'time', 'ohrthr27_avg', $
;;         filter='ohrthr27_avg * 6 - 4 lt 1778 and /
;;                (time lt 95010000 or time gt 95030000)'
;;      > mta_db_plot, test, 'time', 'ohrthr27_avg', $
;;         filter='ohrthr27_avg lt 296', $
;;         opt='psym=5, ystyle=1, xtitle="time", /
;;              ytitle="ohrthr27_avg", title="Test Plot"
;;
;;  To get data, use:
;;      MRDFITS ; for existing tables
;;      MTA_DB_GET ; to retrieve selected data from databases
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

FUNCTION FORM_TAG, name
; return a happy idl tagname
outname = strupcase(name)
; check for msids starting with a number
tmp = strmid(outname,0,1)
for i=0, 9 do begin
  if (tmp eq strtrim(string(i),2)) then outname = "X"+outname
endfor
return, outname
end

FUNCTION FILTER_APP, struct, filter
; apply filter
if (filter eq "" or filter eq "none") then return, -1
filt_app = ""
names = tag_names(struct)
; watch out for these guys
split_on = ['+', '-', '/', '*', '^', '#', '(', ')', '[', ']']

found = intarr(1)
for i = 0, strlen(filter)-1 do begin
  for j = 0, n_elements(split_on)-1 do begin
    if (strmid(filter, i, 1) eq split_on(j)) then begin
      found = [found, i]
    endif
  endfor
endfor
; found special characters, now put space before and after each
num_found = n_elements(found)-1 ; skip first, dummy found element
if (num_found gt 1) then begin
  ; adjust found indexes for moving and make new string to write to
  found = found+(2*(indgen(num_found+1)-1))
  for i = 1, num_found do begin ; skip first, dummy found element
    ;print, filter ; debug
    filter = strmid(filter, 0, found(i))+ $
             " "+strmid(filter, found(i), 1)+" "+ $
             strmid(filter, found(i)+1, strlen(filter))
    ;print, filter ; debug
  endfor
endif
    
pfilt = strsplit(filter, " ", /extract)
org_pfilt = pfilt
; parse filter and recombine so idl understands
for i = 0, n_elements(pfilt) - 1 do begin
  pfilt(i) = form_tag(pfilt(i))
  ;print, pfilt(i) ; debug
  num = where(names eq pfilt(i), count)
  if (count eq 1) then begin
    filt_app = filt_app+"struct."+pfilt(i)+" "
  endif else begin
    filt_app = filt_app+org_pfilt(i)+" "
  endelse
endfor
;print, "filter to apply: ", filt_app ; debug
;b = intarr(n_elements(struct))
b = intarr(1)
bcnt = 0
filt_app = "b=where("+filt_app+",bcnt)"
;print, "filter to apply: ", filt_app ; debug
a = execute(filt_app)
;b=where(struct.OOBAGRD3_AVG lt 2 ,bcnt) ; debug
;print, "execute result: ", a, " bcnt=", bcnt ; debug
;print, "n_el(b)=", n_elements(b) ; debug
return, b
end

PRO MTA_DB_PLOT, data_in, xaxis, yaxis, filter=filter, opt=opt
; plot xaxis vs yaxis from mrdfits created struct
;  provide xaxis and yaxis tagnames and
;   optional where-like filter string based on other tagnames

xaxis = form_tag(xaxis)
yaxis = form_tag(yaxis)

names = tag_names(data_in)
xnum = where(names eq xaxis, xcount)
ynum = where(names eq yaxis, ycount)
if (xcount gt 0 and ycount gt 0) then begin
  if (keyword_set(filter)) then begin
    b = filter_app(data_in, filter)
    ptitle = filter
  endif else begin
    b = indgen(n_elements(data_in))
    ptitle = " "
  endelse
  if (b(0) gt -1) then begin
    print, "plotting "+xaxis+" vs "+yaxis
    xplot = data_in(b).(xnum(0))
    yplot = data_in(b).(ynum(0))
    if (keyword_set(opt)) then begin
      ; add mta_db default opts if not specified
      if (strpos(opt, "psym") eq -1) then opt = opt+",psym=2"
      if (strpos(opt, "title") eq -1) then opt = opt+",title='"+ptitle+"'"
      if (strpos(opt, "xtitle") eq -1) then opt = opt+",xtitle='"+xaxis+"'"
      if (strpos(opt, "ytitle") eq -1) then opt = opt+",ytitle='"+yaxis+"'"
      if (strpos(opt, "ystyle") eq -1) then opt = opt+",ystyle=1"
      if (strpos(opt, "xmargin") eq -1) then opt = opt+",xmargin=15"
      ;call_procedure, plot, xplot, yplot, opt
      result = execute("plot, xplot, yplot,"+opt)
    endif else begin
      plot, xplot, yplot, psym=2, $
            title = ptitle, $
            xtitle = xaxis, ytitle=yaxis, $
            ystyle=1, xmargin=15
    endelse
  endif else begin
    print, "Sorry, no data passes filter"
  endelse
endif else begin
  if (xcount eq 0) then print, "column "+xaxis+" not found"
  if (ycount eq 0) then print, "column "+yaxis+" not found"
endelse

end
