Coverage for larch/wxmap/gse_dtcorrect.py: 23%
161 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
1#!/usr/bin/env python
2"""
3"""
4import os
5import time
6import shutil
7import numpy as np
8from random import randrange
9from functools import partial
10from datetime import timedelta
12import wx
13import wx.lib.scrolledpanel as scrolled
14import wx.lib.mixins.inspection
16HAS_EPICS = False
17try:
18 import epics
19 from epics.wx import DelayedEpicsCallback, EpicsFunction
20 HAS_EPICS = True
21except ImportError:
22 pass
24import larch
25from larch.larchlib import read_workdir, save_workdir
26from larch.utils import get_cwd
27from larch.io import (gsescan_deadtime_correct, gsexdi_deadtime_correct,
28 is_GSEXDI, AthenaProject, new_filename, increment_filename)
30from wxutils import (SimpleText, FloatCtrl, pack, Button, Popup,
31 Choice, Check, MenuItem, GUIColors,
32 CEN, LEFT, FRAMESTYLE, Font)
34from larch.wxlib import LarchWxApp
36CEN |= wx.ALL
37FILE_WILDCARDS = "Scan Data Files(*.0*,*.1*,*.dat,*.xdi)|*.0*;*.1*;*.dat;*.xdi|All files (*)|*"
40def okcancel(panel, onOK=None, onCancel=None):
41 btnsizer = wx.StdDialogButtonSizer()
42 _ok = wx.Button(panel, wx.ID_OK)
43 _no = wx.Button(panel, wx.ID_CANCEL)
44 panel.Bind(wx.EVT_BUTTON, onOK, _ok)
45 panel.Bind(wx.EVT_BUTTON, onCancel, _no)
46 _ok.SetDefault()
47 btnsizer.AddButton(_ok)
48 btnsizer.AddButton(_no)
49 btnsizer.Realize()
50 return btnsizer
52class DTCorrectFrame(wx.Frame):
53 _about = """GSECARS Deadtime Corrections
54 Matt Newville <newville @ cars.uchicago.edu>
55 """
56 def __init__(self, _larch=None, **kws):
58 wx.Frame.__init__(self, None, -1, style=FRAMESTYLE)
59 self.file_groups = {}
60 self.file_paths = []
61 title = "DeadTime Correction "
62 self.larch = _larch
63 self.subframes = {}
65 self.SetSize((500, 275))
66 self.SetFont(Font(10))
68 self.config = {'chdir_on_fileopen': True}
69 self.SetTitle(title)
70 self.createMainPanel()
71 self.createMenus()
72 self.statusbar = self.CreateStatusBar(2, 0)
73 self.statusbar.SetStatusWidths([-3, -1])
74 statusbar_fields = ["Initializing....", " "]
75 for i in range(len(statusbar_fields)):
76 self.statusbar.SetStatusText(statusbar_fields[i], i)
78 def onBrowse(self, event=None):
79 dlg = wx.FileDialog(parent=self,
80 message='Select Files',
81 defaultDir=get_cwd(),
82 wildcard =FILE_WILDCARDS,
83 style=wx.FD_OPEN|wx.FD_MULTIPLE|wx.FD_CHANGE_DIR)
85 if dlg.ShowModal() == wx.ID_OK:
86 paths = dlg.GetPaths()
87 path = paths[0]
88 mdir, p = os.path.split(path)
89 os.chdir(mdir)
90 roiname = self.wid_roi.GetValue().strip()
91 if len(roiname) < 1:
92 Popup(self,
93 'Must give ROI name!', 'No ROI name')
94 return
95 dirname = self.wid_dir.GetValue().strip()
96 if len(dirname) > 1 and not os.path.exists(dirname):
97 try:
98 os.mkdir(dirname)
99 except:
100 Popup(self,
101 'Could not create directory %s' % dirname,
102 "could not create directory")
103 return
104 badchans = self.wid_bad.GetValue().strip()
105 bad_channels = []
106 if len(badchans) > 0:
107 bad_channels = [int(i.strip()) for i in badchans.split(',')]
109 groups = []
110 for fname in dlg.GetFilenames():
111 corr_fcn = gsescan_deadtime_correct
112 if is_GSEXDI(fname):
113 corr_fcn = gsexdi_deadtime_correct
114 print("Correcting %s" % (fname))
115 out = corr_fcn(fname, roiname, subdir=dirname, bad=bad_channels)
116 if out is not None:
117 out.mu = out.mufluor
118 out.filename = fname
119 groups.append((out, fname))
121 athena_name = os.path.join(dirname, self.wid_ath.GetValue().strip())
122 if self.wid_autoname.IsChecked():
123 athena_name = new_filename(athena_name)
125 _, aname = os.path.split(athena_name)
126 self.wid_ath.SetValue(increment_filename(aname))
128 aprj = AthenaProject(filename=athena_name)
129 for grp, label in groups:
130 aprj.add_group(grp, signal='mu')
131 aprj.save(use_gzip=True)
132 print("Corrected %i files, wrote %s" % (len(groups), aname))
134 def createMainPanel(self):
135 panel = wx.Panel(self)
136 sizer = wx.GridBagSizer(5, 4)
138 lab_roi = SimpleText(panel, ' Element / ROI Name:')
139 lab_dir = SimpleText(panel, ' Output Folder:')
140 lab_ath = SimpleText(panel, ' Athena Project File:')
141 lab_bad = SimpleText(panel, ' Bad Channels:')
142 lab_sel = SimpleText(panel, ' Select Files:')
144 self.wid_roi = wx.TextCtrl(panel, -1, '', size=(200, -1))
145 self.wid_dir = wx.TextCtrl(panel, -1, 'DT_Corrected', size=(200, -1))
146 self.wid_ath = wx.TextCtrl(panel, -1, 'Athena_001.prj', size=(200, -1))
147 self.wid_bad = wx.TextCtrl(panel, -1, ' ', size=(200, -1))
148 self.wid_autoname = Check(panel, default=True,
149 size=(150, -1), label='auto-increment?')
151 self.sel_wid = Button(panel, 'Browse', size=(100, -1),
152 action=self.onBrowse)
154 ir = 0
155 sizer.Add(lab_roi, (ir, 0), (1, 1), LEFT, 2)
156 sizer.Add(self.wid_roi, (ir, 1), (1, 1), LEFT, 2)
157 ir += 1
158 sizer.Add(lab_dir, (ir, 0), (1, 1), LEFT, 2)
159 sizer.Add(self.wid_dir, (ir, 1), (1, 1), LEFT, 2)
160 ir += 1
161 sizer.Add(lab_ath, (ir, 0), (1, 1), LEFT, 2)
162 sizer.Add(self.wid_ath, (ir, 1), (1, 1), LEFT, 2)
163 sizer.Add(self.wid_autoname, (ir, 2), (1, 1), LEFT, 2)
165 ir += 1
166 sizer.Add(lab_bad, (ir, 0), (1, 1), LEFT, 2)
167 sizer.Add(self.wid_bad, (ir, 1), (1, 1), LEFT, 2)
168 ir += 1
169 sizer.Add(lab_sel, (ir, 0), (1, 1), LEFT, 2)
170 sizer.Add(self.sel_wid, (ir, 1), (1, 1), LEFT, 2)
172 pack(panel, sizer)
173 wx.CallAfter(self.init_larch)
174 return
176 def init_larch(self):
177 t0 = time.time()
178 if self.larch is None:
179 self.larch = larch.Interpreter()
180 self.larch.symtable.set_symbol('_sys.wx.wxapp', wx.GetApp())
181 self.larch.symtable.set_symbol('_sys.wx.parent', self)
182 self.SetStatusText('ready')
184 def write_message(self, s, panel=0):
185 """write a message to the Status Bar"""
186 self.SetStatusText(s, panel)
188 def createMenus(self):
189 # ppnl = self.plotpanel
190 self.menubar = wx.MenuBar()
191 fmenu = wx.Menu()
192 MenuItem(self, fmenu, "&Quit\tCtrl+Q", "Quit program", self.onClose)
193 self.menubar.Append(fmenu, "&File")
194 self.SetMenuBar(self.menubar)
196 def onClose(self,evt):
197 self.Destroy()
200class DTViewer(LarchWxApp):
201 def __init__(self, _larch=None, **kws):
202 LarchWxApp.__init__(self, _larch=_larch, **kws)
204 def createApp(self):
205 frame = DTCorrectFrame(_larch=self._larch)
206 frame.Show()
207 self.SetTopWindow(frame)
208 return True
210def dtcorrect(wxparent=None, _larch=None, **kws):
211 s = DTCorrectFrame(_larch=_larch, **kws)
212 s.Show()
213 s.Raise()