private static IEnumerable <WeatherIncident> ParseWundergroundResponse(string airportCode, string responseString, DateTime date, WeatherIncidentType?typeToFilter) { var moreInfoUrl = new Uri(string.Format(WeatherHistoryUrl, airportCode, date.Year, date.Month, date.Day)); Airport currentAirpot = AirportList.GetAirport(airportCode); var locationOfAirport = Address.Search(string.Empty, currentAirpot.City, currentAirpot.State.Abbreviation, string.Empty, false); // Remove HTML comments and line breaks responseString = Regex.Replace(responseString, "<!--.*?-->", string.Empty, RegexOptions.Singleline).Replace("<br />", ""); IEnumerable <string> rows = responseString.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); rows = rows.Skip(1); // Skip header row var incidents = new List <WeatherIncident>(); foreach (string row in rows) { var entry = new WeatherUndergroundEntry(date, row); WeatherIncidentType incidentType = WeatherIncidentClassifier.Classify(entry); if (typeToFilter != null) { if (incidentType == typeToFilter) { incidents.Add(new WeatherIncident(locationOfAirport, incidentType, date, moreInfoUrl)); } } else { if (incidentType != WeatherIncidentType.Unclassified) { incidents.Add(new WeatherIncident(locationOfAirport, incidentType, date, moreInfoUrl)); } } } return(incidents); }
private static IEnumerable<WeatherIncident> ParseWundergroundResponse(string airportCode, string responseString, DateTime date, WeatherIncidentType? typeToFilter) { var moreInfoUrl = new Uri(string.Format(WeatherHistoryUrl, airportCode, date.Year, date.Month, date.Day)); Airport currentAirpot = AirportList.GetAirport(airportCode); var locationOfAirport = Address.Search(string.Empty, currentAirpot.City, currentAirpot.State.Abbreviation, string.Empty, false); // Remove HTML comments and line breaks responseString = Regex.Replace(responseString, "<!--.*?-->", string.Empty, RegexOptions.Singleline).Replace("<br />", ""); IEnumerable<string> rows = responseString.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); rows = rows.Skip(1); // Skip header row var incidents = new List<WeatherIncident>(); foreach (string row in rows) { var entry = new WeatherUndergroundEntry(date, row); WeatherIncidentType incidentType = WeatherIncidentClassifier.Classify(entry); if (typeToFilter != null) { if (incidentType == typeToFilter) { incidents.Add(new WeatherIncident(locationOfAirport, incidentType, date, moreInfoUrl)); } } else { if (incidentType != WeatherIncidentType.Unclassified) { incidents.Add(new WeatherIncident(locationOfAirport, incidentType, date, moreInfoUrl)); } } } return incidents; }
/// <summary> /// Classifies an entry of weather underground data as a weather event. /// </summary> /// <param name="entry"></param> /// <returns>The corresponding Weather Incident Type</returns> public static WeatherIncidentType Classify(WeatherUndergroundEntry entry) { // TODO: Document these rules // TODO: Generalize these rules to be used with other web services if (entry.Precipitation > 0.5) { if (entry.Temperature >= 32.0) { return WeatherIncidentType.Flood; } return WeatherIncidentType.WinterStorm; } if (entry.WindSpeed > 57) // approximately equal to 50 knots { return WeatherIncidentType.Wind; } if( entry.Conditions.Contains("hail") ) { return WeatherIncidentType.Hail; } return WeatherIncidentType.Unclassified; }