[go: up one dir, main page]

Menu

[r7]: / llp.pas  Maximize  Restore  History

Download this file

511 lines (448 with data), 16.9 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
 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
{* ***** BEGIN LICENSE BLOCK ***** *}
{* Version: MPL 1.1 *}
{* *}
{* The contents of this file are subject to the Mozilla Public License *}
{* Version 1.1 (the "License"); you may not use this file except in *}
{* compliance with the License. You may obtain a copy of the License at *}
{* http://www.mozilla.org/MPL/ *}
{* *}
{* Software distributed under the License is distributed on an "AS IS" basis, *}
{* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License *}
{* for the specific language governing rights and limitations under the *}
{* License. *}
{* *}
{* The Original Code is Census *}
{* *}
{* The Initial Developer of the Original Code is Justin Wilkins *}
{* *}
{* Portions created by Justin Wilkins are Copyright (C)2001-2011 *}
{* Justin Wilkins. All Rights Reserved. *}
{* *}
{* Contributor(s): *}
{* *}
{* ***** END LICENSE BLOCK ***** *}
unit llp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math, Mask, JvToolEdit, JvMaskEdit, JvSpin, JvComponent,
ExtCtrls, Brkapart, Registry, JvExControls, JvxCheckListBox, JvExMask;
type
TfrmLLP = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
edtOutput: TJvDirectoryEdit;
edtRunStart: TJvSpinEdit;
Label1: TLabel;
GroupBox3: TGroupBox;
lstParams: TJvxCheckListBox;
GroupBox4: TGroupBox;
chk60: TCheckBox;
chk50: TCheckBox;
chk40: TCheckBox;
chk30: TCheckBox;
chk20: TCheckBox;
chk15: TCheckBox;
chk10: TCheckBox;
chk5: TCheckBox;
chkScript: TCheckBox;
edtScript: TEdit;
btnCancel: TButton;
btnGo: TButton;
Panel1: TPanel;
Label2: TLabel;
Label3: TLabel;
cbxFormat: TComboBox;
brkUp: TBrkApart;
edtInput: TEdit;
GroupBox5: TGroupBox;
edtLabel: TEdit;
Label4: TLabel;
brkUp2: TBrkApart;
chk80: TCheckBox;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnGoClick(Sender: TObject);
procedure CreateOutput(inFile: string);
procedure FormCreate(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
function SigFigs(const Value: Extended; const SigFigCount: Integer): string;
procedure chkScriptClick(Sender: TObject);
function UnixSave(strOut: TStrings; FileName: string): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmLLP: TfrmLLP;
implementation
uses main;
{$R *.dfm}
procedure TfrmLLP.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
frmLLP := nil;
end;
procedure TfrmLLP.btnGoClick(Sender: TObject);
begin
if FileExists(edtInput.Text) then
begin
CreateOutput(edtInput.Text);
end;
end;
procedure TfrmLLP.CreateOutput(inFile: string);
var
strIn, strOut, strStore: TStrings;
arPercs, strBatch, strEta: TStrings;
strTemp: string;
n, m, p, q, r, s, intTheta, intTemp, intLine, intCt: Integer;
intProblem, intEtaStart, intEtaEnd, intSigmaStart, intSigmaEnd: Integer;
regIni: TRegIniFile;
begin
strIn := TStringList.Create;
strOut := TStringList.Create;
arPercs := TStringList.Create;
strStore := TStringList.Create;
strBatch := TStringList.Create;
strEta := TStringList.Create;
intLine := 0;
intProblem := 0;
intEtaStart := 0;
intEtaEnd := 0;
intSigmaStart := 0;
intSigmaEnd := 0;
intCt := edtRunStart.AsInteger;
if chk80.Checked then
arPercs.Add('-0.8');
if chk60.Checked then
arPercs.Add('-0.6');
if chk50.Checked then
arPercs.Add('-0.5');
if chk40.Checked then
arPercs.Add('-0.4');
if chk30.Checked then
arPercs.Add('-0.3');
if chk20.Checked then
arPercs.Add('-0.2');
if chk15.Checked then
arPercs.Add('-0.15');
if chk10.Checked then
arPercs.Add('-0.1');
if chk5.Checked then
arPercs.Add('-0.05');
if chk5.Checked then
arPercs.Add('0.05');
if chk10.Checked then
arPercs.Add('0.1');
if chk15.Checked then
arPercs.Add('0.15');
if chk20.Checked then
arPercs.Add('0.2');
if chk30.Checked then
arPercs.Add('0.3');
if chk40.Checked then
arPercs.Add('0.4');
if chk50.Checked then
arPercs.Add('0.5');
if chk60.Checked then
arPercs.Add('0.6');
if chk80.Checked then
arPercs.Add('0.8');
strIn.LoadFromFile(inFile);
intTheta := 0;
brkUp.AllowEmptyString := False;
for n := 0 to lstParams.Items.Count - 1 do
begin
strStore.Clear;
if lstParams.State[n] = cbChecked then
begin
// Thetas
if Pos('Th', lstParams.Items[n]) > 0 then
begin
strTemp := Trim(Copy(lstParams.Items[n], 3, 3));
intTemp := StrToInt(strTemp);
frmNMRun.tblThetas.First;
while intTemp <> frmNMRun.tblThetasTheta.Value do
frmNMRun.tblThetas.Next;
for p := 0 to arPercs.Count - 1 do
begin
strStore.Add('$THETA ' +
SigFigs(frmNMRun.tblThetasThetaValue.Value +
(frmNMRun.tblThetasThetaValue.Value * StrToFloat(arPercs[p])), 3) + ' FIX ; ' +
frmNMRun.tblThetasThetaLabel.Value + ' | ' + FloatToStr(StrToFloat(arPercs[p]) * 100) +
'%');
end;
//ShowMessage(strStore.Text);
for m := 0 to strIn.Count - 1 do
begin
if Pos('$THETA', Trim(strIn[m])) = 1 then
if Pos(frmNMRun.tblThetasThetaLabel.Value, strIn[m]) > 0 then
intLine := m;
if Pos('$PROB', Trim(strIn[m])) = 1 then
intProblem := m;
if (Pos('$SIGMA', Trim(strIn[m])) = 1) and (intSigmaStart = 0) then
intSigmaStart := m;
if Pos('$SIGMA', Trim(strIn[m])) = 1 then
intSigmaEnd := m;
end;
strOut.Assign(strIn);
if DirectoryExists(edtOutput.LongName) = False then
try
CreateDir(edtOutput.LongName);
except
MessageDlg('Unable to create the specified directory.', mtError,
[mbOK], 0);
Exit;
end;
regIni := TRegIniFile.Create('Software\Arrhytmia\Drumsticks');
for m := 0 to strStore.Count - 1 do
begin
strOut[intLine] := strStore[m];
strOut[intProblem] := StringReplace(StringReplace('$PROB ' + edtLabel.Text,
'{P}', frmNMRun.tblThetasThetaLabel.Value, [rfReplaceAll]),
'{%}', '(' + FloatToStr(StrToFloat(arPercs[m]) * 100) + '%)', [rfReplaceAll]);
try
if cbxFormat.ItemIndex = 0 then
strOut.SaveToFile(edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod'))
else
UnixSave(strOut, edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod'));
except
MessageDlg('Unable to create an output file (' + edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod') + ').', mtError,
[mbOK], 0);
Exit;
end;
strBatch.Add(regIni.ReadString('Options', 'NMVersion', 'nmfe5') + ' ' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod') +
' ' + regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtLst', '.lst'));
intCt := intCt + 1;
end;
end;
// Etas
if Pos('Om', lstParams.Items[n]) > 0 then
begin
strTemp := Trim(Copy(lstParams.Items[n], 3, 3));
intTemp := StrToInt(strTemp);
strEta.Clear;
frmNMRun.tblEtas.First;
while intTemp <> frmNMRun.tblEtasEta.Value do
frmNMRun.tblEtas.Next;
for p := 0 to arPercs.Count - 1 do
begin
strStore.Add(
SigFigs(frmNMRun.tblEtasEtaValue.Value +
(frmNMRun.tblEtasEtaValue.Value * StrToFloat(arPercs[p])), 3) + ' FIX ; ' +
frmNMRun.tblEtasEtaLabel.Value + ' | ' + FloatToStr(StrToFloat(arPercs[p]) * 100) +
'%');
end;
for m := 0 to strIn.Count - 1 do
begin
if Pos('$PROB', Trim(strIn[m])) = 1 then
intProblem := m;
if (Pos('$OMEGA', Trim(strIn[m])) = 1) and (intEtaStart = 0) then
intEtaStart := m;
if Pos('$OMEGA', Trim(strIn[m])) = 1 then
intEtaEnd := m;
end;
strTemp := '';
for p := intEtaStart to intEtaEnd do
strTemp := strTemp + ' ' + strIn[p];
brkUp.StringList.Clear;
brkUp.BaseString := strTemp;
brkUp.BreakString := '$OMEGA';
brkUp.BreakApart;
for r := 0 to brkUp.StringList.Count - 1 do
begin
if Pos('BLOCK(1)', brkUp.StringList[r]) = 0 then
begin
brkUp2.StringList.Clear;
brkUp2.BaseString := brkUp.StringList[r];
brkUp2.BreakString := ' ';
brkUp2.BreakApart;
for s := 0 to brkUp2.StringList.Count - 1 do
strEta.Add(brkUp2.StringList[s]);
end
else
strEta.Add(brkUp.StringList[r]);
end;
// ShowMessage(strEta.Text);
for m := 0 to strStore.Count - 1 do
begin
strOut.Clear;
for p := 0 to intEtaStart - 1 do
strOut.Add(strIn[p]);
for s := 0 to strEta.Count - 1 do
if s = intTemp - 1 then
begin
if Pos('BLOCK(1)', strEta[s]) > 0 then
strOut.Add('$OMEGA BLOCK(1) ' + Trim(strStore[m]))
else
strOut.Add('$OMEGA ' + Trim(strStore[m]));
end
else
strOut.Add('$OMEGA ' + Trim(strEta[s]));
for p := intEtaEnd + 1 to strIn.Count - 1 do
strOut.Add(strIn[p]);
strOut[intProblem] := StringReplace(StringReplace('$PROB ' + edtLabel.Text,
'{P}', 'IIV-' + frmNMRun.tblEtasEtaLabel.Value + ' (' +
IntToStr(frmNMRun.tblEtasEta.Value) + ')', [rfReplaceAll]),
'{%}', '(' + FloatToStr(StrToFloat(arPercs[m]) * 100) + '%)', [rfReplaceAll]);
if DirectoryExists(edtOutput.LongName) = False then
try
CreateDir(edtOutput.LongName);
except
MessageDlg('Unable to create the specified directory.', mtError,
[mbOK], 0);
Exit;
end;
regIni := TRegIniFile.Create('Software\Arrhytmia\Drumsticks');
try
if cbxFormat.ItemIndex = 0 then
strOut.SaveToFile(edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod'))
else
UnixSave(strOut, edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod'));
except
MessageDlg('Unable to create an output file (' + edtOutput.LongName + '\' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod') + ').', mtError,
[mbOK], 0);
Exit;
end;
strBatch.Add(regIni.ReadString('Options', 'NMVersion', 'nmfe5') + ' ' +
regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtCtl', '.mod') +
' ' + regIni.ReadString('Options', 'RunPrefix', 'run') +
IntToStr(intCt) + regIni.ReadString('Options', 'ExtLst', '.lst'));
intCt := intCt + 1;
end;
end;
end;
end;
strIn.Free;
strOut.Free;
arPercs.Free;
strStore.Free;
strEta.Free;
if chkScript.Checked then
if cbxFormat.ItemIndex = 0 then
strBatch.SaveToFile(edtOutput.LongName + '\' + edtScript.Text)
else
UnixSave(strBatch, edtOutput.LongName + '\' + edtScript.Text);
MessageDlg('Completed! Files have been placed in the specified directory.',
mtInformation, [mbOK], 0);
Close;
end;
procedure TfrmLLP.FormCreate(Sender: TObject);
var
n: Integer;
begin
edtInput.Text := frmNMRun.tblRunsCtl.Value;
edtLabel.Text := frmNMRun.tblRunsComment.Value + ' - LLP - {P} {%}';
frmNMRun.tblThetas.First;
for n := 0 to frmNMRun.tblThetas.RecordCount - 1 do
begin
if frmNMRun.tblThetasLower.Value <> frmNMRun.tblThetasUpper.Value then
lstParams.Items.Add('Th ' + IntToStr(frmNMRun.tblThetasTheta.Value) +
' (' + frmNMRun.tblThetasThetaLabel.Value + ')');
frmNMRun.tblThetas.Next;
end;
frmNMRun.tblEtas.First;
for n := 0 to frmNMRun.tblEtas.RecordCount - 1 do
begin
lstParams.Items.Add('Om ' + IntToStr(frmNMRun.tblEtasEta.Value) +
' (' + frmNMRun.tblEtasEtaLabel.Value + ')');
frmNMRun.tblEtas.Next;
end;
frmNMRun.tblSigmas.First;
for n := 0 to frmNMRun.tblSigmas.RecordCount - 1 do
begin
lstParams.Items.Add('Sg ' + IntToStr(frmNMRun.tblSigmasSigma.Value));
frmNMRun.tblSigmas.Next;
end;
edtOutput.Text := ExtractFilePath(frmNMRun.tblRunsCtl.Value);
edtRunStart.Value := frmNMRun.tblRunsiRunNo.Value + 1;
end;
procedure TfrmLLP.btnCancelClick(Sender: TObject);
begin
Close;
end;
{
This (imperfect) function formats a value to a given number of significant
figures.
Sometimes an extra digit will be returned, for example the following
assertion
fails:
Assert(SigFigs(0.001, 1) = '0.001'); // SigFigs returns '0.0010'
Author: Gary Williams
}
function TfrmLLP.SigFigs(const Value: Extended; const SigFigCount: Integer): string;
var
DecimalCount: Integer; // may be negative
Scale: Extended;
begin
Assert(SigFigCount > 0);
if (Value = 0.0) then
DecimalCount := SigFigCount - 1
else
DecimalCount := SigFigCount - Floor(Log10(Abs(Value))) - 1;
if (DecimalCount >= 0) then
Result := Format('%1.' + IntToStr(DecimalCount) + 'f', [Value])
else
begin
Scale := IntPower(10.0, DecimalCount);
Result := Format('%1.0f', [Round(Value * Scale) / Scale]); // change superfluous digits to '0'
end;
end;
procedure TfrmLLP.chkScriptClick(Sender: TObject);
begin
if chkScript.Checked then
edtScript.Enabled := True
else
edtScript.Enabled := False;
end;
function TfrmLLP.UnixSave(strOut: TStrings; FileName: string): Boolean;
const
CR = $0D;
LF = $0A;
zero: Byte = 0;
var
stmIn: TMemoryStream;
pcData: PChar;
pcCR: PChar;
strLine: string;
begin
stmIn := TMemoryStream.Create;
try
strOut.SaveToStream(stmIn);
if stmIn.Size > 0 then begin
stmIn.Seek(0, soFromEnd);
stmIn.Write(zero, 1);
pcData := PChar(stmIn.Memory);
repeat
pcCR := StrScan(pcData, #13);
if pcCR <> nil then begin
pcCR[0] := #32;
// strLine := StrPas(pcData);
// strLine := strLine + #13#10;
// Do what you need with the line here.
pcData := pcCR + 1;
end;
until pcCR = nil;
end;
finally
stmIn.SaveToFile(FileName);
stmIn.Free;
end;
end;
end.