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 http://webbook.nist.gov/cgi/cbook.cgi?ID=C7647010&Units=SI&Mask=200#Mass-Spec, IR spectrum at http://webbook.nist.gov/cgi/cbook.cgi?ID=C7647010&Units=SI&Type=IR-SPEC&Index=0#IR-SPEC). 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[{},
Quiet[Check[
ImportString[
StringReplace[
Import["http://webbook.nist.gov/cgi/cbook.cgi?JCAMP=C" <>
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):
##DATA PROCESSING=DIGITIZED BY NIST FROM HARD COPY (FROM TWO SEGMENTS)
##XUNITS=1/CM
##YUNITS=TRANSMITTANCE
##XFACTOR=1.000000
##YFACTOR=1
##DELTAX=0.935163
##FIRSTX=456.047
##LASTX=3765.59
##FIRSTY=0.959
##MAXX=3765.59
##MINX=456.047
##MAXY=1.001
##MINY=0.213
##NPOINTS=3540
##XYDATA=(X++(Y..Y))
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?
l
where you want to averagel[[2;;]]
for the y value, you could use{l[[1]],Mean@l[[2;;]]}
$\endgroup$