forked from polarities/spc2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspc2csv.py
43 lines (34 loc) · 1.24 KB
/
spc2csv.py
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
import hyperspy.api as hs
import csv
import numpy as np
import os
class spc2csv():
def convert_spc2csv(spectrum):
"""
:return: save converted csv file with same directory of original spc file.
"""
s = hs.load(spectrum)
total_dataPoints = s.original_metadata.spc_header.numPts
step = s.original_metadata.spc_header.evPerChan
iteration = np.arange(0, (total_dataPoints - 1))
output_path = spectrum.replace('.spc', '.csv')
with open(output_path, 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(["Energy", "Intensity"])
for i in iteration:
data = (i * step, s.data[i])
writer.writerow(data)
def get_spcFile(directory):
"""
:return: set list that contains 'name' of every .spc file in specified directory.
"""
list = []
for file in os.listdir(directory):
if file.endswith(".spc"):
list.append(os.path.join(file))
return list
if __name__ == "__main__":
directory = './spcfiles'
spc_list = spc2csv.get_spcFile(directory)
for i in spc_list:
spc2csv.convert_spc2csv(str(directory) + '/' + str(i))