# Create a waveform with the sampling time. This will be the X axis of the
# waveform.
record (aSub, "$(P):FAST_WF_TIME") {
  field(SNAM, "calcTimeArray")
  field(FTA, "LONG")
  field(FTVA, "DOUBLE")
  field(NOA, "2020")
  field(NOVA, "2020")
  field(INPA, "$(P):ClkFreq CP")
}

# These are the records that transform tick counts to time in nanoseconds,
# by using the sampling frequency of the ADCs.

# Giving the frequency in Hz, calculate the time in ns between each sample
record (calc, "$(P):SAMP_TIME") {
  field(DESC, "Time between samples in ns")
  field(INPA, "$(P):ClkFreq CP")
  field(CALC, "1000000000/(2 * A)")
}

# Calculate an unsiged 16 bit array from a signed array
record (aSub, "$(P):FAST_RAW_WF_U16") {
  field(DESC, "Slow raw waveform")
  field(SNAM, "calcRWF16")
  field(INPA, "$(P):FAST_RAW_WF_S16")
  field(NOA, "1024")
  field(NOVA, "1024")
  field(FTA, "SHORT")
  field(FTVA, "SHORT")
  field(SCAN, "1 second") 
}

# Calculate the time correspondent to the number of samples of the pre-edge
# of the weight function
record (calc, "$(P):TIME_PRE_RBV") {
  field(DESC, "Duration of pre-edge in ns")
  field(INPA, "$(P):SAMP_TIME CP")
  field(INPB, "$(P):NumSamp0RBV")
  field(CALC, "A * B")
}

# Calculate the time correspondent to the number of samples of between edges
# of the weight function. It must be at least the same size of the
# pre-edge time because the time counts from the beginning of the pre-edge.
record (calc, "$(P):TIME_MID_RBV") {
  field(DESC, "Duration between edges in ns")
  field(INPA, "$(P):SAMP_TIME CP")
  field(INPB, "$(P):TrigDelay1RBV")
  field(CALC, "B*2 * A")
}

# Calculate the time correspondent to the number of samples of the pos-edge
# of the weight function
record (calc, "$(P):TIME_POS_RBV") {
  field(DESC, "Duration of pos-edge in ns")
  field(INPA, "$(P):SAMP_TIME CP")
  field(INPB, "$(P):NumSamp1RBV")
  field(CALC, "A * B")
}

# Desired time of the weight function pre-edge
record (ao, "$(P):TIME_PRE") {
  field(DESC, "Duration of pre-edge in ns")
  field(DRVH, "724")
  field(HOPR, "724")
  info(autosaveFields, "VAL")
}

# Calculate the number of samples of the pre-edge of the weight function 
# giving the correspondent time and writes it to the firmware. Only even
# numbers are allowed in the FPGA register (that is why we use ((~0)-1)&
record (calcout, "$(P):NumSamp0_CLC") {
  field(DESC, "Number samples of pre-edge")
  field(INPA, "$(P):TIME_PRE CP")
  field(INPB, "$(P):ClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "$(P):NumSamp0 PP")
  field(DOPT, "Use CALC")
}

# Desired time of the weight function between edges
record (ao, "$(P):TIME_MID") {
  field(DESC, "Duration of between edge in ns")
  field(DRVH, "1448")
  field(HOPR, "1448")
  info(autosaveFields, "VAL")
}

# Time between edges is doubled related to the edges. Also, timing zero for
# this time is exactly timing zero for the pre-edge. So, we must not allow
# that the between edges time keeps greater than the pre-edge.
record (calc, "$(P):TIME_MIDC") {
  field(DESC, "Duration of between edge in ns")
  field(INPA, "$(P):TIME_PRE CP")
  field(INPB, "$(P):TIME_MID CP")
  field(CALC, "A<B? B/2 : A/2")
}

# Calculate the number of samples of between edges of the weight function 
# giving the correspondent time and writes it to the firmware. The time
# here is twice the time in pre and post edges. Only even numbers are allowed
# in the FPGA register (that is why we use ((~0)-1)&).
record (calcout, "$(P):TrigDelay1_CLC") {
  field(DESC, "Number samples of between edges")
  field(INPA, "$(P):TIME_MIDC CP")
  field(INPB, "$(P):ClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "$(P):TrigDelay1 PP")
  field(DOPT, "Use CALC")
}

#Desired time of the weight function pos-edge
record (ao, "$(P):TIME_POS") {
  field(DESC, "Duration of pos-edge in ns")
  field(DRVH, "724")
  field(HOPR, "724")
  info(autosaveFields, "VAL")
}

# Calculate the number of samples of the pos-edge of the weight function 
# giving the correspondent time and writes it to the firmware. Only even
# numbers are allowed in the FPGA register (that is why we use ((~0)-1)&).
record (calcout, "$(P):NumSamp1_CLC") {
  field(DESC, "Number samples of pos-edge")
  field(INPA, "$(P):TIME_POS CP")
  field(INPB, "$(P):ClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "$(P):NumSamp1 PP")
  field(DOPT, "Use CALC")
}

# Create a waveform with the sampling time. This will be the X axis of the
# waveform.
record (aSub, "$(P):SampTime") {
  field(SNAM, "calcTimeArray")
  field(FTA, "LONG")
  field(FTVA, "DOUBLE")
  field(NOVA, "1024")
  field(INPA, "$(P):ClkFreq CP")
}

# Weight function calculated from pre edge, edge, and pos edge sample amounts.
record (acalcout, "$(P):SCL_WF_CLC") {
  field(DESC, "Weight function")
  field(CALC, "ARR(1){0,A-1} + ARR(-1){B*2,B*2+C-1}")
  field(NELM, "1024")
  field(INPA, "$(P):NumSamp0 CP")
  field(INPB, "$(P):TrigDelay1 CP")
  field(INPC, "$(P):NumSamp1 CP")
}

# Transform an acalcout AVAL info into an waveform VAL, just to maintain
# compatibility with previous systems.
record (waveform, "$(P):SCL_WF") {
  field(DESC, "Weight function")
  field(INP, "$(P):SCL_WF_CLC.AVAL CP")
  field(NELM, "1024")
  field(FTVL, "SHORT")
}

# The records below scales the +1 | 0 | -1 from the Weight Function to
# max(Raw Waveform) | (max(Raw Waveform) + min(Raw Waveform) / 2) | min(Raw Waveform)
# in order to plot raw + weight on the same chart using the same scale.

# Need to use 2 acalcouts to get MAX and MIN from the waveform because the CALC
# field is too small (36 characters)
record (acalcout, "$(P):FAST_RAW_WF_MAX") {
  field(DESC, "Max value of the raw waveform")
  field(CALC, "AMAX(AA)")
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
  field(DOPT, "Use CALC")
  field(NELM, "1024")
}

record (acalcout, "$(P):FAST_RAW_WF_MIN") {
  field(DESC, "Min value of the raw waveform")
  field(CALC, "AMIN(AA)")
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
  field(DOPT, "Use CALC")
  field(NELM, "1024")
}

# Change the top of the visual weight function only if it is greater than 35%
# of the last calculated top value.
record (acalcout, "$(P):WF_MAX") {
  field(DESC, "Smooth function for the top")
  field(CALC, "ABS(A-C)>ABS(A-B)*0.35?A:C")
  field(INPA, "$(P):FAST_RAW_WF_MAX CP")
  field(INPB, "$(P):FAST_RAW_WF_MIN CP")
  field(OUT, "$(P):WF_MAX.C")
  field(DOPT, "Use CALC")
  field(NELM, "1024")
}

# Change the bottom of the visual weight function only if it is greater than 35%
# of the last calculated bottom value.
record (acalcout, "$(P):WF_MIN") {
  field(DESC, "Smooth function for the bottom")
  field(CALC, "ABS(B-C)>ABS(A-B)*0.35?B:C")
  field(INPA, "$(P):FAST_RAW_WF_MAX CP")
  field(INPB, "$(P):FAST_RAW_WF_MIN CP")
  field(OUT, "$(P):WF_MIN.C")
  field(DOPT, "Use CALC")
  field(NELM, "1024")
}

# Line before the edge (corresponding to +1 of the Weight Function)
record (acalcout, "$(P):SCL_VWF_PRE") {
  field(DESC, "Equivalent to +1 in Weight")
  # Fills the array with the maximum of the raw waveform until the edge.
  # All other elements remain zero.
  field(CALC, "ARR(E){0,A-1}")
  field(NELM, "1024")
  # Number of samples pre-edge
  field(INPA, "$(P):NumSamp0 CP")
  field(INPE, "$(P):WF_MAX CP")
  # Raw waveform 16 bits.
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
}

# Line after the edge (corresponding to -1 of the Weight Function)
record (acalcout, "$(P):SCL_VWF_POS") {
  field(DESC, "Equivalent to -1 in Weight")
  # Fills the array with the minimum of the raw waveform after the edge.
  # All other elements remain zero.
  field(CALC, "ARR(D){B*2,B*2+C-1}")
  field(NELM, "1024")
  # Number of samples pre-edge
  field(INPA, "$(P):NumSamp0 CP")
  # Number of samples of the edge
  field(INPB, "$(P):TrigDelay1 CP")
  # Number of samples pos-edge
  field(INPC, "$(P):NumSamp1 CP")
  field(INPD, "$(P):WF_MIN CP")
  # Raw waveform 16 bits.
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
}

# Line of the edge (corresponding to 0 of the Weight Function)
record (acalcout, "$(P):SCL_VWF_MID") {
  field(DESC, "Equivalent to 0 in Weight")
  # Fills the array with the average of the maximum and minimum raw waveform
  # and places it between the pre and the pos-edge curves.
  # All other elements remain zero.
  field(CALC, "ARR((D+E)/2){A,B*2-1}")
  field(NELM, "1024")
  # Number of samples pre-edge
  field(INPA, "$(P):NumSamp0 CP")
  # Number of samples of the edge
  field(INPB, "$(P):TrigDelay1 CP")
  field(INPD, "$(P):WF_MAX CP")
  field(INPE, "$(P):WF_MIN CP")
  # Raw waveform 16 bits.
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
}

# Last part of the weight function, after the pos-edge.
record (acalcout, "$(P):SCL_VWF_FINAL") {
  field(DESC, "Final portion, after pos-edge")
  # Obtain the same value as used for the edge, placing it at the end of the
  # curve.
  field(CALC, "ARR((D+E)/2){B*2+C,-1}")
  field(NELM, "1024")
  # Number of samples pre-edge
  field(INPA, "$(P):NumSamp0 CP")
  # Number of samples of the edge
  field(INPB, "$(P):TrigDelay1 CP")
  # Number of samples pos-edge
  field(INPC, "$(P):NumSamp1 CP")
  field(INPD, "$(P):WF_MAX CP")
  field(INPE, "$(P):WF_MIN CP")
  # Raw waveform 16 bits.
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
}

# This is the weight function multiplied by the raw min and max. The only
# purpose is to fit the curve on the same chart as the raw data using the
# same scale in the y-axis.
record (acalcout, "$(P):SCL_VWF") {
  field(DESC, "Weight scaled for best plot")
  field(CALC, "AA+BB+CC+DD")
  field(NELM, "1024")
  field(INAA, "$(P):SCL_VWF_PRE.AVAL CP")
  field(INBB, "$(P):SCL_VWF_POS.AVAL CP")
  field(INCC, "$(P):SCL_VWF_MID.AVAL CP")
  field(INDD, "$(P):SCL_VWF_FINAL.AVAL CP")
}

# Calculated waveforms when multiplying raw for weight
record (acalcout, "$(P):S_P_WF_CLC") {
  field(DESC, "Raw times weight")
  field(INAA, "$(P):FAST_RAW_WF_U16.VALA CP")
  field(INBB, "$(P):SCL_WF_CLC.AVAL CP")
  field(NELM, "1024")
  field(CALC, "AA * BB")
}

# Transform an acalcout AVAL info into an waveform VAL, just to maintain
# compatibility with previous systems.
record (waveform, "$(P):S_P_WF") {
  field(DESC, "Raw times weight")
  field(INP, "$(P):S_P_WF_CLC.AVAL CP")
  field(NELM, "1024")
  field(FTVL, "SHORT")
}
