pro eitmapval,hdr,xin,yin
;
;PURPOSE: 
;   	Returns longitude and latitude of cursor or x,y location in an EIT carrington map
;   	from NRL.
;
;INPUTS:
;   	hdr 	FITS header from carrington map file
;
; OPTIONAL INPUTS:
;   	xin 	X-position in array (IDL coords)
;   	yin 	Y-position in array
;   	
;KEYWORDS:
;
; OUTPUTS:	based on cursor position or 2nd and 3rd arguments, prints:
;   	    	[lon,lat] in degrees -or-
;   	    	[lon,elon] where elon is arcsec above the limb at the pole
;       ZOOM - Scalar specifying the magnification of the displayed image with respect
;               to the original image.    Use, for example, if image has been
;               REBINed before display.
;       FILENAME  = name of file to where CURVAL data can be saved.
;               Data will only be saved if left or center mouse button
;               are pressed.
;   	    	
; SIDE EFFECTS:
;       X and Y values, etc., of the pixel under the cursor are constantly
;       displayed.
;       Pressing left or center mouse button prints a line of output, and
;       starts a new line.
;       Pressing right mouse button exits the procedure.
;		
;
;ROUTINES CALLED:
;    pb0r.pro
;
; AUTHOR:	Nathan Rich, NRL/I2, June 2008
;
; @(#)eitmapval.pro	1.1 06/03/08 - IDL NRL LASCO Library
;
;-
On_error,2    ;if an error occurs, return to caller
 compile_opt idl2
 
 npar = N_params()
 fileflag=0             ;True once left or middle mouse button pressed
 unzoom=keyword_set(Zoom)
 if !D.WINDOW EQ -1 and npar LT 3 then begin
        message,'ERROR - No image window active',/INF
	message,'Either display the map in a window or supply x,y pixel coords.',/info
        return
 endif

if (!D.FLAGS and 256) EQ 256 then wshow,!D.WINDOW  ;Bring active window to foreground

; Print formats and header for different astrometry,image, BSCALE combinations

 cr = string(13b)
 line0 = 'Longitude'
f0 = "($,a,f9.2,2x,f7.2, 2x, a)"
g0 = "(a,f9.2,2x,f7.2, 2x, a)"

 print,'Press left or center mouse button for new output line,'
 print,'... right mouse button to exit.'

print,line0 

IF datatype(hdr) EQ 'STC' THEN h=hdr ELSE h=fitshead2struct(hdr,/DASH2UNDERSCORE)

IF npar GT 1 THEN goto, nomouse
 LOOP: sv_err = !MOUSE.BUTTON
 !MOUSE.BUTTON = 0
 cursor,x,y,2,/DEVICE,/CHANGE
 cr_err = !MOUSE.BUTTON

 if cr_err EQ 4 then begin
    print,' '
    if fileflag then free_lun,lun
    return

 endif


  x = x>0 & y = y>0

 if unzoom then unzoom_xy,x,y,zoom=zoom

xin=x
yin=y

nomouse:


lon = xin*360./h.naxis1 
tai0=anytim2tai(h.sta_date)
tai1=anytim2tai(h.end_date)
tutc=tai2utc( (tai0 + (tai1-tai0)*xin/h.naxis1) )
solare=pb0r(tutc,/soho,/arcs)
rpix=solare[2]/h.platescl
d = yin-h.ceny_sta
value=	    	'Deg Latitude     '
IF d GE rpix THEN BEGIN
    lat = (d-rpix)*h.platescl 
    value = 	'Arcsec above limb'
ENDIF ELSE lat = asin(d/rpix)*180./!pi

print,form=f0,cr,lon,lat,value
IF npar GT 1 THEN BEGIN
    print,''
    return
ENDIF

 if (cr_err GE 1) and (cr_err LE 3) and (cr_err NE sv_err) then begin
    print,form="($,a)",string(10b)   ; print a form feed
    if keyword_set(filename) and (not fileflag) then begin      ; open file & print table header to file        get_lun,lun
        openw,lun,filename
        printf,lun,'CURVAL:   ',systime()      ;print time and date to file
    	printf,lun,line0
	fileflag=1
    endif
    if keyword_set(filename) THEN printf,lun,form=g0,'',lon,lat
endif

goto, loop

end
