Esempio n. 1
0
def findGalaxies(ra_center, dec_center, radius, rMagMax, baMin, baMax):

   # CONSTANTS
   rad2deg = 180./math.pi
   csize = 100000
   obj = "ASSEMBLEDGALAXY"
   expmjd = 0.
   gal_count = 0
   wavelen_step = 0.25
   Sixflts = ['float','float','float','float','float','float']


   # BANDPASSES 
   # Instantiate bandpasses and read into lsstbp.
   bpdir = os.getenv("LSST_THROUGHPUTS_BASELINE")
   filterlist = ('u', 'g', 'r', 'i', 'z', 'y')
   lsstbp = {}
   for f in filterlist:
      lsstbp[f] = Bandpass(wavelen_min=300,wavelen_max=1200, wavelen_step=0.1)
      lsstbp[f].readThroughput(os.path.join(bpdir, 'total_' + f + '.dat'), wavelen_step=wavelen_step)

   # SEDS
   # Read in all of the galaxy seds in the root directory:
   galdir = "/astro/net/lsst1/shared/data/galaxySED/"
   gals = {}
   gallist = os.listdir(galdir)
   for i in range(len(gallist)):
      if gallist[i].endswith('.gz'):
         gallist[i] = gallist[i][:-3]
   for gal in gallist:
      gals[gal] = Sed()
      gals[gal].readSED_flambda(os.path.join(galdir, gal))

   # Check on resampling - want all galaxy seds to have the same wavelength range.
   # (although our LSST ranges are 300 - 1200 nm, the SED's are ~27 to 2290 nm).
   if ((gals[gallist[0]].wavelen.min() < 30) & (gals[gallist[0]].wavelen.max() > 2000)):
       # If true, then gals[gallist[0]] is okay to use as a template -- this ought to be true.
       wavelen_match = gals[gallist[0]].wavelen
   else:
       print "Had to use simple wavelength array for matching"
       wavelen_match = numpy.arange(30, 2200, 0.1, dtype='float')
   for gal in gallist:
       if gals[gal].needResample(wavelen_match = wavelen_match):
           gals[gal].resampleSED(wavelen_match = wavelen_match)

   # Create the galactic a, b values from CCM 89 for the source galaxy and ours.
   # adjust for redshift, add Milky Way dust (recalculate a and b),
   # normalize fluxes and calculate magnitudes:
   # First: calculate source galaxy a/b on wavelength range required for 
   # source galaxy (internal)from CCM 89.
   a_int, b_int = gals[gallist[0]].setupCCMab()

   # Second: calculate milky way a/b on wavelength range required for calculating 
   # magnitudes - i.e. 300 to 1200 nm.
   # Set up a Sed object that is the same for all galaxies, all chunks.
   # Start with a flat SED with F_AB = 3631 Jy 
   tmpgal = Sed()
   tmpgal.setFlatSED(wavelen_min=300, wavelen_max=1200, wavelen_step=wavelen_step)
   a_mw, b_mw = tmpgal.setupCCMab()  # so this is a/b on 300-1200 range. 

   # Set up phi, the wavelength-normalized system response for each filter, for each bandpass.
   # sb is the system response function (throughputs).  Also set up a bandpass list, for
   # manyMagCalc method and initiate mags w/dust.
   bplist = []
   for f in filterlist:
      lsstbp[f].sbTophi()
      bplist.append(lsstbp[f])
   phiarray, dlambda = tmpgal.setupPhiArray(bplist)

   objId = numpy.empty(0)
   ra = numpy.empty(0)
   dec = numpy.empty(0)
   diskFluxNorm = numpy.empty(0)
   bulgeFluxNorm = numpy.empty(0)
   diskSedFilename = numpy.empty(0)
   bulgeSedFilename = numpy.empty(0)
   a_disk = numpy.empty(0)
   b_disk = numpy.empty(0)
   a_bulge = numpy.empty(0)
   b_bulge = numpy.empty(0)
   pa_d = numpy.empty(0)
   rdshft = numpy.empty(0)
   Rmags = numpy.empty(0)

   #QUERY
   #myqdb = queryDB.queryDB(chunksize=csize,objtype=obj, filetypes=('REFERENCECATALOG',))
   myqdb = queryDB.queryDB(chunksize=csize,objtype=obj, filetypes=('TEST',))
   # Specify a circular field of view:
   ic = myqdb.getInstanceCatalogByCirc(ra_center, dec_center, radius, expmjd=expmjd)
   # Begin iteratively acquiring data
   gal_count = 0
   while ic is not None:
      gal_loop = len(ic.dataArray['raJ2000'])   
      gal_count += gal_loop
      objId = numpy.concatenate((objId, ic.dataArray['objId']), axis=0)
      ra = numpy.concatenate((ra, ic.dataArray['raJ2000']*rad2deg), axis=0)
      dec = numpy.concatenate((dec, ic.dataArray['decJ2000']*rad2deg), axis=0)
      diskFluxNorm = numpy.concatenate((diskFluxNorm, ic.dataArray['diskFluxNorm']), axis=0)
      bulgeFluxNorm = numpy.concatenate((bulgeFluxNorm, ic.dataArray['bulgeFluxNorm']), axis=0)
      diskSedFilename = numpy.concatenate((diskSedFilename, ic.dataArray['diskSedFilename']), axis=0)
      bulgeSedFilename = numpy.concatenate((bulgeSedFilename, ic.dataArray['bulgeSedFilename']), axis=0)
      a_disk = numpy.concatenate((a_disk, ic.dataArray['semiMajorDisk']), axis=0)
      b_disk = numpy.concatenate((b_disk, ic.dataArray['semiMinorDisk']), axis=0)
      a_bulge = numpy.concatenate((a_bulge, ic.dataArray['semiMajorBulge']), axis=0)
      b_bulge = numpy.concatenate((b_bulge, ic.dataArray['semiMinorBulge']), axis=0)
      pa_d = numpy.concatenate((pa_d, ic.dataArray['positionAngleDisk']), axis=0)
      rdshft = numpy.concatenate((rdshft, ic.dataArray['redshift']), axis=0)
      # Get next chunk, if it exists.
      ic = myqdb.getNextChunk()
   myqdb.closeSession()


   # Calculate galactic coordinates:
   gLon = []
   gLat = []
   for i in range(gal_count):
      gcoord = afwCoord.IcrsCoord(ra[i], dec[i]).toGalactic()
      gLon.append(gcoord.getL(afwCoord.DEGREES)*math.pi/180.)
      gLat.append(gcoord.getB(afwCoord.DEGREES)*math.pi/180.)
   ebv_mw = (EBV.calculateEbv(gLon, gLat, ebvMapNorth, ebvMapSouth, interp = True))
   del gLon
   del gLat
   print 'gLon, gLat calculated'

   # Now calculate magnitudes for each galaxy.  If you have a bulge, initiate its Sed
   # instance, multiply fnu by the bulge flux normalization and apply the bulge dust
   # model (currently zero).  Next initiate a Sed instance for the disk (if you have
   # one), multiply fnu by the disk flux normalization and apply the disk dust model.
   # If you have bulge and disk Sed's, add them together; if not you'll just use 
   # whichever one you have.  Correct for redshift, resample the Sed since now it's
   # shifted in wavelength, add the Milky Way dust and calculate magnitudes using the
   # manyMag method.  Uncorrected Magnitudes (no dust, reddening, redshifting added)
   # are calculated as well.

   uncmags = numpy.zeros(gal_count, dtype={'names':['u','g','r','i','z','y'], 'formats':Sixflts})
   raEdge = numpy.zeros(len(ra))
   decEdge = numpy.zeros(len(ra))
   option = numpy.zeros(len(ra)) + 5
   raEdge = ra - a_disk/3600.*math.sin((pa_d)*math.pi/180.)
   decEdge = dec + a_disk/3600.*math.cos((pa_d)*math.pi/180.)

   # Uncorrected Magnitudes (no dust, reddening, redshifting added)
   for i in range(gal_count):
      galdisk = diskSedFilename[i]
      galbulge = bulgeSedFilename[i]
       #raEdge = ra[i] - a_disk[i]/3600.*math.sin((pa_d[i])*math.pi/180.)
      #decEdge = dec[i] + a_disk[i]/3600.*math.cos((pa_d[i])*math.pi/180.)
      if (galbulge is not None) and (galdisk is not None):
         ba_disk = b_disk[i]/a_disk[i]
         ba_bulge = b_bulge[i]/a_bulge[i]
         baRatio = (diskFluxNorm[i]*ba_disk + bulgeFluxNorm[i]
                    *ba_bulge)/(diskFluxNorm[i] + bulgeFluxNorm[i])
         if baMin <= baRatio <= baMax:
            option[i] = 2
         else: continue
         tmpbulge = uncMagCalc(rdshft[i], gals[galbulge].wavelen, gals[galbulge].flambda, 
                               multiFlux=bulgeFluxNorm[i])
         tmpdisk = uncMagCalc(rdshft[i], gals[galdisk].wavelen, gals[galdisk].flambda,
                              multiFlux=diskFluxNorm[i])
         newgal = uncMagCalc(rdshft[i], tmpdisk.wavelen, (tmpdisk.flambda+tmpbulge.flambda), finish=1)
         tmpmags = newgal.manyMagCalc(phiarray, dlambda)
      elif galbulge is not None:
         baRatio = b_disk[i]/a_disk[i]
         if baMin <= baRatio <= baMax:
            option[i] = 0
         else: continue
         tmpbulge = uncMagCalc(rdshft[i], gals[galbulge].wavelen, gals[galbulge].flambda, 
                               multiFlux=bulgeFluxNorm[i], finish=1)
         tmpmags = tmpbulge.manyMagCalc(phiarray, dlambda)
      elif galdisk is not None:
         baRatio = b_disk[i]/a_disk[i]
         if baMin <= baRatio <= baMax:
            option[i] = 1
         else: continue
         tmpdisk = uncMagCalc(rdshft[i], gals[galdisk].wavelen, gals[galdisk].flambda,
                              multiFlux=diskFluxNorm[i], finish=1)
         tmpmags = tmpdisk.manyMagCalc(phiarray, dlambda)
      j = 0
      for f in filterlist:
         uncmags[f][i] = tmpmags[j]
         j += 1

   del diskSedFilename, bulgeSedFilename, tmpmags
   del Av_d, Rv_d, Av_b, Rv_b, rdshft, uncmags, raEdge, decEdge

   idx = numpy.where((tmpmags[2] <= rMagMax) & (option != 5))
   rmags = tmpmags[2][idx]
   objId = ojbId[idx]
   option.append(option)
   ra = ra[idx]
   dec = dec[idx]
   baRatio = baRatio[idx]
   a_disk = a_disk[idx]
   b_disk = b_disk[idx]
   a_bulge = a_bulge[idx]
   b_bulge = b_bulge[idx]
   diskFluxNorm = diskFluxNorm[idx]
   bulgeFluxNorm = bulgeFluxNorm[idx]
   pa_d = pa_d[idx]
   raEdge = raEdge[idx]
   decEdge = decEdge[idx]
   #print '%11.7f %11.7f %11.7f %11.7f' % (ra[i], dec[i], tmpmags[2], baRatio)


   print '# iterations: ', iteration + 1
   print 'Total # of galaxies: %d' % (gal_count)
   print 'Number of qualifying Galaxies within a radius of %s deg. is %s' % (radius, len(rmags))
   #print 'len(ra), len(rmags), len(baRatio), len(option): ', len(ra), len(rmags), len(baRatio), len(option)
   return ra, dec, rmags, baRatio, option, objId, a_disk, b_disk, a_bulge, b_bulge, diskFluxNorm, bulgeFluxNorm, pa_d, raEdge, decEdge