;------------------------------------------------------------------------------
;+
; NAME:
;   get_collate_targets
;
; PURPOSE:
; Reads in the collated target file, fills in photometry from the galimaging file, 
; and spits out a trimmed list of cmass and lowz targets.
;
; COMMENTS:
; Input collated/galimaging files are in BOSSTILELIST_DIR.  Need a data model.
;
; EXAMPLES:
;
; REQUIREMENTS:
;
; REVISION HISTORY:
; 21-Sep-2012 Written by Beth Reid (LBNL)
; 
pro get_collate_targets,cmass_output,lowz_output
  print,' '
  print,'Grab collate targets, fill in photometry, run through masks.'
  print,'Outputting cmass file to ',cmass_output
  print,'Outputting lowz file to ',lowz_output

  ;; these are the filepaths you want if you're on riemann.
  ;infile = "/clusterfs/riemann/raid006/bosswork/boss/lss/collate/bosstile-final-collated-boss2-boss32.fits"
  ;infilephoto = "/clusterfs/riemann/raid006/bosswork/boss/lss/collate/bosstile-final-collated-boss2-boss32-galimaging.fits"
  ;; if not, use the files you've downloaded.

  input_dir = data_dir = filepath(root=getenv("BOSSLSS_DIR"),"/inputfiles/")

  infile = input_dir + "bosstile-final-collated-boss2-boss32.fits"
  infilephoto = input_dir + "bosstile-final-collated-boss2-boss32-galimaging.fits"

  print,infile
  print,infilephoto
  dd =  mrdfits(infile,1)
  ddphoto = mrdfits(infilephoto,1)

  ;; note: Jeremy's file has rerun as a string, and we have it as long.
  ;; things *seem* to work out ok, but we should be careful.
  ;; 
  ;; 
  ;; Jeremy's photo file only has some of the fields we were keeping.
  ;; Jeremy missing r_dev, psf_fwhm. r_dev is used in cmass targeting,
  ;; but psf_fwhm is not.
  ;; r_dev and psf_fwhm will not ever be filled in then!!  not sure if they're
  ;; needed.
  ;;
  ee = {ra:0.D,dec:0.D,boss_target1:0L,boss_target1_009:0L,$
        run:0L,rerun:'',camcol:0L,field:0L,id:0L,$
        ichunk:0L,ipoly:0L,isect:0L,finalN:0L,finalid:0L,$
        nassigned:0L,whichfinal:0L,$
; also want to know if this target's tile has been observed yet, also for assigning ntiles to sectors.
        tile:intarr(3),$
;       some fiber collision info
;       collateindx is the original collate file index.  it's necessary to later 
;       later search through collision groups.
        collateindx:0L,icollided:0L,$
        coll_group_finalid:lonarr(10),$
        coll_group_bosstarget1:lonarr(10),$        
  ;; temp?  let's keep other linked lists too.
        ingroup:-1L,firstgroup:-1L,multgroup:-1L,nextgroup:-1L,$
;        thing_id_009:0L,thing_id_chunk:0L,$
;        ichunk:0L,tchunk:0L,ipoly:0L,isect:0L,ingroup:0L,$
        fracpsf:fltarr(5),expflux:fltarr(5),devflux:fltarr(5),$
        psfflux:fltarr(5),modelflux:fltarr(5),fiber2flux:fltarr(5),$
        r_dev:fltarr(5),psf_fwhm:fltarr(5),modelflux_ivar:fltarr(5),$
        extinction:fltarr(5)}
  ee = replicate(ee,n_elements(dd))

  ee.ra            = dd.ra
  ee.dec           = dd.dec
  ee.boss_target1  = dd.boss_target1
  ee.boss_target1_009  = dd.boss_target1_009
  ee.run           = dd.run
  ee.rerun         = dd.rerun
  ee.camcol        = dd.camcol
  ee.field         = dd.field
  ee.id            = dd.id
  ee.finalN        = dd.finalN
  ee.finalid        = dd.finalid
  ee.nassigned        = dd.nassigned
  ee.whichfinal        = dd.whichfinal
  ee.ipoly         = dd.ipoly
  ee.tile          = dd.tile
  ee.collateindx   = lindgen(n_elements(dd))
  ee.icollided     = dd.icollided
  ee.coll_group_finalid = dd.coll_group_finalid 
  ee.coll_group_bosstarget1 = dd.coll_group_bosstarget1 
  ;; temp?  let's keep other linked lists too.
  ee.ingroup       = dd.ingroup 
  ee.firstgroup       = dd.firstgroup 
  ee.multgroup       = dd.multgroup 
  ee.nextgroup       = dd.nextgroup 

  ;; 
  ;; Jeremy's photo file only has some of the fields we were keeping.
  ;; Jeremy missing r_dev, psf_fwhm. r_dev is used in cmass targeting,
  ;; but psf_fwhm is not.
  ;;
  ee[ddphoto.fileindx].ichunk        = ddphoto.ichunk
  ee[ddphoto.fileindx].fracpsf       = ddphoto.fracpsf
  ee[ddphoto.fileindx].expflux       = ddphoto.expflux
  ee[ddphoto.fileindx].devflux       = ddphoto.devflux
  ee[ddphoto.fileindx].psfflux       = ddphoto.psfflux
  ee[ddphoto.fileindx].modelflux     = ddphoto.modelflux
  ee[ddphoto.fileindx].fiber2flux    = ddphoto.fiber2flux
  ee[ddphoto.fileindx].extinction    = ddphoto.extinction
  ee[ddphoto.fileindx].modelflux_ivar= ddphoto.modelflux_ivar

  dd = ee
  ninput = n_elements(dd) 
  veto,dd
  noutput = n_elements(dd)
  fkeep   = double(noutput)/double(ninput) 
  print,'Veto masks vetoed ',fkeep

  cmassi = where(((dd.boss_target1_009 AND 2L^1) NE 0) AND (dd.boss_target1_009 GT 0),cntcmass) 
  lowzzi = where(((dd.boss_target1_009 AND 2L^0) NE 0) AND (dd.boss_target1_009 GT 0),cntlowzz) 

  print,'Number of CMASS targets after trim:',cntcmass
  print,'Number of LOWZ targets after trim:',cntlowzz

  ddcmass = dd[cmassi] 

  data_dir = filepath(root=getenv("BOSSLSS_DIR"),"/data/")

  outfilecmass = data_dir+cmass_output
  print,'Writing data to ',outfilecmass
  mwrfits,ddcmass,outfilecmass,/create

  ddlowzz = dd[lowzzi]

  outfilelowzz = data_dir+lowz_output
  print,'Writing data to ',outfilelowzz
  mwrfits,ddlowzz,outfilelowzz,/create

end 
