-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathident.py
More file actions
161 lines (133 loc) · 6.39 KB
/
Copy pathident.py
File metadata and controls
161 lines (133 loc) · 6.39 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
import os
import datetime
import numpy as np
from PyQt6.QtWidgets import QFileDialog, QTableWidgetItem, QMessageBox
from PyQt6.QtCore import Qt, QObject, QThread, pyqtSignal
# from astropy.table import Table as aTable
from OES_toolbox.lazy_import import lazy_import
# aTable = lazy_import("astropy.table")
owl = lazy_import("owlspec")
file_dir = os.path.dirname(os.path.abspath(__file__))
class NISTloader(QObject):
def __init__(self, spec, wl_range, max_y, Te=-1, lw=-1, parent=None):
super(self.__class__, self).__init__(parent)
self.spec = spec
self.wl_range = wl_range
self.max_y = max_y
self.Te = Te
self.lw = lw
finished = pyqtSignal()
result_ready = pyqtSignal(np.ndarray, np.ndarray, str)
data_ready = pyqtSignal(str,object) # Using object instead of astropy.table.Table saves import overhead
progress = pyqtSignal(int)
def run(self):
self.progress.emit(1)
try:
ele_spec = owl.spectrum(self.spec, wl_range=self.wl_range)
nist_data = ele_spec.get_linedata()
self.data_ready.emit(self.spec, nist_data)
if self.Te == -1 and self.lw == -1:
x,y = ele_spec.table_to_ident(nist_data)
if self.Te > 0 and self.lw == -1:
x,y = ele_spec.table_to_ident_LTE(nist_data, self.Te)
y = y/np.max(y)
except Exception as e:
print(e)
x = np.linspace(self.wl_range[0],self.wl_range[-1],10)
y = np.zeros(len(x))
self.result_ready.emit(x,self.max_y*y, "NIST: "+self.spec)
self.progress.emit(-1)
self.finished.emit()
class ident_module:
def __init__(self, mainWindow):
self.mw = mainWindow
self.nist_threads = []
self.nist_workers = []
def update_spec_ident(self):
""" Loads NIST spectra and plots them. """
min_x = 0
max_x = 1100
max_y = 1
lim_unset = True
Te = -1
# remove ident specs
for plot_item in self.mw.specplot.listDataItems():
if "NIST" in plot_item.name():
self.mw.specplot.removeItem(plot_item)
# clear ident table
self.mw.ident_table.setRowCount(0)
# find wavelength range from file spec
min_x, max_x, min_y, max_y = self.mw.get_bounds()
# fetch options
if self.mw.ident_int_cbox.currentIndex() == 1:
Te = self.mw.ident_Te.value()
# spectra from the identify module
spec_string = self.mw.spec_line.text()
if len(spec_string)>1:
for spec in spec_string.split(','):
if '-' in spec:
spec1 = spec.split('-')[0]
element, charge1 = owl.util.parse_spectroscopic_name(spec1)
spec2 = spec.split('-')[1]
spec2 = element + ' ' + spec2
_, charge2 = owl.util.parse_spectroscopic_name(spec2)
subspecs = []
for charge in np.arange(charge1,charge2+1):
this_spec = owl.util.get_spectroscopic_name(element, charge)
subspecs.append(this_spec)
else:
subspecs = [spec]
for spec in subspecs:
nist_thread = QThread()
nist_worker = NISTloader(spec, (min_x,max_x), max_y, Te=Te)
nist_worker.moveToThread(nist_thread)
nist_thread.started.connect(nist_worker.run)
nist_worker.data_ready.connect(self.table_add)
nist_worker.result_ready.connect(self.mw.plot)
nist_worker.progress.connect(self.mw.update_progress_bar)
nist_worker.finished.connect(self.mw.update_spec_colors)
nist_worker.finished.connect(nist_worker.deleteLater)
nist_thread.finished.connect(nist_thread.deleteLater)
nist_thread.start()
# We neet to store the local objects in a "self" list to ensure
# they are not garbage collected right after the button press
self.nist_threads.append(nist_thread)
self.nist_workers.append(nist_worker)
self.mw.update_spec_colors()
def clear_spec_ident(self):
# for thread in self.nist_threads:
# thread.terminate()
# TODO: figure out how to stop process
# clear ident table
self.mw.ident_table.setRowCount(0)
for plot_item in self.mw.specplot.listDataItems():
if "NIST" in plot_item.name():
self.mw.specplot.removeItem(plot_item)
self.mw.update_spec_colors()
def table_add(self, spec, nist_data):
" Add nist_data to the ident table and sort for wl "
for i,line in enumerate(nist_data):
c1 = str(line['Observed'])
# astroquery does not filter out headings in the middle of the table
if 'Observed' in c1 or "Wavelength" in c1 or "nm" in c1:
continue
if not np.ma.is_masked(line['Observed']):
count = self.mw.ident_table.rowCount()
self.mw.ident_table.insertRow(count)
self.mw.ident_table.setItem(count, 0, QTableWidgetItem(str(spec)))
wl_col = QTableWidgetItem()
wl_col.setData(0, round(float(line['Observed']),3))
self.mw.ident_table.setItem(count, 1, wl_col)
self.mw.ident_table.setItem(count, 2, QTableWidgetItem(str(line['Rel.'])))
self.mw.ident_table.setItem(count, 3, QTableWidgetItem(str(line['Aki'])))
self.mw.ident_table.setItem(count, 4, QTableWidgetItem(str(line['Ei Ek'])))
self.mw.ident_table.setItem(count, 5, QTableWidgetItem(str(line['Lower level'])))
self.mw.ident_table.setItem(count, 6, QTableWidgetItem(str(line['Upper level'])))
self.mw.ident_table.sortItems(1, Qt.SortOrder.AscendingOrder)
def ident_int_changed(self, index_selected):
if index_selected == 0: # rel int
self.mw.ident_Te.hide()
self.mw.ident_Te_label.hide()
else: # LTE -> show Te input box
self.mw.ident_Te.show()
self.mw.ident_Te_label.show()