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

FIX BlockMeta repeat #20 ? #21

Open
wants to merge 4 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
21 changes: 18 additions & 3 deletions brukeropusreader/block_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,30 @@ class UnknownBlockType(Exception):
},
)

BLOCK_7 = {4: "ScSm", 8: "IgSm", 12: "PhSm"}
# other sample spectral data (single channel, interferogram, power spectrum, ...)
BLOCK_7 = {4: "ScSm", 8: "IgSm", 12: "PhSm", 56: "PwSm", 132: "ScSm_(1)", 136: "IgSm_(1)"}

BLOCK_11 = {4: "ScRf", 8: "IgRf"}
# other reference spectral data (single channel, interferogram, power spectrum, ...)
BLOCK_11 = {4: "ScRf", 8: "IgRf", 56: "PwRf", 132: "ScRf_(1)", 136: "IgRf_(1)"}

# parameters of other sample spectral data
BLOCK_23 = {
4: "ScSm Data Parameter",
8: "IgSm Data Parameter",
12: "PhSm Data Parameter",
56: "PwSm Data Parameter",
132: "ScSm_(1) Data Parameter",
136: "IgSm_(1) Data Parameter",
}

BLOCK_27 = {4: "ScRf Data Parameter", 8: "IgRf Data Parameter"}
# parameters of other reference spectral data
BLOCK_27 = {
4: "ScRf Data Parameter",
8: "IgRf Data Parameter",
56: "PwRf Data Parameter",
132: "ScRf_(1) Data Parameter",
136: "IgRf_(1) Data Parameter",
}

DIFFERENT_BLOCKS = {
31: "AB Data Parameter",
Expand All @@ -51,12 +64,14 @@ def __init__(
data_type: int,
channel_type: int,
text_type: int,
additional_type: int,
chunk_size: int,
offset: int,
):
self.data_type = data_type
self.channel_type = channel_type
self.text_type = text_type
self.additional_type = additional_type
self.chunk_size = chunk_size
self.offset = offset

Expand Down
6 changes: 5 additions & 1 deletion brukeropusreader/block_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
NULL_BYTE,
ENCODING_LATIN,
ENCODING_UTF,
NULL_STR)
NULL_STR,
)
from brukeropusreader.opus_reader import read_chunk
from struct import unpack, error
import numpy as np


def parse_param(data: bytes, block_meta):
"""parse data blocks of parameters

BLOCK_23, BLOCK_27"""
cursor = 0
chunk = read_chunk(data, block_meta)
params = {}
Expand Down
23 changes: 21 additions & 2 deletions brukeropusreader/opus_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
read_data_type,
read_channel_type,
read_text_type,
read_additional_type,
read_chunk_size,
read_offset,
)
Expand All @@ -25,6 +26,15 @@ def read_file(file_path: str) -> OpusData:


def parse_meta(data: bytes) -> List[BlockMeta]:
"""Parse the header of the opus file.

Returns a list of metadata (BlockMeta) for each block to be read,

:parameter:
data: bytes content of the opus file
:returns:
parse_meta: list of BlockMeta
"""
header = data[:HEADER_LEN]
spectra_meta = []
cursor = FIRST_CURSOR_POSITION
Expand All @@ -35,14 +45,14 @@ def parse_meta(data: bytes) -> List[BlockMeta]:
data_type = read_data_type(header, cursor)
channel_type = read_channel_type(header, cursor)
text_type = read_text_type(header, cursor)
additional_type = read_additional_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)
block_meta = BlockMeta(data_type, channel_type, text_type, additional_type, chunk_size, offset)

spectra_meta.append(block_meta)

Expand All @@ -54,12 +64,21 @@ def parse_meta(data: bytes) -> List[BlockMeta]:


def parse_data(data: bytes, blocks_meta: List[BlockMeta]) -> OpusData:
"""parse the data of the opus file using the file header's informations
parame"""
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)
# in some instances, multiple entries - in particular 'AB' are
# present. they are added with a key ending by '_(1)', '_(2)', etc...
if name in opus_data.keys():
i = 1
while name + '_(' + str(i) + ')' in opus_data.keys():
i += 1
name = name + '_(' + str(i) + ')'
opus_data[name] = parsed_data
return opus_data
6 changes: 6 additions & 0 deletions brukeropusreader/opus_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def read_text_type(header: bytes, cursor: int) -> int:
return unpack(UNSIGNED_CHAR, header[p1:p2])[0]


def read_additional_type(header: bytes, cursor: int) -> int:
p1 = cursor + 3
p2 = cursor + 4
return unpack(UNSIGNED_CHAR, header[p1:p2])[0]


def read_chunk_size(header: bytes, cursor: int) -> int:
p1 = cursor + 4
p2 = cursor + 8
Expand Down
52 changes: 52 additions & 0 deletions recipe/meta.yaml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% set name = "brukeropusreader" %}
{% set version = environ['VERSION'] %}

package:
name: "{{ name|lower }}"
version: "{{ version }}"

source:
path: ../

build:
script_env:
- VERSION
- CONDA_BLD_PATH
string: {{ environ['DEVSTRING'] }}
noarch: python
script: "{{ PYTHON }} -m pip install . -vv"

requirements:
build:
- python
host:
- python
- numpy
- pip
- scipy
run:
- python
- numpy
- scipy
test:
- python {{ python }}
- pytest

test:
script_env:
- VERSION
- CONDA_BLD_PATH
imports:
- brukeropusreader

about:
home: "https://github.com/spectrochempy/brukeropusreader"
license: GPLv3
license_family: GPL3
summary: "Bruker OPUS File Reader"
doc_url: "https://github.com/spectrochempy/brukeropusreader"
dev_url: "https://github.com/spectrochempy/brukeropusreader"

extra:
recipe-maintainers:
- fernandezc