;+
pro plotvtime,tm,vals, label, NOWINDOW=nowindow, COLOR=color, $
    DEFAULT_MARGIN=default_margin, _Extra=_extra, XY=xy
;
; $Id: plotvtime.pro,v 1.5 2020/04/27 16:41:21 nathan Exp $
; Name:     plotvtime
;
; Purpose:  Generic routine for plotting value vs. time and labeling value
;
; Inputs:   tm	    Time array for utplot
;   	    vals    what to plot vs. time
;   	    label   string to label vals with
;
; Optional input keywords:
;   	/DEFAULT_MARGIN Use default x-margins for plot
;   	/NOWINDOW   Use existing window for plot
;   	/MDY	    tm is format of MM/DD/YYYY
;   	/XY 	    tm is not time
;   	Any plot or anytim2utc keywords
;
; Outputs:  none - plots values in window
;
; Common blocks:    timeplot	
;
; WRitten by N.Rich, NRL/I2
;-
; $Log: plotvtime.pro,v $
; Revision 1.5  2020/04/27 16:41:21  nathan
; implement /XY
;
; Revision 1.4  2010/12/09 22:41:12  nathan
; make more robust
;
; Revision 1.3  2009/10/27 19:48:01  nathan
; fix COLOR keyword and anytim2utc call
;
; Revision 1.2  2009/05/14 17:29:39  nathan
; fix some things
;
; Revision 1.1  2009/04/13 20:31:45  nathan
; subroutines created for plotstereotemps.pro
;


common timeplot, keyx, keyy, clr

keyx=5
keyy=5
IF keyword_set(COLOR) THEN clr=color ELSE clr=0

ptype='time'
IF keyword_set(XY) THEN ptype='xy'
IF n_params() LT 3 THEN BEGIN
    ; see if tm omitted from input
    IF datatype(vals) EQ 'STR' THEN BEGIN
    	ptype='xy' 
	label=vals
	vals=tm
	tm=indgen(n_elements(vals))
    ENDIF ELSE BEGIN
    	; input should be time, values
	label=''
    	read,'Please enter a label to identify data values',label
    ENDELSE
ENDIF

ymin=min(vals,max=ymax)

; start plots
plotprep
IF ptype EQ 'time' THEN clear_utplot
!x.style=1
;!x.margin=[6,3]

IF keyword_set(DEFAULT_MARGIN) THEN !x.margin=[10,3] ELSE $
IF ymax Ge 100 or ymin LT .1 THEN !x.margin=[10,2] ELSE !x.margin=[6,2]
!y.margin=[5,3]

IF ~keyword_set(NOWINDOW) THEN window,0

IF ptype EQ 'time' THEN BEGIN
    ud=anytim2utc(tm, _Extra=_extra )
    utplot,ud,vals,color=clr, _Extra=_extra
ENDIF ELSE $
    plot,tm,vals,color=clr, _Extra=_extra


xyouts,keyx,keyy,strupcase(label),color=clr,/device,size=2

; increment for next plot with oplotvtime.pro
clr=3	; skip red
keyx=keyx+strlen(label)*13+10

 end
