;+
; NAME:
;     FLAGARR
; PURPOSE:
;     Returns a "flag" array with 1's were the input array is finite, and
;     0's where the input array is not finite, e.g. is NaN or Inf, etc.
;     The flag array is useful where sums and averages are needed.
; CATEGORY:
;     OVRO APC UTILITY
; CALLING SEQUENCE:
;     flg = flagarr(in[,good=good,bad=bad][,/zeroset])
; INPUTS:
;     in       The input array to be tested
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     zeroset  If this keyword is set, the non-finite values in the IN array are
;                set to zero.
; ROUTINES CALLED:
; OUTPUTS:
;     flg      The BYTE output "flag" array, of same dimensions as the IN array.
;     good     The array of indexes where the IN array is finite.
;     bad      The array of indexes where the IN array is not finite.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 22-Jan-1999 by Dale E. Gary
;     29-Jan-1999  DG
;       Changed name of routine from FLAG to FLAGARR, to avoid conflicts with
;       other routines where I used the variable FLAG.
;-
function flagarr,in,good=good,bad=bad,zeroset=zeroset

   ; Make a BYTE-sized copy of the IN array to generate an array of the same
   ; size for output
   inflg = byte(in)

   ; Determine the indexes of finite values of the IN array
   good = where(finite(in),ngood)

   ; Set the finite values to a byte value of 1
   if (ngood gt 0) then inflg(good) = 1b

   ; Determine the indexes of non-finite values of the IN array
   bad = where(finite(in) eq 0,nbad)

   ; Set the non-finite values to a byte value of 0
   if (nbad gt 0) then begin
      inflg(bad) = 0b
      ; Optionally set nonfinite values in IN array to zero
      if (keyword_set(zeroset)) then in(bad) = 0
   endif

return,inflg
end
