pro t_utplot, x0, xplot=x, xrange=xrange, xstart=xst, utbase=utbase, timerange=timerange, $
	base_time=base_time
;+
;
; NAME: t_utplot
;
;
; PURPOSE: 
;	Given the input time vector, x0, prepare the time variable, xplot, for utplot.
;	Put it in a standard form, seconds from 79/1/1, set the plot range, xrange,
;	determine the plot reference time, xstart, and decide whether utbase needs to
;	be set or changed.
;
; CATEGORY: Graphics
;
;
; CALLING SEQUENCE: t_utplot, x0, xplot=x, xrange=xrange, xstart=xst, utbase=utbase, $
;			timerange=timerange
;
;
; CALLED BY: utplot, set_utplot
;
;
; CALLS TO: anytim
;
; INPUTS:
;	x0 - time axis variable in any accepted anytim format
;	utbase - current value of utbase
;	xstart - current value of plot reference time
;	timerange - xrange specified with two fully referenced times
;	base_time - passed reference time for double precession seconds time array
; OUTPUTS:
;       xrange - plot range relative to xstart
;	utbase
;	xstart
;
; MODIFICATION HISTORY:
;	pulled from utplot and set_utplot, ras- 5-jan-94
;	xst is set to a yohkoh time format (external 7xn Int2) prior to return, ras, 25-jan-94
;
;-




;	Fully reference all times immediately to seconds from 1-jan-1979 00:00:00.000
;	First convert the time to this format and determine whether the base time is needed.  If it
;	is needed, then add it.x = anytim( x0, /utime) ;convert utime format, seconds since 79/1/1
;
x = x0
x = (anytim( x, /utime))(*)
typ = datatype( x0(0) )
dims = size( x0)

;
;Interpret double precision seconds array
;       one dimensional or scalar longwords will be interpreted as
;       double precision seconds unless they have either two or seven
;       elements
;
sec_passed = 0	;boolean variable, if set, then seconds array passed

;Looking for time in utime format, nominally double precision seconds
;	but being kind to improper calls using float or integers
;	if they don't look like the 2xn or 7xn formats.
;
if (typ eq 'DOU' or typ eq 'FLO') or $
   ( (typ eq 'INT' or typ eq 'LON') and ( (n_elements(x) eq 1) or $
         (dims(0) eq 1 and (dims(1) ne 2 and dims(1) ne 7)))) then begin
	if n_elements(base_time) eq 0 and n_elements(utbase) eq 0 then begin
		setutbase
	endif
	if n_elements(base_time) eq 0 and n_elements(utbase) ne 0 then $
		base_time = (anytim(utbase,/utime))(0)
	sec_passed = 1
endif

if (n_elements(base_time) ne 0) then xst = (anytim(base_time, /utime))(0)
;
;
;Here the time is passed fully referenced and the 
;the plot start time, xst_ut, will be set to the start of the passed
;array as has been done in the Yohkoh version of UTPLOT.  However, the utbase will then be
;set to xst_ut, and this reinforces keeping the reference time base separately
;because utbase can be changed by any outside routine.
;
if not sec_passed and (typ eq 'LON' or typ eq 'INT' or typ eq 'STR' or typ eq 'STC') then begin 

	xst = ( fcheck( base_time, x(0)))(0)
	xst = anytim(xst,/utim)                 		

	x   =  x - xst 			;x is in seconds from xst
endif
;
setut, utbase= (anytim( xst, /atime))(0)            ;utbase will be set to xst 
xst  = anytim( xst, /ex)			    ;ras, 25-jan-94, yohkoh software wants one of their time formats
xst_ut = anytim( xst, /utime)			    ;ras, 25-jan-94
;						
;
;.........................................................................

checkvar, xrange, !x.range ; if a range is passed use it, unless timerange is used

if (keyword_set(timerange)) then begin          ;MDM added 9-Apr-93
    xrange = fltarr(2)				;ras added 5-jan-94
    xrange(0) = anytim( timerange(0),/utime) - xst_ut 	;ras 4-jan-94, changed to use anytim
    xrange(1) = anytim( timerange(1),/utime) - xst_ut
;    xrange = !x.range				;ras, 5-jan-94
endif



;if !x.range wasn't set, then set xrange from x
if (xrange(0) eq 0.) and (xrange(1) eq 0.) then xrange = x

;
;
;.........................................................................
;
;	checkvar, xrange, [0.,0.]  ; if a range is passed use it, otherwise pass x


;If a two element array is passed, it is assumed to be to set limits and to
;override the limits of utstart and utend.  Otherwise use the limits imposed
;by utstart and utend, if they exist, otherwise the limits of the array passed.
;Only use if a seconds array has been passed.
;
getut, utstart=utstart, utend=utend
if n_elements(xrange) ne 2 and keyword_set(utstart) and keyword_set(utend) then begin   
   xmin=( anytim(utstart,/utime)-xst_ut)(0)
   xmax=( anytim(utend,/utime)  -xst_ut)(0)
   xrange = [xmin, xmax]
endif


end

