Source code for casinotools.file_format.casino2.xray_radial_reader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. py:currentmodule:: casinotools.file_format.casino2.xray_radial_reader
.. moduleauthor:: Hendrix Demers <hendrix.demers@mail.mcgill.ca>
Description
"""
###############################################################################
# Copyright 2020 Hendrix Demers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
# Standard library modules.
# Third party modules.
# Local modules.
# Project modules.
from casinotools.file_format.casino2.xray_radial import XrayRadial, DISTANCE_nm, INTENSITY_ABSORBED
# Globals and constants variables.
K = 'K'
L = 'LIII'
M = 'MV'
HEADER_ELEMENT_LINE = "Radial XRay Distribution"
HEADER_ELEMENT = "Radial Distribution of"
HEADER_ALL = "XRay Radial of"
[docs]
class XrayRadialReader:
def __init__(self):
self._data = {}
self._labels = []
self._currentElementSymbol = None
self._currentLine = None
[docs]
def readTextFile(self, filepath):
with open(filepath, 'r') as fp:
lines = fp.readlines()
self._setTextFileVersion(lines[0])
for line in lines:
if self._isHeaderLine(line):
self._extractHeaderLineData(line)
elif self._isDataLabelLine(line):
self._extractDataLabelLineData(line)
elif self._isValueLine(line):
self._extractValueLineData(line)
[docs]
def _setTextFileVersion(self, line):
if line.startswith(HEADER_ELEMENT_LINE):
self.version = HEADER_ELEMENT_LINE
self._extractHeaderLineData = self._extractHeaderLineDataElementLine
self._extractDataLabelLineData = self._extractDataLabelLineDataElementLine
elif line.startswith(HEADER_ELEMENT):
self.version = HEADER_ELEMENT
self._extractHeaderLineData = self._extractHeaderLineDataElement
self._extractDataLabelLineData = self._extractDataLabelLineDataElement
elif line.startswith(HEADER_ALL):
self.version = HEADER_ALL
self._extractHeaderLineData = self._extractHeaderLineDataElement
self._extractDataLabelLineData = self._extractDataLabelLineDataElement
[docs]
def _isDataLabelLine(self, line):
return line.startswith(DISTANCE_nm[:-5])
[docs]
def _isValueLine(self, line):
try:
dummy = float(line.split('\t')[0])
return True
except:
return False
[docs]
def getDataLabels(self):
return self._labels
[docs]
def getData(self, elementSymbol, line):
return self._data[elementSymbol][line]