package nl.wldelft.aquarius.ratingcurveparsers;
/* ================================================================
* Delft FEWS
* ================================================================
*
* Project Info: http://www.wldelft.nl/soft/fews/index.html
* Project Lead: Karel Heynert (karel.heynert@wldelft.nl)
*
* (C) Copyright 2003, by WL | Delft Hydraulics
* P.O. Box 177
* 2600 MH Delft
* The Netherlands
* http://www.wldelft.nl
*
* DELFT-FEWS is a sophisticated collection of modules designed
* for building a FEWS customised to the specific requirements
* of individual agencies. An open modelling approach allows users
* to add their own modules in an efficient way.
*
* ----------------------------------------------------------------
* AquariusRatingCurveSoapServerParser
* ----------------------------------------------------------------
* (C) Copyright 2003, by WL | Delft Hydraulics
*
* Original Author: rooij_e
* Contributor(s):
*
* Changes:
* --------
* 2/27/14 : Version 1 ();
*
*
*/
import nl.wldelft.aquarius.CsvRatingTableParser;
import nl.wldelft.aquarius.util.AquariusSoapUtils;
import nl.wldelft.util.Period;
import nl.wldelft.util.PeriodConsumer;
import nl.wldelft.util.TextUtils;
import nl.wldelft.util.io.LineReader;
import nl.wldelft.util.io.ServerParser;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;
import nl.wldelft.util.timeseries.TimeSeriesHeader;
import nl.wldelft.util.timeseries.TimeSeriesHeadersConsumer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
public class AquariusRatingCurveSoapServerParser implements ServerParser<TimeSeriesContentHandler>, TimeSeriesHeadersConsumer, PeriodConsumer {
private static final Logger log = LogManager.getLogger();
private TimeSeriesHeader[] headers = null;
private Period searchPeriod = Period.ANY_TIME;
@Override
public void parse(URL url, String username, String password, TimeSeriesContentHandler timeSeriesContentHandler) throws Exception {
if (headers == null || headers.length == 0) {
throw new Exception("No wanted parameters and locations found. Configure these in the id map of"
+ " your import.");
}
String authToken = AquariusSoapUtils.getAuthToke(url, username, password);
for (TimeSeriesHeader header : headers) {
if (header.getQualifierCount() != 3) {
log.warn("Incorrect RatingCurve IdMap configuration for location=%s! " +
"Please configure IdMap as follows: externalLocation=locationId, externalQualifier='input parameter', externalQualifier1='output parameter' and externalQualifier2=['StageDischarge' or 'DischargeStage'] ",
header.getLocationId());
continue;
}
try {
retrieveRatingCurves(timeSeriesContentHandler, url, header, authToken);
log.debug("Successfully parsed ratingcurves for header %s", header);
} catch (Exception e) {
log.warn("Error importing ratingcurvers for header %s: %s", header, e.getMessage());
}
}
}
private void retrieveRatingCurves(TimeSeriesContentHandler timeSeriesContentHandler, URL url,
TimeSeriesHeader header, String authToken) throws Exception {
/** Set destination header for returned data **/
timeSeriesContentHandler.setTimeSeriesHeader(header);
/** Read data for given dataset id **/
String csvRatingTable = AquariusSoapUtils.getRatingCurveTable(url, header.getLocationId(), header.getQualifierId(0),
header.getQualifierId(1), authToken);
csvRatingTable = TextUtils.trimToNull(csvRatingTable);
if (csvRatingTable == null) return;
CsvRatingTableParser csvRatingTableParser = new CsvRatingTableParser(searchPeriod);
csvRatingTableParser.setRatingCurveType(CsvRatingTableParser.RatingCurveType.valueOf(header.getQualifierId(2)));
LineReader lineReader = new LineReader(new StringReader(csvRatingTable), "ratingTable.csv");
try {
csvRatingTableParser.parse(lineReader, "ratingTable.csv", timeSeriesContentHandler);
} catch (IOException e) {
throw new IOException(e.getMessage() + csvRatingTable);
}
}
@Override
public void setTimeSeriesHeaders(TimeSeriesHeader[] timeSeriesHeaders) {
this.headers = timeSeriesHeaders;
}
@Override
public void setPeriod(Period period) {
this.searchPeriod = period;
}
}
|