-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (58 loc) · 2.89 KB
/
Program.cs
File metadata and controls
66 lines (58 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using FloridaReport.Database;
using FloridaReport.Models;
using FloridaReport.Services;
internal class Program
{
private static async Task Main(string[] args)
{
// Initialize required objects
var hurricaneDatabase = new HurricaneDatabase();
var floridaService = new FloridaService();
using var client = new HttpClient();
// Initialize DB
await hurricaneDatabase.SetupDatabaseAsync();
// Download data from NOAA website. Later, replace with an API if there is one to pull the latest version.
Console.WriteLine("Downloading NOAA Atlantic Basin Data...");
// https://www.nhc.noaa.gov/data/hurdat/
string url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2024-040425.txt";
string rawData = await client.GetStringAsync(url);
Console.WriteLine("Data retrieved.");
// Establish new list for the hurricane records using the HurricanRecord object.
var landfalls = new List<HurricaneRecord>();
string currentId = "", currentName = "";
int currentYear = 0;
bool stormAlreadyImpacted = false; // Only record the first record of each storm that impacted Florida.
// Parse NOAA record.
foreach (var line in rawData.Split('\n'))
{
var p = line.Split(',').Select(x => x.Trim()).ToArray();
if (p.Length < 4) continue;
// Ensure hurricane is in the Atlantic basin.
if (p[0].StartsWith("AL"))
{
currentId = p[0];
currentName = p[1];
currentYear = int.Parse(currentId.Substring(4));
stormAlreadyImpacted = false; // Reset for the next storm in the file.
}
// The txt file contains tracking rows. Ensure that the year is >= 1900 as per requirements.
else if (currentYear >= 1900 && !stormAlreadyImpacted)
{
// Convert S and W coords to negatives.
double lat = p[4].EndsWith('N') ? double.Parse(p[4].TrimEnd('N')) : -double.Parse(p[4].TrimEnd('S'));
double lon = p[5].EndsWith('E') ? double.Parse(p[5].TrimEnd('E')) : -double.Parse(p[5].TrimEnd('W'));
int wind = int.Parse(p[6]);
// Use floridaService to check if hurricane is within the coords set.
if (floridaService.InFlorida(lat, lon))
{
landfalls.Add(new HurricaneRecord(currentId, currentName, currentYear, lat, lon, wind));
stormAlreadyImpacted = true; // Storm has already impacted. Mark as done and move onto the next.
}
}
}
// Save records to DB for future use.
await hurricaneDatabase.SaveHurricanesAsync(landfalls);
Console.WriteLine($"Saved {landfalls.Count} Florida landfalls since 1900.");
ExcelReportService.ExportReport(landfalls);
}
}