record(ai, "MODU:$(MICRO):$(CRAT)$(SLOT):CH$(CHAN)") {
  field(DESC, "$(NAME)")
  field(DTYP, "SAM")
  field(SCAN, "1 second")
  field(INP,  "#B0 C$(CRAT) N$(SLOT) F17 A$(CHAN)@DATA")
  field(EGU,  "V")
  field(PREC, "8")
  field(ADEL, ".0001")
  field(HOPR, "10")
  field(LOPR, "-10")
  field(PINI, "YES")
  field(FLNK, "$(NAME)VRaw")
#  field(SDIS, "MODU:$(MICRO):$(CRAT)$(SLOT):STATUS NPP")
}

record(ai, "$(NAME)VRaw") {
  field(DESC, "SAM Card $(SLOT) Ch $(CHAN)") 
  field(INP,  "MODU:$(MICRO):$(CRAT)$(SLOT):CH$(CHAN)")
  field(PREC, "8")
  field(EGU,  "V")
  field(HOPR, "10")
  field(LOPR, "-10")
  field(LOW,  ".00043")
  field(HIGH, "6.45")
  field(HSV,  "INVALID")
  field(LSV,  "INVALID")
  field(HYST, "0")
  field(ADEL, "0.0001")
  field(MDEL, "0.0001")
  field(SIOL, "$(NAME)SimVRaw")
  field(SIML, "$(NAME)Sim")
  field(FLNK, "$(NAME)Curr")
}

# Simulation PVs
record(ai, "$(NAME)SimVRaw") {
 field(DESC,"Simulatated Raw Volts")
 field(PINI, "YES")
}

record(bo, "$(NAME)Sim")
{
  field(DESC, "Simulation Mode")
  field(PINI, "YES")
  field(ZNAM, "Off")
  field(ONAM, "On")
}

# Information PVs
# from the Spear EPICS application 
# EtherIoc/etherIocApp/tempInfo.db
#
record (stringout,"$(NAME)Desc")
{
  field(DESC, "Description")
  field(VAL, "$(DESC)")
  field(PINI, "YES")
}
record (stringout,"$(NAME)RLoc")
{
  field(DESC, "Reference Location")
  field(VAL, "$(RLOC)")
  field(PINI, "YES")
}

record (stringout,"$(NAME)TCDwg")
{
  field(DESC, "Thermocouple Drawing")
  field(VAL, "$(DWG)")
  field(PINI, "YES")
}

# Thermocouple type "T" Reference Conversion from volts to degC

# Convert the sam channel volts to current
# A - volts
# C - sam max voltage
# D - simulatation mode (0=Off,1=On)
# E - simulatation volts
record(calc, "$(NAME)Curr") {
  field(DESC, "Temp Ref Current")
  field(CALC, "B?(C-A)/B:0")
  field(INPA, "$(NAME)VRaw NPP MS")
  field(INPB, "1005100")
  field(INPC, "10.24")
  field(EGU,  "Amp")
  field(PREC, "2")
  field(FLNK, "$(NAME)Resistance")
}

record(calcout, "$(NAME)Resistance") {
  field(DESC, "Temp Ref Resistance")
  field(CALC, "B?A/B:0")
  field(IVOA, "Continue normally")
  field(INPA, "$(NAME)VRaw")
  field(INPB, "$(NAME)Curr NPP MS")
  field(EGU,  "Ohm")
  field(PREC, "2")
  field(OUT, "$(NAME)Tbar.D PP")
}

# INPD - Resistance
record(calc, "$(NAME)Tbar") {
  field(DESC, "$(DESC)")
  field(ASG,  "Internal")
  field(CALC, "D>0?(A+B*LOGE(D)+C*(LOGE(D))^3):0")
  field(INPA, "1.46161E-3")
  field(INPB, "2.39427E-4")
  field(INPC, "9.59358E-8")
  field(PREC, "2")
  field(FLNK, "$(NAME)Kelvin")
}

record(calc, "$(NAME)Kelvin") {
  field(DESC, "$(DESC)")
  field(DESC, "Temp Ref in Kelvin")
  field(CALC, "A#0?1.0/A:0")
  field(INPA, "$(NAME)Tbar")
  field(EGU , "K")
  field(PREC, "2")
  field(FLNK, "$(NAME)Check")
}

# Convert from Kelvin to Celsius and check for invalid raw data
# A - ref temp in Kelvin
# B - offset to convert from Kelvin to ref temp in DegC
# C - raw data serverity
# D - raw data status
# E - value that indicates illegal value
record(calcout,  "$(NAME)Check") {
  field(DESC, "$(DESC)")
  field(ASG,  "Internal")
  field(CALC, "C=3&&(D=4||D=6)?E:A-B")
  field(IVOA, "Continue normally")
  field(INPA, "$(NAME)Kelvin")
  field(INPB, "273.15")

# If if the raw data has an invalid severity due to
# a low or high alarm status, then set the reference
# temperature to an illegal value in E, because the 
# raw data is out of range.
  field(INPC, "$(NAME)VRaw.SEVR")
  field(INPD, "$(NAME)VRaw.STAT")
  field(INPE, "-573.0")
  field(EGU,  "Deg. C")
  field(OUT,  "$(NAME) PP")
  field(FLNK, "$(NAME)_T")
}

record(ai,  "$(NAME)") {
  field(DESC, "$(DESC)")
  field(EGU,  "Deg. C")
  field(PREC, "0")
  field(HIHI, "$(HIHI)")
  field(HIGH, "$(HIGH)")
  field(LOLO, "$(LOLO)")
  field(LOW,  "$(LOW)")
  field(HHSV, "$(HHSV)")
  field(HSV,  "$(HSV)")
  field(HYST, "0")
  field(FLNK, "$(NAME)VCor")
}

# Convert from degC to mV. This pv is
# used in the conversion to degC
# for SAM Channels 0-14 and 16-30.
record(sub, "$(NAME)VCor") {
  field(DESC, "$(DESC)")
  field(ASG,  "Internal")
  field(INAM, "InitPoly")
  field(SNAM, "CalcPolySpear")
  field(INPA, "0.0E00")
  field(INPB, "0.387481063640E-01")
  field(INPC, "0.332922278800E-04")
  field(INPD, "0.206182434040E-06")
  field(INPE, "-0.218822568460E-08")
  field(INPF, "0.109968809280E-10")
  field(INPG, "-0.308157587720E-13")
  field(INPH, "0.454791352900E-16")
  field(INPI, "-0.275129016730E-19")
  field(INPL, "$(NAME).VAL NPP MS")
  field(EGU,  "mV")
  field(PREC, "6")
  field(FLNK, "$(NAME)F")
}

record(calc,  "$(NAME)F") {
  field(DESC, "$(DESC)")
  field(CALC, "A*1.8+32")
  field(INPA, "$(NAME)")
  field(EGU,  "Deg. F")
  field(PREC, "2")
}

# Clemens function: to_tctemp(volts,ref_volts)
#
# Input:
# A - reference temperature
# B - lower limit range of raw data
# C - upper limit range of raw data
# 
# Output:
# F - current
# G - resistance
# H - ref temperature in kelvin
# I - ref temperature in celsius
# J - voltage error due to temp of ref plane
# Note: if this function is working properly
# then 
record(sub, "$(NAME)_T") {
  field(DESC, "$(DESC)")
  field(INAM, "InitAnalogCalc")
  field(SNAM, "AnalogTcRefTemp")
  field(INPA, "$(NAME)VRaw NPP MS")
  field(INPB, "$(NAME)VRaw.LOW")
  field(INPC, "$(NAME)VRaw.HIGH")
  field(PREC, "0")
  field(EGU,  "Deg. C")
  field(BRSV, "INVALID") 
  field(FLNK, "$(NAME)VPlate")
}

# This is the voltage error due to the temperature of 
# the reference plane. If the plane were at 0 degC, then
# the vplate would be 0. This is used in when calculating
# temperatures for SAM channels 0-14 and 15-30.
record(ai,  "$(NAME)VPlate") {
  field(DESC, "$(DESC)")
  field(INP,  "$(NAME)_T.J NPP MS")
  field(PREC, "0")
}

