Pro ALIGN1, nom_image1,nom_image2,nom_dark,imas,itest
;+
; NAME:
;	ALIGN1.PRO
; PURPOSE:
;	Takes two images of EO frange, corrects it from the dark and
;       sums both, loading the result into X_display 
; CATEGORY:
;	??
; CALLING SEQUENCE:
;	 ALIGN1, nom_image1,nom_image2,nom_dark,imas,itest
; INPUTS:
;	nom_image1                  one of frange images
;       nom_image2                  frange image in oposite side
;       nom_dark                    dark image name
;	itest                       'V' or 'H' horizontal or vert alignement
; KEYWORD PARAMETERS:               
;                                   None
; OUTPUTS:
;	imas                        resulting composite image
; COMMON BLOCKS:
;	None.
; SIDE EFFECTS:
;	None
; RESTRICTIONS:
;
; PROCEDURE:
;	Straightforward.
; MODIFICATION HISTORY:
;	Written by A.LL  v.1.0       LAS  08/25/93           
;-
;on_error,2                      ;Return to caller if an error occurs
WSET, 0
VISU_CAL, nom_dark, dark, hdark
;
WSET, 1
VISU_CAL, nom_image1, ima1, hima1
ima1 = ima1-dark
imat = LOAD_IMA( ima1, -100, 8000, 1,1 )
;
WSET, 0
VISU_CAL, nom_image2, ima2, hima2
ima2 = ima2-dark 
imat = LOAD_IMA( ima2, -100, 8000, 1,1 )
;
imas = ima1+ima2
WSET, 1
imat = LOAD_IMA( imas, -100, 8000, 1,1 )
;
WSET,0
IF itest eq 'v' or itest eq 'V' then plot, imas(256,*) else  plot, imas(*,256)
;
RETURN
END

;............................................................................
Pro STAT_CURSOR, ima, nx, ny, z, FIXED=fixed
;+
; NAME:
;	STAT_CURSOR.PRO
; PURPOSE:
;	Draws a box in a image and does a statistic (mean and standard 
;       deviation inside
; CATEGORY:
;	??
; CALLING SEQUENCE:
;	STAT_CURSOR, ima
; INPUTS:
;	ima                          image array (in memory)
; KEYWORD PARAMETERS:               
;                                   None
; OUTPUTS:
;	Values
; COMMON BLOCKS:
;	None.
; SIDE EFFECTS:
;	None
; RESTRICTIONS:
;
; PROCEDURE:
;	Straightforward.
; MODIFICATION HISTORY:
;	Written by A.LL  v.1.0       LAS  08/25/93           
;-
BOX_CURSOR, x1, y1, nx, ny
;
REPEAT BEGIN
xsave = x1
ysave = y1
nxs = nx
nys = ny
x2 = x1+nx-1
y2 = y1+ny-1
PRINT, 'For area [',x1,',',y1,':',x2,',',y2,']'
STAT_IMA, ima, x1, y1, x2, y2, z
IF KEYWORD_SET(FIXED) THEN BOX_CURSOR, x1, y1, nx, ny,/INIT, /FIXED_SIZE $
ELSE                       BOX_CURSOR, x1, y1, nx, ny,/INIT
ENDREP UNTIL (x1 eq xsave) and (y1 eq ysave) and (nx eq nxs) and (ny eq nys) 
;
return
end
;

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:                        dvpt_fra.pro
; PURPOSE:                     make a polar development of OE fringe or
;                              of OI blocked fringe
; CATEGORY:                    Processing high level
; CALLING SEQUENCE:            dvpt_fra,ima_in,Xc,Yc,R,delta_R,ima_out
; INPUTS:                      ima_in                          reference's image
; OPTIONAL INPUT PARAMETERS:   Xc,Yc                           fringe's center
;                              R                               internal radius
;                              delta_R                         width of fringe
; KEYWORD PARAMETERS:          None
; OUTPUTS:                     ima_out                         result frame
; OPTIONAL OUTPUT PARAMETERS:  None
; COMMON BLOCKS:               None
; SIDE EFFECTS:                None
; RESTRICTIONS:                Applications limited to 512*512 frames
; PROCEDURE:
; MODIFICATION HISTORY:   defined by M.B 09/24/93
;                         modified by M.B 02/17/94 : LAS
;
; SCCS variables for IDL use
;
; @(#)dvpt_fra.pro  1.0 09//93 :LAS
;
;----------------------------------------------------------------------------
;
pro DVPT_FRA,ima_in,xc,yc,r1,delta_r1,ima_out
;
r2 = r1+delta_r1 - 1.
r1 = FIX(r1 + 0.5)*1.
r2 = FIX(r2 + 0.5)*1.
r12 = FLOAT(r1*r1)
r22 = FLOAT(r2*r2)
ima = make_array(512,512)
for x=xc+1,xc+1+r2 do begin $
  delta_x = ABS(x-xc)
  deltax2 = FLOAT(delta_x * delta_x)
for y=yc+1,yc+1+r2 do begin $
  delta_y = ABS(y-yc)
  deltay2 = FLOAT(delta_Y * delta_Y)
  xb = FIX(SQRT(deltax2 + deltay2) + 0.5)*1.
  theta = FIX((180./!DPI)*ATAN(delta_Y/delta_X) + 0.5)
  yb = theta*1.
  x0 = FIX(xb*COS(yb*!DPI/180.) + xc + 0.5)*1.
  y0 = FIX(xb*SIN(yb*!DPI/180.) + yc + 0.5)*1.
  ima(xb,yb) = ima_in(x0,y0)
  endfor
endfor
for x=xc-1,xc-1-r2,-1 do begin $
  delta_x = ABS(x-xc)
  deltax2 = FLOAT(delta_x * delta_x)
for y=yc+1,yc+1+r2 do begin $
  delta_y = ABS(y-yc)
  deltay2 = FLOAT(delta_Y * delta_Y)
  xb = FIX(SQRT(deltax2 + deltay2) + 0.5)*1.
  theta = 180. - FIX((180./!DPI)*ATAN(delta_Y/delta_X) + 0.5)
  yb = theta*1.
  x0 = FIX(xb*COS(yb*!DPI/180.) + xc + 0.5)*1.
  y0 = FIX(xb*SIN(yb*!DPI/180.) + yc + 0.5)*1.
  ima(xb,yb) = ima_in(x0,y0)
  endfor
endfor
for x=xc-1,xc-1-r2,-1 do begin $
  delta_x = ABS(x-xc)
  deltax2 = FLOAT(delta_x * delta_x)
for y=yc-1,yc-1-r2,-1 do begin $
  delta_y = ABS(y-yc)
  deltay2 = FLOAT(delta_Y * delta_Y)
  xb = FIX(SQRT(deltax2 + deltay2) + 0.5)*1.
  theta = 180. + FIX((180./!DPI)*ATAN(delta_Y/delta_X) + 0.5)
  yb = theta*1.
  x0 = FIX(xb*COS(yb*!DPI/180.) + xc + 0.5)*1.
  y0 = FIX(xb*SIN(yb*!DPI/180.) + yc + 0.5)*1.
  ima(xb,yb) = ima_in(x0,y0)
  endfor
endfor
for x=xc+1,xc+1+r2 do begin $
  delta_x = ABS(x-xc)
  deltax2 = FLOAT(delta_x * delta_x)
for y=yc-1,yc-1-r2,-1 do begin $
  delta_y = ABS(y-yc)
  deltay2 = FLOAT(delta_Y * delta_Y)
  xb = FIX(SQRT(deltax2 + deltay2) + 0.5)*1.
  theta = 360. - FIX((180./!DPI)*ATAN(delta_y/delta_X) + 0.5)
  yb = theta*1.
  x0 = FIX(xb*COS(yb*!DPI/180.) + xc + 0.5)*1.
  y0 = FIX(xb*SIN(yb*!DPI/180.) + yc + 0.5)*1.
  ima(xb,yb) = ima_in(x0,y0)
  endfor
endfor
ima_bis = make_array(512,512)
ima_bis(0:511,0:511) = ima(0:511,0:511)
;
ima_out = ima_bis
;
WSET,1
ERASE
b = MAX(ima_out(0:511,0:511))
TVSCL,BYTSCL(ima_out,MIN=0.,MAX=b)
WSET,0
ERASE
PLOT,ima_out(r1,0:359),YRANGE=[0.,b],LINESTYLE=0
OPLOT,ima_out(r2,0:359),LINESTYLE=1
rmoy = FIX((r1 + r2)/2.)
OPLOT,ima_out(rmoy,0:359),LINESTYLE=2
STAT_IMA,ima_out,R1,0,R2,359
;
RETURN
END
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:                        ipolarfra.pro
; PURPOSE:                     make the inverse of polar transformation 
;                              for OE developped fringe or for OI blocked 
;                              developped fringe
; CATEGORY:                    Processing high level
; CALLING SEQUENCE:            ipolarfra,ima_in,Xc,Yc,R,delta_R,ima_out
; INPUTS:                      ima_in                          reference's image
; OPTIONAL INPUT PARAMETERS:   Xc,Yc                           fringe's center
;                              R                               internal radius
;                              delta_R                         width of fringe
; KEYWORD PARAMETERS:          None
; OUTPUTS:                     ima_out                         result frame
; OPTIONAL OUTPUT PARAMETERS:  None
; COMMON BLOCKS:               None
; SIDE EFFECTS:                None
; RESTRICTIONS:                Applications limited to 512*512 frames
; PROCEDURE:
; MODIFICATION HISTORY:    defined by M.B 02/17/94 : LAS
;
; SCCS variables for IDL use
;
; @(#)ipolarfra.pro  1.0 02/17/94 : LAS
;
;----------------------------------------------------------------------------
;
pro ipolarfra,ima_in,xc,yc,r1,delta_r1,ima_out
;
r2 = r1+delta_r1 - 1.
r1 = FIX(r1 + 0.5)*1.
r2 = FIX(r2 + 0.5)*1.
ima = fltarr(512,512)
;
k=0
l=0
for i=r1,r2 do begin $
  for j=0.,359. do begin $
    k = fix(i*cos(!dpi*j/180.) + xc + 0.5)
    l = fix(i*sin(!dpi*j/180.) + yc + 0.5)
    ima(k,l) = ima_in(i,j)
  endfor
endfor
ima_out = ima
;
WSET,1
ERASE
b = MAX(ima_out(0:511,0:511))
TVSCL,BYTSCL(ima_out,MIN=0.,MAX=b)
WSET,0
ERASE
PLOT,ima_out(xc,*),YRANGE=[0.,b],LINESTYLE=0
OPLOT,ima_out(*,yc),LINESTYLE=1
STAT_IMA,ima_out,0,0,511,511
;
RETURN
END
;
FUNCTION MEAN,IMA,X1,X2,Y1,Y2
PRINT, 'mean :'
RETURN, TOTAL(IMA(X1:X2,Y1:Y2))/N_ELEMENTS(IMA(X1:X2,Y1:Y2))
END
;
FUNCTION GRAD,IMA
RETURN, ABS(IMA - SHIFT(IMA,1,0)) + ABS(IMA - SHIFT(IMA,0,1))
END
;
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:                        gener_mask.pro
; PURPOSE:                     make a circular mask for photometry's studies
; CATEGORY:                    Processing high level
; CALLING SEQUENCE:            gener_mask,ima_name,npix,Xc,Yc,R,intval,extval
; INPUTS:                      ima_name                         Name of mask
;                              npix                             dimension
; OPTIONAL INPUT PARAMETERS:   Xc,Yc                            circle's coords
;                              R                                radius of mask
;                              intval                           value inside
;                              extval                           value outside
; KEYWORD PARAMETERS:          None
; OUTPUTS:                     ima_name                         mask's frame
; OPTIONAL OUTPUT PARAMETERS:  None
; COMMON BLOCKS:               None
; SIDE EFFECTS:                None
; RESTRICTIONS:                None
; PROCEDURE:
; MODIFICATION HISTORY:   defined by M.B 09/10/93
;                         modified by M.B 02/17/94 : LAS
;
; SCCS variables for IDL use
;
; @(#)gener_mask.pro  1.0 09/10/93 :LAS
;
;----------------------------------------------------------------------------
;
pro GENER_MASK,ima_name,npix,Xc,Yc,R,intval,extval
;
npx = npix/2
R = fix(R + 0.5)
ima_name = MAKE_ARRAY(npix,npix)
ima_name(0:npix-1,0:npix-1)=extval
;
R2 = (float(R))^2
dim = 2*R + 1
msk = fltarr(dim,dim)
ox = dim/2
oy = dim/2
for i=0,dim-1 do begin $
  for j=0,dim-1 do begin $
    rx = i - ox
    ry = j - oy
    rx2 = (float(rx))^2
    ry2 = (float(ry))^2
    rxy2 = rx2 + ry2
    if (rxy2 le R2) then begin $
      msk(i,j) = intval
    endif else begin $
      msk(i,j) = extval
    endelse
  endfor
endfor
dx = (npix-1.)/2. - xc
dy = (npix-1.)/2. - yc
xc = (npix-1.)/2. + dx
yc = (npix-1.)/2. + dy
ima_name(npx-ox:npx-ox+dim-1,npx-oy:npx-oy+dim-1) = msk
ima_name = rot_int(ima_name,0.,1.,Xc,Yc)
WSET,1
ERASE
TVSCL,BYTSCL(ima_name,MIN=MIN(ima_name),MAX=MAX(ima_name))
WSET,0
ERASE
PLOT,ima_name(*,Yc),YRANGE=[MIN(ima_name)-1,MAX(ima_name)+1],LINESTYLE=0
OPLOT,ima_name(Xc,*),LINESTYLE=1
STAT_IMA,ima_name,0,0,npix-1,npix-1
RETURN
END
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:                        diafrai.pro
; PURPOSE:                     apply a circular mask for photometry's studies
;                              to a selected frame 
;                              (define a circular ROI or apply a diaphragm)
; CATEGORY:                    Processing high level
; CALLING SEQUENCE:            diafrai,ima_name,Xc,Yc,R,intval,extval
; INPUTS:                      ima_name                         Name of image
; OPTIONAL INPUT PARAMETERS:   Xc,Yc                            circle's coords
;                              R                                radius of mask
;                              intval      value inside to be multiplied
;                              extval      value outside to be multiplied
; KEYWORD PARAMETERS:          None
; OUTPUTS:                     ima_name                        image with mask
; OPTIONAL OUTPUT PARAMETERS:  None
; COMMON BLOCKS:               None
; SIDE EFFECTS:                None
; RESTRICTIONS:                None
; PROCEDURE:
; MODIFICATION HISTORY:   defined by M.B 02/17/94 : LAS
;
; SCCS variables for IDL use
;
; @(#)diafrai.pro  1.0 02/17/94 :LAS
;
;----------------------------------------------------------------------------
;
pro diafrai,ima_name,Xc,Yc,R,intval,extval
;
sizima = size(ima_name)
dimima = sizima(1)
gener_mask,mask,dimima,Xc,Yc,R,intval,extval
ima_name = ima_name*mask
WSET,1
ERASE
TVSCL,BYTSCL(ima_name,MIN=MIN(ima_name),MAX=MAX(ima_name))
WSET,0
ERASE
PLOT,ima_name(*,Yc),YRANGE=[MIN(ima_name),MAX(ima_name)],LINESTYLE=0
OPLOT,ima_name(Xc,*),LINESTYLE=1
STAT_IMA,ima_name,0,0,dimima-1,dimima-1
RETURN
END
;

;............................................................................
Pro WRITIMA, ima, val, nx, ny, z, FIXED=fixed
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:
;	WRITIMA.PRO
; PURPOSE:
;	Draws a box in an image and replaces all pixels values inside
;       the box by the given value
; CATEGORY:
;	??
; CALLING SEQUENCE:
;	WRITIMA, ima, val
; INPUTS:
;	ima                          image array (in memory)
;       val                          value to be written
; KEYWORD PARAMETERS:               
;                                   None
; OUTPUTS:
;	ima with the selected intervals replaced
; COMMON BLOCKS:
;	None.
; SIDE EFFECTS:
;	None
; RESTRICTIONS:
;
; PROCEDURE:
;	Straightforward.
; MODIFICATION HISTORY:
;	Written by M.B  v.1.0       LAS  12/13/93           
;----------------------------------------------------------------------------
BOX_CURSOR, x1, y1, nx, ny
;
REPEAT BEGIN
xsave = x1
ysave = y1
nxs = nx
nys = ny
x2 = x1+nx-1
y2 = y1+ny-1
PRINT, 'For area [',x1,',',y1,':',x2,',',y2,']'
PRINT, 'Value to be written :',val
ima(x1:x2,y1:y2) = val
IF KEYWORD_SET(FIXED) THEN BOX_CURSOR, x1, y1, nx, ny,/INIT, /FIXED_SIZE $
ELSE                       BOX_CURSOR, x1, y1, nx, ny,/INIT
ENDREP UNTIL (x1 eq xsave) and (y1 eq ysave) and (nx eq nxs) and (ny eq nys) 
;
return
end
;

;............................................................................
Pro REPLACIMA, IMA, minval, maxval, selectval
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:
;	REPLACIMA.PRO
; PURPOSE:
;         Select an interval of pixels values and replace these one by another
; CATEGORY:
;	??
; CALLING SEQUENCE:
;	REPLACIMA, IMA, minval, maxval, selectval
; INPUTS:
;	ima                          image array (in memory)
;       minval                       lower cuts value selected
;       maxval                       higher cuts value selected
;       selectval                    new value of pixels between lcuts & hcuts
; KEYWORD PARAMETERS:               
;                                   None
; OUTPUTS:
;          ima with the selected intervals replaced
; COMMON BLOCKS:
;	None.
; SIDE EFFECTS:
;	None
; RESTRICTIONS:
;
; PROCEDURE:
;	Straightforward.
; MODIFICATION HISTORY:
;	Written by M.B  v.1.0       LAS  12/13/93           
;----------------------------------------------------------------------------
MASK = (IMA GE minval) AND (IMA LE maxval)
IMAB = IMA*MASK
FOR I=0,511 DO BEGIN $
  FOR J=0,511 DO BEGIN $
    IF (IMAB(I,J) NE 0.) THEN BEGIN $
       IMAB(I,J) = selectval
    ENDIF
  ENDFOR
ENDFOR
IMAC = IMAB
IMAC = IMAB + IMA*(1.-MASK)
IMA = IMAC
;
return
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Pro build_fraoe, image1, image2, image3, image4, oefringe
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NAME:
;	BUILD_FRAOE
; PURPOSE:
;	Takes four images of EO fringe, corrects them from the dark and
;       builds composit frame, loading the result into X_display.
;
; CATEGORY:
;	calibration, manipulation
; CALLING SEQUENCE:
;	 BUILD_FRAOE, image1, image2, image3, image4, oefringe
; INPUTS:
;	image1                      first arc of fringe (up)
;       image2                      second & opposite arc of fringe (down)
;       image3                      third arc of fringe (left)
;       image4                      fourth & opposite arc of fringe (right)
;
; KEYWORD PARAMETERS:               
;                                   None
; OUTPUTS:
;	oefringe                    resulting composit image
; COMMON BLOCKS:
;	None
; SIDE EFFECTS:
;	None
; RESTRICTIONS:
;       None
; PROCEDURE:
;	Straightforward.
; MODIFICATION HISTORY:
;	Written by M.B  v.1.0       LAS  03/03/94           
;-------------------------------------------------------------------------------
;
print,'>>> visualization of the 1st oe arc of fringe <<<'
WSET, 0
PRO_CAL, IMAGE1, DRK, HDRK, IMA1, HIMA1
;
print,'>>> visualization of the 2nd oe arc of fringe <<<'
WSET, 1
PRO_CAL, IMAGE2, DRK, HDRK, IMA2, HIMA2
;
print,'>>> visualization of the 3rd oe arc of fringe <<<'
WSET, 0
PRO_CAL, IMAGE3, DRK, HDRK, IMA3, HIMA3
;
print,'>>> visualization of the 4th oe arc of fringe <<<'
WSET, 1
PRO_CAL, IMAGE4, DRK, HDRK, IMA4, HIMA4
;
oefringe = fltarr(512,512)
oefringe = ima1 + ima2 + ima3 + ima4
tvscl,bytscl(oefringe,min=min(oefringe),max=max(oefringe))
print,'>>> determination of the center coordinates & radius value <<<'
pcursor,oefringe,tab_name
circcntr,oefringe,tab_name,xmoy,ymoy,r
xc = xmoy
yc = ymoy
rext = r + 30.
rint = r - 30.
print,'>>> creation of the external mask that limits the oe fringe <<<'
gener_mask,maskext,512,xmoy,ymoy,rext,1.,0.
print,'>>> creation of the internal mask that limits the oe fringe <<<'
gener_mask,maskint,512,xmoy,ymoy,rint,1.,0.
maskfra = maskext-maskint
bckgrd = ((1.-maskfra)*(ima1 + ima2 + ima3 + ima4))/4.
fondval = median(bckgrd)
oefringe = oefringe-3.*fondval
oefra = maskfra*oefringe
print,'>>> addition of background & reconstructed oe fringe <<<'
oefringe = oefra + bckgrd
;
oefringe = oefringe > 0.
wset,0
print,'>>> visualization of resulting fringe <<<'
tvscl,bytscl(oefringe,min=min(oefringe),max=max(oefringe))
wset,1
plot,oefringe(*,yc),linestyle=0
oplot,oefringe(xc,*),linestyle=1
print,'>>> statistics on oe fringe <<<'
STAT_IMA,oefringe,0,0,511,511
;
print,'end of job'
;
RETURN
END
