Skip to content

Commit

Permalink
Merge pull request #169 from h2020charisma/spg_spa_converter
Browse files Browse the repository at this point in the history
Add convertion script for Thermo Nicolet spa and spg files
  • Loading branch information
georgievgeorgi authored Oct 9, 2024
2 parents d83b866 + 7965a14 commit 914bc5c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
'Topic :: Software Development :: Libraries',
]

CONSOLE_SCRIPTS = [
'spg2csv = ramanchada2.standalone.spg2csv:spg2csv',
]


def setup_package():
setup(
Expand All @@ -91,6 +95,7 @@ def setup_package():
packages=PACKAGES,
python_requires=PYTHON_REQUIRES,
url=URL,
entry_points=dict(console_scripts=CONSOLE_SCRIPTS),
version=VERSION,
)

Expand Down
47 changes: 47 additions & 0 deletions src/ramanchada2/standalone/spg2csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

import argparse
import csv
import os

try:
from tqdm import trange as range
except ModuleNotFoundError:
pass

from spectrochempy.core.readers.read_omnic import read_spg


def spg2csv():
parser = argparse.ArgumentParser(description='Convert SPA/SPG file format.')
parser.add_argument('-i', '--input', type=str, required=True, help='Path to SPA or SPG file')
parser.add_argument('-o', '--output', type=str,
help="Output file names in formated string like "
"'/path/to/file-{y_label_name}.csv'. "
"Possible options are: {y_label_date} {y_label_name} {y_coordinate} "
"{input_basename} {input_extension}"
"(default: '{input_basename}-{y_label_name}.csv')")
args = parser.parse_args()
out_file_format = args.output or f'{args.input}-{{y_label_name}}.csv'

input_basename, input_extension = os.path.splitext(args.input)

spe = read_spg(args.input)

x = spe.coordset['x'].data
for i in range(spe.coordset['y'].size):
(y_label_date, y_label_name), y_coordinate = spe.coordset['y'].labels[i], spe.coordset['y'].data[i]
y_label_name = str(y_label_name).replace('/', '_')
out_file_name = out_file_format.format(basename=input_basename,
y_label_date=y_label_date,
y_label_name=y_label_name,
y_coordinate=y_coordinate)
y = spe.data[i]
print(out_file_name)
with open(out_file_name, 'w') as fp:
csv_writer = csv.writer(fp)
csv_writer.writerows(zip(x, y))


if __name__ == '__main__':
spg2csv()

0 comments on commit 914bc5c

Please sign in to comment.