From 7965a14800298bc030bcddd86209e35f11844932 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Wed, 9 Oct 2024 23:37:09 +0300 Subject: [PATCH] make spg2csv an entry point --- scripts/spg2csv.py | 41 ----------------------- setup.py | 5 +++ src/ramanchada2/standalone/spg2csv.py | 47 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 41 deletions(-) delete mode 100755 scripts/spg2csv.py create mode 100755 src/ramanchada2/standalone/spg2csv.py diff --git a/scripts/spg2csv.py b/scripts/spg2csv.py deleted file mode 100755 index 6e6799bd..00000000 --- a/scripts/spg2csv.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/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 - -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)) diff --git a/setup.py b/setup.py index 79b2ae8b..5f05d348 100644 --- a/setup.py +++ b/setup.py @@ -71,6 +71,10 @@ 'Topic :: Software Development :: Libraries', ] +CONSOLE_SCRIPTS = [ + 'spg2csv = ramanchada2.standalone.spg2csv:spg2csv', +] + def setup_package(): setup( @@ -91,6 +95,7 @@ def setup_package(): packages=PACKAGES, python_requires=PYTHON_REQUIRES, url=URL, + entry_points=dict(console_scripts=CONSOLE_SCRIPTS), version=VERSION, ) diff --git a/src/ramanchada2/standalone/spg2csv.py b/src/ramanchada2/standalone/spg2csv.py new file mode 100755 index 00000000..d9aafa50 --- /dev/null +++ b/src/ramanchada2/standalone/spg2csv.py @@ -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()