--- /dev/null
+.tox/
+.vscode/
+*.egg-info/
+__pycache__/
+build/
+dist/
+.pytest_cache/
+*.whl
\ No newline at end of file
--- /dev/null
+
+ 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.
--- /dev/null
+include tox.ini
+include demos/*.py
+include spc/test/*.py
+include spc/test/*.spc
--- /dev/null
+"""\r
+Read an SPC file using the Galactic GSPCIO COM object.\r
+\r
+Will require 32 bit python as the COM object is only available for 32 bit.\r
+"""\r
+\r
+import sys\r
+import argparse\r
+from comtypes.client import CreateObject\r
+\r
+PROGID_GSPCIO='GSPCIOLib.GSPCIO'\r
+\r
+\r
+def main(args=None):\r
+ """Application entry"""\r
+ parser = argparse.ArgumentParser(\r
+ description='Read an SPC file using the Galactic GSPCIO COM library')\r
+ parser.add_argument('filename', help='Path to SPC file.')\r
+ parser.add_argument('--show-log', dest='showlogtext', action='store_true',\r
+ help='Emit the log text data lines.')\r
+ options = parser.parse_args(args)\r
+\r
+ gspcio = CreateObject(PROGID_GSPCIO)\r
+ gspcio.OpenFile(options.filename)\r
+ print(f"File has {gspcio.NumSubfiles} spectra with {gspcio.NumPoints} points")\r
+ print(f'Comment: "{gspcio.Comment}"')\r
+ print(f'Description: "{gspcio.GetLogKey("Description")}"')\r
+ if options.showlogtext:\r
+ print(f"LogText: ({gspcio.LogTextSize} bytes)")\r
+ index = 0\r
+ while True:\r
+ line, length = gspcio.GetLogLine(index)\r
+ if length < 0:\r
+ break\r
+ if line:\r
+ print(f' {line}')\r
+ else:\r
+ print(f'-- empty line {length} bytes reported')\r
+ index += 1\r
+\r
+ return 0\r
+\r
+\r
+if __name__ == '__main__':\r
+ sys.exit(main(sys.argv[1:]))\r
--- /dev/null
+[build-system]
+requires = ["setuptools >= 65.0"]
+build-backend = "setuptools.build_meta"
--- /dev/null
+[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
--- /dev/null
+import setuptools
+
+setuptools.setup()
--- /dev/null
+# 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)
--- /dev/null
+# 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"""
--- /dev/null
+# Copyright (c) 2024 Renishaw plc.\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+# https://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+\r
+"""\r
+"""\r
+\r
+import struct\r
+from dataclasses import dataclass\r
+from datetime import datetime\r
+from enum import IntEnum, IntFlag\r
+from io import BufferedReader, BytesIO\r
+from os import SEEK_SET, SEEK_CUR\r
+from typing import Sequence\r
+from .spcenums import SpcFlags, SpcTechnique, SpcModFlags, SubHdrFlags\r
+\r
+\r
+def from_spc_date(fdate: int) -> datetime:\r
+ """Convert an SPC file timestamp to a Python datetime\r
+\r
+ Date/Time LSB: min=6b,hour=5b,day=5b,month=4b,year=12b"""\r
+ return fdate\r
+ print(hex(fdate), hex(fdate >> (32 - 12)))\r
+ year = ((fdate >> 25) & 0x3f) + 1980\r
+ month = (fdate >> 21) & 0x0f\r
+ day = (fdate >> 16) & 0x0f\r
+ hour = (fdate >> 11) & 0x1f\r
+ mins = (fdate >> 5) & 0x3f\r
+ secs = (fdate) & 0x1f\r
+ return datetime(year, month, day, hour=hour, minute=mins, second=secs)\r
+\r
+\r
+@dataclass\r
+class SpcHdr:\r
+ ftflgs: SpcFlags\r
+ """File flags from the SpcFlags enumeration"""\r
+ fversn: int\r
+ """0x4B=> new LSB 1st, 0x4C=> new MSB 1st, 0x4D=> old format"""\r
+ fexper: int\r
+ """Instrument technique code"""\r
+ fexp: int\r
+ """Fraction scaling exponent integer (80h=>float)"""\r
+ fnpts: int\r
+ """Integer number of points (or TXYXYS directory position)"""\r
+ ffirst: float\r
+ """Floating X coordinate of first point"""\r
+ flast: float\r
+ """Floating X coordinate of last point"""\r
+ fnsub: int\r
+ """Integer number of subfiles (1 if not TMULTI)"""\r
+ fxtype: int\r
+ """Type of X axis units"""\r
+ fytype: int\r
+ """Type of Y axis units"""\r
+ fztype: int\r
+ """Type of Z axis units"""\r
+ fpost: int\r
+ """Posting disposition (see GRAMSDDE.H)"""\r
+ fdate: datetime\r
+ """Date/Time LSB: min=6b,hour=5b,day=5b,month=4b,year=12b"""\r
+ fres: str # 9 bytes\r
+ """Resolution description text (null terminated)"""\r
+ fsource: str # 9 bytes\r
+ """Source instrument description text (null terminated)"""\r
+ fpeakpt: int\r
+ """Peak point number for interferograms (0=not known)"""\r
+ fcmnt: str # 130 bytes\r
+ """Null terminated comment ASCII text string"""\r
+ fcatxt: str # 30 bytes\r
+ """X,Y,Z axis label strings if ftflgs=TALABS"""\r
+ flogoff: int\r
+ """File offset to log block or 0 (see above)"""\r
+ fmods: SpcModFlags\r
+ """File Modification Flags (see below: 1=A,2=B,4=C,8=D..)"""\r
+ fprocs: int\r
+ """Processing code (see GRAMSDDE.H)"""\r
+ flevel: int\r
+ """Calibration level plus one (1 = not calibration data)"""\r
+ fsampin: int\r
+ """Sub-method sample injection number (1 = first or only )"""\r
+ ffactor: float\r
+ """Floating data multiplier concentration factor (IEEE-32)"""\r
+ fmethod: str # 48 bytes\r
+ """Method/program/data filename w/extensions comma list"""\r
+ fzinc: float\r
+ """Z subfile increment (0 = use 1st subnext-subfirst)"""\r
+ fwplanes: int\r
+ """Number of planes for 4D with W dimension (0=normal)"""\r
+ fwinc: float\r
+ """W plane increment (only if fwplanes is not 0)"""\r
+ fwtype: int\r
+ """Type of W axis units (see definitions below)"""\r
+\r
+ _PACKFMT = '<bbbbIddIbbbbI9s9sh32x130s30sIIbbhf48sfIfb187x'\r
+ _SIZE = 8 + 16 + 4 + 4 + 4 + 9 + 9 + 2 + 32 + 130 \\r
+ + 30 + 8 + 2 + 2 + 4 + 48 + 4 + 4 + 4 + 1 + 187\r
+\r
+ @staticmethod\r
+ def fromstream(stream: BytesIO) -> 'SpcHdr':\r
+ data = stream.read(SpcHdr._SIZE)\r
+ values = struct.unpack(SpcHdr._PACKFMT, data)\r
+ hdr = SpcHdr(*values)\r
+\r
+ hdr.ftflgs = SpcFlags(hdr.ftflgs)\r
+ hdr.fexper = SpcTechnique(hdr.fexper)\r
+ hdr.fmods = SpcModFlags(hdr.fmods)\r
+ hdr.fdate = from_spc_date(hdr.fdate)\r
+ for field in ('fres', 'fsource', 'fcmnt', 'fcatxt', 'fmethod'):\r
+ value = getattr(hdr, field)\r
+ setattr(hdr, field, value.strip(b'\0').decode('utf-8'))\r
+ return hdr\r
+\r
+\r
+@dataclass\r
+class SubHdr:\r
+ """This structure defines the subfile headers that preceed each trace in a\r
+ multi-type file. Note that for evenly-spaced files, subtime and subnext are\r
+ optional (and ignored) for all but the first subfile. The (subnext-subtime)\r
+ for the first subfile determines the Z spacing for all evenly-spaced subfiles.\r
+ For ordered and random multi files, subnext is normally set to match subtime.\r
+ However, for all types, the subindx must be correct for all subfiles.\r
+ This header must must always be present even if there is only one subfile.\r
+ However, if TMULTI is not set, then the subexp is ignored in favor of fexp.\r
+ Normally, subflgs and subnois are set to zero and are used internally."""\r
+\r
+ subflgs: SubHdrFlags\r
+ """flags"""\r
+\r
+ subexp: int\r
+ """Exponent for sub-file's Y values (80h=>float)"""\r
+\r
+ subindx: int\r
+ """Integer index number of trace subfile (0=first)"""\r
+\r
+ subtime: float\r
+ """Floating time for trace (Z axis corrdinate)"""\r
+\r
+ subnext: float\r
+ """Floating time for next trace (May be same as beg)"""\r
+\r
+ subnois: float\r
+ """Floating peak pick noise level if high byte nonzero"""\r
+\r
+ subnpts: int\r
+ """Integer number of subfile points for TXYXYS type"""\r
+\r
+ subscan: int\r
+ """Integer number of co-added scans or 0 (for collect)"""\r
+\r
+ subwlevel: float\r
+ """Floating W axis value (if fwplanes non-zero)"""\r
+\r
+ _PACKFMT = '<bbhfffIIf4x'\r
+ _SIZE = 32\r
+\r
+ @staticmethod\r
+ def fromstream(stream: BytesIO) -> 'SubHdr':\r
+ subhdr = SubHdr(struct.unpack(SubHdr._PACKFMT, stream.read(SubHdr._SIZE)))\r
+ subhdr.subflgs = SubHdrFlags(subhdr.subflgs)\r
+ return subhdr\r
+\r
+\r
+class SubFile:\r
+ """\r
+ s = SpCFile[0]\r
+ s[0:10] # ilist\r
+ len(s) is fnpts\r
+ s.index = hdr\r
+ """\r
+\r
+ def fromstream(stream: BytesIO) -> 'SubFile':\r
+ pass\r
+\r
+\r
+@dataclass\r
+class LogHdr:\r
+ """Log block header format.\r
+\r
+ This structure defines the header at the beginning of a flogoff block.\r
+ The logsizd should be large enough to hold the text and its ending zero.\r
+ The logsizm is normally set to be a multiple of 4096 and must be\r
+ greater than logsizd. It is normally set to the next larger multiple.\r
+ The logdsks section is a binary block which is not read into memory."""\r
+\r
+ logsizd: int\r
+ """byte size of disk block"""\r
+\r
+ logsizm: int\r
+ """byte size of memory block"""\r
+\r
+ logtxto: int\r
+ """byte offset to text"""\r
+\r
+ logbins: int\r
+ """byte size of binary area (immediately after logstc)"""\r
+\r
+ logdsks: int\r
+ """byte size of disk area (immediately after logbins)"""\r
+\r
+ _PACKFMT = '<IIIII44x'\r
+ _SIZE = 20 + 44\r
+\r
+ @staticmethod\r
+ def fromstream(stream: BytesIO) -> 'LogHdr':\r
+ values = struct.unpack(LogHdr._PACKFMT, stream.read(LogHdr._SIZE))\r
+ return LogHdr(*values)\r
+\r
+\r
+class SpcIter:\r
+ """Iterator for spectra in an SPC file."""\r
+ def __init__(self, parent, index=0):\r
+ self.parent = parent\r
+ self.index = index\r
+\r
+ def next(self):\r
+ """Python2 iterator support."""\r
+ return self.__next__()\r
+\r
+ def __next__(self):\r
+ if self.index >= len(self.parent):\r
+ raise StopIteration\r
+ result = self.parent[self.index]\r
+ self.index += 1\r
+ return result\r
+\r
+\r
+class SpcFile:\r
+\r
+ def __init__(self, path: str = None, mode: str = 'rb'):\r
+ self.path = path\r
+ self.stream = None\r
+ self.owned = False\r
+ self._hdr = None\r
+ self._datapos = None\r
+ self._datasize = None\r
+ self._loghdr = None\r
+ self._logtext = None\r
+ if path:\r
+ self.open(path, mode)\r
+\r
+ def open(self, path: str, mode: str = 'rb'):\r
+ """Open the specified path as an SPC file.\r
+\r
+ mode: provide the file access mode: 'rb' for read-only\r
+ or 'r+b' for read-write mode."""\r
+ self.path = path\r
+ self.open_stream(open(path, mode), owned=True)\r
+\r
+ def open_stream(self, stream: BytesIO, owned: bool = False):\r
+ self.stream = stream\r
+ self.owned = owned\r
+ hdr = SpcHdr.fromstream(stream)\r
+ self._hdr = hdr\r
+ if hdr.ftflgs & SpcFlags.TXVALS:\r
+ self._xlist = struct.unpack(\r
+ f'<{hdr.fnpts}f', stream.read(4 * hdr.fnpts))\r
+ else:\r
+ self._xlist = None\r
+ self._datapos = stream.tell()\r
+ self._datasize = SubHdr._SIZE + (hdr.fnpts * 4) # or 2 if flag.\r
+ stream.seek(hdr.fnsub * self._datasize, SEEK_CUR)\r
+ self._loghdr = LogHdr.fromstream(stream)\r
+\r
+ def close(self):\r
+ """Close the file stream (if owned by this object)"""\r
+ if self.owned:\r
+ self.stream.close()\r
+ self.stream = None\r
+\r
+ def __len__(self):\r
+ return self._hdr.fnsub\r
+\r
+ def __iter__(self):\r
+ return SpcIter(self)\r
+\r
+ def __getitem__(self, index):\r
+ """return the spectrum at specified index or slice"""\r
+ return self.spectrum(index)\r
+\r
+ def __enter__(self):\r
+ return self\r
+\r
+ def __exit__(self, vtype, value, traceback):\r
+ self.close()\r
+\r
+ @property\r
+ def xlist(self):\r
+ if not self._xlist:\r
+ interval = (self._hdr.flast - self._hdr.ffirst) / (self._hdr.fnpts - 1)\r
+ self._xlist = [self._hdr.ffirst + (n * interval) for n in range(self._hdr.fnpts)]\r
+ return self._xlist\r
+\r
+ @property\r
+ def comment(self) -> str:\r
+ return self._hdr.fcmnt\r
+\r
+ def subhdr(self, index: int) -> SubHdr:\r
+ self.stream.seek(self.datapos + (self._datasize * index), SEEK_SET)\r
+ return SubHdr.fromstream(self.stream)\r
+\r
+ def spectrum(self, index: int):\r
+ """Retrieve the spectrum values for the index-th spectrum.\r
+\r
+ If the index is an integer returns a single spectrum as a tuple of floats.\r
+ If the index is a slice object then returns a tuple of tuples for the set\r
+ of specified spectra."""\r
+ if isinstance(index, int):\r
+ start, stop, step = slice(index, None, None).indices(self._hdr.fnsub)\r
+ stop = start + 1\r
+ elif isinstance(index, slice):\r
+ start, stop, step = index.indices(self._hdr.fnsub)\r
+ else:\r
+ raise TypeError("spectrum index must be an integer or slice")\r
+\r
+ if start < 0 or stop > self._hdr.fnsub:\r
+ raise IndexError("spectrum index out of range")\r
+\r
+ result = []\r
+ for ndx in range(start, stop, step):\r
+ self.stream.seek(self._datapos + (self._datasize * ndx), SEEK_SET)\r
+ self.stream.seek(SubHdr._SIZE, SEEK_CUR) # skip subhdr\r
+ data = self.stream.read(self._hdr.fnpts * 4)\r
+ result.append(struct.unpack(f'{self._hdr.fnpts}f', data))\r
+ return result[0] if len(result) == 1 else tuple(result)\r
+\r
+ @property\r
+ def logtext(self):\r
+ if not self._logtext:\r
+ self.stream.seek(self._hdr.flogoff + LogHdr._SIZE, SEEK_SET)\r
+ data = self.stream.read(self._loghdr.logsizd)\r
+ self._logtext = data.strip(b'\0').decode('iso8859-1')\r
+ return self._logtext\r
--- /dev/null
+# 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.
--- /dev/null
+# 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()
--- /dev/null
+[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