Viewing contents of file '../idllib/uit/pro/bigdiv.pro'
pro bigdiv,image1,image2,scale,image3
;+
; NAME
;	BIGDIV
;
; PURPOSE:
;	Divide a image in one file by another (flatfield) image and write to 
;	a third file.   Do in 8 steps to save virtual memory.   The numerator
;	image and the denominator image must be the same size, INTEGER*2 
;	datatype and the dimensions must be an even multiple of 512 x 512 
;	(usually 2048 x 2048).
;
; CALLING SEQUENCE:
;	bigdiv, image1, image2, scale, image3
;
; INPUTS:
;	image1 - filename of the INTEGER*2 STSDAS numerator image.
;	image2 - filename of the INTEGER*2 SDAS denominator image.  
;	scale  -  scale factor, scalar: ratio = num*scale/denom 
;		The scale factor will always be converted to floating point.
;	image3 - name of output file to write quotient image.   The division is
;		done using REAL*4 arithmetic which is then converted to
;		INTEGER*2.
;
; NOTES:
;	BIGDIV make the following adjustments to the division:
;	(1)   BIGDIV will change any 0 pixels in the denominator image to 1, 
;		to avoid divide by zero.
;	(2)   Any input values of 32766 in the numerator image will be set to 
;		32767 in the quotient image
;	(3)   Any input values of 32767 in the denominator image will be set to 
;		0 in the quotient image
;
; EXAMPLE:
;	Divide the image 'FUV0342_LIN' by the flatfield image 'B1M', scale
;	by 270 and put the quotient image in 'FUV0342F'
;
;	IDL> bigdiv,'FUV0342_LIN','B1M',270,'FUV0324F'
;
;  MODIFICATION HISTORY
;	written by Wayne Landsman, STX
;	modified to handle saturated pixels, Bob C., STX 1/30/91
;	Problem indexing bad pixels  corrected  Wayne L.  STX  27-Sep-91
;	Do image sizes any multiple of 512 x 512 rather than just 2048 x 2048
;                                           Wayne L.  Nov-91
;-
 On_error,2

 if N_params() LT 3 then begin
     print,'Syntax - bigdiv, image1, image2, scale, image3
     return
 endif   

 if N_elements(image1) EQ 0 then imag1 = ''  else imag1 = image1
 if N_elements(image2) EQ 0 then imag2 = ''  else imag2 = image2
 if N_elements(image3) EQ 0 then imag3 = ''  else imag3 = image3

;  Get file names if not supplied.

 if imag1 EQ '' then read,'Enter numerator image file name: ',imag1
 if imag2 EQ '' then $ 
    read,'Enter denominator (flat field) image file name: ',imag2
 if imag3 EQ '' then $
    read,'Enter output image file name: ',imag3

 sxopen,1,imag1,h1
 sxopen,2,imag2,h2
 im1_dimen = sxpar( h1, 'NAXIS*' )
 im2_dimen = sxpar( h2, 'NAXIS*' )
 xsize = im1_dimen(0)
 ysize = im1_dimen(1)
 if ( xsize NE im2_dimen(0) )  or $
    ( ysize NE im2_dimen(1) ) then begin
    message,'ERROR -Numerator and denominator images must be the same size',/CON
    print,'Numerator image: ',strn(im1_dimen)
    print,'Denominator image: ',strn(im2_dimen)
 endif

 if ( (xsize mod 512) NE 0) or ((ysize mod 512) NE 0) then $
     message,'ERROR - Image size must be multiple of 512'

 subsize = xsize/8

 a1 = assoc(1,intarr( xsize,subsize ,/NOZERO ))
 a2 = assoc(2,intarr( xsize,subsize ,/NOZERO ))
 ratio = intarr( xsize, im1_dimen(1), /NOZERO )

 message,'Now beginning division of two images',/INF
 scale = float(scale)
 for i = 0l,7 do begin
	im1 = a1(i)   & im2 = a2(i)
	nbig =   where(im1 EQ 32767,f0)
	dsmall = where(im2 EQ 0,f1)
	if (f1 gt 0) then im2 (dsmall) = 1
	dbig = where (im2 EQ 32767, f2)

    ratio( 0,subsize*i ) = fix( nint( (scale* im1)/im2, /LONG ) < 32767)

    ioffset = i*subsize*long(xsize) 
    if ( f1 GT 0 ) then ratio(dsmall + ioffset)= 32767
    if ( f2 GT 0 ) then ratio(dbig + ioffset) = 0
    if ( f0 GT 0 ) then ratio(nbig + ioffset) = 32767
    print,'Process line #',subsize*i
 endfor

 h3 = h1
 sxaddhist,strmid(!STIME,0,11) + 'Flat-field division',h3
 stwrt,ratio,h3,image3

 return
 end