@jitter_subs
;------------------------------------------------------------------
;+
;	See jitter_subs.pro header for history
;-
;
;
;
common disp_fft_blk, x0, y0, nx, ny
;
if (n_elements(base_dir) eq 0) then base_dir = getenv('MDI_SCI160K_LOG_DIR')
if (n_elements(tfr_dir) eq 0) then tfr_dir = getenv('MDI_SCI160K_LOG_DIR')
;;if (n_elements(base_dir) eq 0) then base_dir = '/data14/jitter
if (n_elements(st_dattim) eq 0) then st_dattim = '951202_000000'
if (n_elements(en_dattim) eq 0) then en_dattim = '981231_000000'
if (n_elements(outdev) eq 0) then outdev = 'X'
if (n_elements(maxfreq) eq 0) then maxfreq = 256
if (n_elements(ss_blocks) eq 0) then ss_blocks = -1
if (keyword_set(fast_exit)) then stop
if (n_elements(qpzt) eq 0) then qpzt = 0
;qxwin = keyword_set(qxwin)
if (n_elements(qxwin) eq 0) then qxwin = 1
!y.margin = [4,4]
;
if (n_elements(gen_info) eq 0) then gen_info = {ss_blocks: '-1', $
					infil: '', $
					qbkg: 0, $
					bkg_info: '', $
					bkg_amp: fltarr(961, 6), $		;BAD - hardwired size
					yrange: [0.,0.], $
					;---- second iteration of code 
					ss_data: '', $
					ss_bkg: '', $
					bkg_infil: '', $
					xy_amp: fltarr(961, 2), $
					pzt_amp: fltarr(961, 2), $
					quse_bkg: 1, $
					ss_mask: '', $
					ss_integrate: '', $
					smooth: 0, $
					title: ''}
;
print, 'Setting output device to X'
outdev = 'X'
;
sublist = [get_item( indgen(8), /get_tit), $
	'of all 5 or 8 above (HC)']
sublist_pad = [	' ' ]
;

main_menu = ['Stop/Exit', $
	' ', $
	'List TFR files', $
	'List ISS_SUM files', $
	'To set the date/time of first file to consider', $
	'To set the date/time of the last file to consider', $
	' ', $
	'Set output device to X', $
	'Set output device to PS', $
	' ', $
	'Make summary file', $				;10
	'Re-make summary file', $
	'To Select a new data directory', $
	' ', $
	' ', $
	'Read ISS_SUM file(s)', $			;15
	'To select all blocks', $
	'To select which blocks to display', $
	'Set the maximum frequency to display', $
	' ', $
	'FFT Time/Spectra Image of ' + sublist, $	;20-29
	sublist_pad, $

	'Single/Averaged Spectra of ' +sublist, $	;30-39
	sublist_pad, $

	'Location/Amp/Phase of Peaks of ' +sublist(0:2), $	;40,41,42
	'Location/Amp/Phase of Peaks of ' +sublist(4:5), $	;43,44
	'Location/Amp/Phase of Peaks of ' +sublist(8), $		;45 (all)
	' ', $
	' ', $
	' ', $
	' ', $

	'4 Spectra (PZT X,Y Error X,Y)', $
	'Transfer Function (PZT/Err X/Y Amplitude and Phase)', $
	'Chris #1. For XY Err, make FFT image/Location plots ', $	;52
	' ', $
	' ', $

	'Make single time series summary plot', $			;55
	' ', $
	' ', $
	' ', $
	' ', $
	'4 Panel time line plot of PZT Summary Averages', $		;60
	'4 Panel time line plot of PZT Summary Std Dev', $
	'4 Panel time line plot of XY Error Summary Avg&Dev', $
	'4 Panel time line plot of Diode summary averages', $
	'4 Panel time line plot of Diode summary averages', $
	'4 Panel time line plots for the 5 above', $
	' ', $
	' ', $
	' ', $
	' ', $
	'Read raw data from TFR file', $				;70
	'To Select which raw data blocks to display', $
	'Original 512HZ PZT Time line plot', $
	'Original 512HZ XY Error time line plot', $
	' ', $
	'Set sublist #1 of menu options to be displayed', $		;75
	'Sel all menu options to be displayed', $
	' ', $
	' ', $
	' ', $
	'Create a background FFT spectra', $				;80
	'To read a background FFT spectra', $
	'To disable using background subtraction', $
	'To enable using background subtraction', $
	' ', $
	'To set the AMP yrange', $					;85
	'To reset to autoscale the AMP yrange', $
	' ', $
	' ', $
	' ', $
	'To select blocks to integrate by clicking on image', $		;90
	'To select bkg blocks to integrate by clicking on image', $
	'To re-calculate average spectra', $
	'To display XY (OR) PZT integrated spectra', $
	'To mask certain frequency of an integrated spectra', $
	'To integrate over a frequency and give total jitter', $	;95
	'To set the smooth factor', $
	'To toggle between XY ERR (OR) PZT. ', $
	'To toggle enable using BKG. ', $
	' ', $
	'X/Y Error Amplitude Spectra (Single or averaged)', $		;100
	' ', $
	'Save summary spectra to file', $
	'Read summary spectra from file', $
	' ', $
	'To set the User Title for plots', $				;105
	' ']

ss_menu1 = [indgen(20), $		;base stuff
		24, 25, 19, $		;fft time/spectra
		34, 35, 19, $		;single/averaged spectra
		43, 44, 19, $		;location/amp/phase
		52, 55, 19, $		;single page summary
		62, 63, 64, 19, $	;4 panel plots
		70, 71, 72, 73, 74, $	;raw plot stuff
		75, 76, 19, $		;sub menu selection
		80, 81, 82, 83, $	;background stuff
		indgen(n_elements(main_menu)-84)+84]		;yrange stuff and all beyond

if (n_elements(ss_menu) eq 0) then ss_menu = indgen(n_elements(main_menu))

xy_pzt_arr = ['XY ERR', 'PZT']
dis_en_arr = ['DISABLED', 'ENABLED']
last_imenu = -1
qdone = 0
while not qdone do begin
    set_plot, 'x'

    main_menu2 = main_menu
    main_menu2(97) = main_menu2(97) + '  <<< Currently: ' + xy_pzt_arr(qpzt)
    main_menu2(98) = main_menu2(98) + '  <<< Currently: ' + dis_en_arr(gen_info.quse_bkg)

    temp_menu = main_menu2(ss_menu)
    if (outdev eq 'X') then temp_menu(7) = temp_menu(7) + '  <<< Current option' $
			else temp_menu(8) = temp_menu(8) + '  <<< Current option'
    if (qxwin) then imenu = xmenu_sel(temp_menu, /one) $
		else imenu = wmenu_sel(temp_menu, /one)
    if (imenu gt 0) then if (qxwin) then imenu = ss_menu(imenu) $
				else imenu = ss_menu(imenu)

    if (imenu le 0) then qdone = 1
    if (imenu eq 2) then print, jitter_files(tfr_dir, st_dattim, en_dattim, '*.tfr')
    if (imenu eq 3) then print, jitter_files(base_dir, st_dattim, en_dattim, '*.iss_sum')
    if (imenu eq 4) then input, 'Enter the first file to consider (YYMMDD_HHMMSS)', st_dattim, st_dattim
    if (imenu eq 5) then input, 'Enter the last  file to consider (YYMMDD_HHMMSS)', en_dattim, en_dattim

    if (imenu eq 7) then outdev = 'X'
    if (imenu eq 8) then outdev = 'PS'

    if (imenu eq 10) or (imenu eq 11) then begin
	tfr_fil = jitter_files(tfr_dir, st_dattim, en_dattim, '*.tfr')
	if (qxwin) then ii = xmenu_sel(tfr_fil) $
		   else ii = wmenu_sel(tfr_fil)
	if (ii(0) ne -1) then begin
	    for i=0,n_elements(ii)-1 do begin
		infil = concat_dir(tfr_dir, tfr_fil(ii(i)))
		if (imenu eq 11) then spawn, 'rm -f ' + infil + '*.tfrx  ' + infil + '*.iss_sum'
		mk_summary, infil, outdir=base_dir
	    end
	end
    end
    if (imenu eq 12) then input, 'Enter new directory ', base_dir, base_dir

    if (imenu eq 15) then begin
	iss_fil = jitter_files(base_dir, st_dattim, en_dattim, '*.iss_sum')
	if (qxwin) then ii = xmenu_sel(iss_fil) $
		   else ii = wmenu_sel(iss_fil)
	if (ii(0) ne -1) then begin
	    infil = concat_dir(base_dir, iss_fil(ii))
	    rd_summary, infil, summary
	    break_file, infil, dsk_log, dir000, filnam
	    gen_info.infil = arr2str(filnam)
	    nnnn = n_elements(filnam)
	    if (nnnn gt 3) then gen_info.infil = filnam(0) + ' .... ' + filnam(nnnn-1)
	end
	gen_info.ss_blocks = '-1'
	gen_info.ss_data = ''
    end
    if (imenu eq 16) then gen_info.ss_blocks = '-2'
    if (imenu eq 17) then gen_info.ss_blocks = arr2str(get_blocks(summary))
    if (imenu eq 18) then input, 'Enter the max freq to display', maxfreq, maxfreq
    nn = fix(960. * maxfreq / 256.)<960>10

    ;set_plot, outdev
    if (strupcase(outdev) eq 'PS') then setps else set_plot,outdev	;MDM added 6-Nov-96
    if (imenu ge 20) and (imenu le 27) then disp_fft, summary, imenu-20, gen_info, nn=nn
    if (imenu eq 28) then for i=0,7 do disp_fft, summary, i, gen_info, nn=nn, /qpause

    if (imenu ge 30) and (imenu le 37) then plot_fft, summary, imenu-30, gen_info, nn=nn
    if (imenu eq 38) then for i=0,7 do plot_fft, summary, i, gen_info, nn=nn, /qpause

    if (imenu ge 40) and (imenu le 42) then plot_peak, summary, imenu-40, gen_info
    if (imenu ge 43) and (imenu le 44) then plot_peak, summary, imenu-40+1, gen_info
    if (imenu eq 45) then begin
	for i=0,2 do plot_peak, summary, i, gen_info, /qpause
	for i=4,5 do plot_peak, summary, i, gen_info, /qpause
    end

    if (imenu eq 50) then plot4fft, summary, gen_info, nn=nn
    if (imenu eq 51) then plot_transf, summary, gen_info, nn=nn
    if (imenu eq 52) then begin
	for i=4,5 do disp_fft, summary, i, gen_info, nn=nn, /qpause
	for i=4,5 do plot_peak, summary, i, gen_info, /qpause
    end

    if (imenu eq 55) then plot_jit1, summary, gen_info

    qpause = (imenu eq 65)
    if (imenu eq 60) or (imenu eq 65) then plot_jit4, summary, gen_info, [0,1,2,3], 'avg', qpause=qpause
    if (imenu eq 61) or (imenu eq 65) then plot_jit4, summary, gen_info, [0,1,2,3], 'dev', qpause=qpause
    if (imenu eq 62) or (imenu eq 65) then plot_jit4, summary, gen_info, [4,4,5,5], ['avg', 'dev', 'avg', 'dev'], qpause=qpause
    if (imenu eq 63) or (imenu eq 65) then plot_jit4, summary, gen_info, [10,11,12,13], 'avg', qpause=qpause
    if (imenu eq 64) or (imenu eq 65) then plot_jit4, summary, gen_info, [10,11,12,13], 'dev', qpause=qpause

    if (imenu eq 70) then begin
	tfr_fil = jitter_files(tfr_dir, st_dattim, en_dattim, '*_01.tfr')
	if (qxwin) then ii = xmenu_sel(tfr_fil, /one) $
		   else ii = wmenu_sel(tfr_fil, /one)
	if (ii(0) ne -1) then begin
	    infil2 = concat_dir(tfr_dir, strmid(tfr_fil(ii), 0, 13))
	    list_tfi, infil2, tfr_info, tfr_str_info, /quiet
	    tfr_str_info = strmid(tfr_str_info, 0, 39) + strmid(tfr_str_info, 83, 25)
	    if (qxwin) then iidsets = xmenu_sel(tfr_str_info) $
	    		else iidsets = wmenu_sel(tfr_str_info)
	    if (iidsets(0) ne -1) then begin
	        rd_and_sort, infil2, out, dep_tim, fast_tim, dsets=iidsets
	        ref_time = fmt_tim(dep_tim(0))
	        fast_tim = fast_tim - int2secarr(ref_time, '1-Jan-58')
	        timerange=0
	    end
	end
    end
    if (imenu ge 71) and (imenu le 73) and (n_elements(out) eq 0) then begin
	imenu = 9999
	tbeep, 3
	print, 'Must read the raw telemetry file before this option'
    end
    if (imenu eq 71) then begin
	if (n_elements(time1) eq 0) then time1 = fmt_tim(dep_tim(0))
	if (n_elements(time2) eq 0) then time2 = fmt_tim(dep_tim(n_elements(dep_tim)-1))
	input, 'Enter the starting date/time', time1, time1
	input, 'Enter the ending date/time  ', time2, time2
	timerange = [time1, time2]
    end
    if (imenu eq 72) then begin
	y = out.one16th.fast.pzt_arr
	tit = '512HZ: ' + get_item(indgen(3),/get_tit)
	iss_plot1, fast_tim, y, 3, 1, 3, tit, ref_time, timerange, gen_info
    end
    if (imenu eq 73) then begin
	y = out.one16th.fast.xy_err
	tit = '512HZ: ' + get_item(indgen(2)+4,/get_tit)
	iss_plot1, fast_tim, y,  2, 1, 2, tit, ref_time, timerange, gen_info
    end

    if (imenu eq 75) then ss_menu = ss_menu1
    if (imenu eq 76) then ss_menu = indgen(n_elements(main_menu))

    if (imenu eq 80) then begin
	print, 'The current selection of BLOCKS and data is what will be
	print, 'saved to the background array.  They are being re-displayed to
	print, 'confirm the time span selected is good'
	set_plot, 'x'
	for i=0,5 do disp_fft, summary, i, gen_info, nn=nn, /qpause
	yesnox, 'Do you wish to save use these as a background set? ', ans, 'Yes'
	if (ans) then begin
	    for i=0,5 do begin
		yyy = get_item(i, summary, gen_info, timrange=timrange, fid=fid)
		nblock = n_elements(yyy(0,*))
		amp = abs(yyy)
		gen_info.bkg_amp(*,i) = total(amp,2) / nblock
	    end
	    gen_info.qbkg = 1
	    gen_info.bkg_info = fid
	    outfil = concat_dir(base_dir, fid+'.bkg')
	    print, 'Saving background file into: ' + outfil
	    savegen, file=outfil, gen_info.bkg_amp
	end
    end
    if (imenu eq 81) then begin
	fids = jitter_files(base_dir, st_dattim, en_dattim, '*.bkg.genx')
	if (qxwin) then ii = xmenu_sel(fids, /one) $
		   else ii = wmenu_sel(fids, /one)
	if (ii(0) ne -1) then begin
	    bkg_fil = concat_dir(base_dir, fids(ii) + '.bkg')
	    restgen, file=bkg_fil, amp
	    gen_info.bkg_amp = amp
	    gen_info.bkg_info = fids(ii)
	    gen_info.qbkg = 1
	end
    end
    if (imenu eq 82) then gen_info.qbkg = 0
    if (imenu eq 83) then begin
	if (gen_info.bkg_info ne '') then begin
	    gen_info.qbkg = 1
	end else begin
	    tbeep, 3
	    print, 'You must read or create a background set before you can enable using it'
	end
    end

    if (imenu eq 85) then begin
	input, 'Enter the ymin', ymin, gen_info.yrange(0)
	input, 'Enter the ymax', ymax, gen_info.yrange(1)
	gen_info.yrange = [ymin, ymax]
    end
    if (imenu eq 86) then gen_info.yrange = [0,0]

    ;---- MDM started adding from here 17-Feb-95

    qcalc_avg = 0
    if (imenu eq 90) or (imenu eq 91) then begin
	if (last_imenu ge 20) and (last_imenu le 27) then begin		;last thing done was an image display
	    qdone2 = 0
	    ss = -1
	    while not (qdone2) do begin
		get_boxcorn, x00, y00, x11, y11, /device
		ss_blocks = str2arr(gen_info.ss_blocks)
		nss_disp = n_elements(ss_blocks)
		i0 = fix((y00-y0) / float(ny) * nss_disp + 0.5)
		i1 = fix((y11-y0) / float(ny) * nss_disp + 0.5)
		i0 = i0>0<(nss_disp-1)
		i1 = i1>0<(nss_disp-1)
		ss0 = ss_blocks(i0:i1)
		if (ss(0) eq -1) then ss=ss0 else ss = [ss, ss0]
		print, 'Blocks selected: ', ss
		case imenu of
		    90: gen_info.ss_data = arr2str(ss)
		    91: gen_info.ss_bkg  = arr2str(ss)
		endcase
		;
		yesnox, 'Select another portion to be appended?', ans, 'No'
		qdone2 = not ans
		qcalc_avg = 1
	    end
        end else begin
            tbeep, 3
            print, 'You must display the data in image form immediately prior to this option'
        end
    end

    if (qcalc_avg) or (imenu eq 92) then begin
	    ss = str2arr(gen_info.ss_blocks)
	    if (gen_info.ss_data ne '') then ss = str2arr(gen_info.ss_data) else gen_info.ss_data = gen_info.ss_blocks
	    print, 'Calculating a new average'
	    ave_psd, summary, XY_PSD, PZT_PSD, BLOCKS = ss
	    ;
	    if (gen_info.ss_bkg ne '') and (gen_info.quse_bkg) then begin
	        if (imenu eq 91) then begin
		    print, 'Calculating a new background'
		    ave_psd, summary, xy_bkg, pzt_bkg, blocks = str2arr(gen_info.ss_bkg)
		    gen_info.bkg_infil = arr2str(filnam)
		end
	        xy_psd  = xy_psd - xy_bkg
	        pzt_psd = pzt_psd - pzt_bkg
	    end
	    gen_info.xy_amp = xy_psd
	    gen_info.pzt_amp = pzt_psd(*,[3,0])

	    gen_info.ss_mask = ''
    end

    if (imenu ge 93) and (imenu le 95) then begin
	plot_integ, gen_info, nn=nn, qmask=(imenu eq 94), qtotal=(imenu eq 95), qpzt=qpzt
    end

    if (imenu eq 96) then begin
	input, 'Enter the smooth factor (0 to turn smoothing off) ', smoo, gen_info.smooth
	gen_info.smooth = smoo
    end

    if (imenu eq 97) then qpzt = abs(1-qpzt)
    if (imenu eq 98) then gen_info.quse_bkg = abs(1-gen_info.quse_bkg)

    if (imenu eq 100) then plot_xy_amp, summary, gen_info, nn=nn

    if (imenu eq 102) then jitt_save_gen, gen_info, summary, base_dir
    if (imenu eq 103) then begin
	sum_fil = file_list(base_dir, '*.genx')
	if (qxwin) then ii = xmenu_sel(sum_fil, /one) $
		   else ii = wmenu_sel(sum_fil, /one)
	if (ii(0) ne -1) then restgen, gen_info, summary, file=sum_fil(ii)
	;;if (ii(0) ne -1) then restgen, gen_info, file=sum_fil(ii)
    end

    if (imenu eq 105) then begin
	input, 'Enter the title (single space to make blank line)', ans, gen_info.title
	gen_info.title = ans
    end

    set_plot, 'x
    if (imenu ne 90) and (imenu ne 91) then last_imenu = imenu
end
;
!y.margin = [4,2]
end
