Skip to content

Commit

Permalink
Merge pull request #12 from mcrossley/master
Browse files Browse the repository at this point in the history
v1.3.1.3
  • Loading branch information
mcrossley authored Aug 10, 2023
2 parents a855750 + 0ffe1c9 commit 7f9f1f9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
8 changes: 6 additions & 2 deletions CreateMissing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<AssemblyVersion>1.3.0.1</AssemblyVersion>
<FileVersion>1.3.0.1</FileVersion>
<AssemblyVersion>1.3.1.3</AssemblyVersion>
<FileVersion>1.3.1.3</FileVersion>
<Version></Version>
<StartupObject>CreateMissing.Program</StartupObject>
<TargetFramework>net48</TargetFramework>
Expand All @@ -16,4 +16,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="if $(ConfigurationName)==Release (&#xD;&#xA;echo.&#xD;&#xA;echo Update Distribution folder&#xD;&#xA;echo copy MX exe&#xD;&#xA;xcopy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)..\CumulusMX-Dist\CumulusMX&quot; /D /-I /Q /Y&#xD;&#xA;echo copy MX config&#xD;&#xA;xcopy &quot;$(TargetPath).config&quot; &quot;$(SolutionDir)..\CumulusMX-Dist\CumulusMX&quot; /D /-I /Q /Y&#xD;&#xA;)" />
</Target>

</Project>
17 changes: 10 additions & 7 deletions DayFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public DayFile()

if (File.Exists(dayFileName + ".sav"))
{
Console.WriteLine("The dayfile.txt backup file dayfile.txt.sav already exists, aborting to prevent overwriting the original data.");
Console.WriteLine("Press any key to exit");
Program.LogConsole("The dayfile.txt backup file dayfile.txt.sav already exists, aborting to prevent overwriting the original data.", ConsoleColor.Cyan);
Program.LogConsole("Press any key to exit", ConsoleColor.DarkYellow);
Console.ReadKey(true);
Console.WriteLine("Exiting...");
Environment.Exit(1);
Expand Down Expand Up @@ -77,7 +77,8 @@ public void LoadDayFile()
if (newRec.Date < lastDate)
{
Program.LogMessage($"LoadDayFile: Error - Date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'");
Console.WriteLine($"\nError, date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'");
Console.WriteLine();
Program.LogConsole($"Error, date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'", ConsoleColor.Red);
Environment.Exit(3);
}

Expand All @@ -88,7 +89,8 @@ public void LoadDayFile()
if (newRec.Date == lastDate)
{
Program.LogMessage($"LoadDayFile: Error - Duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'");
Console.WriteLine($"\nError, duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'");
Console.WriteLine();
Program.LogConsole($"Error, duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'", ConsoleColor.Red);
Environment.Exit(4);
}

Expand All @@ -106,8 +108,9 @@ public void LoadDayFile()
if (errorCount >= 20)
{
Program.LogMessage($"LoadDayFile: Too many errors reading {dayFileName} - aborting load of daily data");
Console.WriteLine($"Too many errors reading {dayFileName} - aborting load of daily data");
Console.WriteLine("Please see the log file for more details");
Console.WriteLine();
Program.LogConsole($"Too many errors reading {dayFileName} - aborting load of daily data", ConsoleColor.Red);
Program.LogConsole("Please see the log file for more details", ConsoleColor.Red);
Environment.Exit(5);
}
}
Expand All @@ -125,7 +128,7 @@ public void LoadDayFile()
else
{
Program.LogMessage("LoadDayFile: No Dayfile found - No entries added to recent daily data list");
Console.WriteLine("No Dayfile found - No entries added to recent daily data list");
Program.LogConsole("No Dayfile found - No entries added to recent daily data list", ConsoleColor.Cyan);
// add a rcord for yesterday, just so we have something to process,
// if it is left at default we will not write it out
var newRec = new Dayfilerec
Expand Down
98 changes: 71 additions & 27 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Program
public static Cumulus cumulus;
public static string location;

private static ConsoleColor defConsoleColour;

private static DayFile dayfile;
private static readonly List<string> CurrentLogLines = new List<string>();
private static string CurrentLogName;
Expand All @@ -35,13 +37,17 @@ static void Main()
Trace.Listeners.Add(myTextListener);
Trace.AutoFlush = true;

defConsoleColour = Console.ForegroundColor;

var fullVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var version = $"{fullVer.Major}.{fullVer.Minor}.{fullVer.Build}";
LogMessage("CreateMissing v." + version);
Console.WriteLine("CreateMissing v." + version);

LogMessage("Processing started");
Console.WriteLine($"\nProcessing started: {DateTime.Now:U}\n");
Console.WriteLine();
Console.WriteLine($"Processing started: {DateTime.Now:U}");
Console.WriteLine();

// get the location of the exe - we will assume this is in the Cumulus root folder
location = AppDomain.CurrentDomain.BaseDirectory;
Expand All @@ -59,7 +65,8 @@ static void Main()
LogMessage($"First dayfile record: {dayfileStart:d}");
LogMessage($"Records Began Date : {currDate:d}");
Console.WriteLine($"First dayfile record: {dayfileStart:d}");
Console.WriteLine($"Records Began Date : {currDate:d}\n");
Console.WriteLine($"Records Began Date : {currDate:d}");
Console.WriteLine();

// Sanity check #1. Is the first date in the day file order than the records began date?
if (dayfileStart < currDate)
Expand All @@ -80,8 +87,8 @@ static void Main()
if (currDate >= DateTime.Today)
{
LogMessage("Start date is today!???");
Console.WriteLine("Start date is today!???");
Console.WriteLine("Press any key to exit");
LogConsole("Start date is today!???", ConsoleColor.Cyan);
LogConsole("Press any key to exit", ConsoleColor.DarkYellow);
Console.ReadKey(true);
Console.WriteLine("Exiting...");

Expand Down Expand Up @@ -124,14 +131,14 @@ static void Main()
if (newRec == null)
{
LogMessage($"Date: {currDate:d} : No monthly data was found, not creating a record");
Console.WriteLine($"\nDate: {currDate:d} : No monthly data was found, not creating a record");
LogConsole("No monthly data was found, not creating a record", ConsoleColor.Yellow);
RecsNoData++;
}
else
{
newRec = GetSolarDayRecFromMonthly(currDate, newRec);
dayfile.DayfileRecs.Insert(i, newRec);
Console.WriteLine(" done.");
LogConsole("done.", ConsoleColor.Green);
RecsAdded++;
i++;
}
Expand Down Expand Up @@ -167,7 +174,8 @@ static void Main()
else
{
LogMessage($"Date: {currDate:d} : Entry is OK");
Console.WriteLine($"Date: {currDate:d} : Entry is OK");
Console.Write($"Date: {currDate:d} : ");
LogConsole("Entry is OK", ConsoleColor.Green);
RecsOK++;
}
}
Expand Down Expand Up @@ -209,14 +217,14 @@ static void Main()
if (newRec == null)
{
LogMessage($"Date: {currDate:d} : No monthly data was found, not creating a record");
Console.WriteLine($"\nDate: {currDate:d} : No monthly data was found, not creating a record");
LogConsole("No monthly data was found, not creating a record", ConsoleColor.Yellow);
RecsNoData++;
}
else
{
newRec = GetSolarDayRecFromMonthly(currDate, newRec);
dayfile.DayfileRecs.Add(newRec);
Console.WriteLine(" done.");
LogConsole("done.", ConsoleColor.Green);
RecsAdded++;
}
currDate = IncrementMeteoDate(currDate);
Expand All @@ -230,7 +238,8 @@ static void Main()

// create the new dayfile.txt with a different name
LogMessage("Saving new dayfile.txt");
Console.WriteLine("\nSaving new dayfile.txt");
Console.WriteLine();
Console.WriteLine("Saving new dayfile.txt");

dayfile.WriteDayFile();

Expand All @@ -242,23 +251,26 @@ static void Main()
LogMessage($"Number of records No Data: {RecsNoData}");
LogMessage($"Number of records were OK: {RecsOK}");

Console.WriteLine($"\nNumber of records processed: {RecsAdded + RecsUpdated + RecsNoData + RecsOK}");
Console.WriteLine();
Console.WriteLine($"Number of records processed: {RecsAdded + RecsUpdated + RecsNoData + RecsOK}");
Console.WriteLine($" Were OK: {RecsOK}");
Console.WriteLine($" Added : {RecsAdded}");
Console.WriteLine($" Updated: {RecsUpdated}");
Console.Write( $" No Data: {RecsNoData}");
LogConsole( $" No Data: {RecsNoData}", ConsoleColor.Red, false);
if (RecsNoData > 0)
{
Console.WriteLine(" - please check the log file for the errors");
LogConsole(" - please check the log file for the errors", ConsoleColor.Cyan);
}
else
{
Console.WriteLine();
}
Console.WriteLine($" Were OK: {RecsOK}");

LogMessage("\nProcessing complete.");
Console.WriteLine("\n\nProcessing complete.");
Console.WriteLine("Press any key to exit");
LogMessage("Processing complete.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Processing complete.");
LogConsole("Press any key to exit", ConsoleColor.DarkYellow);
Console.ReadKey(true);
}

Expand All @@ -267,6 +279,22 @@ public static void LogMessage(string message)
Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message);
}

public static void LogConsole(string msg, ConsoleColor colour, bool newLine = true)
{
Console.ForegroundColor = colour;

if (newLine)
{
Console.WriteLine(msg);
}
else
{
Console.Write(msg);
}

Console.ForegroundColor = defConsoleColour;
}

private static Dayfilerec GetDayRecFromMonthly(DateTime date)
{
var rec = new Dayfilerec()
Expand Down Expand Up @@ -382,6 +410,14 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date)
// we use idx 0 & 1 together (date/time), set the idx to 1
idx = 1;
// process each record in the file
// first a sanity check for an empty line!
if (string.IsNullOrWhiteSpace(CurrentLogLines[CurrentLogLineNum]))
{
CurrentLogLineNum++;
LogMessage($"LogFile: Error at line {CurrentLogLineNum}, an empty line was detected!");
continue;
}

//var st = new List<string>(Regex.Split(line, CultureInfo.CurrentCulture.TextInfo.ListSeparator));
// Regex is very expensive, let's assume the separator is always a single character
var st = new List<string>(CurrentLogLines[CurrentLogLineNum++].Split(dayfile.FieldSep[0]));
Expand Down Expand Up @@ -804,7 +840,7 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date)
raintoday += (raincounter - lastentrycounter) * cumulus.CalibRainMult;
}

// add last hours rain for this last record.
// add last hours rain for this last record.
AddLastHoursRainEntry(entrydate, totalRainfall + raintoday, ref rain1hLog, ref rain24hLog);

// rainfall in last hour
Expand All @@ -831,8 +867,8 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date)
finished = true;
}
}


if (started && recCount >= 5) // need at least five records to create a day
{
// we were in the right day, now we aren't
Expand Down Expand Up @@ -862,8 +898,8 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date)
{
LogMessage($"LogFile: Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}");
LogMessage("LogFile: Please edit the file to correct the error");
Console.WriteLine($"Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}");
Console.WriteLine("Please edit the file to correct the error");
LogConsole($"Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}", ConsoleColor.Red);
LogConsole("Please edit the file to correct the error", ConsoleColor.Red);

Environment.Exit(1);
}
Expand Down Expand Up @@ -987,6 +1023,14 @@ private static Dayfilerec GetSolarDayRecFromMonthly(DateTime date, Dayfilerec re
while (CurrentSolarLogLineNum < CurrentSolarLogLines.Count)
{
// process each record in the file
// first a sanity check for an empty line!
if (string.IsNullOrWhiteSpace(CurrentLogLines[CurrentLogLineNum]))
{
CurrentLogLineNum++;
LogMessage($"Solar: Error at line {CurrentLogLineNum}, an empty line was detected!");
continue;
}

// Regex is very expensive, let's assume the separator is always a single character
var st = new List<string>(CurrentSolarLogLines[CurrentSolarLogLineNum].Split(dayfile.FieldSep[0]));
var entrydate = Utils.DdmmyyhhmmStrToDate(st[0], st[1]);
Expand Down Expand Up @@ -1015,8 +1059,8 @@ private static Dayfilerec GetSolarDayRecFromMonthly(DateTime date, Dayfilerec re
{
LogMessage($"Solar: Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}");
LogMessage("Solar: Please edit the file to correct the error");
Console.WriteLine($"Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}");
Console.WriteLine("Please edit the file to correct the error");
LogConsole($"Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}", ConsoleColor.Red);
LogConsole("Please edit the file to correct the error", ConsoleColor.Red);

Environment.Exit(1);
}
Expand Down Expand Up @@ -1055,7 +1099,7 @@ private static void AddMissingData(int idx, DateTime metDate)
{
RecsNoData++;
LogMessage($"{metDate:d} : No monthly data was found, not updating this record");
Console.WriteLine($"\n{metDate:d} : No monthly data was found, not updating this record");
LogConsole("No monthly data was found, not updating this record", ConsoleColor.Yellow);
return;
}

Expand Down Expand Up @@ -1208,7 +1252,7 @@ private static void AddMissingData(int idx, DateTime metDate)
dayfile.DayfileRecs[idx].HighRain24h = newRec.HighRain24h;
dayfile.DayfileRecs[idx].HighRain24hTime = newRec.HighRain24hTime;
}
Console.WriteLine("done.");
LogConsole("done.", ConsoleColor.Green);
RecsUpdated++;
}

Expand Down Expand Up @@ -1323,7 +1367,7 @@ private static void AddLastHoursRainEntry(DateTime ts, double rain, ref Queue<La
h24Queue.Dequeue();
}
}
}
}


class LastHourRainLog
Expand Down
4 changes: 4 additions & 0 deletions Updates.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.3.1
—————
- Now skips (with a warning in the diags log) blank lines in the monthly log files

1.3.0
—————
- Support for CMX v3.20.0 - adds 24 hour rainfall
Expand Down

0 comments on commit 7f9f1f9

Please sign in to comment.