;
;
;
qdisp = 1
qpause = 1
qread_ada = 1
log_file = '/2p/morrison/soft/solara_db/test/tfss_check.txt'
;
if (n_elements(sav_file) eq 0) then sav_file = '/2p/morrison/soft/solara_db/data/sea_mdm3.genx
input, 'Enter "TEST_SEA" results file to use', sav_file, sav_file
;
restgen, sxtp, sxtf, fid, $
        ffiles, pfiles, dsxtf, dsxtp, flimits, plimits, $
        fnmap, map, key, time, $
        file = sav_file
;
qffi = 0 
qpfi = 0
yesnox, 'Is the output for FFI data (other option is PFI data)', qffi, 'Y'
if (qffi) then begin
    files = ffiles
    limits = flimits
    dsxt = dsxtf
end else begin
    files = pfiles
    limits = plimits
    dsxt = dsxtp
    qpfi = 1
end
;
if (n_elements(log_file) ne 0) then openw, lunlog, log_file, /get_lun		;, /append
printf, lunlog, 'TFSS_CHECK
printf, lunlog, 'Program Run: ', !stime
printf, lunlog, '  '
printf, lunlog, ' TFSS x,y         Shift         xCen   yCen   Radius   Err   Err2
printf, lunlog, '    Arcmin       1x1 Pix               1x1 pix
printf, lunlog, '  '
;
last_infil2 = 'XX'			;ADA input file name (to avoid reading roadmap unnecessarily)
n = n_elements(dsxt)
for i=15,n-1 do begin

    istfil = where((limits(*,0) le i) and (limits(*,1) ge i))
    infil = ffiles(istfil(0))
    dset = dsxt(i)

    temp = gt_shape_cmd(sxtf(i),/x)
    if (temp(0) gt 512) then goto, skip		;MDM afterthought
    temp = gt_filta(sxtf(i))
    if (temp(0) ne 2) then goto, skip         ;MDM afterthought - only look for narrow band data
    temp = mask(sxtf(i).imgparam, 2, 2)
    if (temp(0) eq 1) then goto, skip		;skip low-8

    if (infil eq '') then goto, skip				;MDM afterthought

    rd_sda, infil, dset, index, data0, roadmap
    info = get_info2(roadmap(dset))

    print, 'Image # ', i, ' of ', n-1, '   ', fmt_tim(index.gen)
    ;
    ;---- Optionally change the summation size
    ;
    data = change_res(data0, index)		;make them all 2x2 (default)
					;TODO - change DN level for summation mode (?)
    data = sxt_decomp(data)
    if (qdisp) then tvscl, data
    ;
    ;---- Optionally read the ADA file to get the pointing information
    ;
    if (qread_ada) then begin
        break_file, infil, dsk_log, dir, filnam, ext
        infil2 = dsk_log + dir + 'ada' + strmid(filnam, 3, 6) + ext
        if (infil2 ne last_infil2) then rd_roadmap, infil2, ada_roadmap
        last_infil2 = infil2
        dummy = min( abs(int2secarr(ada_roadmap, index.gen)) )
	ist = (!c - 12)>0
	ien = (ist+ 24)<(n_elements(ada_roadmap)-1)
	ada_ss = indgen(ien-ist+1)+ist
        rd_ada, infil2, ada_ss, ada_index, ada_data

	tfss_x = reform(ada_data.tfss(0,*), n_elements(ada_index)*4)
	tfss_y = reform(ada_data.tfss(1,*), n_elements(ada_index)*4)
	tfss_x = unsign( mask(tfss_x, 4, 12), 12) * .00054*60	;arcminutes
	tfss_y = unsign( mask(tfss_y, 4, 12), 12) * .00054*60
	tfss_x = smooth(tfss_x, 25)
	tfss_y = smooth(tfss_y, 25)

	tfss_x = total( tfss_x(25:74) ) /50
	tfss_y = total( tfss_y(25:74) ) /50

	;TODO - fix so that checks for time discontinuity and not select that data
	if (n_elements(ada_index) ne 25) then print,'ADA did not get 25 records'
    end
    ;
	;---- Get X,Y from TFSS (Hugh method)
							;Reference points from 12-Nov-91 around 11:48
	x0_tfss = 67.59		& x0_ccd = 496.991	;67.25		499.306		after move
	y0_tfss = 58.79		& y0_ccd = 563.329	;60.83		610.961
							;tfss is in arcminutes, ccd is in 1x1 pixels

	;offset in arcmin from 512,512 - somewhat arbitrary
	x = x0_ccd - (tfss_x - x0_tfss)*60./2.45		;x center of sun (use - sign since reversed image)
	y = y0_ccd + (tfss_y - y0_tfss)*60./2.45

	x_off = (512-x)/2.	;convert to 2x2 pixels
	y_off = (512-y)/2.

	;----- Do limb fit before translation

	go_find_limb, data, index, limb_out1

	;----- Peform the translation

	data = translate(data, [x_off, y_off], /interp)

	go_find_limb, data, index, limb_out2

	fmt1 = '(2f10.4, 2f10.4, 5f10.3, 3x, a, 2x, a)
	fmt2 = '(20x, 20x, 5f10.3)
	fmt3 = '(20x, 20x, 2f10.3, 35x, a)
	str1 = string(tfss_x, tfss_y, x_off*2, y_off*2, limb_out1(1:5), fmt_tim(index), gt_res(index,/str), format=fmt1)
	str2 = string(                             limb_out2(1:5), format=fmt2)
	str3 = string(limb_out2(1:2) - limb_out1(1:2), 'Diff between fits (before/after)', format=fmt3)
	str4 = string(limb_out2(1:2) - 512,            'Diff from 512,512', format=fmt3)

	print, str1
	print, str2
	print, str3
	print, str4
	printf, lunlog, str1
	printf, lunlog, str2
	printf, lunlog, str3
	printf, lunlog, str4

    skip:
endfor
;

end
