;+
; Project     : SOHO - CDS     
;                   
; Name        : PLOT_TREND_CHECK  
;               
; Purpose     : Plot telemetry data and return information via the cursor
;               
; Explanation : Uses fast_emon to plot the data from the file, and then invokes
;		emon itself to 'zoom in' on the data
;
; Use         : IDL> plot_trend_check [,'datafilename']
;
; Inputs      : Will ask for the parameter of interest
;
; Opt. Inputs : datafilename = the telemetry file, defaults to 
;				/disk7/yurow/cds_tm_db
;
; Outputs     : plots of the data
; 
; Opt. Outputs: None
;             
; Keywords    : none
;
; Calls       : emon, utplot, fast_emon, tm_calib
;
; Common      : none
;               
; Restrictions: none
;               
; Side effects: ?
;               
; Category    : Telemetry
;               
; Prev. Hist. : Adapted from Ron Yurow's PLOT_TREND_DATA
;
; Written     : 27 August 1997,  Eddie Breeveld (erb@mssl.ucl.ac.uk)
;               
; Modified    : v2 erb  2 Sep 97. Corrected bug when looking at
;				raw telemetry data
;
; Version     : Version 2, 2 Spetember 1997
;-            
;
	PRO PLOT_TREND_CHECK, DATAFILE
;
; Check if a string contianing the name of the telemetry database file was
; passed to us.  If no filename was passed to us, then set datafile to the
; name of a default database file
;
	IF not keyword_set(DATAFILE) THEN BEGIN
	   DATAFILE = "/disk7/yurow/cds_tm_db"
	ENDIF
;
;  Define the packet size for telemetry files
;
	packet_size = 306	; bytes
;
;  Enter the name of the telemetry parameter to be examined
;
	PARAMETER = ''
	READ, 'Enter the telemetry parameter name: ', PARAMETER
;
; Make sure that we managed to read in a telemetry point
;
	PARAMETER = trim(PARAMETER)
	IF strlen(PARAMETER) eq 0 THEN BEGIN
	   MESSAGE = "Null telemetry point.  "
           GOTO, HANDLE_ERROR
	ENDIF

;
; Use FAST_EMON to get the data from the engineering database for the the
; telemetry point in PARAMETER.
;
	FAST_EMON,    A_DATA,				$ 
                      B_DATA,				$ 
                      A_NAMES,				$ 
                      B_NAMES,				$ 
                      DATAFILE,				$ 
                      PARAMETER,			$ 
                      ARRAY = 500000L,			$
		      A_DATPOS, B_DATPOS
;
;  Check if EngA or EngB
;
	If n_elements(A_DATA) ne 0 then begin
		DATA = A_DATA
		NAME = A_NAMES(0)
		DATPOS = A_DATPOS
	endif else if n_elements(B_DATA) ne 0 then begin
		DATA = B_DATA
		NAME = B_NAMES(0)
		DATPOS = B_DATPOS
	endif else begin
		message = 'Telemetry point "' + parameter + '" not found'
		goto, handle_error
	endelse
;
; Calibrate the data
;
	CALIB  = TM_CALIB (NAME, DATA, WORDS)
;
	while !err ne 4 do begin	; test for right button
;
;  Plot the data
;
		WINDOW, 0
		PLOT, DATPOS, CALIB, TITLE = NAME + "   " + WORDS(1)	
;
;  Use the cursor utility to select the required data
;
		bell
		Print, '  Place the cursor over the region of interest and press the left button'
		Print, '  Press the right button to exit'
		cursor, xpos, ypos
;
		if !err ne 4 then begin
			bstart = (long(xpos) - 50L) > 1L
			bend = bstart + 100L
;
;  Use 'emon' to get the data required
;
			emon, e_time, e_data, e_names, $
				/batch, fname=datafile, pname=parameter, $
				bstart=bstart, bend=bend
;
;  Calibrate and plot
;
			window, 1, title='Emon data'
			e_calib = tm_calib(e_names(0), e_data(0, *), words)
			utplot, str2utc(e_time, /dmy), e_calib, ytitle=e_names(0), $
				title = NAME + "   " + words(1)
		endif
	endwhile
;
	RETURN
;
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN BEGIN
           ERRMSG = 'PLOT_TREND_CHECK: ' + MESSAGE 
        ENDIF ELSE BEGIN
	   MESSAGE, MESSAGE, /CONTINUE
        ENDELSE
;
        RETURN
	END
