-
Notifications
You must be signed in to change notification settings - Fork 1
/
AIA_pipeline.py
executable file
·161 lines (119 loc) · 6.55 KB
/
AIA_pipeline.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
import os
import fnmatch
import sunpy.map as map
import sunpy.instr.aia as aia
import astropy.units as u
from astropy.io import fits
import numpy as np
from skimage import restoration
from sunpy.image.coalignment import mapcube_coalign_by_match_template
from scipy.signal import fftconvolve
import matplotlib.pyplot as plt
class CubeProcess:
def __init__(self, wavelength, directory=None):
"""
All of the routines needed to process and AIA image through the DeNoising pipeline.
(We can slot actual de-noising routines in as needed)
:param wavelength: aia wavelength directory to process
"""
self.wavelength = wavelength * u.AA
self.get_files(directory)
self.get_psf()
def get_files(self, directory=None):
if directory:
self.dir = directory
else:
directory = {94 * u.AA: '/Volumes/DataDisk/AIA/094/',
131 * u.AA: '/Volumes/DataDisk/AIA/131/',
171 * u.AA: '/Volumes/DataDisk/AIA/171/',
193 * u.AA: '/Volumes/DataDisk/AIA/193/',
211 * u.AA: '/Volumes/DataDisk/AIA/211/',
304 * u.AA: '/Volumes/DataDisk/AIA/304/',
335 * u.AA: '/Volumes/DataDisk/AIA/335/'}
self.dir = directory[self.wavelength]
self.filelist = fnmatch.filter(os.listdir(self.dir), '*aia*'+str(np.int(self.wavelength.value))+'*.fits')
def read_aia(self, ii):
self.full_map = map.Map(self.dir + self.filelist[ii])
def get_psf(self, origin='LMSAL'):
if origin == 'LMSAL':
psf_files = {94 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_94_PSF.fits',
131 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_131_PSF.fits',
171 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_171_PSF.fits',
193 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_193_PSF.fits',
211 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_211_PSF.fits',
304 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_304_PSF.fits',
335 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/LMSAL/AIA_335_PSF.fits'}
self.psf = fits.getdata(psf_files[self.wavelength])
if origin == 'SWRI':
psf_files = {94 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF94A.fits',
131 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF131A.fits',
171 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF171A.fits',
193 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF193A.fits',
211 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF211A.fits',
304 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF304A.fits',
335 * u.AA: '/Users/mskirk/data/AIA/Point Spread Function/SWRI/PSF335A.fits'}
self.psf = np.pad(fits.getdata(psf_files[self.wavelength]),(1847,1848), 'constant', constant_values=(0,0))
def deconvolve_psf(self, method='WH'):
self.full_map.meta['lvl_num'] = 1.4
if method == 'WH':
self.full_map.data = restoration.unsupervised_wiener(self.full_map.data.astype('float64'), self.psf.astype('float64'), clip=False)[0]
if method == 'RL_SSW':
# should be equivalent to the IDL AIA_DECONVOLVE_RICHARDSONLUCY() routine but it isn't
# Not working...
image = self.full_map.data.astype(np.float)
im_deconv = np.copy(self.full_map.data.astype(np.float))
psf = self.psf.astype(np.float)
psf_mirror = psf[::-1, ::-1] # to make the correlation easier
psfnorm = fftconvolve(psf, np.ones_like(psf), 'same')
for _ in range(25):
relative_blur = image / fftconvolve(psf, im_deconv, 'same')
im_deconv *= fftconvolve(relative_blur, psf_mirror, 'same')/psfnorm
self.full_map.data=np.abs(im_deconv)
if method == 'RL':
# Not working...
self.full_map.data = restoration.richardson_lucy(self.full_map.data.astype('float64'), self.psf.astype('float64'), iterations=25, clip=False)
def region_cutout(self):
self.aia_promote()
ji_xrange = [-539.16847142, -115.07568342] * u.arcsec
ji_yrange = [-116.93790609, 113.73012591] * u.arcsec
self.submap = self.full_map.submap(ji_xrange, ji_yrange)
def aia_promote(self):
if self.full_map.processing_level == 1.4:
self.full_map = aia.aiaprep(self.full_map)
self.full_map.meta['lvl_num'] = 1.6
else:
self.full_map = aia.aiaprep(self.full_map)
self.full_map.meta['lvl_num'] = 1.5
def image_save(self, outfile, svdir, filenm):
outfile.save(svdir+filenm, filetype='fits', clobber=True)
def png_save(self, outfile, svdir, filenm):
outfile.plot()
plt.savefig(svdir+filenm, bbox_inches='tight')
def im_coalign(self):
self.mapcube = mapcube_coalign_by_match_template(self.mapcube, layer_index=np.round(len(self.mapcube)/2))
class CubeAnalysis:
def __init__(self, wavelength, directory=None):
"""
All of the routines needed to analyse AIA image cubes.
:param wavelength: aia wavelength directory to process
"""
self.wavelength = wavelength * u.AA
self.get_files(directory)
def get_files(self, directory=None):
if directory:
self.dir = directory
else:
directory = {94 * u.AA: '/Volumes/DataDisk/AIA/094/',
131 * u.AA: '/Volumes/DataDisk/AIA/131/',
171 * u.AA: '/Volumes/DataDisk/AIA/171/',
193 * u.AA: '/Volumes/DataDisk/AIA/193/',
211 * u.AA: '/Volumes/DataDisk/AIA/211/',
304 * u.AA: '/Volumes/DataDisk/AIA/304/',
335 * u.AA: '/Volumes/DataDisk/AIA/335/'}
self.dir = directory[self.wavelength]
self.filelist = fnmatch.filter(os.listdir(self.dir), '*aia*'+str(np.int(self.wavelength.value))+'*.fits')
def make_cube(self):
self.mapcube = sunpy.map.Map(self.dir+'*.fits', cube=True)
def read_image(self, ii):
self.map = map.Map(self.dir + self.filelist[ii])
def cube_ssim(self, comp_dir):