package nl.wldelft.timeseriesparsers;
import nl.wldelft.util.io.LineReader;
import nl.wldelft.util.io.TextParser;
import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;
import org.apache.log4j.Logger;
import java.io.IOException;
public class CampbellTimeSeriesParser implements TextParser<TimeSeriesContentHandler> {
private int columnCount = 0;
private LineReader reader = null;
private TimeSeriesContentHandler contentHandler = null;
private boolean skipValues[] = null;
private static final Logger log = Logger.getLogger(CampbellTimeSeriesParser.class);
@Override
public void parse(LineReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws IOException {
this.contentHandler = contentHandler;
this.contentHandler.addMissingValue("NAN");
this.reader = reader;
parseHeader();
for (String[] buffer = new String[columnCount]; this.reader.readLine(',', '\"', buffer) != -1; ) {
this.contentHandler.setTime(this.contentHandler.getDefaultTimeZone(), "yyyy-MM-dd HH:mm:ss", buffer[0]);
for (int i = 1; i < columnCount; i++) {
if (!skipValues[i]) {
this.contentHandler.setTimeSeriesHeader(i);
String valueText = buffer[i];
this.contentHandler.setValue('.', valueText);
this.contentHandler.applyCurrentFields();
}
}
}
}
// The first few lines contain vital information about the file:
// - Whether the separator character is a , or a ;
// - The names of the parameters and locations
private void parseHeader() throws IOException {
String[] headerLine1 = reader.readLine(',');
String[] parameterIdsLine = reader.readLine(',');
String[] unitLine = reader.readLine(',');
String[] qualifierLine = reader.readLine(',');
skipValues = new boolean[qualifierLine.length];
columnCount = parameterIdsLine.length;
DefaultTimeSeriesHeader header = new DefaultTimeSeriesHeader();
header.setLocationId(headerLine1[1]);
if (header.getLocationId() == null) {
throw new IOException("Location id is missing for first header line ");
}
if (!"TIMESTAMP".equals(parameterIdsLine[0])) {
throw new IOException("First parameter is not TIMESTAMP");
}
for (int i = 1; i < columnCount; i++) {
header.setParameterId(parameterIdsLine[i]);
header.setUnit(unitLine[i]);
if ("TMx".equals(qualifierLine[i]) || "TMn".equals(qualifierLine[i])) {
skipValues[i] = true;
log.warn("parameter " + parameterIdsLine[i] + " will be skipped because of unknown use");
} else {
header.setQualifierIds(qualifierLine[i]);
}
if (header.getParameterId() == null) {
throw new IOException("Parameter id is missing for column " + (i + 1));
}
contentHandler.createTimeSeriesHeaderAlias(i, header);
}
}
}