Skip to content

Commit

Permalink
Merge pull request #187 from h2020charisma/ssl_csv_converter
Browse files Browse the repository at this point in the history
ADD ssl_csv_converter as a standalone executable
  • Loading branch information
georgievgeorgi authored Nov 8, 2024
2 parents 87ad0c6 + 9083bab commit 7c8c5ff
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@

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


Expand Down
7 changes: 5 additions & 2 deletions src/ramanchada2/spectrum/creators/from_local_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ramanchada2.misc.types import SpeMetadataModel

from ..spectrum import Spectrum
from .from_chada import from_chada


@add_spectrum_constructor()
Expand All @@ -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.
Expand All @@ -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'}:
Expand Down
96 changes: 96 additions & 0 deletions src/ramanchada2/standalone/ssl_csv_converter.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7c8c5ff

Please sign in to comment.