Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. from astropy import units as u
  2. from astropy.coordinates import SkyCoord
  3. from scipy.ndimage.interpolation import map_coordinates
  4. import os.path
  5. from collections import Iterable
  6. import pyfits as fits
  7. def get_ebv_from_map(coordinates, interpolate=True, order=1):
  8.  
  9. mapdir='/vol/Dust_Map/maps/'
  10. fname = os.path.join(mapdir, 'SFD_dust_4096_{0}.fits')
  11.  
  12. # Parse input
  13. ra, dec = coordinates
  14. coordinates = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree))
  15.  
  16. # Convert to galactic coordinates.
  17. coordinates = coordinates.galactic
  18. l = coordinates.l.radian
  19. b = coordinates.b.radian
  20.  
  21. # Check if l, b are scalar
  22. return_scalar = False
  23. if not isinstance(l, Iterable):
  24. return_scalar = True
  25. l, b = np.array([l]), np.array([b])
  26.  
  27. # Initialize return array
  28. ebv = np.empty_like(l)
  29.  
  30. # Treat north (b>0) separately from south (b<0).
  31. for n, idx, ext in [(1, b >= 0, 'ngp'), (-1, b < 0, 'sgp')]:
  32.  
  33. if not np.any(idx): continue
  34. hdulist = fits.open(fname.format(ext))
  35. mapd = hdulist[0].data
  36.  
  37. # Project from galactic longitude/latitude to lambert pixels.
  38. # (See SFD98).
  39. npix = mapd.shape[0]
  40. x = (npix / 2 * np.cos(l[idx]) * np.sqrt(1. - n*np.sin(b[idx])) +
  41. npix / 2 - 0.5)
  42. y = (-npix / 2 * n * np.sin(l[idx]) * np.sqrt(1. - n*np.sin(b[idx])) +
  43. npix / 2 - 0.5)
  44.  
  45. # Get map values at these pixel coordinates.
  46. if interpolate:
  47. ebv[idx] = map_coordinates(mapd, [y, x], order=order)
  48. else:
  49. x=np.round(x).astype(np.int)
  50. y=np.round(y).astype(np.int)
  51. ebv[idx] = mapd[y, x]
  52.  
  53. hdulist.close()
  54.  
  55. if return_scalar:
  56. return ebv[0]
  57. return ebv
  58.  
  59. ABtoVEGA=[+0.77,-0.13,-0.02,+0.19,+0.49,-0.19,-0.18,-0.06,-0.06,+0.04,+0.10,+0.22,+0.27,+0.36,+0.45,+0.56,+0.50]
  60.  
  61. ZP=np.loadtxt("zero-points.offsets.asc")
  62. Gal=np.loadtxt("photometry_galaxies_photometry.cat", dtype=None)
  63. ra=Gal[:,1]
  64. dec=Gal[:,2]
  65.  
  66. Av=[3.836 ,3.070,2.542,2.058,1.313, 3.466 , 3.082 , 2.882 , 2.652 , 2.367 , 2.226 , 2.066 , 1.872 , 1.652 , 1.423 , 1.298 , 1.156]
  67. count=0
  68. for i in range(Gal.shape[0]):
  69. j=0
  70. m=39
  71. while (j<len(ABtoVEGA)):
  72. if ((isnan(float(Gal[i,j+5]))) or (isnan(float(Gal[i,j+22])))):
  73. Gal[i,j+5]='-99.0'
  74. Gal[i,j+22]='0.0'
  75. count+=1
  76. else:
  77. value=get_ebv_from_map((ra[i], dec[i]), interpolate=True, order=1)
  78. Gal[i,j+5]=str(float(Gal[i,j+5])-ABtoVEGA[j]-ZP[j]-value*Av[j])
  79. j+=1
  80. while (m<45):
  81. if (math.isnan(float( Gal[i,m]))):
  82. Gal[i,m] = '0.0'
  83. m+=1
  84.  
  85. for n, idx, ext in [(1, b >= 0, 'ngp'), (-1, b < 0, 'sgp')]:
  86.  
  87. if not np.any(idx): continue
  88. hdulist = fits.open(fname.format(ext)) # this one
  89. mapd = hdulist[0].data
  90.  
  91. def main_loop(a):
  92. for n, idx, ext in [(1, b >= 0, 'ngp'), (-1, b < 0, 'sgp')]:
  93. # Rest of the code
  94.  
  95. for n, idx, ext in [(1, b >= 0, 'ngp'), (-1, b < 0, 'sgp')]:
  96.  
  97. # Other necessary code
  98. if ext not in mapdcache:
  99. with fits.open(fname.format(ext)) as hdulist:
  100. mapdcache[ext] = hdulist[0].data
  101. else:
  102. mapd = mapdcache[ext]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement