Skip to content

Instantly share code, notes, and snippets.

@aakashjog
Last active March 3, 2021 18:53
Show Gist options
  • Save aakashjog/62d63cf8db0448f1141ab834c22d81ef to your computer and use it in GitHub Desktop.
Save aakashjog/62d63cf8db0448f1141ab834c22d81ef to your computer and use it in GitHub Desktop.

Installation

Download the scripts by running

git clone https://gist.github.com/aakashjog/62d63cf8db0448f1141ab834c22d81ef .

and make the files executable by running

chmod u+x ./process_all_files.sh

and

chmod u+x ./process_csv.py

Usage

The script should be executed as

./process_all_files.sh <technique>

with <technique> replaced by CV, chrono, EIS, biCV, or OCP.

It is not required to execute process_csv.py.

Functionality

The script will convert all files in the current folder with the extension .mpt to .csv files with the appropriate data columns. All files to be processed must be data files for the same electrchemical technique. For example, you cannot process 2 OCP files and 3 EIS files in the same folder. If you need to do such processing, make multiple subfolders with each subfolder having files for a single technique.

#!/usr/bin/env bash
git pull origin master
chmod u+x *.sh
for filename in ./*.mpt; do
./process_csv.py $filename $1
done
#!/usr/bin/env python3
# vim: autoindent noexpandtab tabstop=8 shiftwidth=8
import csv
import fileinput
import os
import sys
import re
filename = sys.argv[1]
technique = sys.argv[2]
base_filename = filename[:-4]
INPUT_FILENAME = base_filename + ".mpt"
OUTPUT_FILENAME = base_filename + ".csv"
with open(INPUT_FILENAME, 'rb') as input_file:
first_line = input_file.readline().decode("utf-8")
assert("EC-Lab ASCII FILE" in first_line)
second_line = input_file.readline().decode("utf-8")
number_of_header_lines = int(re.search("\d+", second_line)[0])
if technique == "chrono":
os.system("tail -n +{} {} | cut -f 8,10,11 > {}".format(number_of_header_lines, INPUT_FILENAME, OUTPUT_FILENAME))
elif technique == "CV":
os.system("tail -n +{} {} | cut -f 8,9,10 > {}".format(number_of_header_lines, INPUT_FILENAME, OUTPUT_FILENAME))
elif technique == "EIS":
os.system("tail -n +{} {} | cut -f 1,4,5 > {}".format(number_of_header_lines, INPUT_FILENAME, OUTPUT_FILENAME))
elif technique == "OCP":
os.system("tail -n +{} {} | cut -f 3,4 > {}".format(number_of_header_lines, INPUT_FILENAME, OUTPUT_FILENAME))
elif technique == "biCV":
os.system("tail -n +{} {} | cut -f 8,9,10,13,14 > {}".format(number_of_header_lines, INPUT_FILENAME, OUTPUT_FILENAME))
for line in fileinput.input(OUTPUT_FILENAME, inplace = True):
print(line.replace('\t', ','), end = "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment