/** * TimeSeries reader for NTURAIN Datalogger files. * <p> * These contain observed telemetry data for Rain * <p> * * The locationID is encoded in the filename e.g: MC02_Rain.dat contains data for * locationId MC02 * * <pre> * Colums are: * Date/time number p(mm) * * Example: * "2007-04-30 00:00:00",5594,0 * "2007-04-30 00:05:00",5595,0 * "2007-04-30 00:10:00",5596,0 * "2007-04-30 00:15:00",5597,0 * "2007-04-30 00:20:00",5598,0 * </pre> * The second column (data number) is not used in the import * <p> */ public class NtuRainTimeSeriesParser implements TextParser<TimeSeriesContentHandler> { private static final Logger log = Logger.getLogger(NTURAINTimeSeriesParser.class); private String virtualFileName = null; private TimeSeriesContentHandler contentHandler = null; @Override public void parse(LineReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception { this.virtualFileName = virtualFileName; this.contentHandler = contentHandler; reader.setCommentLinePrefix('#'); parseParameterLocationIdFromFileName(); if (contentHandler.isCurrentTimeSeriesHeaderForAllTimesRejected()) return; for (String[] buffer = new String[2]; reader.readLine(',', buffer) != -1;) { contentHandler.setTime(contentHandler.getDefaultTimeZone(), "yyyy-MM-dd HH:mm:ss", buffer[0]); contentHandler.setValue('.', buffer[2]); contentHandler.applyCurrentFields(); } } private void parseParameterLocationIdFromFileName() throws IOException { /* parsefile name This is locationid and type concanated e.g. MC02_Rain.dat we only need the locationID */ String fileName = FileUtils.getNameWithoutExt(virtualFileName); /* Check if filename contains the file name seperator */ String[] fileNameParts = TextUtils.split(fileName, '_'); if (fileNameParts.length < 2) throw new IOException("File with name <" + this.virtualFileName + "> cannot be parsed to find location Id"); DefaultTimeSeriesHeader timeSeriesHeader = new DefaultTimeSeriesHeader(); // spit on underscore - first items are the externalLocatioId, second Parameter (Rain) timeSeriesHeader.setLocationId(fileNameParts[0]); if (!fileNameParts[1].equals("Rain")) { log.warn("File <" + fileName + "> contains data for external parameter <" + fileNameParts[1] + "> Forcing to Rain"); } // Set to "Rain" Anyway after sending the warning timeSeriesHeader.setParameterId("Rain"); if (log.isDebugEnabled()) { log.debug("File <" + fileName + "> contains data for external locationdId <" + fileNameParts[0] + "> and parameter <" + fileNameParts[1] + '>'); } contentHandler.setNewTimeSeriesHeader(timeSeriesHeader); } }