-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.bas
3315 lines (2773 loc) · 138 KB
/
convert.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "CodeCONVERT"
' (c) Copyright 1995-2024 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.
Function ConvertAtomToWeight(lchan As Integer, row As Integer, atoms() As Single, syms() As String) As Single
' Calculate weight percent from total atomic percent or formula atoms for a single element
ierror = False
On Error GoTo ConvertAtomToWeightError
Dim i As Integer, ip As Integer
Dim sum As Single, temp As Single
' Sum the atomic percents or formulas
sum = 0#
For i% = 1 To lchan%
ip% = IPOS1(MAXELM%, syms$(i%), Symlo$())
If ip% > 0 Then
sum! = sum! + atoms!(i%) * AllAtomicWts!(ip%)
End If
Next i%
' Check for bad sum
If sum! <= 0# Then
msg$ = "Bad atomic or formula sum = " & Format$(sum!)
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertAtomToWeight"
ierror = True
Exit Function
End If
' Do atomic percent or formula atoms to weight percent conversion
ConvertAtomToWeight! = 0#
ip% = IPOS1(MAXELM%, syms$(row%), Symlo$())
If ip% > 0 Then
temp! = atoms!(row%) * AllAtomicWts!(ip%)
ConvertAtomToWeight! = 100# * temp! / sum!
End If
Exit Function
' Errors
ConvertAtomToWeightError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertAtomToWeight"
ierror = True
Exit Function
End Function
Function ConvertElmToOxd(weight As Single, sym As String, cat As Integer, oxd As Integer) As Single
' Converts an elemental weight percent to an oxide weight percent
ierror = False
On Error GoTo ConvertElmToOxdError
Dim temp As Double
Dim ip As Integer
temp# = 0#
ip% = IPOS1(MAXELM%, sym$, Symlo$())
If ip% > 0 Then
temp# = weight! * (AllAtomicWts!(ip%) * cat% + AllAtomicWts!(ATOMIC_NUM_OXYGEN%) * oxd%)
ConvertElmToOxd! = temp# / (AllAtomicWts!(ip%) * cat%)
End If
Exit Function
' Errors
ConvertElmToOxdError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertElmToOxd"
ierror = True
Exit Function
End Function
Sub ConvertMinerals(analysis As TypeAnalysis, sample() As TypeSample)
' Converts atoms to mineral end members
ierror = False
On Error GoTo ConvertMineralsError
Dim j As Integer
' Check for sufficient basis of mineral end members basis elements
If ConvertMineralsLineCheck(analysis, sample()) Then
' Calculate all rows
For j% = 1 To sample(1).Datarows%
If sample(1).LineStatus(j%) Then
' Perform actual mineral end member calculation
Call ConvertMineralsLine(j%, analysis, sample())
If ierror Then Exit Sub
End If
Next j%
Else
msg$ = "Insufficient number of the basis atoms for the specified mineral end-member calculation for sample " & SampleGetString2$(sample())
Call IOWriteLogRichText(msg$, vbNullString, Int(LogWindowFontSize%), vbMagenta, Int(FONT_REGULAR%), Int(0))
End If
Exit Sub
' Errors
ConvertMineralsError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertMinerals"
ierror = True
Exit Sub
End Sub
Sub ConvertMineralsLine(sampleline As Integer, analysis As TypeAnalysis, sample() As TypeSample)
' Perform the actual mineral end-member calculation
ierror = False
On Error GoTo ConvertMineralsLineError
Dim ia As Integer, ib As Integer, ic As Integer, id As Integer
Dim temp1 As Single, temp2 As Single, temp3 As Single, temp4 As Single
Dim sum As Single
sum! = 0#
ia% = 0
ib% = 0
ic% = 0
id% = 0
temp1! = 0
temp2! = 0
temp3! = 0
temp4! = 0
' Olivine (skip disabled quant elements)
If sample(1).MineralFlag% = 1 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
If ia% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ib%)
If sum! <= 0# Then GoTo ConvertMineralsLineZeroSum
If ia% > 0 Then temp1! = analysis.CalData!(sampleline%, ia%) / sum!
If ib% > 0 Then temp2! = analysis.CalData!(sampleline%, ib%) / sum!
If ia% > 0 Then analysis.CalData!(sampleline%, 1) = temp1! * 100#
If ib% > 0 Then analysis.CalData!(sampleline%, 2) = temp2! * 100#
analysis.CalData!(sampleline%, 3) = 0#
analysis.CalData!(sampleline%, 4) = 0#
End If
' Feldspar (skip disabled quant elements)
If sample(1).MineralFlag% = 2 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_SODIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Na
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_POTASSIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' K
If ia% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ic%)
If sum! <= 0# Then GoTo ConvertMineralsLineZeroSum
If ia% > 0 Then temp1! = analysis.CalData!(sampleline%, ia%) / sum!
If ib% > 0 Then temp2! = analysis.CalData!(sampleline%, ib%) / sum!
If ic% > 0 Then temp3! = analysis.CalData!(sampleline%, ic%) / sum!
If ia% > 0 Then analysis.CalData!(sampleline%, 1) = temp1! * 100#
If ib% > 0 Then analysis.CalData!(sampleline%, 2) = temp2! * 100#
If ic% > 0 Then analysis.CalData!(sampleline%, 3) = temp3! * 100#
analysis.CalData!(sampleline%, 4) = 0#
End If
' Pyroxene (skip disabled quant elements)
If sample(1).MineralFlag% = 3 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
If ia% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ic%)
If sum! <= 0# Then GoTo ConvertMineralsLineZeroSum
If ia% > 0 Then temp1! = analysis.CalData!(sampleline%, ia%) / sum!
If ib% > 0 Then temp2! = analysis.CalData!(sampleline%, ib%) / sum!
If ic% > 0 Then temp3! = analysis.CalData!(sampleline%, ic%) / sum!
If ia% > 0 Then analysis.CalData!(sampleline%, 1) = temp1! * 100#
If ib% > 0 Then analysis.CalData!(sampleline%, 2) = temp2! * 100#
If ic% > 0 Then analysis.CalData!(sampleline%, 3) = temp3! * 100#
analysis.CalData!(sampleline%, 4) = 0#
End If
' Garnet (Normal) (skip disabled quant elements)
If sample(1).MineralFlag% = 4 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
id% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MANGANESE%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mn
If ia% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ic%)
If id% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, id%)
If sum! <= 0# Then GoTo ConvertMineralsLineZeroSum
If ia% > 0 Then temp1! = analysis.CalData!(sampleline%, ia%) / sum!
If ib% > 0 Then temp2! = analysis.CalData!(sampleline%, ib%) / sum!
If ic% > 0 Then temp3! = analysis.CalData!(sampleline%, ic%) / sum!
If id% > 0 Then temp4! = analysis.CalData!(sampleline%, id%) / sum!
If ia% > 0 Then analysis.CalData!(sampleline%, 1) = temp1! * 100#
If ib% > 0 Then analysis.CalData!(sampleline%, 2) = temp2! * 100#
If ic% > 0 Then analysis.CalData!(sampleline%, 3) = temp3! * 100#
If id% > 0 Then analysis.CalData!(sampleline%, 4) = temp4! * 100#
End If
' Garnet (Grossular, Andradite, Uvarovite) (skip disabled quant elements)
If sample(1).MineralFlag% = 5 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_ALUMINUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Al
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CHROMIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Cr
If ia% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(sampleline%, ic%)
If sum! <= 0# Then GoTo ConvertMineralsLineZeroSum
If ia% > 0 Then temp1! = analysis.CalData!(sampleline%, ia%) / sum!
If ib% > 0 Then temp2! = analysis.CalData!(sampleline%, ib%) / sum!
If ic% > 0 Then temp3! = analysis.CalData!(sampleline%, ic%) / sum!
If ia% > 0 Then analysis.CalData!(sampleline%, 1) = temp1! * 100#
If ib% > 0 Then analysis.CalData!(sampleline%, 2) = temp2! * 100#
If ic% > 0 Then analysis.CalData!(sampleline%, 3) = temp3! * 100#
End If
Exit Sub
' Errors
ConvertMineralsLineError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertMineralsLine"
ierror = True
Exit Sub
ConvertMineralsLineZeroSum:
msg$ = "Zero sum on line " & Format$(sample(1).Linenumber&(sampleline%)) & " for sample " & SampleGetString2$(sample()) & ". Most likely an insufficient number of the basis atoms for the specified mineral end-member calculation."
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertMineralsLine"
ierror = True
Exit Sub
End Sub
Function ConvertMineralsLineCheck(analysis As TypeAnalysis, sample() As TypeSample) As Boolean
' Perform only a check for sufficient concentrations for mineral end-member calculations
ierror = False
On Error GoTo ConvertMineralsLineCheckError
Dim j As Integer
Dim ia As Integer, ib As Integer, ic As Integer, id As Integer
Dim sum As Single
' Assume OK
ConvertMineralsLineCheck = True
' Check each data line
For j% = 1 To sample(1).Datarows%
If sample(1).LineStatus(j%) Then
sum! = 0#
ia% = 0
ib% = 0
ic% = 0
id% = 0
' Olivine (skip disabled quant elements)
If sample(1).MineralFlag% = 1 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
If ia% > 0 Then sum! = sum! + analysis.CalData!(j%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(j%, ib%)
If sum! <= 0# Then ConvertMineralsLineCheck = False
End If
' Feldspar (skip disabled quant elements)
If sample(1).MineralFlag% = 2 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_SODIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Na
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_POTASSIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' K
If ia% > 0 Then sum! = sum! + analysis.CalData!(j%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(j%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(j%, ic%)
If sum! <= 0# Then ConvertMineralsLineCheck = False
End If
' Pyroxene (skip disabled quant elements)
If sample(1).MineralFlag% = 3 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
If ia% > 0 Then sum! = sum! + analysis.CalData!(j%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(j%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(j%, ic%)
If sum! <= 0# Then ConvertMineralsLineCheck = False
End If
' Garnet (Normal) (skip disabled quant elements)
If sample(1).MineralFlag% = 4 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CALCIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Ca
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MAGNESIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mg
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
id% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_MANGANESE%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Mn
If ia% > 0 Then sum! = sum! + analysis.CalData!(j%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(j%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(j%, ic%)
If id% > 0 Then sum! = sum! + analysis.CalData!(j%, id%)
If sum! <= 0# Then ConvertMineralsLineCheck = False
End If
' Garnet (Grossular, Andradite, Uvarovite) (skip disabled quant elements)
If sample(1).MineralFlag% = 5 Then
ia% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_ALUMINUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Al
ib% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_IRON%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Fe
ic% = IPOS1DQ(sample(1).LastChan%, Symlo$(ATOMIC_NUM_CHROMIUM%), sample(1).Elsyms$(), sample(1).DisableQuantFlag%()) ' Cr
If ia% > 0 Then sum! = sum! + analysis.CalData!(j%, ia%)
If ib% > 0 Then sum! = sum! + analysis.CalData!(j%, ib%)
If ic% > 0 Then sum! = sum! + analysis.CalData!(j%, ic%)
If sum! <= 0# Then ConvertMineralsLineCheck = False
End If
End If
Next j%
Exit Function
' Errors
ConvertMineralsLineCheckError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertMineralsLineCheck"
ierror = True
Exit Function
End Function
Function ConvertOxdToElm(weight As Single, sym As String, cat As Integer, oxd As Integer) As Single
' Converts an oxide weight percent to an elemental weight percent
ierror = False
On Error GoTo ConvertOxdToElmError
Dim temp As Single
Dim ip As Integer
temp! = 0#
ip% = IPOS1(MAXELM%, sym$, Symlo$())
If ip% > 0 Then
temp! = weight! * (AllAtomicWts!(ip%) * cat%)
ConvertOxdToElm! = temp! / (AllAtomicWts!(ip%) * cat% + AllAtomicWts!(ATOMIC_NUM_OXYGEN%) * oxd%)
End If
Exit Function
' Errors
ConvertOxdToElmError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertOxdToElm"
ierror = True
Exit Function
End Function
Function ConvertTotalToExcessOxygen(mode As Integer, unksample() As TypeSample, stdsample() As TypeSample) As Single
' Function to convert the total oxygen in a standard composition (calculated and excess)
' to actual excess oxygen based on the current unknown sample cation ratios.
' mode = 1 elemental mode
' mode = 2 oxide mode
ierror = False
On Error GoTo ConvertTotalToExcessOxygenError
Dim i As Integer, ip As Integer
Dim cat As Integer, oxd As Integer
Dim total As Single, oxygen As Single
Dim temp As Single
ConvertTotalToExcessOxygen! = 0#
' Find the position of unknown specified oxygen in the standard composition array
ip% = IPOS1(stdsample(1).LastChan%, Symlo$(ATOMIC_NUM_OXYGEN%), stdsample(1).Elsyms$())
If ip% = 0 Then Exit Function
' Load total oxygen from standard composition database
total! = stdsample(1).ElmPercents!(ip%)
' If unknown is calculated as elemental, just return
ConvertTotalToExcessOxygen! = total!
If unksample(1).OxideOrElemental% = 2 And mode% = 1 Then Exit Function
' Now calculate the oxygen based on the unksample cation ratios
oxygen! = 0#
For i% = 1 To stdsample(1).LastChan%
cat% = stdsample(1).numcat%(i%)
oxd% = stdsample(1).numoxd%(i%)
' See if sample uses different cation ratios than standard, if so, use them
ip% = IPOS1(unksample(1).LastChan%, stdsample(1).Elsyms$(i%), unksample(1).Elsyms$())
If ip% > 0 Then
cat% = unksample(1).numcat%(ip%)
oxd% = unksample(1).numoxd%(ip%)
End If
' Calculated difference between oxide and elemental weight percent (calculated oxygen)
temp! = ConvertElmToOxd(stdsample(1).ElmPercents!(i%), stdsample(1).Elsyms$(i%), cat%, oxd%)
oxygen! = oxygen! + (temp! - stdsample(1).ElmPercents!(i%))
Next i%
ConvertTotalToExcessOxygen! = total! - oxygen!
Exit Function
' Errors
ConvertTotalToExcessOxygenError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertTotalToExcessOxygen"
ierror = True
Exit Function
End Function
Function ConvertWeightToAtom(lchan As Integer, chan As Integer, wts() As Single, syms() As String) As Single
' Calculate weight percent to atomic percent for a single element
ierror = False
On Error GoTo ConvertWeightToAtomError
Dim i As Integer, ip As Integer
Dim sum As Single, temp As Single
' Sum the atoms of the elemental weight percents
sum = 0#
For i% = 1 To lchan%
ip% = IPOS1(MAXELM%, syms$(i%), Symlo$())
If ip% > 0 Then
sum! = sum! + wts!(i%) / AllAtomicWts!(ip%)
End If
Next i%
' Check for bad sum
If sum! <= 0# Then
msg$ = "Bad atomic sum = " & Format$(sum!)
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertWeightToAtom"
ierror = True
Exit Function
End If
' Do weight percent to atomic percent conversion
ConvertWeightToAtom! = 0#
ip% = IPOS1(MAXELM%, syms$(chan%), Symlo$())
If ip% > 0 Then
temp! = wts!(chan%) / AllAtomicWts!(ip%)
ConvertWeightToAtom! = 100# * temp! / sum!
End If
Exit Function
' Errors
ConvertWeightToAtomError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToAtom"
ierror = True
Exit Function
End Function
Function ConvertWeightToAtom2(lchan As Integer, chan As Integer, wts() As Single, atwts() As Single, syms() As String) As Single
' Calculate weight percent to atomic percent for a single element (uses passed atomic weights)
ierror = False
On Error GoTo ConvertWeightToAtom2Error
Dim i As Integer, ip As Integer
Dim sum As Single, temp As Single
' Sum the atoms of the elemental weight percents
sum = 0#
For i% = 1 To lchan%
sum! = sum! + wts!(i%) / atwts!(i%)
Next i%
' Check for bad sum
If sum! <= 0# Then
msg$ = "Bad atomic sum = " & Format$(sum!)
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertWeightToAtom2"
ierror = True
Exit Function
End If
' Do weight percent to atomic percent conversion
ConvertWeightToAtom2! = 0#
temp! = wts!(chan%) / atwts!(chan%)
ConvertWeightToAtom2! = 100# * temp! / sum!
Exit Function
' Errors
ConvertWeightToAtom2Error:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToAtom2"
ierror = True
Exit Function
End Function
Sub ConvertWeightToZFraction(lchan As Integer, atnums() As Single, atwts() As Single, conc() As Single, zfracs() As Single)
' Convert from weight fraction to Z fraction (assume Z fraction exponent is 1.0) (obsolete function)
ierror = False
On Error GoTo ConvertWeightToZFractionError
Dim i As Integer
Dim sum As Single
' Calculate sum
sum! = 0#
For i% = 1 To lchan%
sum! = sum! + atnums!(i%) * conc!(i%) / atwts!(i%)
Next i%
If sum! = 0# Then GoTo ConvertWeightToZFractionZeroSum
For i% = 1 To lchan%
If sum! <> 0# Then
zfracs!(i%) = atnums!(i%) * (conc!(i%) / atwts!(i%)) / sum!
Else
zfracs!(i%) = 0#
End If
Next i%
Exit Sub
' Errors
ConvertWeightToZFractionError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToZFraction"
ierror = True
Exit Sub
ConvertWeightToZFractionZeroSum:
msg$ = "Sum of concentrations is zero"
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertWeightToZFraction"
ierror = True
Exit Sub
End Sub
Sub ConvertWeightToZFractionBSE(lchan As Integer, exponent As Single, atnums() As Integer, atwts() As Single, conc() As Single, keV() As Single, zfracs() As Single)
' Convert from weight fraction to Z fraction (based on passed exponent) for electron backscatter corrections
ierror = False
On Error GoTo ConvertWeightToZFractionBSEError
Dim i As Integer
Dim sum As Single
Dim texponent(1 To MAXCHAN%) As Single
' Load passed exponent or calculate based on electron beam energy if exponent is zero
For i% = 1 To lchan%
If exponent! = 0# Then
If keV!(i%) = 0# Then GoTo ConvertWeightToZFractionBSEZerokeV
texponent!(i%) = ConvertCalculateZFractionExponentBSE(keV!(i%))
If ierror Then Exit Sub
Else
texponent!(i%) = exponent!
End If
Next i%
' Calculate sum
sum! = 0#
For i% = 1 To lchan%
sum! = sum! + (atnums%(i%) ^ texponent!(i%)) * conc!(i%) / atwts!(i%)
Next i%
If sum! = 0# Then GoTo ConvertWeightToZFractionBSEZeroSum
' Calculate Z fractions
For i% = 1 To lchan%
If sum! <> 0# Then
zfracs!(i%) = (atnums%(i%) ^ texponent!(i%)) * (conc!(i%) / atwts!(i%)) / sum!
Else
zfracs!(i%) = 0#
End If
Next i%
Exit Sub
' Errors
ConvertWeightToZFractionBSEError:
Screen.MousePointer = vbDefault
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToZFractionBSE"
ierror = True
Exit Sub
ConvertWeightToZFractionBSEZerokeV:
Screen.MousePointer = vbDefault
msg$ = "Variable zbar calculation was passed a zero keV value. This error should not occur, please contact Probe Sofwtare technical support."
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertWeightToZFractionBSE"
ierror = True
Exit Sub
ConvertWeightToZFractionBSEZeroSum:
Screen.MousePointer = vbDefault
msg$ = "Sum of concentrations is zero"
MsgBox msg$, vbOKOnly + vbExclamation, "ConvertWeightToZFractionBSE"
ierror = True
Exit Sub
End Sub
Sub ConvertWeightToAtomic(lchan As Integer, atwts() As Single, wts() As Single, atoms() As Single)
' Convert from weight percent/fraction to atomic fraction for the entire array
ierror = False
On Error GoTo ConvertWeightToAtomicError
Dim i As Integer
Dim sum As Single
' Calculate sum
sum! = 0#
For i% = 1 To lchan%
sum! = sum! + wts!(i%) / atwts!(i%)
Next i%
For i% = 1 To lchan%
If sum! <> 0# Then
atoms!(i%) = (wts!(i%) / atwts!(i%)) / sum!
Else
atoms!(i%) = 0#
End If
Next i%
Exit Sub
' Errors
ConvertWeightToAtomicError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToAtomic"
ierror = True
Exit Sub
End Sub
Sub ConvertWeightToOxide(lchan As Integer, atwts() As Single, NumCats() As Integer, NumOxds() As Integer, wts() As Single, excess As Single, oxwts() As Single)
' Convert from weight percent to oxide percent for the entire array
ierror = False
On Error GoTo ConvertWeightToOxideError
Dim i As Integer
Dim temp As Single
For i% = 1 To lchan%
If atwts!(i%) <> AllAtomicWts!(ATOMIC_NUM_OXYGEN%) Then
temp! = wts!(i%) * (atwts!(i%) * NumCats%(i%) + AllAtomicWts!(ATOMIC_NUM_OXYGEN%) * NumOxds%(i%))
oxwts!(i%) = temp! / (atwts!(i%) * NumCats%(i%))
Else
oxwts!(i%) = excess!
End If
Next i%
Exit Sub
' Errors
ConvertWeightToOxideError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertWeightToOxide"
ierror = True
Exit Sub
End Sub
Sub ConvertAtomicToWeight(lchan As Integer, atwts() As Single, wts() As Single, atoms() As Single)
' Convert from atomic fraction to weight fraction for the entire array
ierror = False
On Error GoTo ConvertAtomicToWeightError
Dim i As Integer
Dim sum As Single
' Calculate sum
sum! = 0#
For i% = 1 To lchan%
sum! = sum! + atoms!(i%) * atwts!(i%)
Next i%
For i% = 1 To lchan%
If sum! <> 0# Then
wts!(i%) = (atoms!(i%) * atwts!(i%)) / sum!
Else
wts!(i%) = 0#
End If
Next i%
Exit Sub
' Errors
ConvertAtomicToWeightError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertAtomicToWeight"
ierror = True
Exit Sub
End Sub
Sub ConvertElectronToWeight(lchan As Integer, atnums() As Single, atwts() As Single, elecs() As Single, wts() As Single)
' Convert from electron fraction to weight fraction
ierror = False
On Error GoTo ConvertElectronToWeightError
Dim i As Integer
Dim sum As Single
' Calculate sum
sum! = 0#
For i% = 1 To lchan%
sum! = sum! + atwts!(i%) * elecs!(i%) / atnums!(i%)
Next i%
For i% = 1 To lchan%
If sum! <> 0# Then
wts!(i%) = atwts!(i%) * (elecs!(i%) / atnums!(i%)) / sum!
Else
wts!(i%) = 0#
End If
Next i%
Exit Sub
' Errors
ConvertElectronToWeightError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertElectronToWeight"
ierror = True
Exit Sub
End Sub
Function ConvertHalogensToOxygen(lchan As Integer, syms() As String, dqs() As Integer, conc() As Single) As Single
' Calculate the equivalent oxygen based on the halogen concentrations or weight percents
' lchan = number of channels
' syms() = symbol (element) array
' dqs() = disable quant flag array
' conc() = concentration of element array
ierror = False
On Error GoTo ConvertHalogensToOxygenError
Dim FtoO As Single, CltoO As Single
Dim BrtoO As Single, ItoO As Single
Dim ip As Integer
Dim temp As Single
FtoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / (AllAtomicWts!(ATOMIC_NUM_FLUORINE%) * 2#)
CltoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / (AllAtomicWts!(ATOMIC_NUM_CHLORINE%) * 2#)
BrtoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / (AllAtomicWts!(ATOMIC_NUM_BROMINE%) * 2#)
ItoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / (AllAtomicWts!(ATOMIC_NUM_IODINE%) * 2#)
' Check for fluorine
temp! = 0#
ip% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_FLUORINE%), syms$(), dqs%())
If ip% > 0 Then
temp! = conc!(ip%) * FtoO!
End If
' Check for chlorine
ip% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_CHLORINE%), syms$(), dqs%())
If ip% > 0 Then
temp! = temp! + conc!(ip%) * CltoO!
End If
' Check for bromine
ip% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_BROMINE%), syms$(), dqs%())
If ip% > 0 Then
temp! = temp! + conc!(ip%) * BrtoO!
End If
' Check for iodine
ip% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_IODINE%), syms$(), dqs%())
If ip% > 0 Then
temp! = temp! + conc!(ip%) * ItoO!
End If
ConvertHalogensToOxygen! = temp!
Exit Function
' Errors
ConvertHalogensToOxygenError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertHalogensToOxygen"
ierror = True
Exit Function
End Function
Function ConvertSulfurToOxygen(lchan As Integer, syms() As String, dqs() As Integer, conc() As Single, charges() As Single) As Single
' Calculate the equivalent oxygen based on the sulfur concentration and charge valence
' lchan = number of channels
' syms() = symbol (element) array
' dqs() = disable quant flag array
' conc() = concentration of element array
' charges() = charge valences of element array
ierror = False
On Error GoTo ConvertSulfurToOxygenError
Dim StoO As Single
Dim ips As Integer, ipo As Integer
Dim temp As Single
ConvertSulfurToOxygen! = 0#
' Find index for sulfur and oxygen
ips% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_SULFUR%), syms$(), dqs%())
ipo% = IPOS1DQ(lchan%, Symlo$(ATOMIC_NUM_OXYGEN%), syms$(), dqs%())
If ips% = 0 Then Exit Function
If charges!(ips%) = 0# Then Exit Function
' Calculate stoiometric correction factor
If ipo% <> 0 Then
StoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / AllAtomicWts!(ATOMIC_NUM_SULFUR%) * charges!(ipo%) / charges!(ips%)
Else
StoO! = AllAtomicWts!(ATOMIC_NUM_OXYGEN%) / AllAtomicWts!(ATOMIC_NUM_SULFUR%) * -2# / charges!(ips%)
End If
' Check for sulfur charge valence less than zero (negative charge valence replaces oxygen equivalent)
If charges!(ips%) < 0# Then
temp! = 0#
If ips% > 0 Then
temp! = conc!(ips%) * StoO!
End If
' Sulfur valence is positive
Else
temp! = 0#
End If
ConvertSulfurToOxygen! = temp!
Exit Function
' Errors
ConvertSulfurToOxygenError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertSulfurToOxygen"
ierror = True
Exit Function
End Function
Function ConvertOxygenFromCations(sample() As TypeSample) As Single
' Calculate oxygen from cations for sample
ierror = False
On Error GoTo ConvertOxygenFromCationsError
Dim i As Integer
Dim temp As Single
' Calculate oxygen from cations
temp! = 0#
For i% = 1 To sample(1).LastChan%
If i% <> sample(1).OxygenChannel% Then
temp! = temp! + (ConvertElmToOxd(sample(1).ElmPercents!(i%), sample(1).Elsyms$(i%), sample(1).numcat%(i%), sample(1).numoxd%(i%)) - sample(1).ElmPercents!(i%))
End If
Next i%
ConvertOxygenFromCations! = temp!
Exit Function
' Errors
ConvertOxygenFromCationsError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertOxygenFromCations"
ierror = True
Exit Function
End Function
Function ConvertOxygenFromCations2(sampleline As Integer, analysis As TypeAnalysis, sample() As TypeSample) As Single
' Calculate oxygen from cations for sample (uses "analysis" array)
ierror = False
On Error GoTo ConvertOxygenFromCations2Error
Dim i As Integer
Dim temp As Single
' Calculate oxygen from cations
temp! = 0#
For i% = 1 To sample(1).LastChan%
If i% <> sample(1).OxygenChannel% Then
temp! = temp! + (analysis.OxPercents!(i%) - analysis.WtsData!(sampleline%, i%))
End If
Next i%
ConvertOxygenFromCations2! = temp!
Exit Function
' Errors
ConvertOxygenFromCations2Error:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertOxygenFromCations2"
ierror = True
Exit Function
End Function
Function ConvertOxygenFromCations3(analysis As TypeAnalysis, sample() As TypeSample) As Single
' Calculate oxygen from cations for sample analyzed weight percents
ierror = False
On Error GoTo ConvertOxygenFromCations3Error
Dim i As Integer
Dim temp1 As Single, temp2 As Single
' Calculate oxygen from cations
temp1! = 0#
For i% = 1 To sample(1).LastChan%
If i% <> sample(1).OxygenChannel% Then
temp2! = ConvertElmToOxd(analysis.WtPercents!(i%), sample(1).Elsyms$(i%), sample(1).numcat%(i%), sample(1).numoxd%(i%))
temp1! = temp1! + (temp2! - analysis.WtPercents!(i%))
End If
Next i%
ConvertOxygenFromCations3! = temp1!
Exit Function
' Errors
ConvertOxygenFromCations3Error:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertOxygenFromCations3"
ierror = True
Exit Function
End Function
Function ConvertChargeBalance(lchan As Integer, atwts() As Single, wts() As Single, chrgs() As Single) As Single
' Calculate charge balance
ierror = False
On Error GoTo ConvertChargeBalanceError
Dim i As Integer
Dim temp As Single
Dim atoms(1 To MAXCHAN%) As Single ' calculate from atwts and wts
' Convert to atomic percent
Call ConvertWeightToAtomic(lchan%, atwts!(), wts!(), atoms!())
If ierror Then Exit Function
' Calculate for composition
temp! = 0#
For i% = 1 To lchan%
temp! = temp! + atoms!(i%) * chrgs!(i%)
Next i%
ConvertChargeBalance! = temp!
Exit Function
' Errors
ConvertChargeBalanceError:
MsgBox Error$, vbOKOnly + vbCritical, "ConvertChargeBalance"
ierror = True
Exit Function
End Function
Sub ConvertWeightToFormula(analysis As TypeAnalysis, sample() As TypeSample)
' Routine to convert elemental weight percents to formula coefficients (analysis.WtPercents to analysis.Formulas)
ierror = False
On Error GoTo ConvertWeightToFormulaError
Dim i As Integer, ip As Integer
Dim temp As Single
Dim totalatoms As Single, TotalCations As Single
Dim atoms(1 To MAXCHAN%) As Single
' Zero arrays
For i% = 1 To sample(1).LastChan%
analysis.Formulas!(i%) = 0#
Next i%
' Check for insufficient total atoms