-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuMainForm.pas
375 lines (335 loc) · 13.4 KB
/
uMainForm.pas
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
unit uMainForm;
//
// PHOLIAGE Model, (c) Roelof Oomen, 2006-2007
//
// Main user interface handling
//
interface
uses
// Own units
Ellipsoid_Integration,
// Delphi Units
LCLIntf, LCLType, Controls, Forms, StdCtrls, ExtCtrls,
ValEdit, Dialogs, ComCtrls, Classes;
const
PROCTHREADS = 3;
type
{ TMainForm }
TMainForm = class(TForm)
private
fVersion: string;
function getVersion : string;
published
GPValueListEditor: TValueListEditor;
PageControl1: TPageControl;
Model: TTabSheet;
TabSheet2: TTabSheet;
IntervalValueListEditor: TValueListEditor;
ModelValueListEditor: TValueListEditor;
ParamLabel: TLabel;
Label17: TLabel;
Label13: TLabel;
AbsLightEdit: TEdit;
Edit7: TEdit;
Label3: TLabel;
IndTreeRunButton: TButton;
RuntimeLabeledEdit: TLabeledEdit;
OpenDialog: TOpenDialog;
SelectFileButton: TButton;
FilenameEdit: TEdit;
CountLabeledEdit: TLabeledEdit;
MainRunButton: TButton;
ProgressBar1: TProgressBar;
TimeLabeledEdit: TLabeledEdit;
SaveButton: TButton;
SaveOpenDialog: TOpenDialog;
SaveOpenCheckBox: TCheckBox;
LADistComboBox: TComboBox;
LADistLabel: TLabel;
PhotoSynthEdit: TEdit;
Label1: TLabel;
PhotValueListEditor: TValueListEditor;
HorLeafCheckBox: TCheckBox;
Label2: TLabel;
procedure IndTreeRunButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure DoGP(Ellipsoid : TEllipsoid);
procedure DoIntervals(Ellipsoid : TEllipsoid);
procedure SelectFileButtonClick(Sender: TObject);
procedure MainRunButtonClick(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
public
property version: string read fVersion;
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
uses
Model_Absorption, GaussInt, DataRW,
strutils, sysutils,
fileinfo, // fileinfo reads exe resources as long as you register the appropriate units
winpeimagereader, {need this for reading exe info}
elfreader, {needed for reading ELF executables}
machoreader; {needed for reading MACH-O executables}
var
Plot : TPlot;
procedure TMainForm.DoGP(Ellipsoid : TEllipsoid);
// Initialise the gaussion integration points
begin
Ellipsoid.gpPsi:=StrToInt(GPValueListEditor.Cells[1,3]);
Ellipsoid.gpR:= StrToInt(GPValueListEditor.Cells[1,2]);
Ellipsoid.gpZ:= StrToInt(GPValueListEditor.Cells[1,1]);
Ellipsoid.Ring.Ellipse.Env.Absorption.GP:= StrToInt(GPValueListEditor.Cells[1,4]);
Ellipsoid.Ring.Ellipse.Env.Absorption.AbsorptionAz.GP:=StrToInt(GPValueListEditor.Cells[1,5]);
Ellipsoid.Ring.Ellipse.Env.Light.GP:=StrToInt(GPValueListEditor.Cells[1,6]);
Ellipsoid.Ring.Ellipse.Env.Light.Extinction.GP:=StrToInt(GPValueListEditor.Cells[1,7]);
Ellipsoid.Ring.Ellipse.Env.Light.Extinction.ExtinctionAz.GP:=StrToInt(GPValueListEditor.Cells[1,8]);
Ellipsoid.Ring.Ellipse.Env.Assimilation.AssimilationAz.IncidentL.GP:=StrToInt(GPValueListEditor.Cells[1,9]);
Ellipsoid.Ring.Ellipse.Env.Assimilation.AssimilationAz.IncidentL.IncidentLAz.GP:=StrToInt(GPValueListEditor.Cells[1,10]);
Ellipsoid.Ring.Ellipse.Env.Assimilation.GP:=StrToInt(GPValueListEditor.Cells[1,11]);
Ellipsoid.Ring.Ellipse.Env.Assimilation.AssimilationAz.GP:=StrToInt(GPValueListEditor.Cells[1,12]);
end;
procedure TMainForm.DoIntervals(Ellipsoid : TEllipsoid);
// Initialise the gaussion integration intervals and three azimuth widths
begin
//Ellipsoid z (min)=-1
//Ellipsoid z (max)=1
Ellipsoid.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,1]);
Ellipsoid.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,2]);
//Ellipsoid psi (min*pi)=0
//Ellipsoid psi (max*pi)=2
Ellipsoid.Ring.Ellipse.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,3])*pi;
Ellipsoid.Ring.Ellipse.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,4])*pi;
//I psi (min*pi)=0
//I psi (max*pi)=2
Ellipsoid.Ring.Ellipse.Env.Absorption.AbsorptionAz.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,5])*pi;
Ellipsoid.Ring.Ellipse.Env.Absorption.AbsorptionAz.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,6])*pi;
//I_0 theta (min*pi)=0
//I_0 theta (max*pi)=0,5
Ellipsoid.Ring.Ellipse.Env.Light.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,7])*pi;
Ellipsoid.Ring.Ellipse.Env.Light.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,8])*pi;
//Kf psi (min*pi)=0
//Kf psi (max*pi)=2
Ellipsoid.Ring.Ellipse.Env.Light.Extinction.ExtinctionAz.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,9])*pi;
Ellipsoid.Ring.Ellipse.Env.Light.Extinction.ExtinctionAz.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,10])*pi;
//P psi (min*pi)=0
//P psi (max*pi)=2
Ellipsoid.Ring.Ellipse.Env.Assimilation.AssimilationAz.x_min:=StrToFloat(IntervalValueListEditor.Cells[1,11])*pi;
Ellipsoid.Ring.Ellipse.Env.Assimilation.AssimilationAz.x_max:=StrToFloat(IntervalValueListEditor.Cells[1,12])*pi;
//Crown AzWidth f_omega (*pi)=2
Ellipsoid.Ring.Ellipse.Env.Crown.F.AzWidth:=StrToFloat(IntervalValueListEditor.Cells[1,13])*pi;
//Veg AzWidth f_omega (*pi)=2
Ellipsoid.Ring.Ellipse.Env.Vegetation.F.AzWidth:=StrToFloat(IntervalValueListEditor.Cells[1,14])*pi;
//AzWidth i_omega_0 (*pi)=2
Ellipsoid.Ring.Ellipse.Env.Light.AzWidth:=StrToFloat(IntervalValueListEditor.Cells[1,15])*pi;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
PageControl1.TabIndex:=0;
Plot:=TPlot.Create;
fVersion := getVersion; // Retrieve project version information
MainForm.Caption := MainForm.Caption + ' v.' + version;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FreeAndNil(Plot);
end;
procedure TMainForm.MainRunButtonClick(Sender: TObject);
var
I: Integer;
RunTime : DWord;
GIR : GIResult;
begin
TimeLabeledEdit.Text:='';
runtime:=GetTickCount64;
ProgressBar1.Min:=0;
ProgressBar1.Max:=Length(Plot.Plants);
ProgressBar1.Position:=0;
try
// Prevent user action during calculations
PageControl1.Enabled:=false;
MainRunButton.Enabled:=false;
Screen.Cursor := crHourglass;
SetLength(Plot.AbsResults,Length(Plot.Plants));
SetLength(Plot.PhotResults,Length(Plot.Plants));
for I := 0 to High(Plot.Plants) do
// Initialise and calculate
begin
// Set up integration points
DoGP(Plot.Plants[I]);
// The actual work
GIR:=Plot.Plants[I].Calculate;
// Save results
Plot.AbsResults[I]:=GIR[0];
Plot.PhotResults[i]:=GIR[1];
ProgressBar1.StepIt;
TimeLabeledEdit.Text:=FloatToStr((GetTickCount64-RunTime)/1000);
// Keep application responsive
Application.ProcessMessages;
end;
finally
Screen.Cursor := crDefault;
MainRunButton.Enabled:=true;
PageControl1.Enabled:=true;
end;
end;
procedure TMainForm.IndTreeRunButtonClick(Sender: TObject);
var
RunTime : DWord;
Ellipsoid : TEllipsoid;
Env : TEnvironment;
Phot : TPhotosynthesis;
tmpS : String;
GIR : GIResult;
begin
runtime:=GetTickCount64;
Ellipsoid:= TEllipsoid.Create;
Env:=Ellipsoid.Ring.Ellipse.Env;
Phot:=Ellipsoid.Ring.Ellipse.Env.Photosynthesis;
try // Make sure Ellipsoid is cleaned up
// Set Gaussian points
DoGP(Ellipsoid);
// Set up crown dimensions
Env.Crown.a:=StrToFloat(ModelValueListEditor.Cells[1,1]);
Env.Crown.b:=StrToFloat(ModelValueListEditor.Cells[1,2]);
Env.Crown.c:=StrToFloat(ModelValueListEditor.Cells[1,3]);
// Set up vegetation parameters
Env.Vegetation.r_gap:=StrToFloat(ModelValueListEditor.Cells[1,4]);
Env.Vegetation.h_t:= StrToFloat(ModelValueListEditor.Cells[1,5]);
Env.Vegetation.h_b:= StrToFloat(ModelValueListEditor.Cells[1,6]);
// Add some leaves and leaf angle classes
Env.Crown.F.Clear;
Env.Crown.F.LADist:=LADistComboBox.ItemIndex;
Env.Crown.F.F:=StrToFloat(ModelValueListEditor.Cells[1,7]);
If HorLeafCheckBox.Checked then
begin
Env.Crown.F.addclassD(0.5,1,1);
end
else
begin
Env.Crown.F.addclassD(15,30, StrToFloat(ModelValueListEditor.Cells[1,8]));
Env.Crown.F.addclassD(45,30, StrToFloat(ModelValueListEditor.Cells[1,9]));
Env.Crown.F.addclassD(75,30, StrToFloat(ModelValueListEditor.Cells[1,10]));
end;
Env.Crown.F.fractionise;
Env.Crown.F.a_L:=StrToFloat(ModelValueListEditor.Cells[1,11]);
// Add some leaves and leaf angle classes
Env.Vegetation.F.Clear;
Env.Vegetation.F.LADist:=LADistComboBox.ItemIndex;
Env.Vegetation.F.F:=StrToFloat(ModelValueListEditor.Cells[1,12]);
If HorLeafCheckBox.Checked then
begin
Env.Vegetation.F.addclassD(0.5,1,1);
end
else
begin
Env.Vegetation.F.addclassD(15,30, StrToFloat(ModelValueListEditor.Cells[1,13]));
Env.Vegetation.F.addclassD(45,30, StrToFloat(ModelValueListEditor.Cells[1,14]));
Env.Vegetation.F.addclassD(75,30, StrToFloat(ModelValueListEditor.Cells[1,15]));
end;
Env.Vegetation.F.fractionise;
Env.Vegetation.F.a_L:=StrToFloat(ModelValueListEditor.Cells[1,16]);
Env.Light.c:=StrToFloat(ModelValueListEditor.Cells[1,17]);
Env.Light.I_H:=StrToFloat(ModelValueListEditor.Cells[1,18]);
// Set Integration intervals (after clearing and init of F's)
DoIntervals(Ellipsoid);
Phot.N_top:=StrToFloat(PhotValueListEditor.Cells[1,1]);
Phot.a_N:=StrToFloat(PhotValueListEditor.Cells[1,2]);
Phot.a_P:=StrToFloat(PhotValueListEditor.Cells[1,3]);
Phot.b_P:=StrToFloat(PhotValueListEditor.Cells[1,4]);
tmpS:=PhotValueListEditor.Cells[1,5];
if tmpS='' then
begin
Phot.p_lin:=true;
tmpS:='0';
end
else
Phot.p_lin:=false;
Phot.c_P:=StrToFloat(tmpS);
Phot.a_R:=StrToFloat(PhotValueListEditor.Cells[1,6]);
Phot.b_R:=StrToFloat(PhotValueListEditor.Cells[1,7]);
Phot.phi:=StrToFloat(PhotValueListEditor.Cells[1,8]);
Phot.theta:=StrToFloat(PhotValueListEditor.Cells[1,9]);
// Calculate absorption&photosynthesis
GIR:=Ellipsoid.Calculate;
AbsLightEdit.Text:=FloatToStr(GIR[0]);
PhotoSynthEdit.Text:=FloatToStr(GIR[1]);
// Crown volume
Edit7.Text:=FloatToStr(4/3*pi*Env.Crown.a*Env.Crown.b*Env.Crown.c);
finally
FreeAndNil(Ellipsoid);
end;
// Timing
runtime:=GetTickCount64-RunTime;
RuntimeLabeledEdit.Text:=inttoStr(round(runtime/1000));
end;
procedure TMainForm.SelectFileButtonClick(Sender: TObject);
var
toReadPlot : ReadPlot;
begin
OpenDialog.Filter:='Microsoft Excel files (*.xls)|*.XLS;*.xls|CSV files (*.csv)|*.CSV;*.csv';
OpenDialog.Title:='Open';
OpenDialog.FileName:='';
OpenDialog.InitialDir:=GetCurrentDir;
try
// Prevent user action during calculations
PageControl1.Enabled:=false;
Screen.Cursor := crHourglass;
toReadPlot:= ReadPlot.Create;
if OpenDialog.Execute then
begin
MainRunButton.Enabled:=false;
ProgressBar1.Position:=0;
FilenameEdit.Text:=OpenDialog.FileName;
Plot.Clear;
// Update UI
Application.ProcessMessages;
CountLabeledEdit.Text:=IntToStr(toReadPlot.ReadData(Plot,OpenDialog.FileName));
end;
finally
FreeAndNil(toReadPlot);
MainRunButton.Enabled:=true;
Screen.Cursor := crDefault;
PageControl1.Enabled:=true;
end;
end;
procedure TMainForm.SaveButtonClick(Sender: TObject);
var
WriteExcel : TWriteExcel;
begin
SaveOpenDialog.Filter:='Microsoft Excel files (*.xls)|*.XLS;*.xls';
SaveOpenDialog.Title:='Save';
SaveOpenDialog.FileName:='';
SaveOpenDialog.InitialDir:=GetCurrentDir;
if SaveOpenDialog.Execute then
begin
WriteExcel:=TWriteExcel.Create;
if not WriteExcel.Write(Plot, SaveOpenDialog.FileName) then
Application.MessageBox('Error writing results file','Error',MB_OK)
else
Application.MessageBox('Succesfully written file','Succes',MB_OK);
FreeAndNil(WriteExcel);
If SaveOpenCheckBox.Checked then
OpenDocument(PChar(SaveOpenDialog.FileName)); { *Converted from ShellExecute* }
end;
end;
function TMainForm.getVersion : string;
var
FileVerInfo: TFileVersionInfo;
version_: string;
begin
FileVerInfo := TFileVersionInfo.Create(nil);
try
FileVerInfo.FileName := ParamStr(0);
FileVerInfo.ReadFileInfo;
version_ := FileVerInfo.VersionStrings.Values['FileVersion'];
// Cut off build number
version_ := LeftStr(version_, RPos('.', version_) - 1);
finally
FreeAndNil(FileVerInfo);
end;
Result := version_;
end;
end.