-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_fits_file.py
206 lines (160 loc) · 6.78 KB
/
make_fits_file.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
import numpy as np, matplotlib.pyplot as plt
import argparse
import glob
import aplpy
from astropy.io import fits
from astropy.wcs import WCS
from matplotlib import gridspec
from astropy.utils.data import get_pkg_data_filename
"""
The Purpose of this script it to find the proper scaling factor S and convert unity to Jansky. This script also makes Stokes images with two type of corrd axies [galactic long. {Deg}, galactic lat. {Deg}] and [RA {Hr}, Dec {Deg}].
"""
def S_sgrA(nu): # Flux density of GC
# Make Amplitude Scale Flux Desnity of the Non-Linear Galactic Center
# SSgrA∗ (ν) ≈ 3709 Jy × (ν/408 MHz)−0.5 ---> Saul's Paper [Eq.11]
flux_density= 3709 # [Jansky]
nu_0 = 408 # [MHz]
alpha = -05
S_of_nu = flux_density*np.power(nu/nu_0,-0.5)
hdu = fits.PrimaryHDU(S_of_nu)
hdu.writeto("Scale_Spectrum_of_GC.fits",overwrite=True) #save
return S_of_nu
"""
# This makes a spectrum and plots it.
nimage = 150
freq = np.linspace(115,188,nimage)
S = S_sgrA(freq)
for ind in range(nimage):
plt.plot(chan[ind],S[ind].max(),'.')
plt.xlabel("Channel [Arb]")
#plt.plot("Freq [MHz]")
plt.ylabel("Flux Density [Arb]")
plt.title('Flux Density Spectrum Scale Value')
plt.savefig('{}_ScaleValue_FluxDensitySpectrum.png'.format(filename))
plt.show()
plt.close()
"""
files_from_fits = glob.glob('zen.2457755.89889.conj.HH.uv.TrueVis.TrueVis_NoDeconvolution_Saul_paper_Fig4.image.fits')[0]
my_sim = fits.open(files_from_fits)
freq_value = my_sim[0].header['CRVAL3']*1e-6 # In units on MHz
copy_fits = my_sim[0].copy()
S = S_sgrA(np.abs(freq_value)) #insert the freq given in GHz. Convert to MHz and take the absolute value
copy_fits.data *= S
# Write it out to a new fits file
# You probably don't need to do this step, just fine the factor array and then mult it by the data but it might be a good idea to have the numbers saved somewhere.
#hdu1 = fits.PrimaryHDU(S)
#hdu1.writeto("factor.fits",overwrite=True)
copy_fits.writeto("scaled_{}".format(files_from_fits),overwrite=True)
# New STOKES Visibility Plots
# Mult my fits data by this factor and create new product fits file
#----------------------------------------------------------------------------------
#==================================================================================
# No WCS header added
#factor = fits.open("factor.fits")[0].data
new_my_sim = S*my_sim_data
wcs = WCS(my_sim[0].header,naxis=2)
iarr = [0,0,1,1]
jarr = [0,1,0,1]
nrow,ncol = 2,2
stoke = ['I','Q','U','V']
name = "scaled_{}".format(fitsfile)
for fitsfile in files_from_fits:
f,axarr = plt.subplots(nrow,ncol,sharex=True,sharey=True,figsize=(15,8))
f.suptitle("Scaled STOKES Visibility Plots: {}".format(fitsfile), fontsize=14)
gs = gridspec.GridSpec(nrow,ncol)
for pol in range(4):
ax=plt.subplot(gs[iarr[pol],jarr[pol]],projection=wcs)
#ax[iarr[pol],jarr[pol]].subplot(projection=wcs)
if pol == 0:
vmax=3.e4
vmin=0
cmap="viridis"
cax=ax.imshow(new_my_sim[pol,0,:,:], vmax=vmax, vmin=vmin, cmap=cmap, origin='lower')
f.colorbar(cax,label='Stokes '+stoke[pol])
ax.grid(color='white', ls='solid',which='major')
ax.set_xlabel('Galactic Longitude [Deg]')
ax.set_ylabel('Galactic Latitude [Deg]')
if pol == 1:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
cax=ax.imshow(new_my_sim[pol,0,:,:], vmax=vmax, vmin=vmin, cmap=cmap, origin='lower')
f.colorbar(cax,label='Stokes '+stoke[pol])
ax.grid(color='k', ls='solid',which='major')
ax.set_xlabel('Galactic Longitude [Deg]')
ax.set_ylabel('Galactic Latitude [Deg]')
if pol == 2:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
cax=ax.imshow(new_my_sim[pol,0,:,:], vmax=vmax, vmin=vmin, cmap=cmap, origin='lower')
f.colorbar(cax,label='Stokes '+stoke[pol])
ax.grid(color='k', ls='solid',which='major')
ax.set_xlabel('Galactic Longitude [Deg]')
ax.set_ylabel('Galactic Latitude [Deg]')
if pol == 3:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
cax=ax.imshow(new_my_sim[pol,0,:,:], vmax=vmax, vmin=vmin, cmap=cmap, origin='lower')
f.colorbar(cax,label='Stokes '+stoke[pol])
ax.grid(color='k', ls='solid',which='major')
ax.set_xlabel('Galactic Longitude [Deg]')
ax.set_ylabel('Galactic Latitude [Deg]')
f.savefig("type1_{}_scaled_STOKES.png".format(fitsfile))
#plt.show()
plt.close()
"""
# I think this is the orginal command line stuff
factor = fits.open("factors.fits")[0].data
new_my_sim = factor*my_sim_data
wcs = WCS(my_sim[0].header,naxis=2)
plt.subplot(projection=wcs)
plt.imshow(new_my_sim[0,0,:,:], vmin=0, vmax=3.e4, origin='lower')
plt.colorbar()
plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude [Deg]')
plt.ylabel('Galactic Latitude [Deg]')
plt.show()
"""
#==================================================================================
#----------------------------------------------------------------------------------
# WCS header added to new fits file. Now you can use aply to make the plots.
files_from_fits = glob.glob(name)
for fitsfile in files_from_fits:
f = plt.figure(figsize=(15,8))
for pol in np.arange(4):
fig = aplpy.FITSFigure(fitsfile,dimensions=[0,1],slices=[0,pol],figure=f,subplot=(2,2,pol+1))
if pol == 0:
vmax=3.e4
vmin=0
cmap="viridis"
if pol == 1:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
if pol == 2:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
if pol == 3:
vmax=3.e2
vmin=-3.e2
cmap="RdYlGn"#"PRGn"
fig.show_colorscale(cmap=cmap,vmax=vmax,vmin=vmin)#,stretch='arcsczdxcinh')
fig.add_grid()
fig.grid.set_color(color='black')#, linestyle='solid')
fig.grid.set_xspacing(15)
fig.grid.set_yspacing(15)
fig.grid.show()
fig.axis_labels.set_font(size='small')
fig.tick_labels.set_font(size='small')
fig.tick_labels.set_xformat('hh')
fig.tick_labels.set_yformat('dd')
fig.add_colorbar()
fig.colorbar.set_font(size='small')
plt.suptitle('{} STOKE Visibilities'.format(fitsfile))
fig.savefig('type2_{}_scaled_STOKES.png'.format(fitsfile))
plt.close()
#----------------------------------------------------------------------------------
#==================================================================================