ucar.nc2.iosp.noaa.StructureDataAscii Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.iosp.noaa;
import ucar.ma2.*;
import ucar.nc2.util.TableParser;
/**
* StructureData whose data is stored in ascii, with a TableParser to extract the values.
* @author caron
* @since Dec 8, 2010
*/
public class StructureDataAscii extends StructureData {
protected String line;
public StructureDataAscii(StructureMembers members, String line) {
super(members);
this.line = line;
}
@Override
public Array getArray(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
if (m.getDataType() == DataType.STRING) {
String result = (String) f.parse(line);
return Array.factory(DataType.STRING, new int[] {}, new Object[] {result.trim()});
} else if (m.getDataType() == DataType.SEQUENCE) {
return getArraySequence(m);
} else if (!m.isScalar()) {
if (m.getDataType() == DataType.FLOAT) {
float[] ja = getJavaArrayFloat(m);
return Array.factory(DataType.FLOAT, m.getShape(), ja);
} else if (m.getDataType() == DataType.CHAR) {
char[] ja = getJavaArrayChar(m);
return Array.factory(DataType.CHAR, m.getShape(), ja);
} else if (m.getDataType() == DataType.BYTE) {
byte[] ja = getJavaArrayByte(m);
return Array.factory(DataType.BYTE, m.getShape(), ja);
}
}
Object result = f.parse(line);
if (m.getDataType() == DataType.CHAR)
return new ArrayChar((String) result);
else
return new ArrayScalar(result, m.getDataType().isUnsigned());
}
@Override
public float convertScalarFloat(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
return ((Number) f.parse(line)).floatValue();
}
@Override
public double convertScalarDouble(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
return ((Number) f.parse(line)).doubleValue();
}
@Override
public int convertScalarInt(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
return ((Number) f.parse(line)).intValue();
}
@Override
public long convertScalarLong(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
return ((Number) f.parse(line)).longValue();
}
@Override
public double getScalarDouble(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
return (Double) f.parse(line);
}
@Override
public double[] getJavaArrayDouble(StructureMembers.Member m) {
return new double[0]; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public float getScalarFloat(StructureMembers.Member m) {
TableParser.Field f = (TableParser.Field) m.getDataObject();
Object result = f.parse(line);
return (result instanceof Float) ? (Float) f.parse(line) : ((Double) f.parse(line)).floatValue();
}
@Override
public float[] getJavaArrayFloat(StructureMembers.Member m) {
int n = m.getSize();
float[] result = new float[n];
TableParser.Field f = (TableParser.Field) m.getDataObject();
for (int i=0; i