Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

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

1 Answer 1

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["http://webbook.nist.gov/cgi/cbook.cgi?JCAMP=C" <> 
  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

 
discard

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.