Skip to content

Commit

Permalink
Release open source code
Browse files Browse the repository at this point in the history
v1.0
  • Loading branch information
byBenPuls authored Aug 6, 2023
0 parents commit d17038e
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions spectrum-converter 1.0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import xml.etree.ElementTree as ET
import sys, os, csv
from datetime import datetime
def XMLtoTXT (filename):
print("Начато конвертирование...")
tree = ET.parse(filename)
#import data from xml
DT = tree.find('ResultDataList/ResultData/SampleInfo/Time').text
VPC = tree.find('ResultDataList/ResultData/EnergySpectrum/ValidPulseCount').text
TPC = tree.find('ResultDataList/ResultData/EnergySpectrum/TotalPulseCount').text
MT = tree.find('ResultDataList/ResultData/EnergySpectrum/MeasurementTime').text

N = 0

#get datapoints from xml

DP = list('')
for elem in tree.iterfind('ResultDataList/ResultData/EnergySpectrum/Spectrum/DataPoint'):
N+=1
DP.append(elem.text)

#manage Date, 0 - year, 1 - month, 2 - day
DateList = DT.split('-')
TimeList = DateList[2].split('T')
DateList[2] = TimeList[0]

#manage Time
del TimeList[0]
TimeList = TimeList[0].split('.')
del TimeList[1]

#Compute LiveTime
LT = float(VPC)/float(TPC)*float(MT)

#manage out file
NewFileName = os.path.splitext(filename)
NewFileName = NewFileName[0] + ".txt"

f = open (NewFileName, "w+")
f.write("DATE={}-{}-{}\n".format(DateList[2],DateList[1],DateList[0]))
f.write("TIME={}\n".format(TimeList[0]))
f.write("TLIVE={:.2f}\n".format(LT))
f.write("TREAL={:.2f}\n".format(float(MT)))
f.write("SPECTRTXT={}\n".format(N))

for i in range(N):
f.write(str(i+1)+'\t'+str(DP[i])+'\n')
f.close()
print("Конвертирование завершено!")
input('Нажмите на любую клавишу для продолжения..')
def CSVtoTXT (filename, LT):
print("Начато конвертирование...")
#open and read csv
csvfile = open(filename, 'r')
csvdata = csv.reader(csvfile, delimiter=',')

#get spectra data
CHN = list()
IMP = list()

for row in csvdata:
CHN.append(row[0])
IMP.append(row[1])

N = len(CHN)

#get time and date
created = os.stat(filename).st_ctime
DT = str(datetime.fromtimestamp(created))

#manage Date, 0 - year, 1 - month, 2 - day
DateList = DT.split('-')
TimeList = DateList[2].split(' ')
DateList[2] = TimeList[0]

#manage Time
del TimeList[0]
TimeList = TimeList[0].split('.')
del TimeList[1]

#manage out file
NewFileName = os.path.splitext(filename)
NewFileName = NewFileName[0] + ".txt"
f = open (NewFileName, "w+")

f.write("DATE={}-{}-{}\n".format(DateList[2],DateList[1],DateList[0]))
f.write("TIME={}\n".format(TimeList[0]))
f.write("TLIVE={:}\n".format(str(LT)))
f.write("TREAL={:}\n".format(str(LT)))
f.write("SPECTRTXT={}\n".format(N))

for i in range(N):
f.write(str(i+1)+'\t'+str(int(IMP[i]))+'\n')

f.close()
print("Конвертирование завершено!")
input('Нажмите на любую клавишу для продолжения..')
#main
print("Author: Ben Puls")
if len(sys.argv) == 1:
path = os.getcwd()
else:
path = sys.argv[1]

for file in os.listdir(path):
if file.endswith(".xml"):
XMLtoTXT(os.path.join(path, file))
if file.endswith(".csv"):
LiveTime = input("Укажите время в секундах набора спектра: ".format(file))
CSVtoTXT(os.path.join(path, file), LiveTime)

0 comments on commit d17038e

Please sign in to comment.