From 9083babbca1715f694fd629c2eef466c2ff03835 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Fri, 8 Nov 2024 17:26:25 +0200 Subject: [PATCH] ADD ssl_csv_converter as a standalone executable --- setup.py | 1 + .../spectrum/creators/from_local_file.py | 7 +- .../standalone/ssl_csv_converter.py | 96 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100755 src/ramanchada2/standalone/ssl_csv_converter.py diff --git a/setup.py b/setup.py index 5f05d348..a0e62c13 100644 --- a/setup.py +++ b/setup.py @@ -73,6 +73,7 @@ CONSOLE_SCRIPTS = [ 'spg2csv = ramanchada2.standalone.spg2csv:spg2csv', + 'ssl_csv_converter = ramanchada2.standalone.ssl_csv_converter:ssl_csv_converter', ] diff --git a/src/ramanchada2/spectrum/creators/from_local_file.py b/src/ramanchada2/spectrum/creators/from_local_file.py index 7895c534..fdea1fd8 100644 --- a/src/ramanchada2/spectrum/creators/from_local_file.py +++ b/src/ramanchada2/spectrum/creators/from_local_file.py @@ -12,6 +12,7 @@ from ramanchada2.misc.types import SpeMetadataModel from ..spectrum import Spectrum +from .from_chada import from_chada @add_spectrum_constructor() @@ -20,7 +21,7 @@ def from_local_file( in_file_name: str, filetype: Union[None, Literal['spc', 'sp', 'spa', '0', '1', '2', 'wdf', 'ngs', 'jdx', 'dx', - 'txt', 'txtr', 'csv', 'prn', 'rruf', 'spe']] = None, + 'txt', 'txtr', 'csv', 'prn', 'rruf', 'spe', 'cha']] = None, backend: Union[None, Literal['native', 'rc1_parser']] = None): """ Read experimental spectrum from a local file. @@ -44,7 +45,9 @@ def load_native(): ft = os.path.splitext(in_file_name)[1][1:] else: ft = filetype - if ft in {'txt', 'txtr', 'prn', 'rruf'}: + if ft in {'cha'}: + return from_chada(filename=in_file_name) + elif ft in {'txt', 'txtr', 'prn', 'rruf'}: with open(in_file_name) as fp: x, y, meta = read_txt(fp) elif ft in {'csv'}: diff --git a/src/ramanchada2/standalone/ssl_csv_converter.py b/src/ramanchada2/standalone/ssl_csv_converter.py new file mode 100755 index 00000000..d8a9f1f3 --- /dev/null +++ b/src/ramanchada2/standalone/ssl_csv_converter.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +import argparse +import os + +import numpy as np + +import ramanchada2 as rc2 + + +def ssl_csv_to_spectra(filename): + with open(filename) as f: + lines = [i.strip() for i in f.readlines()] + assert lines[0].startswith('ssl-SA') + head_sep = '***** X axis & Intensity *****' + assert head_sep in lines + global_meta = {'format': lines[0], + 'datetime': lines[1], + } + header_sep_idx = lines.index(head_sep) + + global_meta.update(dict([[j.strip() for j in i.split('=')] for i in lines[2:header_sep_idx] if '=' in i])) + + k = 'Laser wavelength' + if k in global_meta.keys(): + v = global_meta[k] + if v.endswith(' nm'): + v = int(v[:-3]) + global_meta['laser wavelength [nm]'] = v + + k = 'Integ time' + if k in global_meta.keys(): + v = global_meta[k] + if v.endswith(' ms'): + v = int(v[:-3]) + global_meta['integration time [ms]'] = v + global_meta['intigration times(ms)'] = v + + basename = os.path.basename(filename) + global_meta['Original file'] = basename + + ccc = np.loadtxt(lines[header_sep_idx+1:], delimiter=',') + ccc = ccc.T + x = ccc[0] + spectra = [] + for y_i, y in enumerate(ccc[1:], 1): + meta = {'spectrum number': y_i, 'total spectra number': ccc.shape[1]} + meta.update(global_meta) + spectra.append(rc2.spectrum.Spectrum(x=x, y=y, metadata=meta)) + return spectra + + +def ssl_csv_converter(): + parser = argparse.ArgumentParser(description='Convert ssl csv file format.') + parser.add_argument('-i', '--input', type=str, required=True, help='Path to ssl csv file') + parser.add_argument('-t', '--format-to', type=str, required=False, + help='Output file format cha or csv', default='cha') + parser.add_argument('-o', '--output', type=str, + help="Output file names in formated string " + "Possible options are: {date} {succ_num} {all_spe_num} " + "{laser_wavelength} {integration_time} " + "{input_basename} {input_extension}" + "(default: '{input_basename}--{laser_wavelength}nm" + "-{integration_time}ms-{succ_num}-{all_spe_num}.cha')") + args = parser.parse_args() + out_fmt = args.format_to or 'cha' + out_file_format = ( + args.output or + f'{args.input}-{{laser_wavelength}}nm-{{integration_time}}ms-{{succ_num}}-{{all_spe_num}}.{out_fmt}' + ) + + input_basename, input_extension = os.path.splitext(args.input) + + spes = ssl_csv_to_spectra(args.input) + + for spe_i, spe in enumerate(spes, 1): + dct = dict(input_basename=input_basename, + input_extension=input_extension, + date=spe.meta['datetime'].strftime('%F'), + succ_num=spe_i, + all_spe_num=len(spes), + laser_wavelength=spe.meta['laser wavelength [nm]'], + integration_time=spe.meta['integration time [ms]'], + ) + + out_file_name = out_file_format.format(**dct) + if out_fmt == 'cha': + spe.write_cha(out_file_name, '/raw') + elif out_fmt == 'csv': + spe.write_csv(out_file_name) + else: + raise Exception('File format {out_fmt} is unknown') + + +if __name__ == '__main__': + ssl_csv_converter()