2021-02-16 Python 导入实验数据,一键求解面积比活性-Pd/PdO

# /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Liye Zhu
Date: 2021.02.16   21:42
Description: the input text is exported from chi760e with a select circle of CV, 6th circle(2 segments!),
etc.How to start: Open the experiment raw data with chi760e, select 【File】->【Text file format】->
select starting and ending segments, and the export the data. We need to prepare two txt, the first input
txt is CV in KOH, it gives a plot of CV in KOH, after close the figure window, the second input will be
initiate, we should select the txt data file for CV in KOH+Ethanol. ect. And this code is based on the
original code designed on 2020.10.13 23:31. Using Hg/HgO electrode as RE, and Pd based metals as active
components.
Copyrights : https://blog.csdn.net/you_us/article/details/113828667
本代码为原创,转载请注明出处。
Principle: For the calculation of  charge, formula <Q =Σi*dt = Σi*dE/(50mV/s)> is used. For chi760E,
sample interval is set to be 1 mV on Parameters panel  which is a self-defined figure satisfying Nyquist
Sampling Theorem.
"""

from tkinter.filedialog import askopenfilename
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np
import xlwt
# I. Acquire data path.
path_and_name = askopenfilename(title='Select a data for Surface Area', filetypes=[('TXT', '*.txt')],
                              initialdir='D:\\')
cv_in_koh = []
# II. Create txt,read data, data lines:In CHI760E txt format, first data appears at line 31.(or line 33)
with open(path_and_name)as txt_file:
    line = txt_file.readlines()
    for i, rows in enumerate(line):
        if i in range(33, len(line) - 1):
            cv_in_koh.append(rows)
# Output raw data to  dat file for  Origin plot.
file_output = path_and_name[0:path_and_name.rfind('.', 1) + 1] + 'dat'
E0 = np.zeros(len(cv_in_koh))
I0 = np.zeros(len(cv_in_koh))
with open(file_output, "w", ) as f:
    for i in range((len(cv_in_koh))):
        f.writelines(cv_in_koh[i])
        [variable1, variable2] = cv_in_koh[i].split(',', 1)
        E0[i] = float(variable1)
        I0[i] = float(variable2)
f.close()

# Initiate processing: give a plot of data first.
# using matrix method instead of 【for】 command to raise efficiency.
# Select the range of PdO reduction peak.
Epdo = E0[np.where((E0 < 0) & (E0 > -0.4) & (I0 < 0))]
Ipdo= I0[np.where((E0 < 0) & (E0 > -0.4) & (I0 < 0))]

# Determine background current: In PdO/Pd region [-0.4,0]versus Hg/HgO, use interpolation algorithm.
# a index of e1, b index of e2
e1 = np.where(Epdo == min(Epdo))
e2 = np.where(Epdo == max(Epdo))
for i in e1:
    a = list(i)[0].tolist()
for i in e2:
    b = list(i)[0].tolist()
print(a,b)


# negative sweet: E1(a)= -0.2-- min    E1(b)= -0.001-- max
# Note : TypeError: 'numpy.ndarray' object is not callable---if  print(E1(199)) print(E1[199]) is
# correct.
# f = interpolate.interp1d(x, y, kind='linear')
E_termipoint = [min(Epdo), max(Epdo)]
I_termipoint = [Ipdo[a], Ipdo[b]]
#  Get length of E1, determine insert numbers n.
if a > b:
    n = a
else:
    n = b
n = n + 1
f = interpolate.interp1d(E_termipoint, I_termipoint, kind='linear')
Eb = np.linspace(max(Epdo), min(Epdo), n)
Ib = f(Eb)
fig1 = plt.figure(1, figsize=(16, 10))
plt.plot(Epdo, Ipdo, '.', color='pink', label='Raw Data')
plt.plot(Eb, Ib, '.', color='gray', label='Linear Background')

plt.grid(True)
plt.xlabel('E vs Hg/HgO')
plt.ylabel('Current/A')
plt.xlim(-0.4, 0)
plt.legend()
plt.title('Area in Ar saturated KOH')
plt.show()

# Q(unit is C) = ΣI(t)dt
# For 50mv/s, dE=1mV,then dt=1/50=0.02s,I is current derived from CHI760E and its unit is A.
# Q=sum(I2) * 1 / 50 (C) = sum(I2) * 1 / 50*1000 (mC); S= sum(I2) * 1 / 50*1000/0.424 (cm^2)
# 0.424 (mC/cm^2) for reduction of PdO mono-layer.
I = Ipdo - Ib
S = -1000 * sum(I) * (1 / 50) / 0.424
print(S)

# Retract CV current in KOH+ Ethanol
path_and_name1 = askopenfilename(title='Select a data for CV', filetypes=[('TXT', '*.txt')],
                                 initialdir='D:\\')
data_CV = []
with open(path_and_name1)as txt_file:
    line = txt_file.readlines()
    for i, rows in enumerate(line):
        if i in range(30, len(line)):
            data_CV.append(rows)
print("length", len(data_CV))

# Copy CV data to another dat file(file_output), and it can be processed by Origin software.
file_output1 = path_and_name1[0:path_and_name1.rfind('.', 1) + 1] + 'dat'
Eeor = np.zeros(len(data_CV))
Ieor = np.zeros(len(data_CV))
with open(file_output1, "w", ) as f:
    for i in range((len(data_CV))):
        f.writelines(data_CV[i])
        [variable1, variable2] = data_CV[i].split(',', 1)
        Eeor[i] = float(variable1)
        Ieor[i] = float(variable2)
f.close()
I_eor = 1000 * Ieor
Specific_activity = I_eor / S
fig2 = plt.figure(2, figsize=(16, 10))
plt.plot(Eeor, Specific_activity, '.', color='pink', label='Specific_activity')
plt.grid(True)
plt.xlabel('E vs Hg/HgO')
plt.ylabel('Specific Activity/mA·$\mathregular{cm^-2}$')
plt.xlim(-1.1, 0.5)
plt.legend()
plt.title('Specific Activity in Ethanol and KOH')
plt.show()

# output SA data to file
workbook11 = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet',cell_overwrite_ok=True)
for ii in range(len(Specific_activity)):
    worksheet.write(ii, 0, Eeor[ii])
    worksheet.write(ii, 1, Specific_activity[ii] )
file_output_SA = path_and_name1[0:path_and_name1.rfind('.', 1) - 1] + 'Specific-activity.xls'
workbook11.save(file_output_SA)

猜你喜欢

转载自blog.csdn.net/you_us/article/details/113828667