So since there is this impending storm (blizzard, snow storm, snowmageddon, snowpocalypse, snowzilla) “Nemo” hitting us here on the East coast within a few hours, I figured a good topic might be on weather alert data. There is a bunch of data available from the NOAA (National Oceanic and Atmospheric Administration) on their site. Data is available in several categories, including Forecasts, Watch/warnings, Storm Prediction Center Forecast Products, etc. In my very quick example I’m going to pick the “Watch / Warning” data for my region and display this using WPF for an easy visualization.
In short, the process is: get the data using “ServiceModel.Syndication”, parse through, and display!
The Xaml:
<Window x:Class="wpfAlert.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Alerts" Height="319" Width="637" Background="#FFF1F1F1" WindowStyle="ToolWindow" SizeToContent="WidthAndHeight">
<Grid>
<ScrollViewer Margin="10,41,10,27">
<StackPanel x:Name="stkMain" Margin="10,10,10,10"/>
</ScrollViewer>
<Label x:Name="lblStatus" Content="Refreshing..." HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Label x:Name="lblStatusBot" Content="Refreshing..." VerticalAlignment="Bottom" Margin="10,0,0,0"/>
</Grid>
</Window>
The Code:
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Diagnostics;
using System.Timers;
using System.Windows.Threading;
using System.Windows.Controls;
namespace wpfAlert
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
loadAlerts();
// The Refresh Timer
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
//5 Minutes
dispatcherTimer.Interval = new TimeSpan(0, 5, 0);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
loadAlerts();
}
private void loadAlerts()
{
stkMain.Children.Clear();
Atom10FeedFormatter aReader = new Atom10FeedFormatter();
using (XmlReader xmlReader = XmlReader.Create("http://alerts.weather.gov/cap/wwaatmget.php?x=NJC005&y=0"))
{
aReader.ReadFrom(xmlReader);
}
int aCount = 0;
foreach (SyndicationItem sItem in aReader.Feed.Items)
{
//the alert summary
TextBlock aTxt = new TextBlock();
aTxt.Text = ((TextSyndicationContent)sItem.Summary).Text;
aTxt.Width = 600;
aTxt.VerticalAlignment = System.Windows.VerticalAlignment.Top;
aTxt.TextWrapping = TextWrapping.Wrap;
//a link to the entire alert
Button aButLink = new Button();
aButLink.Tag = sItem.Links[0].GetAbsoluteUri();
aButLink.Width = 100;
aButLink.Click += new RoutedEventHandler(alertLink_Click);
aButLink.Content = "Alert Detail";
//a panel to hold alert & link
StackPanel aStk = new StackPanel();
aStk.Children.Add(aTxt);
aStk.Children.Add(aButLink);
//title / expander
Expander aExpand = new Expander();
aExpand.Width = stkMain.Width;
aExpand.Header = sItem.Title.Text;
aExpand.Content = aStk;
//add them to the form
stkMain.Children.Add(aExpand);
aCount++;
}
//update some label for info
lblStatusBot.Content = aCount.ToString() + " Alerts Updated @ " + DateTime.Now.ToString();
lblStatus.Content = aReader.Feed.Title.Text;
}
private void alertLink_Click(object sender, RoutedEventArgs e)
{
//handle links
Button aLink = (Button)sender;
Process.Start(aLink.Tag.ToString());
}
}
}
Obviously we can go so much deeper with this – radar, email, gps – but the purpose of this was to just share a quick / fun sketch.
[…] Display Weather Alerts From NOAA Using Atom (WPF Example) (chrisbitting.com) […]
LikeLike
[…] Display Weather Alerts From NOAA Using Atom (WPF Example) (chrisbitting.com) […]
LikeLike