Skip to content

Commit

Permalink
Nexrad working
Browse files Browse the repository at this point in the history
  • Loading branch information
k2fc committed Oct 3, 2020
1 parent 21d256c commit d009801
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 32 deletions.
2 changes: 1 addition & 1 deletion NexradDecoder/NexradDecoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6A8559F0-0F1F-49C6-BAAE-6D1C0A624940}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NexradDecoder</RootNamespace>
<AssemblyName>NexradDecoder</AssemblyName>
Expand Down
152 changes: 132 additions & 20 deletions scope/NexradDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NexradDecoder;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
Expand All @@ -12,7 +13,25 @@ public class NexradDisplay
{
[XmlIgnore]
[DisplayName("Colors"), Description("Weather Radar Colors by Value")]
public Color[] Colors { get; set; } = new Color[16];
public Color[] Colors { get; set; } = new Color[16]
{
Color.FromArgb(0, 255, 255, 255),
Color.FromArgb(0, 255, 255, 255),
Color.FromArgb(0, 255, 255, 255),
Color.FromArgb(0, 255, 255, 255),
Color.FromArgb(0, 255, 0),
Color.FromArgb(0, 192, 0),
Color.FromArgb(0, 128, 0),
Color.FromArgb(255, 255, 0),
Color.FromArgb(192, 192, 0),
Color.FromArgb(255, 128, 0),
Color.FromArgb(255, 0, 0),
Color.FromArgb(192, 0, 0),
Color.FromArgb(128, 0, 0),
Color.FromArgb(255, 0, 255),
Color.FromArgb(128, 0, 128),
Color.FromArgb(255, 255, 255),
};
[XmlElement("Colors")]
[Browsable(false)]
public int[] ColorsAsArgb
Expand All @@ -35,62 +54,155 @@ public int[] ColorsAsArgb
}
}
}
double intensity = 1;
[DisplayName("Color Intensity"), Description("Weather Radar Color intensity")]
public int ColorIntensity {
get
{
return (int)(255 * intensity);
}
set
{
if (value > 255)
intensity = 1;
else if (value < 0)
intensity = 0;
else
intensity = value / 255d;
}
}
double alphafactor = .5;
[DisplayName("Transparency"), Description("Weather Radar Transparency")]
public int Transparency
{
get
{
return (int)(255 * (1 - alphafactor));
}
set
{
if (value > 255)
alphafactor = 0;
else if (value < 0)
alphafactor = 1;
else
alphafactor = 1 - (value / 255d);
}
}
public string URL { get; set; }
public int DownloadInterval { get; set; } = 300;
public int Range { get; set; } = 124;
RadialPacketDecoder decoder = new RadialPacketDecoder();
SymbologyBlock symbology;
RadialSymbologyBlock symbology;
DescriptionBlock description;
bool gotdata = false;
void GetRadarData(string url)
{
//var client = new WebClient();
//Stream response = client.OpenRead("file://e/users/dennis/downloads/KOKX_SDUS51_N0ROKX_202009292354");
decoder.setFileResource("e:\\users\\dennis\\downloads\\KOKX_SDUS51_N0ROKX_202009292354");
decoder.parseMHB();
description = decoder.parsePDB();
symbology = decoder.parsePSB();
polygons = Polygons((RadialSymbologyBlock)symbology);
using (var client = new WebClient())
{
try
{
Stream response = client.OpenRead(url);
MemoryStream stream = new MemoryStream();
response.CopyTo(stream);
decoder.setStreamResource(stream);
decoder.parseMHB();
description = decoder.parsePDB();
symbology = (RadialSymbologyBlock)decoder.parsePSB();
gotdata = true;
}
catch { }
}
//decoder.setFileResource("e:\\users\\dennis\\downloads\\KOKX_SDUS51_N0ROKX_202009292354");

RecomputeVertices(_center, _scale, _rotation);
}

public NexradDisplay()
{
GetRadarData("REMOVE THIS DENNIS");

}

public Polygon[] Polygons()
System.Threading.Timer timer;
private void cbTimerElapsed(object state)
{
GetRadarData(URL);
}
public Polygon[] Polygons(GeoPoint center, double scale, double rotation = 0)
{
GetRadarData("REMOVE THIS DENNIS");
if (_center != center || _scale != scale || _rotation != rotation)
{
_center = center;
_scale = scale;
_rotation = rotation;
RecomputeVertices(center, scale, rotation);
}
if (timer == null)
timer = new System.Threading.Timer(new System.Threading.TimerCallback(cbTimerElapsed), null,0,DownloadInterval * 1000);

if (polygons == null)
return new Polygon[0];
return polygons;
}
Polygon[] polygons;
Polygon[] Polygons (RadialSymbologyBlock block)

GeoPoint _center = new GeoPoint();
double _scale;
double _rotation;
public void RecomputeVertices(GeoPoint center, double scale, double rotation = 0)
{
GeoPoint radarLocation = new GeoPoint(description.Latitude, description.Longitude);
if (!gotdata)
return;
_center = center;
_scale = scale;
_rotation = rotation;
var polygons = new List<Polygon>();
double resolution = (double)Range / block.LayerNumberOfRangeBins;
for (int i = 0; i < block.NumberOfRadials; i++)
GeoPoint radarLocation = new GeoPoint(description.Latitude, description.Longitude);
double resolution = (double)Range / symbology.LayerNumberOfRangeBins;
for (int i = 0; i < symbology.NumberOfRadials; i++)
{
var radial = block.Radials[i];
var radial = symbology.Radials[i];
for (int j = 0; j < radial.ColorValues.Length; j++)
{
Polygon polygon = new Polygon();
polygon.Points.Add(radarLocation.FromPoint(resolution * j, radial.StartAngle));
polygon.Points.Add(radarLocation.FromPoint(resolution * j, radial.StartAngle + radial.AngleDelta));
polygon.Points.Add(radarLocation.FromPoint(resolution * (j + 1), radial.StartAngle + radial.AngleDelta));
polygon.Points.Add(radarLocation.FromPoint(resolution * (j + 1), radial.StartAngle));
polygon.Color = Colors[radial.ColorValues[j]];
var color = Colors[radial.ColorValues[j]];
polygon.Color = Color.FromArgb((int)(color.A * alphafactor), (int)(color.R * intensity), (int)(color.G * intensity), (int)(color.B * intensity));
polygon.ComputeVertices(center, scale, rotation);
polygons.Add(polygon);

}
}
return polygons.ToArray();
this.polygons = polygons.ToArray();
}

// ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.kokx/sn.last
}

public class Polygon
{
public float[][] vertices = new float[2][];
public List<GeoPoint> Points { get; set; } = new List<GeoPoint>();
public Color Color { get; set; }

public void ComputeVertices(GeoPoint center, double scale, double ScreenRotation = 0)
{
GeoPoint[] points;
lock (Points)
{
points = Points.ToArray();
}
vertices[0] = new float[points.Length];
vertices[1] = new float[points.Length];
for (int i = 0; i < points.Length; i++)
{
double bearing = center.BearingTo(points[i]) - ScreenRotation;
double distance = center.DistanceTo(points[i]);
vertices[0][i] = (float)(Math.Sin(bearing * (Math.PI / 180)) * (distance / scale));
vertices[1][i] = (float)(Math.Cos(bearing * (Math.PI / 180)) * (distance / scale));
}
}
}
}
29 changes: 18 additions & 11 deletions scope/RadarWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public ListOfIReceiver Receivers
radar.Receivers = value;
}
}
private GeoPoint _homeLocation;
private GeoPoint _homeLocation = new GeoPoint();
[DisplayName("Screen Center Point"), Category("Radar Properties")]
public GeoPoint ScreenCenterPoint
{
Expand All @@ -158,7 +158,7 @@ public GeoPoint ScreenCenterPoint
_homeLocation = value;
}
}
private double _startingRange;
private double _startingRange = 20;
[DisplayName("Radar Range"), Category("Radar Properties"), Description("About two more minutes, chief!")]
public double Range
{
Expand Down Expand Up @@ -244,6 +244,7 @@ public RadarWindow()
window.KeyDown += Window_KeyDown;
window.MouseWheel += Window_MouseWheel;
window.MouseMove += Window_MouseMove;
window.MouseUp += Window_MouseUp;
aircraftGCTimer.Start();
aircraftGCTimer.Elapsed += AircraftGCTimer_Elapsed;
GL.ClearColor(BackColor);
Expand All @@ -255,6 +256,12 @@ public RadarWindow()
settingshash = md5.Hash;
}
}

private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
dragging = false;
}

byte[] settingshash;
public void Run(bool isScreenSaver)
{
Expand Down Expand Up @@ -284,6 +291,7 @@ private void Window_WindowStateChanged(object sender, EventArgs e)
}

bool _mousesettled = false;
bool dragging = false;
private void Window_MouseMove(object sender, MouseMoveEventArgs e)
{
if (!e.Mouse.IsAnyButtonDown)
Expand All @@ -297,6 +305,7 @@ private void Window_MouseMove(object sender, MouseMoveEventArgs e)
}
else if (e.Mouse.RightButton == ButtonState.Pressed)
{
dragging = true;
double xMove = e.XDelta * xPixelScale;
double yMove = e.YDelta * xPixelScale;
radar.Location = radar.Location.FromPoint(xMove * scale, 270);
Expand Down Expand Up @@ -401,7 +410,8 @@ private void Window_RenderFrame(object sender, FrameEventArgs e)
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.DstAlpha);
DrawRangeRings();
DrawNexrad();
if(!dragging)
DrawNexrad();
DrawReceiverLocations();
DrawLines();
GenerateTargets();
Expand Down Expand Up @@ -529,13 +539,9 @@ private void DrawPolygon (Polygon polygon)
double yscale = (double)window.Width / (double)window.Height;
GL.Begin(PrimitiveType.Polygon);
GL.Color4(polygon.Color);
foreach (var point in polygon.Points)
for (int i = 0; i < polygon.vertices[0].Length; i++)
{
double bearing = radar.Location.BearingTo(point) - ScreenRotation;
double distance = radar.Location.DistanceTo(point);
float x = (float)(Math.Sin(bearing * (Math.PI / 180)) * (distance / scale));
float y = (float)(Math.Cos(bearing * (Math.PI / 180)) * (distance / scale) * yscale);
GL.Vertex2(x, y);
GL.Vertex2(polygon.vertices[0][i], polygon.vertices[1][i] * yscale);
}
GL.End();
}
Expand All @@ -544,10 +550,11 @@ private void DrawNexrad()
{
foreach (var nexrad in Nexrads)
{
var polygons = nexrad.Polygons();
var polygons = nexrad.Polygons(radar.Location, scale, ScreenRotation);
for (int i = 0; i < polygons.Length; i++)
{
DrawPolygon(polygons[i]);
if(polygons[i].Color.A > 0)
DrawPolygon(polygons[i]);
}
}
}
Expand Down

0 comments on commit d009801

Please sign in to comment.