Commit 4dd4b680 authored by Ulrich Andreas Mortensen's avatar Ulrich Andreas Mortensen
Browse files

Added Functions to read sta.txt files directly into pandas dataframe

parent 01c01090
......@@ -245,4 +245,103 @@ def BuildTable(TableHeading,SpecNames,C1=1,C2=1,C3=1,C4=1,C5=1,C6=1,C7=1):
# the_table.scale(1.1, 1.5)
plt.plot()
plt.savefig('TableOut.png')
plt.show()
\ No newline at end of file
plt.show()
def ReadStaTxtFile(fileName):
"""
This function reads full sta.txt files and returns a data-header (list of strings), a data description (dictionary) and the data columns (numpy array).
:param fileName: The file name/location on to be read
:type fileName: String
:return: Data header
:rtype: List of strings
:return: Description, the file information used to describe the DSC run
:rtype: Dictionary
:return: Data
:rtype: Numpy array
"""
# list container for the data and flag for when header is passed
valueList = []
flagStart = 0
Description = dict()
# Open the file
with open(fileName, 'r',errors='replace') as file:
# Read all lines
lines = file.readlines()
for i,line in enumerate(lines):
if "Time [s]" in line:
flagStart = i
header = line.split("]")
break
# Try to get the descriptions. There might not be any, which is why this is placed under a try-catch statement
try:
# Get the description lines
DescriptionStrings = [line for i,line in enumerate(lines) if i < flagStart]
# Retrieve the header line (always the last line before the data)
#header = DescriptionStrings.pop(-1)
# Create dictionary from description
for D in DescriptionStrings:
#print(DescriptionStrings)
#print(D.split(':')[0].strip())
#Description[D.rstrip().split(':')[0]] = D.rstrip().split(':')[1]
if len(D.strip()) > 0:
Description[D.split(':')[0].strip()] = D.split(':')[1].strip()
except:
print("Failed during reading of the description")
stringLines = [line.split('\t') for i,line in enumerate(lines) if i > flagStart]# not in line and len(line.rstrip())>0]
valueList = [[float(x) for x in stringList if len(x) > 0] for stringList in stringLines]
#floatRow = [float(x) for x in stringLines]
#stringLine = line.split(';')
#floatRow = [float(x) for x in stringLine]
return header,Description,np.array(valueList)
def StaTxtFileToPandas(fileName):
header,Description,Data = ReadStaTxtFile(fileName)
panData = pd.DataFrame()
unitsDict = dict()
for i,head in enumerate(header):
try:
item,unit = tuple(head.strip().split("["))
print("Item '{}' has unit(s) {}".format(item,unit))
item2 = ''.join(item.split())
panData[item2] = Data[:,i]
unitsDict[item] = unit
except:
if len(head.strip()) > 0:
print("Failed to unpack {}".format(head))
else:
pass
return panData,unitsDict,Description
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment