-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser_int.pas
548 lines (479 loc) · 17.9 KB
/
User_int.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
unit User_int;
{$MODE objfpc}{$H+}
// Main user interface
// Copyright (c), 2004-2006, Roelof Oomen
interface
uses
Classes, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls,
Calc;
type
{ TForm1 }
TForm1 = class(TForm)
BS1Edit: TLabeledEdit;
BS2Edit: TLabeledEdit;
BS3Edit: TLabeledEdit;
BL1Edit: TLabeledEdit;
BL2Edit: TLabeledEdit;
BL3Edit: TLabeledEdit;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
PageControl1: TPageControl;
FileOpenTabSheet: TTabSheet;
Label1: TLabel;
Label3: TLabel;
Label4: TLabel;
Label2: TLabel;
Label5: TLabel;
Label12: TLabel;
Edit1: TEdit;
FileOpenButton: TButton;
DataMemo: TMemo;
Edit2: TEdit;
SubplotCountEdit: TEdit;
IndCountEdit: TEdit;
SpeciesCountEdit: TEdit;
PlotNameEdit: TEdit;
OptionSheet: TTabSheet;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label10: TLabel;
Label11: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
Label13: TLabel;
Label14: TLabel;
Bevel4: TBevel;
Label15: TLabel;
Bevel6: TBevel;
RunButton: TButton;
SpeciesListBox: TListBox;
SubplotListBox: TListBox;
IndListBox: TListBox;
CalcAlphaCheckBox: TCheckBox;
File_kCheckBox: TCheckBox;
DateEdit: TEdit;
DateUpDown: TUpDown;
OvercastCheckBox: TCheckBox;
I_difEdit: TEdit;
I_difUpDown: TUpDown;
GDayUpDown: TUpDown;
GDayEdit: TLabeledEdit;
f_RdUpDown: TUpDown;
ProgressBar1: TProgressBar;
DisplayButton: TButton;
GLayerEdit: TLabeledEdit;
GLayerUpDown: TUpDown;
ResultTabSheet: TTabSheet;
ResultMemo: TMemo;
FileSaveTabSheet: TTabSheet;
Label9: TLabel;
SaveButton: TButton;
LightAbsOutCheckBox: TCheckBox;
PhotosynthOutCheckBox: TCheckBox;
LayersOutCheckBox: TCheckBox;
Label19: TLabel;
Bevel7: TBevel;
Bevel8: TBevel;
Bevel9: TBevel;
Bevel10: TBevel;
RuntimeEdit: TEdit;
Label29: TLabel;
Label30: TLabel;
Label31: TLabel;
Label32: TLabel;
Label33: TLabel;
f_RdEdit: TEdit;
Label44: TLabel;
AngleOptsCheckBox: TCheckBox;
Panel1: TPanel;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
Label40: TLabel;
Label41: TLabel;
BS3UpDown: TUpDown;
BS2UpDown: TUpDown;
BS1UpDown: TUpDown;
Bevel11: TBevel;
Label37: TLabel;
Label38: TLabel;
Label39: TLabel;
Label42: TLabel;
Label43: TLabel;
BL3UpDown: TUpDown;
BL2UpDown: TUpDown;
BL1UpDown: TUpDown;
procedure ExitButtonClick(Sender: TObject);
procedure FileOpenButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure IndListBoxClick(Sender: TObject);
procedure SubplotListBoxClick(Sender: TObject);
procedure SpeciesListBoxClick(Sender: TObject);
procedure RunButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure LightAbsOutCheckBoxClick(Sender: TObject);
procedure OvercastCheckBoxClick(Sender: TObject);
procedure f_RdUpDownClick(Sender: TObject);
procedure DisplayButtonClick(Sender: TObject);
procedure f_RdEditChange(Sender: TObject);
procedure AngleOptsCheckBoxClick(Sender: TObject);
private
{ Private declarations }
procedure SetOptionsPlotM;
procedure ResetOptions;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
Controls, Contnrs, LazLogger, SysUtils, strutils,
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}
Data_defs, Data_RW, Uhashtable;
var
PlotM: TPlot; // Main Data Structure
WorkBook: TPlotWB; // For reading the data
Output: TWriteExcel; // For saving the output
Calculate: TCalc; // For calculating
version: string;
function 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;
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.PageControl1.ActivePageIndex := 0; // Select first TabSheet
DataMemo.DoubleBuffered := True;
Application.HintHidePause := 15000; // Make hints (tooltips) visible for 15s
version := getVersion; // Retrieve project version information
Form1.Caption := Form1.Caption + ' v.' + version;
end;
procedure TForm1.FileOpenButtonClick(Sender: TObject);
var
i: integer;
begin
OpenDialog1.Filter :=
'Spreadsheet files (*.xls;*.xlsx;*.ods)|*.xls;*.XLS;*.xlsx;*.XLSX;*.ods;*.ODS|All files|*.*';
if opendialog1.Execute then
Edit1.Text := ExtractFileName(OpenDialog1.FileName)
else
exit;
WorkBook := TPlotWB.Create;
if FileExists(OpenDialog1.FileName) then // Check if file exists
try { Finally statement below makes sure the cursor get back to normal }
Screen.Cursor := crHourglass;
FileOpenButton.Enabled := False;
DataMemo.Clear;
SpeciesListBox.Clear;
SubplotListBox.Clear;
IndListBox.Clear;
ResultMemo.Clear;
FreeAndNil(PlotM); // Make sure all data is cleared, in case data was read in before
PlotM := TPlot.Create;
WorkBook.PlotWB := PlotM;
WorkBook.filename := OpenDialog1.FileName;
i := WorkBook.WBRead;
if i = 0 then
begin
OptionSheet.Enabled := True;
ResetOptions;
PlotNameEdit.Text := PlotM.Name;
SpeciesCountEdit.Text := IntToStr(PlotM.Species.Count);
SubplotCountEdit.Text := IntToStr(PlotM.SubPlot.Count);
IndCountEdit.Text := IntToStr(PlotM.Individual.Count);
if IndListBox.Count > 0 then // Select first Individual
begin
IndListBox.ItemIndex := 0;
i := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).subplot.hash;
SubplotListBox.ItemIndex := PlotM.HTSubPlot.Getindex(i);
i := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).species.hash;
SpeciesListBox.ItemIndex := PlotM.HTSpecies.Getindex(i);
end;
end
else
Edit1.Text := '--error ' + IntToStr(i) + ' reading file: select another file--';
finally
FileOpenButton.Enabled := True;
Screen.Cursor := crDefault;
end
else
MessageDlg('File not found.', mtInformation, [mbOK], 0);
FreeAndNil(Workbook);
end;
{
Reset everything in the model options tab to defaults
}
procedure TForm1.ResetOptions;
begin
File_kCheckBox.Checked := PlotM.k_veg_data;
CalcAlphaCheckBox.Checked := not PlotM.alpha_const;
I_difUpDown.Position := PlotM.I_overcast;
OvercastCheckBox.Checked := PlotM.Overcast;
DateUpDown.Position := PlotM.doy;
f_RdEdit.Text := FloatToStr(f_Rd_def);
// Default leaf angles
BS1UpDown.Position := round(rad2deg(r15));
BS2UpDown.Position := round(rad2deg(r45));
BS3UpDown.Position := round(rad2deg(r75));
BL1UpDown.Position := round(rad2deg(r15));
BL2UpDown.Position := round(rad2deg(r45));
BL3UpDown.Position := round(rad2deg(r75));
// Integration steps
GDayUpDown.Position:=8;
GLayerUpDown.Position:=5;
ProgressBar1.Position := 0; // Indicate not calculated
RuntimeEdit.Text := '';
end;
{
Initialise calculation with options from GUI
Debug output to check that all GUI settings are applied correctly
}
procedure TForm1.SetOptionsPlotM;
var
i: integer;
begin
// Write options to Plot
PlotM.k_veg_data := File_kCheckBox.Checked;
PlotM.Alpha_const := not CalcAlphaCheckBox.Checked;
PlotM.I_overcast := I_difUpDown.Position;
PlotM.Overcast := OvercastCheckBox.Checked;
PlotM.doy := DateUpDown.Position;
DebugLn('k_veg_data: ' + BoolToStr(PlotM.k_veg_data));
DebugLn('alpha_const: ' + BoolToStr(PlotM.alpha_const));
DebugLn('I_overcast: ' + IntToStr(PlotM.I_overcast));
DebugLn('Overcast: ' + BoolToStr(PlotM.Overcast));
DebugLn('doy: ' + inttostr(PlotM.doy));
// Apply Night Respiration Constant to all Species
for i := 0 to PlotM.Species.Count - 1 do
begin
TSpecies(PlotM.Species[i]).f_Rd := f_RdUpDown.position / 100;
if i = 0 then DebugLn('f_Rd: '+ FloatToStr(TSpecies(PlotM.Species[i]).f_Rd));
end;
// Apply sun angles to all individuals
for i := 0 to PlotM.Individual.Count - 1 do
begin
TIndividual(PlotM.Individual[i]).Bs1 := deg2rad(BS1UpDown.Position);
TIndividual(PlotM.Individual[i]).Bs2 := deg2rad(BS2UpDown.Position);
TIndividual(PlotM.Individual[i]).Bs3 := deg2rad(BS3UpDown.Position);
if i = 0 then
begin
DebugLn('BS1: '+ FloatToStr(round(rad2deg(TIndividual(PlotM.Individual[i]).Bs1))));
DebugLn('BS2: '+ FloatToStr(round(rad2deg(TIndividual(PlotM.Individual[i]).Bs2))));
DebugLn('BS3: '+ FloatToStr(round(rad2deg(TIndividual(PlotM.Individual[i]).Bs3))));
end;
end;
// Apply sun angles to all subplots
for i := 0 to PlotM.SubPlot.Count - 1 do
begin
TSubPlot(PlotM.Subplot[i]).Bs1 := deg2rad(BS1UpDown.Position);
TSubPlot(PlotM.Subplot[i]).Bs2 := deg2rad(BS2UpDown.Position);
TSubPlot(PlotM.Subplot[i]).Bs3 := deg2rad(BS3UpDown.Position);
end;
// Apply leaf angles to all species
for i := 0 to PlotM.Species.Count - 1 do
begin
TSpecies(PlotM.Species[i]).Bl1 := deg2rad(BL1UpDown.Position);
TSpecies(PlotM.Species[i]).Bl2 := deg2rad(BL2UpDown.Position);
TSpecies(PlotM.Species[i]).Bl3 := deg2rad(BL3UpDown.Position);
if i = 0 then
begin
DebugLn('Bl1: '+ FloatToStr(round(rad2deg(TSpecies(PlotM.Species[i]).Bl1))));
DebugLn('Bl2: '+ FloatToStr(round(rad2deg(TSpecies(PlotM.Species[i]).Bl2))));
DebugLn('Bl3: '+ FloatToStr(round(rad2deg(TSpecies(PlotM.Species[i]).Bl3))));
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(PlotM);
end;
procedure TForm1.IndListBoxClick(Sender: TObject);
var
t: integer;
begin
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).subplot.hash;
SubplotListBox.ItemIndex := PlotM.HTSubPlot.Getindex(t);
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).species.hash;
SpeciesListBox.ItemIndex := PlotM.HTSpecies.Getindex(t);
end;
procedure TForm1.SubplotListBoxClick(Sender: TObject);
var
t: integer;
begin
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).subplot.hash;
SubplotListBox.ItemIndex := PlotM.HTSubPlot.Getindex(t);
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).subplot.hash;
SpeciesListBox.ItemIndex := PlotM.HTSpecies.Getindex(t);
end;
procedure TForm1.SpeciesListBoxClick(Sender: TObject);
var
t: integer;
begin
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).subplot.hash;
SubplotListBox.ItemIndex := PlotM.HTSubPlot.Getindex(t);
t := TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).species.hash;
SpeciesListBox.ItemIndex := PlotM.HTSpecies.Getindex(t);
end;
procedure TForm1.LightAbsOutCheckBoxClick(Sender: TObject);
begin
if LightAbsOutCheckBox.Checked then
PhotosynthOutCheckBox.State := cbChecked
else
PhotosynthOutCheckBox.State := cbGrayed;
end;
procedure TForm1.OvercastCheckBoxClick(Sender: TObject);
begin
I_difUpDown.Enabled := OvercastCheckBox.Checked;
I_difEdit.Enabled := OvercastCheckBox.Checked;
end;
procedure TForm1.f_RdUpDownClick(Sender: TObject);
begin
f_RdEdit.Text := floattostr(f_RdUpDown.position / 100);
end;
procedure TForm1.f_RdEditChange(Sender: TObject);
var
i: integer;
begin
i := round(strtofloat(f_RdEdit.Text) * 100);
if (i < 0) or (i > 100) then
f_RdEdit.Text := floattostr(f_RdUpDown.position / 100)
else
f_RdUpDown.position := round(strtofloat(f_RdEdit.Text) * 100);
end;
procedure TForm1.AngleOptsCheckBoxClick(Sender: TObject);
var
i: integer;
begin
Panel1.Enabled := AngleOptsCheckBox.Checked;
for i := 0 to Panel1.ControlCount - 1 do
Panel1.Controls[i].Enabled := AngleOptsCheckBox.Checked;
end;
procedure TForm1.RunButtonClick(Sender: TObject);
var
i: integer;
runtime: cardinal;
begin
runtime := GetTickCount64;
ProgressBar1.Position := 0;
try { Finally statement below makes sure cursor gets back to normal }
RunButton.Enabled := False;
Screen.Cursor := crHourglass;
Form1.ResultTabsheet.Enabled := True;
Form1.FileSaveTabsheet.Enabled := True;
Calculate := TCalc.Create(PlotM, GDayUpDown.Position, GLayerUpDown.Position);
DebugLn('-- Running calculations --');
DebugLn('GP_day: ' + IntToStr(Calculate.GP_day));
DebugLn('GP_layer: ' + IntToStr(Calculate.GP_layer));
SetOptionsPlotM;
ProgressBar1.Max := PlotM.Individual.Count;
for i := 0 to PlotM.Individual.Count - 1 do
begin
Calculate.calc(TIndividual(PlotM.Individual[i]).hash, PlotM.doy);
ProgressBar1.Position := i + 1;
end;
finally
RunButton.Enabled := True;
Screen.Cursor := crDefault;
FreeAndNil(Calculate);
end;
runtime := GetTickCount64 - RunTime;
RuntimeEdit.Text := IntToStr(round(runtime / 1000));
end;
procedure TForm1.DisplayButtonClick(Sender: TObject);
var
i: integer;
begin
try { Finally statement below makes sure cursor gets back to normal }
Screen.Cursor := crHourglass;
Form1.ResultTabsheet.Enabled := True;
Form1.FileSaveTabsheet.Enabled := True;
Calculate := TCalc.Create(PlotM, GDayUpDown.Position, GLayerUpDown.Position);
SetOptionsPlotM;
Calculate.calc(PlotM.HTIndividual.ELFHash(IndListBox.Items[IndListBox.ItemIndex]), PlotM.doy);
finally
Screen.Cursor := crDefault;
end;
ResultMemo.Text := 'Plant ' + TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(
IndListBox.Items[IndListBox.ItemIndex])]).Name;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Day photosynthesis';
for i := 0 to length(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(
IndListBox.Items[IndListBox.ItemIndex])]).layer) - 1 do
begin
ResultMemo.Text := ResultMemo.Text + #13 + #10 + floattostr(
TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).layer[i].P);
end;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Total:' + #13 + #10 +
floattostr(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).P_totd);
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Plant ' + TIndividual(
PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).Name;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Day photosynthesis-night resp.';
for i := 0 to length(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(
IndListBox.Items[IndListBox.ItemIndex])]).layer) - 1 do
begin
ResultMemo.Text := ResultMemo.Text + #13 + #10 + floattostr(
TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).layer[i].P -
TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).layer[i].R_dn);
end;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Total:' + #13 + #10 +
floattostr(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).P_totd -
TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).R_dntot);
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Plant ' + TIndividual(
PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).Name;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Day PPFD absorption';
for i := 0 to length(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(
IndListBox.Items[IndListBox.ItemIndex])]).layer) - 1 do
begin
ResultMemo.Text := ResultMemo.Text + #13 + #10 + floattostr(
TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).layer[i].I_abs);
end;
ResultMemo.Text := ResultMemo.Text + #13 + #10 + 'Total:' + #13 + #10 +
floattostr(TIndividual(PlotM.Individual[PlotM.HTIndividual.GetStrIndex(IndListBox.Items[IndListBox.ItemIndex])]).I_abstot);
FreeAndNil(Calculate);
Form1.PageControl1.ActivePageIndex := 2;
end;
procedure TForm1.SaveButtonClick(Sender: TObject);
begin
Output := TWriteExcel.Create;
Output.PlotW := PlotM;
// Set what to write to file
Output.layers := LayersOutCheckBox.Checked;
Output.light := LightAbsOutCheckBox.Checked;
case PhotosynthOutCheckBox.state of
cbUnchecked: Output.photosynth := False
else // cbChecked or cbGrayed
Output.photosynth := True;
end;
SaveDialog1.Filter := 'Excel files (*.xls)|*.xls;*.XLS';
if SaveDialog1.Execute then
Output.xlsname := SaveDialog1.FileName;
if Output.xlsname <> '' then
if not Output.Write then
MessageDlg('Error writing file: ' + Output.xlsname, mtInformation, [mbOK], 0);
FreeAndNil(Output);
end;
procedure TForm1.ExitButtonClick(Sender: TObject);
begin
Application.Terminate;
end;
end.