
FUNCTION TP_SMH, Time

        hour = long(Time)/3600
        minute = long(Time-3600*hour)/60
        sec = Time mod 60
		sec_int=double(fix(sec))
		msec=(sec-sec_int)*1d3
		hour=hour mod 24


return, strmid(string(transpose([[hour], [minute], [sec_int], [msec]]), format = "(I2.2,':',I2.2,':',I2.2,'.', I3.3)"), 0, 8)

        END




	pro timeplot, time, data, nsum=nsum, psym=psym, symsize=symsize, $
xstyle=xstyle, ystyle=ystyle, millisec=millisec, $
diagnostic=diagnostic,color=color, xticklen=xticklen, subtitle=subtitle,	$
yticklen=yticklen, xmargin=xmargin, ymargin=ymargin, title=title, $
xtitle=xtitle, ytitle=ytitle, xthick=xthick, ythick=ythick, $
noerase=noerase, background=background, $
charsize=charsize, position=position, xticks=xticks, yticks=yticks, $
xtickformat=xtickformat, ytickformat=ytickformat, $
xtickname=xtickname, ytickname=ytickname, $
xtickv=xtickv, ytickv=ytickv, xeminor=xminor, yminor=yminor, ynozero = ynozero, $
xrange = xrange, yrange = yrange, ytype = ytype, linestyle = linestyle, $
max = max, min = min



  if N_params() LT 2 then begin
print,'timeplot, time, data, nsum=nsum, psym=psym, simsize=symsize'
print,'xstyle=xstyle, ystyle=ystyle, millisec=millisec'
print,'diagnostic=diagnostic,color=color, xticklen=xticklen, subtitle=subtitle'
print,'yticklen=yticklen, xmargin=xmargin, ymargin=ymargin, title=title'
print,'xtitle=xtitle, ytitle=ytitle, xthick=xthick, ythick=ythick'
print,'noerase=noerase, background=background'
print,'charsize=charsize, position=position, xticks=xticks, yticks=yticks'
print,'xtickformat=xtickformat, ytickformat=ytickformat'
print,'xtickname=xtickname, ytickname=ytickname'
print,'xtickv=xtickv, ytickv=ytickv, xminor=xminor, yminor=yminor, ynozero = ynozero'
print,'xrange = xrange, yrange = yrange, ytype = ytype'
              return
  endif


if n_elements(ytype) le 0 then ytype=!y.type
if n_elements(nsum) le 0 then nsum=1
if n_elements(psym) le 0 then psym=!p.psym
if n_elements(symsize) le 0 then symsize=!p.symsize
if n_elements(xstyle) le 0 then xstyle=!x.style
if n_elements(ystyle) le 0 then ystyle=!y.style
if n_elements(color) le 0 then color=!P.color
if n_elements(xticklen) le 0 then xticklen=!x.ticklen
if n_elements(yticklen) le 0 then yticklen=!y.ticklen
if n_elements(xmargin) le 0 then xmargin=!x.margin
if n_elements(ymargin) le 0 then ymargin=!y.margin
if n_elements(ytitle) le 0 then ytitle=!y.title
if n_elements(xtitle) le 0 then xtitle=!x.title
if n_elements(title) le 0 then title=!p.title
if n_elements(xthick) le 0 then xthick=!x.thick
if n_elements(ythick) le 0 then ythick=!y.thick
if n_elements(noerase) le 0 then noerase=!p.noerase
if n_elements(background) le 0 then background=!p.background
if n_elements(charsize) le 0 then charsize=!P.charsize
if n_elements(xticks) le 0 then xticks=!x.ticks
if n_elements(yticks) le 0 then yticks=!y.ticks
if n_elements(xtickv) le 0 then xtickv=!x.tickv
if n_elements(ytickv) le 0 then ytickv=!y.tickv
if n_elements(xtickformat) le 0 then xtickformat=!x.tickformat
if n_elements(ytickformat) le 0 then ytickformat=!y.tickformat
;if n_elements(xtickname) le 0 then xtickname=!x.tickname
if n_elements(ytickname) le 0 then ytickname=!y.tickname
if n_elements(xminor) le 0 then xminor=!x.minor
if n_elements(yminor) le 0 then yminor=!y.minor
if n_elements(ynozero) le 0 then ynozero=0
if n_elements(yrange) le 0 then yrange=!y.range
if n_elements(xrange) le 0 then xrange=!x.range
if n_elements(xrange) le 0 then linestyle = !p.linestyle

	if n_tags(time) gt 0 then begin
names = tag_names(time)

	if (where(names eq 'TIME'))(0) ge 0 then begin
t=time.time
day = time(0).day
	endif

	if (where(names eq 'GEN'))(0) ge 0 then begin
t=time.gen.time
day = time(0).gen.day
	endif

if !version.release ge 5 then jul = julday( 1, 1, 1979, 23,55,0)+day+0.5 $
	else jul = julday( 1, 1, 1979)+day+0.5

date = '!C'+caldatg(jul)
	endif else begin
t = time
date =''
	endelse

if n_elements(subtitle) le 0 then subtitle=date

tmax = max(t, min = tmin)

if tmax gt 2e6 and tmax lt 1e8 then t = t/1000d0

t=t-(long(t(0)/86400)*86400d0)

;if not keyword_set(universal) then t=t-24d0*3600*(t gt 12d0*3600)
;t=t-24d0*3600*(t gt 12d0*3600)
;--------------------
; net universalnosti -obichnii sluchai
if t[0] ge t[n_elements(t)-1] then universal=1 else universal=0

;print, universal

if universal eq 1 then t=t-24d0*3600*(t gt 12d0*3600)

tmin=min(t)
tmax=max(t)
xran=xrange


	if abs(xrange[0]) + abs(xrange[1]) ne 0 then begin
	if universal eq 0 then xran=xran-24d0*3600*(xran gt 12d0*3600)
;	if not keyword_set(universal) then	xran=xran-24d0*3600*(xran gt 12d0*3600)

	tmin=min(xran)
	tmax=max(xran)

	endif

tmin0=tmin
tmax0=tmax

	if xstyle eq 1 then begin
	;tmin0=time(0)
	;tmax0=time(n_elements(time)-1)
;	tmin0=t(0)
;	tmax0=t(n_elements(time)-1)

	endif

drange=tmax-tmin

if keyword_set(diagnostic) then print,'tmin0=',smh(tmin, ms = 3),'   tmax0=',smh(tmax, ms = 3),' drange0=',drange

a2:
hour=long(Tmin)/3600
minute=long(tmin-3600*hour)/60
sec=tmin mod 60

hour1=long(Tmax)/3600
minute1=long(tmax-3600*hour1)/60
sec1=tmax mod 60

if tmin lt 0 then begin ; tmin - negative time
case 1 of

	drange ge 60:begin
	;shift of boundaries
			case 1 of
		minute gt -15 : 				  tmin=hour*3600
		minute le -15 and minute gt -30 : tmin=hour*3600-30*60
		minute lt -30 and minute gt -45 : tmin=hour*3600-30*60
		minute le -45 and minute gt -60 : tmin=hour*3600-60*60
			else:
			endcase
			    end

	drange lt 60 :begin
	;shift of boundaries
			case 1 of
		sec lt 0 and sec gt -15 : 	tmin=hour*3600+minute*60
		sec le -15 and sec gt -30 : tmin=hour*3600+minute*60-30
		sec lt -30 and sec gt -45 : tmin=hour*3600+minute*60-30
		sec le -45 and sec gt -60 : tmin=hour*3600+minute*60-60
			else:
			endcase

		                      end
		        else:

 	 endcase
hour=long(Tmin)/3600
minute=long(tmin-3600*hour)/60
sec=tmin mod 60
drange=tmax-tmin

if keyword_set(diagnostic) then print,'new  ',smh(tmin, ms = 3),'  ',smh(tmax, ms = 3),'  ',DRANGE

endif
;calculated drange  with new value

     case 1 of
 	drange ge 60 and drange le 3600:begin
case 1 of
		sec gt 0 and sec lt 15 : 	tmin=hour*3600+minute*60
		sec ge 15 and sec lt 30 :	tmin=hour*3600+minute*60+30
		sec gt 30 and sec lt 45 :	tmin=hour*3600+minute*60+30
		sec ge 45 and sec lt 60 :	tmin=hour*3600+minute*60+60
else:
			endcase
			case 1 of
		sec1 gt 0 and sec lt 15 : 		tmax=hour1*3600+minute1*60
		(sec1 ge 15) and (sec1 lt 30) : tmax=hour1*3600+minute1*60+30
		(sec1 gt 30) and (sec1 lt 45) : tmax=hour1*3600+minute1*60+30
		(sec1 ge 45) and (sec1 lt 60) : tmax=hour1*3600+minute1*60+60
else:
	          endcase
		                      end

drange ge 3600:begin
	;shift of boundaries
			case 1 of
		minute ge 0 and minute lt 15 :	tmin=hour*3600
		minute ge 15 and minute lt 30 : tmin=hour*3600+30*60
		minute gt 30 and minute lt 45 : tmin=hour*3600+30*60
		minute ge 45 and minute lt 60 : tmin=hour*3600+60*60
else:
			endcase

			case 1 of
		minute1 ge 0 and minute1 lt 15 :      tmax=hour1*3600
		(minute1 ge 15) and (minute1 lt 30) : tmax=hour1*3600+30*60
		(minute1 gt 30) and (minute1 lt 45) : tmax=hour1*3600+30*60
		(minute1 ge 45) and (minute1 lt 60) : tmax=hour1*3600+60*60
else:
	          endcase

	          end

drange le (-3600):begin
	;shift of boundaries
			case 1 of
		minute le 0 and minute gt -15 : 	tmin=hour*3600
		minute le -15 and minute gt -30 : 	tmin=hour*3600-30*60
		minute lt -30 and minute gt -45 : 	tmin=hour*3600-30*60
		minute le -45 and minute gt -60 : 	tmin=hour*3600-60*60
else: tmin=tmin
			endcase
			case 1 of
		minute1 le 0 and minute1 gt -15 : 		tmax=hour1*3600
		(minute1 le -15) and (minute1 gt -30) : tmax=hour1*3600-30*60
		(minute1 lt -30) and (minute1 gt -45) : tmax=hour1*3600-30*60
		(minute1 le -45) and (minute1 gt -60) : tmax=hour1*3600-60*60
else: tmax=tmax
	          endcase
		                      end

drange le (-60) and drange ge (-3600):begin

	;shift of boundaries

			case 1 of
		sec lt 0 and sec gt -15 : 	tmin=hour*3600+minute*60
		sec le -15 and sec gt -30 : tmin=hour*3600+minute*60-30
		sec lt -30 and sec gt -45 : tmin=hour*3600+minute*60-30
		sec le -45 and sec gt -60 : tmin=hour*3600+minute*60-60

			else:
			endcase

			case 1 of
		(sec1 lt 0) and (sec1 gt -15) :	  tmax=hour1*3600+minute1*60
		(sec1 le -15) and (sec1 gt -30) : tmax=hour1*3600+minute1*60-30
		(sec1 lt -30) and (sec1 gt -45) : tmax=hour1*3600+minute1*60-30
		(sec1 le -45) and (sec1 gt -60) : tmax=hour1*3600+minute1*60-60

			else:
	        endcase

		                      end
		        else:begin
		        tmin=tmin
		        tmax=tmax
					end
 	 endcase
;===

a3:
;sdelali  sdvig
drange=tmax-tmin

tick_units=1.d0 * [10^findgen(5)*1.0d-3,10^findgen(4)*.002,10^findgen(4)*.005,$
	20.,30.,60.,60.*[2,4,5,6,10,15,20,30,60],3600.*[2.+findgen(5),8.,10.,12.],$
	86400.*[findgen(6)+1.,10.,20.,30.,60.]]

minor_units=1.d0 * [.0001,.0002,.001,.002,.01,.02, .1, .2, 1.0, 2.0, 5.0, 10., 20., 60.,$
	120.,300.,600.,3600.,7200.,14400.,3600.*[6.,12.],$
	86400.*[1.,2.,5.,10.]]

w_ok=where( (drange/tick_units lt 7) and (drange/tick_units ge 2),n_ok)

nn_ok=min(w_ok)

tick_unit = tick_units(nn_ok)
m_ok =ceil(max( abs(drange/tick_units(w_ok))))	;number of intervals

xrn=tmin

int=min(tick_units(w_ok))	; number of seconds within one interval
xrk=xrn+m_ok*int

if keyword_set(diagnostic) then begin
print,'final  ',smh(tmin, ms = 3),'  ',smh(tmax, ms = 3),'  ',DRANGE
print,'n_inter=',m_ok,'*',min(tick_units(w_ok)),' = ',m_ok*min(tick_units(w_ok))
print,'calc    ',smh(xrn, ms = 3),"  ", smh(xrk, ms = 3)
endif
;tmax0 is outside of the calculated boundary

if xrk lt tmax0 then begin

;---------
;shift of boundaries
case 1 of
	drange ge 60 and drange le 3600:	case 1 of
		sec1 ge 0 and sec1 lt 30 :	        tmax=hour1*3600+minute1*60+30
		sec1 ge 30 and sec1 lt 60 :	        tmax=hour1*3600+minute1*60+60
			else:
	          							endcase

drange gt 3600:		case 1 of
		minute1 ge 0 and minute1 lt 30 :      	   tmax=hour1*3600+30*60
		minute1 ge 30 and minute1 lt 60 :      	   tmax=hour1*3600+60*60
				else:
	          	    endcase


else:
endcase


if keyword_set(diagnostic) then print,' tmax0 > right calculate limit, make new iteration'
goto,a3
	 endif

if keyword_set(xminor) then goto,a1

wminors=where(tick_unit mod minor_units lt 1.e-5*tick_unit, count)

			if count eq 0 then xminor = 0 else begin

   		   	nminors=tick_unit/minor_units(wminors)

			index = WHERE (nminors lt 15,count)	;Use Count to get the number of nonzero elements.

				if count eq 0 then begin
				xminor=min(index)
				goto,a1
				endif

			index = WHERE (nminors lt 12,count)

			if count eq 0 then xminor=min(nminors)

			if xticks le 4 then xminor=max(nminors[where (nminors lt 15)]) else xminor=max(nminors [where (nminors lt 12)])

			endelse

if (tick_unit eq 1) or (tick_unit eq 0.1) or (tick_unit eq 0.01) then xminor=10.

a1:

if keyword_set(diagnostic) then print,'minor= ',int/xminor	;chemu ravna cena deleniya

xtickv=tmin+findgen(m_ok+1)*min(tick_units(w_ok))


;Hide marginal tick marks
;
if tmin0 gt xtickv[0] then xtickv=xtickv[1:*];!!!
if tmax0 gt xtickv[n_elements(xtickv)-1] then xtickv=xtickv[0:(n_elements(xtickv)-2)]


	if keyword_set(xstyle) and xstyle eq 1 then begin
	if tmax0 lt xtickv[n_elements(xtickv)-1] then xtickv=xtickv[0:(n_elements(xtickv)-2)]
	endif

xticks = n_elements(xtickv)-1
;----------------------------------


	IF not (keyword_set(xtickformat)) THEN BEGIN
	vxtickname = TP_SMH(xtickv+24.*(xtickv lt 0)*3600.)
	if (where(strmid(vxtickname, 6,2) ne '00'))[0] lt 0 then vxtickname = strmid(vxtickname, 0,5)
	ENDIF ELSE vxtickname = replicate('', n_elements(xtickv))


	if n_elements(xtickname) gt 0 then begin
	no_ticks = where(xtickname[0:(n_elements(xtickname) < n_elements(vxtickname)-1)] eq ' ', count)
	if count ne 0 then vxtickname[no_ticks]=' '
	endif

;---

if n_elements(position) le 0 then $

plot, t, data,  nsum=nsum, subt = subtitle, $
xst=xstyle, yst=ystyle, psym=psym, symsize=symsize, 	$
noerase=noerase, nodata=nodata, color=color, xticklen=xticklen,		$
yticklen=yticklen, xmargin=xmargin, ymargin=ymargin, title=title, 	$
xtitle=xtitle, ytitle=ytitle, xthick=xthick,		$
charsize=charsize, xticks=xticks, yticks=yticks, ythick=ythick,		$
ytickformat=ytickformat, xtickname=vxtickname, 	$
ytickname=ytickname, ynozero = ynozero, xrange = xran, $
	yrange = yrange, ytype = ytype,$
xtickv=xtickv, ytickv=ytickv, xminor=xminor, yminor=yminor, $
linestyle = linestyle, xtickf = tickf, max = max, min = min else $

plot, t, data, nsum=nsum, subt = subtitle, $
xst=xstyle, yst=ystyle, psym=psym, symsize=symsize, 	$
noerase=noerase, nodata=nodata, color=color, xticklen=xticklen,		$
yticklen=yticklen, xmargin=xmargin, ymargin=ymargin, title=title, 	$
xtitle=xtitle, ytitle=ytitle, xthick=xthick,		$
charsize=charsize, xticks=xticks, yticks=yticks, ythick=ythick,		$
ytickformat=ytickformat, xtickname=vxtickname, 	$
ytickname=ytickname,position=position, ynozero = ynozero, 		$
xtickv=xtickv, ytickv=ytickv, xminor=xminor, yminor=yminor, 		$
xrange = xran, yrange = yrange, ytype = ytype,xtickf = tickf, $
linestyle = linestyle, max = max, min = min

	end
