;+
; NAME        : EIT_PA_PROCESS
;
; PURPOSE     : Parse info from file header, display, and set up coord system
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGUMENTS   :	
;	Cat_text  text array containing catalog output of the selected files
;
; KEYWORDS    :
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (HSTX)
;		2001/06/13 modified for EMS movie files
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

pro eit_pa_process, cat_text, sel_struc=sel_struc
common eit_pa_list, in_list, out_list, out_pa
common eit_pa_movie, last_movie, data, time_stamp

 lz_data = 0					; most likely raw data

 if n_elements(last_movie) eq 0 then last_movie = ''	; first movie pass

 if not(sel_struc.movie) then begin	; process the single image clicked on

    ; Parse the catalog string to get the filename

    loc = strpos(cat_text,'EFR')
    if loc eq -1 then begin
       loc = strpos(cat_text,'EFZ')		; must be LZ cat text
       lz_data = 1					; it's LZ data
    endif 
    if loc eq -1 then return			; impossible, but handle it
   
    ; Cut out the filename
    file = strmid(cat_text, loc, 18) 
   
    ; What color table should be used

    loc2 = strpos(cat_text,'::')-3		; wavelength is before the "::"
    wave = strmid(cat_text, loc2, 3)		; parse out wavelength

 endif else begin

    ; parse to get movie file name
    loc = strpos(cat_text,')')+1		
    file = strtrim(strmid(cat_text, loc, 99),2)	; the ems movie filename

    ; parse to get wavelength
    loc = strpos(cat_text,'(')+1
    wave = strmid(cat_text, loc, 3)		; the wavelength

    ; parse to get the frame number
    loc = strpos(cat_text,':')
    frame = strmid(cat_text, 0, loc)

 endelse

 case wave of
      '171': i_wave=0
      '195': i_wave=1
      '284': i_wave=2
      '304': i_wave=3
      else : return				; yup, another impossible 
 endcase
    
 loadct, 42 + i_wave, file = getenv('coloreit')	; load correct colors
    
   
 ; Where to find the data file

 if (lz_data) then dir = 'LZ_ARCHIVE:' $
 else if (sel_struc.movie) then dir = 'MOVIE_DIR:' $
 else dir = 'REF_DIR:' 
   
 print, 'Reading file '+dir+file
   
 if not(sel_struc.movie) then begin  ; read in and display individual fits file

    data = readfits(dir+file, hd)		; get the data with header
   
    ; Get the sun center pixel

    pix_x = strpos(hd, 'CRPIX1')
    x_index = where(pix_x ne -1)
    x_cen_line = (hd(x_index))(0)
    xcen = float(strmid(x_cen_line, strpos(x_cen_line,'=')+1, $
			      strpos(x_cen_line,'/')-strpos(x_cen_line,'=')-1))

    pix_y = strpos(hd, 'CRPIX2')
    y_index = where(pix_y ne -1)
    y_cen_line = (hd(y_index))(0)
    ycen = float(strmid(y_cen_line, strpos(y_cen_line,'=')+1, $
			      strpos(y_cen_line,'/')-strpos(y_cen_line,'=')-1))

 endif else begin		; doing a movie

    ; Have we read in this movie already?

    widget_control, sel_struc.sel_base, sensitive=0	; grey out widget

    if last_movie ne file then begin			; is a different movie
       restore, file					; read in the movie file
       data = b0(*,*, frame)
       last_movie = file					
    endif


    widget_control, sel_struc.sel_base, sensitive=1	; give user access again
    
    xcen = 506.			; hardcoded X center pixel of sun center
    ycen = 513.			; hardcoded Y center pixel of sun center

 endelse


 wset, sel_struc.win				; activate the display window
    
 xsiz = (size(data))(1) 			; image size in x
 ysiz = (size(data))(2) 			; image size in y

    
 if xsiz eq sel_struc.resize_x and ysiz eq sel_struc.resize_x then begin

       ; draw window is already the correct size, so just display it

       if not(sel_struc.movie) then tv, bytscl(alog10((data-850)>1.)) $
			       else tv, data

 endif else begin

       ; the draw window needs to be resized
       widget_control, sel_struc.drawpa_id, xsize=xsiz, ysize=ysiz

       if not(sel_struc.movie) then  tv, bytscl(alog10((data-850)>1.)) $
			       else  tv, data

       sel_struc.resize_x = xsiz
       Sel_struc.resize_x = ysiz

 endelse

 ; make a mark at sun center

 plots, [xcen+15, xcen-15], [ycen, ycen], /dev
 plots, [xcen, xcen], [ycen+15,ycen-15],  /dev


 ; Parse the cat string to get the image resolution

 if not(sel_struc.movie) then begin  

    loc = strpos(cat_text,'x(')
    resol = fix(strmid(cat_text, loc-2, 2)) * 2.6

 endif else begin

    ; we dont' have this resolution info for movies, so we are going
    ; to have to trust that the user is only displaying full field movies

    case 1 of 
       xsiz eq 1024 : resol = 1 * 2.6
       xsiz eq 512  : resol = 2 * 2.6
       else : return
    endcase

 endelse

 x_sc_arc = xcen * resol           ; pixel of EIT's xcen, converted to arcsec
 y_sc_arc = ycen * resol 	   ; pixel of EIT's ycen

 ; make a coord system that is in arcsec

 range = findgen(sel_struc.resize_x) * resol 	; assumes square

 ; make the axis center at sun center

 xcoor = range - x_sc_arc       ; FOV (about -1331 to +1331 east to west)
 ycoor = range - y_sc_arc       ; FOV (about -1331 to +1331 south to north)


 ; make the arcsec coord system active

 plot, xcoor, ycoor, xstyle=5, ystyle=5, /noerase, /nodata, /device, $
                    position=[0,0, sel_struc.resize_x, sel_struc.resize_x]

 ; write out the time in bottom left corner
 ; images time format is yyyy-mon-dd; movies is yy-mm-dd

 if not(sel_struc.movie) then stime = strmid(cat_text,0,20) $
 else   stime = strmid(cat_text,strpos(cat_text,':')+2,19)

 stime = anytim2utc(stime,/ecs,/truncate)	; consistant YYYY/MM/DD format

 xyouts, 20, 70, stime, size=2, /device

 ; write the time to the correct out_pa array index

 item = widget_info(sel_struc.pick_list_id, /list_select)
 out_pa(item) = stime 


 ; put marks for all the previous cursor clicks

 wdone = where(strmid(out_pa, 47,5) ne '', ndone)	; ones already done

 for i = 0, ndone-1 do begin		; make mark for each

    if i ne item then begin	; dont do the current one

       xdev = float( strmid(out_pa(wdone(i)), 47, 5) ) 
       ydev = float( strmid(out_pa(wdone(i)), 57, 5) ) 

       dev_coord = convert_coord(xdev,ydev,/data, /to_device)
       xyouts, dev_coord(0)-1, dev_coord(1)-1, 'x', /device
       xyouts, dev_coord(0), dev_coord(1)+10, $
					 strtrim(wdone(i),2), /device
    endif
 endfor	


 ; displays the axis lines in arcsec coord system

 axis,xax=0,0,0,xstyle=1  &  axis,yax=0,0,0,ystyle=1    ; for debugging

return
end
