-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundCIMP.py
More file actions
205 lines (147 loc) · 5.16 KB
/
BackgroundCIMP.py
File metadata and controls
205 lines (147 loc) · 5.16 KB
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
"""
This is similar to BackgroundNRL.py but uses background computed using
the CIMP Background class.
"""
from matplotlib.colors import Colormap
from CIMP import Enhance
import math
import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
import sunpy.map
import sunpy.visualization.colormaps as cm
from sunpy.net import attrs as a
from sunpy.net import Fido
from sunpy.io import fits
import sunkit_image.utils.utils
from scipy import interpolate
#======================================================================
def radial_cut(r,a,N=100):
"""
extract a curve vs theta at a selected radius
radius is given in terms of the size of the image, assuming
the range of the image goes from -1 to 1
"""
ny, nx = data.shape[:2]
x = np.arange(nx)
y = np.arange(ny)
theta = np.linspace(0.,2*np.pi,N)
ainterp = interpolate.interp2d(x, y, a, kind='linear')
acut = np.zeros(N)
for idx in np.arange(N):
xp = (1.0 + r1 * np.cos(theta[idx])) * nx/2
yp = (1.0 + r1 * np.sin(theta[idx])) * ny/2
ap = ainterp(xp,yp)
acut[idx] = ap[0]
theta_deg = theta * 180.0 / np.pi
return acut, theta_deg
#======================================================================
# copied over from the Enhance class for convenience
def mask_annulus(im, rmin = 0.0, rmax = None, missingval = 0.0):
"""
This sets the pixels inside rmin and/or outside rmax to the missing value (default 0)
"""
nx = im.shape[0]
ny = im.shape[1]
nn = 0.5 * float(np.min((nx, ny)))
rr1 = (rmin*nn)**2
if rmax is None:
rr2 = np.inf
else:
rr2 = (rmax*nn)**2
x0 = 0.5*float(im.shape[0])
y0 = 0.5*float(im.shape[1])
for i in np.arange(0,im.shape[0]):
for j in np.arange(0,im.shape[1]):
r2 = (float(i)-x0)**2 + (float(j)-y0)**2
if (r2 < rr1) or (r2 > rr2):
im[i,j] = missingval
#======================================================================
pcase = 3
if pcase == 1:
instrument = 'lasco'
detector = 'c3'
dir = '/home/mark.miesch/data/lasco_monthly/c3/2012_04'
file = dir+'/15/32296650.fts'
bgfile = dir+'/'+'background.fts'
elif pcase == 2:
instrument = 'lasco'
detector = 'c3'
dir = '/home/mark.miesch/data/lasco_monthly/c3/2014_01'
file = dir+'/17/33385479.fts'
bgfile = dir+'/'+'background.fts'
elif pcase == 3:
instrument = 'stereo'
detector = 'cor2'
dir = '/home/mark.miesch/sunpy/data/secchi_cor2/L1/2012/09'
#file = dir+'/20/20120920_172400_14c2A.fts'
file = dir+'/20/20120920_002400_14c2A.fts'
bgfile = dir+'/'+'background.fts'
else:
print("Pick a valid test case")
exit()
#======================================================================
# basic image to work with
data, header = fits.read(file)[0]
# Background file computed by CIMP
bkg, bheader = sunpy.io.fits.read(bgfile)[0]
#======================================================================
# apply a mask fot data outside the FOV
pos_pix = np.ma.masked_less_equal(bkg, 0.0)
bgmin = pos_pix.min()
print(f"background missing val {bgmin}")
mask_annulus(data, rmin = 0.15, rmax = 1.0)
mask_annulus(bkg, rmin = 0.15, rmax = 1.0, missingval = bgmin)
#======================================================================
amap = sunpy.map.Map(data,header)
print(f"data range: {amap.min()}, {amap.max()}")
bmap = sunpy.map.Map(bkg,header)
print(f"bkg range: {bmap.min()}, {bmap.max()}")
#======================================================================
# Now plot
cmap = plt.get_cmap('stereocor2')
fig = plt.figure(figsize=[16,12])
ax = fig.add_subplot(2,3,1,projection=amap)
amap.plot(cmap=cmap,vmin=0,vmax=1.e-8)
ax = fig.add_subplot(2,3,4,projection=bmap)
bmap.plot(cmap=cmap,vmin=0,vmax=1.e-8)
#======================================================================
# polar intensity plots at a particular radius
r1 = 0.2
acut, theta = radial_cut(r1,data)
bcut, theta = radial_cut(r1,bkg)
ax = fig.add_subplot(2,3,2)
plt.plot(theta,acut,'black')
plt.plot(theta,bcut,'blue')
plt.title("r = 0.2")
r1 = 0.8
acut, theta = radial_cut(r1,data)
bcut, theta = radial_cut(r1,bkg)
ax = fig.add_subplot(2,3,5)
plt.plot(theta,acut,'black')
plt.plot(theta,bcut,'blue')
plt.title("r = 0.8")
#======================================================================
# difference
pa = data - bkg
pb = Enhance.powerlaw(pa)
pmap = sunpy.map.Map(pb,header)
#dmap = Enhance.nrgf(pmap, instrument, detector)
ax = fig.add_subplot(2,3,3,projection=pmap)
print(f"Difference minmax: {pmap.min()} {pmap.max()}")
#pmap.plot(cmap=cmap,vmin=0,vmax=1.0e3)
pmap.plot(cmap=cmap,vmin=0,vmax=1.0e-9)
#pmap.plot(cmap=cmap)
#dmap.plot(cmap=cmap,clip_interval=(1,99)*u.percent)
#======================================================================
# ratio
rat = np.where(bkg <= 0.0, 0.0, data/bkg)
rmap = sunpy.map.Map(rat,header)
#pa = Enhance.powerlaw(rat,n=1.0)
#rmap = sunpy.map.Map(pa,header)
#emap = Enhance.fnrgf(rmap, instrument, detector)
#emap = Enhance.nrgf(rmap, instrument, detector)
ax = fig.add_subplot(2,3,6,projection=rmap)
print(f"Ratio minmax: {rmap.min()} {rmap.max()}")
rmap.plot(cmap=cmap,vmin=1.0,vmax=3)
plt.show()