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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
#!/usr/bin/env python
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2020 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import sys
import os
import numpy
import types
import logging
_logger = logging.getLogger(__name__)
#spx and rtx file formats based on XML
import xml.etree.ElementTree as ElementTree
from PyMca5.PyMcaIO import SpecFileAbstractClass
def myFloat(x):
try:
return float(x)
except ValueError:
if ',' in x:
try:
return float(x.replace(',','.'))
except:
return float(x)
elif '.' in x:
try:
return float(x.replace('.',','))
except:
return float(x)
else:
raise
class ArtaxFileParser(object):
'''
Class to read ARTAX .spx or .rtx files
'''
def __init__(self, filename):
'''
Parameters:
-----------
filename : str
Name of the .spx or .rtx file.
'''
if not os.path.exists(filename):
raise IOError("File %s does not exists" % filename)
if not isArtaxFile(filename):
raise IOError("This does not look as an Artax file")
f = ElementTree.parse(filename)
root = f.getroot()
self._classDict = {}
for classType in [#'TRTProject',
#'TRTBase',
#'TScanInfo',
#'TRTImageData',
'TRTSpectrum']:
content = root.findall(".//ClassInstance[@Type='%s']" % classType)
self._classDict[classType] = content
self.__artaxTScanInfo = self.__getArtaxTScanInfo(root)
self._cacheScan = None
self._file = os.path.abspath(filename)
if self.scanno():
self._cacheScan = ArtaxScan(self._classDict["TRTSpectrum"][0], 0, self._file)
self._lastScan = 0
@property
def artaxTScanInfo(self):
return self.__artaxTScanInfo
def __getArtaxTScanInfo(self, root):
# obtain some Artax Map specific information
node = root.find(".//ClassInstance[@Type='TScanInfo']")
scanInfoKeys = ["XFirst",
"YFirst",
"ZFirst",
"XLast",
"YLast",
"ZLast",
"MeasNo", # number of spectra
"Mapping"]
scanInfo = {}
if node:
for child in node:
if child.tag in scanInfoKeys:
key = child.tag
if key == "Mapping":
if child.text.upper() == "TRUE":
scanInfo[key] = True
else:
scanInfo[key] = False
else:
scanInfo[key] = myFloat(child.text)
for key in scanInfoKeys:
if key in scanInfo:
continue
if key == "Mapping":
scanInfo[key] = False
else:
scanInfo[key] = numpy.nan
# done with the Artax map specific information
return scanInfo
def scanno(self):
return len(self._classDict["TRTSpectrum"])
def __getitem__(self, item):
if item == self._lastScan and self._cacheScan:
scan = self._cacheScan
else:
scan = ArtaxScan(self._classDict["TRTSpectrum"][item], item, self._file)
self._lastScan = item
self._cacheScan = scan
return scan
def list(self):
return "1:%d" % self.scanno()
def select(self, key):
"""
key is of the from s.o
scan number, scan order
"""
n = key.split(".")
return self.__getitem__(int(n[0])-1)
def allmotors(self):
if self.scanno():
return self._cacheScan._motorNames * 1
else:
return []
class ArtaxScan(object):
def __init__(self, spectrumNode, number, filename):
self._node = spectrumNode
self._number = number
self.__data = None
command = ""
if "Name" in spectrumNode.keys():
command = "%s" % self._node.attrib["Name"]
else:
command = "TRTSpectrum %d" % number
# we expect only one spectrum (if not we would use findall)
self._spectra = [self._node.find(".//Channels")]
# get the position(s) at which the spectrum was collected
self._motorNames = []
self._motorValues = []
keyToSearch = ".//ClassInstance[@Type='TRTAxesHeader']//AxesParameter"
positionsNode = self._node.find(keyToSearch)
if positionsNode:
for child in positionsNode:
if "AxisName" in child.attrib:
motorName = child.attrib["AxisName"]
if "AxisPosition" in child.attrib:
motorValue = myFloat(child.attrib["AxisPosition"])
self._motorNames.append(motorName)
self._motorValues.append(motorValue)
elif "PDHID" in command and "(X " in command and " Y " in command:
# Aaron's approximation to Artax files
# we should not crash because of it
try:
import re
expr = r"(?:[XY] \d+(?:[.]\d*)?|[.]\d+)"
XY = re.findall(expr, command)
if len(XY) == 2:
X, Y = XY
self._motorNames = ["x", "y"]
self._motorValues = [myFloat(X.split(" ")[-1]),
myFloat(Y.split(" ")[-1])]
except:
_logger.warning("Could not extract positions from %s" % command)
# get the additional information
info = {}
infoKeys = ['HighVoltage', 'TubeCurrent',
'RealTime', 'LifeTime', 'DeadTime',
'ZeroPeakPosition', 'ZeroPeakFrequency', 'PulseDensity',
'Amplification', 'ShapingTime',
'Date','Time',
'ChannelCount','CalibAbs', 'CalibLin']
classTypeList = ['TRTSpectrumHeader',
'TRTGeneratorHeader',
'TRTSpectrumHardwareHeader']
for classType in classTypeList:
nodeToSearch = ".//ClassInstance[@Type='%s']" % classType
target = self._node.find(nodeToSearch)
if target is None:
_logger.debug("Unused class <%s>" % classType)
continue
for child in target:
if child.tag in ["Date", "Time"]:
info[child.tag] = child.text
elif child.tag in infoKeys:
info[child.tag] = myFloat(child.text)
for key in infoKeys:
if key not in info:
_logger.debug("key not found %s" % key)
self._command = command
scanheader = []
scanheader.append("#S %d %s" % (self._number + 1, self.command()))
i = 0
for key in infoKeys:
scanheader.append("#U%d %s %s" % (i, key, info.get(key, "Unknown")))
i += 1
liveTime = info.get('LifeTime', None)
realTime = info.get('RealTime', liveTime)
if liveTime is not None:
scanheader.append("#@CTIME %f %f %f" % (myFloat(realTime)/1000.,
myFloat(liveTime)/1000.,
myFloat(realTime)/1000.))
scanheader.append("#@CALIB %f %f 0" % (myFloat(info.get('CalibAbs', 0.0)),
myFloat(info.get('CalibLin', 1.0))))
self._scanHeader = scanheader
self._fileHeader = ["#F %s" % filename]
if len(self._motorNames):
spacing = " " * 4
motorsLine = "#O0%s" % spacing
for mne in self._motorNames:
motorsLine += "%s%s" % (spacing, mne)
self._fileHeader.append(motorsLine)
def _readSpectrum(self, channelsNode):
if self.__data is None:
self.__data = numpy.array([myFloat(x) for x in channelsNode.text.split(',')],
dtype=numpy.float32)
return self.__data
def nbmca(self):
return len(self._spectra)
def mca(self, number):
return self._readSpectrum(self._spectra[number - 1])
def alllabels(self):
return []
def allmotorpos(self):
return self._motorValues
def command(self):
return self._command
def date(self):
return self._data["TimeStamp"][self._number]
def fileheader(self):
return self._fileHeader
def header(self, key):
_logger.debug("Requested key = %s", key)
_logger.debug("Requested key = %s", key)
if key in ['S', '#S']:
return self.fileheader()[0]
elif key == 'N':
return []
elif key == 'L':
return []
elif key in ['D', '@CTIME', '@CALIB', '@CHANN']:
for item in self._scanHeader:
if item.startswith("#" + key):
return [item]
return []
elif key == "" or key == " ":
return self._scanHeader
else:
return []
def order(self):
return 1
def number(self):
return self._number + 1
def lines(self):
return 0
def isArtaxFile(filename):
try:
if filename[-3:].lower() not in ["rtx", "spx"]:
return False
with open(filename, 'rb') as f:
# expected to read an xml file
someChar = f.read(20).decode()
if "xml version" in someChar:
return True
except:
pass
return False
def test(filename):
if isArtaxFile(filename):
sf=ArtaxFileParser(filename)
else:
print("Not an Artax .spx or .rtx File")
return
sf = ArtaxFileParser(filename)
print(sf[0].nbmca())
print(sf[0].mca(1))
print(sf[0].header('S'))
#print(sf[0].header('D'))
#print(sf[0].alllabels())
print(sf.allmotors())
print(sf[0].allmotorpos())
print(sf[0].header('@CTIME'))
print(sf[0].header('@CALIB'))
print(sf[0].header(''))
if __name__ == "__main__":
test(sys.argv[1])
|