;+
; NAME        : EIT_SUB_UTIL_FILE
;
; PURPOSE     : Request to get a new EIT file and display it.
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
;
; 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)
;		1996 Sept 25 Einfalt - added over_file ability (and c1)
;		1998 Jan - eitnfalt- added old_point keyword
;		2003-july-21   Einfalt - Added substruc.antenna_roll value
;		2004-may    Einfalt - Added Mac/SSW compatablity
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

pro EIT_SUB_UTIL_FILE, 	$
			sub_ids		= sub_ids,	$
		        substruc	= substruc,	$
			old_point	= old_point


 ;
 ; Read in data file.
 ;

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

 ; 2004/05/24 - einfalt - Making this Mac/SSW compatible,
 ; so need to have it check for qkl data on the archive if
 ; it doesnt find the given day in REF_DIR, on local system.

 ;if not(substruc.over_file) $
 ;   then get_file = 'REF_DIR' + delim + substruc.cur_file $
 ;   else get_file = substruc.cur_file 

 if not(substruc.over_file) then begin 

   get_file = getenv('REF_DIR') + delim + substruc.cur_file  ; local REF_DIR
   
   if (findfile(get_file))(0) eq '' then begin	; nothing found in REF_DIR
 
      ; Didn't find this data file in local REF_DIR, so look on archive
      get_file = getenv('EIT_QUICKLOOK') + '/' + $
		 strmid(substruc.cur_file,3,4) + '/' + $  ; the year directory
		 strmid(substruc.cur_file,7,2) + '/' + $  ; the month directory
		 strmid(substruc.cur_file,9,2) + '/' + $  ; the date
		 substruc.cur_file
   endif

 endif else get_file = substruc.cur_file 	; user is doing lasco or 
						; using specific input file
 print, '%EIT_SUB_UTIL_FILE, getting file: ' + get_file
 substruc.get_file = get_file			; full path with the filename

 ;1998/3/12 New way to handle lasco subfields
 
 if substruc.lasco gt 0 then begin	; which lasco is it
    l_pos = strpos(get_file, '_C')
    substruc.lasco = fix(strmid( get_file, l_pos+2, 1))
 endif


 image = READFITS(get_file, hd)   &   image = image(0:*, 0:*)

 if (substruc.lasco eq 0) then substruc.date = strmid(fxpar(hd,'DATE_OBS'),0,10) $
                          else substruc.date = strmid(fxpar(hd,'DATE-OBS'),0,10)

 ; Spacecraft antenna problem result in spending part of the year rolled 180
 ; Images are still displayed as taken, but Arcsec, Helio, and R/theta
 ; outputs need to be adjusted. -- 2003/07/21 eee

 ; Look at fits file header to see if we are rolled 180 degrees

 antenna_roll = eit_fxpar(hd, "SC_ROLL")
 if antenna_roll ne 180. then substruc.antenna_roll = 0. $
			 else substruc.antenna_roll = 180.

 ; added march 7,1996 corrected july 29, 1997
 ; if not(substruc.over_file) then image = bytscl(alog10((image -848) > 1.)) 
 if substruc.lasco eq 0 then image = bytscl(alog10((image -848) > 1.)) 

 ;
 ; Display image
 ;

 wave = eit_fxpar(hd, 'WAVELNTH')

 case wave of
   '171' : loadct, 42, file=getenv('coloreit')
   '195' : loadct, 43, file=getenv('coloreit')          
   '284' : loadct, 44, file=getenv('coloreit')
   '304' : loadct, 45, file=getenv('coloreit')
    else : loadct,  0, file=getenv('coloreit')		; generic
 endcase
  
 linecolors, /image


 if not(substruc.over_file) then $
 
    tv, bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
	       top=!d.table_size-substruc.used-1)		    $

 else case (substruc.lasco) of
	1:     tv, reverse ( $
		 rotate( $
		 bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
	         top=!d.table_size-substruc.used-1) $
		,1) $
		,2)

	2:     tv, $
		 rotate( $
		 bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
	         top=!d.table_size-substruc.used-1) $
		,2)

	3:     tv, $
		 rotate( $
		 bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
	         top=!d.table_size-substruc.used-1) $
		,2)

	else : tv, $
		bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
	               top=!d.table_size-substruc.used-1)
      endcase


;ee=bytscl(congrid(image, substruc.resize_x, substruc.resize_x), $
;	         top=!d.table_size-substruc.used-1)
;stop

 ;
 ; Setup arcsec data coordinate system
 ;

 EIT_SUB_UTIL_COOR, resize_x=substruc.resize_x, old_point=old_point

 ;
 ; If block grid is selected (grid_flag=1), draw the EIT grid over the image
 ; If solar grid is selected (grid_flag=2), draw heliographic grid over image
 ;

 if substruc.grid_flag $

    then EIT_SUB_UTIL_LINE,  /grid,		 		$
			     pix_per  = substruc.pix_per, 	$
			     resize_x = substruc.resize_x 	$

    else EIT_SUB_UTIL_HELIO, date = substruc.date,		$
			     antenna_roll = substruc.antenna_roll


 ;
 ; Draw the camera readout area, in yellow (linecolors index 5)
 ;

 EIT_SUB_UTIL_RO, cam_ro  = substruc.cam_ro_area, $
	          pix_per = substruc.pix_per


 ;
 ; If set, draw the MDI fov in thick purple
 ;

 if substruc.mdi_fov then EIT_SUB_UTIL_MDI

 ;
 ; If any of the subfield ids are set, then display them as highlights
 ;

 blocks = where(sub_ids eq 1b, n_blks)

 if n_blks gt 0 then $

    EIT_SUB_UTIL_HIGH, 	$
			blocks	= blocks, 		$
			color	= sub_ids(blocks),	$ 
			pix_per	= substruc.pix_per,		$
		       	sub_ids	= sub_ids

return
end
