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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
unit convertlogfileunit;
//Tool: .dat to Moon Sun .csv
{$mode objfpc}
interface
uses
appsettings, //Required to read application settings (like locations).
dateutils, //Required to convert logged UTC string to TDateTime
strutils, //Required for checking lines in conversion file.
moon, //required for Moon calculations
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, ComCtrls;
type
{ Tconvertdialog }
Tconvertdialog = class(TForm)
CloseButton: TButton;
OutputFilenameDisplay: TLabeledEdit;
LatitudeDisplay: TLabeledEdit;
LongitudeDisplay: TLabeledEdit;
Memo1: TMemo;
OpenFileDialog: TOpenDialog;
SelectButton: TButton;
StatusBar1: TStatusBar;
procedure CloseButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SelectButtonClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
convertdialog: Tconvertdialog;
implementation
uses
Unit1
, header_utils;
{ Tconvertdialog }
procedure Tconvertdialog.SelectButtonClick(Sender: TObject);
var
File1,OutFile: TextFile;
Str: String;
pieces: TStringList;
MoonElevation: extended = 0.0;
MoonAzimuth: extended = 0.0;
SunElevation: extended = 0.0;
SunAzimuth: extended = 0.0;
UTCRecord :TDateTime;
ComposeString: String;
OutFileString: String;
WriteAllowable: Boolean = True; //Allow output file to be written or not.
LineNumber: Integer =0;
EndTrimString: String =', Zenith, Azimuth, MoonPhaseDeg, MoonElevDeg, MoonIllum';
ErrorString: String;
begin
pieces := TStringList.Create;
{ Clear status bar }
StatusBar1.Panels.Items[0].Text:='';
OpenFileDialog.Filter:='data log files|*.dat|All files|*.*';
OpenFileDialog.InitialDir := appsettings.LogsDirectory;
if OpenFileDialog.Execute then begin
//Start reading file.
StatusBar1.Panels.Items[0].Text:='Reading Input file';
AssignFile(File1, OpenFileDialog.Filename);
OutFileString:=ChangeFileExt(OpenFileDialog.Filename,'.csv');
AssignFile(OutFile, OutFileString);
OutputFilenameDisplay.Text:=OutFileString;
//StatusBar1.Panels.te;
if FileExists(OutFileString) then begin
if (MessageDlg('Overwrite existing file?','Do you want to overwrite the existing file?',mtConfirmation,[mbOK,mbCancel],0) = mrOK) then
WriteAllowable:=True
else
WriteAllowable:=False;
end;
if WriteAllowable then begin
{$I+}
try
Reset(File1);
Rewrite(OutFile); //Open file for writing
StatusBar1.Panels.Items[0].Text:='Processing Input file, please wait ...'; Application.ProcessMessages;
repeat
// Read one line at a time from the file.
Readln(File1, Str);
inc(LineNumber);
// Get location data from header.
if AnsiStartsStr('# Position',Str) then begin
//Prepare for parsing.
pieces.Delimiter := ',';
pieces.StrictDelimiter := True; //Do not parse spaces.
//Remove comment from beginning of line.
pieces.DelimitedText := AnsiRightStr(Str,length(Str) - RPos(':',Str));
if (pieces.Count<2) then begin
ErrorString:='No location data found in header.';
StatusBar1.Panels.Items[0].Text:=ErrorString;
MessageDlg('Error',ErrorString, mtError, [mbOK],0);
Writeln(OutFile,'Error: No location data found in header.');
LatitudeDisplay.Text:='nul';
LongitudeDisplay.Text:='nul';
break;
end
else
begin
MyLatitude:=StrToFloat(pieces.Strings[0]);
if ((MyLatitude<-90.0) or (MyLatitude>90.0)) then begin
ErrorString:='Error: Latitude ' + FloatToStr(MyLatitude) +' out of range';
StatusBar1.Panels.Items[0].Text:=ErrorString;
StatusMessage(ErrorString);
break;
end;
MyLongitude:=StrToFloat(pieces.Strings[1]);
if ((MyLongitude<-180.0) or (MyLongitude>180.0)) then begin
ErrorString:='Error: Longitude '+FloatToStr(MyLongitude)+' out of range';
StatusBar1.Panels.Items[0].Text:=ErrorString;
StatusMessage('Error: Longitude '+FloatToStr(MyLongitude)+' out of range');
break;
end;
LatitudeDisplay.Text:=Format('%.3f',[MyLatitude]);
LongitudeDisplay.Text:=Format('%.3f',[MyLongitude]);
end;
end;
Application.ProcessMessages;
//Get and modify record header string
//if AnsiStartsStr('# YYYY-MM-DDTHH:mm:ss.fff;',Str) then
// Writeln(OutFile,'UTC '+AnsiRightStr(Str,length(Str) - 2)+';MoonPhaseDeg;MoonElevDeg;MoonIllum%;SunElevDeg');
Str:=Trim(Str);
if AnsiStartsStr('# UTC Date & Time,',Str) then begin
if AnsiEndsStr(EndTrimString,Str) then begin
Str:=AnsiLeftStr(Str,Length(Str)-Length(EndTrimString));
end;
//Remove preceding comment marker
Writeln(OutFile,AnsiRightStr(Str,length(Str) - 2)+';MoonPhaseDeg;MoonElevDeg;MoonIllum%;SunElevDeg');
end;
Application.ProcessMessages;
//Ignore comment lines which have # as first character.
if not AnsiStartsStr('#',Str) then
begin
//Separate the fields of the record.
pieces.Delimiter := ';';
pieces.DelimitedText := Str;
begin
//parse the fields, and convert as necessary.
//Convert UTC string 'YYYY-MM-DDTHH:mm:ss.fff' into TDateTime
UTCRecord:=ScanDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz',pieces.Strings[0]);
//Calculate Moon position
//Change sign for Moon calculations
Moon_Position_Horizontal(
StrToDateTime(DateTimeToStr(UTCRecord)),
-1.0*MyLongitude,
MyLatitude,
MoonElevation,
MoonAzimuth);
Sun_Position_Horizontal(
StrToDateTime(DateTimeToStr(UTCRecord)),
-1.0*MyLongitude,
MyLatitude,SunElevation,
SunAzimuth);
//Prepare string for output.
ComposeString:= Str + ';'
//Moon Phase angle (0 to 180 degrees).
+ Format('%.1f;',[moon_phase_angle(StrToDateTime(DateTimeToStr(UTCRecord)))])
//Moon elevation (positive = above horizon, negative = below horizon).
+ Format('%.3f;',[MoonElevation])
//Moon illumination pecent.
+ Format('%.1f;',[current_phase(StrToDateTime(DateTimeToStr(UTCRecord)))*100.0])
//Sun elevation (positive = above horizon, negative = below horizon).
+ Format('%.3f;',[SunElevation])
;
WriteLn(OutFile,ComposeString);
end;//End of checking number of fields in record.
end;
until(EOF(File1)); // EOF(End Of File) The the program will keep reading new lines until there is none.
CloseFile(File1);
StatusBar1.Panels.Items[0].Text:='Writing Output file';
except
//on E: EInOutError do begin
on E: Exception do begin
MessageDlg('Error', 'File handling error occurred. Details: ' + sLineBreak + 'On Line number: '+ IntToStr(LineNumber) + sLineBreak +E.ClassName+'/'+E.Message, mtError, [mbOK],0);
StatusBar1.Panels.Items[0].Text:='Error encountered.';
end;
end;
Flush(OutFile);
CloseFile(OutFile);
StatusBar1.Panels.Items[0].Text:='Processing complete';
end;//End of WriteAllowable check.
end;
end;
procedure Tconvertdialog.CloseButtonClick(Sender: TObject);
begin
Close;
end;
procedure Tconvertdialog.FormCreate(Sender: TObject);
begin
{ Clear status bar }
StatusBar1.Panels.Items[0].Text:='';
end;
initialization
{$I convertlogfileunit.lrs}
end.
|