Merge "Omaha #5265 Make MetarSeparator safer and cleaner." into omaha_16.2.2

Former-commit-id: 3366f6b67e3d54c013fec578ff52e443ceb5a21b
This commit is contained in:
Nate Jensen 2016-02-01 12:06:13 -06:00 committed by Gerrit Code Review
commit 3d2ee72702

View file

@ -1,28 +1,25 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.edex.plugin.obs.metar;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -42,36 +39,28 @@ import com.raytheon.uf.common.util.StringUtil;
import com.raytheon.uf.common.wmo.WMOHeader;
/**
*
*
* Separator implementation for metar record
*
* Separator implementation for METAR/SPECI files.
*
* <pre>
*
*
*
*
* SOFTWARE HISTORY
*
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul ??, 2006 3 &amp; 14 Phillippe Initial Creation
* Nov 14, 2006 71 Rockwood Implemented filter for NIL observations
* Nov 14, 2006 71 Rockwood Implemented filter for NIL observations
* Apr 14, 2008 1093 jkorman Added filter for Alaskan &quot;Airways&quot;
* observations.
* May 14, 2014 2536 bclement moved WMO Header to common, removed unused HEADERREGEX
* Oct 02, 2014 3693 mapeters Changed pattern String constants to Pattern constants.
* Dec 15, 2015 5166 kbisanz Update logging to use SLF4J
* Jan 29, 2016 5265 nabowle Update type replaceAll. General cleanup.
* </pre>
*
*
* @author bphillip
* @version 1
*/
/**
* Implementation of file separator for METAR/SPECI files
*
* @author bphillip
*
*/
public class MetarSeparator extends AbstractRecordSeparator {
private final Logger theLogger = LoggerFactory.getLogger(getClass());
@ -83,6 +72,7 @@ public class MetarSeparator extends AbstractRecordSeparator {
/** Regex used for determining metar type */
private static final Pattern METARTYPE = Pattern.compile("METAR|SPECI");
/** Regex to check for Alaskan Airways observations. */
private static final Pattern AIRWAYS = Pattern
.compile("[A-Z][A-Z,0-9]{3} (SP|SA) \\d{4} AWOS");
@ -98,6 +88,7 @@ public class MetarSeparator extends AbstractRecordSeparator {
/** List of record bodies contained in file */
private List<String> records;
/** Iterator over the records. */
private Iterator<String> iterator = null;
public MetarSeparator() {
@ -113,29 +104,19 @@ public class MetarSeparator extends AbstractRecordSeparator {
/**
* Get the WMO Header found within this data.
*
*
* @return The message WMO Header.
*/
public WMOHeader getWMOHeader() {
return header;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.edex.plugin.AbstractRecordSeparator#setData(byte[])
*/
@Override
public void setData(byte[] data, Headers headers) {
this.doSeparate(new String(data));
iterator = records.iterator();
}
/*
* (non-Javadoc)
*
* @see com.raytheon.edex.plugin.AbstractRecordSeparator#nextRecord()
*/
public boolean hasNext() {
if (iterator == null) {
return false;
@ -144,11 +125,6 @@ public class MetarSeparator extends AbstractRecordSeparator {
}
}
/*
* (non-Javadoc)
*
* @see java.util.Iterator#next()
*/
public byte[] next() {
try {
String temp = iterator.next();
@ -163,8 +139,10 @@ public class MetarSeparator extends AbstractRecordSeparator {
}
/**
*
* Separates the composite message into the individual records.
*
* @param message
* The message.
*/
private void doSeparate(String message) {
@ -177,28 +155,23 @@ public class MetarSeparator extends AbstractRecordSeparator {
header = wmoHeader;
}
// Pattern pattern = Pattern.compile(HEADERREGEX);
// Matcher matcher = pattern.matcher(message);
//
// if (matcher.find()) {
// header = matcher.group();
// }
// Determines the type
Matcher matcher = METARTYPE.matcher(message);
if (matcher.find()) {
type = matcher.group();
message = matcher.replaceAll("");
} else {
type = "METAR";
}
message = message.replaceAll(type, "");
matcher = ICAODATEPAIR.matcher(message);
List<Integer> bodyIndex = new ArrayList<Integer>();
Map<String, String> bodyMap = new HashMap<String, String>();
// Extracts all the matches out of the message. Looks for ICAO/date
// pairs. Does not allow duplicate entries.
/*
* Extracts all the matches out of the message. Looks for ICAO/date
* pairs. Does not allow duplicate entries.
*/
if (matcher.find()) {
bodyIndex.add(matcher.start());
String obsKey = matcher.group();
@ -228,9 +201,11 @@ public class MetarSeparator extends AbstractRecordSeparator {
// If it exists in the map then keep this observation.
if (bodyMap.containsKey(obsKey)) {
bodyRecords.add(observation);
// now that we have data for this key,
// remove it from the map so we skip any
// subsequent observations with the same key.
/*
* now that we have data for this key, remove it from
* the map so we skip any subsequent observations with
* the same key.
*/
bodyMap.remove(obsKey);
}
}
@ -239,10 +214,11 @@ public class MetarSeparator extends AbstractRecordSeparator {
// Perform a some more checks on the data.
for (int i = 0; i < bodyRecords.size(); i++) {
String observation = bodyRecords.get(i);
// 20080418 - 1093
// Check for old style AIRWAYS data from Alaskan stations. This
// data will be at the end of valid METAR/SPECI data so just
// remove it.
/*
* 20080418 - 1093 Check for old style AIRWAYS data from Alaskan
* stations. This data will be at the end of valid METAR/SPECI
* data so just remove it.
*/
matcher = AIRWAYS.matcher(observation);
if (matcher.find()) {
observation = observation.substring(0, matcher.start());
@ -257,46 +233,8 @@ public class MetarSeparator extends AbstractRecordSeparator {
}
}
} catch (Exception e) {
e.printStackTrace();
theLogger.warn("No valid METAR records found.");
theLogger.warn("Invalid METAR message received.", e);
}
return;
}
/**
* Test function
*
* @param args
*/
public static void main(String[] args) {
String CRCRLF = "\r\r\n";
String fileName = "/common/jkorman/data_store/obs/"
+ "SAUS70_KWBC_241218.obs";
String str;
MetarSeparator ben = new MetarSeparator();
System.out.println(fileName);
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
while ((str = in.readLine()) != null) {
sb.append(CRCRLF);
sb.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
ben.setData(sb.toString().getBytes(), null);
int i = 0;
while (ben.hasNext()) {
byte[] tData = ben.next();
System.out.println("Record # " + (++i) + " [" + tData.length
+ "]: \n{" + new String(tData) + "}");
}
}
}