Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a little function to import JCAMP-DX electron ionization or mass spectral files from the NIST webbook web site (e.g. mass spectrum of hydrogen chloride at, IR spectrum at I already managed to fix a small issue to allow importing mass spectra, but the IR spectrum import does not work yet. My function is

importspectrum[casnr_, type_] := Module[{},
        Import["" <> 
          StringReplace[casnr, "-" -> ""] <> "&Index=0&Type=" <> type,
          "String"], {x : NumberString ~~ " " ~~ y : NumberString :> 
          x <> "\n" <> y}], "JCAMPDX"][[1]], "NA"]]]; 
importmassspectrum[casnr_] := importspectrum[casnr, "Mass"];
importIRspectrum[casnr_] := importspectrum[casnr, "IR"];

This works OK for mass spectra:

spec=importspectrum["7647-01-0", "Mass"]
{{35, 1702}, {36, 9999}, {37, 540}, {38, 3243}}

ListPlot[spec, Filling -> Axis, FillingStyle -> Blue, 
 PlotRange -> All, PlotStyle -> PointSize[0]]

But it doesn't work for the IR spectra:

spec=importspectrum["7647-01-0", "IR"]

There, the problem is that Mathematica expects X, Y values whereas in the NIST IR spectrum files each line is an X value plus several measured Y values (which should be averages):

456.047000 0.9590 0.9590 0.9590 0.9580 0.9580
460.722814 0.9560 0.9560 0.9560 0.9560 0.9560
465.398628 0.9550 0.9550 0.9550 0.9530 0.9530
470.074442 0.9530 0.9530 0.9520 0.9520 0.9520
474.750255 0.9520 0.9500 0.9500 0.9500 0.9490

So this should first be transformed to

456.047000 average of (0.9590 0.9590 0.9590 0.9580 0.9580)

Does anyone know what would be the most elegant way to do this in Mathematica, without using For to loops etc?

share|improve this question
It's not obvious to me how you will do this within your optimized function, but for a list of values, l where you want to average l[[2;;]] for the y value, you could use {l[[1]],Mean@l[[2;;]]} – bobthechemist Nov 18 '13 at 0:11
up vote 1 down vote accepted

I quite tentatively suggest that the format of "JCAMPDX" is different than you describe. There is a parameter in the file, ##DELTAX=0.935163. When Mathematica imports this file, in this way,

Import["" <> 
  StringReplace["7647-01-0", "-" -> ""] <> "&Index=0&Type=" <> "IR", "JCAMPDX"]

you get a list of {x, y} values:

{{456.047, 0.959}, {456.982, 0.959}, {457.917, 0.959}, {458.852, 0.958}, {459.788, 0.958},
 {460.723, 0.956}, {461.658, 0.956}, {462.593, 0.956}, {463.528, 0.956}, {464.463, 0.956},

Note that the differences in the x coordinates correspond to the DELTAX parameter in the file. And after five entries, the x coordinate on the next line in the file corresponds to the previous x plus five times DELTAX.

I think the proper way to import a "JCAMPDX" file is with the above Import command.

share|improve this answer
Ha thanks once again - yes, it seemed that a plain JCAMPDX import in this case did work, and that I made a mistake in preprocessing the file (which was required for the mass spectral files, but not for these ones)! – Tom Wenseleers Nov 18 '13 at 3:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.