pro IRIS_PREP_TEST2, description, $
	drms, files, $
	save1p5 = save1p5, mov1p5 = mov1p5, $
	makelev2 = makelev2, wavelev2 = wavelev2, $
	lev2 = lev2, showlev2 = showlev2, $
	opath = opath, pstop = pstop, _extra = extra

;+
;
; Test the /filter_fid and /filter_wave keywords being rolled out with IRIS_PREP v32
; by running IRIS_PREP with and without those keywords and checking the fiducial
; and neutral-line wavelength position in both cases.
;
; INPUT/OUTPUT:
;	description	-	Structure describing an OBS as written by IRIS_PREP_RUN; if
;					not passed in, its read from 
;					/irisa/data/prep/2014/09/15/20140915_042522/description.genx
;	drms, files	-	Metadata and filenames for the images taken during that OBS, 
;					read from SSW_JSOC_TIME2DATA. If drms and files are passed in, 
;					then no JSOC call is made.
;
; KEYWORDS:
;	/save1p5	-	(NEED TO SET FOR THE FIRST RUN OF AN OBS -- TAKES ABOUT 3 HOURS)
;					If set, then all the level 1.5 data generated by this routine
;					are saved in the OPATH. Needs to be set for the first run on 
;					an OBS; by default, the routine tries to load previously saved
;					lev1.5 data
;
;	/makelev2	-	(OPTIONAL, BUT NEED TO SET ONCE TO INCLUDE LEV2 -- TAKES 3 HOURS)
;					If set, then level 2 data are made both with and without the
;					OBSFIT keywords
;
;	/lev2		-	If set, then previously-made lev2 data are included in the png
;					comparison plots of fiducial and wavelength fitting
;	/wavelev2	-	If set, then the previously-made lev2 data are analyzed for
;					wavelength shift
;	/showlev2	-	If set, then some zoomed in lev2 images are shown in addition
;					to the plots of alignment information derived from lev2 data
;	/mov1p5		-	If set, then frames for a movie zoomed in on the fiducial 
;					and neutral line are produced.
;	opath		-	Set to the path where all the results should be saved. Defaults
;					to ~/iris/prep/YYYYMMDD_HHMMSS of the OBS being tested. Note
;					that this path and a variety of subdirectories are created
;					if needed. Probably best to leave this for the default.
;	/pstop		-	Standard debug switch
;
;-

tt0 = SYSTIME(/sec)

; Set up defaults and constants
fidsep = 269.5
pdb = IRIS_MK_POINTDB(version = pdb_ver)
img_paths = ['NUV', 'SJI_1330', 'SJI_1400', 'SJI_2796', 'SJI_2832', 'SJI_1600W', 'SJI_5000W', 'FUV']
npaths = N_ELEMENTS(img_paths)
fuvltarget = pdb.cpx1_fu2 - (pdb.cvl1_fu2 - 1392.82) / pdb.cdlt1_f2		;	We're not fitting the line that the pdb knows about
xtarget = [pdb.cpx1_nuv, pdb.cpx1_1p5, pdb.cpx1_1p5, pdb.cpx1_1p5, pdb.cpx1_1p5, pdb.cpx1_1p5, pdb.cpx1_1p5, pdb.cpx1_fu1, fuvltarget] - 1
ytarget = pdb.cpx2_1p5 - 1

if N_TAGS(description) eq 0 then begin
	PRINT, 'IRIS_PREP_TEST2 : Invalid description structure, using default...'
	RESTGEN, file = '/irisa/data/prep/2014/09/15/20140915_042522/description.genx', description
endif

; Set up output path
ttag = TIME2FILE(/sec, description.date_obs)
if N_ELEMENTS(opath) eq 0 then opath = CONCAT_DIR('~/iris/prep/', ttag)
if not FILE_EXIST(opath) then SPAWN, 'mkdir ' + opath, result, errcode
subpaths = ['figs', 'lev1p5', 'lev1p5/tmpframes', $
	'obsfit_old', 'obsfit_old/fid', 'obsfit_old/wavecorr', $
	'obsfit_new', 'obsfit_new/fid', 'obsfit_new/wavecorr', $
	'obsfit_poly', 'obsfit_poly/fid', 'obsfit_poly/wavecorr', $
	'lev2_old', 'lev2_old/xml', 'lev2_old/obsfit', $
	'lev2_old/obsfit/fid', 'lev2_old/obsfit/wavecorr', $
	'lev2_new', 'lev2_new/xml', 'lev2_new/obsfit', $
	'lev2_new/obsfit/fid', 'lev2_new/obsfit/wavecorr']
for i = 0, N_ELEMENTS(subpaths) - 1 do begin
	if not FILE_EXIST(CONCAT_DIR(opath, subpaths[i])) then $
		SPAWN, 'mkdir ' + CONCAT_DIR(opath, subpaths[i]), result, errcode
endfor

; Get metadata and filenames from the JSOC if you don't already have them
if N_ELEMENTS(drms) lt 2 or N_ELEMENTS(files) lt 2 then begin
	files = IRIS_TIME2FILES(description.date_obs, description.date_end, /level, drms)
endif

; Set up plotting
oldpmulti = !p.multi
TVLCT, rr, gg, bb, /get
PB_SET_LINE_COLOR
WDEF, 12, 800, 600, /uleft
WDEF, 11, 1200, 600, /uright
WDEF, 10, 800, 600, /umid

;++++++++++++++++++++++
; Loop through all the 
; img_paths to prep the
; data
;----------------------
tt1 = SYSTIME(/sec)
if KEYWORD_SET(save1p5) then begin
	for i = 0, npaths - 1 do begin
		thispath = img_paths[i]
		if STRMID(thispath, 1, 2) eq 'UV' then spec = 1 else spec = 0
		index = WHERE(drms.img_path eq thispath, numind)
		if numind gt 0 then begin
			tt2 = SYSTIME(/sec)
			PRINT, 'Prepping ', thispath, form = '(a12, a10)'
			READ_IRIS, files[index], hdr, dat
			
			; First pass: prep the data and log the fiducial and wavelength 
			; positions, then build an obsfits structure that can be used in the
			; second pass
			IRIS_PREP, hdr, dat, ohdr_old, odat_old, /verb, /run, /strict, $
				/shift_fid, /shift_wave, /post, /write, /update, $
				logdir = CONCAT_DIR(opath, 'obsfit_old/'), _extra = extra
			fidfiles = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_old/fid/*.txt'))
			fidfits = IRIS_PREP_TXT2STR(/fid, fidfiles)
			fidstr = IRIS_PREP_FILTER_FID(fidfits, /png, loud = 0, $
				outpath = CONCAT_DIR(opath, 'obsfit_old/fid'))
			wavefiles = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_old/wavecorr/*.txt'))
			wavefits = IRIS_PREP_TXT2STR(/wave, wavefiles)
			wavestr = IRIS_PREP_FILTER_WAVE(wavefits, /png, loud = 0, $
				outpath = CONCAT_DIR(opath, 'obsfit_old/wavecorr'))
			aiastr = CREATE_STRUCT('tstart', STRARR(1), 'chans', STRARR(2), $
				'axisnames', STRARR(2), 'paramnames', STRARR(4), $
				'numfit', LONARR(2,2), 'chisqs', DBLARR(2,2), 'paramvals', DBLARR(2,2,4), $
				'combo_chisqs', DBLARR(2), 'combo_numfit', LONARR(2), $
				'combo_paramvals', DBLARR(2,5))
			obsfits = {obs : description, fid : fidstr, wave : wavestr, aia : aiastr}
			SAVEGEN, str = obsfits, file = CONCAT_DIR(opath, 'obsfits.genx')
			PRINT, 'IRIS_PREP_TEST2: Made trending...'
			PRINT, 'Finished trending lev1.5 for ', thispath, ', elapsed time = ', SYSTIME(/sec) - tt2, $
				form = '(a30, a10, a20,f10.1)'
			
			; The next passes make use of this obsfits file
			IRIS_PREP, hdr, dat, ohdr_new, odat_new, /verb, /run, /strict, $
				/shift_fid, /shift_wave, /post, /write, /update, $
				/filter_fid, /filter_wave, $
				obsfile = CONCAT_DIR(opath, 'obsfits.genx'), $
				logdir = CONCAT_DIR(opath, 'obsfit_new/'), _extra = extra
			IRIS_PREP, hdr, dat, ohdr_poly, odat_poly, /verb, /run, /strict, $
				/shift_fid, /shift_wave, /post, /write, /update, $
				/filter_fid, /filter_wave, /poly, /nosat, $
				obsfile = CONCAT_DIR(opath, 'obsfits.genx'), $
				logdir = CONCAT_DIR(opath, 'obsfit_poly/'), _extra = extra

			SAVEGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_old.genx'), $
				str = {hdr : ohdr_old, dat : odat_old}
			SAVEGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_new.genx'), $
				str = {hdr : ohdr_new, dat : odat_new}
			SAVEGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_poly.genx'), $
				str = {hdr : ohdr_poly, dat : odat_poly}
		
			PRINT, 'IRIS_PREP_TEST2: Made lev1.5...'
			PRINT, 'Finished making lev1.5 for ', thispath, ', elapsed time = ', SYSTIME(/sec) - tt2, $
				form = '(a30, a10, a20,f10.1)'
			PRINT
			if KEYWORD_SET(pstop) then STOP

		endif
	endfor
	PRINT
	PRINT, 'IRIS_PREP_TEST2: Made lev1.5...'
	PRINT, 'Finished making all lev1.5, elapsed time = ', SYSTIME(/sec) - tt1, $
		form = '(a60, f10.1)'
	PRINT

endif

;++++++++++++++++++++++
; Make level 2 data
;----------------------
tt3 = SYSTIME(/sec)
if KEYWORD_SET(makelev2) then begin
	IRIS_LEVEL1TO2, files, l1to2log = blahblah, CONCAT_DIR(opath, 'lev2_old'), $
		xmlparent = CONCAT_DIR(opath, 'lev2_old/xml/'), /verb, /run, /strict, $
		/shift_fid, /shift_wave, /post, /write, /update, $
		obsfile = CONCAT_DIR(opath, 'obsfits.genx'), $
		logdir = CONCAT_DIR(opath, 'lev2_old/obsfit/'), _extra = extra
	PRINT, 'IRIS_PREP_TEST2: Made lev2 data without OBS fitting...'
	PRINT, ' Elapsed time = ', SYSTIME(/sec) - tt3, form = '(a20,f10.1)'
	tt4 = SYSTIME(/sec)

	IRIS_LEVEL1TO2, files, l1to2log = blahblah, CONCAT_DIR(opath, 'lev2_new'), $
		xmlparent = CONCAT_DIR(opath, 'lev2_new/xml/'), /verb, /run, /strict, $
		/shift_fid, /shift_wave, /filter_fid, /filter_wave, /update, $
		/post, /write, obsfile = CONCAT_DIR(opath, 'obsfits.genx'), $
		logdir = CONCAT_DIR(opath, 'lev2_new/obsfit/'), _extra = extra
	PRINT, 'IRIS_PREP_TEST2: Made lev2 data with OBS fitting...'
	PRINT, ' Elapsed time = ', SYSTIME(/sec) - tt4, form = '(a20,f10.1)'

	PRINT, 'Finished making lev2, elapsed time = ', SYSTIME(/sec) - tt3, $
		form = '(a60,f10.1)'
	PRINT
	if KEYWORD_SET(pstop) then STOP
endif

;++++++++++++++++++++++++++++++++++++
; Load the automated measurements of
; fiducial and wavelength position
; so they can be compared across 
; different methods
;------------------------------------
ofidfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_old/fid/*.txt'))
ofid_all = IRIS_PREP_TXT2STR(ofidfile, /fid)
nfidfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_new/fid/*.txt'))
nfid_all = IRIS_PREP_TXT2STR(nfidfile, /fid)
pfidfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_poly/fid/*.txt'))
pfid_all = IRIS_PREP_TXT2STR(pfidfile, /fid)
if KEYWORD_SET(lev2) then begin
	o2fidfile = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_old/obsfit/fid/*.txt'))
	o2fid_all = IRIS_PREP_TXT2STR(o2fidfile, /fid)
	n2fidfile = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_new/obsfit/fid/*.txt'))
	n2fid_all = IRIS_PREP_TXT2STR(n2fidfile, /fid)
	o2wavefile = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_old/obsfit/wavecorr/*.txt'))
	o2wave_all = IRIS_PREP_TXT2STR(o2wavefile, /wave)
	n2wavefile = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_new/obsfit/wavecorr/*.txt'))
	n2wave_all = IRIS_PREP_TXT2STR(n2wavefile, /wave)
endif

;+++++++++++++++++++++++++++++++++++++
; Now loop back through the img_paths
; and make a whole mess of plots
;-------------------------------------
tt5 = SYSTIME(/sec)
for i = 0, npaths - 1 do begin
	thispath = img_paths[i]
	if STRMID(thispath, 1, 2) eq 'UV' then spec = 1 else spec = 0
	index = WHERE(drms.img_path eq thispath, numind)
	if numind gt 0 then begin
		
		; Load the level 1.5 data to use in showing images
		RESTGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_old.genx'), $
			str = old1p5
		RESTGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_new.genx'), $
			str = new1p5
		RESTGEN, file = CONCAT_DIR(opath, 'lev1p5/' + thispath + '_poly.genx'), $
			str = poly1p5
		ohdr_old = old1p5.hdr
		odat_old = old1p5.dat
		old1p5 = 0
		ohdr_new = new1p5.hdr
		odat_new = new1p5.dat
		new1p5 = 0
		ohdr_poly = poly1p5.hdr
		odat_poly = poly1p5.dat
		poly1p5 = 0
		
		; Generate a set of plots showing the accuracy of the spatial (Y-axis) 
		; alignment with and without the corrections
		if thispath eq 'FUV' then thispath = ['FUVS', 'FUVL']
		for k = 0, N_ELEMENTS(thispath) - 1 do begin
			tt6 = SYSTIME(/sec)
			ofid = ofid_all[WHERE(ofid_all.type eq thispath[k])]
			nfid = nfid_all[WHERE(nfid_all.type eq thispath[k])]
			pfid = pfid_all[WHERE(pfid_all.type eq thispath[k])]
			thisytarget = ytarget / ofid[0].sumspat
			thisfidsep = fidsep / ofid[0].sumspat
			thisytarget_low = (ytarget-fidsep)/ofid[0].sumspat	; 	For image around fiducial

			bs = 8. / numind			; Histogram bin size
			nbins = CEIL(20/bs)
			if spec eq 1 then begin
				yra = 3					; plot range for offset
			endif else begin
				yra = 5
			endelse
			ogood = WHERE(ofid.y1 ne 0 and ofid.y2 ne 0, numogood)
			if numogood gt 0 then begin
				ofidhist = HISTOGRAM( ((ofid[ogood].y1 + ofid[ogood].y2)/2.)>0. - thisytarget, min = -10, bin = bs)
			endif else ofidhist = FLTARR(nbins)
			ngood = WHERE(nfid.y1 ne 0 and nfid.y2 ne 0, numngood)
			if numngood gt 0 then begin
				nfidhist = HISTOGRAM( ((nfid[ngood].y1 + nfid[ngood].y2)/2.)>0. - thisytarget, min = -10, bin = bs)
			endif else nfidhist = FLTARR(nbins)
			histx = FINDGEN(nbins) * bs - 10 + bs/2.
		
			WSET, 10
			!p.multi = 0
			PLOT, histx, nfidhist, psym = 10, chars = 1.5, xtitle = 'Offset from target [pix]', $
				ytitle = 'Frequency', yrange = [0, MAX([ofidhist, nfidhist])+1], thick = 2, $
				title = thispath[k] + ' Y-axis spatial alignment', xrange = yra * [-1, 1], /xstyle
			OPLOT, histx, ofidhist, psym = 10, col = 2
			LEGEND, pos = 12, chars = 2, col = [255, 2], ['v32', 'v31']
			PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spat_hist.png')
		
			UTPLOT, nfid.t_obs, nfid.y1+thisfidsep, yrange = thisytarget + yra * [-1,1], chars = 1.5, $
				title = thispath[k] + ' Y-axis spatial alignment', /xstyle, $
				ytitle = 'Offset from target [pix]', psym = 4, /ystyle
			OUTPLOT, nfid.t_obs, nfid.y2-thisfidsep, psym = 6
			OUTPLOT, ofid.t_obs, ofid.y1+thisfidsep, psym = 4, col = 2
			OUTPLOT, ofid.t_obs, ofid.y2-thisfidsep, psym = 6, col = 2
			OUTPLOT, [MIN(ofid.t_obs), MAX(ofid.t_obs)], thisytarget + [0,0], $
				col = 5, line = 2, thick = 2
			LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
				['v32 Y1', 'v32 Y2', 'v31 Y1', 'v31 Y2']
			PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spat_time.png')

			; Generate a set of plots showing the accuracy of the spectral (X-axis) 
			; alignment with and without the corrections
			if spec eq 1 then begin
				; For spectral channels, look at the neutral line position
				owvlfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_old/wavecorr/*.txt'))
				owvl_all = IRIS_PREP_TXT2STR(owvlfile, /wave)
				owvl = owvl_all[WHERE(owvl_all.img_path eq thispath[k])]
				nwvlfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_new/wavecorr/*.txt'))
				nwvl_all = IRIS_PREP_TXT2STR(nwvlfile, /wave)
				nwvl = nwvl_all[WHERE(nwvl_all.img_path eq thispath[k])]
				pwvlfile = FILE_SEARCH(CONCAT_DIR(opath, 'obsfit_poly/wavecorr/*.txt'))
				pwvl_all = IRIS_PREP_TXT2STR(pwvlfile, /wave)
				pwvl = pwvl_all[WHERE(pwvl_all.img_path eq thispath[k])]

				histx = FINDGEN(CEIL(20/bs)) * bs - 10 + bs/2.
				thisxtarget = xtarget[i+k]/owvl[0].sumsptrl
				owvls = [owvl.line_bot, owvl.line_mid, owvl.line_top]
				nwvls = [nwvl.line_bot, nwvl.line_mid, nwvl.line_top]
				if MAX(FINITE(owvls)) eq 0 then owvlhist = histx * 0 $
					else owvlhist = HISTOGRAM(owvls>0. - thisxtarget, min = -10, bin = bs)
				if MAX(FINITE(nwvls)) eq 0 then nwvlhist = histx * 0 $
					else nwvlhist = HISTOGRAM(nwvls>0. - thisxtarget, min = -10, bin = bs)
				
				PLOT, histx, nwvlhist, psym = 10, chars = 1.5, xtitle = 'Offset from target [pix]', $
					ytitle = 'Frequency', yrange = [0, MAX([owvlhist, nwvlhist])+1], thick = 2, $
					title = thispath[k] + ' Spectral alignment', xrange = [-5, 5], /xstyle
				OPLOT, histx, owvlhist, psym = 10, col = 2
				LEGEND, pos = 12, chars = 2, col = [255, 2], ['v32', 'v31']
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_hist.png')
		
				UTPLOT, nwvl.t_obs, nwvl.line_bot, yrange = thisxtarget + [-5,5], chars = 1.5, $
					title = thispath[k] + ' Spectral alignment', /xstyle, $
					ytitle = 'Offset from target [pix]', psym = 4, /ystyle
				OUTPLOT, nwvl.t_obs, nwvl.line_mid, psym = 6
				OUTPLOT, nwvl.t_obs, nwvl.line_top, psym = 2
				OUTPLOT, owvl.t_obs, owvl.line_bot, psym = 4, col = 2
				OUTPLOT, owvl.t_obs, owvl.line_mid, psym = 6, col = 2
				OUTPLOT, owvl.t_obs, owvl.line_mid, psym = 2, col = 2
				OUTPLOT, [MIN(owvl.t_obs), MAX(owvl.t_obs)], thisxtarget + [0,0], $
					col = 5, line = 2, thick = 2
				LEGEND, pos = 12, chars = 2, psym = [4,6,2,4,6,2], col = [255,255,255,2,2,2], $
					['v32 Bot', 'v32 Mid', 'v32 Top', 'v31 Bot', 'v31 Mid', 'v31 Top']
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time.png')
			endif else begin
				; For slit jaw channels, look at the X-axis fiducial position
				thisxtarget = xtarget[i+k]/ofid[0].sumsptrl
				ofidhist = HISTOGRAM( [ofid.x1, ofid.x2]>0. - thisxtarget, min = -10, bin = bs)
				nfidhist = HISTOGRAM( [nfid.x1, nfid.x2]>0. - thisxtarget, min = -10, bin = bs)
				histx = FINDGEN(CEIL(20/bs)) * bs - 10 + bs/2.
				
				PLOT, histx, nfidhist, psym = 10, chars = 1.5, xtitle = 'Offset from target [pix]', $
					ytitle = 'Frequency', yrange = [0, MAX([ofidhist, nfidhist])+1], thick = 2, $
					title = thispath[k] + ' X-axis spatial alignment', xrange = yra * [-1, 1], /xstyle
				OPLOT, histx, ofidhist, psym = 10, col = 2
				LEGEND, pos = 12, chars = 2, col = [255, 2], ['v32', 'v31']
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_hist.png')
		
				UTPLOT, nfid.t_obs, nfid.x1, yrange = thisxtarget + yra * [-1,1], chars = 1.5, $
					title = thispath[k] + ' X-axis spatial alignment', /xstyle, $
					ytitle = 'Offset from target [pix]', psym = 4, /ystyle
				OUTPLOT, nfid.t_obs, nfid.x2, psym = 6
				OUTPLOT, ofid.t_obs, ofid.x1, psym = 4, col = 2
				OUTPLOT, ofid.t_obs, ofid.x2, psym = 6, col = 2
				OUTPLOT, [MIN(ofid.t_obs), MAX(ofid.t_obs)], thisxtarget + [0,0], $
					col = 5, line = 2, thick = 2
				LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
					['v32 X1', 'v32 X2', 'v31 X1', 'v31 X2']
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time.png')
			endelse
		
			; Generate an image and movie showing the appearance of the data zoomed in
			; around the fiducial (and neutral line, if its a spectrum)
			if KEYWORD_SET(mov1p5) then begin
				WSET, 11
				for j = 0, numind - 1 do begin
					IRIS_LCT, ohdr_old
					oimage = IRIS_INTSCALE(odat_old[thisxtarget-40:thisxtarget+40,thisytarget_low-40:thisytarget_low+40,j], ohdr_old)
					PLOT_IMAGE, oimage, chars = 1.5, title = ohdr_old[j].date_obs + ' v31', $
						origin = [thisxtarget -40, thisytarget - 310], $
						pos = [0.05, 0.05, 0.45, 0.95]
					PB_SET_LINE_COLOR
					PLOTS, thisxtarget + [0,0], thisytarget - thisfidsep + [-40, 40], col = 5, line = 2, thick = 2
					PLOTS, thisxtarget + [-40,40], thisytarget - thisfidsep + [0,0], col = 5, line = 2, thick = 2

					IRIS_LCT, ohdr_new
					nimage = IRIS_INTSCALE(odat_new[thisxtarget-40:thisxtarget+40,thisytarget_low-40:thisytarget_low+40,j], ohdr_new)
					PLOT_IMAGE, nimage, chars = 1.5, title = ohdr_new[j].date_obs + ' v32', $
						 /noerase, origin = [thisxtarget -40, thisytarget - 310], $
						pos = [0.55, 0.05, 0.95, 0.95]
					PB_SET_LINE_COLOR
					PLOTS, thisxtarget + [0,0], thisytarget - thisfidsep + [-40, 40], col = 5, line = 2, thick = 2
					PLOTS, thisxtarget + [-40,40], thisytarget - thisfidsep + [0,0], col = 5, line = 2, thick = 2
					if j eq 0 then PB_WIN2JPG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_image.jpg')
					PB_WIN2JPG, CONCAT_DIR(opath, 'lev1p5/tmpframes/' + thispath[k] + '_' + STRING(j,form = '(i04)') + '.jpg')
				endfor	
				framebase = CONCAT_DIR(opath, 'lev1p5/tmpframes/' + thispath[k])
				movfile = CONCAT_DIR(opath, 'figs/' + thispath[k] + '_mov.mp4')
				movcmd = 'ffmpeg -framerate 24 -loglevel panic -i ' + framebase + '_%04d.jpg ' + movfile
				rmcmd = 'rm ' + framebase + '*.jpg'
				PRINT, movcmd
				PRINT, rmcmd
			endif
			
			; Generate plots comparing the corrections calculated during lev2 processing
			; with those calculated during lev1.5 (iris_prep) processing
			if KEYWORD_SET(lev2) then begin
				WSET, 12
				
				o2fid = o2fid_all[WHERE(o2fid_all.type eq thispath[k])]
				n2fid = n2fid_all[WHERE(n2fid_all.type eq thispath[k])]
				o2wvl = o2wave_all[WHERE(o2wave_all.img_path eq thispath[k])]
				n2wvl = n2wave_all[WHERE(n2wave_all.img_path eq thispath[k])]
				thisxtarget = xtarget[i+k]/ofid[0].sumsptrl
				thisytarget = ytarget/ofid[0].sumspat
			
				UTPLOT, nfid.t_obs, nfid.y1+thisfidsep, yrange = thisytarget + yra * [-1,1], chars = 1.5, $
					title = thispath[k] + ' Y-axis spatial alignment', /xstyle, $
					ytitle = 'Offset from target [pix]', psym = 4, /ystyle, symsize = 2
				OUTPLOT, nfid.t_obs, nfid.y2-thisfidsep, psym = 6, symsize = 2
				OUTPLOT, n2fid.t_obs, n2fid.y1+thisfidsep, psym = 4, col = 2
				OUTPLOT, n2fid.t_obs, n2fid.y2-thisfidsep, psym = 6, col = 2
				OUTPLOT, [MIN(n2fid.t_obs), MAX(n2fid.t_obs)], thisytarget + [0,0], $
					col = 5, line = 2, thick = 2
				LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
					['v32 l1p5 Y1', 'v32 l1p5 Y2', 'v32 l2 Y1', 'v32 l2 Y2'], symsize = [2,2,1,1]
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spat_time_newl2.png')

				UTPLOT, ofid.t_obs, ofid.y1+thisfidsep, yrange = thisytarget + yra * [-1,1], chars = 1.5, $
					title = thispath[k] + ' Y-axis spatial alignment', /xstyle, $
					ytitle = 'Offset from target [pix]', psym = 4, /ystyle, symsize = 2
				OUTPLOT, ofid.t_obs, ofid.y2-thisfidsep, psym = 6, symsize = 2
				OUTPLOT, o2fid.t_obs, o2fid.y1+thisfidsep, psym = 4, col = 2
				OUTPLOT, o2fid.t_obs, o2fid.y2-thisfidsep, psym = 6, col = 2
				OUTPLOT, [MIN(o2fid.t_obs), MAX(o2fid.t_obs)], thisytarget + [0,0], $
					col = 5, line = 2, thick = 2
				LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
					['v31 l1p5 Y1', 'v31 l1p5 Y2', 'v31 l2 Y1', 'v31 l2 Y2'], symsize = [2,2,1,1]
				PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spat_time_oldl2.png')

				if spec eq 1 then begin
					; Compare lev1.5 and lev2 using new (per-obs filtered) results
					UTPLOT, nwvl.t_obs, nwvl.line_bot, yrange = thisxtarget + [-5,5], chars = 1.5, $
						title = thispath[k] + ' Spectral alignment', /xstyle, $
						ytitle = 'Offset from target [pix]', psym = 4, /ystyle, symsize = 2
					OUTPLOT, nwvl.t_obs, nwvl.line_mid, psym = 6, symsize = 2
					OUTPLOT, nwvl.t_obs, nwvl.line_top, psym = 2, symsize = 2
					OUTPLOT, n2wvl.t_obs, n2wvl.line_bot, psym = 4, col = 2
					OUTPLOT, n2wvl.t_obs, n2wvl.line_mid, psym = 6, col = 2
					OUTPLOT, n2wvl.t_obs, n2wvl.line_top, psym = 2, col = 2
					OUTPLOT, [MIN(nwvl.t_obs), MAX(nwvl.t_obs)], thisxtarget + [0,0], $
						col = 5, line = 2, thick = 2
					LEGEND, pos = 12, chars = 2, psym = [4,6,2,4,6,2], col = [255,255,255,2,2,2], $
						['v32 l1p5 Bot', 'v32 l1p5 Mid', 'v32 l1p5 Top', $
						'v32 l2 Bot', 'v32 l2 Mid', 'v32 l2 Top'], symsize = [2,2,2,1,1,1]
					PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time_newl2.png')
					; Compare lev1.5 and lev2 using old code
					UTPLOT, owvl.t_obs, owvl.line_bot, yrange = thisxtarget + [-5,5], chars = 1.5, $
						title = thispath[k] + ' Spectral alignment', /xstyle, symsize = 2, $
						ytitle = 'Offset from target [pix]', psym = 4, /ystyle
					OUTPLOT, owvl.t_obs, owvl.line_mid, psym = 6, symsize = 2
					OUTPLOT, owvl.t_obs, owvl.line_top, psym = 2, symsize = 2
					OUTPLOT, o2wvl.t_obs, o2wvl.line_bot, psym = 4, col = 2
					OUTPLOT, o2wvl.t_obs, o2wvl.line_mid, psym = 6, col = 2
					OUTPLOT, o2wvl.t_obs, o2wvl.line_top, psym = 2, col = 2
					OUTPLOT, [MIN(nwvl.t_obs), MAX(nwvl.t_obs)], thisxtarget + [0,0], $
						col = 5, line = 2, thick = 2
					LEGEND, pos = 12, chars = 2, psym = [4,6,2,4,6,2], col = [255,255,255,2,2,2], $
						['v31 l1p5 Bot', 'v31 l1p5 Mid', 'v31 l1p5 Top', $
						'v31 l2 Bot', 'v31 l2 Mid', 'v31 l2 Top'], symsize = [2,2,2,1,1,1]
					PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time_oldl2.png')
				endif else begin
					; Compare lev1.5 and lev2 using new (per-obs filtered) results
					UTPLOT, nfid.t_obs, nfid.x1, yrange = thisxtarget + yra * [-1,1], chars = 1.5, $
						title = thispath[k] + ' X-axis spatial alignment', /xstyle, $
						ytitle = 'Offset from target [pix]', psym = 4, /ystyle, symsize = 2
					OUTPLOT, nfid.t_obs, nfid.x2, psym = 6, symsize = 2
					OUTPLOT, n2fid.t_obs, n2fid.x1, psym = 4, col = 2
					OUTPLOT, n2fid.t_obs, n2fid.x2, psym = 6, col = 2
					OUTPLOT, [MIN(n2fid.t_obs), MAX(n2fid.t_obs)], thisxtarget + [0,0], $
						col = 5, line = 2, thick = 2
					LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
						['v32 l1p5 X1', 'v32 l1p5 X2', 'v32 l2 X1', 'v32 l2 X2'], symsize = [2,2,1,1]
					PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time_newl2.png')
					; Compare lev1.5 and lev2 using old code
					UTPLOT, ofid.t_obs, ofid.x1, yrange = thisxtarget + yra * [-1,1], chars = 1.5, $
						title = thispath[k] + ' X-axis spatial alignment', /xstyle, $
						ytitle = 'Offset from target [pix]', psym = 4, /ystyle, symsize = 2
					OUTPLOT, ofid.t_obs, ofid.x2, psym = 6, symsize = 2
					OUTPLOT, o2fid.t_obs, o2fid.x1, psym = 4, col = 2
					OUTPLOT, o2fid.t_obs, o2fid.x2, psym = 6, col = 2
					OUTPLOT, [MIN(o2fid.t_obs), MAX(o2fid.t_obs)], thisxtarget + [0,0], $
						col = 5, line = 2, thick = 2
					LEGEND, pos = 12, chars = 2, psym = [4,6,4,6], col = [255, 255, 2, 2], $
						['v31 l1p5 X1', 'v31 l1p5 X2', 'v31 l2 X1', 'v31 l2 X2'], symsize = [2,2,1,1]
					PB_WIN2PNG, CONCAT_DIR(opath, 'figs/' + thispath[k] + '_spec_time_oldl2.png')
				endelse
				
			endif

			PRINT, 'IRIS_PREP_TEST2: Made a set of plots...'
			PRINT, 'Finished plotting ', thispath[k], ', elapsed time = ', SYSTIME(/sec) - tt6, $
				form = '(a20, a10, a20,f10.1)'
			PRINT
			if KEYWORD_SET(pstop) then STOP
			
		endfor
	endif
endfor

;+++++++++++++++++++++++++++++++++++++
; This section is plots level2 
; wavelength corrections with new, old
; and original level 2 files
;-------------------------------------
if KEYWORD_SET(wavelev2) then begin
	o2spfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_old/*rast*'))
	ocorr = IRIS_PREP_WAVECORR_L2(o2spfiles)
	n2spfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_new/*rast*'))
	ncorr = IRIS_PREP_WAVECORR_L2(n2spfiles)
	origpath = '/irisa/data/level2/' + STRMID(ttag, 0, 4) + '/' + STRMID(ttag, 4, 2) + $
		'/' + STRMID(ttag, 6, 2) + '/' + ttag + '_' + STRTRIM(description.obsid,2)
	origspfiles = FILE_SEARCH(CONCAT_DIR(origpath, '*rast*'))
	if origspfiles[0] ne '' then origcorr = IRIS_PREP_WAVECORR_L2(origspfiles) $
		else begin
			PRINT, 'IRIS_PREP_TEST2: Could not find original L2 data'
			origcorr = ocorr
		endelse
	
	nlim = LIMITS([origcorr.corr_nuv, ocorr.corr_nuv, ncorr.corr_nuv])
	UTPLOT, TAI2UTC(ncorr.corr_tai), ncorr.corr_nuv, yrange = nlim, chars = 1.5, $
		title = 'Lev 2 NUV Spectral correction', /xstyle, $
		ytitle = 'Offset from target [A]', psym = 4, /ystyle, symsize = 2
	OUTPLOT, ncorr.times, ncorr.corrs[*,*,0], psym = 6
	OUTPLOT, TAI2UTC(ocorr.corr_tai), ocorr.corr_nuv, col = 2, psym = 4
	OUTPLOT, ocorr.times, ocorr.corrs[*,*,0], psym = 6, col = 2
	OUTPLOT, TAI2UTC(origcorr.corr_tai), origcorr.corr_nuv, col = 6, psym = 4
	LEGEND, pos = 12, chars = 2, psym = [4,4,4], col = [255, 2, 6], $
		['v32', 'v31', 'ORIG'], symsize = [2,2,2]
	PB_WIN2PNG, CONCAT_DIR(opath, 'figs/lev2_NUV_spec.png')

	flim = LIMITS([origcorr.corr_fuv, ocorr.corr_fuv, ncorr.corr_fuv])
	UTPLOT, TAI2UTC(ncorr.corr_tai), ncorr.corr_fuv, yrange = flim, chars = 1.5, $
		title = 'Lev 2 FUV Spectral correction', /xstyle, $
		ytitle = 'Offset from target [A]', psym = 4, /ystyle, symsize = 2
	OUTPLOT, ncorr.times, ncorr.corrs[*,*,3], psym = 6
	OUTPLOT, TAI2UTC(ocorr.corr_tai), ocorr.corr_fuv, col = 2, psym = 4
	OUTPLOT, ocorr.times, ocorr.corrs[*,*,3], psym = 6, col = 2
	OUTPLOT, TAI2UTC(origcorr.corr_tai), origcorr.corr_fuv, col = 6, psym = 4
	LEGEND, pos = 12, chars = 2, psym = [4,4,4], col = [255, 2, 6], $
		['v32', 'v31', 'ORIG'], symsize = [2,2,2]
	PB_WIN2PNG, CONCAT_DIR(opath, 'figs/lev2_FUV_spec.png')
endif


;+++++++++++++++++++++++++++++++++++++
; This section is currently disabled;
; it makes movies showing zoomed in
; versions of the lev2 data for 
; comparison
;-------------------------------------
if KEYWORD_SET(show_lev2) then begin
	; Read the lev2 data that is currently in the IRIS data archive (if any)
	origpath = '/irisa/data/level2/' + STRMID(ttag, 0, 4) + '/' + STRMID(ttag, 4, 2) + $
		'/' + STRMID(ttag, 6, 2) + '/' + ttag + '_' + STRTRIM(description.obsid,2)
	origsjfiles = FILE_SEARCH(CONCAT_DIR(origpath, '*SJI*'))
	origspfiles = FILE_SEARCH(CONCAT_DIR(origpath, '*rast*'))
	if N_ELEMENTS(origsjifiles) gt 0 then begin
		READ_IRIS_L2, origsjfiles[0], g2h0, g2d0
	endif
	
	; Read lev2 data that you just wrote using the "old" version	
	o2spfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_old/*rast*'))
	o2sjfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_old/*SJI*'))
	READ_IRIS_L2, o2sjfiles[0], o2h0, o2d0
	READ_IRIS_L2, o2spfiles[0], wave = 'Si IV', o2hs, o2ds
	READ_IRIS_L2, o2spfiles[0], wave = 'C II', o2hc, o2dc
	READ_IRIS_L2, o2spfiles[0], wave = 'Mg II', o2hm, o2dm
	omx = (pdb.cvl1_nuv - o2hm[0].crval1) / o2hm[0].cdelt1	;	Pixel location of Ni II line
	omsub = o2dm[omx-50:omx+50,*,*]
	omprof = TOTAL(omsub[*,0:o2hm[0].naxis2/2,0], 1)	;	Profile of bottom half of image
	omfid = WHERE(omprof eq MIN(omprof[WHERE(FINITE(omprof))])) ; Fiducial pixel location
	oox = (pdb.cvl1_fu1 - o2hc[0].crval1) / o2hc[0].cdelt1	;	Pixel location of O I line
	oosub = o2dc[oox-50:oox+50,*,*]
	ocx = (1335.71 - o2hc[0].crval1) / o2hc[0].cdelt1	;	Pixel location of C II line
	ocsub = o2dc[ocx-50:ocx+50,*,*]	
	osx = (1393.78 - o2hs[0].crval1) / o2hs[0].cdelt1	;	Pixel location of Si IV line
	ossub = o2ds[osx-50:osx+50,*,*]	
	ojx = o2h0[0].naxis2 / 2
	ojsub0 = o2d0[ojx-50:ojx+50,*,*]
	opecdat = [oosub, ocsub, ossub, omsub/10.]
	subsize = SIZE(ossub)
	oalldat = [specdat, CONGRID(njsub0[*,*,[0,0]]/3., subsize[1], subsize[2], subsize[3]) ]

	; Read lev2 data that you just wrote using the "new" version	
	n2spfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_new/*rast*'))
	n2sjfiles = FILE_SEARCH(CONCAT_DIR(opath, 'lev2_new/*SJI*'))
	READ_IRIS_L2, n2sjfiles[0], n2h0, n2d0
	READ_IRIS_L2, n2spfiles[0], wave = 'Si IV', n2hs, n2ds
	READ_IRIS_L2, n2spfiles[0], wave = 'C II', n2hc, n2dc
	READ_IRIS_L2, n2spfiles[0], wave = 'Mg II', n2hm, n2dm
	nmx = (pdb.cvl1_nuv - n2hm[0].crval1) / n2hm[0].cdelt1	;	Pixel location of Ni II line
	nmsub = n2dm[nmx-50:nmx+50,*,*]
	nmprof = TOTAL(nmsub[*,0:n2hm[0].naxis2/2,0], 1)	;	Profile of bottom half of image
	nmfid = WHERE(nmprof eq MIN(nmprof[WHERE(FINITE(nmprof))])) ; Fiducial pixel location
	nox = (pdb.cvl1_fu1 - n2hc[0].crval1) / n2hc[0].cdelt1	;	Pixel location of O I line
	nosub = n2dc[nox-50:nox+50,*,*]
	ncx = (1335.71 - n2hc[0].crval1) / n2hc[0].cdelt1	;	Pixel location of C II line
	ncsub = n2dc[ncx-50:ncx+50,*,*]	
	nsx = (1393.78 - n2hs[0].crval1) / n2hs[0].cdelt1	;	Pixel location of Si IV line
	nssub = n2ds[nsx-50:nsx+50,*,*]	
	njx = n2h0[0].naxis2 / 2
	njsub0 = n2d0[njx-50:njx+50,*,*]
	specdat = [nosub, ncsub, nssub, nmsub/10.]
	subsize = SIZE(nssub)
	nalldat = [specdat, CONGRID(njsub0[*,*,[0,0]]/3., subsize[1], subsize[2], subsize[3]) ]

	; Use ploop to compare the fiducial region of the old and new versions
	WDEF, 13, 1200, 480, /ur
	PLOOP, ALOG10(oalldat>1), ALOG10(nalldat>1), yrange = nmfid[0] + [-40,40], min = 0, max = 1.3
	STOP
endif


PRINT, 'IRIS_PREP_TEST2: FINISHED EVERYTHING!'
PRINT, 'Elapsed time = ', SYSTIME(/sec) - tt0, $
	form = '(a60, f10.1)'
PRINT


if KEYWORD_SET(pstop) then STOP

end
