Import/Export of JCAMP-DX files#

JCAMP-DX is an open format used by several spectroscopies. Use scp.read_jcamp() or scp.jcamp.read() to open infrared spectra and supported processed NMR spectra in 1D and 2D. The reader selects the workflow from the data type declared in the file. If that type is ambiguous, use jcamp_kind="ir" or jcamp_kind="nmr".

The IR reader supports AFFN and DIFDUP spectra on a regular grid, including linked series on a shared grid. Reading processed NMR spectra requires the SpectroChemPy NMR plugin. For a supported real 2D spectrum stored in JCAMP-DX NTUPLES, install nmrglue-ng 1.3 or later and call scp.read_jcamp("cosy.jdx"). The reader returns a 2D dataset with the F1 and F2 axes from the file.

The example below shows how to export and read back an infrared spectrum.

[1]:
import spectrochempy as scp

X = scp.read_omnic("irdata//CO@Mo_Al2O3.SPG")
S0 = X[0]
S0
[1]:
NDDataset [CO@Mo_Al2O3] — float64, shape: (y:1, x:3112), a.u.
name
:
CO@Mo_Al2O3
author
:
runner@runnervm8axu0
created
:
2026-11-15 08:44:31+00:00
description
:
Omnic title: Group sust Mo_Al2O3_base line.SPG
Omnic filename: /home/runner/.spectrochempy/testdata/irdata/CO@Mo_Al2O3.SPG
history
:
2026-11-15 08:44:31+00:00> Imported OMNIC SPG file CO@Mo_Al2O3.SPG
2026-11-15 08:44:31+00:00> Sorted by date
2026-11-15 08:44:31+00:00> Slice extracted: y index [0]
Data
title
:
absorbance
values
:
[[0.0008032 3.788e-05 ... 0.0003027 0.0003745]] a.u.
shape
:
(y:1, x:3112)
Dimension `x`
size
:
3112
title
:
wavenumbers
coordinates
:
[ 4000 3999 ... 1001 999.9] cm⁻¹
Dimension `y`
size
:
1
title
:
acquisition timestamp (GMT)
coordinates
:
[1.477e+09] s
labels
:
[[ 2016-10-18 13:49:35+00:00]
[ *Résultat de Soustraction:04_Mo_Al2O3_calc_0.003torr_LT_after sulf_Oct 18 15:46:42 2016 (GMT+02:00)]]
[2]:
S0.write_jcamp("CO@Mo_Al2O3_0.jdx", confirm=False)
[2]:
PosixPath('/home/runner/work/spectrochempy/spectrochempy/docs/sources/userguide/importexport/CO@Mo_Al2O3_0.jdx')

The namespace API is also available for writing:

[3]:
scp.jcamp.write(S0, "CO@Mo_Al2O3_0.jdx", confirm=False, overwrite=True)
[3]:
PosixPath('/home/runner/work/spectrochempy/spectrochempy/docs/sources/userguide/importexport/CO@Mo_Al2O3_0.jdx')

Then used (and maybe changed) by a 3rd party software, and re-imported in spectrochempy:

[4]:
newS0 = scp.read_jcamp("CO@Mo_Al2O3_0.jdx")
newS0
[4]:
NDDataset [CO@Mo_Al2O3] — float64, shape: (y:1, x:3112), a.u.
name
:
CO@Mo_Al2O3
author
:
runner@runnervm8axu0
created
:
2026-11-15 08:44:32+00:00
description
:
Dataset from jdx file: 'CO@Mo_Al2O3'
history
:
2026-11-15 08:44:32+00:00> Imported JCAMP-DX file CO@Mo_Al2O3_0.jdx
Data
title
:
absorbance
values
:
[[0.0008032 3.787e-05 ... 0.0003027 0.0003745]] a.u.
shape
:
(y:1, x:3112)
Dimension `x`
size
:
3112
title
:
wavenumbers
coordinates
:
[ 4000 3999 ... 1001 999.9] cm⁻¹
Dimension `y`
title
:
coordinates
:
Undefined

The namespace API works identically:

[5]:
newS0 = scp.jcamp.read("CO@Mo_Al2O3_0.jdx")
newS0
[5]:
NDDataset [CO@Mo_Al2O3] — float64, shape: (y:1, x:3112), a.u.
name
:
CO@Mo_Al2O3
author
:
runner@runnervm8axu0
created
:
2026-11-15 08:44:32+00:00
description
:
Dataset from jdx file: 'CO@Mo_Al2O3'
history
:
2026-11-15 08:44:32+00:00> Imported JCAMP-DX file CO@Mo_Al2O3_0.jdx
Data
title
:
absorbance
values
:
[[0.0008032 3.787e-05 ... 0.0003027 0.0003745]] a.u.
shape
:
(y:1, x:3112)
Dimension `x`
size
:
3112
title
:
wavenumbers
coordinates
:
[ 4000 3999 ... 1001 999.9] cm⁻¹
Dimension `y`
title
:
coordinates
:
Undefined

It is important to note here that the conversion to JCAMP-DX changes the last digits of absorbance and wavenumbers:

The writer preserves the numeric values it writes. It does not silently convert wavelength to wavenumber or transmittance to absorbance. Current export support is therefore limited to exact-scale JCAMP mappings:

  • x: cm^-1, um, nm, or no unit (ARBITRARY UNITS);

  • y: absorbance, transmittance, or no unit (ARBITRARY UNITS).

Other merely convertible or named arbitrary units are rejected until you convert them explicitly before export.

[6]:
from spectrochempy.utils.compare import difference
[7]:
max_error, max_rel_error = difference(S0, newS0)
print(f"Max absolute difference in absorbance: {max_error:.3g}")
print(f"Max relative difference in absorbance: {max_rel_error:.3g}")
Max absolute difference in absorbance: 1e-08
Max relative difference in absorbance: 0.863
[8]:
max_error, max_rel_error = difference(S0.x, newS0.x)
print(f"Max absolute difference in wavenumber: {max_error:.3g}")
print(f"Max relative difference in wavenumber: {max_rel_error:.3g}")
Max absolute difference in wavenumber: 0.0005
Max relative difference in wavenumber: 4.98e-07

But this is much beyond the experimental accuracy of the data.