;+
; NAME:
;	smei_plot_timeseries
; PURPOSE:
;	Plots SMEI timeseries from one of the points files generated
;	by the SMEI tomography program.
; CATEGORY:
; CALLING SEQUENCE:
	PRO smei_plot_timeseries, file, nr=nr, $
		nvalid_los	= nvalid_los	, $
		xrange		= xrange		, $
		refresh 	= refresh		, $
		path		= path			, $
		_extra		= _extra
; INPUTS:
;	file		scalar; type: string
;				fully-qualified file name.
;				If not specified the IDL dialog_pickfile is used.
; OPTIONAL INPUT PARAMETERS:
;	nr=nr		array; type: integer; default: 0
;				list of timeseries to be plotted
;	nvalid_los=nvalid_los
;			scalar; type: integer; default: 1
;			threshold for plotting timeseries: only timeseries with
;			more than "nvalid_los" points are displayed.
;	/refresh	if set, forces reading of new file, instead of using
;			data still in memory
;	path=path	scalar; type: string
;				directory name passed to dialog_pickfile
;				Only used if 'file' is not defined
;	xrange=xrange	array[2]; type: time structure
;				range of time axis (passed to PlotCurver)
; OPTIONAL OUTPUT PARAMETERS:
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; COMMON BLOCKS:
	common smei_plot_timeseries_common, old_file, d
; CALLS:
;	InitVar, destroyvar, IsType, flt_read, TimeGet, TimeSet, TimeUnit
;	BadValue, PlotCurve
; PROCEDURE:
;	Once read, information from the last read file is kept in memory
;	for replotting, except when /refresh is set (which forces reading
;	of a new file.
; MODIFICATION HISTORY:
;	APR-2009, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

InitVar, refresh, /key
IF refresh THEN destroyvar, file

IF IsType(file,/undefined) THEN $
	file = dialog_pickfile(path=path)

new_file = (file_search(file))[0]

IF new_file EQ '' THEN BEGIN
	message, /info, 'file does not exist, '+hide_env(file)
	RETURN
ENDIF

InitVar, old_file, ''

IF new_file NE old_file THEN BEGIN
	IF NOT flt_read(new_file,/modify_format,d) THEN BEGIN
		destroyvar, d
		RETURN
	ENDIF
ENDIF

old_file = new_file

InitVar, nvalid_los, 1

camera = round(reform(d[0,*]))
ra     = reform(d[1,*])
dec    = reform(d[2,*])
elo    = reform(d[3,*])
pa     = reform(d[4,*])

ntimes = ((size(d,/dim))[0]-6)/2
nlos   =  (size(d,/dim))[1]

message, /info, strcompress(nlos,/rem)+' timeseries with'+strcompress(ntimes)+' points per timeseries.'

tt = d[6+2*indgen(ntimes),*]	; Array(nt,nlos)
ff = d[7+2*indgen(ntimes),*]	; Array(nt,nlos)

InitVar, xrange, [ TimeGet(TimeSet(smei=min(tt)),TimeUnit(/day),/bot), TimeGet(TimeSet(smei=max(tt)),TimeUnit(/day),/eot) ]

good_ff = ff GT -9999.9
bad = where(1-good_ff)
IF bad[0] NE -1 THEN ff[bad] = BadValue(ff)

bad_los = total(good_ff,1) LT nvalid_los

message, /info, strcompress(round(total(1-bad_los)),/rem)	+ $
	' timeseries with more than'+strcompress(nvalid_los-1)+' valid points'

InitVar, nr, 0

FOR i=0,n_elements(nr)-1 DO BEGIN

	good_nr = (where(1-bad_los))[nr[i]]

	title = $
		'Camera '+strcompress(camera[good_nr])+', '+	$
		'(RA,Dec) = ('+string(ra [good_nr],format='(F6.2)')+','+string(dec[good_nr],format='(F6.2)')+'), '+ $ 
		'(Elo,PA) = ('+string(elo[good_nr],format='(F7.2)')+','+string(pa [good_nr],format='(F6.2)')+')'

	good = where( finite(ff[*,good_nr]) )

	PlotCurve, TimeSet(smei=tt[good,good_nr]), ff[good,good_nr], $
		title 	= title	, $
		xrange	= xrange, $
		_extra	= _extra


ENDFOR

RETURN  &  END
