nD to 1D spectra from a peak position

What does the Macro do?

A macro to extract 1D sliceData at a (selected) peak Position and create 1D spectra for each dimension.

How to run the Macro

Open a SpectrumDisplay, select a Peak of interest, then run the macro from the iPython Console or macro editor.

Code

# Copyright 2023 Luca Mureddu
# This code is distributed using the MIT licence. See http://opensource.org/license/mit for details.
#############################################################################
import numpy as np
peak = current.peak
spectrum = peak.spectrum
for dim in spectrum.dimensions:
    pointPositions = np.array(peak.pointPositions, dtype=int)
    data = spectrum.getSliceData(position=list(pointPositions), sliceDim=dim)
    isotopeCodes = spectrum.getByDimensions('isotopeCodes',dimensions=[dim])
    pointCounts = spectrum.getByDimensions('pointCounts', dimensions=[dim])
    dd = {}
    properties = ['spectralWidths', 'spectralWidthsHz', 'spectrometerFrequencies', 'referencePoints', 'referenceValues']
    for p in properties:
        dd[p] = spectrum.getByDimensions(p, [dim])
    sp = project.newEmptySpectrum(name=f'{spectrum.name}-slice-{dim}',isotopeCodes=isotopeCodes, pointCounts=pointCounts, **dd)
    sp.noiseLevel = spectrum.noiseLevel
    sp.setBuffering(True)
    sp.setSliceData(data,)

Credits and References

Luca Mureddu, CCPN team, University of Leicester