;+
; NAME        : EIT_SUB_MAN_INPUT
;
; PURPOSE     : User enters coordinates to place a mark.
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGUMENTS   :	none
;
; KEYWORDS    : none
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  1997 Dec, Elaine Einfalt (HSTX)
; 		2003/07/21 _ Einfalt - Spacecraft antenna problem result
;				 in spending part of the year rolled 180
;				 Images are still displayed as taken, 
;				 but Arcsec and Helio "X" need to be adjusted.
;		2004/06/14 - einfalt - changed a call from anytim to anytim2utc
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

;===================================================================
; Little routine to keep the user from messing up the code with bad input
;====

function eit_sub_io_tester, input, fixit=fixit, longit=longit, floatit=floatit

   ; Convert the user's input string to the required data type
   ; On IO errors, returns a 0 of the requested type

TRY_AGAIN: on_ioerror, IO_PROB

    case 1 of 
     keyword_set(fixit)   : result = fix(input)
     keyword_set(longit)  : result = long(input)
     keyword_set(floatit) : result = float(input)
     else: result = 0
    endcase

on_ioerror, null

    if (0) then begin			; can only get in clause on an error
IO_PROB:  input = 0			; force it to be some kind of number
          goto, TRY_AGAIN
    endif

return, result
end

;=================================================================

pro EIT_SUB_MAN_EV, event

 widget_control, event.id,  get_uvalue=input	; get the UVALUE of the event 
						;     generated by the user

; widget_control, event.top, get_uvalue=substruc	; get pointer from UVALUE
 widget_control, event.top, get_uvalue=sub_man_struc	; get pointer from UVALUE

 
 if input eq 'CANCEL' then begin   ; If it is a CANCEL button then just exit now
    widget_control, event.top, /destroy 	; destroy manual input widget
    return					; back to subfield tool
 endif


 ;
 ; Depending on the widget used for the input, convert units to arcsecond
 ; The data plot unit format in image window is in arcsecs
 ;

 case input of
	'ARC_INPUT' : begin			; no conversions needed

 			; Read whatever the user entered

 			widget_control, event.id, get_value=value
 			if value(0) eq '' then return   ; leave window up

   			coord = str_sep(value(0),',')	; read users input

   			if n_elements(coord) eq 2 then begin	

			   x_coord = eit_sub_io_tester(coord(0), /fixit) 
		           y_coord = eit_sub_io_tester(coord(1), /fixit) 
		           widget_control, event.id, set_value= $
				strtrim(x_coord,2) + ',' + strtrim(y_coord,2) 

			endif else return	; only 1 coord entered


    			if sub_man_struc.antenna_roll eq 180. then begin
    			; If we are rolled 180 then inputs are inverse
			; 2003/07/21 - einfalt
       				x_coord = -(x_coord)
       				y_coord = -(y_coord)
    			endif


		      end

	'HEL_INPUT' : begin			; convert from helio to arcsecs

 			; Read whatever the user entered

 			widget_control, event.id, get_value=value
 			if value(0) eq '' then return   ; leave window up

   			coord = str_sep(value(0),',')	; read users input

   			if n_elements(coord) eq 2 then begin	

			   ns = strupcase(coord(0))	; no/so coord, w/letter
			   
			   n_pos = strpos(ns,'N')	; is there a north
			   s_pos = strpos(ns,'S')	; is there a south
			   if n_pos ne -1 then begin	          ; was a north
			      y_hel = strmid(ns,n_pos+1,9) 
			      ns = 'N' + strtrim(eit_sub_io_tester(y_hel, /fixit),2)

			      if sub_man_struc.antenna_roll ne 180. then $
			        n_s = 'N' + strtrim(eit_sub_io_tester(y_hel,/fixit),2) $
			      else $	; case of 180 antenna rolled 03/7/23
			        n_s = 'S' + strtrim(eit_sub_io_tester(y_hel,/fixit),2) 


			   endif else if s_pos ne -1 then begin	  ;was a south
			      y_hel = strmid(ns,s_pos+1,9) 
			      ns = 'S' + strtrim(eit_sub_io_tester(y_hel, /fixit),2)

			      if sub_man_struc.antenna_roll ne 180. then $
			        n_s = 'S' + strtrim(eit_sub_io_tester(y_hel, /fixit),2) $
			      else $	; case of 180 antenna rolled 03/7/23
			        n_s = 'N' + strtrim(eit_sub_io_tester(y_hel, /fixit),2)

			   endif else return		; no north/south

			   ew = strupcase(coord(1))	; E/W coord, w/letter
			   
			   e_pos = strpos(ew,'E')	; is there an east
			   w_pos = strpos(ew,'W')	; is there a west
			   if e_pos ne -1 then begin	          ; was an east
			      x_hel = strmid(ew,e_pos+1,9) 
			      ew = 'E' + strtrim(eit_sub_io_tester(x_hel, /fixit),2)

			      if sub_man_struc.antenna_roll ne 180. then $
			        e_w = 'E' + strtrim(eit_sub_io_tester(x_hel, /fixit),2) $
			      else $	; case of 180 antenna rolled 03/7/23
			        e_w = 'W' + strtrim(eit_sub_io_tester(x_hel, /fixit),2)

			   endif else if w_pos ne -1 then begin	  ; was a west
			      x_hel = strmid(ew,w_pos+1,9) 
			      ew = 'W' + strtrim(eit_sub_io_tester(x_hel, /fixit),2)

			      if sub_man_struc.antenna_roll ne 180. then $
			        e_w = 'W' + strtrim(eit_sub_io_tester(x_hel, /fixit),2) $
			      else $	; case of 180 antenna rolled 03/7/23
			        e_w = 'E' + strtrim(eit_sub_io_tester(x_hel, /fixit),2)

			   endif else return			 ; no east/west


		           widget_control, event.id, set_value= ns+','+ew


			   ; If rolled 180 then need to make B angle code 
			   ; think upside down. So subtract 6 months to date
			   ; 2003/07/21 - einfalt
			   date = sub_man_struc.date		; current format yyyy-mm-dd

			   if sub_man_struc.antenna_roll eq 180. then begin

    				six_months = anytim2utc(date)
    				six_months.mjd = six_months.mjd-(365/2)	; 6 mo. ago

				; format yyyy/mm/dd 
    				date = anytim2utc(six_months, /ecs,/date)


				print,'%EIT_SUB_MAN_INPUT, S/C rolled 180 '+ $
					'so using date ' + date + $
					' to simulate B angle'
			   endif

  			   arcmin = hel2arcmin(n_s, e_w, date=date, error=error)
  			   arcsec = arcmin * 60.

			   x_coord = arcsec(0)
			   y_coord = arcsec(1)

			endif else return		; didnt have 2 input values
 
		      end

	'CAM_X1' :   begin
			sub_man_struc.x1 = event.index
  			if sub_man_struc.x1 gt sub_man_struc.x2 then begin
      			   sub_man_struc.x2 = sub_man_struc.x1 
			   widget_control, sub_man_struc.cam_ro_id(2), $
					   set_droplist_select=sub_man_struc.x2 
			endif
		     end
	'CAM_Y1' :   begin
			sub_man_struc.y1 = event.index
  			if sub_man_struc.y1 gt sub_man_struc.y2 then begin
      			   sub_man_struc.y2 = sub_man_struc.y1 
			   widget_control, sub_man_struc.cam_ro_id(3), $
					   set_droplist_select=sub_man_struc.y2 
			endif
		     end
	'CAM_X2' :   begin
			sub_man_struc.x2 = event.index
  			if sub_man_struc.x2 lt sub_man_struc.x1 then begin
      			   sub_man_struc.x1 = sub_man_struc.x2 
			   widget_control, sub_man_struc.cam_ro_id(0), $
					   set_droplist_select=sub_man_struc.x1 
			endif
		     end
	'CAM_Y2' :   begin
			sub_man_struc.y2 = event.index
  			if sub_man_struc.y2 lt sub_man_struc.y1 then begin
      			   sub_man_struc.y1 = sub_man_struc.y2 
			   widget_control, sub_man_struc.cam_ro_id(1), $
					   set_droplist_select=sub_man_struc.y1 
			endif
		     end

	'LOC_READY': begin
		    ;
		    ; Got the coords, so draw the LOCS box
		    ;

		    EIT_SUB_UTIL_RO, cam_ro=[sub_man_struc.x1,   $  ; Port B
				     sub_man_struc.y1,   $
				     sub_man_struc.x2,   $
				     sub_man_struc.y2],  $ 
				     pix_per=sub_man_struc.pix_per

		    end

	else : 	print, '%EIT_SUB_MAN_ev, IMPOSSIBLE CASE' 

 endcase

 wset, sub_man_struc.draw_index

 if input eq 'ARC_INPUT' or input eq 'HEL_INPUT' then begin
    ;
    ; Got the coords, so draw an "X" at that location
    ;
   		
    ; Draw "X" at arcsec position (data plotting is in arcsecs)
   
    col=EIT_SOHO_PLOT_COLORS(5)			; draw it in yellow
   
    oplot, [X_coord-50,X_coord+50], [Y_coord-50,Y_coord+50], thick=3, color=col
    oplot, [X_coord-50,X_coord+50], [Y_coord+50,Y_coord-50], thick=3, color=col

 endif 

 widget_control, event.top, set_uvalue=sub_man_struc
  
return
end

;------------------------------------------------------------------------

pro EIT_SUB_MAN_INPUT, substruc=substruc, arc=arc, helio=helio, locs=locs

; TEMPORARILY REMOVE
; if XREGISTERED('EIT_SUB_MANUAL') then return

 if keyword_set(arc) then begin

    enter_base = widget_base(title='Arcsec Manual Input', /column, $
					xpad=50,ypad=50)
    enter_lab  = widget_label(enter_base, $
				  value='Enter your ARCSECs coordinates')
    enter_lab  = widget_label(enter_base, $
				  value='Format:   +/-XXX, +/-YYY')
    enter_lab  = widget_label(enter_base, $
				  value='Then press return')
    enter_txt  = widget_text(enter_base, /editable, uvalue='ARC_INPUT')
    arc_cancel     = widget_button(enter_base, value='Cancel', $
						  uvalue='CANCEL')

    widget_control, enter_base, /realize

    cam_ro_id= intarr(4)
    x1=0
    y1=0
    x2=31
    y2=31 

 endif

 if keyword_set(helio) then begin

    enter_base = widget_base(title='Helio Manual Input', /column, $
					xpad=50,ypad=50)
    enter_lab  = widget_label(enter_base, $
				  value='Enter your HELIO coordinates')
    enter_lab  = widget_label(enter_base, $
				  value='Format:   N(s)XXX , E(w)XXX ')
    enter_lab  = widget_label(enter_base, $
				  value='Then press return')
    enter_txt  = widget_text(enter_base, /editable, uvalue='HEL_INPUT')
    hel_cancel     = widget_button(enter_base, value='Cancel', $
						  uvalue='CANCEL')

    widget_control, enter_base, /realize

    cam_ro_id= lonarr(4)
    x1=0
    y1=0
    x2=31
    y2=31 

 endif

 if keyword_set(locs) then begin

    enter_base = widget_base(title='LOCS Manual Input', /column, $
					xpad=50,ypad=50)
    enter_lab  = widget_label(enter_base, $
				  value='Enter your LOCS coordinates')

    cam_blocks_p1_row = strtrim(indgen(32)*32+1,2)
    cam_blocks_p1_col = strtrim(indgen(32)*32+20,2)
    cam_blocks_p2_row = strtrim(indgen(32)*32+32,2)
    cam_blocks_p2_col = strtrim(indgen(32)*32+32+19,2)


    ro_fields = widget_base(enter_base, /row, space=0)

    cam_ro_id = lonarr(4)
    cam_ro_id(0) = widget_droplist(ro_fields,  value=cam_blocks_p1_row, $
                                                        UVALUE='CAM_X1')
    cam_ro_id(1) = widget_droplist(ro_fields,  value=cam_blocks_p1_col, $
                                                        uvalue='CAM_Y1')

    cam_ro_id(2) = widget_droplist(ro_fields, value=cam_blocks_p2_row, $
                                                        uvalue='CAM_X2')
    cam_ro_id(3) = widget_droplist(ro_fields, value=cam_blocks_p2_col, $
                                                        uvalue='CAM_Y2')

    enter_lab  = widget_label(enter_base, value= $
			'NOTE: If you resize the image window')
    enter_lab  = widget_label(enter_base, value= $
			'      you MUST cancel and restart   ')
    enter_lab  = widget_label(enter_base, value= $
			'      this manual input widget.     ')

    loc_ready      = widget_button(enter_base, value='READY', $
						  uvalue='LOC_READY')

    loc_cancel     = widget_button(enter_base, value='Cancel', $
						  uvalue='CANCEL')
 

    widget_control, enter_base, /realize

    widget_control, cam_ro_id(0), set_droplist_select = 0  ; X1=p1col
    widget_control, cam_ro_id(1), set_droplist_select = 0  ; Y1=p1row
    widget_control, cam_ro_id(2), set_droplist_select = 31 ; X2=p2col
    widget_control, cam_ro_id(3), set_droplist_select = 31 ; Y2=p2row
 

 endif

 sub_man_struc = {manual_inputs, $
			cur_file:substruc.cur_file, $
			pix_per:substruc.pix_per,   $
			draw_index:substruc.draw_index, $
			cam_ro_id:cam_ro_id, $
			antenna_roll:substruc.antenna_roll, $
			date:substruc.date,	$
			x1:0, $
			y1:0, $
			x2:31, $
			y2:31 }
	
 widget_control, enter_base, set_uvalue=sub_man_struc
 XMANAGER, 'EIT_SUB_MANUAL', enter_base, group=substruc.sub_base_id, $
				event_handler='eit_sub_man_ev'

return
end
