-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze8.bas
495 lines (383 loc) · 15.6 KB
/
analyze8.bas
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
Attribute VB_Name = "CodeAnalyze8"
' (c) Copyright 1995-2025 by John J. Donovan
Option Explicit
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
' FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
' IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
' Array of filenames for output
Dim arraysize As Integer
Dim filenamearray() As String
Dim MatrixAverages(1 To MAXCHAN% + 1, 1 To MAXZAF%) As Single ' wt%
Dim MatrixAverages2(1 To MAXCHAN% + 1, 1 To MAXZAF%) As Single ' at %
Sub AnalyzeChangeZAF(analysis As TypeAnalysis, sample() As TypeSample, stdsample() As TypeSample)
' Change ZAF selections for calculating all matrix corrections
ierror = False
On Error GoTo AnalyzeChangeZAFError
' Load individual selections
Call InitGetZAFSetZAF2(izaf%)
If ierror Then Exit Sub
' Print current ZAF selections
Call TypeZAFSelections
If ierror Then Exit Sub
' Load element arrays
Call ElementGetData(sample())
If ierror Then Exit Sub
' Load primary intensities (0 = phi/rho/z, 1,2,3,4 = alpha fits, 5 = calilbration curve, 6 = fundamental parameters)
If CorrectionFlag% <> MAXCORRECTION% Then
Call ZAFSetZAF(sample())
If ierror Then Exit Sub
Else
'Call ZAFSetZAF3(sample())
'If ierror Then Exit Sub
End If
' Update the standard kfacs based on changed conditions
Call UpdateAllStdKfacs(analysis, sample(), stdsample())
If ierror Then Exit Sub
Exit Sub
' Errors
AnalyzeChangeZAFError:
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeChangeZAF"
ierror = True
Exit Sub
End Sub
Sub AnalyzeCalculateAllSave(afilename As String, nsams As Integer, nstring As String, analysis As TypeAnalysis, sample() As TypeSample, tForm As Form)
' Save the results from all matrix corrections
ierror = False
On Error GoTo AnalyzeCalculateAllSaveError
Dim sampleline As Integer
Dim astring As String, lstring As String, tmsg As String
Dim tfilename As String, bstring As String
' Reset array size if first correction
If izaf% = 1 Then arraysize% = 0
' Load filename from database, sample name and zaf correction
tmsg$ = nstring$ & "_" & zafstring$(izaf%)
Call MiscModifyStringToFilename(tmsg$)
If ierror Then Exit Sub
' Create file name for output
tfilename$ = MiscGetFileNameNoExtension$(afilename$) & "-" & tmsg$ & ".dat"
' Store filename for subsequent Excel loading
Close (Temp1FileNumber%) ' close in case file was left open
arraysize% = arraysize% + 1
ReDim Preserve filenamearray$(1 To arraysize%)
filenamearray$(arraysize%) = tfilename$ ' already includes full path
Open tfilename$ For Output As #Temp1FileNumber%
' Write header
Print #Temp1FileNumber%, VbDquote$ & ProbeDataFile$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBUserName$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBFileTitle$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBFileDescription$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & "Nominal Beam: " & Format$(NominalBeam!) & VbDquote$
msg$ = macstring$(MACTypeFlag%)
Print #Temp1FileNumber%, VbDquote$ & msg$ & VbDquote$
Print #Temp1FileNumber%, vbNullString
' Output column labels
Call AnalyzeCalculateAllColumnString(lstring$, sample())
If ierror Then Exit Sub
Print #Temp1FileNumber%, lstring$
' Output each line
For sampleline% = 1 To sample(1).Datarows%
If sample(1).LineStatus(sampleline%) Then
' Output column data
Call AnalyzeCalculateAllDataString(astring$, sampleline%, analysis, sample())
If ierror Then Exit Sub
Print #Temp1FileNumber%, astring$
End If
Next sampleline%
' Calculate averages (bstring used below)
Call AnalyzeCalculateAllAverage(astring$, bstring$, analysis, sample())
If ierror Then Exit Sub
Print #Temp1FileNumber%, vbNullString
Print #Temp1FileNumber%, lstring$
Print #Temp1FileNumber%, astring$
Close (Temp1FileNumber%)
' Output just the average compositions only to separate ASCII file
tmsg$ = nstring$ & "_All Corrections"
Call MiscModifyStringToFilename(tmsg$)
If ierror Then Exit Sub
' Create filename for output
tfilename$ = MiscGetFileNameNoExtension$(afilename$) & "-" & tmsg$ & ".dat"
' Check if first time
If izaf% = 1 Then
Open tfilename$ For Output As #Temp1FileNumber%
Else
Open tfilename$ For Append As #Temp1FileNumber%
End If
' Write header
If izaf% = 1 Then
Print #Temp1FileNumber%, VbDquote$ & ProbeDataFile$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBUserName$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBFileTitle$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & MDBFileDescription$ & VbDquote$
Print #Temp1FileNumber%, VbDquote$ & "Nominal Beam: " & Format$(NominalBeam!) & VbDquote$
msg$ = macstring$(MACTypeFlag%)
Print #Temp1FileNumber%, VbDquote$ & msg$ & VbDquote$
Print #Temp1FileNumber%, "All Matrix Correction Averages Only"
Print #Temp1FileNumber%, vbNullString
End If
If izaf% = 1 Then
Print #Temp1FileNumber%, lstring$
End If
' Print numerical results with zafstring (do not need tab before bstring$)
Print #Temp1FileNumber%, VbDquote$ & zafstring(izaf%) & VbDquote$ & bstring$
' Store filename for subsequent Excel loading
If izaf% = MAXZAF% Then
arraysize% = arraysize% + 1
ReDim Preserve filenamearray$(1 To arraysize%)
filenamearray$(arraysize%) = tfilename$ ' already includes full path
End If
Close (Temp1FileNumber%)
' Confirm output files with user
If izaf% = MAXZAF% Then
If nsams% = 1 Then
tmsg$ = "All Matrix Corrections sample data were output to ASCII data files based on sample names"
MsgBox tmsg$, vbOKOnly + vbInformation, "AnalyzeCalculateAllSave"
End If
' Output to excel spreadsheet
Call OutputSaveCustom2SendToExcel(Int(0), arraysize%, filenamearray$(), tForm)
If ierror Then Exit Sub
' Print averages to log window
Call AnalyzeCalculateAllAverageOutput(nstring$, sample())
If ierror Then Exit Sub
End If
Exit Sub
' Errors
AnalyzeCalculateAllSaveError:
Close (Temp1FileNumber%)
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeCalculateAllSave"
ierror = True
Exit Sub
End Sub
Sub AnalyzeCalculateAllAverageOutput(nstring As String, sample() As TypeSample)
' Calculate average and output to log window
ierror = False
On Error GoTo AnalyzeCalculateAllAverageOutputError
Dim j As Integer, chan As Integer
Dim average As TypeAverage
' Print MAC and sample name
msg$ = "Summary of All Calculated (averaged) Matrix Corrections:"
Call IOWriteLog(vbCrLf & msg$)
msg$ = nstring$
Call IOWriteLog(msg$)
msg$ = macstring$(MACTypeFlag%)
Call IOWriteLog(msg$)
' Output wt% averages
msg$ = "Elemental Weight Percents:"
Call IOWriteLog(vbCrLf & msg$)
msg$ = "ELEM: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(sample(1).Elsyup$(chan%), a80$)
Next chan%
msg$ = msg$ & Format$("TOTAL", a80$)
Call IOWriteLog(msg$)
For j% = 1 To MAXZAF%
msg$ = Format$(j%, a60$)
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(MatrixAverages!(chan%, j%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(MatrixAverages!(sample(1).LastChan% + 1, j%), f83$), a80$)
Call IOWriteLog(msg$ & " " & zafstring$(j%))
Next j%
' Output standard deviation and standard error
Call MathArrayAverage3(average, MatrixAverages!(), CLng(MAXZAF%), sample(1).LastChan% + 1)
If ierror Then Exit Sub
msg$ = vbCrLf & "AVER: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.averags!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.averags!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "SDEV: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Stddevs!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Stddevs!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "SERR: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Stderrs!(chan%), f83$), a80$)
Next chan%
Call IOWriteLog(msg$)
msg$ = vbCrLf & "MIN: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Minimums!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Minimums!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "MAX: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Maximums!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Maximums!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
' Output at% averages
If sample(1).AtomicPercentFlag% Then
msg$ = "Atomic Percents:"
Call IOWriteLog(vbCrLf & msg$)
msg$ = "ELEM: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(sample(1).Elsyup$(chan%), a80$)
Next chan%
msg$ = msg$ & Format$("TOTAL", a80$)
Call IOWriteLog(msg$)
For j% = 1 To MAXZAF%
msg$ = Format$(j%, a60$)
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(MatrixAverages2!(chan%, j%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(MatrixAverages2!(sample(1).LastChan% + 1, j%), f83$), a80$)
Call IOWriteLog(msg$ & " " & zafstring$(j%))
Next j%
' Output standard deviation and standard error
Call MathArrayAverage3(average, MatrixAverages2!(), CLng(MAXZAF%), sample(1).LastChan% + 1)
If ierror Then Exit Sub
msg$ = vbCrLf & "AVER: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.averags!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.averags!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "SDEV: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Stddevs!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Stddevs!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "SERR: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Stderrs!(chan%), f83$), a80$)
Next chan%
Call IOWriteLog(msg$)
msg$ = vbCrLf & "MIN: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Minimums!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Minimums!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
msg$ = "MAX: "
For chan% = 1 To sample(1).LastChan%
msg$ = msg$ & Format$(Format$(average.Maximums!(chan%), f83$), a80$)
Next chan%
msg$ = msg$ & Format$(Format$(average.Maximums!(sample(1).LastChan% + 1), f83$), a80$)
Call IOWriteLog(msg$)
End If
Exit Sub
' Errors
AnalyzeCalculateAllAverageOutputError:
Close (Temp1FileNumber%)
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeCalculateAllAverageOutput"
ierror = True
Exit Sub
End Sub
Sub AnalyzeCalculateAllAverage(astring As String, bstring As String, analysis As TypeAnalysis, sample() As TypeSample)
' Calculate average ZAF for file output (CalculateAllMatrixCorrections)
ierror = False
On Error GoTo AnalyzeCalculateAllAverageError
Dim chan As Integer
Dim average2 As TypeAverage ' for weight percents
Dim average3 As TypeAverage ' for atomic percents
' Line number
astring$ = "AVER:"
bstring$ = vbNullString
' Calculate weight percent average
Call MathArrayAverage(average2, analysis.WtsData!(), sample(1).Datarows%, sample(1).LastChan% + 1, sample())
If ierror Then Exit Sub
' Calculate atomic percent average
Call MathArrayAverage(average3, analysis.CalData!(), sample(1).Datarows%, sample(1).LastChan% + 1, sample())
If ierror Then Exit Sub
' Elemental data
For chan% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & MiscAutoFormat$(average2.averags!(chan%))
bstring$ = bstring$ & vbTab & MiscAutoFormat$(average2.averags!(chan%))
' Save average for each channel
MatrixAverages!(chan%, izaf%) = average2.averags!(chan%)
Next chan%
' Add total average
astring$ = astring$ & vbTab & MiscAutoFormat$(average2.averags!(sample(1).LastChan% + 1))
bstring$ = bstring$ & vbTab & MiscAutoFormat$(average2.averags!(sample(1).LastChan% + 1))
' Save total average
MatrixAverages!(sample(1).LastChan% + 1, izaf%) = average2.averags!(sample(1).LastChan% + 1)
' Other data
If sample(1).AtomicPercentFlag% Then
For chan% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & MiscAutoFormat$(average3.averags!(chan%))
bstring$ = bstring$ & vbTab & MiscAutoFormat$(average3.averags!(chan%))
' Save average
MatrixAverages2!(chan%, izaf%) = average3.averags!(chan%)
Next chan%
' Add total
astring$ = astring$ & vbTab & MiscAutoFormat$(average3.averags!(sample(1).LastChan% + 1))
bstring$ = bstring$ & vbTab & MiscAutoFormat$(average3.averags!(sample(1).LastChan% + 1))
' Save total
MatrixAverages2!(sample(1).LastChan% + 1, izaf%) = average3.averags!(sample(1).LastChan% + 1)
End If
Exit Sub
' Errors
AnalyzeCalculateAllAverageError:
Close (Temp1FileNumber%)
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeCalculateAllAverage"
ierror = True
Exit Sub
End Sub
Sub AnalyzeCalculateAllColumnString(astring As String, sample() As TypeSample)
' Create a column label string
ierror = False
On Error GoTo AnalyzeCalculateAllColumnStringError
Dim i As Integer
' Line number
astring$ = VbDquote$ & Format$("LINE", a80$) & VbDquote$
' Elemental labels
For i% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & VbDquote$ & Format$(sample(1).Elsyup$(i%) & " WT%", a80$) & VbDquote$
Next i%
' Add total label
astring$ = astring$ & vbTab & VbDquote$ & Format$("TOTAL", a80$) & VbDquote$
' Atomic labels
If sample(1).AtomicPercentFlag% Then
For i% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & VbDquote$ & Format$(sample(1).Elsyup$(i%) & " AT%", a80$) & VbDquote$
Next i%
' Add total label
astring$ = astring$ & vbTab & VbDquote$ & Format$("TOTAL", a80$) & VbDquote$
End If
Exit Sub
' Errors
AnalyzeCalculateAllColumnStringError:
Close (Temp1FileNumber%)
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeCalculateAllColumnString"
ierror = True
Exit Sub
End Sub
Sub AnalyzeCalculateAllDataString(astring As String, sampleline As Integer, analysis As TypeAnalysis, sample() As TypeSample)
' Create a data line string for each line
ierror = False
On Error GoTo AnalyzeCalculateAllDataStringError
Dim i As Integer
' Line number
astring$ = Format$(sample(1).Linenumber&(sampleline%), a80$)
' Elemental data
For i% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & MiscAutoFormat$(analysis.WtsData!(sampleline%, i%))
Next i%
' Add total
astring$ = astring$ & vbTab & MiscAutoFormat$(analysis.WtsData!(sampleline%, sample(1).LastChan% + 1))
' Atomic data
If sample(1).AtomicPercentFlag% Then
For i% = 1 To sample(1).LastChan%
astring$ = astring$ & vbTab & MiscAutoFormat$(analysis.CalData!(sampleline%, i%))
Next i%
' Add total
astring$ = astring$ & vbTab & MiscAutoFormat$(analysis.CalData!(sampleline%, sample(1).LastChan% + 1))
End If
Exit Sub
' Errors
AnalyzeCalculateAllDataStringError:
Close (Temp1FileNumber%)
MsgBox Error$, vbOKOnly + vbCritical, "AnalyzeCalculateAllDataString"
ierror = True
Exit Sub
End Sub