Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for time-resolved files #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions OPUS_TR_example.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions brukeropusreader/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions brukeropusreader/__init__.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .opus_parser import read_file
from .opus_data import OpusData

name = "brukeropusreader"

__all__ = ["read_file", "OpusData"]
__version__ = "1.3"
22 changes: 22 additions & 0 deletions brukeropusreader/opus_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
read_chunk_size,
read_offset,
)
import numpy as np


def read_file(file_path: str) -> OpusData:
Expand Down Expand Up @@ -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
65 changes: 65 additions & 0 deletions brukeropusreader/opus_parser.py~
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions brukeropusreader/opus_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from struct import unpack
import numpy as np

from brukeropusreader.constants import UNSIGNED_INT, UNSIGNED_CHAR

Expand Down