pro new_hkplot, hk, yrange=yrange00, nover=nover, timerange=timerange, $
		codelist=codelist, tit=tit, qdebug=qdebug, qlinestyle=qlinestyle, $
		notyminmax=notyminmax, median_pm=median_pm, sigma=sigma, $
		label=label0,every=every,$
		pm_filter=pm_filter, filter1d=filter1d, last_tim_lab=last_tim_lab
;
;HISTORY:
;	Written Jan-95 by M.Morrison
;	 9-Feb-95 (MDM) - Added /NOTYMINMAX
;	12-Jun-95 (MDM) - Added SIGMA option (similar to median_pm)
;	21-Nov-95 (MDM) - Changed the conditional for setting yrange
;V2.0	21-Nov-95 (MDM) - Added LABEL option
;       27-Feb-96 (DBG) - Added EVERY option which plots on the bottom
;                         the number of data points being used
;V2.1	 6-Jun-96 (MDM) - Added setting the plot title as the label if
;			  there is just a single item being plotted
;V2.2	10-Jun-96 (MDM) - Added protection when "codelist" is passed in
;			  but there is no data for that item
;V2.21	13-Jun-96 (MDM) - Re-added 27-Feb modification
;V2.22	 2-Jan-97 (MDM) - Mod so that multiple plots labels work 
;			  correctly when forcing yrange
;V2.23	 6-Jan-97 (MDM) - Corrected error introduced in V2.22 (undefined variable)
;V2.24	 7-Jan-97 (MDM) - Fixed V2.23 (typo)
;V2.25	 2-Apr-98 (MDM) - Added pm_filter option
;			- Changed yrange logic to have the filtering
;			  options even if not passing "codelist"
;V2.26	 2-Apr-98 (MDM) - Corrected yrange logic to do the filtering option
;			  on the full set of mnemonics to be plotted (not
;			  just the last)
;V2.27	 2-Apr-98 (MDM) - Corrected bug in plotting of timerange when data is
;			  available from more than one packet (different time
;			  vector).
;V2.28	16-Apr-98 (MDM) - Added "filter_1d" option
;			- Added "last_tim_lab" option
;V2.29	22-Sep-98 (RIB) - Change date plottime to explicit string

progver = 'NEW_HKPLOT  Ver 2.29

if (!d.name eq 'PS') then device,/land

tags = tag_names(hk)
ntags = n_elements(tags)
;
ntot_mnem = 0
for i=0,ntags-1 do ntot_mnem = ntot_mnem + n_elements(hk.(i).mnem)
if (n_elements(codelist) ne 0) then ntot_mnem = n_elements(codelist)
;
if (n_elements(yrange00) eq 2) then yrange = yrange00
if (not keyword_set(yrange)) then begin
	print, 'Auto Y ranging...'
	qfirst = 1
	for i=0,ntags-1 do begin
	    for j=0,n_elements(hk.(i).mnem)-1 do begin
		qplot = 1
		if (n_elements(codelist) ne 0) then dummy = where([codelist] eq hk.(i).code(j), qplot)
		if (qplot) then begin
		    y = hk.(i).value(*,j)
		    labtit = string(hk.(i).mnem(j)+'    ', hk.(i).descr(j), format='(a12,a)')
		    yrange0 = [min(y), max(y)]
		    case 1 of
			keyword_set(notyminmax): begin
				    ss = where((y ne min(y)) and (y ne max(y)))
				    if (ss(0) ne -1) then yrange0 = [min(y(ss)), max(y(ss))]
				end
			keyword_set(pm_filter): begin
				    ss = where((y gt -1*pm_filter) and (y le pm_filter))
				    if (ss(0) ne -1) then yrange0 = [min(y(ss)), max(y(ss))]
				end
			keyword_set(filter1d): begin
				    junk = filter_1d(y, ix_good=ss)
				    if (ss(0) ne -1) then yrange0 = [min(y(ss)), max(y(ss))]
				end
			keyword_set(median_pm): begin
				    med = median(y)
				    yrange0 = [med-median_pm, med+median_pm]
				end
			keyword_set(sigma): begin
				    idev = stdev(y, iavg)
				    yrange0 = [-1,1]*idev*sigma + iavg
				end
			else: 
		    endcase
		    if (qfirst) then begin
			yrange = yrange0
			qfirst = 0
		    end else begin
			yrange(0) = min([yrange(0), yrange0(0)])
			yrange(1) = max([yrange(1), yrange0(1)])
		    end
		    if (keyword_set(qdebug)) then print, min(y), max(y), yrange
		end
	    end
	end
end

if (n_elements(yrange) eq 0) then begin
    print, 'NEW_HKPLOT: Cannot find the information you requested 
    print, '            in the structure passed in.   Returning....'
    return
end
;
if (n_elements(codelist) ne 0) then begin 
  for i=0,ntags-1 do begin	;MDM added so that multiple plots work correctly when forcing yrange
    for j=0,n_elements(hk.(i).mnem)-1 do begin
	dummy = where([codelist] eq hk.(i).code(j), qplot)
	if (qplot) then labtit = string(hk.(i).mnem(j)+'    ', hk.(i).descr(j), format='(a12,a)')
    end
  end
end
;
if (yrange(0) eq yrange(1)) and (yrange(0) ne 0) then yrange = yrange(0) + [-0.05,0.05]*yrange(0)

label = keyword_set(label0)
if (keyword_set(label)) and (ntot_mnem eq 1) and (not keyword_set(tit)) then begin
    if (keyword_set(labtit)) then tit = labtit $
			else tit = string(hk.(0).mnem(0)+'    ', hk.(0).descr(0), format='(a12,a)')
    label = 0
end

if (keyword_set(last_tim_lab)) then begin
    fmt_timer, hk.(0).daytime, t1, t2, /noprint
    xtit = 'Last Data: ' + t2
end
ilin = 0
qfirst = 1
for i=0,ntags-1 do begin
    for j=0,n_elements(hk.(i).mnem)-1 do begin
	qplot = 1
	if (n_elements(codelist) ne 0) then dummy = where([codelist] eq hk.(i).code(j), qplot)
	if (qplot) then begin
	    if (qfirst) then begin
		if(yrange(0) eq yrange(1)) then yrange(1) =yrange(1)+0.01 ; Added range modifier 18-Nov-98 CED (prevents crashing when no data are present)
		utplot, hk.(i).daytime, hk.(i).value(*,0), yrange=yrange, $
				/nodata, timerange=timerange, /ynozero, tit=tit, /xstyle, $
				xtit=xtit
		qfirst = 0
	    end
	    ;
	    if (keyword_set(qlinestyle)) then linestyle = ilin mod 5
	    outplot, hk.(i).daytime, hk.(i).value(*,j), linestyle=linestyle
	    if (keyword_set(label)) then begin		;MDM added 21-Nov-95
		labtit = string(hk.(i).mnem(j)+'       ', hk.(i).descr(j), format='(a12,a)')
		linelabel, .02, 1-.02*(ilin+1), labtit, linestyle=linestyle, size=(3./((!p.multi(1)*!p.multi(2))>1))<1
	    end
	    ilin = ilin + 1
	end
    end
end

if n_elements(every) ne 0 then begin
    if every eq 1 then infostr='VALUE' else infostr=every+' VALUES'
    plottime,.25,0,'PLOTTING EVERY '+infostr
endif

if (not keyword_set(nover)) then begin
    plottime, 0.01, 0, progver
    plottime, 0.5, 0, 'Plot Made ' + strmid(!stime,0,17)
    pprint
end
;
end
