pro mdi_imagetool, date, fd=fd, hr=hr, npix=npix, xnpix=xnpix, ynpix=ynpix, roll90=roll90
;+
;NAME:
;	mdi_imagetool
;PURPOSE:
;	Front end to call IMAGETOOL (SSW SOHO tool)
;	* Sets the MDI high resolution fixed field of view
;	* Converts coordinates to corner pixels
;SAMPLE CALLING SEQUENCE:
;	mdi_imagetool, '21-Nov-96 16:00'
;OPTIONAL INPUT:
;	date	- The date/time of the observation in the future
;OPTIONAL KEYWORD INPUT:
;	fd	- If set, then the extract is for full disk pixels
;	hr	- If set, then the extract is for high resolution
;		  pixels (the default)
;	npix	- Number of pixels in the extracted field of view
;		  if the X and Y size is the same.  Default=256
;	xnpix	- E/W extract size in pixels (Default=256)
;	ynpix	- N/S extract size in pixels (Default=256)
;	roll90	- Simulate a 90 degree roll such that the west
;		  limb ends up at the top of the CCD where the
;		  north limb used to be.
;HISTORY:
;	Written Apr-96 by M.Morrison
;	20-Nov-96 (MDM) - Renamed to mdi_imagetool
;			- Documented and put on-line
;-

;setenv,'PRIVATE_DATA=/sswdb/soho/private/data/planning
;setenv,'PRIVATE_DATA=/data14/mdi_fits'

fdpix = 2.0		;arcsec/pixel
hrpix = 0.6		;arcsec/pixel

if (n_elements(date) eq 0) then date = ut_time()
if (keyword_set(npix)) then begin
    xnpix = npix
    ynpix = npix
end
if (n_elements(xnpix) eq 0) then xnpix = 256
if (n_elements(ynpix) eq 0) then ynpix = 256
if (keyword_set(fd)) then epix = fdpix else epix = hrpix	;set extract pixel size

n_pointings = 1
mk_point_stc,point, n_pointings=n_pointings

fov = replicate({x: 0.0, y: 0.0}, 5)
fov.x = [-307, 307, 307, -307, -307] + (519 - 512)*fdpix
fov.y = [307, 307, -307, -307, 307]  + (573 - 512)*fdpix

if (keyword_set(roll90)) then begin
    fov.y = [-307, 307, 307, -307, -307] + (519 - 512)*fdpix
    fov.x = [307, 307, -307, -307, 307]  + (573 - 512)*fdpix
end

point.pointings(0).point_id = 'Extract Location'
point.pointings(0).width  = xnpix * epix
point.pointings(0).height = ynpix * epix
point.date_obs = int2secarr(date,'1-jan-58')

image_tool, fov=fov, point=point, /modal

xarc = point.pointings(0).ins_x
yarc = point.pointings(0).ins_y
xfd = xarc/fdpix + 512
yfd = yarc/fdpix + 512
xhr = (xarc - (519 - 512)*fdpix )/hrpix - xnpix/2 + 512
yhr = (yarc - (573 - 512)*fdpix )/hrpix - ynpix/2 + 512
;
if (keyword_set(roll90)) then begin
    print, '*** For roll 90 (swapping x and y) ***'
    xarc = -1 * point.pointings(0).ins_y
    yarc =      point.pointings(0).ins_x
    xfd = xarc/fdpix + 512
    yfd = yarc/fdpix + 512
    xhr = (xarc + (519 - 512)*fdpix )/hrpix - xnpix/2 + 512
    yhr = (yarc - (573 - 512)*fdpix )/hrpix - ynpix/2 + 512
end
;
print, 'Projected pointing for: ' + fmt_tim(date)
print, 'FOV center in arcseconds from sun center:  ', xarc, yarc, format='(a, 2f8.2)'
print, 'FOV center in FD pixels:                   ', xfd,  yfd,  format='(a, 2f8.2)'
print, 'Lower Left of FOV in HR pixels:            ', xhr,  yhr,  format='(a, 2f8.2)'
;
if (xhr+xnpix ge 1024) then print, '*** Warning, X corner puts extract outside of FOV'
if (yhr+ynpix ge 1024) then print, '*** Warning, Y corner puts extract outside of FOV'
;
end
