' BandWidthPlot.bas for Scantraq 32-bit
' Copyright 1995-2001 FTG Software Associates
'
' Plots graph showing max, center and half width
' To use this program, first scan a spectrum or load one from disk.
' To run this program as a macro, insert the following under Evaluate...Macro...Edit
'
'   Description   BandPass Filter
'   Command       basrun c:\scantraq\basic\bandwidthplot.bas;
'
Option Explicit
DefInt I-N
DefSng A-H, O-Z

Sub Main
    ' Scantraq BASIC keywords
    Dim k, kErr As Boolean, b50, tm, wc, wMin, wMax, gs$
    ' Calculate within range wMin, wMax as shown in Scantraq screen
    ' This allows the spectral data set to be larger than the displayed axes
    ' when there are several peaks to be considered
    AxesRange wMin, wMax
    BandMax wMin, wMax, tm, kErr
    tm = 100*tm
    If Not kErr Then BandCtr wMin, wMax, wc, kErr   ' Band center
    If Not kErr Then BandWidth 50, wMin, wMax, b50, kErr  ' 50% bandwidth
    If Not kErr Then
        PlotClose
        ' Pasted annotation provides code basis
        '    vline 1640,5,|b  1640 nm,9;0
        '    hline 96.7,5,|l96.70%,9;0
        '    graph 1633.7,48.3,43,,9;0
        '    graph 1646.3,48.3,45,,9;1
        '    graph 1647.3,47.6,0,12.6 nm,9;2
        gs$ = "vline " & tStr(wc) & ",5,|b  " & eFmt$(wc) & " nm,9;0" & vbCrLf
        gs$ = gs$ & "hline " & tStr(tm) & ",5,|l" & eFmt$(tm) & "%,9;0" & vbCrLf
        tm = tm/2
        gs$ = gs$ & "graph " & tStr(wc-b50/2) & "," & tStr(tm) & ",43,,9;0" & vbCrLf
        gs$ = gs$ & "graph " & tStr(wc+b50/2) & "," & tStr(tm) & ",45,,9;1" & vbCrLf
        ' offset label +1 nm, -.7 %
        gs$ = gs$ & "graph " & tStr(wc+b50/2 +1) & "," & tStr(tm-.7) & ",0," & eFmt$(b50) & " nm,9;2"
        PlotAnnotation = gs$
        PlotAnnotate True   ' Must be before PlotActivate
        PlotActivate
        PlotNext
    Else
        Display "----Error----", "Bandwidth Calculation", 6000, 4000, "Arial", 32, 1
    End If
End Sub

Sub AxesRange(wMin, wMax)  ' Returns current axes range
    Dim k, s$
    AxesCopy
    s$ = Clipboard$()
    k = InStr(s$, vbTab)
    wMin = Val(Mid$(s$, 1, k -1))
    s$ = Mid$(s$, k + 1)
    k = InStr(s$, vbTab)
    wMax = Val(Mid$(s$, 1, k -1))
End Sub

Function tStr(ByVal v!) As String
    tStr = Trim$(Str(Round(v,3)))
End Function

Function eFmt$(v)
    ' Formats for European decimal (replace "," by "`")
    ' "`" is then replaced by "," in the FSPlot Module
    eFmt$ = Replace(Format$(v, "0.00"),",","`")
End Function