#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)10:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)10:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)10:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)10:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)10:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)10:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)10:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)10:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)10:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)10:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)10:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)10:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)10:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)10:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)10:CAV:SCALE.DOL to
# $(CM,recursive)10:CAV:CAL_SCALE
record(calc, "$(CM,recursive)10:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)10:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)10:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)10:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)10:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)10:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)10:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)10:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)10:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)10:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)10:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)10:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)10:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)10:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)10:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)10:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)10:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)10:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)10:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)10:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)10:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)10:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)10:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)10:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)10:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)10:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)10:DRV:SCALE.DOL to
# $(CM,recursive)10:DRV:CAL_SCALE
record(calc, "$(CM,recursive)10:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)10:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)10:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)10:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)10:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)10:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)10:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)10:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)10:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)10:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)10:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)10:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)10:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)10:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)10:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)10:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)10:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)10:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)10:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)10:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)10:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)10:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)10:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)10:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)10:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)10:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)10:FWD:SCALE.DOL to
# $(CM,recursive)10:FWD:CAL_SCALE
record(calc, "$(CM,recursive)10:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)10:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)10:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)10:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)10:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)10:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)10:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)10:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)10:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)10:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)10:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)10:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)10:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)10:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)10:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)10:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)10:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)10:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)10:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)10:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)10:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)10:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)10:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)10:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)10:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)10:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)10:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)10:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)10:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)10:REV:SCALE.DOL to
# $(CM,recursive)10:REV:CAL_SCALE
record(calc, "$(CM,recursive)10:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)10:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)10:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)10:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)10:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)10:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)10:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)10:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)10:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)10:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)10:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)10:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)10:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)10:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)10:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)10:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)10:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)10:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)10:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)10:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)10:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)10:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)10:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)10:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)10:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)10:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)10:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)10:SPARE:SCALE.DOL to
# $(CM,recursive)10:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)10:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)10:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)10:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)10:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)10:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)10:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)10:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)10:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)10:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)10:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)10:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)10:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)10:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)10:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)10:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)10:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)10:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)10:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)10:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)10:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)10:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)10:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)10:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)10:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)10:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)10:QPROBE CP MS")
  field(INPC, "$(CM,recursive)10:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)10:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)10:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)10:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)10:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)10:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)10:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)10:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)10:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)10:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)10:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)10:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)10:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)10:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)10:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)10:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)10:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)10:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)10:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)10:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)10:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)10:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)10:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)10:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)10:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)10:CAV:SCALER")
	field(DOL2, "$(CM,recursive)10:CAV:SCALER")
	field(DOL3, "$(CM,recursive)10:CAV:SCALER")
	field(DOL4, "$(CM,recursive)10:CAV:SCALER")
	field(DOL5, "$(CM,recursive)10:CAV:SCALER")
	field(DOL6, "$(CM,recursive)10:CAV:SCALER")
	field(DOL7, "$(CM,recursive)10:CAV:SCALER")
	field(DOL8, "$(CM,recursive)10:CAV:SCALER")
	field(DOL9, "$(CM,recursive)10:CAV:SCALER")
	field(DOLA, "$(CM,recursive)10:CAV:SCALER")
	field(LNK1, "$(CM,recursive)10:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)10:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)10:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)10:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)10:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)10:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)10:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)10:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)10:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)10:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)10:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)10:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)10:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)10:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)10:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)10:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)10:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)10:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)10:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)10:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)10:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)10:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)10:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)10:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)10:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)10:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)10:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)10:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)10:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)10:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)10:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)10:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)10:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)10:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)10:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)10:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)10:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)10:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)10:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)10:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)10:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)10:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)10:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)10:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)10:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)10:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)10:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)10:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)10:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)10:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)10:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)10:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)10:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)10:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)10:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)10:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)10:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)10:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)10:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)10:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)10:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)10:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)10:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)10:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)10:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)10:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)10:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)10:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)10:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)10:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)10:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)10:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)10:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)10:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)10:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)10:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)10:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)10:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)10:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)10:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)10:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)10:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)10:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)10:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)10:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)10:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)10:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)10:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)10:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)10:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)10:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)10:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)10:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)10:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)10:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)10:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)20:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)20:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)20:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)20:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)20:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)20:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)20:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)20:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)20:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)20:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)20:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)20:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)20:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)20:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)20:CAV:SCALE.DOL to
# $(CM,recursive)20:CAV:CAL_SCALE
record(calc, "$(CM,recursive)20:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)20:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)20:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)20:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)20:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)20:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)20:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)20:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)20:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)20:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)20:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)20:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)20:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)20:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)20:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)20:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)20:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)20:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)20:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)20:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)20:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)20:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)20:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)20:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)20:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)20:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)20:DRV:SCALE.DOL to
# $(CM,recursive)20:DRV:CAL_SCALE
record(calc, "$(CM,recursive)20:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)20:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)20:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)20:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)20:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)20:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)20:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)20:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)20:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)20:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)20:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)20:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)20:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)20:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)20:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)20:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)20:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)20:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)20:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)20:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)20:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)20:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)20:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)20:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)20:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)20:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)20:FWD:SCALE.DOL to
# $(CM,recursive)20:FWD:CAL_SCALE
record(calc, "$(CM,recursive)20:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)20:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)20:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)20:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)20:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)20:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)20:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)20:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)20:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)20:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)20:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)20:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)20:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)20:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)20:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)20:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)20:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)20:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)20:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)20:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)20:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)20:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)20:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)20:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)20:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)20:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)20:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)20:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)20:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)20:REV:SCALE.DOL to
# $(CM,recursive)20:REV:CAL_SCALE
record(calc, "$(CM,recursive)20:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)20:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)20:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)20:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)20:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)20:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)20:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)20:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)20:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)20:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)20:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)20:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)20:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)20:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)20:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)20:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)20:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)20:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)20:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)20:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)20:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)20:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)20:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)20:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)20:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)20:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)20:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)20:SPARE:SCALE.DOL to
# $(CM,recursive)20:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)20:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)20:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)20:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)20:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)20:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)20:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)20:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)20:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)20:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)20:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)20:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)20:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)20:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)20:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)20:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)20:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)20:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)20:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)20:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)20:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)20:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)20:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)20:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)20:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)20:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)20:QPROBE CP MS")
  field(INPC, "$(CM,recursive)20:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)20:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)20:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)20:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)20:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)20:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)20:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)20:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)20:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)20:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)20:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)20:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)20:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)20:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)20:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)20:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)20:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)20:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)20:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)20:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)20:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)20:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)20:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)20:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)20:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)20:CAV:SCALER")
	field(DOL2, "$(CM,recursive)20:CAV:SCALER")
	field(DOL3, "$(CM,recursive)20:CAV:SCALER")
	field(DOL4, "$(CM,recursive)20:CAV:SCALER")
	field(DOL5, "$(CM,recursive)20:CAV:SCALER")
	field(DOL6, "$(CM,recursive)20:CAV:SCALER")
	field(DOL7, "$(CM,recursive)20:CAV:SCALER")
	field(DOL8, "$(CM,recursive)20:CAV:SCALER")
	field(DOL9, "$(CM,recursive)20:CAV:SCALER")
	field(DOLA, "$(CM,recursive)20:CAV:SCALER")
	field(LNK1, "$(CM,recursive)20:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)20:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)20:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)20:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)20:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)20:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)20:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)20:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)20:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)20:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)20:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)20:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)20:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)20:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)20:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)20:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)20:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)20:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)20:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)20:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)20:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)20:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)20:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)20:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)20:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)20:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)20:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)20:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)20:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)20:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)20:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)20:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)20:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)20:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)20:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)20:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)20:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)20:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)20:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)20:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)20:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)20:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)20:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)20:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)20:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)20:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)20:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)20:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)20:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)20:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)20:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)20:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)20:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)20:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)20:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)20:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)20:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)20:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)20:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)20:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)20:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)20:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)20:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)20:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)20:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)20:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)20:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)20:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)20:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)20:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)20:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)20:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)20:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)20:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)20:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)20:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)20:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)20:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)20:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)20:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)20:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)20:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)20:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)20:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)20:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)20:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)20:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)20:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)20:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)20:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)20:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)20:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)20:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)20:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)20:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)20:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)30:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)30:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)30:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)30:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)30:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)30:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)30:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)30:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)30:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)30:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)30:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)30:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)30:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)30:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)30:CAV:SCALE.DOL to
# $(CM,recursive)30:CAV:CAL_SCALE
record(calc, "$(CM,recursive)30:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)30:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)30:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)30:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)30:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)30:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)30:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)30:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)30:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)30:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)30:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)30:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)30:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)30:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)30:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)30:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)30:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)30:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)30:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)30:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)30:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)30:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)30:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)30:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)30:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)30:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)30:DRV:SCALE.DOL to
# $(CM,recursive)30:DRV:CAL_SCALE
record(calc, "$(CM,recursive)30:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)30:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)30:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)30:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)30:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)30:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)30:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)30:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)30:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)30:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)30:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)30:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)30:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)30:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)30:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)30:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)30:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)30:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)30:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)30:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)30:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)30:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)30:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)30:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)30:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)30:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)30:FWD:SCALE.DOL to
# $(CM,recursive)30:FWD:CAL_SCALE
record(calc, "$(CM,recursive)30:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)30:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)30:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)30:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)30:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)30:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)30:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)30:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)30:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)30:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)30:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)30:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)30:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)30:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)30:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)30:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)30:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)30:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)30:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)30:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)30:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)30:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)30:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)30:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)30:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)30:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)30:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)30:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)30:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)30:REV:SCALE.DOL to
# $(CM,recursive)30:REV:CAL_SCALE
record(calc, "$(CM,recursive)30:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)30:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)30:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)30:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)30:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)30:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)30:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)30:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)30:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)30:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)30:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)30:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)30:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)30:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)30:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)30:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)30:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)30:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)30:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)30:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)30:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)30:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)30:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)30:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)30:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)30:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)30:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)30:SPARE:SCALE.DOL to
# $(CM,recursive)30:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)30:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)30:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)30:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)30:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)30:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)30:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)30:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)30:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)30:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)30:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)30:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)30:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)30:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)30:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)30:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)30:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)30:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)30:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)30:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)30:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)30:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)30:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)30:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)30:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)30:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)30:QPROBE CP MS")
  field(INPC, "$(CM,recursive)30:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)30:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)30:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)30:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)30:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)30:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)30:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)30:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)30:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)30:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)30:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)30:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)30:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)30:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)30:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)30:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)30:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)30:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)30:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)30:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)30:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)30:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)30:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)30:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)30:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)30:CAV:SCALER")
	field(DOL2, "$(CM,recursive)30:CAV:SCALER")
	field(DOL3, "$(CM,recursive)30:CAV:SCALER")
	field(DOL4, "$(CM,recursive)30:CAV:SCALER")
	field(DOL5, "$(CM,recursive)30:CAV:SCALER")
	field(DOL6, "$(CM,recursive)30:CAV:SCALER")
	field(DOL7, "$(CM,recursive)30:CAV:SCALER")
	field(DOL8, "$(CM,recursive)30:CAV:SCALER")
	field(DOL9, "$(CM,recursive)30:CAV:SCALER")
	field(DOLA, "$(CM,recursive)30:CAV:SCALER")
	field(LNK1, "$(CM,recursive)30:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)30:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)30:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)30:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)30:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)30:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)30:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)30:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)30:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)30:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)30:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)30:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)30:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)30:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)30:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)30:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)30:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)30:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)30:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)30:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)30:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)30:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)30:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)30:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)30:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)30:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)30:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)30:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)30:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)30:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)30:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)30:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)30:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)30:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)30:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)30:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)30:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)30:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)30:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)30:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)30:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)30:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)30:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)30:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)30:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)30:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)30:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)30:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)30:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)30:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)30:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)30:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)30:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)30:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)30:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)30:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)30:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)30:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)30:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)30:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)30:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)30:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)30:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)30:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)30:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)30:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)30:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)30:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)30:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)30:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)30:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)30:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)30:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)30:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)30:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)30:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)30:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)30:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)30:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)30:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)30:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)30:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)30:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)30:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)30:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)30:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)30:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)30:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)30:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)30:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)30:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)30:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)30:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)30:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)30:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)30:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)40:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)40:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)40:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)40:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)40:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)40:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)40:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)40:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)40:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)40:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)40:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)40:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)40:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)40:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)40:CAV:SCALE.DOL to
# $(CM,recursive)40:CAV:CAL_SCALE
record(calc, "$(CM,recursive)40:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)40:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)40:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)40:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)40:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)40:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)40:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)40:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)40:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)40:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)40:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)40:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)40:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)40:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)40:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)40:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)40:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)40:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)40:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)40:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)40:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)40:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)40:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)40:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)40:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)40:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)40:DRV:SCALE.DOL to
# $(CM,recursive)40:DRV:CAL_SCALE
record(calc, "$(CM,recursive)40:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)40:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)40:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)40:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)40:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)40:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)40:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)40:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)40:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)40:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)40:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)40:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)40:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)40:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)40:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)40:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)40:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)40:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)40:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)40:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)40:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)40:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)40:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)40:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)40:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)40:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)40:FWD:SCALE.DOL to
# $(CM,recursive)40:FWD:CAL_SCALE
record(calc, "$(CM,recursive)40:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)40:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)40:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)40:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)40:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)40:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)40:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)40:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)40:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)40:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)40:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)40:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)40:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)40:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)40:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)40:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)40:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)40:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)40:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)40:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)40:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)40:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)40:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)40:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)40:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)40:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)40:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)40:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)40:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)40:REV:SCALE.DOL to
# $(CM,recursive)40:REV:CAL_SCALE
record(calc, "$(CM,recursive)40:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)40:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)40:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)40:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)40:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)40:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)40:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)40:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)40:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)40:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)40:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)40:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)40:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)40:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)40:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)40:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)40:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)40:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)40:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)40:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)40:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)40:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)40:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)40:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)40:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)40:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)40:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)40:SPARE:SCALE.DOL to
# $(CM,recursive)40:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)40:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)40:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)40:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)40:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)40:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)40:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)40:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)40:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)40:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)40:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)40:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)40:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)40:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)40:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)40:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)40:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)40:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)40:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)40:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)40:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)40:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)40:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)40:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)40:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)40:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)40:QPROBE CP MS")
  field(INPC, "$(CM,recursive)40:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)40:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)40:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)40:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)40:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)40:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)40:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)40:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)40:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)40:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)40:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)40:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)40:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)40:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)40:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)40:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)40:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)40:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)40:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)40:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)40:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)40:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)40:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)40:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)40:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)40:CAV:SCALER")
	field(DOL2, "$(CM,recursive)40:CAV:SCALER")
	field(DOL3, "$(CM,recursive)40:CAV:SCALER")
	field(DOL4, "$(CM,recursive)40:CAV:SCALER")
	field(DOL5, "$(CM,recursive)40:CAV:SCALER")
	field(DOL6, "$(CM,recursive)40:CAV:SCALER")
	field(DOL7, "$(CM,recursive)40:CAV:SCALER")
	field(DOL8, "$(CM,recursive)40:CAV:SCALER")
	field(DOL9, "$(CM,recursive)40:CAV:SCALER")
	field(DOLA, "$(CM,recursive)40:CAV:SCALER")
	field(LNK1, "$(CM,recursive)40:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)40:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)40:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)40:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)40:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)40:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)40:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)40:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)40:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)40:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)40:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)40:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)40:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)40:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)40:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)40:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)40:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)40:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)40:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)40:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)40:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)40:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)40:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)40:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)40:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)40:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)40:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)40:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)40:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)40:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)40:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)40:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)40:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)40:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)40:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)40:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)40:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)40:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)40:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)40:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)40:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)40:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)40:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)40:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)40:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)40:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)40:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)40:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)40:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)40:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)40:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)40:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)40:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)40:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)40:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)40:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)40:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)40:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)40:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)40:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)40:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)40:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)40:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)40:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)40:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)40:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)40:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)40:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)40:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)40:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)40:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)40:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)40:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)40:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)40:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)40:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)40:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)40:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)40:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)40:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)40:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)40:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)40:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)40:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)40:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)40:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)40:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)40:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)40:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)40:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)40:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)40:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)40:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)40:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)40:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)40:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)50:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)50:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)50:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)50:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)50:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)50:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)50:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)50:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)50:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)50:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)50:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)50:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)50:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)50:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)50:CAV:SCALE.DOL to
# $(CM,recursive)50:CAV:CAL_SCALE
record(calc, "$(CM,recursive)50:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)50:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)50:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)50:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)50:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)50:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)50:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)50:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)50:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)50:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)50:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)50:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)50:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)50:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)50:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)50:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)50:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)50:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)50:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)50:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)50:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)50:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)50:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)50:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)50:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)50:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)50:DRV:SCALE.DOL to
# $(CM,recursive)50:DRV:CAL_SCALE
record(calc, "$(CM,recursive)50:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)50:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)50:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)50:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)50:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)50:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)50:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)50:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)50:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)50:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)50:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)50:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)50:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)50:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)50:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)50:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)50:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)50:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)50:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)50:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)50:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)50:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)50:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)50:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)50:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)50:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)50:FWD:SCALE.DOL to
# $(CM,recursive)50:FWD:CAL_SCALE
record(calc, "$(CM,recursive)50:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)50:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)50:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)50:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)50:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)50:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)50:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)50:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)50:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)50:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)50:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)50:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)50:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)50:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)50:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)50:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)50:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)50:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)50:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)50:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)50:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)50:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)50:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)50:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)50:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)50:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)50:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)50:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)50:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)50:REV:SCALE.DOL to
# $(CM,recursive)50:REV:CAL_SCALE
record(calc, "$(CM,recursive)50:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)50:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)50:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)50:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)50:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)50:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)50:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)50:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)50:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)50:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)50:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)50:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)50:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)50:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)50:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)50:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)50:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)50:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)50:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)50:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)50:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)50:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)50:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)50:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)50:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)50:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)50:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)50:SPARE:SCALE.DOL to
# $(CM,recursive)50:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)50:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)50:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)50:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)50:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)50:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)50:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)50:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)50:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)50:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)50:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)50:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)50:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)50:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)50:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)50:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)50:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)50:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)50:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)50:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)50:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)50:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)50:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)50:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)50:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)50:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)50:QPROBE CP MS")
  field(INPC, "$(CM,recursive)50:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)50:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)50:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)50:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)50:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)50:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)50:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)50:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)50:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)50:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)50:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)50:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)50:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)50:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)50:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)50:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)50:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)50:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)50:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)50:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)50:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)50:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)50:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)50:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)50:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)50:CAV:SCALER")
	field(DOL2, "$(CM,recursive)50:CAV:SCALER")
	field(DOL3, "$(CM,recursive)50:CAV:SCALER")
	field(DOL4, "$(CM,recursive)50:CAV:SCALER")
	field(DOL5, "$(CM,recursive)50:CAV:SCALER")
	field(DOL6, "$(CM,recursive)50:CAV:SCALER")
	field(DOL7, "$(CM,recursive)50:CAV:SCALER")
	field(DOL8, "$(CM,recursive)50:CAV:SCALER")
	field(DOL9, "$(CM,recursive)50:CAV:SCALER")
	field(DOLA, "$(CM,recursive)50:CAV:SCALER")
	field(LNK1, "$(CM,recursive)50:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)50:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)50:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)50:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)50:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)50:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)50:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)50:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)50:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)50:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)50:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)50:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)50:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)50:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)50:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)50:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)50:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)50:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)50:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)50:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)50:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)50:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)50:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)50:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)50:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)50:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)50:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)50:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)50:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)50:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)50:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)50:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)50:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)50:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)50:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)50:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)50:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)50:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)50:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)50:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)50:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)50:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)50:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)50:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)50:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)50:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)50:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)50:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)50:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)50:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)50:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)50:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)50:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)50:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)50:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)50:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)50:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)50:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)50:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)50:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)50:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)50:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)50:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)50:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)50:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)50:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)50:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)50:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)50:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)50:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)50:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)50:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)50:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)50:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)50:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)50:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)50:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)50:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)50:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)50:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)50:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)50:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)50:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)50:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)50:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)50:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)50:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)50:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)50:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)50:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)50:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)50:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)50:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)50:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)50:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)50:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)60:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)60:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)60:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)60:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)60:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)60:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)60:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)60:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)60:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)60:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)60:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)60:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)60:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)60:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)60:CAV:SCALE.DOL to
# $(CM,recursive)60:CAV:CAL_SCALE
record(calc, "$(CM,recursive)60:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)60:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)60:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)60:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)60:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)60:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)60:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)60:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)60:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)60:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)60:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)60:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)60:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)60:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)60:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)60:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)60:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)60:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)60:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)60:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)60:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)60:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)60:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)60:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)60:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)60:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)60:DRV:SCALE.DOL to
# $(CM,recursive)60:DRV:CAL_SCALE
record(calc, "$(CM,recursive)60:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)60:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)60:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)60:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)60:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)60:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)60:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)60:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)60:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)60:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)60:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)60:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)60:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)60:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)60:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)60:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)60:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)60:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)60:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)60:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)60:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)60:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)60:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)60:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)60:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)60:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)60:FWD:SCALE.DOL to
# $(CM,recursive)60:FWD:CAL_SCALE
record(calc, "$(CM,recursive)60:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)60:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)60:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)60:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)60:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)60:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)60:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)60:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)60:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)60:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)60:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)60:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)60:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)60:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)60:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)60:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)60:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)60:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)60:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)60:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)60:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)60:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)60:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)60:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)60:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)60:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)60:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)60:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)60:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)60:REV:SCALE.DOL to
# $(CM,recursive)60:REV:CAL_SCALE
record(calc, "$(CM,recursive)60:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)60:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)60:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)60:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)60:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)60:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)60:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)60:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)60:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)60:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)60:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)60:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)60:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)60:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)60:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)60:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)60:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)60:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)60:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)60:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)60:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)60:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)60:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)60:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)60:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)60:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)60:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)60:SPARE:SCALE.DOL to
# $(CM,recursive)60:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)60:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)60:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)60:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)60:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)60:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)60:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)60:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)60:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)60:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)60:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)60:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)60:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)60:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)60:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)60:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)60:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)60:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)60:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)60:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)60:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)60:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)60:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)60:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)60:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)60:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)60:QPROBE CP MS")
  field(INPC, "$(CM,recursive)60:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)60:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)60:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)60:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)60:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)60:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)60:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)60:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)60:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)60:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)60:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)60:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)60:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)60:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)60:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)60:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)60:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)60:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)60:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)60:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)60:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)60:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)60:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)60:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)60:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)60:CAV:SCALER")
	field(DOL2, "$(CM,recursive)60:CAV:SCALER")
	field(DOL3, "$(CM,recursive)60:CAV:SCALER")
	field(DOL4, "$(CM,recursive)60:CAV:SCALER")
	field(DOL5, "$(CM,recursive)60:CAV:SCALER")
	field(DOL6, "$(CM,recursive)60:CAV:SCALER")
	field(DOL7, "$(CM,recursive)60:CAV:SCALER")
	field(DOL8, "$(CM,recursive)60:CAV:SCALER")
	field(DOL9, "$(CM,recursive)60:CAV:SCALER")
	field(DOLA, "$(CM,recursive)60:CAV:SCALER")
	field(LNK1, "$(CM,recursive)60:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)60:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)60:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)60:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)60:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)60:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)60:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)60:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)60:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)60:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)60:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)60:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)60:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)60:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)60:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)60:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)60:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)60:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)60:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)60:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)60:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)60:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)60:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)60:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)60:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)60:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)60:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)60:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)60:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)60:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)60:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)60:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)60:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)60:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)60:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)60:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)60:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)60:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)60:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)60:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)60:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)60:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)60:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)60:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)60:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)60:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)60:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)60:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)60:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)60:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)60:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)60:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)60:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)60:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)60:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)60:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)60:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)60:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)60:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)60:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)60:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)60:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)60:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)60:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)60:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)60:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)60:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)60:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)60:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)60:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)60:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)60:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)60:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)60:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)60:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)60:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)60:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)60:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)60:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)60:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)60:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)60:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)60:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)60:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)60:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)60:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)60:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)60:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)60:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)60:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)60:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)60:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)60:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)60:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)60:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)60:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)70:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)70:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)70:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)70:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)70:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)70:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)70:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)70:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)70:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)70:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)70:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)70:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)70:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)70:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)70:CAV:SCALE.DOL to
# $(CM,recursive)70:CAV:CAL_SCALE
record(calc, "$(CM,recursive)70:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)70:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)70:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)70:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)70:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)70:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)70:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)70:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)70:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)70:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)70:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)70:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)70:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)70:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)70:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)70:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)70:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)70:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)70:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)70:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)70:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)70:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)70:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)70:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)70:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)70:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)70:DRV:SCALE.DOL to
# $(CM,recursive)70:DRV:CAL_SCALE
record(calc, "$(CM,recursive)70:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)70:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)70:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)70:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)70:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)70:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)70:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)70:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)70:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)70:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)70:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)70:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)70:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)70:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)70:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)70:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)70:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)70:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)70:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)70:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)70:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)70:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)70:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)70:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)70:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)70:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)70:FWD:SCALE.DOL to
# $(CM,recursive)70:FWD:CAL_SCALE
record(calc, "$(CM,recursive)70:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)70:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)70:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)70:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)70:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)70:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)70:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)70:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)70:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)70:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)70:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)70:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)70:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)70:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)70:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)70:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)70:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)70:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)70:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)70:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)70:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)70:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)70:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)70:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)70:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)70:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)70:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)70:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)70:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)70:REV:SCALE.DOL to
# $(CM,recursive)70:REV:CAL_SCALE
record(calc, "$(CM,recursive)70:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)70:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)70:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)70:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)70:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)70:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)70:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)70:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)70:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)70:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)70:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)70:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)70:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)70:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)70:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)70:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)70:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)70:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)70:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)70:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)70:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)70:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)70:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)70:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)70:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)70:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)70:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)70:SPARE:SCALE.DOL to
# $(CM,recursive)70:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)70:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)70:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)70:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)70:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)70:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)70:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)70:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)70:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)70:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)70:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)70:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)70:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)70:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)70:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)70:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)70:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)70:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)70:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)70:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)70:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)70:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)70:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)70:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)70:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)70:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)70:QPROBE CP MS")
  field(INPC, "$(CM,recursive)70:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)70:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)70:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)70:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)70:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)70:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)70:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)70:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)70:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)70:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)70:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)70:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)70:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)70:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)70:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)70:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)70:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)70:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)70:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)70:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)70:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)70:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)70:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)70:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)70:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)70:CAV:SCALER")
	field(DOL2, "$(CM,recursive)70:CAV:SCALER")
	field(DOL3, "$(CM,recursive)70:CAV:SCALER")
	field(DOL4, "$(CM,recursive)70:CAV:SCALER")
	field(DOL5, "$(CM,recursive)70:CAV:SCALER")
	field(DOL6, "$(CM,recursive)70:CAV:SCALER")
	field(DOL7, "$(CM,recursive)70:CAV:SCALER")
	field(DOL8, "$(CM,recursive)70:CAV:SCALER")
	field(DOL9, "$(CM,recursive)70:CAV:SCALER")
	field(DOLA, "$(CM,recursive)70:CAV:SCALER")
	field(LNK1, "$(CM,recursive)70:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)70:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)70:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)70:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)70:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)70:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)70:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)70:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)70:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)70:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)70:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)70:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)70:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)70:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)70:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)70:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)70:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)70:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)70:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)70:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)70:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)70:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)70:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)70:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)70:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)70:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)70:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)70:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)70:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)70:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)70:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)70:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)70:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)70:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)70:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)70:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)70:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)70:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)70:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)70:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)70:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)70:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)70:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)70:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)70:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)70:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)70:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)70:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)70:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)70:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)70:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)70:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)70:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)70:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)70:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)70:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)70:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)70:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)70:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)70:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)70:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)70:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)70:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)70:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)70:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)70:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)70:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)70:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)70:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)70:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)70:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)70:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)70:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)70:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)70:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)70:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)70:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)70:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)70:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)70:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)70:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)70:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)70:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)70:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)70:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)70:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)70:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)70:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)70:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)70:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)70:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)70:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)70:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)70:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)70:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)70:ACQ_STAT_WIDTH_SEL.L PP")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)80:CAV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:CAV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)80:CAV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)80:CAV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)80:CAV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)80:CAV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:CAV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:CAV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:CAV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)80:CAV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)80:CAV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)80:CAV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)80:CAV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)80:CAV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)80:CAV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)80:CAV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)80:CAV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)80:CAV:CAL_SCALE_MV PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:CAV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)80:CAV:SCALE.DOL to
# $(CM,recursive)80:CAV:CAL_SCALE
record(calc, "$(CM,recursive)80:CAV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:CAV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)80:CAV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)80:CAV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:CAV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)80:CAV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)80:CAV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)80:CAV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)80:CAV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:CAV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)80:CAV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)80:CAV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)80:CAV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)80:CAV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)80:CAV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)80:DRV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:DRV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)80:DRV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)80:DRV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)80:DRV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)80:DRV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:DRV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:DRV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:DRV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)80:DRV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)80:DRV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)80:DRV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)80:DRV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)80:DRV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)80:DRV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)80:DRV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)80:DRV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)80:DRV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:DRV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DRV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)80:DRV:SCALE.DOL to
# $(CM,recursive)80:DRV:CAL_SCALE
record(calc, "$(CM,recursive)80:DRV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:DRV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)80:DRV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)80:DRV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:DRV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)80:DRV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)80:DRV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)80:DRV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)80:DRV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:DRV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)80:DRV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)80:DRV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)80:DRV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)80:DRV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)80:DRV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)80:FWD:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:FWD:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)80:FWD:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)80:FWD:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)80:FWD:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)80:FWD:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:FWD:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:FWD:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:FWD:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)80:FWD:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)80:FWD:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)80:FWD:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)80:FWD:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)80:FWD:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)80:FWD:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)80:FWD:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)80:FWD:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)80:FWD:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:FWD:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:FWD:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)80:FWD:SCALE.DOL to
# $(CM,recursive)80:FWD:CAL_SCALE
record(calc, "$(CM,recursive)80:FWD:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:FWD:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)80:FWD:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)80:FWD:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:FWD:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)80:FWD:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)80:FWD:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)80:FWD:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)80:FWD:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:FWD:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)80:FWD:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)80:FWD:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)80:FWD:AMEAN_ADC")
  field(INPB, "$(CM,recursive)80:FWD:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)80:FWD:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)80:REV:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)80:REV:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:REV:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)80:REV:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)80:REV:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)80:REV:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)80:REV:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)80:REV:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)80:REV:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:REV:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:REV:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:REV:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)80:REV:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)80:REV:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)80:REV:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)80:REV:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:REV:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)80:REV:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)80:REV:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)80:REV:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)80:REV:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)80:REV:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:REV:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:REV:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)80:REV:SCALE.DOL to
# $(CM,recursive)80:REV:CAL_SCALE
record(calc, "$(CM,recursive)80:REV:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:REV:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)80:REV:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)80:REV:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:REV:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)80:REV:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)80:REV:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)80:REV:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)80:REV:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:REV:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)80:REV:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)80:REV:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)80:REV:AMEAN_ADC")
  field(INPB, "$(CM,recursive)80:REV:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)80:REV:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal.template:
#
# Calculate scale factor to convert
# ADC counts to engineering units:
# MV for cavity signal; sqrt(Watts)
# for others
#----------------------------------
#==================================

#==================================
# In the lab, measure the ADC counts
# at 10 dBm RF input power and then 
# extrapolate to calculate RF power
# at ADC full scale
#
# P[dBm] = 10*log10(((V^2/R)*1e-3)/1mW)
#    V in volts
#    R = 50 ohms
#
# Per A. Benwell, calculate power [dBm] 
# at ADC full scale: 
# P = 10 + 20*log10(32767/counts_at_10_dBm)
#==================================

record(ai, "$(CM,recursive)80:SPARE:CAL_ADC_10DBM") {
  field(VAL, "0")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_REF_CALC")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:SPARE:CAL_REF_CALC") {
  field(CALC, "10+20*LOG(C/B)")
  field(INPB, "$(CM,recursive)80:SPARE:CAL_ADC_10DBM")
# full-scale ADC counts
# for signed 16-bit ADC, = 2^15 - 1
  field(INPC, "32767")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_REF_PWR")
}

record(ao, "$(CM,recursive)80:SPARE:CAL_REF_PWR") {
  field(SCAN, "Passive")
  field(EGU,  "dBm")
  field(PREC, "2")
  field(DOL,  "$(CM,recursive)80:SPARE:CAL_REF_CALC")
  field(OMSL, "closed_loop")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_SCALE_SQRTW")
}

#==================================
# Measured losses in the signal path:
#   coupler
#   cable
#   fixed attenuator
#   other - for losses not attributed
#           to a particular component
# Loss values should be entered as 
# *positive* numbers.
#
# Not all of these are used for every
# signal. For example, DRV (loopback)
# is measured at the chassis, so
# there are no cable losses to consider.
# Unused values should be set to 0.
#==================================
record(ao, "$(CM,recursive)80:SPARE:CAL_LOSS_COUPLER") {
  field(EGU, "dB")
  field(PINI, "YES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "3")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:SPARE:CAL_LOSS_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:SPARE:CAL_LOSS_ATTEN") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:SPARE:CAL_LOSS_OTHER") {
  field(SCAN, "Passive")
  field(EGU,  "dB")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL")
  info(autosaveFields, "VAL")
}

record(calc, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL") {
  field(SCAN, "Passive")
  field(CALC, "A+B+C+D")
  field(INPA, "$(CM,recursive)80:SPARE:CAL_LOSS_COUPLER")
  field(INPB, "$(CM,recursive)80:SPARE:CAL_LOSS_CABLE")
  field(INPC, "$(CM,recursive)80:SPARE:CAL_LOSS_ATTEN")
  field(INPD, "$(CM,recursive)80:SPARE:CAL_LOSS_OTHER")
  field(EGU,  "dB")
  field(PREC, "2")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_SCALE_SQRTW")
  info(autosaveFields, "VAL")
}

#==================================
# Calculate calibration scale factor 
# in sqrt(Watts).
#
#    (1/d)*10^(referenceRfPower+losses-30)/20
# (Note that 'losses' is added because it is
#  a positive number. If we switch to using 
#  negative values, then 'losses' should be
#  subtracted.)
#    c = optional normalization factor:
#        Value to divide by if raw signal
#        (to which scale factor will be applied)
#        is not in fraction of full scale
#	 Can be link or static value. 
#	 If unused, set to 1.
#
# For cavity probe scale factor,
# set CALCOUT macro to
#    $(CM,recursive)80:SPARE:CAL_SCALE_MV PP
# in order to calculate scale in MV. In that
# case, CAL_SCALE is set by 
# <cavity>:CAVSCALEF in rfSigCalCav.template
#==================================

record(calcout, "$(CM,recursive)80:SPARE:CAL_SCALE_SQRTW") {
  field(CALC, "(10^((A+B-30)/20))/C")
  field(INPA, "$(CM,recursive)80:SPARE:CAL_REF_PWR")
  field(INPB, "$(CM,recursive)80:SPARE:CAL_LOSS_TOTAL")
  field(INPC, "1.0")
  field(OOPT, "Every Time")
  field(EGU,  "sqrt(W)")
  field(OUT,  "$(CM,recursive)80:SPARE:CAL_SCALE PP")
  info(autosaveFields, "VAL")
}

record(ao, "$(CM,recursive)80:SPARE:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:SPARE:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Final calculated scale factor
# which gets applied to waveform data
# "SCALE" PV resides in wfCav.template
# or gun/buncher scllrfApp. This calc
# is left over from when we had a 'fudge'
# scale factor for every signal. Now
# that is only supported for cavity probe
# and that is handled in rfSigCalCav.template.
# If needed for other signals, add here.
# If you remove this extraneous calc,
# be sure to set $(CM,recursive)80:SPARE:SCALE.DOL to
# $(CM,recursive)80:SPARE:CAL_SCALE
record(calc, "$(CM,recursive)80:SPARE:SCALE_CALC") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:SPARE:CAL_SCALE")
  field(INPB, "1")
  field(FLNK,  "$(CM,recursive)80:SPARE:SCALE")
}

# Signal waveform mean converted to counts
# To support ADC calibration
record(calc, "$(CM,recursive)80:SPARE:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:SPARE:AMEAN CPP MSI")
  field(INPB, "$(CM,recursive)80:SPARE:CAL_SCALE MSI")
  field(INPC, "32767")
  field(EGU,  "counts")
  field(FLNK, "$(CM,recursive)80:SPARE:CAL_LOSS_REF_DB")
}

# Convenience record to push the current
# counts value to $(CM,recursive)80:SPARE:CAL_ADC_10DBM.
# Should be done during calibration when
# 10dBm is injected into ADC channel
record(ao, "$(CM,recursive)80:SPARE:PUSH_ADC_10DBM") {
  field(DESC, "Push current counts for 10 dBm")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:SPARE:AMEAN_ADC")
  field(OUT,  "$(CM,recursive)80:SPARE:CAL_ADC_10DBM PP")
}
# Calculated db ratio of live counts wrt counts
# at 10 dBm. To support measuring signal loss
# due to cable (and other components).
record(calc, "$(CM,recursive)80:SPARE:CAL_LOSS_REF_DB") {
  field(DESC, "Live loss wrt 10 dBm")
  field(CALC, "20*LOG(B/A)")
  field(PREC, "4")
  field(INPA, "$(CM,recursive)80:SPARE:AMEAN_ADC")
  field(INPB, "$(CM,recursive)80:SPARE:CAL_ADC_10DBM CP")
  field(EGU,  "dB")
  field(PINI, "YES")
}

#==================================
# Cable delay length
#==================================

record(ao, "$(CM,recursive)80:SPARE:CAL_DELAY_CABLE") {
  field(SCAN, "Passive")
  field(EGU, "ns")
  field(PREC, "3")
  field(OMSL, "supervisory")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}
#==================================
#----------------------------------
# rf_sig_cal_simple.template:
#
# Scale factor to convert waveform
# fraction-of-full-scale readout 
# to engineering units: MV for cavity 
# signal; sqrt(Watts) for others
#----------------------------------
#==================================

record(ao, "$(CM,recursive)80:DAC:CAL_SCALE") {
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(PREC, "7")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:DAC:SCALE_CALC")
  info(autosaveFields, "VAL")
}

# Unnecessary intermediate PV 
# to be compatible with wfCav.template
record(ao, "$(CM,recursive)80:DAC:SCALE_CALC") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM,recursive)80:DAC:CAL_SCALE")
  field(FLNK, "$(CM,recursive)80:DAC:SCALE")
}

# When reference is in sqrt(Watts)
record(calcout, "$(CM,recursive)80:DAC:SCALETOREF") {
  field(CALC, "B*A/C")
  field(INPA, "$(CM,recursive)80:DAC:CAL_SCALE") # current scaling
  field(INPB, "1")# B = known reference, in power
  field(INPC, "$(CM,recursive)80:DAC:AMEAN") # current amplitude mean
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)80:DAC:CAL_SCALE PP") # new scaling
  field(PINI, "NO") # do not overwrite cal on startup
  info(autosaveFields, "B")
}

# Signal waveform mean converted to counts
# To be consistent with signals. Keep this?
record(calc, "$(CM,recursive)80:DAC:AMEAN_ADC") {
  field(DESC, "WF mean in counts")
  field(CALC, "FLOOR(C*A/B)")
  field(INPA, "$(CM,recursive)80:DAC:AMEAN CPP")
  field(INPB, "$(CM,recursive)80:DAC:CAL_SCALE")
  field(INPC, "32767")
  field(EGU,  "counts")
}
#==================================
#----------------------------------
# rf_sig_cal_cav.template:
#
# Calibration database specifically
# for cavity probe scaling.
# Load in addition to rf_sig_cal
# and cav_parms
#----------------------------------
#==================================

#==================================
# Calculate calibration scale factor 
# in MV. In this case, PV CAV:SCALEF
# writes to CAL_SCALE.
#
# Users can use CALSCALE_SEL PV
# to choose between different 
# calibration methods. The chosen
# method gets pushed CAV:SCALEF.
#
# Currently there are 2 method options.
# If more added, add more SCALEFCALC PVs
#==================================

record(calcout, "$(CM,recursive)80:CAV:CAL_SCALE_MV") {
  field(CALC, "A*SQRT(B*C)/1E6")
  field(INPA, "$(CM,recursive)80:CAV:CAL_SCALE_SQRTW")
  field(INPB, "$(CM,recursive)80:QPROBE CP MS")
  field(INPC, "$(CM,recursive)80:IMPED CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)80:CAV:SCALER_SEL.A PP")
}

# Additional scaling whose value is set
# by beam-based measurement.
record(ao, "$(CM,recursive)80:CAV:FUDGE") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:FUDGE_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value before FUDGE
# is applied ('R' = raw)
record(ao, "$(CM,recursive)80:CAV:SCALER") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:SCALER_SEQ")
  info(autosaveFields, "VAL")
}

# Scale factor value after FUDGE
# is applied ('F' = fudge)
record(ao, "$(CM,recursive)80:CAV:SCALEF") {
  field(SCAN, "Passive")
  field(PREC, "2")
  field(OMSL, "supervisory")
  field(VAL,  "1")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)80:CAV:SCALEF_SEQ")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM,recursive)80:CAV:SCALEFCALCA") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:CAV:SCALER_SEL.A CP")
  field(INPB, "$(CM,recursive)80:CAV:FUDGE_SEL.A CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)80:CAV:SCALEF_SEL.A PP")
}

record(calcout, "$(CM,recursive)80:CAV:SCALEFCALCB") {
  field(CALC, "A*B")
  field(INPA, "$(CM,recursive)80:CAV:SCALER_SEL.B CP")
  field(INPB, "$(CM,recursive)80:CAV:FUDGE_SEL.B CP")
  field(EGU,  "MV")
  field(PREC, "7")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)80:CAV:SCALEF_SEL.B PP")
}

# Be sure to set CP flags for inputs
record(calcout, "$(CM,recursive)80:CAV:PWRSCALE_CALC") {
  field(CALC, "1000*A^2/B^2")
  field(INPA, "$(CM,recursive)80:CAV:CAL_SCALE_SQRTW CP")
  field(INPB, "$(CM,recursive)80:CAV:SCALE CP")
  field(EGU,  "mW")
  field(OOPT, "Every Time")
  field(OUT,  "$(CM,recursive)80:CAV:PWRSCALE PP")
}

# Selector for cavity scale factor method
record(longout,"$(CM,recursive)80:CALPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)80:CAVSCALE_SEL CP")
}

# Selector for cavity mode
# Used for waveform statistics
record(calcout,"$(CM,recursive)80:WFSTATPARMSEL_") {
	field(CALC,"A=4?1:0")
	field(INPA, "$(CM,recursive)80:RFMODECTRL CP")
    field(FLNK,"$(CM,recursive)80:WFSTATPARMSEL")
}
record(longout,"$(CM,recursive)80:WFSTATPARMSEL") {
	field(OMSL,"closed_loop")
	field(DOL, "$(CM,recursive)80:WFSTATPARMSEL_")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)80:CAV:SCALER_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)80:CAV:SCALER")
	field(DOL2, "$(CM,recursive)80:CAV:SCALER")
	field(DOL3, "$(CM,recursive)80:CAV:SCALER")
	field(DOL4, "$(CM,recursive)80:CAV:SCALER")
	field(DOL5, "$(CM,recursive)80:CAV:SCALER")
	field(DOL6, "$(CM,recursive)80:CAV:SCALER")
	field(DOL7, "$(CM,recursive)80:CAV:SCALER")
	field(DOL8, "$(CM,recursive)80:CAV:SCALER")
	field(DOL9, "$(CM,recursive)80:CAV:SCALER")
	field(DOLA, "$(CM,recursive)80:CAV:SCALER")
	field(LNK1, "$(CM,recursive)80:CAV:SCALER_SEL.A PP")
	field(LNK2, "$(CM,recursive)80:CAV:SCALER_SEL.B PP")
	field(LNK3, "$(CM,recursive)80:CAV:SCALER_SEL.C PP")
	field(LNK4, "$(CM,recursive)80:CAV:SCALER_SEL.D PP")
	field(LNK5, "$(CM,recursive)80:CAV:SCALER_SEL.E PP")
	field(LNK6, "$(CM,recursive)80:CAV:SCALER_SEL.F PP")
	field(LNK7, "$(CM,recursive)80:CAV:SCALER_SEL.G PP")
	field(LNK8, "$(CM,recursive)80:CAV:SCALER_SEL.H PP")
	field(LNK9, "$(CM,recursive)80:CAV:SCALER_SEL.I PP")
	field(LNKA, "$(CM,recursive)80:CAV:SCALER_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)80:CAV:SCALER_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)80:CAV:SCALER_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)80:CAV:SCALER_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)80:CAV:SCALER_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)80:CAV:SCALER_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)80:CAV:SCALER PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)80:CAV:SCALER_CTRL")
{
	field(INPA, "$(CM,recursive)80:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)80:CAV:SCALER_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)80:CAV:FUDGE_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL2, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL3, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL4, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL5, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL6, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL7, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL8, "$(CM,recursive)80:CAV:FUDGE")
	field(DOL9, "$(CM,recursive)80:CAV:FUDGE")
	field(DOLA, "$(CM,recursive)80:CAV:FUDGE")
	field(LNK1, "$(CM,recursive)80:CAV:FUDGE_SEL.A PP")
	field(LNK2, "$(CM,recursive)80:CAV:FUDGE_SEL.B PP")
	field(LNK3, "$(CM,recursive)80:CAV:FUDGE_SEL.C PP")
	field(LNK4, "$(CM,recursive)80:CAV:FUDGE_SEL.D PP")
	field(LNK5, "$(CM,recursive)80:CAV:FUDGE_SEL.E PP")
	field(LNK6, "$(CM,recursive)80:CAV:FUDGE_SEL.F PP")
	field(LNK7, "$(CM,recursive)80:CAV:FUDGE_SEL.G PP")
	field(LNK8, "$(CM,recursive)80:CAV:FUDGE_SEL.H PP")
	field(LNK9, "$(CM,recursive)80:CAV:FUDGE_SEL.I PP")
	field(LNKA, "$(CM,recursive)80:CAV:FUDGE_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)80:CAV:FUDGE_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)80:CAV:FUDGE_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "2")
	field(FLNK, "$(CM,recursive)80:CAV:FUDGE_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)80:CAV:FUDGE_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)80:CAV:FUDGE_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)80:CAV:FUDGE PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)80:CAV:FUDGE_CTRL")
{
	field(INPA, "$(CM,recursive)80:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)80:CAV:FUDGE_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)80:CAV:SCALEF_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL2, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL3, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL4, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL5, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL6, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL7, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL8, "$(CM,recursive)80:CAV:SCALEF")
	field(DOL9, "$(CM,recursive)80:CAV:SCALEF")
	field(DOLA, "$(CM,recursive)80:CAV:SCALEF")
	field(LNK1, "$(CM,recursive)80:CAV:SCALEF_SEL.A PP")
	field(LNK2, "$(CM,recursive)80:CAV:SCALEF_SEL.B PP")
	field(LNK3, "$(CM,recursive)80:CAV:SCALEF_SEL.C PP")
	field(LNK4, "$(CM,recursive)80:CAV:SCALEF_SEL.D PP")
	field(LNK5, "$(CM,recursive)80:CAV:SCALEF_SEL.E PP")
	field(LNK6, "$(CM,recursive)80:CAV:SCALEF_SEL.F PP")
	field(LNK7, "$(CM,recursive)80:CAV:SCALEF_SEL.G PP")
	field(LNK8, "$(CM,recursive)80:CAV:SCALEF_SEL.H PP")
	field(LNK9, "$(CM,recursive)80:CAV:SCALEF_SEL.I PP")
	field(LNKA, "$(CM,recursive)80:CAV:SCALEF_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)80:CAV:SCALEF_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)80:CAV:SCALEF_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "1")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "7")
	field(FLNK, "$(CM,recursive)80:CAV:SCALEF_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)80:CAV:SCALEF_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)80:CAV:SCALEF_SEL")
	field(OUTA, "$(CM,recursive)80:CAV:CAL_SCALE PP")
	field(OUTB, "$(CM,recursive)80:CAV:SCALEF PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)80:CAV:SCALEF_CTRL")
{
	field(INPA, "$(CM,recursive)80:CALPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)80:CAV:SCALEF_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)80:ACQ_STAT_START_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL2, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL3, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL4, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL5, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL6, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL7, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL8, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOL9, "$(CM,recursive)80:ACQ_STAT_START")
	field(DOLA, "$(CM,recursive)80:ACQ_STAT_START")
	field(LNK1, "$(CM,recursive)80:ACQ_STAT_START_SEL.A PP")
	field(LNK2, "$(CM,recursive)80:ACQ_STAT_START_SEL.B PP")
	field(LNK3, "$(CM,recursive)80:ACQ_STAT_START_SEL.C PP")
	field(LNK4, "$(CM,recursive)80:ACQ_STAT_START_SEL.D PP")
	field(LNK5, "$(CM,recursive)80:ACQ_STAT_START_SEL.E PP")
	field(LNK6, "$(CM,recursive)80:ACQ_STAT_START_SEL.F PP")
	field(LNK7, "$(CM,recursive)80:ACQ_STAT_START_SEL.G PP")
	field(LNK8, "$(CM,recursive)80:ACQ_STAT_START_SEL.H PP")
	field(LNK9, "$(CM,recursive)80:ACQ_STAT_START_SEL.I PP")
	field(LNKA, "$(CM,recursive)80:ACQ_STAT_START_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)80:ACQ_STAT_START_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)80:ACQ_STAT_START_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "0")
	field(B,    "0.06")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)80:ACQ_STAT_START_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)80:ACQ_STAT_START_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)80:ACQ_STAT_START_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)80:ACQ_STAT_START PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)80:ACQ_STAT_START_CTRL")
{
	field(INPA, "$(CM,recursive)80:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)80:ACQ_STAT_START_SEL.L PP")
}
# Maintain multiple (up to 10) values of
# a parameter:
# 
# Record holding the parameter $[parm]
# must FLNK -> $[parm]_SEQ
#
# Different values of the parameter 
# $[parm0]..$[parm9] are stored in
# $[parm]_SEL.A .. $[parm]_SEL.J
#
# The actual value of the parameter is
# selected based on the value of a controlling
# PV $[ctrl] using a 'mapping' CALC expression
# $[mapc] to convert the $[ctrl] value into
# the integer range 1..10. 
#
# The selected value of $[parm] is written
# to output link $[outl]
#
# A) When the user writes $[parm] then
#    its value is deposited in the currently
#    selected field of $[parm]_SEL and
#    propagated to $[outl]
#
# B) When the user writes any of $[parm]_SEL.A..J
#    then the value is stored there. If she
#    writes to the currently 'active' field\
#    (as selected by $[ctrl]) then 
#    the $[parm] PV itself is updated and the
#    value is also propagated to $[outl]
#
# C) If the user changes $[ctrl] then the
#    corresponding value $[parm]_SEL.A..J is 
#    propagated to $[outl] and $[parm].
#
# D) At initialization, the value corresponding
#    to the initial setting of $[ctrl] is written
#    to $[outl] and $[parm]
#
# MACROS:
#
# $[parm]  PV holding the parameter
# $[ctrl]  PV that controls the selection of
#          the currently active setting of 
#          $[parm]
# $[outl]  PV where the currently selected 
#          value of $[parm] shall be written-to
#          (if applicable)
# $[prec]  Precision of the $[parm]_SEL variable
#          (should be set to same value as $[parm].PREC)
#
# $[parm0]..$[parm9] initial values of $[parm]
#
# $[mapc]  CALC expression to convert the value
#          of the controlling PV which is available
#          as 'A' to the range 1..10 (or less if
#          less selections are used).
#          E.g., to convert the value of a bo (0/1)
#          you would set mapc to 'A+1' (0..1 -> 1..2)
#
# NOTE:    This sb snippet is loaded/expanded
#          for every parameter under control
#          of $[ctrl]. ALSO, the companion
#          snippet parmCtl.db must be loaded/expanded
#          but only once per controlling PV.
#          

# This record, when scanned (as a result
# of $[parm] being written and processing
# its FLNK to this record] fetches $[parm]
# and deposits the value into the currently
# selected field of the SEL record.
# 
# NOTE: unfortunately, the selection
#       mechanism of the SEQ and SEL
#       records differs: SEQ is 1-based,
#       SEL is 0-based -- now that makes
#       sense, doesn't it?
#
record(seq, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEQ")
{
	field(DESC, "Propagate Parm -> SEL")
	field(DOL1, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL2, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL3, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL4, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL5, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL6, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL7, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL8, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOL9, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(DOLA, "$(CM,recursive)80:ACQ_STAT_WIDTH")
	field(LNK1, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.A PP")
	field(LNK2, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.B PP")
	field(LNK3, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.C PP")
	field(LNK4, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.D PP")
	field(LNK5, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.E PP")
	field(LNK6, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.F PP")
	field(LNK7, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.G PP")
	field(LNK8, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.H PP")
	field(LNK9, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.I PP")
	field(LNKA, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.J PP")
	field(SELM, "Specified")
	field(SELL, "$(CM,recursive)80:ACQ_STAT_WIDTH_CTRL NPP NMS")
}

# The $[parm]_SEL record holds all possible
# values of $[parm] in the 'A..J' fields.
#
# When processed it updates it's own VAL
# to the currently selected input. Input
# 'L' serves as the (1-based) selector,
# i.e., L==1 selects 'A'->'VAL'.
# 
# When the selected value changes (as a 
# result of SEQ depositing a new value OR
# $[ctrl] changing and updating L) then
# the propagator record attached to the
# FLNK is processed.
#

record(sub, "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL")
{
#	field(INAM, "parmSelSubInit")
	field(SNAM, "parmSelSubProc")
	field(A,    "1")
	field(B,    "0.04")
	field(C,    "")
	field(D,    "")
	field(E,    "")
	field(F,    "")
	field(G,    "")
	field(H,    "")
	field(I,    "")
	field(J,    "")
	field(PREC, "0")
	field(FLNK, "$(CM,recursive)80:ACQ_STAT_WIDTH_PROP")
  	info(autosaveFields, "A B C D E F G H I J")
}

#
# Finally the dfanout record 
# fetches the currently selected
# value from SEL and propagates it
# to $[outl]. It also updates
# $[parm] itself so that $[parm]
# always reflects the currently
# selected value.
#
record(dfanout,  "$(CM,recursive)80:ACQ_STAT_WIDTH_PROP")
{
	field(OMSL, "closed_loop")
	field(DOL,  "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL")
	field(OUTA, "")
	field(OUTB, "$(CM,recursive)80:ACQ_STAT_WIDTH PP")
}

# Drive the SEL record when the 'ctrl' PV changes
# Use a CA link to monitor such changes
record(calcout,  "$(CM,recursive)80:ACQ_STAT_WIDTH_CTRL")
{
	field(INPA, "$(CM,recursive)80:WFSTATPARMSEL CP")
	field(CALC, "A+1")
	field(DOPT, "Use CALC")
	field(OOPT, "On Change")
	field(OUT,  "$(CM,recursive)80:ACQ_STAT_WIDTH_SEL.L PP")
}
