;+
;
;	EIT_SUBFIELD can run locally on the OPs machine or using SSW.
;
;	To run using IDL, the code expects the following logicals/environment:
;	
;		Path to EIT data files : REF_DIR
;
;		Path to EIT QKL catalog files : CAT_DIR  
;			Note: The code can function without CAT_DIR if
;			      there are images in the directory REF_DIR.
;
;		Path to EIT QKL catalog files : EIT_QKL_CATALOG
;
;		Path to EIT_SUBFIELD data files : EIT_SUB
;			ex: setenv EIT_SUB /data/subfield
;			Note: The code can function without EIT_SUB if
;			      exact pointing is not needed 
;			      (the subfield coords will still be accurate) 
;			      and if you do not use the /LASCO keyword and 
;
;		Definition for EIT color table : COLOREIT
;
;
;	To run using SSWIDL, the expects the following environment variables:
;
;		Path to EIT data files : EIT_QUICKLOOK
;
;		Path to EIT QKL catalog files : EIT_QKL_CATALOG
;
;		Path to EIT_SUBFIELD data files : EIT_SUB
;			ex: setenv EIT_SUB $SSWDB/soho/eit/planning/subfield
;			Note: The code can function without EIT_SUB if
;			      exact pointing is not needed 
;			      (the subfield coords will still be accurate) 
;			      and if you do not use the /LASCO keyword and 
;
;		Definition for EIT color table : COLOREIT
;
;==============================================================================		
;
;
; NAME        : SUBFIELD_EV
;
; PURPOSE     :
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : To view files that are on the archive
;	IDL> file = eit_catrd('1-aug-1996', /lz, wave = 304)
;	IDL> eit_subfield, over_file=eit_file2path(file)
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGURMENTS  :	none
;
; KEYWORDS    : OLD_POINT 	if set, use pre recentering (sun more north) coords.

;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (HSTX)
;		1998 Jan - eitnfalt- added old_point keyword
;
;		V1.0.1, D.M. fecit:	Spelling correction	1996/07/14
; 	1999-march  Einfalt - - passing group to more routines for V5 modal
;	2000-nov-27 einfalt - changed initial draw widget size from 640 to 1024
;	2004-jun-02 einfalt - initial draw widget to 832, macs cant grab corners off screen
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

pro subfield_ev, event

	      
common newfields, $			; BYTARR(1024) of the EIT subfields
	new_sub_ids,	$		;   user's working array
	lst_sub_ids,    $		;   user's last accepted pattern
	lst_offset			;   user's last accepted offset

 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


 ; Note: Rewrite the structure SUBSTRUC to the main subfield tool base
 ;       at the bottom of the event handler.  This saves any modifications.
 ;	 If this action is not desired then exit directly with a RETURN.

 case input of 

   'QUIT'   :  begin
		new_sub_ids = lst_sub_ids 		; ignore last subs

		; Resensitize the experiment creation widget (if present)
		if substruc.prog_widg_id ne -1 then $
			widget_control, substruc.prog_widg_id, /sensitive

       		; Rewrite the main Science Plan's offset (if present)
       		if substruc.main_offset_id ne -1 then $
	   		widget_control, substruc.main_offset_id, set_value= $
;1997/6/25			EIT_SUB_CONVOFF(offset=substruc.offset,/text)
				strtrim(substruc.offset,2)

		if substruc.caller_id ne 0 then begin	; return control and
		   if substruc.caller_d	ge 0 $
		      then wset, substruc.caller_d	; coordinate system
		   !P = substruc.caller_p	
		   !X = substruc.caller_x
		   !Y = substruc.caller_y
		   !Z = substruc.caller_z
		   widget_control, substruc.caller_id, sensitive=1
		endif

		widget_control, event.top, /destroy 	; destroy widget
		return					; so doesn't "rewrite"
	       end

   'OPTIONS' :  case event.value of 	
		1: XLOADCT, file="coloreit"
		2: EIT_SUB_MDI, substruc=substruc, new_sub_ids=new_sub_ids
		3: EIT_SUB_MDI, substruc=substruc, new_sub_ids=new_sub_ids
		4: EIT_SUB_PRINT, 0, substruc=substruc, new_sub_ids=new_sub_ids
		5: EIT_SUB_PRINT, 1, substruc=substruc, new_sub_ids=new_sub_ids
		6: EIT_SUB_PRINT, 2, substruc=substruc, new_sub_ids=new_sub_ids
		7: print,'Sorry, no help here'
	        else: print,'Impossible case in eit_subfield'
	      endcase


   'READY'  : begin					; destroy widget,
		
		; Exitting the subfield interface.  The tool and the window
		; will be destroyed but the final subfield pattarn and
		; offset will be available for returning to a calling routine.

		lst_sub_ids = new_sub_ids		; user is happy w/subs
		lst_offset  = substruc.offset		; user is happy w/off

		if substruc.main_offset_id ge 0 then 		$
	           widget_control, substruc.main_offset_id, 	$
		      set_value= strtrim(substruc.offset,2)

		; Resensitize the experiment creation widget (if present)
		; Load finalized subfield pattern into handle		

		if substruc.prog_widg_id ne -1 then begin
		   handle_value, substruc.sub_id_ptr, new_sub_ids, /set
		   widget_control, substruc.prog_widg_id, /sensitive
		endif

		widget_control, event.top, /destroy 	; new subfields are

		if substruc.caller_id ne 0 then begin	; return control and
		   if substruc.caller_d	ge 0 $
		      then wset, substruc.caller_d	; coordinate system
		   !P = substruc.caller_p	
		   !X = substruc.caller_x
		   !Y = substruc.caller_y
		   !Z = substruc.caller_z
		   widget_control, substruc.caller_id, sensitive=1
		endif

	        return					; save in the common
	      end

   'SELECT' : begin

		; Mouse activities in the draw window will be interpretted
		; as subfield selections, either highlighting or unhighlighting

		widget_control, substruc.select_id, 	set_button=1
		widget_control, substruc.drag_id,   	set_button=0
		widget_control, substruc.rot_mode_id,   set_button=0
		substruc.draw_flag = 1		; select by (un)highlighting

	      end

   'DRAG'   : begin

		; Mouse activities in the draw window will be interpretted
		; as whole subfield pattern movements.

		widget_control, substruc.select_id, 	set_button=0
		widget_control, substruc.drag_id,   	set_button=1
		widget_control, substruc.rot_mode_id,   set_button=0
		substruc.draw_flag = 2		; drag pattern around

	      end

   'ROT_MODE' : begin

		; Mouse activities in the draw window will be interpretted
		; as rotation requests.

		widget_control, substruc.select_id, 	set_button=0
		widget_control, substruc.drag_id,   	set_button=0
		widget_control, substruc.rot_mode_id,   set_button=1
		substruc.draw_flag = 3		; select point to rotate

	      end

   'CLEAR'   : begin

		; File        : same
		; Grid        : same
		; Rotation    : current displayed in tool interface
		; Offset      : current displayed in tool interface
		; Subfields   : none
		; Window size : same

		new_sub_ids  = bytarr(1024)		; all zeros, non set

		EIT_SUB_DISP,    /refile,				$
				 sub_ids    = new_sub_ids, 		$
				 substruc   = substruc
	       end

   'EIT_GRID' : begin

		; File        : same
		; Grid        : EIT, if not already
		; Rotation    : current displayed in tool interface
		; Offset      : current displayed in tool interface
		; Subfields   : same
		; Window size : same

		widget_control, substruc.blk_grid_id, set_button=1
		widget_control, substruc.sol_grid_id, set_button=0

		  if not(substruc.grid_flag) then begin	; not already this one

		     substruc.grid_flag = 1		; use EIT subfield grid
		     EIT_SUB_DISP, /refile,				$
				   sub_ids    = new_sub_ids, 		$
			           substruc   = substruc

		  endif
		end	     

   'SOL_GRID' : begin

		; File        : same
		; Grid        : Heliographic, if not already
		; Rotation    : current displayed in tool interface
		; Offset      : current displayed in tool interface
		; Subfields   : same
		; Window size : same

		widget_control, substruc.blk_grid_id, set_button=0
		widget_control, substruc.sol_grid_id, set_button=1
		
		  if substruc.grid_flag then begin	; not already this one

		     substruc.grid_flag = 2		; use solar helio grid
		     EIT_SUB_DISP, /refile,			$
				   sub_ids    = new_sub_ids, 	$
			           substruc   = substruc

		  endif
		end

   'ARC_ENTER': EIT_SUB_MAN_INPUT, substruc=substruc, /arc
   'HEL_ENTER': EIT_SUB_MAN_INPUT, substruc=substruc, /helio
   'LOC_ENTER': EIT_SUB_MAN_INPUT, substruc=substruc, /locs


   'ROTATE' : begin

		; File        : same
		; Grid        : EIT (if not already)
		; Rotation    : current displayed in tool interface
		; Offset      : current displayed in tool interface
		; Subfields   : same
		; Window size : same

    		ack = respond_widg(mess= $
			['To use rotation:             ', $
 	 		' ', $
 	 		'Select "Rotation mode" ', $
	 		'Enter rotation time into "ROTATED" window', $
	 		'Click mouse button on point to be rotated.',$
	 		' ', $
	 		'To redraw image without rotation marks:', $
			' try some things.'],$
			button='Acknowledge', group=event.top, /row)

	      end

   'LIST'   : if event.clicks eq 2 then begin

		; File        : list item just selected
		; Grid        : EIT (if not already)
		; Rotation    : current displayed in tool interface
		; Offset      : current displayed in tool interface
		; Subfields   : same
		; Window size : same
	
		substruc.cur_file = substruc.image_files(event.index)

		EIT_SUB_DISP,   /refile,			$
				sub_ids    = new_sub_ids, 	$
				substruc   = substruc
	      end


   else: print,'SUBFIELD_EV, Invalid event ' +input
 endcase 


 ; Rewrite the UVALUE to save any SUBSTRUC modification, to both bases

 widget_control, event.top, set_uvalue=substruc
 widget_control, substruc.draw_base, set_uvalue=substruc

return
end


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

;+
;
; NAME: 
;	EIT_SUBFIELD
;
; PURPOSE:
;	Create a widget for the selection of EIT program subfields
;
; CATEGORY:
;	EIT Planning
;
; CALLING SEQUENCE:
;	result = EIT_SUBFIELD
;
; CALLED BY:
;	EIT_PROGRRAM_EV
;
; CALLS TO:
;	SUBFIELD_EV (through the XMANAGER)
;
; ARGUMENTS:
;	NCATDAYS	If present, the number of catalog to search to
;			obtain full field images.  (default = 5)
; KEYWORDS:
;       SUBFIELDS	The most recently accessed EIT subfield array
;	OVER_FILE	String or string array (?) of a fits file name
;			with full path included.
;			Note: if over_file is a LASCO image include
;			      keyword LASCO 
;	LASCO		If over_file is set, then LASCO may be set to
;			1 for C1, 2 for C2, or 3 for C3 to provide flips
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       Returns an INTARR of the subfield numbers selected by user 
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Create the EIT subfield widget where the draw window displays the
;	EIT subfield grid for user to select desired subfields.
;
; MODIFICATION HISTORY:
;	1994 December 2, Elaine Einfalt (HSTX)
;	1996 July 3, Einfalt - search catalog for full field images only
;	1996 Sept 25 Einfalt - added over_file keyword (full path)
;				and LASCO keywords
; 	1999-march  Einfalt - - passing group to more routines for V5 modal
;	2003-july   Einfalt - Added substruc.antenna_roll to structure
;	2004-may    Einfalt - Added Mac/SSW compatablity
;	2004-june   einfalt - removed call to UTIL_TRUNC_FILE
;-

pro EIT_SUBFIELD, ncatdays,				$
		  sub_ids	 = sub_ids, 		$
		  offset	 = offset, 		$
		  rotate_time	 = rotate_time,		$   ; not compatible
		  main_offset_id = main_offset_id, 	$
		  prog_widg_id	 = prog_widg_id,	$
		  sub_id_ptr	 = sub_id_ptr,		$
		  cam_ro	 = cam_ro,		$
		  over_file	 = over_file,		$
		  lasco		 = lasco,		$
		  old_point	 = old_point,		$
		  group		 = group

common local, process, last_block, $	; just needed for the eventhandler
	      orig_x,  orig_y,     $
	      prev_x,  prev_y

common newfields, $			; BYTARR(1024) of the EIT subfields
	new_sub_ids,	$		;   user's working array
	lst_sub_ids,    $		;   user's last accepted pattern
	lst_offset			;   user's last accepted offset

common eit_linecolors, used


 ; These values are usually only provided when called interactively
 ; so if from planning tool don't waste much time reading catalogs

 if n_params() eq 0 then ncatdays = 3	; 3 days worth
 
 if not(keyword_set(old_point)) then old_point = 0

 ; These values are usually only provided when called for EIT_PROGRAM

 if n_elements(offset)  	eq 0 then offset = 0
 if n_elements(main_offset_id) 	eq 0 then main_offset_id = -1
 if n_elements(prog_widg_id)	eq 0 then prog_widg_id   = -1
 if n_elements(sub_id_ptr) 	eq 0 then sub_id_ptr 	 = -1

 if n_elements(rotate_time)     eq 0 then rot_time = 0	$
				     else rot_time = rotate_time 

 ; Because of conflicting coordinate systems we can't do plan stuff 
 ; while the subfield stuff is displayed. Restored at READY or CANCEL.

 if n_elements(group) ne 0 then widget_control, group, sensitive=0 $
			   else group = 0l
 caller_d = !D.window	
 caller_p = !P	
 caller_x = !X	
 caller_y = !Y	
 caller_z = !Z	

 ; Initialize values

 process    = 0			
 last_block = -1
 grid_flag  = 1					; initially use EIT block grid
 lst_offset = offset


 if n_elements(sub_ids) eq 0 then begin
 	new_sub_ids = bytarr(1024)
	lst_sub_ids = bytarr(1024)
        draw_flag = 1				; default to area boxing
 endif else begin
	new_sub_ids = sub_ids 			; user's working array  
	lst_sub_ids = sub_ids			; last user accepted array

        if prog_widg_id eq -1 then draw_flag=2 $  ; dragging pattern for offset
			      else draw_flag=1    ; boxing for exp def.		
 endelse

 if strupcase(!version.os) eq 'VMS' then delim = ':' else delim = '/'

 if not(keyword_set(lasco)) and (n_elements(over_file) eq 0) then begin
 
 ; Not doing a lasco subfield and not passing in a specific file
 ; Use eit catalogs to find list of images to pick from

    ; Get list of full sun images

    ; Modify the list of files thing.
    print,'Searching ' + strtrim(ncatdays,2) +$
	 ' days worth of catalogs for full field images'
    get_utc,t   &   today = utc2tai(t)	
    month = [' ', 'Jan','Feb','Mar','Apr','May','Jun', $
	       'Jul','Aug','Sep','Oct','Nov','Dec']
    image_files = 'nofiles'
    image_list  = 'nofiles'

    for onerot = 0, ncatdays-1 do begin		; look backwards through catalogs

     date = anytim2utc((today-onerot*86400d) ,/ecs, /date)

     ; Want to use the QKL catalogs. 
     ; First see if we have any local, if not then use archive QKL cats

     local_qkl_catalog = findfile ( getenv('CAT_DIR') + $
		     strmid(date,0,4) + strmid(date,5,2)+strmid(date,8,2) + $
		     '.cat_copy')

     if local_qkl_catalog ne '' then begin

        input_file = local_qkl_catalog

     endif else begin

        archive_qkl_catalog = findfile(getenv('EIT_QKL_CATALOG') + $
		     strmid(date,0,4) + strmid(date,5,2)+strmid(date,8,2) + $
		     '.cat_copy')
	input_file = archive_qkl_catalog
     endelse    

     if input_file ne '' then begin	; no catalog on this day

        oneday = EIT_CATRD( date, /raw, /no_next, input_file=input_file(0))

        if datatype(oneday) eq 'STC' then begin	; have a catalog on this day 
           ffv = where( (oneday.nx*oneday.nx_sum) eq 32 $
		      and (oneday.ny*oneday.ny_sum) eq 32, count)
           if count ne 0 then begin		; have some ffv images this day

              wave = oneday(ffv).wave(0) + 170 	; look in catrd_daily for help 
              efr_file = string(oneday(ffv).file_name)    
   	      image_times =   strmid(efr_file,3,4) + '-' + $
	 		      month(fix(strmid(efr_file,7,2))) + '-' + $
			      string(strmid(efr_file,9,2),'(i2.2)') + ' ' + $
			      strmid(efr_file,12,2) + ':' + $
			      strmid(efr_file,14,2) + ':' + $
			      strmid(efr_file,16,2) + '      '

	      image_files = [image_files, reverse(efr_file)]
	      image_list  = [image_list,  reverse(image_times + strtrim(wave,2)) ]
           endif
        endif			; end for 'if datatype...

      endif			; end for 'if input_file...'
    endfor


    if n_elements(image_files) gt 1 then begin	  ; remove initial bogus	

       image_files = image_files(1:*)  
       image_list  = image_list(1:*)  

    endif else if n_elements(image_files) eq 1 then begin  ; can't find anything 

        print,'No full field images in given range'

        ;2004/06/03 image_files = reverse(UTIL_TRUNC_FILE('REF_DIR' + delim + 'EFR*.*'))

	local_findfile = findfile(getenv('REF_DIR') + delim + 'EFR*.*')
	break_file, local_findfile, dum,dum, all_names, all_ext
        image_files = all_names + all_ext

        image_list  = image_files

        if n_elements(image_list) EQ 1 then begin   ; no images, only have the init value

	   ack = respond_widg(mess=['**********', $
                     '  There were no catalogs/images in the last '+ $
		         strtrim(ncatdays,2) + ' (or more) days.',    $ 
   	             '*', $
	             '  You could try to :', $
                     '    1) Increase your range, ex: eit_subfield,10 ', $
	             '    2) Pick a specific file that you know exists,', $
	             '       ex: eit_subfield,' + $
			      'over=''/service/eit/lz/2004/03/efz20040306.132551''', $
	             '*', $
	             '**********'], $
		     button='Acknowledge', group=group)
	
	    return		; nothing we can do without a catalog or images

        endif

    endif
    
    over_file = 0		; no override file(s)
    lasco = 0			; not lasco

 endif else begin		; file override, only input file is available
				; Not using EIT catalog lists to find files

     if not(keyword_set(lasco)) then begin

	; Use the specifically input file

	lasco = 0		 ; not a lasco
     	image_files = over_file  ; use this specific file
	image_list  = over_file

     endif else begin

	; Use only the preset LASCO files in this subdirectory.
	
	lasco = 1 		 ; is a lasco
        lasco_fits_location = getenv('EIT_SUB')

        if lasco_fits_location eq '' then begin
 
	   print,' '
	   print,'  The location of the lasco fits files was not defined'
	   print,'  Feel free to define the SSW archive location for'
	   print,'  the LASCO_C*_SUB.FTS files'
           print,' ' 
           print,'     setenv EIT_SUB $SSWDB/soho/eit/planning/subfield'
           print,' '
           print,'  The code is now going to assume the appropriate lasco'
           print,'  files are in that directory.'
           print,'  If they are not, or you do not have access to SSW'
           print,'  then the code will return'
           print, ' '
 
           ; so assume we are running the SSW version (else it would
           ; have been defined), therefore the lasco files are here...

	   lasco_fits_location = '$SSWDB/soho/eit/planning/subfield'

        endif

        image_files = findfile(lasco_fits_location+delim+'LASCO_C*_SUB.FTS')
        image_list  = findfile(lasco_fits_location+delim+'LASCO_C*_SUB.FTS')
 
        if image_files(0) eq '' then begin
           print, ' '
           print, ' Sorry no LASCO_C*_SUB.FTS files were found. '
           print, ' '
 
           return
        endif

     endelse

     over_file = 1		; yes, override standard file(s)
 endelse

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

 cur_file = image_files(0)
 
 sub_base_id = widget_base(title='EIT Subfield Tool', /row, xpad=20, ypad=20, $
			   xoffset=0, yoffset=0)

             control_base = widget_base(sub_base_id, /column)

	ready = widget_button(control_base, value='READY', uvalue='READY')
	options_base = widget_base(control_base, /row)

; 	   color = widget_button(aid_base, value='Adjust color table',  $
;					       uvalue='COLOR')

           opt_opt = ['Adjust colors' , $
			'MDI FOV on',   $
			'MDI FOV off',  $
			'B/W Print',    $
			'Cheap Color',  $
		      	'$$$ Color',    $
			'HELP']

           desc = [ {cw_pdmenu_s, flags:1, name:'OPTIONS' }, $
   	            {cw_pdmenu_s, flags:0, name:opt_opt(0) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(1) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(2) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(3) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(4) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(5) }, $
	            {cw_pdmenu_s, flags:0, name:opt_opt(6) }]

           option_id = cw_pdmenu(options_base, desc, uvalue='OPTIONS') 

;   	help_base = widget_base(control_base, /row)
;	   help  = widget_button(help_base, value='HELP',  uvalue='HELP')
;	   mdi_id = widget_button(help_base, value='MDI FOV',  uvalue='MDI')

 	quit  = widget_button(control_base, value='CANCEL',  uvalue='QUIT')


	do_base_id = widget_base(control_base, /exclusive, /frame, /column)
	  select_id = widget_button(do_base_id, value='Select', uvalue='SELECT')
	  drag_id   = widget_button(do_base_id, value='Drag', 	uvalue='DRAG')
	  rot_mode_id = widget_button(do_base_id, value='Rotation mode', $
						uvalue='ROT_MODE')

	clear_id = widget_button(control_base, value='Clear Subfields', $
					       uvalue = 'CLEAR')

	grid_base_id = widget_base(control_base, /exclusive, /frame, /column)
	  blk_grid_id = widget_button(grid_base_id, value='Subfield Grid', $
				       		   uvalue='EIT_GRID')
	  sol_grid_id = widget_button(grid_base_id, value='Helio Grid', $
					/align_left, uvalue='SOL_GRID')

;1997/1/27 this space was given to locs coords
;	offset_base = widget_base(control_base, /row, /frame)
;	  lab = widget_label(offset_base, value='Offset block:')
;	  offset_label_id = widget_label(offset_base, $
;			   value='0, 0     ', uvalue='OFFSET')

	locs_base = widget_base(control_base, /row, /frame)
;	  lab = widget_label(locs_base, value='LOCS Coord:')
	  lab = widget_button(locs_base, value='LOCS Coord:', $
					 uvalue='LOC_ENTER')
	  locs_label_id = widget_label(locs_base, $
			value='      0,      0,      0,      0 ')

	blk_cursor_base = widget_base(control_base, /row, /frame)
	  lab = widget_label(blk_cursor_base, value='Block Coord:')
	  blk_curs_pos_id = widget_label(blk_cursor_base, value='0, 0     ')

	pix_cursor_base = widget_base(control_base, /row, /frame)
	  lab = widget_label(pix_cursor_base, value='Blk Pix (B) :')
	  pix_curs_pos_id = widget_label(pix_cursor_base, $
			value='      0,     0,      0,      0 ')

	sol_cursor_base = widget_base(control_base, /row, /frame)
;	  lab = widget_label(sol_cursor_base, value='Arcsec Coord:')
	  lab = widget_button(sol_cursor_base, value='Arcsec Coord:', $
						uvalue='ARC_ENTER')
	  sol_curs_pos_id = widget_label(sol_cursor_base, $
				value='0, 0                ')

	hel_cursor_base = widget_base(control_base, /row, /frame)
;	  lab = widget_label(hel_cursor_base, value='Helio Coord:')
	  lab = widget_button(hel_cursor_base, value='Helio Coord:', $
						uvalue='HEL_ENTER')
	  hel_curs_pos_id = widget_label(hel_cursor_base, $
				value='0 N, 0 E              ')

	thet_cursor_base = widget_base(control_base, /row, /frame)
	  lab = widget_label(thet_cursor_base, value='R and Theta:')
	  thet_curs_pos_id = widget_label(thet_cursor_base, $
				value='1.0 R, 0 deg         ')

	if n_elements(cam_ro) eq 0 then cam_ro = '0,0,31,31,0,0'
	cam_ro_area = fix((str_sep(cam_ro,','))(0:3))
	
        cam_cursor_base = widget_base(control_base, /row, /frame)
	  lab = widget_label(cam_cursor_base, value='Readout area:')
	  cam_curs_pos_id = widget_label(cam_cursor_base, $
;1996/5/22		  value='   ' + strtrim(cam_ro_area(1),2) + ', ' + $
;				strtrim(cam_ro_area(0),2) + ', ' + $
;				strtrim(cam_ro_area(3),2) + ', ' + $
;				strtrim(cam_ro_area(2),2) + '           ')
			  value='   ' + strtrim(cam_ro_area(0),2) + ', ' + $
				strtrim(cam_ro_area(1),2) + ', ' + $
				strtrim(cam_ro_area(2),2) + ', ' + $
				strtrim(cam_ro_area(3),2) + '           ')

	rotate_base = widget_base(control_base, /row, /frame)
	  lab = widget_label(rotate_base, value='Rotated ')
	  rotate_id = widget_text(rotate_base, 				$
;1997/10/14			 value=string(rot_time,'(f7.2)'), 	$
				  value=strtrim(rot_time,2)+'.', 	$
				  /editable, xsize=7,			$
				  uvalue='ROTATE')
	  lab = widget_label(rotate_base, value='hours')

;1996/7/1        list_id = widget_list(control_base, value=image_files, $
        list_id = widget_list(control_base, value=image_list, $
				 ysize=9,uvalue='LIST')

 widget_control, sub_base_id, /realize		; make control widget appear
 widget_control, sub_base_id, tlb_get_offset=widg_offset

 ;
 ; The draw widget where the image and subfield pattern selection
 ; occurs.
 ;

 draw_base = widget_base( row=1, $
	title          = 'EIT Subfield Tool...' + cur_file, $
	xoffset        = widg_offset(0)+400, 				   $
	group_leader   = sub_base_id, 					   $
	event_pro      = 'EIT_SUB_DRAW_EV', 				   $
	tlb_size_event =1)

 resize_x = 960			; initial window size (multiple of 32)
 pix_per  = resize_x / 32		; device pixels per each subfield side

 draw_id = widget_draw(draw_base, /button_events,	$
				    /motion_events, 	$
				    xsize  = resize_x, 	$
				    ysize  = resize_x,  $
				    retain = 2) 	; , uvalue='DRAW')

 widget_control, draw_base, /realize		; make image widget appear


 ;
 ; Some initial widget setups 
 ;

;1997/10/14 widget_control, rotate_base, sensitive=0

 if draw_flag  then widget_control, select_id,   set_button = 1 $
	       else widget_control, drag_id,     set_button = 1 

 if grid_flag  then widget_control, blk_grid_id, set_button = 1 $
	       else widget_control, sol_curs_id, set_button = 1

 ; 1997/1/27 space on widget was given to locs_label_id
 ; widget_control, offset_label_id, $
 ;		 set_value=EIT_SUB_CONVOFF(offset=offset,/text)

 widget_control, draw_id, get_value=draw_index	; get draw index
 wset, draw_index

 ; This pixmap is used as a "snap" of the draw window. 1024 is max size.

 window, /free, xsize=1024, ysize=1024, /pixmap
 pix_index = !d.window    

 ; Pick a color table and find out how many lines are used for line colors.

 linecolors, table=3, /image, used=used


 substruc = {   $
		sub_base_id:sub_base_id,$	; parent widget 
		select_id:select_id, 	$	; widget ID of select button
		drag_id:drag_id,	$	; widget ID of drag button
		rot_mode_id:rot_mode_id,$	; widget ID of rotation button
						;
		; 1997/1/27 space given to locs_label_id
		; offset_label_id:offset_label_id, $; widget ID of offset text
						;
		locs_label_id:locs_label_id, $  ; 
						;
		offset:offset,		$	; the offset value in text win
 		caller_id:group,	$	; Id from calling routine.
		caller_d:caller_d,	$	; WSET of caller
		caller_p:caller_p,	$	; !P system variables of caller
		caller_x:caller_x,	$	; !X system variables of caller
		caller_y:caller_y,	$	; !Y system variables of caller
		caller_z:caller_z,	$	; !Z system variables of caller
				  ;
		mdi_fov:0b,		$	  ; toggles display of MDI FOV
		blk_curs_pos_id:blk_curs_pos_id,$ ; widget ID of block cursor
		pix_curs_pos_id:pix_curs_pos_id,$ ; widget ID of block pixels
		sol_curs_pos_id:sol_curs_pos_id,$ ; widget ID of solar cursor 
		hel_curs_pos_id:hel_curs_pos_id,$ ; widget ID of solar cursor 
		thet_curs_pos_id:thet_curs_pos_id,$ ; widget ID of solar cursor 
		blk_grid_id:blk_grid_id,	$ ; widg ID of subfield blocks
		sol_grid_id:sol_grid_id,	$ ; widg ID of heliographic
		grid_flag:grid_flag,		$ ; 1=sub grid, 2=solar grid
;96/5/22	cam_ro_area:cam_ro_area,	$ ; RO area,intarr(y1,x1,y2,x2)
		cam_ro_area:cam_ro_area,	$ ; RO area,intarr(x1,y1,x2,y2)
						;
		rotate_id:rotate_id,	$	; widget ID for rotate text
		rot_time:rot_time,	$	; # hours image is rotated
						;
		list_id:list_id,	$	; widget ID of filename list
		cur_file:cur_file,	$	; currently displayed file
		get_file:cur_file,	$	; full path of current file
		date:'yyyy-mm-dd',	$	; date of image, string
		image_files:image_files, $	; STRARR of filenames
		over_file:over_file,	$	; override file or not
		lasco:lasco,		$	; 1=c1,2=c2,3=c3
						;
		draw_base:draw_base,	$	; widget Id for draw base
		draw_id:draw_id,	$	; widget Id for draw window
		draw_index:draw_index,	$	; window index for draw window
		pix_index:pix_index,	$	; window index of pixmap
		resize_x:resize_x,	$	; current window size
		pix_per:pix_per, 	$	; pixels in a subfield block
						;
		used:used, 		$	; # or line colors used
						;
		antenna_roll:0., 	$	; antenna related s/c roll
		draw_flag:draw_flag,	$	; Draw window action flag
						;    1 : Select
						;    2 : Drag pattern
						;
		prog_widg_id:prog_widg_id,  $	; if from exp creation widget
		sub_id_ptr:sub_id_ptr,  $	; handle for stored sub_ids
						;    event handler loads them
	        main_offset_id:main_offset_id $ ; widget ID of main science
					        ; plan tool offset text widget
						; If it is the parent widget
	    }




 ; Display a full sun image with grid and highlight the subfield pattern 
 ; (if any) at the given offset.  If a file error than no image.

 EIT_SUB_TOOL, 	  blocks	= -1,		$	; no blocks to "add"
		  error		= error, 	$
		  offset	= offset,	$	; passed in offset?
		  refile	= 1,		$	; read and TV cur_file 
		  sub_ids	= new_sub_ids, 	$	; passed in pattern?
		  substruc	= substruc,	$
		  old_point	= old_point



 ; Whenever the window is updated it is copied to the pixmap.

 wset, pix_index
 device,copy=[0, 0, resize_x, resize_x, 0, 0, draw_index]
 wset, draw_index



 ; Must save the structure in both the interface tool main base, and
 ; the separate draw window main base.

 widget_control, sub_base_id, set_uvalue=substruc
 widget_control, draw_base,   set_uvalue=substruc

 ; When called by EIT_PROGRAM.PRO, this is not "blocked".  The xmanager
 ; will start this interface, but not wait for the QUIT button. 
 ; So, anything set in here must be retrieved independent of any 
 ; subfield event handling.  (Thus, the SUB_ID_PTR handle is used).
 ;
 ; NOTE: Case matters for widget name.  Used later in XREGISTERED()

 XMANAGER, 'EIT_SUBFIELD', sub_base_id, event_handler='subfield_ev', $
					group=group

 print,'>>> You may call EIT_SUBFIELD with a number of days argument.'
 print,'>>> For example: EIT_SUBFIELD, 20   lists past 20 days of images.'

return
end
