From: Pat Thoyts Date: Mon, 11 Mar 2024 07:59:54 +0000 (+0000) Subject: hack X-Git-Url: http://privyetmir.co.uk/gitweb?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=renishaw_spc.git hack --- 4bbc113bc46c19733b3f7536ff11149fad780d09 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7021e5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.tox/ +.vscode/ +*.egg-info/ +__pycache__/ +build/ +dist/ +.pytest_cache/ +*.whl \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..6bcf23b --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include tox.ini +include demos/*.py +include spc/test/*.py +include spc/test/*.spc diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/demos/gspc_test.py b/demos/gspc_test.py new file mode 100644 index 0000000..a64d3a7 --- /dev/null +++ b/demos/gspc_test.py @@ -0,0 +1,45 @@ +""" +Read an SPC file using the Galactic GSPCIO COM object. + +Will require 32 bit python as the COM object is only available for 32 bit. +""" + +import sys +import argparse +from comtypes.client import CreateObject + +PROGID_GSPCIO='GSPCIOLib.GSPCIO' + + +def main(args=None): + """Application entry""" + parser = argparse.ArgumentParser( + description='Read an SPC file using the Galactic GSPCIO COM library') + parser.add_argument('filename', help='Path to SPC file.') + parser.add_argument('--show-log', dest='showlogtext', action='store_true', + help='Emit the log text data lines.') + options = parser.parse_args(args) + + gspcio = CreateObject(PROGID_GSPCIO) + gspcio.OpenFile(options.filename) + print(f"File has {gspcio.NumSubfiles} spectra with {gspcio.NumPoints} points") + print(f'Comment: "{gspcio.Comment}"') + print(f'Description: "{gspcio.GetLogKey("Description")}"') + if options.showlogtext: + print(f"LogText: ({gspcio.LogTextSize} bytes)") + index = 0 + while True: + line, length = gspcio.GetLogLine(index) + if length < 0: + break + if line: + print(f' {line}') + else: + print(f'-- empty line {length} bytes reported') + index += 1 + + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a4654d2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools >= 65.0"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3edcf39 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,45 @@ +[metadata] +name = renishaw_spc +version = attr: spc.__version__ +description = Python access classes for Galactic SPC spectroscopy data files +long_description = file: README.md +author = Renishaw Spectroscopy +author_email = Raman.Support@renishaw.com +license = Apache Software License +platforms = any +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Science/Research + License :: OSI Approved :: Apache Software License + Operating System :: OS Independent + Programming Language :: Python :: 3 + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 + Topic :: Scientific/Engineering :: Physics + +[options] +python_requires = >=3.8 +packages = + spc + spc.browser + +[options.entry_points] +gui_scripts = + spcbrowser = spc.browser:main + +[options.extras_require] +tools = + matplotlib >= 3.5.0 + numpy >= 1.20.0 + +[options.packages.find] +exclude = + demos* + spc.test* + +[sdist] +formats=zip diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b908cbe --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +import setuptools + +setuptools.setup() diff --git a/spc/__init__.py b/spc/__init__.py new file mode 100644 index 0000000..6c09e9f --- /dev/null +++ b/spc/__init__.py @@ -0,0 +1,29 @@ +# Copyright (c) 2024 Renishaw plc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +"""Galactic SPC spectroscopy data file accessor classes""" + + +__title__ = "renishaw_spc" +__version__ = "1.0.0" +__author__ = "Renishaw plc" +__author_email__ = "Raman.Support@renishaw.com" +__license__ = "Apache Software License" + + +from .spcfile import ( + SpcFile, SpcHdr, SpcIter) +from .spcenums import ( + SpcFlags, SpcTechnique, SpcModFlags, SubHdrFlags) diff --git a/spc/spcenums.py b/spc/spcenums.py new file mode 100644 index 0000000..aaf3b84 --- /dev/null +++ b/spc/spcenums.py @@ -0,0 +1,128 @@ +# Copyright (c) 2024 Renishaw plc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from enum import IntEnum, IntFlag + + +class SpcFlags(IntFlag): + """Possible bit FTFLGS flag byte settings. + + Note that TRANDM and TORDRD are mutually exclusive. + Code depends on TXVALS being the sign bit. TXYXYS must be 0 if TXVALS=0. + In old software without the fexper code, TCGRAM specifies a chromatogram.""" + + TNONE = 0 + + TSPREC = 1 + """Single precision (16 bit) Y data if set.""" + + TCGRAM = 2 + """Enables fexper in older software (CGM if fexper=0)""" + + TMULTI = 4 + """Multiple traces format (set if more than one subfile)""" + + TRANDM = 8 + """If TMULTI and TRANDM=1 then arbitrary time (Z) values""" + + TORDRD = 16 + """If TMULTI abd TORDRD=1 then ordered but uneven subtimes""" + + TALABS = 32 + """Set if should use fcatxt axis labels, not fxtype etc.""" + + TXYXYS = 64 + """If TXVALS and multifile, then each subfile has own X's""" + + TXVALS = 128 + """Floating X value array preceeds Y's (New format only)""" + + +class SpcTechnique(IntEnum): + """Instrument Technique fexper settings + + In older software, the TCGRAM in ftflgs must be set if fexper is non-zero. + A general chromatogram is specified by a zero fexper when TCGRAM is set.""" + + SPCGEN = 0 + """General SPC (could be anything)""" + SPCGC = 1 + """Gas Chromatogram""" + SPCCGM = 2 + """General Chromatogram (same as SPCGEN with TCGRAM)""" + SPCHPLC = 3 + """HPLC Chromatogram""" + SPCFTIR = 4 + """FT-IR, FT-NIR, FT-Raman Spectrum or Igram (Can also be used for scanning IR.)""" + SPCNIR = 5 + """NIR Spectrum (Usually multi-spectral data sets for calibration.)""" + SPCUV = 7 + """UV-VIS Spectrum (Can be used for single scanning UV-VIS-NIR.)""" + SPCXRY = 8 + """X-ray Diffraction Spectrum""" + SPCMS = 9 + """Mass Spectrum (Can be single, GC-MS, Continuum, Centroid or TOF.)""" + SPCNMR = 10 + """NMR Spectrum or FID""" + SPCRMN = 11 + """Raman Spectrum (Usually Diode Array, CCD, etc. use SPCFTIR for FT-Raman.)""" + SPCFLR = 12 + """Fluorescence Spectrum""" + SPCATM = 13 + """Atomic Spectrum""" + SPCDAD = 14 + """Chromatography Diode Array Spectra""" + + +class SpcModFlags(IntFlag): + """Spectral modifications flag setting conventions""" + Averaging = 1 << 1 + """Averaging (from multiple source traces)""" + Baseline = 1 << 2 + """Baseline correction or offset functions""" + Conversion = 1 << 3 + """Interferogram to spectrum Computation""" + Derivative = 1 << 4 + """Derivative (or integrate) functions""" + Enhancement = 1 << 6 + """Resolution Enhancement functions (such as deconvolution)""" + Interpolation = 1 << 9 + """Interpolation functions""" + Noise = 1 << 14 + """Noise reduction smoothing""" + Other = 1 << 15 + """Other functions (add, subtract, noise, etc.)""" + Subtraction = 1 << 19 + """Spectral Subtraction""" + Truncation = 1 << 20 + """Truncation (only a portion of original X axis remains)""" + When = 1 << 23 + """When collected (date and time information) has been modified""" + Xunits = 1 << 24 + """X units conversions or X shifting""" + Yunits = 1 << 25 + """Y units conversions (transmission->absorbance, etc.)""" + Zap = 1 << 26 + """Zap functions (features removed or modified)""" + + +class SubHdrFlags(IntFlag): + SUBCHGD = 1 + """Subflgs bit if subfile changed""" + + SUBNOPT = 8 + """Subflgs bit if peak table file should not be used""" + + SUBMODF = 128 + """Subflgs bit if subfile modified by arithmetic""" diff --git a/spc/spcfile.py b/spc/spcfile.py new file mode 100644 index 0000000..1ca640b --- /dev/null +++ b/spc/spcfile.py @@ -0,0 +1,342 @@ +# Copyright (c) 2024 Renishaw plc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +""" + +import struct +from dataclasses import dataclass +from datetime import datetime +from enum import IntEnum, IntFlag +from io import BufferedReader, BytesIO +from os import SEEK_SET, SEEK_CUR +from typing import Sequence +from .spcenums import SpcFlags, SpcTechnique, SpcModFlags, SubHdrFlags + + +def from_spc_date(fdate: int) -> datetime: + """Convert an SPC file timestamp to a Python datetime + + Date/Time LSB: min=6b,hour=5b,day=5b,month=4b,year=12b""" + return fdate + print(hex(fdate), hex(fdate >> (32 - 12))) + year = ((fdate >> 25) & 0x3f) + 1980 + month = (fdate >> 21) & 0x0f + day = (fdate >> 16) & 0x0f + hour = (fdate >> 11) & 0x1f + mins = (fdate >> 5) & 0x3f + secs = (fdate) & 0x1f + return datetime(year, month, day, hour=hour, minute=mins, second=secs) + + +@dataclass +class SpcHdr: + ftflgs: SpcFlags + """File flags from the SpcFlags enumeration""" + fversn: int + """0x4B=> new LSB 1st, 0x4C=> new MSB 1st, 0x4D=> old format""" + fexper: int + """Instrument technique code""" + fexp: int + """Fraction scaling exponent integer (80h=>float)""" + fnpts: int + """Integer number of points (or TXYXYS directory position)""" + ffirst: float + """Floating X coordinate of first point""" + flast: float + """Floating X coordinate of last point""" + fnsub: int + """Integer number of subfiles (1 if not TMULTI)""" + fxtype: int + """Type of X axis units""" + fytype: int + """Type of Y axis units""" + fztype: int + """Type of Z axis units""" + fpost: int + """Posting disposition (see GRAMSDDE.H)""" + fdate: datetime + """Date/Time LSB: min=6b,hour=5b,day=5b,month=4b,year=12b""" + fres: str # 9 bytes + """Resolution description text (null terminated)""" + fsource: str # 9 bytes + """Source instrument description text (null terminated)""" + fpeakpt: int + """Peak point number for interferograms (0=not known)""" + fcmnt: str # 130 bytes + """Null terminated comment ASCII text string""" + fcatxt: str # 30 bytes + """X,Y,Z axis label strings if ftflgs=TALABS""" + flogoff: int + """File offset to log block or 0 (see above)""" + fmods: SpcModFlags + """File Modification Flags (see below: 1=A,2=B,4=C,8=D..)""" + fprocs: int + """Processing code (see GRAMSDDE.H)""" + flevel: int + """Calibration level plus one (1 = not calibration data)""" + fsampin: int + """Sub-method sample injection number (1 = first or only )""" + ffactor: float + """Floating data multiplier concentration factor (IEEE-32)""" + fmethod: str # 48 bytes + """Method/program/data filename w/extensions comma list""" + fzinc: float + """Z subfile increment (0 = use 1st subnext-subfirst)""" + fwplanes: int + """Number of planes for 4D with W dimension (0=normal)""" + fwinc: float + """W plane increment (only if fwplanes is not 0)""" + fwtype: int + """Type of W axis units (see definitions below)""" + + _PACKFMT = ' 'SpcHdr': + data = stream.read(SpcHdr._SIZE) + values = struct.unpack(SpcHdr._PACKFMT, data) + hdr = SpcHdr(*values) + + hdr.ftflgs = SpcFlags(hdr.ftflgs) + hdr.fexper = SpcTechnique(hdr.fexper) + hdr.fmods = SpcModFlags(hdr.fmods) + hdr.fdate = from_spc_date(hdr.fdate) + for field in ('fres', 'fsource', 'fcmnt', 'fcatxt', 'fmethod'): + value = getattr(hdr, field) + setattr(hdr, field, value.strip(b'\0').decode('utf-8')) + return hdr + + +@dataclass +class SubHdr: + """This structure defines the subfile headers that preceed each trace in a + multi-type file. Note that for evenly-spaced files, subtime and subnext are + optional (and ignored) for all but the first subfile. The (subnext-subtime) + for the first subfile determines the Z spacing for all evenly-spaced subfiles. + For ordered and random multi files, subnext is normally set to match subtime. + However, for all types, the subindx must be correct for all subfiles. + This header must must always be present even if there is only one subfile. + However, if TMULTI is not set, then the subexp is ignored in favor of fexp. + Normally, subflgs and subnois are set to zero and are used internally.""" + + subflgs: SubHdrFlags + """flags""" + + subexp: int + """Exponent for sub-file's Y values (80h=>float)""" + + subindx: int + """Integer index number of trace subfile (0=first)""" + + subtime: float + """Floating time for trace (Z axis corrdinate)""" + + subnext: float + """Floating time for next trace (May be same as beg)""" + + subnois: float + """Floating peak pick noise level if high byte nonzero""" + + subnpts: int + """Integer number of subfile points for TXYXYS type""" + + subscan: int + """Integer number of co-added scans or 0 (for collect)""" + + subwlevel: float + """Floating W axis value (if fwplanes non-zero)""" + + _PACKFMT = ' 'SubHdr': + subhdr = SubHdr(struct.unpack(SubHdr._PACKFMT, stream.read(SubHdr._SIZE))) + subhdr.subflgs = SubHdrFlags(subhdr.subflgs) + return subhdr + + +class SubFile: + """ + s = SpCFile[0] + s[0:10] # ilist + len(s) is fnpts + s.index = hdr + """ + + def fromstream(stream: BytesIO) -> 'SubFile': + pass + + +@dataclass +class LogHdr: + """Log block header format. + + This structure defines the header at the beginning of a flogoff block. + The logsizd should be large enough to hold the text and its ending zero. + The logsizm is normally set to be a multiple of 4096 and must be + greater than logsizd. It is normally set to the next larger multiple. + The logdsks section is a binary block which is not read into memory.""" + + logsizd: int + """byte size of disk block""" + + logsizm: int + """byte size of memory block""" + + logtxto: int + """byte offset to text""" + + logbins: int + """byte size of binary area (immediately after logstc)""" + + logdsks: int + """byte size of disk area (immediately after logbins)""" + + _PACKFMT = ' 'LogHdr': + values = struct.unpack(LogHdr._PACKFMT, stream.read(LogHdr._SIZE)) + return LogHdr(*values) + + +class SpcIter: + """Iterator for spectra in an SPC file.""" + def __init__(self, parent, index=0): + self.parent = parent + self.index = index + + def next(self): + """Python2 iterator support.""" + return self.__next__() + + def __next__(self): + if self.index >= len(self.parent): + raise StopIteration + result = self.parent[self.index] + self.index += 1 + return result + + +class SpcFile: + + def __init__(self, path: str = None, mode: str = 'rb'): + self.path = path + self.stream = None + self.owned = False + self._hdr = None + self._datapos = None + self._datasize = None + self._loghdr = None + self._logtext = None + if path: + self.open(path, mode) + + def open(self, path: str, mode: str = 'rb'): + """Open the specified path as an SPC file. + + mode: provide the file access mode: 'rb' for read-only + or 'r+b' for read-write mode.""" + self.path = path + self.open_stream(open(path, mode), owned=True) + + def open_stream(self, stream: BytesIO, owned: bool = False): + self.stream = stream + self.owned = owned + hdr = SpcHdr.fromstream(stream) + self._hdr = hdr + if hdr.ftflgs & SpcFlags.TXVALS: + self._xlist = struct.unpack( + f'<{hdr.fnpts}f', stream.read(4 * hdr.fnpts)) + else: + self._xlist = None + self._datapos = stream.tell() + self._datasize = SubHdr._SIZE + (hdr.fnpts * 4) # or 2 if flag. + stream.seek(hdr.fnsub * self._datasize, SEEK_CUR) + self._loghdr = LogHdr.fromstream(stream) + + def close(self): + """Close the file stream (if owned by this object)""" + if self.owned: + self.stream.close() + self.stream = None + + def __len__(self): + return self._hdr.fnsub + + def __iter__(self): + return SpcIter(self) + + def __getitem__(self, index): + """return the spectrum at specified index or slice""" + return self.spectrum(index) + + def __enter__(self): + return self + + def __exit__(self, vtype, value, traceback): + self.close() + + @property + def xlist(self): + if not self._xlist: + interval = (self._hdr.flast - self._hdr.ffirst) / (self._hdr.fnpts - 1) + self._xlist = [self._hdr.ffirst + (n * interval) for n in range(self._hdr.fnpts)] + return self._xlist + + @property + def comment(self) -> str: + return self._hdr.fcmnt + + def subhdr(self, index: int) -> SubHdr: + self.stream.seek(self.datapos + (self._datasize * index), SEEK_SET) + return SubHdr.fromstream(self.stream) + + def spectrum(self, index: int): + """Retrieve the spectrum values for the index-th spectrum. + + If the index is an integer returns a single spectrum as a tuple of floats. + If the index is a slice object then returns a tuple of tuples for the set + of specified spectra.""" + if isinstance(index, int): + start, stop, step = slice(index, None, None).indices(self._hdr.fnsub) + stop = start + 1 + elif isinstance(index, slice): + start, stop, step = index.indices(self._hdr.fnsub) + else: + raise TypeError("spectrum index must be an integer or slice") + + if start < 0 or stop > self._hdr.fnsub: + raise IndexError("spectrum index out of range") + + result = [] + for ndx in range(start, stop, step): + self.stream.seek(self._datapos + (self._datasize * ndx), SEEK_SET) + self.stream.seek(SubHdr._SIZE, SEEK_CUR) # skip subhdr + data = self.stream.read(self._hdr.fnpts * 4) + result.append(struct.unpack(f'{self._hdr.fnpts}f', data)) + return result[0] if len(result) == 1 else tuple(result) + + @property + def logtext(self): + if not self._logtext: + self.stream.seek(self._hdr.flogoff + LogHdr._SIZE, SEEK_SET) + data = self.stream.read(self._loghdr.logsizd) + self._logtext = data.strip(b'\0').decode('iso8859-1') + return self._logtext diff --git a/spc/test/__init__.py b/spc/test/__init__.py new file mode 100644 index 0000000..9184903 --- /dev/null +++ b/spc/test/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Renishaw plc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/spc/test/anatase.spc b/spc/test/anatase.spc new file mode 100644 index 0000000..166d3b8 Binary files /dev/null and b/spc/test/anatase.spc differ diff --git a/spc/test/benzene.spc b/spc/test/benzene.spc new file mode 100644 index 0000000..12eeeef Binary files /dev/null and b/spc/test/benzene.spc differ diff --git a/spc/test/test_spcfile.py b/spc/test/test_spcfile.py new file mode 100644 index 0000000..a85ea28 --- /dev/null +++ b/spc/test/test_spcfile.py @@ -0,0 +1,36 @@ +# Copyright (c) 2024 Renishaw plc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import unittest +import os +from ..spcfile import SpcFile + + +class TestWdf(unittest.TestCase): + testfile = os.path.join(os.path.dirname(__file__), r'anatase.spc') + + def test_with_spcfile(self): + with SpcFile(self.testfile) as data: + self.assertIsNotNone(data) + self.assertEqual(1, len(data)) + + def test_open(self): + data = SpcFile() + data.open(self.testfile) + data.close() + + +if __name__ == '__main__': + unittest.main() diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..86f2e19 --- /dev/null +++ b/tox.ini @@ -0,0 +1,27 @@ +[tox] +envlist = py38, py39, py310, py311, py312, pypy310, pypy39, lint +skip_missing_interpreters = True + +[testenv] +setenv = + PYTHONPATH = {toxinidir};{toxinidir}/spc +deps = + pytest +commands = + py.test + +[testenv:lint] +deps = + flake8 +commands = + flake8 spc demos + +[flake8] +max-line-length = 110 +ignore = + # F401 Constants imported into __init__ are not used there but are exposed to users here. + F401 + # W503 Line break before binary operator + W503 + # E402 module level import not at top of file + E402