code: def XYZ_to_lbd(X,Y,Z,degree=False):
      
      #Whether to use degrees and scalar input is handled by decorators
      d= nu.sqrt(X**2.+Y**2.+Z**2.)
      b=nu.arcsin(Z/d)
      cosl= X/d/nu.cos(b)
      sinl= Y/d/nu.cos(b)
      l= nu.arcsin(sinl)
      l[cosl < 0.]= nu.pi-l[cosl < 0.]
      l[(cosl >= 0.)*(sinl < 0.)]+= 2.*nu.pi
      out= nu.empty((len(l),3))
      out[:,0]= l
      out[:,1]= b
      out[:,2]= d
      return out
docs: NAME:
  
         XYZ_to_lbd
  
      PURPOSE:
  
         transform from rectangular Galactic coordinates to spherical Galactic coordinates (works with vector inputs)
  
      INPUT:
  
         X - component towards the Galactic Center (in kpc; though this obviously does not matter))
  
         Y - component in the direction of Galactic rotation (in kpc)
  
         Z - component towards the North Galactic Pole (kpc)
  
         degree - (Bool) if True, return l and b in degrees
  
      OUTPUT:
  
         [l,b,d] in (rad or degree,rad or degree,kpc)
  
         For vector inputs [:,3]
  
      HISTORY:
  
         2009-10-24 - Written - Bovy (NYU)
  
         2014-06-14 - Re-written w/ numpy functions for speed and w/ decorators for beauty - Bovy (IAS)