package nl.wldelft.timeseriesparsers;
import nl.wldelft.util.FastDateFormat;
import nl.wldelft.util.Properties;
import nl.wldelft.util.TextUtils;
import nl.wldelft.util.TimeZoneUtils;
import nl.wldelft.util.io.XmlParser;
import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;
import org.apache.log4j.Logger;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.ListDate;
/**
*
* @autor pelgrim
* @author ekkelenk
* import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
/**
*
* @autor pelgrim
* @author ekkelenk
* Date: 8/9/13
* Time: 9:35 AM
* Import the warnings from the AifsML format
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public class AifsMLTimeSeriesParser implements XmlParser<TimeSeriesContentHandler> {
private publicfinal static finalMap<String, String HYFS_MARKER[]> PART_OF_DAY_MAPPINGS = "[HYFS]";new HashMap<>();
private// TimeSeriesContentHandlerProperty contentHandlerkeys
= null;
public static privatefinal XMLStreamReader readerString WARNING_NEXT_ISSUE = null;
"Warning_Next_Issue"; private
DefaultTimeSeriesHeader header = null;
public static final //Use for all
private String title = nullString PREDICTION_LEVEL_TYPE = "Prediction_Level_Type";
public static final String WARNING_LIKELIHOOD = "Warning_Likelihood";
privatepublic String identifierstatic final String HYDROGRAPH_TYPE = null"Hydrograph_Type";
public static privatefinal String externalForecastTimeTARGET_TIME_TYPE = null;
"Target_Time_Type";
@Override
public voidstatic parse(XMLStreamReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception {final String PART_DAY_VALUE = "Part_Day_Value";
public static final this.readerString PREDICTION_LEVEL = reader"Prediction_Level";
public static final this.contentHandlerString WARNING_PREDICTION_TEXT = contentHandler;
"Warning_Prediction_Text";
public static final String headerWARNING_TIME_ZONE = new DefaultTimeSeriesHeader()"Warning_Timezone";
public static final reader.require(XMLStreamConstants.START_DOCUMENT, null, null)String WARNING_AREA_TYPE = "river-basin";
public static final String WARNING_ID reader.nextTag()= "Warning_Id";
// target time reader.require(XMLStreamConstants.START_ELEMENT, null, "product")types
public static final String TARGET_TIME_TYPE_DATE_TIME = "DateTime";
public static final parseIdentifier();
String TARGET_TIME_TYPE_DAY = "Day";
public static final if (!XmlStreamReaderUtils.goTo(reader, "warning")) {String TARGET_TIME_TYPE_PART_DAY = "PartDay";
// target time values
// There has to be a warning element.
public static final String TARGET_TIME_VALUE_OVERNIGHT = "Overnight";
public static final String TARGET_TIME_VALUE_NIGHT = "Night";
public static final String TARGET_TIME_VALUE_LATE = "Late";
public static final String TARGET_TIME_VALUE_EVENING = "Evening";
public static final String TARGET_TIME_VALUE_EARLY_EVENING = "Early Evening";
public static final String TARGET_TIME_VALUE_LATE_EVENING = "Late Evening";
public static final String TARGET_TIME_VALUE_LATE_AFTERNOON = "Late Afternoon";
public static final String TARGET_TIME_VALUE_AFTERNOON = "Afternoon";
public static final String TARGET_TIME_VALUE_EARLY_AFTERNOON = "Early Afternoon";
public static final String TARGET_TIME_VALUE_LATE_MORNING = "Late Morning";
public static final String TARGET_TIME_VALUE_MORNING = "Morning";
public static final String TARGET_TIME_VALUE_EARLY = "Early";
public static final String TARGET_TIME_VALUE_EARLY_MORNING = "Early Morning";
public static final String TARGET_TIME_VALUE_DAY = "Day";
private final static String dateFormatString = "yyyy-MM-dd'T'HH:mm:ss";
private final static String dateFormatStringWithTimeZone = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final String WARNING_SEQUENCE = "Warning_Sequence";
private static FastDateFormat dateFormat = null;
static {
// The time ranges of the different par day values in HH:mm:ss format
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_EARLY_MORNING, new String[]{"00:00:00", "06:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_EARLY, new String[] {"00:00:00", "08:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_MORNING, new String[] {"00:00:00", "12:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_LATE_MORNING, new String[] {"09:00:00", "12:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_EARLY_AFTERNOON, new String[] {"12:00:00", "15:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_AFTERNOON, new String[]{"12:00:00", "18:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_LATE_AFTERNOON, new String[] {"15:00:00", "18:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_EARLY_EVENING, new String[] { "18:00:00", "21:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_EVENING, new String[] {"18:00:00", "24:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_LATE_EVENING, new String[]{"21:00:00", "24:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_LATE, new String[] {"18:00:00", "24:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_NIGHT, new String[] {"18:00:00", "24:00:00"});
PART_OF_DAY_MAPPINGS.put(TARGET_TIME_VALUE_DAY, new String[] {"00:00:00", "24:00:00"});
}
private static final String HYFS_MARKER = "[HYFS]";
private TimeSeriesContentHandler contentHandler = null;
private XMLStreamReader reader = null;
private DefaultTimeSeriesHeader header = null;
private String warningTitle = null;
private String warningNextIssue = null;
private String productIdentifier = null;
private String externalForecastTime = null;
private String warningSequence=null; // Warning sequence should be used by all area locations.
private static final Logger log = Logger.getLogger(AifsMLTimeSeriesParser.class);
@Override
public void parse(XMLStreamReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception {
clearFieldsFromPreviousParse();
this.reader = reader;
this.contentHandler = contentHandler;
header = new DefaultTimeSeriesHeader();
reader.require(XMLStreamConstants.START_DOCUMENT, null, null);
reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "product");
parseIdentifier();
parseExternalForecastTime();
if (!XmlStreamReaderUtils.goTo(reader, "warning")) {
// There has to be a warning element.
throw new IOException("Input file doesn't contain a warning element: " + virtualFileName);
}
reader.require(XMLStreamConstants.START_ELEMENT, null, "warning");
reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "warning-info");
parseWarningInfo();
parseWarningAreas();
}
private void clearFieldsFromPreviousParse() {
warningTitle = null;
warningNextIssue = null;
productIdentifier = null;
externalForecastTime = null;
}
/**
* Retrieve the identifier from the source element
*
* @throws XMLStreamException;
*/
private void parseIdentifier() throws XMLStreamException {
XmlStreamReaderUtils.goTo(reader, "identifier");
if (TextUtils.equals(reader.getLocalName(), "identifier")) {
this.productIdentifier = reader.getElementText();
}
}
private void parseExternalForecastTime() throws XMLStreamException {
XmlStreamReaderUtils.goTo(reader, "issue-time-utc");
if (TextUtils.equals(reader.getLocalName(), "issue-time-utc")) {
this.externalForecastTime = reader.getElementText();
}
}
/**
* filter the warning sequence and warning title text attributes from the text elements.
* they will be used for all areas.
*
* @throws XMLStreamException
*/
private void parseWarningInfo() throws XMLStreamException {
do {
if (!XmlStreamReaderUtils.goTo(reader, "text", "warning-info")) {
// no more text areas, return.
return;
}
if (this.warningTitle == null || warningNextIssue == null) {
String type = reader.getAttributeValue(null, "type");
if (TextUtils.equals("warning_title", type)) {
this.warningTitle = reader.getElementText();
}
if (TextUtils.equals("warning_next_issue", type)) {
this.warningNextIssue = reader.getElementText();
}
}
if (this.warningTitle != null && this.warningNextIssue != null) {
// we're finished with the text elements, skip to the areas
return;
}
} while (reader.hasNext());
}
/**
* Retrieve the locationId from the area element(s) and call separate method to parse forecast data
*
* @throws XMLStreamException;
*/
private void parseWarningAreas() throws XMLStreamException {
// parse all areas inside a warning element.
warningSequence = null; // Warning sequence should be used by all area locations.
do {
if (!XmlStreamReaderUtils.goTo(reader, "area", "warning")) {
// alle area's have been processed or an end tag warning has been found.
return;
}
String locationId = reader.getAttributeValue(null, "aac");
String areaType = reader.getAttributeValue(null, "type");
// an area can have more that one fore-cast periods.
parseForecastData(locationId, areaType);
} while (reader.hasNext());
}
/**
* helper class to store forecast data per area.
*/
private static class ForecastData {
private Properties.Builder propertiesBuilder = new Properties.Builder();
private String parameter = null;
private String value = null;
private String localTime = null;
private String comment = null;
private Long timeRangeTime = null;
private Long timeRangeStart = null;
private Long timeRangeEnd = null;
private String valueRangeStart = null;
private String valueRangeEnd = null;
}
/**
* This method skips elements until a start element with the corresponding local name is found and returns true,
* If an end tag with name endName is found, return false.
* Otherwise returns false
* Warning: if the element is not found and the end tag is not found, the reader is skipped until the end of the document
*
* @param reader the xml reader
* @param localName local name to look for
* @param endName local name of end tag to find.
* @return boolean return true if localName cannot be found or the endName end tag has been found.
* @throws XMLStreamException
*/
private static boolean goTo(XMLStreamReader reader, Set<String> localNameSet, String endName) throws XMLStreamException {
do {
if (!reader.hasNext()) return false;
if (reader.isEndElement() && TextUtils.equals(reader.getLocalName(), endName)) return false;
reader.next();
} while (!reader.isStartElement() || !localNameSet.contains(reader.getLocalName()) );
return true;
}
/**
* Retrieve the variable, value, unit, time and comment from the forecast data element(s)
* Retrieve comment text from forecast text element
* return true if more forecast periods are available
*
* @throws XMLStreamException;
*/
private void parseForecastData(String locationId, String areaType) throws XMLStreamException {
// not every area has a forecast-period. Try to find the next forecast-period,
// but stop if not found, or an end-tag area has been found.
Set<String> forecastPeriodSubElements = new HashSet<>();
// We're interested in text end element sub elements inside the forecast period.
boolean isRiverBasin = WARNING_AREA_TYPE.equals(areaType);
forecastPeriodSubElements.add("text");
forecastPeriodSubElements.add("element");
List<ForecastData> forecastDataList = new ArrayList<>();
String warningPrediction = null; // There is only one warningPrediction per area.
while (XmlStreamReaderUtils.goTo(reader, "forecast-period", "area")) {
String predictionLevelValue = null;
String commentText = null;
String predictionLowerValueString = null;
String predictionUpperValueString = null;
String time = null;
String targetTimeType = null;
String partDayValueString = null;
Properties.Builder propertiesBuilder = new Properties.Builder();
// inside the forecast-period we need to process the text elements first.
do {
// process all elements until we reach the end tag forecast-period.
if (!goTo(reader, forecastPeriodSubElements, "forecast-period")) {
// break so we can write this forecast as a timeseries.
break;
}
String elementName = reader.getLocalName();
String type = reader.getAttributeValue(null, "type");
if (isRiverBasin && TextUtils.equals("element", elementName) && TextUtils.equals("warning_sequence", type)) warningSequence = reader.getElementText();
if (TextUtils.equals("text", elementName) && TextUtils.equals("warning_prediction", type)) warningPrediction = reader.getElementText();
if (TextUtils.equals("text", elementName) && TextUtils.equals("river_level_prediction", type) ) {
String riverLevelPredictionText = reader.getElementText();
int index = riverLevelPredictionText.indexOf(HYFS_MARKER);
if (index != -1) {
String levelPredictionPart = riverLevelPredictionText.substring(0, index);
//noinspection StringConcatenationMissingWhitespace
String fieldSeparatorRiverLevelPrediction = "\\[FFWDEL" +
"IM]";
String[] splits = levelPredictionPart.split(fieldSeparatorRiverLevelPrediction);
if (splits.length > 0 && splits[0] != null && !splits[0].isEmpty()) {
propertiesBuilder.addString(PREDICTION_LEVEL_TYPE, splits[0].trim());
}
if (splits.length > 2 && splits[2] != null) {
if (!splits[2].isEmpty() && !"##".equals(splits[2])) {
try {
Float.parseFloat(splits[2]);
predictionLowerValueString = splits[2];
} catch (NumberFormatException nfe) {
predictionLowerValueString = null;
}
}
}
if (splits.length > 3 && splits[3] != null) {
if (!splits[3].isEmpty() && !"##".equals(splits[3])) {
try {
Float.parseFloat(splits[3]);
predictionUpperValueString = splits[3];
} catch (NumberFormatException nfe) {
predictionUpperValueString = null;
}
}
}
if (splits.length >4 && splits[4] != null && !splits[4].isEmpty()) {
propertiesBuilder.addString(PREDICTION_LEVEL, splits[4].trim());
}
String valueComment = riverLevelPredictionText.substring(index + HYFS_MARKER.length());
int split = valueComment.indexOf(",");
if (split != -1) {
predictionLevelValue = valueComment.substring(0, split);
commentText = valueComment.substring(split + 1);
try {
Float.parseFloat(predictionLevelValue);
} catch (NumberFormatException nfe) {
log.warn("Import.Warn: Unparsable prediction level value in river_level_prediction: " + predictionLevelValue);
predictionLevelValue = null;
}
if (warningPrediction != null) {
propertiesBuilder.addString(WARNING_PREDICTION_TEXT, warningPrediction.trim());
}
}
if (productIdentifier != null) {
propertiesBuilder.addString(WARNING_ID, productIdentifier);
}
}
} else if (TextUtils.equals("text", elementName) && TextUtils.equals("hydrograph_type", type)) {
String hydroGraphType = reader.getElementText();
propertiesBuilder.addString(HYDROGRAPH_TYPE, hydroGraphType);
} else if (TextUtils.equals("element", elementName) && TextUtils.equals("warning_likelihood", type)) {
String warningLikelihood = reader.getElementText();
propertiesBuilder.addString(WARNING_LIKELIHOOD, warningLikelihood);
} else if (TextUtils.equals("element", elementName) && TextUtils.equals("time", type)) {
String timeString = reader.getElementText();
int index = timeString.indexOf(HYFS_MARKER);
if (index != -1) {
String timeStringSeparator = "\\[FFWDELIM]";
String timeStringPart = timeString.substring(0, index);
String[] splits = timeStringPart.split(timeStringSeparator);
if (splits.length > 0 && splits[0] != null && !splits[0].isEmpty()) {
propertiesBuilder.addString(TARGET_TIME_TYPE, splits[0]);
throw new IOException("Input file doesn't contain a warning element: " + virtualFileName);
targetTimeType = splits[0];
}
}
if reader.require(XMLStreamConstants.START_ELEMENT, null, "warning");
reader.nextTag();
(targetTimeType != null && targetTimeType.equals("PartDay") && splits.length > 4 && splits[4] != null && !splits[4].isEmpty()) {
reader.require(XMLStreamConstants.START_ELEMENT, null, "warning-info");
parseWarningInfo();
parseWarningAreas(propertiesBuilder.addString(PART_DAY_VALUE, splits[4]);
}
/**
* Retrieve the identifier from the source element
*
partDayValueString * @throws XMLStreamException= splits[4];
*/
private void parseIdentifier() throws XMLStreamException {
XmlStreamReaderUtils.goTo(reader, "identifier");
}
if (TextUtils.equals(reader.getLocalName(), "identifier")) {
time = thistimeString.identifiersubstring(index =+ readerHYFS_MARKER.getElementTextlength());
}
}
/**
}
* filter the warning sequence and warning title text attributes from the text elements.
* they// willwe befound used for all areas.
a river_level_prediction.
*
* @throws XMLStreamException
*/
if (time != privatenull void parseWarningInfo() throws XMLStreamException&& predictionLevelValue != null) {
do {
if (!XmlStreamReaderUtils.goTo(reader, "text", "warning-info")) {
warningSequence != null) propertiesBuilder.addString(WARNING_SEQUENCE, warningSequence);
// no more text areas, return.
if (validWarningNextIssue(warningNextIssue)) propertiesBuilder.addString(WARNING_NEXT_ISSUE, warningNextIssue);
return propertiesBuilder.addString(WARNING_TIME_ZONE, time.substring(19));
}
//List<Long> wetimeRange only need the the warning title.= getTimeRange(time, targetTimeType, partDayValueString);
if (this.title == null) {
ForecastData forecastData = String type = reader.getAttributeValue(null, "type"new ForecastData();
if (TextUtils.equals("warning_title", type)) {
forecastData.comment = commentText;
this.title = reader.getElementText();
forecastData.localTime = }time;
}
forecastData.timeRangeTime if (this.title != timeRange == null) { ? null : timeRange.get(0);
// we're finished with the text elements, skip toforecastData.timeRangeStart the= areas
timeRange == null ? null : timeRange.get(1);
return;
}
forecastData.timeRangeEnd = timeRange == null }? whilenull: (readertimeRange.hasNextget(2));
}
/**
* Retrieve the locationId from the area element(s) and call separate method to parse forecast data
*
forecastData.valueRangeStart =predictionLowerValueString == null ? predictionLevelValue : predictionLowerValueString;
* @throws XMLStreamException;
*/
private void parseWarningAreas() throws XMLStreamException {
forecastData.valueRangeEnd = predictionUpperValueString == null ? predictionLevelValue: predictionUpperValueString;
// parse alle areas binnen een warning element.
do {
forecastData.value = predictionLevelValue;
if (!XmlStreamReaderUtils.goTo(reader, "area", "warning")) {
// alle area's have been processed or an end tag warning has been found.
forecastData.parameter = "prediction";
forecastData.propertiesBuilder = returnpropertiesBuilder;
}
String locationId = reader.getAttributeValue(null, "aac"forecastDataList.add(forecastData);
// an area can have more}
that one fore-cast periods.
parseForecastData(locationId);}
} while (reader.hasNext());
}
/**
* helper class to store forecast data.
/ All events have been collected
*/writeTimeSerie(locationId, forecastDataList);
private static class ForecastData {}
}
private boolean validWarningNextIssue(String parameter = null;s) {
privateif String(s value == null) return false;
privateif String("This localTimeis =a null;
final warning, no further warnings will be issued privatefor String comment = nullthis event.".equals(s)) return false;
}
/**return true;
}
* Retrieve theprivate variable,static value, unit,List<Long> determineOvernightRange(String time and comment from the forecast data element(s), int beforeNight, int afterNight) {
* Retrieve comment textlong fromrawTimeZoneOffset forecast text element= TimeZoneUtils.parseRawTimeZoneOffset(time.substring(19));
* return true iflong moredate forecast periods are available= parseTime(TimeZoneUtils.getTimeZone(rawTimeZoneOffset), dateFormatString, time);
*
Calendar cal * @throws XMLStreamException= GregorianCalendar.getInstance();
*/
private void parseForecastData(String locationId) throws XMLStreamException {cal.setTimeZone(TimeZoneUtils.getTimeZone(rawTimeZoneOffset));
// not every area has a forecast-period. Try to find the next forecast-period,cal.setTimeInMillis(date);
Date currentDate = cal.getTime();
//int buthour stop if not found, or an end-tag area has been found.= cal.get(Calendar.HOUR_OF_DAY);
cal.set(Calendar.MINUTE, 0);
List<ForecastData> forecastDataList = new ArrayList<>(cal.set(Calendar.SECOND, 0);
while (XmlStreamReaderUtils.goTo(reader, "forecast-period", "area")) {cal.set(Calendar.MILLISECOND, 0);
boolean beforeMidnight = hour <= 24 this.externalForecastTime = reader.getAttributeValue(null, "start-time-utc")&& hour > 12;
Date beforeDate;
// inside the forecast-period we need to process the text elements first. Date afterDate;
if (beforeMidnight) {
do {cal.set(Calendar.HOUR_OF_DAY, beforeNight);
beforeDate = cal.getTime();
// process all elements until we reach the end tag forecast-period. cal.add(Calendar.DATE, 1);
if (!XmlStreamReaderUtils.goTo(reader, "text", "forecast-period")) {cal.set(Calendar.HOUR_OF_DAY, afterNight);
afterDate = cal.getTime();
// break so} weelse can{
write this forecast as a timeseries.
cal.set(Calendar.HOUR_OF_DAY, afterNight);
afterDate = breakcal.getTime();
cal.add(Calendar.DATE, -1);
}
cal.set(Calendar.HOUR_OF_DAY, beforeNight);
String sequencebeforeDate = readercal.getAttributeValue(null, "type"getTime();
}
if (!TextUtilscurrentDate.equals("river_level_prediction", sequencebefore(beforeDate)) {
log.warn("Import.Warn: Overnight was configured but event time is //not weinside only want the riverovernight level prediction.
continue;
}
range of " + beforeNight + ".00-" + afterNight + ".00. The event time '" + currentDate + "' is before " + beforeDate);
// we found a river_level_prediction.
return null;
}
String value = null;
if (currentDate.after(afterDate)) {
log.warn("Import.Warn: Overnight was configured but Stringevent commenttime =is null;
not inside the overnight range of " + beforeNight + ".00-" + afterNight + ".00. The Stringevent time = null;
'" + currentDate + "' is after " + afterDate);
String riverLevelPredictionText = reader.getElementText()return null;
}
intList<Long> indexresult = new riverLevelPredictionText.indexOf(HYFS_MARKERArrayList<>();
if (index != -1) {result.add(currentDate.getTime());
result.add(beforeDate.getTime());
result.add(afterDate.getTime());
String valueComment = riverLevelPredictionText.substring(index + HYFS_MARKER.length())return result;
}
public static List<Long> getTimeRange(Date time, String targetTimeType, String partDayValueString) {
intDateFormat splitfmt = valueComment.indexOf(","FastDateFormat.getInstance(dateFormatString, TimeZoneUtils.GMT, Locale.getDefault(), null);
String timeString = fmt.format(time);
timeString if (split != -1) {
+= "+00:00";
return getTimeRange(timeString, targetTimeType, partDayValueString);
}
public static List<Long> getTimeRange(String valuetime, = valueComment.substring(0, split);
String targetTimeType, String partDayValueString) {
if (TARGET_TIME_TYPE_DATE_TIME.equals(targetTimeType)) return null; // no range
if (TARGET_TIME_TYPE_DAY.equals(targetTimeType)) return comment = valueComment.substring(split + 1);
timeRangeByHours(time, PART_OF_DAY_MAPPINGS.get(TARGET_TIME_VALUE_DAY));
time = parseTime();
//if (TARGET_TIME_TYPE_DAY.equals(targetTimeType)) return null; // no range
if }(TARGET_TIME_TYPE_PART_DAY.equals(targetTimeType)) {
if }(TARGET_TIME_VALUE_OVERNIGHT.equals(partDayValueString)) {
ifreturn determineOvernightRange(time, !=18, null6);
{
}
ForecastData forecastData = new ForecastData(return timeRangeByHours(time, PART_OF_DAY_MAPPINGS.get(partDayValueString));
}
forecastData.comment = commentreturn null;
}
@SuppressWarnings("StringConcatenationMissingWhitespace")
private static List<Long> timeRangeByHours(String time, String[] range) {
forecastData.localTime = time;
if (range == null) return null;
List<Long> ranges forecastData.value= =new valueArrayList<>();
long rawTimeZoneOffset = TimeZoneUtils.parseRawTimeZoneOffset(time.substring(19));
long forecastData.parameterdate = "prediction";
parseTime(TimeZoneUtils.getTimeZone(rawTimeZoneOffset), dateFormatString, time);
// Replace the hour part of the forecastDataList.add(forecastData);date: 2016-10-05T12:00:00+11:00
String timeRangeStart = time.substring(0, 11) + range[0] }+ time.substring(13,19);
String timeRangeEnd } while (reader.hasNext());
= time.substring(0, 11) + range[1] + time.substring(13, 19);
//long AlldateStart events have been collected= parseTime(TimeZoneUtils.getTimeZone(rawTimeZoneOffset), dateFormatString, timeRangeStart);
long dateEnd = writeTimeSerie(locationIdparseTime(TimeZoneUtils.getTimeZone(rawTimeZoneOffset), dateFormatString, forecastDataListtimeRangeEnd);
}ranges.add(date);
}
private String parseTime() throws XMLStreamException {
// if we get here, a river_level_prediction was found. No we need to parse all elements until we find the end of the forecast period
do {ranges.add(dateStart);
ranges.add(dateEnd);
return ranges;
}
private static long parseTime(TimeZone timeZone, String pattern, String dateTime) {
if (timeZone == // process all elements until we reach the end tag forecast-period.
null)
throw new IllegalArgumentException("timeZone == null");
if (!XmlStreamReaderUtils.goTo(reader, "element", "forecast-period")) {pattern == null)
throw new IllegalArgumentException("pattern // no element found
== null");
if (TextUtils.trimToNull(dateTime) == null) {
return null;
return Long.MIN_VALUE;
}
dateFormat = FastDateFormat.getInstance(pattern, String typeAttribute = reader.getAttributeValue(null, "type"timeZone, Locale.US, dateFormat);
long res;
if (TextUtils.equals("time", typeAttribute)) {
try {
String timeStringres = readerdateFormat.getElementTextparseToMillis(dateTime);
} catch (Exception e) {
int index = timeString.indexOf(HYFS_MARKER);
return Long.MIN_VALUE;
}
return res;
}
if (index != -1) {
/**
* Write a time serie header and it's events based on the forecastData.
return timeString.substring(index + HYFS_MARKER.length());
* @param locationId location of the timeserie
* @param externalForecastTime forecast time.
}
* @param forecastCollection collection of forecast events.
}*/
private void writeTimeSerie(String locationId, }Collection<ForecastData> while (reader.hasNext());forecastCollection) {
return null;//noinspection SpellCheckingInspection
}
/**
header.setForecastTime(TimeZoneUtils.GMT, dateFormatStringWithTimeZone, this.externalForecastTime);
* Write a time serie header and it's events based on the forecastData.
* @param locationId location of the timeserie
.setLocationId(locationId);
for (ForecastData forecastData : forecastCollection) {
* @paramlong externalForecastTimerawTimeZoneOffset forecast time.= TimeZoneUtils.parseRawTimeZoneOffset(forecastData.localTime.substring(19));
* @param forecastCollection collection of forecast events.
String sequenceText = */"";
private void writeTimeSerie(String locationId, Collection<ForecastData> forecastCollection) {
if (warningSequence != null && !warningSequence.isEmpty()) //noinspection SpellCheckingInspectionStringConcatenationInLoop
header.setForecastTime(TimeZoneUtils.GMT, "yyyy-MM-dd'T'HH:mm:ss'Z'", this.externalForecastTime);
sequenceText += " header.setLocationId(locationId);
(ws " + warningSequence + ")"; for (ForecastData forecastData : forecastCollection) {
contentHandler.setComment(identifierproductIdentifier + sequenceText + ": " + warningTitle + title' '+ '\n' + forecastData.comment);
contentHandler.setProperties(forecastData.propertiesBuilder.build());
header.setParameterId(forecastData.parameter);
contentHandler.setValuesetValueAndRange('.', forecastData.value);
, forecastData.valueRangeStart, forecastData.valueRangeEnd);
if (forecastData.timeRangeTime != null) {
// headercontentHandler.setUnit(setTimeAndRange(forecastData.timeRangeTime, forecastData.timeRangeStart, forecastData.unittimeRangeEnd);
} longelse rawTimeZoneOffset{
= TimeZoneUtils.parseRawTimeZoneOffset(forecastData.localTime.substring(forecastData.localTime.length() - 6));
contentHandler.setTime(TimeZoneUtils.getTimeZone(rawTimeZoneOffset), "yyyy-MM-dd'T'HH:mm:ss"dateFormatString, forecastData.localTime.substring(0, forecastData.localTime.length() - 6));19));
}
contentHandler.setTimeSeriesHeader(header);
contentHandler.applyCurrentFields();
}
}
}
|