-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.0
- Loading branch information
0 parents
commit d17038e
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |