Viewing contents of file '../idllib/sdss/allpro/extract_galaxies.pro'
pro extract_galaxies, pstruct, color_index, ostruct, no_objc_type=no_objc_type, max_mag=max_mag, indices=indices, silent=silent

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; NAME: 
;    EXTRACT_GALAXIES
;
; PURPOSE:
;     Extracts a clean set of galaxies
; 
; Inputs:  pstruct: input photo structure 
;	   color_index: bandpass to select on:
;          ostruct: output photo structure containing galaxies
;          max_mag: maximum magnitude to use (default=24.0)
;          no_objc_type:  don't use the photo objc_type to pick galaxies
;	   silent: don't make plots
;
; Outputs: Plots flags for these objects....
;	   indices: returns the indices of the galaxies in the original
;			struct
;
; Author:  Phil Fischer
; Date: 1/14/99
; Altered to get galaxies: Erin Scott Sheldon
; Date: 2/19/99
; Added silent and indices options: Tim McKay
; Date: 6/10/99
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

if n_params() LT 2 then begin
   print,'-syntax extract_galaxies, pstruct, color_index, ostruct,no_objc_type=no_objc_type, max_mag=max_mag, indices=indices, silent=silent'
   return
endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; if requested, use photo type classifier to make the first cut
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  print,'Number of entries: ',n_elements(pstruct)

  if (not keyword_set(photo_type) ) then begin
	_ss=where(pstruct.objc_type eq 3)
	index=_ss
	ostruct=pstruct(_ss)
        print,'After objc_type cut: ',n_elements(ostruct)
  endif else begin
	_ss = where(pstruct.objc_type ne 100)
	index=_ss
	ostruct=pstruct
  endelse

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; extract based on object1 flags in selected bandpass
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  make_flag_struct, fs
  fs.canonical_center='N' 
  fs.edge='N' 
  fs.blended='N' 
  ;;;;;;;;;;;;;;;;;;;;;; fs.child='N' ;;; don't care if galaxies are children
  ;;;;;;;;;;;;;;;;;;;;;;fs.peakcenter='N' 
  fs.nodeblend='N' 
  fs.nopetro='N'   
  ;;;;;;;;;;;;;;;;;;;;;;fs.manypetro='N'   
  fs.manyr50='N'   
  fs.manyr90='N'   
  fs.incomplete_profile='N'   
  fs.interp='N'   
  fs.notchecked='N'   
  fs.subtracted='N'   
  fs.nostokes='N'   
  fs.badsky='N'   
  ;;;;;;;;;;;;;;;;;;;;;fs.petrofaint='N'   
  fs.too_large='N'   
  fs.deblended_as_psf='N'
  fs.deblend_pruned='N'   
  fs.ellipfaint='N'   
  fs.moved='N'   
  flag_select,ostruct,fs,color_index,_ss

  index=index(_ss)
  ostruct=ostruct(_ss)
  print,'After object1 cuts: ',n_elements(ostruct)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; extract multiple entries using the objc flags
;;;; check for things in all colors
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  make_flag_struct, fs
  fs.bright='N'
  fs.blended='N'  ;;;; gets rid of parent
  flag_select,ostruct,fs,color_index,_ss,objc=1
  make_flag_struct, fs

  index=index(_ss)
  ostruct=ostruct(_ss)
  print,'After objc cuts: ', n_elements(ostruct)  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; cut on maximum magnitude
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  maxm=24.0
  if keyword_set(max_mag) then begin
    maxm=max_mag
  endif
  _ss=where(ostruct.fibercounts(color_index) lt maxm)
  index=index(_ss)
  ostruct=ostruct(_ss)
  print,'After magnitude cuts: ', n_elements(ostruct)  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

indices=index

if not keyword_set(silent) then begin
  title='Extracted Galaxies'
  xtitle='fibercounts'
  ytitle='petrorad'

  plot,ostruct.fibercounts(color_index),ostruct.petrorad(color_index), $
  yrange=[0,10],psym=3,title=title,xtitle=xtitle,ytitle=ytitle
endif

end