;---------------------------------------------------------------------------
; Document name: OECORRECT.PRO
; Created by:    Udo Schuehle, MPAE Lindau, 
; February 2003
;
; Last Modified:
;---------------------------------------------------------------------------
;

PRO oecorrect, filespec, outpath = outpath
   
;+
; PROJECT:
;       SOHO - SUMER
;
; NAME:
;       oecorrect
;
; PURPOSE: find the odd/even pixel sensitivity difference from SUMER images
;          that are caused by the nonlinearity of the detector ADC 
;          and create a correction matrix for A and B detectors
;
; CATEGORY:
;       
; 
; EXPLANATION:
;       
; SYNTAX: 
;       oecorrect, filespec
;
; EXAMPLES:
;       
; INPUTS:
;       filespec - files to process
;       
;
; OPTIONAL INPUTS: 
;       None.
;
; OUTPUTS:
;       The file ODDEVEN_ARRAY_A.RST and ODDEVEN_ARRAY_B containing the data 
;       to correct images by using the SUM_ODDEVEN.PRO procedure
;
; OPTIONAL OUTPUTS:
;       None.
;
; KEYWORDS: 
;       None.
;
; COMMON:
;       None.
;
; RESTRICTIONS: 
;       Expects data files that are decompressed and reversed!!!
;      
;
; SIDE EFFECTS:
;       None.
;
; HISTORY:
;       Version 1, February, 2003, Udo Schuehle, MPAE Lindau.
; 
; CONTACT:
;       Udo Schuehle, MPAE Lindau (schuehle@linmpi.mpg.de)
;-
;
   ON_ERROR, 2
      
oeall=fltarr(360)
oe=fltarr(360)
odd_even_a = fltarr(1024,360)
odd_even_b = fltarr(1024,360)

;---------------------------------------------------------------------------
;  look for files by filespec
;---------------------------------------------------------------------------

   f$ = findfile(filespec, count=fnum)
   PRINT,fnum,' Files to process'
   
   FOR i=0, fnum-1 DO BEGIN

      RESTORE, f$(i)
      
      break_file, f$(i), dir_log, dir_t, filnam, ext_t
      
      
;---------------------------------------------------------------------------
;  Determine which detector
;---------------------------------------------------------------------------
      

      det = detector(header_data)
      det = det(0)
      
       CASE det OF
          0: detector = 'no'
          1: detector = 'a'
          2: detector = 'b'
          3: detector = 'rsc'
       ENDCASE


;---------------------------------------------------------------------------
;  filter oddeven pattern from image collumns
;---------------------------------------------------------------------------
     

	im=image
	tt=total(image,1)
	tt(WHERE(tt EQ 0))=1

	uu=shift(tt,1)
	vv=0.5*(tt+uu)

	oe=tt/vv
	oe(359)=1

	oeall=oeall+oe

ENDFOR

	oevector=oeall/fnum

;---------------------------------------------------------------------------
;  determine the average of this oddeven pattern & create correction array
;---------------------------------------------------------------------------
;  for detector A ...
;---------------------------------------------------------------------------
IF (det EQ 1) then BEGIN

	oev_a = oevector(10:300)

	ho = WHERE(oev_a gt 1,counto)
	hu = WHERE(oev_a lt 1,countu)

	pos_a = oev_a(ho)
	neg_a = oev_a(hu)
	avr_ao = total(pos_a)/counto
	avr_au = total(neg_a)/countu

print,detector,' ',avr_ao,' ',counto,' ',avr_au,' ',countu

	ss = fltarr(360)
	ss(WHERE(oevector GT 1)) = avr_ao
	ss(WHERE(oevector LE 1)) = avr_au

	uu = fltarr(1024)+1

	odd_even_a = uu#ss

;	 SAVE, file='oddeven_array_a',header_data, odd_even_a, /XDR

ENDIF ELSE BEGIN

;----------------------------------------------------------------------------
; ... and Detector B
;----------------------------------------------------------------------------

	oev_b = oevector(10:310)

	ho = WHERE(oev_b gt 1,counto)
	hu = WHERE(oev_b lt 1,countu)

	pos_b = oev_b(ho)
	neg_b = oev_b(hu)
	avr_bo = total(pos_b)/counto
	avr_bu = total(neg_b)/countu

print,detector,' ',avr_bo-1,' ',counto,' ',avr_bu-1,' ',countu

	ss = fltarr(360)
	ss(WHERE(oevector GE 1)) = avr_bo
	ss(WHERE(oevector LT 1)) = avr_bu

	uu = fltarr(1024)+1

	odd_even_b = uu#ss

	SAVE, file='oddeven_array_b',header_data, odd_even_b, /XDR

ENDELSE

;-------------------------------------------------------------------------
; check results
;-------------------------------------------------------------------------
;
;	image=im/odd_even_b
;
;plot,total(image,1)
;wait,1
;oplot,total(im,1)
;wait,4
;tvscl, rebin(image(100:199,100:199),500,500),0
;tvscl, rebin(im(100:199,100:199),500,500),1
;
;
;---------------------------------------------------------------------------
;  plot routine
;---------------------------------------------------------------------------
;
;set_plot,'ps'
;device,filename='plot1.ps'
;plot,image(100,100:199)
;oplot,im(100,100:199)
;device,/close
;wait,15
;
;device,filename='plot2.ps'
;plot,oeall(100:199)/fnum,yrange=[0.75,1.25],title='Odd/even pattern from all ff-files'
;device,/close
;set_plot,'x'
;--------------------------------------------------------------------------

END

;---------------------------------------------------------------------------
; End of 'OECORRECT.PRO'.
;---------------------------------------------------------------------------







