Viewing contents of file '../idllib/sdss/allpro/flag_select.pro'
pro flag_select, pstruct, flag_struct, colorindex, select_index, objc=objc, input_index=input_index

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;+
;
; NAME:
;    FLAG_SELECT
; PURPOSE:
;    Makes cuts based on flag structure. These cuts are strictly "anded" 
;    together, so they must all be true for the object to survive.
; 
; Inputs:  pstruct: a photo output structure (must have .flags tag...)
;	   flag_struct: Premade flag structure. This will require any
;		flags set to 'Y' and insist that any flag set to 'N' be
;		off
;	   colorindex: which color do you want to cut on...
;	   objc: set if you want to select on objc flags
;	   input_index: you can input an index, from an earlier selection
;		for instance. If this has size(input_index)(0)=0 then
;		the returned selection index will be -1
;
; Outputs: select_index: indices of selected objects....
;
; Author:  Tim McKay
; Date: 1/8/99
; Phil Fischer: 1/15/99
; Erin Scott Sheldon UM 2/5/00 Added flags2
;-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Help message
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  if n_params() LT 3 then begin
      print,'-syntax flag_select, pstruct, flag_struct, colorindex, select_index, objc=objc, input_index=input_index'
      return
  endif

  if keyword_set(input_index) then begin
	if ((size(input_index))(0) eq 0) then begin
	   select_index = -1
	   return
	endif else begin
	   k = input_index
	endelse
  endif else begin
	k=lindgen(n_elements(pstruct))
  endelse

  tags=tag_names(pstruct)
  w1=where(tags EQ 'FLAGS2', nw1)
  w2=where(tags EQ 'OBJC_FLAGS2', nw2)

  q=lindgen(n_elements(pstruct))

  IF NOT keyword_set(objc) THEN BEGIN
      f=long(pstruct(q).flags(colorindex))
      IF nw2 EQ 0 THEN BEGIN
          doflags2 = 0
      ENDIF ELSE BEGIN 
          doflags2 = 1
          f2=long(pstruct(q).flags2(colorindex))
      ENDELSE 
  ENDIF ELSE BEGIN 
      f=long(pstruct(q).objc_flags)
      IF nw1 EQ 0 THEN BEGIN
          doflags2 = 0
      ENDIF ELSE BEGIN 
          doflags2 = 1
          f2=long(pstruct(q).objc_flags2)
      ENDELSE 
  ENDELSE 
  
  fs=flag_struct

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; Begin flags
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  if (fs.CANONICAL_CENTER eq 'Y') then begin
	h=where((f(k) and 2L^0) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.CANONICAL_CENTER eq 'N') then begin
	h=where((f(k) and 2L^0) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BRIGHT eq 'Y') then begin
	h=where((f(k) and 2L^1) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BRIGHT eq 'N') then begin
	h=where((f(k) and 2L^1) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.EDGE eq 'Y') then begin
	h=where((f(k) and 2L^2) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.EDGE eq 'N') then begin
	h=where((f(k) and 2L^2) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BLENDED eq 'Y') then begin
	h=where((f(k) and 2L^3) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BLENDED eq 'N') then begin
	h=where((f(k) and 2L^3) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.CHILD eq 'Y') then begin
	h=where((f(k) and 2L^4) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.CHILD eq 'N') then begin
	h=where((f(k) and 2L^4) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.PEAKCENTER eq 'Y') then begin
	h=where((f(k) and 2L^5) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.PEAKCENTER eq 'N') then begin
	h=where((f(k) and 2L^5) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NODEBLEND eq 'Y') then begin
	h=where((f(k) and 2L^6) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NODEBLEND eq 'N') then begin
	h=where((f(k) and 2L^6) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NOPROFILE eq 'Y') then begin
	h=where((f(k) and 2L^7) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NOPROFILE eq 'N') then begin
	h=where((f(k) and 2L^7) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NOPETRO eq 'Y') then begin
	h=where((f(k) and 2L^8) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NOPETRO eq 'N') then begin
	h=where((f(k) and 2L^8) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.MANYPETRO eq 'Y') then begin
	h=where((f(k) and 2L^9) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.MANYPETRO eq 'N') then begin
	h=where((f(k) and 2L^9) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NOPETRO_BIG eq 'Y') then begin
	h=where((f(k) and 2L^10) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NOPETRO_BIG eq 'N') then begin
	h=where((f(k) and 2L^10) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.DEBLEND_TOO_MANY_PEAKS eq 'Y') then begin
	h=where((f(k) and 2L^11) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.DEBLEND_TOO_MANY_PEAKS eq 'N') then begin
	h=where((f(k) and 2L^11) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.CR eq 'Y') then begin
	h=where((f(k) and 2L^12) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.CR eq 'N') then begin
	h=where((f(k) and 2L^12) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.MANYR50 eq 'Y') then begin
	h=where((f(k) and 2L^13) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.MANYR50 eq 'N') then begin
	h=where((f(k) and 2L^13) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.MANYR90 eq 'Y') then begin
	h=where((f(k) and 2L^14) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.MANYR90 eq 'N') then begin
	h=where((f(k) and 2L^14) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BAD_RADIAL eq 'Y') then begin
	h=where((f(k) and 2L^15) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BAD_RADIAL eq 'N') then begin
	h=where((f(k) and 2L^15) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.INCOMPLETE_PROFILE eq 'Y') then begin
	h=where((f(k) and 2L^16) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.INCOMPLETE_PROFILE eq 'N') then begin
	h=where((f(k) and 2L^16) eq 0)
	k=k(h)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
  endif

  if (fs.INTERP eq 'Y') then begin
	h=where((f(k) and 2L^17) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.INTERP eq 'N') then begin
	h=where((f(k) and 2L^17) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.SATUR eq 'Y') then begin
	h=where((f(k) and 2L^18) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.SATUR eq 'N') then begin
	h=where((f(k) and 2L^18) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NOTCHECKED eq 'Y') then begin
	h=where((f(k) and 2L^19) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NOTCHECKED eq 'N') then begin
	h=where((f(k) and 2L^19) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.SUBTRACTED eq 'Y') then begin
	h=where((f(k) and 2L^20) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.SUBTRACTED eq 'N') then begin
	h=where((f(k) and 2L^20) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.NOSTOKES eq 'Y') then begin
	h=where((f(k) and 2L^21) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.NOSTOKES eq 'N') then begin
	h=where((f(k) and 2L^21) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BADSKY eq 'Y') then begin
	h=where((f(k) and 2L^22) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BADSKY eq 'N') then begin
	h=where((f(k) and 2L^22) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.PETROFAINT eq 'Y') then begin
	h=where((f(k) and 2L^23) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.PETROFAINT eq 'N') then begin
	h=where((f(k) and 2L^23) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.TOO_LARGE eq 'Y') then begin
	h=where((f(k) and 2L^24) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.TOO_LARGE eq 'N') then begin
	h=where((f(k) and 2L^24) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.DEBLENDED_AS_PSF eq 'Y') then begin
	h=where((f(k) and 2L^25) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.DEBLENDED_AS_PSF eq 'N') then begin
	h=where((f(k) and 2L^25) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.DEBLEND_PRUNED eq 'Y') then begin
	h=where((f(k) and 2L^26) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.DEBLEND_PRUNED eq 'N') then begin
	h=where((f(k) and 2L^26) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.ELLIPFAINT eq 'Y') then begin
	h=where((f(k) and 2L^27) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.ELLIPFAINT eq 'N') then begin
	h=where((f(k) and 2L^27) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BINNED1 eq 'Y') then begin
	h=where((f(k) and 2L^28) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BINNED1 eq 'N') then begin
	h=where((f(k) and 2L^28) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BINNED2 eq 'Y') then begin
	h=where((f(k) and 2L^29) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BINNED2 eq 'N') then begin
	h=where((f(k) and 2L^29) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.BINNED4 eq 'Y') then begin
	h=where((f(k) and 2L^30) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.BINNED4 eq 'N') then begin
	h=where((f(k) and 2L^30) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  if (fs.MOVED eq 'Y') then begin
	h=where((f(k) and 2L^31) ne 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif 
  if (fs.MOVED eq 'N') then begin
	h=where((f(k) and 2L^31) eq 0)
	if ((size(h))(0) eq 0) then begin
	     select_index = -1
	     return
	endif
	k=k(h)
  endif

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; begin flags2
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  IF NOT doflags2 THEN BEGIN
      select_index=k
      return
  ENDIF 

  if (fs.DEBLENDED_AS_MOVING eq 'Y') then begin
      h=where((f2(k) and 2L^0) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.NODEBLEND_MOVING eq 'N') then begin
      h=where((f2(k) and 2L^0) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif

  if (fs.TOO_FEW_DETECTIONS eq 'Y') then begin
      h=where((f2(k) and 2L^1) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.BAD_MOVING_FIT eq 'N') then begin
      h=where((f2(k) and 2L^1) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  ENDIF

  if (fs.STATIONARY eq 'Y') then begin
      h=where((f2(k) and 2L^2) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.PEAKS_TOO_CLOSE eq 'N') then begin
      h=where((f2(k) and 2L^2) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif

  if (fs.MEDIAN_CENTRE eq 'Y') then begin
      h=where((f2(k) and 2L^3) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.LOCAL_EDGE eq 'N') then begin
      h=where((f2(k) and 2L^3) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif

  if (fs.BAD_COUNTS_ERROR eq 'Y') then begin
      h=where((f2(k) and 2L^4) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.BAD_MOVING_FIT_CHILD eq 'N') then begin
      h=where((f2(k) and 2L^4) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  ENDIF

  if (fs.DEBLEND_UNASSIGNED_FLUX eq 'Y') then begin
      h=where((f2(k) and 2L^5) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.SATUR_CENTER eq 'N') then begin
      h=where((f2(k) and 2L^5) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif

  if (fs.INTERP_CENTER eq 'Y') then begin
      h=where((f2(k) and 2L^6) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.DEBLENDED_AT_EDGE eq 'N') then begin
      h=where((f2(k) and 2L^6) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif

  if (fs.DEBLEND_NOPEAK eq 'Y') then begin
      h=where((f2(k) and 2L^7) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.PSF_FLUX_INTERP eq 'N') then begin
      h=where((f2(k) and 2L^7) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  ENDIF

  if (fs.TOO_FEW_GOOD_DETECTIONS eq 'Y') then begin
      h=where((f2(k) and 2L^8) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.MEASURED eq 'N') then begin
      h=where((f2(k) and 2L^8) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  ENDIF

  if (fs.GROWN_MERGED eq 'Y') then begin
      h=where((f2(k) and 2L^9) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 
  if (fs.HAS_CENTER eq 'N') then begin
      h=where((f2(k) and 2L^9) eq 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  ENDIF

  if (fs.MEASURE_BRIGHT eq 'Y') then begin
      h=where((f2(k) and 2L^10) ne 0)
      if ((size(h))(0) eq 0) then begin
          select_index = -1
          return
      endif
      k=k(h)
  endif 

  select_index=k 


return

end