-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfittingN2HP_full.py
executable file
·166 lines (123 loc) · 4.44 KB
/
fittingN2HP_full.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
#!/usr/bin/env python
import pyspeckit
import astropy.io.fits as fits
import astropy.units as u
import skimage.morphology as immorph
import matplotlib.pyplot as plt
from config import file_N2Hp_base_erode, file_N2Hp_base_erode_rms, \
thinFile_N2Hp, thickFile_N2Hp, tpeakFile_N2Hp, maskFile_N2Hp
thinFile = thinFile_N2Hp.replace('.fits', '_full.fits')
thickFile = thickFile_N2Hp.replace('.fits', '_full.fits')
tpeakFile = tpeakFile_N2Hp.replace('.fits', '_full.fits')
maskFile = maskFile_N2Hp.replace('.fits', '_full.fits')
freqLine = 93173704000.0 * u.Hz
snr_min = 3.0
ncpu = 4
vmin = 8.8 # mininum velocity of the main hyperfine components
vmax = 12.5 # maximum velocity of the main hyperfine components
xmax = 65.
ymax = 90.
vmin_plot=10.0
vmax_plot=10.5
vmean = 10.2
#
# set some flags
prepareFiles = True
optThin = True
optThick = True
showOptThin = True
showOptThick = True
plot_dv = False
fitSingleSpectrum = False
#
# load the data cube
file_N2Hp_base_erode_full = 'data/B5_N2H+_1-0_ARGUS_Tmb_8arcsec_full_rebase1_erode.fits'
cube = pyspeckit.Cube(file_N2Hp_base_erode_full)
#
# these values need to be set
cube.xarr.refX = freqLine
cube.xarr.velocity_convention = 'radio'
cube.xarr.convert_to_unit('km/s')
file_N2Hp_base_erode_rms_full = 'data/B5_N2H+_1-0_ARGUS_Tmb_8arcsec_full_rebase1_erode_rms.fits'
if prepareFiles:
rmsMap = fits.getdata(file_N2Hp_base_erode_rms_full)
tpeakMap = cube.slice(vmin, vmax, unit='km/s').cube.max(axis=0)
peakSNR = tpeakMap / rmsMap
planemask = (peakSNR > snr_min)
planemask = immorph.opening(planemask, immorph.disk(5))
hd_cube = cube.header.copy()
removeKeys = ['NAXIS3', 'CRPIX3', 'CDELT3', 'CUNIT3', 'CTYPE3', 'CRVAL3', 'SPECSYS']
for key in removeKeys:
hd_cube.remove(key)
hd_cube['WCSAXES'] = 2
fits.writeto(tpeakFile, tpeakMap, hd_cube, overwrite=True)
fits.writeto(maskFile, planemask.astype(int), hd_cube, overwrite=True)
else:
planemask = fits.getdata(maskFile)
tpeakMap = fits.getdata(tpeakFile)
rmsMap = fits.getdata(file_N2Hp_base_erode_rms_full)
peakSNR = tpeakMap / rmsMap
plt.ion()
cube.Registry.add_fitter('n2hp_vtau', pyspeckit.models.n2hp.n2hp_vtau_fitter, 4)
printOut = []
outFile = []
limMax = []
maxPars = []
fixed = []
verbose = []
guesses = [] # Tex, tau, v_cen, \sigma_v
if optThin:
printOut.append('Starting optically thin fit...')
outFile.append(thinFile)
guesses.append([5.0, 0.1, vmean, 0.2])
limMax.append([True, False, True, True])
maxPars.append([250.0, 0.0, vmax, 1.0])
fixed.append([False, True, False, False])
verbose.append(1)
if optThick:
printOut.append('Starting optically thick fit...')
outFile.append(thickFile)
guesses.append([7.0, 2.0, vmean, 0.1])
limMax.append([True, True, True, True])
maxPars.append([20.0, 50.0, vmax, 1.0])
fixed.append([False, False, False, False])
verbose.append(2)
for i in range(len(printOut)):
print("")
print('%s' % printOut[i])
cube.fiteach(fittype='n2hp_vtau', guesses=guesses[i], verbose_level=verbose[i],
limitedmin=[True, True, True, True], limitedmax=limMax[i],
minpars=[2.75, 0.01, vmin, 0.05], maxpars=maxPars[i], fixed=fixed[i],
use_neighbor_as_guess=True, position_order=1/peakSNR, errmap=rmsMap,
maskmap=planemask, multicore=ncpu, start_from_point=(46, 58))
cube.write_fit(outFile[i], overwrite=True)
#
# some visualization
cube.Registry.add_fitter('n2hp_vtau', pyspeckit.models.n2hp.n2hp_vtau_fitter, 4) # is that really needed again??
vmin = []
vmax = []
modelFile = []
plotNames = []
if showOptThin:
vmin.append(0.05)
vmax.append(0.25)
modelFile.append(thinFile)
plotNames.append('figures/B5_N2Hp_full_pyspeckit_poor_thin_fit.png')
if showOptThick:
vmin.append(0.05)
vmax.append(0.21)
modelFile.append(thickFile)
plotNames.append('figures/B5_N2Hp_full_pyspeckit_poor_thick_fit.png')
for p, plotName in enumerate(plotNames):
cube.load_model_fit(modelFile[p], npars=4, npeaks=1, _temp_fit_loc=(xmax,ymax))
cube.mapplot()
cube.plot_spectrum(xmax, ymax, plot_fit=True)
if plot_dv:
cube.mapplot.plane = cube.parcube[3,:,:]
cube.mapplot(estimator=None, vmin=0.05, vmax=vmax[p])
else:
cube.mapplot.plane = cube.parcube[2,:,:]
cube.mapplot(estimator=None, vmin=vmin_plot, vmax=vmax_plot)
plt.draw()
plt.show()
plt.savefig(plotName)