Commit 630b7a7af1ff851480ee38116348bc137dd0a42a

Authored by bmarechal
1 parent 4f6d4cb85c
Exists in master

replace double tabs by one

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

#!/usr/bin/env python 1 1 #!/usr/bin/env python
2 2
# -*- coding: utf-8 -*- 3 3 # -*- coding: utf-8 -*-
4 4
import time, os, instruments, inspect, sys, threading 5 5 import time, os, instruments, inspect, sys, threading
import PyQt4.QtGui as QtGui 6 6 import PyQt4.QtGui as QtGui
from PyQt4.QtCore import pyqtSlot 7 7 from PyQt4.QtCore import pyqtSlot
8 8
#============================================================================== 9 9 #==============================================================================
#============================================================================== 10 10 #==============================================================================
11 11
class acq_routine(): 12 12 class acq_routine():
def __init__(self, instrument, channels, vtypes, address, samplingtime, path = os.getcwd(), fileduration = 24*3600): 13 13 def __init__(self, instrument, channels, vtypes, address, samplingtime, path = os.getcwd(), fileduration = 24*3600):
exec('self.instrument = instruments.%s.%s(%s, %s, "%s")'%(instrument, instrument, channels, vtypes, address)) 14 14 exec('self.instrument = instruments.%s.%s(%s, %s, "%s")'%(instrument, instrument, channels, vtypes, address))
self.path = path 15 15 self.path = path
self.samplingtime = samplingtime 16 16 self.samplingtime = samplingtime
self.fileduration = fileduration 17 17 self.fileduration = fileduration
18 18
def makeTree(self): 19 19 def makeTree(self):
try: 20 20 try:
year = time.strftime("%Y", time.gmtime(self.t0)) 21 21 year = time.strftime("%Y", time.gmtime(self.t0))
month = time.strftime("%Y-%m", time.gmtime(self.t0)) 22 22 month = time.strftime("%Y-%m", time.gmtime(self.t0))
os.chdir(self.path + '/' + year + '/' + month) 23 23 os.chdir(self.path + '/' + year + '/' + month)
except: 24 24 except:
try: 25 25 try:
os.chdir(self.path + '/' + year) 26 26 os.chdir(self.path + '/' + year)
os.mkdir(month) 27 27 os.mkdir(month)
os.chdir(self.path + '/' + year + '/' + month) 28 28 os.chdir(self.path + '/' + year + '/' + month)
except: 29 29 except:
os.chdir(self.path) 30 30 os.chdir(self.path)
os.mkdir(year) 31 31 os.mkdir(year)
os.chdir(self.path + '/' + year) 32 32 os.chdir(self.path + '/' + year)
os.mkdir(month) 33 33 os.mkdir(month)
os.chdir(self.path + '/' + year + '/' + month) 34 34 os.chdir(self.path + '/' + year + '/' + month)
35 35
def connect(self): 36 36 def connect(self):
self.instrument.connect() 37 37 self.instrument.connect()
38 38
self.t0 = time.time() 39 39 self.t0 = time.time()
self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat' 40 40 self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat'
self.makeTree() 41 41 self.makeTree()
self.data_file = open(self.filename, 'wr', 0) 42 42 self.data_file = open(self.filename, 'wr', 0)
43 43
def start(self): 44 44 def start(self):
tic = time.time() 45 45 tic = time.time()
46 46
if (time.time() - self.t0 >= self.fileduration) & (self.fileduration >0 ): 47 47 if (time.time() - self.t0 >= self.fileduration) & (self.fileduration >0 ):
self.data_file.close() 48 48 self.data_file.close()
49 49
self.t0 = time.time() 50 50 self.t0 = time.time()
self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat' 51 51 self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat'
self.makeTree() 52 52 self.makeTree()
self.data_file = open(self.filename, 'wr', 0) 53 53 self.data_file = open(self.filename, 'wr', 0)
54 54
#epoch time 55 55 #epoch time
epoch = time.time() 56 56 epoch = time.time()
#MJD time 57 57 #MJD time
mjd = epoch / 86400.0 + 40587 58 58 mjd = epoch / 86400.0 + 40587
# Meas values 59 59 # Meas values
meas = self.instrument.getValue() 60 60 meas = self.instrument.getValue()
meas = meas.replace(",", "\t") 61 61 meas = meas.replace(",", "\t")
meas = meas.replace(";", "\t") 62 62 meas = meas.replace(";", "\t")
meas = meas.replace("\t\t", "\t") 63
meas = meas.replace("+", "") 64 63 meas = meas.replace("+", "")
meas = meas.replace("E", "e") 65 64 meas = meas.replace("E", "e")
66 65
string = "%f\t%f\t%s" % (epoch, mjd, meas) 67 66 string = "%f\t%f\t%s" % (epoch, mjd, meas)
67 string = string.replace("\t\t", "\t")
self.data_file.write(string) # Write in a file 68 68 self.data_file.write(string) # Write in a file
print(string) 69 69 print(string)
70 70
self.thread = threading.Timer(self.samplingtime - (time.time() - tic), self.start) 71 71 self.thread = threading.Timer(self.samplingtime - (time.time() - tic), self.start)
self.thread.start() 72 72 self.thread.start()
73 73
def stop(self): 74 74 def stop(self):
self.thread.cancel() 75 75 self.thread.cancel()
self.instrument.disconnect() 76 76 self.instrument.disconnect()
self.data_file.close() 77 77 self.data_file.close()
78 78
#============================================================================== 79 79 #==============================================================================
#============================================================================== 80 80 #==============================================================================
81 81
class mainGui(): 82 82 class mainGui():
def __init__(self): 83 83 def __init__(self):
self.setWindow() 84 84 self.setWindow()
self.setSignalsSlots() 85 85 self.setSignalsSlots()
self.runApp() 86 86 self.runApp()
87 87
def setWindow(self): 88 88 def setWindow(self):
self.a = QtGui.QApplication(sys.argv) 89 89 self.a = QtGui.QApplication(sys.argv)
self.w = QtGui.QMainWindow() 90 90 self.w = QtGui.QMainWindow()
self.w.resize(640, 480) 91 91 self.w.resize(640, 480)
self.w.setWindowTitle('datalogger-gui') 92 92 self.w.setWindowTitle('datalogger-gui')
93 93
self.wid = QtGui.QWidget() 94 94 self.wid = QtGui.QWidget()
self.w.setCentralWidget(self.wid) 95 95 self.w.setCentralWidget(self.wid)
self.layout = QtGui.QGridLayout() 96 96 self.layout = QtGui.QGridLayout()
self.wid.setLayout(self.layout) 97 97 self.wid.setLayout(self.layout)
98 98
self.comboInst = QtGui.QComboBox() 99 99 self.comboInst = QtGui.QComboBox()
self.layout.addWidget(self.comboInst, 0, 0) 100 100 self.layout.addWidget(self.comboInst, 0, 0)
101 101
self.address = QtGui.QLineEdit() 102 102 self.address = QtGui.QLineEdit()
self.address.setMinimumWidth(140) 103 103 self.address.setMinimumWidth(140)
self.address.setMaximumWidth(140) 104 104 self.address.setMaximumWidth(140)
self.layout.addWidget(self.address, 0, 1) 105 105 self.layout.addWidget(self.address, 0, 1)
106 106
self.samplingtime = QtGui.QDoubleSpinBox() 107 107 self.samplingtime = QtGui.QDoubleSpinBox()
#self.samplingtime.setMinimumWidth(60) 108 108 #self.samplingtime.setMinimumWidth(60)
#self.samplingtime.setMaximumWidth(60) 109 109 #self.samplingtime.setMaximumWidth(60)
self.samplingtime.setMinimum(0.1) 110 110 self.samplingtime.setMinimum(0.1)
self.samplingtime.setMaximum(1000) 111 111 self.samplingtime.setMaximum(1000)
self.samplingtime.setSingleStep(0.1) 112 112 self.samplingtime.setSingleStep(0.1)
self.samplingtime.setValue(1) 113 113 self.samplingtime.setValue(1)
self.layout.addWidget(self.samplingtime, 0, 2) 114 114 self.layout.addWidget(self.samplingtime, 0, 2)
115 115
self.startButton = QtGui.QPushButton() 116 116 self.startButton = QtGui.QPushButton()
self.startButton.setText('Start log') 117 117 self.startButton.setText('Start log')
self.layout.addWidget(self.startButton, 99, 0) 118 118 self.layout.addWidget(self.startButton, 99, 0)
self.startButton.setEnabled(False) 119 119 self.startButton.setEnabled(False)
120 120
self.stopButton = QtGui.QPushButton() 121 121 self.stopButton = QtGui.QPushButton()
self.stopButton.setText('Stop log') 122 122 self.stopButton.setText('Stop log')
self.layout.addWidget(self.stopButton, 99, 1) 123 123 self.layout.addWidget(self.stopButton, 99, 1)
self.stopButton.setEnabled(False) 124 124 self.stopButton.setEnabled(False)
125 125
self.textDisplay = QtGui.QLabel() 126 126 self.textDisplay = QtGui.QLabel()
self.textDisplay.setText('>>') 127 127 self.textDisplay.setText('>>')
self.layout.addWidget(self.textDisplay, 99, 2) 128 128 self.layout.addWidget(self.textDisplay, 99, 2)
129 129
self.setComboInst() 130 130 self.setComboInst()
self.updateSignal() 131 131 self.updateSignal()
132 132
def setComboInst(self): 133 133 def setComboInst(self):
for name, obj in inspect.getmembers(instruments): 134 134 for name, obj in inspect.getmembers(instruments):
if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False: 135 135 if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False:
self.comboInst.addItem(name) 136 136 self.comboInst.addItem(name)
137 137
def setSignalsSlots(self): 138 138 def setSignalsSlots(self):
self.comboInst.currentIndexChanged.connect(self.updateSignal) 139 139 self.comboInst.currentIndexChanged.connect(self.updateSignal)
self.startButton.clicked.connect(self.startLog) 140 140 self.startButton.clicked.connect(self.startLog)
self.stopButton.clicked.connect(self.stopLog) 141 141 self.stopButton.clicked.connect(self.stopLog)
142 142
def runApp(self): 143 143 def runApp(self):
self.w.show() 144 144 self.w.show()
self.a.aboutToQuit.connect(self.closeEvent) 145 145 self.a.aboutToQuit.connect(self.closeEvent)
sys.exit(self.a.exec_()) 146 146 sys.exit(self.a.exec_())
147 147
def closeEvent(self): 148 148 def closeEvent(self):
try: 149 149 try:
self.stopLog() 150 150 self.stopLog()
except: 151 151 except:
pass 152 152 pass
print('Done') 153 153 print('Done')
154 154
@pyqtSlot() 155 155 @pyqtSlot()
def updateSignal(self): 156 156 def updateSignal(self):
for i in reversed(range(5, self.layout.count())): 157 157 for i in reversed(range(5, self.layout.count())):
self.layout.itemAt(i).widget().setParent(None) 158 158 self.layout.itemAt(i).widget().setParent(None)
159 159
defaultAddress = '' 160 160 defaultAddress = ''
channelsAviables = [] 161 161 channelsAviables = []
vtypesAviables = [] 162 162 vtypesAviables = []
163 163
exec('channelsAviables = instruments.%s.ALL_CHANNELS'%self.comboInst.currentText()) 164 164 exec('channelsAviables = instruments.%s.ALL_CHANNELS'%self.comboInst.currentText())
exec('vtypesAviables = instruments.%s.ALL_VAL_TYPE'%self.comboInst.currentText()) 165 165 exec('vtypesAviables = instruments.%s.ALL_VAL_TYPE'%self.comboInst.currentText())
exec('defaultAddress = instruments.%s.ADDRESS'%self.comboInst.currentText()) 166 166 exec('defaultAddress = instruments.%s.ADDRESS'%self.comboInst.currentText())
167 167
self.address.setText(defaultAddress) 168 168 self.address.setText(defaultAddress)
169 169
self.checkBoxChannels = [None]*len(channelsAviables) 170 170 self.checkBoxChannels = [None]*len(channelsAviables)
self.chListVtypes = [None]*len(self.checkBoxChannels) 171 171 self.chListVtypes = [None]*len(self.checkBoxChannels)
172 172
for i in range(len(self.checkBoxChannels)): 173 173 for i in range(len(self.checkBoxChannels)):
self.checkBoxChannels[i] = QtGui.QCheckBox() 174 174 self.checkBoxChannels[i] = QtGui.QCheckBox()
self.checkBoxChannels[i].setText(channelsAviables[i]) 175 175 self.checkBoxChannels[i].setText(channelsAviables[i])
self.checkBoxChannels[i].setChecked(False) 176 176 self.checkBoxChannels[i].setChecked(False)
self.chListVtypes[i] = QtGui.QListWidget() 177 177 self.chListVtypes[i] = QtGui.QListWidget()
for vtype in vtypesAviables: 178 178 for vtype in vtypesAviables:
self.chListVtypes[i].addItem(vtype) 179 179 self.chListVtypes[i].addItem(vtype)
self.chListVtypes[i].setCurrentRow(0) 180 180 self.chListVtypes[i].setCurrentRow(0)
self.layout.addWidget(self.checkBoxChannels[i], i+3, 1) 181 181 self.layout.addWidget(self.checkBoxChannels[i], i+3, 1)
self.layout.addWidget(self.chListVtypes[i], i+3, 2) 182 182 self.layout.addWidget(self.chListVtypes[i], i+3, 2)
self.checkBoxChannels[i].stateChanged.connect(self.infoSignal) 183 183 self.checkBoxChannels[i].stateChanged.connect(self.infoSignal)
self.chListVtypes[i].currentItemChanged.connect(self.infoSignal) 184 184 self.chListVtypes[i].currentItemChanged.connect(self.infoSignal)
185 185
self.address.textChanged.connect(self.infoSignal) 186 186 self.address.textChanged.connect(self.infoSignal)
self.samplingtime.valueChanged.connect(self.infoSignal) 187 187 self.samplingtime.valueChanged.connect(self.infoSignal)
188 188
self.infoSignal() 189 189 self.infoSignal()
190 190
@pyqtSlot() 191 191 @pyqtSlot()
def infoSignal(self): 192 192 def infoSignal(self):
self.instToLog = self.comboInst.currentText() 193 193 self.instToLog = self.comboInst.currentText()
self.addressToLog = self.address.text() 194 194 self.addressToLog = self.address.text()
self.chToLog = [] 195 195 self.chToLog = []
self.vTypeToLog = [] 196 196 self.vTypeToLog = []
self.ts = self.samplingtime.value() 197 197 self.ts = self.samplingtime.value()
198 198
for i in range(len(self.checkBoxChannels)): 199 199 for i in range(len(self.checkBoxChannels)):
if self.checkBoxChannels[i].isChecked(): 200 200 if self.checkBoxChannels[i].isChecked():
self.chListVtypes[i].setEnabled(True) 201 201 self.chListVtypes[i].setEnabled(True)
self.chToLog.append(str(self.checkBoxChannels[i].text())) 202 202 self.chToLog.append(str(self.checkBoxChannels[i].text()))
self.vTypeToLog.append(str(self.chListVtypes[i].currentItem().text())) 203 203 self.vTypeToLog.append(str(self.chListVtypes[i].currentItem().text()))
else: 204 204 else:
self.chListVtypes[i].setEnabled(False) 205 205 self.chListVtypes[i].setEnabled(False)
206 206
allChannelsUnchecked = False 207 207 allChannelsUnchecked = False