pro focus3_trace, files, img, sharp, norm, loc, get_files=get_files, quad=quad, $
	hc=hc, pos=pos, next=next, xcen=xcen, ycen=ycen, ncr=ncr, dark_sub=dark_sub
;+
;NAME:
;	focus3_trace
;PURPOSE:
;	To analyze a single set of TRACE grid target focus data
;SAMPLE CALLING SEQUENCES:
;	focus_trace, /get_files
;	focus_trace, /get_files, /hc
;	focus_trace, /get_files, /hc, /dark_sub
;(or)
;	ff = file_list('/td02/log/rawimage', '*')
;	ss = wmenu_sel(ff)
;	ff = ff(ss)
;	focus_trace, ff
;	focus_trace, ff, next=512, pos=[-40, -20, 15, 40]
;	focus_trace, ff, dark_sub='filename'
;INPUTS:
;	ff	- files to read
;OPTIONAL KEYWORD INPUT:
;	get_files- if set, then put up a menu prompt for the files
;	hc	- make hardcopy
;	next	- extract next x next pixels to search for grid (def = 300)
;	pos	- An array with the focus position values
;	dark_sub- The file name for the dark subtraction.
;		  If using the "GET_FILES" option, it can be
;		  simply set as an integer and you will be prompted
;		  with a menu for the dark file name.
;	xcen, ycen  - Center of extracted next x next sub-image.
;		  Uses centered extract if xcen or ycen not given.
;	ncr	- size of extract centered on grid crossing (def = 100)
;	quad	- if set, use squared derivatives for sharpness, not abs()
;
;HISTORY:
;	Written 19-Sep-96 by M.Morrison
;	21-Oct-96 (MDM) - Rename from trcfocus to focus_trace
;			- Added documentation header
;	31-Oct-96 (MDM) - Gutted some code and added call to XRD_TRACE
;	16-Dec-96 (MDM) - Added DARK_SUB
;			- Added writing out info about the frame
;	25-Feb-97 (TDT)	- Derived from focus_trace to use focus3 for sharpness
;	12-Mar-97 (TDT) - Ver 1.1 in title, fixed bug (hardcopy displaying img)
;
if (keyword_set(get_files) or (n_elements(files) eq 0)) then begin
    xrd_trace, h, img, files
    if (n_elements(img) lt 10) then return
end else begin
    rd_trace_i0, files, -1, h, img
    img = img(9:1032, 0:1023, *)
end
;
if (keyword_set(dark_sub)) then begin
    if (data_type(dark_sub) eq 7) then begin
	rd_trace_i0, dark_sub, -1, hd, dark
	dark = dark(9:1032, 0:1023, *)
	dfile = dark_sub
    end else begin
	print, 'Select the dark frame'
	xrd_trace, hd, dark, dfile
	if (n_elements(dark) lt 10) then return
    end
    n = n_elements(files)
    for i=0,n-1 do img(*,*,i) = img(*,*,i) - dark
end
;

if (not keyword_set(next)) then next = 300
    next = next > 120
    if (not keyword_set(xcen)) then xcen = 512
    x0 = xcen-next/2
    x1 = x0+next-1
    if (not keyword_set(ycen)) then ycen = 512 
    y0 = ycen-next/2
    y1 = y0+next-1
    img = img(x0:x1, y0:y1, *)
n = n_elements(img(0,0,*))
nx = n_elements(img(*,0,0))
ny = n_elements(img(0,*,0))
;
break_file, files, dsk_log, dir00, filnam, ext
info = tr_decode_head(h, infil='/tsw/dbase/cal/info/mhead_focus.lst', img=img, /left)
;
if (n_elements(pos) eq 0) then pos = (findgen(n)-2)*20
;

if (not keyword_set(ncr)) then ncr = 100
ncr2 = ncr/2
imgncr=intarr(ncr,ncr,n)
for i=0,n-1 do begin
    find_grid, img(*,*,i), coord, angles, vals, coeff_arr
    print,'Grid in ',files(i),' at:  ',coord+[x0,y0]
    x1 = ((coord(0)-ncr2) > 0)
    x1 = x1 < (nx-ncr)
    y1 = ((coord(1)-ncr2) > 0)
    y1 = y1 < (ny-ncr)
    imgncr(*,*,i) = img(x1:x1+ncr-1,y1:y1+ncr-1,i)
endfor
peak3 = fltarr(3)
sharp3 = fltarr(3,n)
if (not keyword_set(quad)) then quad=0
foc3 = focus3(imgncr,sharp=sharp3,peak=peak3,/normalize,quad=quad)
print,'focus3 Internal Results'
print,'      X-Focus      Y-Focus      R-Focus'
print,foc3
print,peak3
print,' '
print,sharp3

; sharpfocus2, img, sharp, norm, fd_loc, loc, /iss

loc = {focus_area, w: ncr2, l: ncr2, leg: ncr2, $
        xcut: [ncr2,ncr2], ycut: [ncr2,ncr2], $
        strt: [0,0], stp: [ncr-1,ncr-1], img_num: n/2}

disp1focus, imgncr, sharp3(0:1,*), loc, hc=hc, tit='FOCUS3_TRACE (ver 1.1)', $ 
	files=filnam+ext, pos=pos, dfile=dfile, info=info, next=next, /noboxes, $
	xcen=xcen, ycen=ycen, ncr=ncr, quad=quad
;
if (not keyword_set(hc)) then begin
    yesnox, 'Hardcopy?', ans
    if (ans) then disp1focus, imgncr, sharp3(0:1,*), loc, tit='FOCUS3_TRACE (ver 1.1)', $
	files=filnam+ext, pos=pos, dfile=dfile, info=info, next=next,  /noboxes, /hc, $
	xcen=xcen, ycen=ycen, ncr=ncr, quad=quad
end
;
end