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