I have a list of objects, which contains field rawData
(eg rawData="KLAX 141353Z 06003KT 6SM BR FEW002 FEW008 BKN280 12/12 A3007 RMK AO2 SLP182 T01170117 $"). In my case rawData is METAR report from which i want to extract the most severe weather condition (clouds, fog, rain, snow).
Currently I am applying parseWeather
to each object of my list.
METAR string can contain multiple instance of same tag e.g. (BRN065 BRN033), it even can contain all of them in same time . To my output - I want to display "simplified" worst weather condition, where the worst, for the current task, is considered as fog, then snow, then other weather conditions.
To answer why i return same type as argument - initial argument have some pre parsed detailed report from FlightAware service. However I have different requirenment for simplified report, than provided. Thus function parseWeather
is a side effect function which fills extra fields.
private PollFlightResponse.ResultEntity.MetarEntity parseWeather(
PollFlightResponse.ResultEntity.MetarEntity d) {
int severity = 0; //how bad is weather from 0 to 2
String wCode = ""; // map key
String weather = ""; // human readable weather
if (d.getRawData().contains("FG")) {
wCode = "FG";
} else if (d.getRawData().contains("+SN")) {
wCode = "+SN";
} else if (d.getRawData().contains("SN")) {
wCode = "SN";
} else if (d.getRawData().contains("-SN")) {
wCode = "-SN";
} else if (d.getRawData().contains("+RA")) {
wCode = "+RA";
} else if (d.getRawData().contains("-RA")) {
wCode = "-RA";
} else if (d.getRawData().contains("RA")) {
wCode = "RA";
} else if (d.getRawData().contains("VV")) {
wCode = "VV";
} else if (d.getRawData().contains("OVC")) {
wCode = "OVC";
} else if (d.getRawData().contains("BKN")) {
wCode = "BKN";
} else if (d.getRawData().contains("SCT")) {
wCode = "SCT";
} else if (d.getRawData().contains("FEW")) {
wCode = "FEW";
} else if (d.getRawData().contains("NSC")) {
wCode = "NSC";
} else if (d.getRawData().contains("CLR")) {
wCode = "CLR";
}
if (TextUtils.isEmpty(wCode)) {
wCode = "CLR";
}
weather = SearchActivity.conditions.get(wCode);
severity = SearchActivity.severity.get(wCode);
if (d.getRawData().contains("MPS")) {
int index = d.getRawData().indexOf("MPS");
String wind = d.getRawData().substring(index - 2, index);
int s = Integer.parseInt(wind);
if (s == 0) {
//no wind does not affect results
} else if (s < 3) {
weather += ", Light wind";
} else if (s < 10) {
weather += ", Windy";
if (severity < 1) severity = 1;
} else {
weather += ", Strong wind";
if (severity < 1) severity = 2;
}
}
d.setGeneraWeather(weather);
d.setWeatherSeverity(severity);
return d;
}
where conditions and severity is static public hashmaps
private void generateWeatherMap() {
conditions.clear();
severity.clear();
//Strings
conditions.put("FG", "Fog");
conditions.put("+SN", "Heavy snow");
conditions.put("SN", "Snow");
conditions.put("-SN", "Light snow");
conditions.put("+RA", "Heavy rain");
conditions.put("RA", "Rain");
conditions.put("-RA", "Light rain");
conditions.put("CLR", "Clear sky");
conditions.put("NSC", "Little clouds");
conditions.put("FEW", "Few clouds");
conditions.put("SCT", "Scattered clouds");
conditions.put("BKN", "Broken clouds");
conditions.put("OVC", "Overcast");
conditions.put("VV", "Heavy precipitation");
//Values
severity.put("FG", 2);
severity.put("+SN", 2);
severity.put("SN", 1);
severity.put("-SN", 1);
severity.put("+RA", 1);
severity.put("RA", 1);
severity.put("-RA", 1);
severity.put("CLR", 0);
severity.put("NSC", 0);
severity.put("FEW", 0);
severity.put("SCT", 1);
severity.put("BKN", 1);
severity.put("OVC", 2);
severity.put("VV", 2);
}
"SN-"
. \$\endgroup\$