;------------------------------------------------------------------------------
;+
; NAME:
;   veto
;
; PURPOSE:
; Applies a series of veto masks (and survey boundary definitions)
; to a structure, throwing away items which lie outside of the
; survey boundary or which lie in a veto mask.
; The structure which is passed in is modified and the fraction of
; objects which passes the cuts is returned (note this can also be
; trivially obtained as the ratio of the pre- to post-veto sizes).
; This section also assigns sector and ipoly indices to all the objects
; based on the geometry file.
;
; COMMENTS:
; Need a data model.
;
; EXAMPLES:
;
; REQUIREMENTS:
;
; REVISION HISTORY:
; 20-Nov-2012 Written by Martin White (LBNL)
; 
pro veto,dd
  print,' '
  print,'Veto called with '+strtrim(n_elements(dd),2)+' elements.'
  ninput = n_elements(dd)

  ; Apply survey mask.  We keep things in this mask ...
  print,'Trimming to survey region ...'
  file_survey = filepath(root=getenv("BOSSLSS_DIR"),$
                  'geometry/boss_survey.ply')
  print,'Reading in the survey geometry from ',file_survey
  read_mangle_polygons,file_survey,survey
  dd=dd[$
         where(is_in_window(ra=dd.ra,dec=dd.dec,survey)$
            GT 0)]
  tmp = n_elements(dd)
  print,'survey geom vetoed ',ninput-tmp,'elts.'

  ; Trim to the current survey geometry.
  ; This step will be duplicated elsewhere because it is useful to
  ; have the polygon and sector information from the mask.  For now
  ; we simply trim.
  ;file_survey = filepath(root=getenv("BOSSLSS_DIR"),$
  ;                '/geometry/boss_geometry_2012_12_04')
;;; new geometry effective March 28 2014.  Used for 'dr12v1'
;  file_survey = filepath(root=getenv("BOSSLSS_DIR"),$
;                  '/geometry/boss_geometry_2014_03_12')
;;;; new geometry effective May 5 2014.  Used for dr12v1
  file_survey = filepath(root=getenv("BOSSLSS_DIR"),$
                  '/geometry/boss_geometry_2014_05_28')
  print,'Reading in the current survey geometry from ',file_survey
  polyhdr=0
  read_mangle_polygons,file_survey+'.ply',survey,hdr=polyhdr
  read_fits_polygons,file_survey+'.fits',survey
  scheme = (strsplit(polyhdr[0],/extract))[1]
  in_survey=where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,survey,$
                    in_polygon=ipoly) GT 0,count)
  if (count LE 0) then begin
    print,'No objects in survey!!'
    STOP
  endif
  dd       = dd[in_survey]
  ;; assign ipoly and sector values using geometry file.
  print,'Assigning ipoly and isect using geometry file.'
  dd.ipoly =  ipoly[in_survey]
  dd.isect = survey[ipoly[in_survey]].sector 

  ; Now remove objects which are in various veto masks.
  print,'Vetoing targets with bad astrometry'
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/badfield_mask_postprocess_pixs8.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  print,'Vetoing targets with badfield mask ...'
  ;; new mask created by nikhil, checked in on July 10 2014
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/badfield_mask_unphot_seeing_extinction_pixs8_dr12.ply')
  ;;;; old version pre- July 2 2013
  ;file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/badfield_mask_unphot-ugriz_pix.ply')
  ;; dr11v2 was made with this veto mask, but then we reverted back to the old one.  dr11v1 and dr10v8 were made with hte above veto mask.
  ;file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/badfield_mask_unphot_seeing_extinction_pixs8.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  print,'Vetoing targets with allsky_bright_star_mask_pix ...'
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/allsky_bright_star_mask_pix.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  print,'Vetoing targets with bright object mask ...'
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/bright_object_mask_rykoff_pix.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  print,'Vetoing targets with collision_priority_mask ...'
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/collision_priority_mask_dr12.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  print,'Vetoing targets with centerpost_mask ...'
  ;; updated May 14 2014 to include centerposts through DR12.
  ;;file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/centerpost_mask.ply')
  file_veto = filepath(root=getenv("BOSSLSS_DIR"),'/geometry/centerpost_mask_dr12.ply')
  polyhdr=0
  read_mangle_polygons,file_veto,veto,hdr=polyhdr
  scheme = (strsplit(polyhdr[0],/extract))[1]
  dd=dd[where(is_in_window_pix(ra=dd.ra,dec=dd.dec,scheme=scheme,veto) LE 0)]

  noutput = n_elements(dd)
  print,'Veto returning '+strtrim(n_elements(dd),2)+' elements.'
  fkeep   = double(noutput)/double(ninput)
  print,'this is fkeep',fkeep
  ;return,fkeep

end 
