org.jcamp.parser.CommonSpectrumJCAMPReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcamp-dx Show documentation
Show all versions of jcamp-dx Show documentation
The JCAMP-DX project is the reference implemention of the IUPAC JCAMP-DX spectroscopy data standard.
The newest version!
package org.jcamp.parser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;
import org.jcamp.math.IArray2D;
import org.jcamp.spectrum.Assignment;
import org.jcamp.spectrum.IAssignmentTarget;
import org.jcamp.spectrum.Multiplicity;
import org.jcamp.spectrum.Pattern;
import org.jcamp.spectrum.Peak1D;
import org.jcamp.spectrum.Spectrum;
import org.jcamp.spectrum.assignments.AtomReference;
import org.jcamp.spectrum.notes.NoteDescriptor;
import org.jcamp.units.CommonUnit;
import org.jcamp.units.Unit;
/**
* common implementation of JCAMPReader, implements shorthands for common LDRs.
*
* @author Thomas Weber
*/
abstract class CommonSpectrumJCAMPReader
implements ISpectrumJCAMPReader {
/**
* CommonJCAMPAdapter constructor comment.
*/
protected CommonSpectrumJCAMPReader() {
super();
}
/**
* createSpectrum method comment.
*/
public Spectrum createSpectrum(JCAMPBlock block) throws JCAMPException {
throw new JCAMPException("unimplemented adapter method");
}
/**
* gets ##LASTX= content
*
* @return double
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected double getFirstX(JCAMPBlock block) throws JCAMPException {
JCAMPVariable x = block.getVariable("X");
if (x == null || x.getFirst() == null) {
block.getErrorHandler().error("missing first x");
}
return x.getFirst().doubleValue();
}
/**
* gets ##FIRSTY= content
*
* @return double
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected double getFirstY(JCAMPBlock block) throws JCAMPException {
JCAMPVariable y = block.getVariable("Y");
if (y == null || y.getFirst() == null) {
block.getErrorHandler().error("missing first y");
}
return y.getFirst().doubleValue();
}
/**
* gets ##LASTX= content
*
* @return double
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected double getLastX(JCAMPBlock block) throws JCAMPException {
JCAMPVariable x = block.getVariable("X");
if (x == null || x.getLast() == null) {
block.getErrorHandler().error("missing last x");
}
return x.getLast().doubleValue();
}
/**
* gets ##NPOINTS= content
*
* @return double
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected int getNPoints(JCAMPBlock block) throws JCAMPException {
JCAMPDataRecord ldrNPoints = block.getDataRecord("NPOINTS");
if (ldrNPoints == null) {
block.getErrorHandler().error("missing required label ##NPOINTS=");
}
String nPoints = ldrNPoints.getContent();
return Integer.parseInt(nPoints);
}
/**
* gets NTuple Page data.
*
* @param block com.creon.chem.jcamp.JCAMPBlock
* @param page com.creon.chem.jcamp.JCAMPNTuplePage
* @return IArray2D
*/
protected IArray2D getNTuplePageData(JCAMPNTuplePage page)
throws JCAMPException {
return page.getXYData();
}
/**
* gets ##DATATABLE= content
*
* @return double[]
* @param block JCAMPBlock
* @param page JCAMPNTuplePage
* @param firstX double starting x value (from ##FIRSTX=)
* @param lastX double ending x value (from ##LASTX=)
* @param nPoints int number of data points (from ##NPOINTS=)
* @param xFactor double factor to be applied to x values (from ##XFACTOR=)
* @param yFactor double factor to be applied to y values (from ##YFACTOR=)
* @exception JCAMPException The exception description.
* @deprecated
*/
@Deprecated
protected double[] getNTupleXYData(
JCAMPBlock block,
JCAMPNTuplePage page,
double firstX,
double lastX,
int nPoints,
double xFactor,
double yFactor)
throws JCAMPException {
JCAMPDataRecord ldrXYData = page.getDataRecord("DATATABLE");
if (ldrXYData == null) {
block.getErrorHandler().fatal("missing required label ##DATATABLE=");
return null;
}
DataVariableInfo varInfo = new DataVariableInfo(ldrXYData);
if (!varInfo.isIncremental())
block.getErrorHandler().fatal("data form missmatch");
double[] y =
block.getASDFDecoder().decode(ldrXYData, firstX, lastX, xFactor, nPoints);
int n = y.length;
double[] yValues = new double[n];
for (int i = 0; i < n; i++)
yValues[i] = yFactor * y[i];
return yValues;
}
/**
* gets ##DATATABLE= content
*
* @return double[]
* @param block JCAMPBlock
* @param nPoints int number of data points (from ##NPOINTS=)
* @exception JCAMPException The exception description.
* @deprecated
*/
@Deprecated
protected double[][] getNTupleXYPoints(JCAMPBlock block, JCAMPNTuplePage page)
throws JCAMPException {
JCAMPDataRecord ldrPeaktable = page.getDataRecord("DATATABLE");
if (ldrPeaktable == null) {
block.getErrorHandler().fatal("missing required label ##DATATABLE=");
return null;
}
double[][] xy = new double[2][];
ArrayList x = new ArrayList(20);
ArrayList y = new ArrayList(20);
AFFNTokenizer tokenizer = new AFFNTokenizer(ldrPeaktable);
while (tokenizer.hasMoreGroups()) {
AFFNGroup group = tokenizer.nextGroup();
x.add(new Double(group.getValue(0)));
y.add(new Double(group.getValue(1)));
}
xy[0] = new double[x.size()];
xy[1] = new double[y.size()];
for (int i=0; i p.x)
return 1;
return 0;
}
};
JCAMPDataRecord ldrXYPoints = block.getDataRecord("XYPOINTS");
if (ldrXYPoints == null) {
block.getErrorHandler().fatal("missing required label ##XYPOINTS=");
return null;
}
int i = 0;
AFFNTokenizer tokenizer = new AFFNTokenizer(ldrXYPoints);
TreeSet data = new TreeSet();
while (tokenizer.hasMoreGroups()) {
AFFNGroup group = tokenizer.nextGroup();
data.add(new XYPair(xFactor * group.getValue(0), yFactor * group.getValue(1)));
}
if (data.size() != nPoints)
block.getErrorHandler().error("bad ##NPOINTS= or duplicate X values");
double[][] xy = new double[2][data.size()];
for (Iterator it = data.iterator(); it.hasNext();) {
XYPair p = (XYPair) it.next();
xy[0][i] = p.x;
xy[1][i] = p.y;
}
return xy;
}
/**
* gets ##XFACTOR= content
*
* @return double
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected double getYFactor(JCAMPBlock block) throws JCAMPException {
JCAMPVariable y = block.getVariable("Y");
if (y == null || y.getFactor() == null) {
block.getErrorHandler().warn("missing y factor, assuming 1.0");
return 1.0;
}
return y.getFactor().doubleValue();
}
/**
* gets ##YUNITS= content
*
* @return Unit
* @param block JCAMPBlock
* @exception JCAMPException The exception description.
*/
protected Unit getYUnits(JCAMPBlock block) throws JCAMPException {
JCAMPVariable y = block.getVariable("Y");
if (y == null || y.getUnit() == null) {
block.getErrorHandler().warn("missing y unit");
return CommonUnit.generic;
}
return y.getUnit();
}
/**
* analyse assignment text for targets.
*
* currently assumes SpecInfo convention of a list of integer atom numbers
*
* @return IAssignmentTarget[]
* @param assign java.lang.String
*/
protected static IAssignmentTarget[] parseAssignment(String assign) {
ArrayList targets = new ArrayList(5);
StringTokenizer tokenizer = new StringTokenizer(assign, ",");
while (tokenizer.hasMoreTokens()) {
String target = tokenizer.nextToken();
try {
int atomNo = Integer.parseInt(target.trim());
targets.add(new AtomReference(null, atomNo));
} catch (Exception e) {
}
}
IAssignmentTarget[] assigns = new IAssignmentTarget[targets.size()];
for (int i=0;i Double.MIN_VALUE) {
px.add(new Double(x0));
py.add(new Double(y0));
x0 = x;
y0 = y;
} else {
y0 += y;
}
}
px.add(new Double(x0));
py.add(new Double(y0));
double[][] xy = new double[2][px.size()];
for (int i = 0; i < px.size(); i++) {
xy[0][i] = ((Double) px.get(i)).doubleValue();
xy[1][i] = ((Double) py.get(i)).doubleValue();
}
return xy;
}
/**
* set spectrum note
*
* @param block JCAMPBlock
* @param ldr JCAMPDataRecord
* @param spectrum com.creon.chem.spectrum.Spectrum
*/
protected void setNote(
JCAMPBlock block,
JCAMPDataRecord ldr,
Spectrum spectrum)
throws JCAMPException {
String key = ldr.getKey();
if (key.length()==0) // comment
return;
NoteDescriptor descr = NoteDescriptorFactory.getInstance().findByJCAMPKey(key);
if (descr.equals(NoteDescriptor.IGNORE))
return;
Object content;
try {
content =
descr.getNoteContentParser().parseContent(
ldr.getContent(),
descr.getNoteContentClass());
spectrum.setNote(descr, content);
} catch (org.jcamp.spectrum.notes.BadContentException ex) {
StringBuilder msg =
new StringBuilder("bad ").append(descr.getName()).append(" note:\n").append(
ex.getMessage());
block.getErrorHandler().warn(msg.toString());
}
}
/**
* after creation of spectrum, add other notes
*
* @param block com.creon.chem.jcamp.JCAMPBlock
* @param spectrum com.creon.chem.spectrum.Spectrum
*/
protected void setNotes(JCAMPBlock block, Spectrum spectrum)
throws JCAMPException {
String title = getTitle(block);
spectrum.setTitle(title);
/* String owner = getOwner(block);
spectrum.setOwner(owner);
String origin = getOrigin(block);
spectrum.setOrigin(origin); */
int n = block.numDataRecords();
for (int i = 0; i < n; i++) {
JCAMPDataRecord ldr = (JCAMPDataRecord) block.getDataRecord(i);
setNote(block, ldr, spectrum);
}
}
}