;------------------------------------------------------------------------------
;+
; NAME:
;   convert_catalog
;
; PURPOSE:
;   convert data and random catalogs to ascii or sas formats.
;
;
; CALLING SEQUENCE:
;   make_catalog,'catalog-file-name','random-file-name'
;
; OPTIONAL INPUTS:
;
; OUTPUTS:
;   catalogs in new format.
;                                
; COMMENTS:
;
; EXAMPLES:
;
; REQUIREMENTS:
;   A bunch of stuff we should document here.
; 
; REVISION HISTORY:
;   xx-May-2013 Written by Shirley (in make_catalog.pro)
;   31-May-2013 Ported to this separate module instead by Beth
;   02-June-2013 added randomsonly variable.  set to 1 if there's no
;   corresponding galaxy catalog.
;
pro convert_catalog,catalog_base,catalog_type,want_ascii,want_sas,randomsonly
  print,' '
  print,'convert_catalog called with:'
  print,'  catalog_base  =',catalog_base
  print,'  catalog_type  =',catalog_type
  print,'  want_ascii = ',want_ascii ; 1 -> want ascii, -1-> no ascii 
  print,'  want_sas = ',want_sas ; 1 -> want sas version outputs, -1-> nope
  print,'  randomsonly = ',randomsonly
  print,' '

  data_dir  = filepath(root=getenv("BOSSLSS_DIR"),"/data/")
  ;; tmp!! already moved all the files over to public spot.
  ;data_dir = "/clusterfs/riemann/raid006/bosswork/boss/lss/"
  output_base = catalog_base

  if ((want_ascii EQ 0 and want_sas EQ 0) or (catalog_type ne 0)) then begin
    print,'Nothing to do!'
    print,'currently only written for catalog_type = 0'
  endif else begin ;; don't bother reading everything in unless you need it.
    galaxy_file = catalog_base+'.dat.fits'
    random_file = catalog_base+'.ran.fits'
    if(randomsonly eq 0) then begin
      dd = mrdfits(data_dir+galaxy_file,1,hdr)
      print,'Read ',n_elements(dd),' galaxies from ',data_dir+galaxy_file
    endif
    rancat = mrdfits(data_dir+random_file,1,hdr)
    print,'Read ',n_elements(rancat),' randoms from ',data_dir+random_file

    if(want_sas GT 0 and catalog_type EQ 0) then begin
      if(randomsonly eq 0) then begin
        sas_dd = replicate(                                                                                $
               create_struct( "RA", 1.0D, "DEC", 1.0D, "z", 1.0, "weight_fkp", 1.0, "weight_noz", 1.0, $
                              "weight_cp", 1.0, "weight_sdc", 1.0, "weight_see", 1.0, "run", 0L, "rerun", 0L,"camcol",0L, $
                              "field", 1L, "obj", 1L,  $
                              "plate",0L,"mjd",0L,"fiberid",0L, "ipoly", 1L)  $
               ,n_elements(dd))
        struct_assign, dd,sas_dd
        sas_dd.obj = dd.id
        sas_dd.weight_see = dd.weight_seeing
        print,'Writing data to :',output_base+'.sasdat.fits'
        mwrfits,sas_dd,data_dir+output_base+'.sasdat.fits',/create
      endif ; randomsonly = 0
      sas_ran= replicate(                                                                                $
                 create_struct( "RA", 1.0D, "DEC", 1.0D, "z", 1.0, "weight", 1.0, "ipoly", 1L), $
                 n_elements(rancat))
      struct_assign, rancat,sas_ran
      sas_ran.weight = rancat.weight_fkp

      print,'Writing data to :',output_base+'.sasran.fits'
      mwrfits,sas_ran,data_dir+output_base+'.sasran.fits',/create
    endif
    if(want_ascii GT 0 and catalog_type EQ 0) then begin
      if(randomsonly eq 0) then begin
        close,10
        openw,10,data_dir+output_base+'.dat.ascii'
        for iii=0L,n_elements(dd)-1L do begin
          dd_weight_tmp = double(dd[iii].weight_systot*dd[iii].weight_fkp)*double(dd[iii].weight_noz + dd[iii].weight_cp -1.0)
          printf,10,format='(F10.5, F10.5, F10.5, E15.5, E15.5 )',dd[iii].ra,dd[iii].dec,dd[iii].z, dd_weight_tmp, dd[iii].nz
        endfor
      endif ; randomsonly eq 0

      close,10
      openw,10,data_dir+output_base+'.ran.ascii'
      for iii=0L,n_elements(rancat)-1L do begin
        printf,10,format='(F10.5, F10.5, F10.5, E15.5, E15.5)',rancat[iii].ra,rancat[iii].dec,rancat[iii].z, rancat[iii].weight_fkp,rancat[iii].nz
      endfor
      close,10
    endif ;;want_ascii
  endelse
end




