forked from solerjuan/magnetar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhro3D.py
251 lines (188 loc) · 6.28 KB
/
hro3D.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# This file is part of MAGNETAR, the set of magnetic field analysis tools
#
# Copyright (C) 2013-2020 Juan Diego Soler
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
from scipy import ndimage
def roangles3D(dens, Bx, By, Bz, mode='nearest', pxksz=3):
"""
Calculates the relative orientation angles between the density structures
and the magnetic field.
Parameters
----------
dens : numpy.ndarray.
density field.
Bx, y, z : float or numpy.ndarray
magnetic field strength in each direction.
Returns
-------
numpy.ndarray of the relative angles between the density structure and
and magnetic field structure.
Notes
-----
...
References
----------
.. [1] Soler et al 2013 ...
Examples
--------
...
"""
# Calculates the relative orientation angles between the density structures and the magnetic field.
# INPUTS
# dens - regular cube with the values of density
# Bx -
# By -
# Bz -
#
# OUTPUTS
#
#
#gx=grad[1]; gy=grad[0]; gz=grad[2];
gx=ndimage.filters.gaussian_filter(dens, [pxksz, pxksz, pxksz], order=[0,0,1], mode=mode)
gy=ndimage.filters.gaussian_filter(dens, [pxksz, pxksz, pxksz], order=[0,1,0], mode=mode)
gz=ndimage.filters.gaussian_filter(dens, [pxksz, pxksz, pxksz], order=[1,0,0], mode=mode)
normgrad=np.sqrt(gx*gx+gy*gy+gz*gz)
normb =np.sqrt(Bx*Bx+By*By+Bz*Bz)
zerograd=(normgrad==0.).nonzero()
zerob =(normb ==0.).nonzero()
cross=np.sqrt((gy*Bz-gz*By)**2+(gx*Bz-gz*Bx)**2+(gx*By-gy*Bx)**2)
dot =gx*Bx+gy*By+gz*Bz
# The cosine of the angle between the iso-density and B is the sine of the angle between
# the density gradient and B.
cosphi=dot/(normgrad*normb)
cosphi[(normgrad == 0.).nonzero()]=np.nan
cosphi[(normb == 0.).nonzero()]=np.nan
return cosphi
def equibins(dens, steps=10, mind=0.):
"""
...
Parameters
----------
dens : numpy.ndarray.
density field.
steps: int
...
nubd : float
...
Returns
-------
...
Notes
-----
...
References
----------
.. [1] Soler et al 2013 ...
Examples
--------
...
"""
# Calculates the relative orientation angles between the density structures and the magnetic field.
# INPUTS
# dens - regular cube with the values of density
# steps -
# mind -
sz=np.shape(dens)
hist, bin_edges = np.histogram(dens[(dens > mind).nonzero()], bins=10*sz[0]*sz[1])
bin_centre =0.5*(bin_edges[0:np.size(bin_edges)-1]+bin_edges[1:np.size(bin_edges)])
chist=np.cumsum(hist)
pitch=np.max(chist)/float(steps)
hsteps=pitch*np.arange(0,steps+1,1)
dsteps=np.zeros(steps+1)
for i in range(0, np.size(dsteps)-1):
good=np.logical_and(chist>hsteps[i],chist<hsteps[i+1]).nonzero()
dsteps[i]=np.min(bin_centre[good])
dsteps[np.size(dsteps)-1]=np.max(dens)
return dsteps
def roparameter(cosphi, hist, s_cosphi=0.25):
"""
...
Parameters
----------
cosphi : ...
hist : ...
s_cosphi : ...
Returns
-------
...
"""
perp=(np.abs(cosphi)>1.-s_cosphi).nonzero()
para=(np.abs(cosphi)<s_cosphi).nonzero()
xi=(np.sum(hist[para])-np.sum(hist[perp]))/float(np.sum(hist[para])+np.sum(hist[perp]))
return xi
def hro3D(dens, Bx, By, Bz, steps=10, hsize=21, mind=0, outh=[0,4,9], pxksz=3):
"""
Calculate the histogram of relative orientations (HRO) in three-dimensional data.
Parameters
----------
dens : ...
Bx,y,z : ...
steps : ...
hsize : ...
mind : ...
outh : ...
Returns
-------
hro : ...
cdens : ...
zeta : ...
"""
cosphi=roangles3D(dens, Bx, By, Bz, pxksz=pxksz)
dsteps=equibins(dens, steps=steps, mind=mind)
hros = np.zeros([steps,hsize])
cdens = np.zeros(steps)
xi = np.zeros(steps)
scube = 0.*dens
for i in range(0, np.size(dsteps)-1):
good=np.logical_and(dens>dsteps[i],dens<dsteps[i+1]).nonzero()
hist, bin_edges=np.histogram(cosphi[good], bins=hsize, range=(-1.,1.))
bin_centre=0.5*(bin_edges[0:np.size(bin_edges)-1]+bin_edges[1:np.size(bin_edges)])
hros[i,:]=hist
scube[good]=i
cdens[i]=np.mean([dsteps[i],dsteps[i+1]])
xi[i]=roparameter(bin_centre, hist)
outsteps = np.size(outh)
color = iter(cm.cool(np.linspace(0, 1, outsteps)))
fig = plt.figure()
for i in range(0, outsteps):
c = next(color)
labeltext = str(np.round(dsteps[outh[i]],2))+' < n < '+str(np.round(dsteps[outh[i]+1],2))
plt.plot(bin_centre, hros[outh[i],:], '-', linewidth=2, c=c, label=labeltext) #drawstyle
plt.xlabel(r'cos($\phi$)')
plt.legend()
plt.show()
fig = plt.figure(figsize=(8.0,4.0))
plt.rc('font', size=10)
ax1=plt.subplot(111)
ax1.semilogx(cdens, xi, 'o-', linewidth=2, color='blue')
ax1.axhline(y=0., c='k', ls='--')
ax1.set_xlabel(r'log$_{10}$ ($n_{\rm H}/$cm$^{-3}$)')
ax1.set_ylabel(r'$\xi$')
#plt.savefig(prefix + '-' + 'ROvsLogNH' + 'Thres' + "%d" % (thr) + '.png')
plt.show()
return hros, cdens, xi
# Testing the hro3D. should go in the test script or something.
#def main(args=None):
#
# if res.prnt:
# print('Primes: {0}'.format(primes))
#
#from astropy.convolution import Gaussian2DKernel
#g2D=Gaussian2DKernel(10)
#sz=np.shape(g2D)
#dens=np.dstack([g2D]*sz[0])
#
#grad=np.gradient(dens, edge_order=2)
##Bx=grad[1]
##By=grad[0]
##Bz=grad[2]
#Bx=np.random.uniform(low=-1., high=1., size=np.shape(dens))
#By=np.random.uniform(low=-1., high=1., size=np.shape(dens))
##Bz=np.random.uniform(low=-1., high=1., size=np.shape(dens))
##Bx=0.*dens
##By=0.*dens
#Bz=0.*dens
#hros, cdens, zeta = hro3D(dens, Bx, By, Bz, mind=np.mean(dens))