-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.py
213 lines (174 loc) · 7.32 KB
/
example.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
#
# This file is part of Magnetar
#
# Copyright (C) 2013-2018 Juan Diego Soler
import sys
import numpy as np
from astropy.io import fits
import matplotlib
import matplotlib.pyplot as plt
from hro import *
from bvisual import *
from astropy.wcs import WCS
from reproject import reproject_interp
import os
import imageio
indir='data/'
prefix='Taurusfwhm10'
# =====================================================================================================================
def exampleHRO2D():
hdu=fits.open(indir+'Taurusfwhm5_logNHmap.fits')
logNHmap=hdu[0].data
hdrREF=hdu[0].header
hdu.close()
hdu=fits.open(indir+prefix+'_Qmap.fits')
Qmap=hdu[0].data
hdu.close()
hdu=fits.open(indir+prefix+'_Umap.fits')
Umap=hdu[0].data
hdu.close()
sz=np.shape(logNHmap)
deltab=60.*hdrREF['CDELT2'] # in arcmin
ksz=15. # arcmin
pxksz=ksz/deltab
steps=25 # number of histograms
weights=np.ones(sz)*(deltab/ksz)**2
# mask definition
mask=np.ones_like(logNHmap)
mask[(logNHmap < np.mean(logNHmap))]=0.
Qmap[:,0]=np.nan
# HRO calculation
outputhro = hroLITE(logNHmap, Qmap, -Umap, steps=20, minI=np.min(logNHmap), w=weights, mask=mask, ksz=pxksz)
# --------------------------------
# Calculation of the polarization angle
psi=0.5*np.arctan2(-Umap,Qmap)
ex=-np.sin(psi); ey=np.cos(psi)
# 90 degrees rotation to obtain the magnetic field orientation
bx=ey; by=-ex
sImap=ndimage.filters.gaussian_filter(logNHmap, [ksz, ksz], order=[0,0], mode='nearest')
dIdx =ndimage.filters.gaussian_filter(logNHmap, [ksz, ksz], order=[0,1], mode='nearest')
dIdy =ndimage.filters.gaussian_filter(logNHmap, [ksz, ksz], order=[1,0], mode='nearest')
# ------------------------------------------------------------------------------------
vecpitch=20
xx, yy, gx, gy = vectors(logNHmap, dIdx, dIdy, pitch=vecpitch)
xcoord=hdrREF['CRVAL1']+(xx-hdrREF['CRPIX1'])*hdrREF['CDELT1']
ycoord=hdrREF['CRVAL2']+(yy-hdrREF['CRPIX2'])*hdrREF['CDELT2']
xx, yy, ux, uy = vectors(logNHmap, bx, by, pitch=vecpitch)
xcoord=hdrREF['CRVAL1']+(xx-hdrREF['CRPIX1'])*hdrREF['CDELT1']
ycoord=hdrREF['CRVAL2']+(yy-hdrREF['CRPIX2'])*hdrREF['CDELT2']
fig = plt.figure(figsize=(8.0,7.0))
plt.rc('font', size=10)
ax1=plt.subplot(111, projection=WCS(hdrREF))
im=ax1.imshow(np.abs(np.rad2deg(outputhro['Amap'])), origin='lower', cmap='cividis')
ax1.contour(logNHmap, origin='lower', levels=[np.mean(logNHmap),np.mean(logNHmap)+1.0*np.std(logNHmap),np.mean(logNHmap)+2.0*np.std(logNHmap)], colors='white', linewidths=1.0)
ax1.quiver(xx, yy, gx, gy, units='width', color='red', pivot='middle', scale=25., headlength=0, headwidth=1, alpha=0.8, transform=ax1.get_transform('pixel'), label=r'$\nabla I$')
ax1.quiver(xx, yy, ux, uy, units='width', color='black', pivot='middle', scale=25., headlength=0, headwidth=1, transform=ax1.get_transform('pixel'), label=r'$B_{\perp}$')
ax1.coords[0].set_axislabel(r'$l$')
ax1.coords[1].set_axislabel(r'$b$')
cbar=fig.colorbar(im, ax=ax1, fraction=0.046, pad=0.04)
cbar.ax.set_title(r'$\phi$ [deg]')
plt.legend()
plt.show()
isteps=outputhro['csteps']
icentres=0.5*(isteps[0:np.size(isteps)-1]+isteps[1:np.size(isteps)])
fig = plt.figure(figsize=(8.0,4.5))
plt.rc('font', size=10)
ax1=plt.subplot(111)
ax1.plot(icentres, outputhro['xi'], color='orange')
ax1.axhline(y=0, color='grey', alpha=0.5)
ax1.set_xlabel(r'log($N_{\rm H}/$cm$^{2}$)')
ax1.set_ylabel(r'$\xi$')
plt.show()
#plt.plot(isteps, prs, color='orange')
#plt.show()
#plt.plot(isteps, np.abs(meanphi)*180./np.pi, color='orange')
#plt.show()
# =========================================================================================================================
def exampleVisualization():
hdu=fits.open(indir+'Taurusfwhm5_logNHmap.fits')
Imap=hdu[0].data
refhdr1=hdu[0].header
hdu=fits.open(indir+prefix+'_Qmap.fits')
Qmap=hdu[0].data
hdu=fits.open(indir+prefix+'_Umap.fits')
Umap=hdu[0].data
# --------------------------------
# Calculation of the polarization angle
psi=0.5*np.arctan2(-Umap,Qmap)
ex=-np.sin(psi); ey=np.cos(psi)
# 90 degrees rotation to obtain the magnetic field orientation
bx=ey; by=-ex
sz=np.shape(Qmap)
length=int(0.05*sz[0]) # integration length in the LIC calculation
niter=3 # number of iterations in the LIC calculation
liccube=lic(bx, by, length=length, niter=niter)
licmap=liccube[niter-1]
x, y, ux, uy = vectors(Imap, bx, by, pitch=15)
licmin=np.mean(licmap)-np.std(licmap)
licmax=np.mean(licmap)+np.std(licmap)
fig = plt.figure(figsize=(8.0,7.0))
plt.rc('font', size=10)
ax1=plt.subplot(111, projection=WCS(refhdr1))
im=ax1.imshow(Imap, origin='lower', cmap=planckct())
ax1.imshow(licmap, origin='lower', alpha=0.3, cmap='binary', vmin=licmin, vmax=licmax)
arrows=plt.quiver(x, y, ux, uy, units='width', color='black', pivot='middle', scale=25., headlength=0, headwidth=1, transform=ax1.get_transform('pixel'), alpha=0.6)
plt.colorbar(im, fraction=0.046, pad=0.04)
plt.show()
import pdb; pdb.set_trace()
# ==============================================================================================================
def exampleStructureFunction():
hdu=fits.open('../PlanckHerschelGouldBelt/Orion/Orionfwhm10_Imap.fits')
Imap0=hdu[0].data
hdrI=hdu[0].header
import pdb; pdb.set_trace()
hdu=fits.open('../PlanckHerschelGouldBelt/Orion/Orionfwhm10_Qmap.fits')
Qmap0=hdu[0].data
hdu=fits.open('../PlanckHerschelGouldBelt/Orion/Orionfwhm10_Umap.fits')
Umap0=hdu[0].data
y0=50
y1=250
x0=210
x1=410
Imap=Imap0[y0:y1,x0:x1]
Qmap=Qmap0[y0:y1,x0:x1]
Umap=Umap0[y0:y1,x0:x1]
mask=1+0*Qmap
#mask[(Imap < 21.5).nonzero()]=0
fig = plt.figure(figsize=(4.0, 4.0), dpi=150)
plt.rc('font', size=8)
ax1=plt.subplot(111, projection=WCS(hdrI))
ax1.set_xlim(x0,x1)
ax1.set_ylim(y0,y1)
ax1.imshow(np.log10(Imap0), origin='lower', cmap=planckct())
ax1.coords[0].set_axislabel('Galactic Longitude')
ax1.coords[1].set_axislabel('Galactic Latitude')
plt.savefig('/Users/soler/Downloads/OrionA_Imap.png', bbox_inches='tight')
plt.close()
psi=0.5*np.arctan2(-Umap,Qmap)
sigmapsi=circvar(psi)
print('SigmaPsi',sigmapsi*180.0/np.pi)
#lags=np.arange(2.,40.,4.)
lags=np.arange(2.,50.,2.)
s2=np.zeros(np.size(lags))
for i in range(0, np.size(lags)):
s2[i]=structurefunction(Qmap, Umap, mask=mask, lag=lags[i], s_lag=1.0)
print(60.*np.abs(hdrI['CDELT1'])*lags[i],s2[i]*180.0/np.pi)
physlags=60.*np.abs(hdrI['CDELT1'])*lags
fitrange=(physlags > 10.*2.).nonzero()
z = np.polyfit(physlags[fitrange]**2, s2[fitrange]**2, 2)
poly = np.poly1d(z)
print(np.sqrt(poly(0.0))*180.0/np.pi)
fig = plt.figure(figsize=(6.0, 4.0), dpi=150)
plt.rc('font', size=8)
plt.plot(60.*np.abs(hdrI['CDELT1'])*lags, s2*180.0/np.pi, color='black')
plt.plot(60.*np.abs(hdrI['CDELT1'])*lags, np.sqrt(poly(physlags**2))*180.0/np.pi, color='orange')
plt.axhline(y=sigmapsi*180.0/np.pi, color='cyan')
plt.xlabel(r'$\delta$ [arcmin]')
plt.ylabel(r'$S_{2}(\delta)$ [deg]')
#plt.show()
plt.savefig('/Users/soler/Downloads/OrionA_StructureFunction.png', bbox_inches='tight')
plt.close()
import pdb; pdb.set_trace()
#exampleVisualization();
exampleHRO2D();
#exampleVisualization();