record(fanout, "$(BL)GammaDoseFanout")
{
  field(DESC, "Fanout Gamma data read")
  field(ASG,  "Internal")
  field(LNK1, "$(BL)GammaDoseInfo")
  field(LNK2, "$(BL)GammaDoseSec")
  field(LNK3, "$(BL)GammaDoseSecHst")
  field(LNK4, "$(BL)GammaDoseRateHst")
}

record(longout, "$(BL)GammaDoseRateEvent") {
   field(DESC, "Gamma infoeEvent Id")
   field(ASG,  "Internal")
   field(VAL,  "42")
   field(PINI, "YES")
}

record(longout, "$(BL)GammaNegErrLimit") {
   field(DESC, "Max num of neg. dose")
   field(VAL,  "60")
   field(HOPR, "300")
   field(DRVL, "0")
   field(PINI, "YES")
}

record(ao, "$(BL)GammaDoseLowLimit") {
   field(DESC, "Lower Limit for Neg Dose")
   field(VAL,  "-0.09")
   field(EGU,  "mR/hr")
   field(PREC, "2")
   field(PINI, "YES")
   info(autosaveFields,"VAL")
}

record(aSub, "$(BL)GammaDoseInfo") {
  field(DESC, "Get secs between data reads")
  field(ASG,  "Internal")
  field(INAM, "myAsubInit")
  field(SNAM, "doseStatus")
  field(BRSV, "INVALID")

# Inputs:
#  VALA -  dose rate
#  VALB -  dose rate severity
#  VALC -  event number of pvs to be processed
#  VALD -  debug flag
#  VALE -  max consecutive neg. dose rates (if hpi is online)
#  VALF -  hpi online status
#  VALG -  number of consecutive neg. dose rates below dose low limit
#  VALH -  dose rate lower limit allowable.
  field(INPA, "$(BL)GammaDoseRate")
  field(INPB, "$(BL)GammaDoseRate.SEVR")
  field(INPC, "$(BL)GammaDoseRateEvent")
  field(INPD, "$(BL)GammaDebug")
  field(INPE, "$(BL)GammaNegErrLimit")
  field(INPF, "$(BL)GammaCommLink")
  field(INPG, "$(BL)GammaNegCnt")
  field(INPH, "$(BL)GammaDoseLowLimit")
  field(FTA,  "DOUBLE")
  field(FTB,  "SHORT")
  field(FTC,  "LONG")
  field(FTD,  "SHORT")
  field(FTE,  "LONG")
  field(FTF,  "SHORT")
  field(FTG,  "LONG")
  field(FTH,  "DOUBLE")
# Outputs:
#  VALA = time when last data was received (timestamp)
#  VALB = time when last data was received (string)
#  VALC = time when last data was received (nsec)
#  VALD = time diff in seconds between current and last data read (nsec)
#  VALE = count consecutive negative dose rates
#  VALF = negative dose rate error (0=no error, 1=error)
#  VALG = gamma dose over-range status
  field(FTVA,  "ULONG")
  field(FTVB,  "STRING")
  field(FTVC,  "DOUBLE")
  field(FTVD,  "DOUBLE")
  field(FTVE,  "LONG")
  field(FTVF,  "SHORT")
  field(FTVG,  "SHORT")
  field(OUTC,  "$(BL)GammaDoseRateTOD PP")
  field(OUTD,  "$(BL)GammaDoseRateDeltaSec PP")
  field(OUTE,  "$(BL)GammaNegCnt PP")
  field(OUTF,  "$(BL)GammaNegStatus PP")
  field(OUTG,  "$(BL)GammaOverRangeStatus PP")
  field(NOVA,  "2")
#  info(autosaveFields_pass0,"VALA,VALB,VALC,VALD")
}

record(stringout, "$(BL)GammaDoseRateTOD") {
  field(DESC, "Last does rate reading")
#  field(DOL,  "$(BL)GammaDoesInfo.VALC")
}

record(ao, "$(BL)GammaDoseRateDeltaSec") {
  field(DESC, "Last does rate reading")
#  field(DOL, "$(BL)GammaDoesInfo.VALD")
  field(EGU,  "sec")
  field(PREC, "2")
}

record(longout, "$(BL)GammaNegCnt") {
  field(DESC, "Num consecutive neg. dose rates")
  field(ASG,  "Internal")
#  field(DOL,  "$(BL)GammaDoseInfo.VALE")
  field(HIGH,  "60")
  field(HSV,   "MAJOR")
  field(PINI, "YES")
  info(autosaveFields,"VAL")
}

# Negative dose rate for more then a mniute
record(bo, "$(BL)GammaNegStatus") {
  field(DESC, "Negative Dose Rate")
  field(DTYP, "Raw Soft Channel")
# field(DOL,  "$(BL)GammaDoseInfo.VALF")
  field(ZNAM, "Ok")
  field(ONAM, "Error")
  field(OSV,  "MAJOR")
  field(PINI, "YES")
  info(autosaveFields_pass0,"VAL RVAL")
}

record(bo, "$(BL)GammaNegCntReset") {
  field(DESC, "Reset neg. dose cnt")
  field(ZNAM, "None")
  field(ONAM, "Reset")
  field(OUT,  "$(BL)GammaNegCntResetSeq.SELN PP NMS")
}

record(seq, "$(BL)GammaNegCntResetSeq") {
  field(ASG,  "Internal")
  field(SELL, "$(BL)GammaNegCntReset")
  field(SELM, "Mask")
  field(DOL1, "0")
  field(LNK1, "$(BL)GammaNegCnt PP")
}

# Over Range is indicated on hpi display, in the
# the first line in place of the radiation level and
# shows the text "Over 1000 mrem/h". The display
# actually goes slightly over 1000 mrem/h to allow
# veification of full scale response.
# see hpi manual p5, for details.
#
record(bo, "$(BL)GammaOverRangeStatus") {
  field(DESC, "hpi over range failure")
# field(DOL,  "$(BL)GammaDoseInfo.VALG")
  field(ZNAM, "Ok")
  field(ONAM, "Failure")
  field(OSV,  "MAJOR")
  field(PINI, "YES")
  info(autosaveFields_pass0, "VAL RVAL")
}

record(calcout, "$(BL)GammaDoseSec") {
  field(DESC, "gamma dose for last second")
  field(ASG,  "Internal")
  field(CALC, "B<0?0:(B/3600.)*C")
  field(INPB, "$(BL)GammaDoseRate NPP MS")
  field(INPC, "$(BL)GammaDoseRateDeltaSec NPP NMS")
  field(EGU,  "mR")
  field(PREC, "2")
  field(OUT,  "$(BL)GammaDailyDoseCalc.B PP NMS")
  field(FLNK, "$(BL)GammaDoseCalc")
}

record(compress, "$(BL)GammaDoseSecHst") 
{
  field(DESC, "Last 30 minutes of dose rates")
  field(INP,  "$(BL)GammaDoseSec")
  field(ALG,  "Circular Buffer")
  field(NSAM, "1800")
  field(EGU,  "mR/hr")
  field(PREC, "4")
}

record(compress, "$(BL)GammaDoseRateHst") 
{
  field(DESC, "Last 30 minutes of dose rates")
  field(INP,  "$(BL)GammaDoseRate")
  field(ALG,  "Circular Buffer")
  field(NSAM, "1800")
  field(EGU,  "mR/hr")
  field(PREC, "4")
}

record(calcout, "$(BL)GammaDoseCalc") 
{
  field(DESC, "gamma total dose")
  field(ASG,  "Internal")
  field(INPA, "$(BL)GammaDose NPP NMS")
  field(INPB, "$(BL)GammaDoseSec NPP MS")
  field(CALC, "C?0:A+B")
# C isset by GammaDoseResetSeq to 1 if total dose should be set to 0.
  field(EGU,  "mR")
  field(PREC, "2")
  field(OUT,  "$(BL)GammaDose PP MS")
}

# We need to have total dose and daily dose
# in pvs that can be save restored in case
# the hpi is frozen after a reboot, and a second
# reboot happens and wipes out the restore file.
record(ao, "$(BL)GammaDose")
{
  field(DESC, "gamma total dose")
  field(EGU,  "mR")
  field(PREC, "2")
  field(PINI, "YES")
  info(autosaveFields_pass0, "VAL")
  field(FLNK, "$(BL)GammaNumIO")
}

record(calcout, "$(BL)GammaDailyDoseCalc") 
{
  field(DESC, "gamma daily dose")
  field(CALC, "C?0:A+B")
  field(INPA, "$(BL)GammaDailyDose NPP NMS")
  field(INPB, "$(BL)GammaDoseSec NPP MS")
# check the alarm clock to see if it's time to reset the daily dose.
# if it's time to reset the daily dose (c=0), then do so.
# otherwise calculate a running total for the day.
  field(INPC, "$(sys):AlarmClock NPP NMS")
  field(EGU,  "mR")
  field(PREC, "2")
  field(OUT,  "$(BL)GammaDailyDose PP MS")
}

record(ao, "$(BL)GammaDailyDose")
{
  field(DESC, "gamma daily dose")
  field(EGU,  "mR")
  field(PREC, "2")
  field(PINI, "YES")
  info(autosaveFields_pass0, "VAL")
}

# Check for missing data
record(longout, "$(BL)GammaNoIOEvent") {
   field(DESC, "Gamma infoeEvent Id")
   field(ASG,  "Internal")
   field(VAL,  "43")
   field(PINI, "YES")
}

record(ao, "$(BL)GammaIOLimit") {
   field(DESC, "Max sec between data")
   field(VAL,  "60")
   field(PINI, "YES")
}

record(longout, "$(BL)GammaIOEvent") {
   field(DESC, "Gamma infoeEvent Id")
   field(ASG,  "Internal")
   field(VAL,  "43")
   field(PINI, "YES")
}

record(aSub, "$(BL)GammaIOCheck") {
   field(DESC, "Check data missing")
   field(ASG,  "Internal")
   field(SCAN, "1 second")
   field(INAM, "myAsubInit")
   field(SNAM, "isDataMissing")
   field(BRSV, "INVALID")
#
#Input:
#  INPA = dose rate time stamp (epicsTimeStamp) 
#  INPB = max seconds allowed between data reads, before data is considered missing
#  INPC = event number to post timestamp pv as string
#  INPD = debug flag (0=0ff,1=On)
   field(INPA, "$(BL)GammaDoseInfo.VALA")
   field(INPB, "$(BL)GammaIOLimit")
   field(INPC, "$(BL)GammaIOEvent")
   field(FTB,  "DOUBLE")
   field(FTA,  "ULONG")
   field(FTC,  "LONG")
   field(FTD,  "SHORT") 
   field(NOA,  "2")
#
# Outputs:
#  VALA = time diff between last data and current time (nsec)
#  VALB = data missing flag (0=data ok, 1=data missing)
#  VALC = time when data went missing (string)
   field(FTVA, "DOUBLE")
   field(FTVB, "USHORT")
   field(FTVC, "STRING")
   field(OUTB, "$(BL)GammaHeartbeat PP")
   field(OUTC, "$(BL)GammaIOLostTOD PP")
#   info(autosaveFields_pass0,"VALA,VALB,VALC")
}

record(bo, "$(BL)GammaHeartbeat") {
  field(DESC, "HPI Heartbeat")
  field(DTYP, "Raw Soft Channel")
# field(DOL,  "$(BL)GammaIOCheck.VALB")
  field(ZNAM, "Scanning")
  field(ONAM, "Frozen")
  field(OSV,  "MAJOR")
  field(PINI, "YES")
  info(autosaveFields_pass0,"VAL RVAL")
}
record(stringout,"$(BL)GammaIOLostTOD") {
  field(DESC, "Date data went missing")
# field(DOL,  "$(BL)GammaIOCheck.VALC")
  field(PINI, "YES")
 info(autosaveFields_pass0,"VAL")
}

record(ao, "$(BL)GammaOffset") {
  field(DESC, "Display zero offset")
  field(DTYP, "Raw Soft Channel")
  field(PINI, "YES")
  field(EGU,  "mR/hr")
  field(PREC, "2")
  field(VAL,  "0.05")
  info(autosaveFields_pass0,"VAL RVAL")
}


# RESET TOTAL Daily Dose

record(bo, "$(BL)GammaDoseReset") {
  field(DESC, "Get BSOIC commission date")
  field(ZNAM, "None")
  field(ONAM, "Reset")
  field(OUT,  "$(BL)GammaDoseResetCheck.A PP NMS")
}

record(calcout, "$(BL)GammaDoseResetCheck") {
  field(DESC, "Get BSOIC commission date")
  field(ASG,  "Internal")
  field(CALC, "A")
  field(OOPT, "When Non-zero")
  field(OUT,  "$(BL)GammaDoseDate.PROC PP")
}

record(stringin, "$(BL)GammaDoseDate") {
  field(DESC, "Ion chamber commission date")
  field(DTYP, "Soft Timestamp")
  field(INP,  "@%m/%d/%Y %H:%M:%S")
  field(VAL,  "None")
  field(FLNK, "$(BL)GammaDoseResetSeq")
}

# Set date ion chamber commissioned,
# and then set the total dose to 0, by
record(seq,   "$(BL)GammaDoseResetSeq") {
  field(ASG,  "Internal")
  field(DOL1, "1")
  field(LNK1, "$(BL)GammaDoseSince.OMSL PP")
  field(DLY2, "0.1")
  field(LNK2, "$(BL)GammaDoseSince.OMSL PP")
# flag total dose reset
  field(DOL3, "1")
  field(LNK3, "$(BL)GammaDoseCalc.C PP")
  field(DLY4, "0.2")
# Don't process, we want the next data read to process the total dose calcuation
  field(LNK5, "$(BL)GammaDoseCalc.C")
}

record(stringout,"$(BL)GammaDoseSince") {
  field(DESC, "Ion chamber comission date")
  field(DOL,  "$(BL)GammaDoseDate")
  field(PINI, "YES")
  info(autosaveFields_pass0,"VAL")
}

