Viewing contents of file '../idllib/uit/pro/aperoi.pro'
pro aperoi,image,roi,mag,errm,skyin,bscale,badpix,textout=textout
;+
; NAME:
;	APEROI 
; PURPOSE:
;	Procedure  to compute aperture photometry for arbitrarily
;	shaped aperture defined by DEFROI (DEFine Region Of Interest)
;	The sky value is input.
;
; CALLING SEQUENCE:
;	APEROI, Image, Roi, Mag, Errm, Skyin, [ Bscale, Badpix, 
;		TEXTOUT = ]
;
; INPUTS:
;	Image  - input image array
;	Roi    - array of pixel locations defining aperture    
;	Skyin    - constant sky value used for all sources.  
;
; OPTIONAL INPUTS:
;	Bscale - Scale factor to give absolute calibration.  BSCALE may
;		be obtained from an image header HDR by the command
;			BSCALE = SXPAR(HDR,'BSCALE')
;		Set BSCALE = 1 to keep data units rather than convert to
;		magnitudes.   
;	Badpix - Two element vector giving the minimum and maximum value
;		of a good pixel (Default [-32765,32767])
;               APEROI prompts for Bscale and Badpix if not supplied
; OPTIONAL INPUT KEYWORDS:
;	TEXTOUT - Optional, textout = 3 prints to a file APER.PRT, 
;		textout = 1 (default) prints to the screen.
;
; OUTPUTS:
;	Mag   -   magnitude for region of interest.
;		After computing an aperture flux,
;		FLUX, in data units, a magnitude flux is computed from the
;		relation 
;			MAGS = -2.5 * alog10(BSCALE*FLUX) - 21.1 
;
;		If BSCALE =1 then MAGS will return the output flux in data 
;		units, and not convert to magnitudes.
;	Errm  -   NAPER by NSTAR array giving error in magnitude (or flux if
;		BSCALE =1) for each star.  If a magnitude could not be deter-
;		mined then ERRAP = 9.99 (magnitude) or -9.99 (flux units)
;
; SYSTEM VARIABLES:
;	Set !QUIET = 1 to suppress print output
;
; REVISON HISTORY:
;	1991 April 26   J. K. Hill, STX
;-                                                
;             Set parameter limits

 On_Error, 2

 if N_params() LT 3 then begin    ;Enough parameters supplied?
  print, $
  'Syntax - aperoi, image, roi, mag, errm, skyin, [ bscale, badpix, TEXTOUT = ]'
  return                       
 endif

 if not keyword_set(TEXTOUT) then textout = !TEXTOUT

 if N_elements(badpix) ne 2 then begin ;Bad pixel values supplied
GET_BADPIX:  ans = ''
	print,'Enter low and high bad pixel values, [RETURN] for defaults: '
	read,'Low and high bad pixel values [-32765,32767]: ',ans
	if ans eq  '' then badpix = [-32765,32767] else begin
	badpix = getopt(ans,'F')
	if N_elements(badpix) NE 2 then begin
		print,string(7B),'INPUT ERROR - expecting 2 scalar values'
		goto, GET_BADPIX 
	endif
 endelse
 endif 

 if N_elements(bscale) lt 1 then begin
 bscale = ''
 read,'Enter BSCALE factor for absolute calibration ([RETURN] for none): ',bscale
 if bscale eq '' then bscale = 1. else bscale = float(bscale)
 endif

 if bscale eq 1 then begin                         ;Flux units
       units = 'NET FLUX'          ;String to display flux for each aperture 
       fmt = '(F9.1,A,F9.1)'                     ;Flux format
       signerr = -1
 endif else begin                                 ;Magnitude units
       units = 'MAGNITUDES'                       
        fmt = '(F9.3,A,F6.3)'                      ;Magnitude format
       signerr = 1
 endelse

 s = size(image)
 ncol = s(1) & nrow = s(2)	;Number of columns and rows in image array

 IF textout NE 1 THEN BEGIN  ;Open output file and write header info?
    textopen,'UIT_APER'
    printf,!TEXTUNIT,'Program: APEROI '+strmid(!stime,0,20)
    printf,!TEXTUNIT,form='(a,i5,a,i5)','IMAGE SIZE - X:',ncol,' Y:',nrow
   ENDIF
 flux = total(image(roi)-skyin)
 smstvr= total(ffvar(image(roi)))
 area=n_elements(roi)
;   error1 = area*skyvar   ;Scatter in sky values
 error1=0
 error2 = smstvr        ;Random "photographic" noise 
;  error3(good) = sigsq*area(good)^2  ;Uncertainty in mean sky brightness
 error3 = 0.

 magerr = sqrt(error1 + error2 + error3)
 mag = flux
 errm = magerr
 if (bscale EQ 1.0) then return
 errm = 1.0857*magerr/flux
 mag = flux2mag(bscale*flux)

 RETURN
 END