public void ImportStationsAsync() { ThreadPool.QueueUserWorkItem((e) => { var url = Settings.StationIndexUrl; var client = new WebClient(); var result = client.DownloadString(url); var html = new HtmlAgilityPack.HtmlDocument(); html.LoadHtml(result); var content = html.DocumentNode.SelectSingleNode("//b").InnerHtml; var rows = content.Split(new string[] { "\n" }, System.StringSplitOptions.RemoveEmptyEntries).Skip(2).ToList(); //split and remove headers var stations = new List<Station>(); var regex = new Regex(@"(?<id>\d{7})\s{2}(?<dcp>\d)\s{2}(?<name>.+?)(?<lat>\d+\s+\d+\.\d+\s+\w?)\s+(?<long>\d+\s+\d+\.\d+\s+\w)\s+(?<install>\d{2}/\d{2}/\d{4})?\s+(?<remove>\d{2}/\d{2}/\d{4})?\s+(?<benchmark>\d{2}/\d{2}/\d{4})?\s+(?<epoch>\d{4}-\d{4})?"); foreach (var row in rows) { var match = regex.Match(row); if (!match.Success) continue; var station = new Station(); station._id = ObjectId.GenerateNewId(); station.StationId = int.Parse(match.Groups["id"].Value); station.Name = match.Groups["name"].Value.Trim(); station.Location = new Location(DirectionUtils.Parse(match.Groups["long"].Value.Trim()), DirectionUtils.Parse(match.Groups["lat"].Value.Trim())); //station.GMTOffset = _geoService.GetGMTOffset(station.Location.Latitude, station.Location.Longitude); if (match.Groups["install"].Success) station.InstallDate = DateTime.Parse(match.Groups["install"].Value.Trim()); if (match.Groups["remove"].Success) station.RemovalDate = DateTime.Parse(match.Groups["remove"].Value.Trim()); stations.Add(station); } stations = stations.Distinct().ToList(); stations.AsParallel().ForAll(ScrapeStationInfo); RecreateCollection(stations); OnImportStationsCompleted(); }); }
private void ScrapeStationInfo(Station station) { var stationId = station.StationId; var client = new WebClient(); try { var result = client.DownloadString(Settings.GetStationDetailUrl(stationId)); var referenceStationMatch = ReferencedStationRegex.Match(result); if (referenceStationMatch.Success) { station.ReferencedStationId = int.Parse(referenceStationMatch.Groups["stationid"].Value); var offsetMatch = OffsetRegex.Match(result); station.HeightOffset = new Offset(double.Parse(offsetMatch.Groups["heighthigh"].Value), double.Parse(offsetMatch.Groups["heightlow"].Value)); station.TimeOffset = new Offset(int.Parse(offsetMatch.Groups["timehigh"].Value),int.Parse(offsetMatch.Groups["timelow"].Value)); } else station.IsHarmonic = true; station.IsComplete = true; } catch (Exception) { station.IsComplete = false; } }
private static void GetOffsetPrediction(Station station, Prediction pred) { double timeOffset = 0; double heightOffset = 0; //Need to apply an offset for subordinate stations if (!station.IsHarmonic) { if (pred.Level == TideLevel.High || pred.Level == TideLevel.Rising) { timeOffset = station.TimeOffset.High; heightOffset = station.HeightOffset.High; } else { timeOffset = station.TimeOffset.Low; heightOffset = station.HeightOffset.Low; } } pred.Height += heightOffset; pred.TimeStamp = pred.TimeStamp.AddMinutes(timeOffset); }