-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin_conversion.py
154 lines (137 loc) · 5.76 KB
/
origin_conversion.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import originpro as op
import pandas as pd
import numpy as np
import re
import argparse
import pathlib
import threading
import itertools, sys
import os
spinner = itertools.cycle(['-', '/', '|', '\\'])
print('CD ORIGIN PLOTTER \n Parses Applied Photophysics Chirascan output and plots key properties in an origin project. \n Louis Minion 2023')
def loadCDdata(filename):
with open(filename) as f:
g = f.readlines()
experimentinfo = {}
for i in range(0,33):
line = g[i]
if line == '\n':
# print('break')
continue
if ':' not in line:
continue
if 'Wavelength' in line:
w = line
ls = re.split(',', w)[1:]
for j in ls:
# print(j)
[title, value] = re.split(':', j)
experimentinfo[title] = value
continue
try:
[title, value] = re.split(':', line)
except ValueError:
continue
experimentinfo[title] = value
n= re.split('-', experimentinfo['Wavelength'])
for i,N in enumerate(n):
n[i]= float(N.strip()[:-2])
experimentinfo['StartWavelength'] = n[1]
experimentinfo['EndWavelength'] = n[0]
nws = int(experimentinfo['StartWavelength']-experimentinfo['EndWavelength']+1)
skiprparam = 37
CDdf = pd.read_csv(filename, skiprows=skiprparam, nrows=nws, header=None)
CDdf.columns =['Wavelength', 'CD']
HVdf = pd.read_csv(filename, skiprows=(skiprparam+3+int(nws)), nrows=nws,header=None)
HVdf.columns = ['Wavelength', 'HV']
Absdf = pd.read_csv(filename, skiprows=(skiprparam+6+2*int(nws)), nrows=nws,header=None)
Absdf.columns = ['Wavelength', 'Abs']
Voltagedf = pd.read_csv(filename, skiprows=(skiprparam+9+3*int(nws)), nrows=nws,header=None)
Voltagedf.columns = ['Wavelength', 'Voltage']
Countdf = pd.read_csv(filename, skiprows=(skiprparam+12+4*int(nws)), nrows=nws,header=None)
Countdf.columns = ['Wavelength', 'Count']
SEdf = pd.read_csv(filename, skiprows=(skiprparam+15+5*int(nws)), nrows=nws,header=None)
SEdf.columns = ['Wavelength', 'SE']
Tempdf = pd.read_csv(filename, skiprows=(skiprparam+18+6*int(nws)), nrows=nws,header=None)
Tempdf.columns = ['Wavelength', 'Temp']
result = {'experimentinfo':experimentinfo, 'CD':CDdf, 'HV':HVdf, 'Abs':Absdf, 'Voltage':Voltagedf, 'Count':Countdf, 'SE':SEdf, 'Temp':Tempdf}
return result
parser = argparse.ArgumentParser(
prog='Chirascan Origin Plotter',
description='Parses Applied Photophysics Chirascan output and plots key properties in an origin project.',
epilog='Louis Minion 2023')
parser.add_argument('filename', metavar='f', type=str,
help='File to be converted.')
args = parser.parse_args()
filename = args.filename
filenamepath = pathlib.Path(filename)
print('Will process file:{}'.format(filename))
def createOriginBook(filenamepath):
filename = str(filenamepath)
results = loadCDdata(filename)
book = op.new_book('w')
book.lname = 'Experimental Data:{}'.format(filenamepath.name)
# Get 1st sheet from book.
wks = book[0]
# Set sheet short name.
wks.name = 'Data'
wks.from_list(0,results['CD']['Wavelength'], axis='X', lname='Wavelength', units='nm')
wks.from_list(1,results['CD']['CD'], axis='Y', lname='CD', units='mdeg')
wks.from_list(2,results['Abs']['Abs'], axis='Y', lname='Abs', units='A.U.')
wks.from_list(3,results['HV']['HV'], axis='Y', lname='HV', units='V')
wks.from_list(4,results['SE']['SE'], axis='Y', lname='SE', units='arb.')
wks.from_list(5,results['Voltage']['Voltage'], axis='Y', lname='Voltage', units='V')
wks.from_list(6,results['Count']['Count'], axis='Y', lname='Count', units='arb.')
wks.from_list(7,results['Temp']['Temp'], axis='Y', lname='Temp', units='C')
gfactor = results['CD']['CD']/(32982*results['Abs']['Abs'])
wks.from_list(8,gfactor, axis='Y', lname='g')
graph = op.new_graph()
gl=graph[0]
# plot whole sheet as XY plot
plot = gl.add_plot(wks,colx=0, coly=1, type='l')
gl.rescale()
layr = gl
pindex = gl.index()
pname = layr.obj.GetStrProp(f'plot{pindex+1}.name')
layr.lt_exec(f'set {pname} -w 2500')
plot.color = '#ff0000'
absg = op.new_graph()
agl = absg[0]
aplot = agl.add_plot(wks, colx=0, coly=2, type='l')
agl.rescale()
layr = agl
pindex = agl.index()
pname = layr.obj.GetStrProp(f'plot{pindex+1}.name')
layr.lt_exec(f'set {pname} -w 2500')
aplot.color = '#ff0000'
ggr = op.new_graph()
ggrgl = ggr[0]
gplot = ggrgl.add_plot(wks, colx=0, coly=8, type='l')
ggrgl.rescale()
layr = ggrgl
pindex = ggrgl.index()
pname = layr.obj.GetStrProp(f'plot{pindex+1}.name')
layr.lt_exec(f'set {pname} -w 2500')
gplot.color = '#ff0000'
# Save the opju.
a = op.save('{}.opju'.format(str( pathlib.Path(os.getcwd()) / filenamepath.name)[:-4]))
print(' Saved at :{}.opju'.format(str( pathlib.Path(os.getcwd()) / filenamepath.name)[:-4]))
return False
# Ensures that the Origin instance gets shut down properly.
import sys
def origin_shutdown_exception_hook(exctype, value, traceback):
op.exit()
sys.__excepthook__(exctype, value, traceback)
if op and op.oext:
sys.excepthook = origin_shutdown_exception_hook
# Set Origin instance visibility.
if op.oext:
op.set_show(True)
t = threading.Thread(target=createOriginBook, args=[filenamepath])
t.run()
while t.is_alive():
sys.stdout.flush() # flush stdout buffer (actual character display)
sys.stdout.write('\b')
# Exit running instance of Origin.
if op.oext:
op.exit()