From 4bbc113bc46c19733b3f7536ff11149fad780d09 Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Mon, 11 Mar 2024 07:59:54 +0000 Subject: [PATCH] hack --- .gitignore | 8 + LICENSE | 202 +++++++++++++++++++++++ MANIFEST.in | 4 + README.md | 0 demos/gspc_test.py | 45 ++++++ pyproject.toml | 3 + setup.cfg | 45 ++++++ setup.py | 3 + spc/__init__.py | 29 ++++ spc/spcenums.py | 128 +++++++++++++++ spc/spcfile.py | 342 +++++++++++++++++++++++++++++++++++++++ spc/test/__init__.py | 13 ++ spc/test/anatase.spc | Bin 0 -> 3929 bytes spc/test/benzene.spc | Bin 0 -> 7912 bytes spc/test/test_spcfile.py | 36 +++++ tox.ini | 27 ++++ 16 files changed, 885 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 MANIFEST.in create mode 100644 README.md create mode 100644 demos/gspc_test.py create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 spc/__init__.py create mode 100644 spc/spcenums.py create mode 100644 spc/spcfile.py create mode 100644 spc/test/__init__.py create mode 100644 spc/test/anatase.spc create mode 100644 spc/test/benzene.spc create mode 100644 spc/test/test_spcfile.py create mode 100644 tox.ini 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 0000000000000000000000000000000000000000..166d3b8784295be4c2940ad0eacfc551e76f5780 GIT binary patch literal 3929 zcmds4O=uHA6nv~{roVDEA-D3vro;P59YQNQbbZsI zeT2j3RbCArD^#y87T!L2RH%lX3DkvN0k z!dc*=*=)r8nc{3W?}&NT{CVTtA8O*9PI2$NhHMVzU4I?8&Bb|g*obitQ~UV)jF1D* z8qW#2asIe4ADtQ1fHGKLGkZCndZB(W7v{wpuok>Gcz>{;>qj}Pra2_fa`-;(_fx?B!;KB(ifh3A6(c+Z-^%e`P9axZi~av#AO>zxTbW8|xMKUMd-RbM^+ zU=38uPI4!XMs})I#*vLA`bck8(z`~EBOA(~UEZTEV^w-9SQXE1e1AH(ia+RUw3qus zY!EN|RXW8s8!zMK7}<{bXup$AewCzHk}B+8`2M#%dohz{Iq1F9ES_+U;#BV}m5=yU zl5z)mWIOrDhJ2(ql0j^6Pw`YE?Mce`W-u5h<47kSS(2|_jGVuj7~-Hf+6QB0Kk0HE zvW;|-GLH0rM83pNls14|i=Qe4*CJQEFIRTHVisM;m>YB~cV^ynt>QH;GHE&s;+t|H w8BIjv(L|pXN%q9ziC8io?_-JHY@$D#7RJ$v=@_o<49rg_lBxboTFnYS0iV2;i2wiq literal 0 HcmV?d00001 diff --git a/spc/test/benzene.spc b/spc/test/benzene.spc new file mode 100644 index 0000000000000000000000000000000000000000..12eeeefaaf1c12feefd40715402377fe9fe3d639 GIT binary patch literal 7912 zcmd5>YjjjqmOl4Yk5r0O2uKi+VgLbo72+c_+7t#P2#iT10wPij(!c^FsElER;j+*s z4BCdK5g0_`gAqj~3@R`{Q>aK-h$JE=kc3o7LJ~+!g-1xJ^O*A;r&#^VpS^~)4)@+u z=ehT{_x|<{nLzaWodo~N6 zXiZ~-tu1_}<>D)?Qnt;C#Ex6j#0D!&Ct3L27AMUdI^3-1r;Qc#n6Zc*GV<7J;}J34 zkaLZ{vLwStnT|GwR_gU^n_j?Y>Ydn7J;-iq|19dXLhjT9^g+FzkJ5h;ZXitke@($le{+*SO8?oagPn;wLLQ^olDVCC*qMN$rVUa}sTdXE8utUTk z@00&w2gx5;vi7y;p*4v)q+BGEV`4jbjk~oMX&3EzVQ7EheYM|-N44kKV6_kWd8|tN zj-AsUq2Fkkbg^E*4(h3Nz5W`Ts~_ge^&)yy-eb9u!PCuvIA!LrxmJI^)k>uwSqH>! ztBCJa&mgU|QrQGY1{>_iEv3^r|rFoS@ygQ=x+_xd>=e1au48`5>STY(P+vjoo(}mn z+q~JAP9%n^dM8Hs3{OGh>V$CHEqAnPnLF>i;SNVjTzSnOxpG>XozW}8Q5Y?8Y-w$9 zgvwrbL}QZ`#$0TuUedEgPkkCsR~R}#JL}(z5qeu}vi=Sq zt$!*sJ)b4%E|#J%qFwZCHeL_2ae8CyxGvW+N%es z(y=G@UO|T&-_v2nM7r2WWk(FTr;*8vjUv{f?(C-znwesmStw4J^1hajq*+n=x|Pqb zN*^%WA@%P#z*-!E=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