diff --git a/OPUS_TR_example.py b/OPUS_TR_example.py new file mode 100644 index 0000000..200b898 --- /dev/null +++ b/OPUS_TR_example.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 + +#This requires python 3, and brukeropusreader, which can be installed with pip3 install brukeropusreader (or sometimes just pip install...) +# but to be sure it's up to date, get it from https://github.com/qedsoftware/brukeropusreader + + +from brukeropusreader import read_file +from brukeropusreader import parse_sm + +import matplotlib.pyplot as plt +import numpy as np +import glob +import sys +import os + +plot=False + + + +def ProcessFile(filename): + opus_data = read_file(filename) + SC_X=opus_data.get_range("ScSm") + IG_X=opus_data.get_range("IgSm") + ScSm=parse_sm(opus_data) + IgSm=parse_sm(opus_data,"IgSm") + format_Ig=["%d"] + format_Ig.extend(["%.8e"]*np.shape(IgSm)[1]) + format_Sc="%.8e" + print (format_Ig) + print(format_Sc) + print (ScSm) + + np.savetxt(filename+"IG.txt",np.c_[IG_X,IgSm], fmt=format_Ig) + np.savetxt(filename+"SC.txt",np.c_[SC_X,ScSm], fmt=format_Sc) + + if plot: + plt.plot(SC_X,ScSm[:,0]) + plt.show() + plt.plot(IG_X,IgSm[:,0]) + plt.show() + +for arg in sys.argv: + if arg.lower().find('plot')>=0: + plot=True + elif len(arg.lower())>=1 : + InputFileName=arg +print (InputFileName) +if InputFileName.find("*")>=0 or InputFileName.find("?")>=0:#called with wildcards + InputFileName=os.path.join(os.getcwd(),InputFileName) + for InputFile in glob.glob(InputFileName): + print ("Globbed input: loading file "+InputFile) + + ProcessFile(InputFile) +else: + InputFile=InputFileName + print ("Loading file "+InputFile) + ProcessFile(InputFile) \ No newline at end of file diff --git a/brukeropusreader/__init__.py b/brukeropusreader/__init__.py index 865e158..df16962 100644 --- a/brukeropusreader/__init__.py +++ b/brukeropusreader/__init__.py @@ -1,7 +1,7 @@ -from .opus_parser import read_file +from .opus_parser import read_file,parse_sm from .opus_data import OpusData name = "brukeropusreader" -__all__ = ["read_file", "OpusData"] +__all__ = ["read_file", "OpusData", "parse_sm"] __version__ = "1.3" diff --git a/brukeropusreader/__init__.py~ b/brukeropusreader/__init__.py~ new file mode 100644 index 0000000..865e158 --- /dev/null +++ b/brukeropusreader/__init__.py~ @@ -0,0 +1,7 @@ +from .opus_parser import read_file +from .opus_data import OpusData + +name = "brukeropusreader" + +__all__ = ["read_file", "OpusData"] +__version__ = "1.3" diff --git a/brukeropusreader/opus_parser.py b/brukeropusreader/opus_parser.py index ce81644..8a338f7 100644 --- a/brukeropusreader/opus_parser.py +++ b/brukeropusreader/opus_parser.py @@ -14,6 +14,7 @@ read_chunk_size, read_offset, ) +import numpy as np def read_file(file_path: str) -> OpusData: @@ -63,3 +64,24 @@ def parse_data(data: bytes, blocks_meta: List[BlockMeta]) -> OpusData: parsed_data = parser(data, block_meta) opus_data[name] = parsed_data return opus_data + +def parse_sm(data_struct, data_type="ScSm"): + # Time-resolved data (interferogram goes in IgSm, spectrum in ScSm) + # unless only one time slice, when it can be handled as normal data, + # has some lines of junk in there. The magic numbers below are consistent + # across all tests by ChrisHodgesUK + junk_lines_at_start=8 + junk_lines_between_spectra=38 + WAS=data_struct["Acquisition"]["WAS"]#number of timeslices + NPT=data_struct[f"{data_type} Data Parameter"]["NPT"]# points per timeslice + raw_Sm=data_struct[data_type]#grab the data + if WAS==1: + Sm=opus_data[data_type][0:NPT] + else: + Sm=np.zeros((NPT,WAS)) + for timeslice in range(WAS): #reshape the array, discarding junk + start=junk_lines_at_start+timeslice*(NPT+junk_lines_between_spectra) + Sm[:,timeslice]=raw_Sm[start:start+NPT] + + + return Sm diff --git a/brukeropusreader/opus_parser.py~ b/brukeropusreader/opus_parser.py~ new file mode 100644 index 0000000..ce81644 --- /dev/null +++ b/brukeropusreader/opus_parser.py~ @@ -0,0 +1,65 @@ +from typing import List + +from brukeropusreader.block_data import BlockMeta, UnknownBlockType +from brukeropusreader.constants import ( + HEADER_LEN, + FIRST_CURSOR_POSITION, + META_BLOCK_SIZE, +) +from brukeropusreader.opus_data import OpusData +from brukeropusreader.opus_reader import ( + read_data_type, + read_channel_type, + read_text_type, + read_chunk_size, + read_offset, +) + + +def read_file(file_path: str) -> OpusData: + with open(file_path, "rb") as opus_file: + data = opus_file.read() + meta_data = parse_meta(data) + opus_data = parse_data(data, meta_data) + return opus_data + + +def parse_meta(data: bytes) -> List[BlockMeta]: + header = data[:HEADER_LEN] + spectra_meta = [] + cursor = FIRST_CURSOR_POSITION + while True: + if cursor + META_BLOCK_SIZE > HEADER_LEN: + break + + data_type = read_data_type(header, cursor) + channel_type = read_channel_type(header, cursor) + text_type = read_text_type(header, cursor) + chunk_size = read_chunk_size(header, cursor) + offset = read_offset(header, cursor) + + if offset <= 0: + break + + block_meta = BlockMeta(data_type, channel_type, + text_type, chunk_size, offset) + + spectra_meta.append(block_meta) + + next_offset = offset + 4 * chunk_size + if next_offset >= len(data): + break + cursor += META_BLOCK_SIZE + return spectra_meta + + +def parse_data(data: bytes, blocks_meta: List[BlockMeta]) -> OpusData: + opus_data = OpusData() + for block_meta in blocks_meta: + try: + name, parser = block_meta.get_name_and_parser() + except UnknownBlockType: + continue + parsed_data = parser(data, block_meta) + opus_data[name] = parsed_data + return opus_data diff --git a/brukeropusreader/opus_reader.py b/brukeropusreader/opus_reader.py index 81d4e0f..88248cc 100644 --- a/brukeropusreader/opus_reader.py +++ b/brukeropusreader/opus_reader.py @@ -1,4 +1,5 @@ from struct import unpack +import numpy as np from brukeropusreader.constants import UNSIGNED_INT, UNSIGNED_CHAR