forked from eblur/dust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhalodict.py
194 lines (150 loc) · 6.31 KB
/
halodict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import dust
import sigma_scat as ss
import galhalo as gh
import numpy as np
import constants as c
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from astropy.io import fits
## UPDATED July 10, 2013 : Rewrote ecf method in HaloDict object
## UPDATED June 11, 2013 : Make this file independent of asciidata
## July 17, 2012 : A library of objects and functions for simulating a
## full halo given an object's spectrum.
## See sim_cygx3.py for original code (and testing)
#---------------------------------------------------------------
class HaloDict( object ):
"""
A dictionary of halos where each property can be looked up with energy as a key.
"""
def __init__( self, energy, alpha=np.power(10.0, np.arange(0.0,3.01,0.05)), \
rad=dust.Grain(), scatm=ss.Scatmodel() ):
self.alpha = alpha
self.energy = energy
self.index = dict( zip(energy,range(len(energy))) )
self.rad = rad
self.scatm = scatm
self.intensity = np.zeros( shape=( len(energy), len(alpha) ) )
# The following variables get defined when htype is set
# See analytic.set_htype
self.htype = None
self.dist = None
self.taux = None
## Issues with comparing flouts, try round
## http://stackoverflow.com/questions/23721230/float-values-as-dictionary-key
def __getitem__( self, key, n=2 ):
i = self.index[round(key,n)]
return self.intensity[i,:]
## http://stackoverflow.com/questions/19151/build-a-basic-python-iterator
def __iter__( self ):
self.count = 0
return self
def next( self ):
if self.count >= len( self.energy ):
raise StopIteration
else:
self.count += 1
return self.intensity[count,:]
def __getslice__( self, i, j ):
slice = np.where( np.logical_and( self.energy>=i, self.energy<j ) )[0]
return sum( self.intensity[slice,:], 0 )
@property
def len( self ):
return len( self.energy )
@property
def hsize( self ):
return len( self.alpha )
@property
def superE( self ):
NE, NA = len(self.energy), len(self.alpha)
return np.tile( self.energy.reshape(NE,1), NA ) # NE x NA
@property
def superA( self ):
NE, NA = len(self.energy), len(self.alpha)
return np.tile( self.alpha, (NE,1) ) # NE x NA
def total_halo( self, fluxes ):
"""
Sums the halos from different energy bins to create a total halo profile.
Creates / updates the self.total parameter
"""
NE, NA = len(self.energy), len(self.alpha)
if len(fluxes) != NE:
print 'Error: Number of flux bins must equal the number of halo energy bins'
return
superflux = np.tile( fluxes.reshape(NE,1), NA )
result = np.sum( superflux * self.intensity, 0 )
self.total = result
return
# Update -- Feb 10, 2015 to match halo.ecf
def ecf( self, theta, nth=500 ):
"""
Returns the enclosed fraction for the halo surface brightness
profile, as a function of energy via
integral(theta,2pi*theta*halo)/total_halo_counts
-------------------------------------------------------------------------
theta : float : Value for which to compute enclosed fraction (arcseconds)
nth : int (500) : Number of angles to use in calculation
"""
NE, NA = len(self.energy), len(self.alpha)
result = np.zeros( NE ) # NE x NA
taux = self.taux
tharray = np.linspace( min(self.alpha), theta, nth )
if self.taux == None:
print 'ERROR: No taux is specified. Need to run halo calculation'
return result
for i in range(NE):
interpH = interp1d( self.alpha, self.intensity[i,:] )
result[i] = c.intz( tharray, interpH(tharray) * 2.0*np.pi*tharray ) / taux[i]
return result
#---------------------------------------------------------------
# Supporting functions
def get_spectrum( filename ):
data = c.read_table( filename, 2 )
energy, flux = np.array( data[0] ), np.array( data[1] )
return energy, flux
def aeff( filename ):
data = c.read_table( filename, 2 )
energy = np.array( data[0] )
aeff = np.array( data[1] )
return interp1d( energy, aeff ) # keV vs cm^2
## 2015.01.29 - Add a function that will save and load halo dicts into/from fits files
def fitsify_halodict( hd, outfile, clobber=False ):
# Set up the header
prihdr = fits.Header()
prihdr['COMMENT'] = "This is a fits file containing halo dictionary information"
prihdr['SCATM'] = hd.scatm.stype
prihdr['HTYPE'] = hd.htype.ismtype
prihdr['DUSTMASS'] = hd.dist.md
prihdr['DUSTDENS'] = hd.rad.rho
prihdr['DUSTPOWR'] = hd.rad.p
prihdu = fits.PrimaryHDU(header=prihdr)
# Create a block that contains the energy and taux info
tbhdu = fits.BinTableHDU.from_columns( \
[fits.Column(name='energy', format='1E', array=hd.energy), \
fits.Column(name='taux', format='1E', array=hd.taux)] )
dust_hdu = fits.BinTableHDU.from_columns( \
[fits.Column(name='rad', format='1E', array=hd.rad.a)] )
thdulist = fits.HDUList([prihdu, tbhdu, dust_hdu])
for EE in hd.energy:
tbhdu = fits.BinTableHDU.from_columns( \
[fits.Column(name='alpha', format='1E', array=hd.alpha), \
fits.Column(name='intensity', format='1E', array=hd[EE])] )
thdulist.append(tbhdu)
thdulist.writeto(outfile, clobber=clobber)
return
def read_halodict_fits( infile ):
hdulist = fits.open(infile)
header = hdulist[0].header
energy = hdulist[1].data['energy']
taux = hdulist[1].data['taux']
rad = hdulist[2].data['rad']
md = header['DUSTMASS']
rho = header['DUSTDENS']
powr = header['DUSTPOWR']
alpha = hdulist[3].data['alpha']
dd = dust.Dustdist( rad=rad, p=powr, rho=rho )
result = HaloDict( energy, alpha=alpha, rad=dd )
result.taux = taux
result.dist = dust.Dustspectrum( rad=dd, md=md )
for i in np.arange(len(energy))+3:
result.intensity[i-3,:] = hdulist[i].data['intensity']
return result