# DraMS Consumables

Post processing softare measures consumable items such as WRP run time, HCV cycles, etc.
How each of the DraMS consummables is tracked.

# WRP

## WRP run-time

Run-time is calculated by measuring from the time the WRP speed exceeds 5400 RPM until it returns below 5400 RPM.

## WRP Cycles

A cycle is an Start/Stop pair. A start is anytime the WRP RPM reaches at leat 5400 RPM. A stop is each time the WRP RPM crosses below 5400 RPM.

# HCV

## HCV Cycles

One cycle of the hCV include an HCV open and an HCV close. An HCV open is defined by completing at least 50 positive raw hall counts by monitoring the mnemominc HCV1_HALL_POSITION_COUNTS. An HCV close is counted when the HCV changes by at leat 100 negative counts.

In the event that the HCV is left open when a TID ends, the algorithm should assume the HCV is in the state the previous TID left it.

# Filaments

DraMS has two filaments, filament A and filament B.

### On Time 

Filament A and Filament B on time are determined by measuring the
amount of time that HFB_FIL_ON_A or HFB_FIL_ON_B are 1.

### Cycles 

Filament A and Filament B cycles are determined by counting the number
of times the filaments transition from off to on. A few notes about the
algorithm:

-   if the digital status packet indicates that the SEB is off, then the
    state of the filaments is considered to be off.
-   a filament is considered to be on if if its on "on bit" is set to 1, and if the FIL_VMON is above 2.5V. Otherwise, it is assumed to be off.
-   if the FIL_VMON is not being sampled, then only the "on bits" are used to determine if the filaments are on.

# Detectors (aka EMs)

### On Time 

EM A and EM B on times are determined by summing the on times for all
scans, and then looking for any time when the EMs were constantly on
outside of scanning.

CTL sequences are examined to determine the on time for individual
scans. In order to avoid double-counting scans, only sum packet scans are
examined when in EI mode, and only science packet scans are examined when
in LDI mode.

Additionally, outside of science scans, the total time that the EMs are
on are summed. This is determined by summing the amount of time that EM
A and EM B are below -1900 V. 

Bradley Tse pointed out a flaw in this algorithm:

> There is a corner case where that may introduce its own problems. If
> the user stops the script after the Sequence was dumped but before
> scanning started (maybe because they noticed one of the parameters was
> incorrect), then that means the SebSequences will be duplicated.
>
> And so EM on time for all scans after that (until the next Sequence
> Dump) will have 2x the amount (or however many times the script was
> stopped before scans occurred)

### Full Cycles and Soft Cycles 

EM Cycles are determined through two passes of the data.

The first pass of EM Cycles are counted from the scan status packets.
The cycles are calculated differently depending on the type of mode the
test is in.

If in LDI mode \[1\], if HKID 222 (SCN_EM1) or HKID 223 (SCN_EM2) is
below the -1900V threshold, then a full cycle is added to the
corresponding detector's count. If in Dark Counts mode \[2\], no full
cycles are added here, as typically a Dark Counts run contains only 1
full cycle total. If in EI mode \[3\], soft counts are added to EM1 if
HKID17 (IS_EMON_A) is greater than 9, and the same procedure for EM2 and
HKID18 (EMON_B).

The timestamps of the scan packets are used to determine gaps in time
when scans are not occurring during the test. A second pass through but
looking specifically in the gaps and the binned HK data is necessary to
avoid missing any full or soft cycles.

Here, HKID47 (SEB: EM_1_DAC0_SET) and HKID48 (SEB: EM_2_DAC1_SET) are
looked through individually. If the HK value is \> -50V, the detector is
OFF. If the HK value is \< -1900 the detector is ON. Full cycles are
added for each occurrence where a detector changes from the OFF state to
the ON state within a gap.

\[1\] - LDI Mode is when Filaments are OFF (\[HKIDs 17/18\] \< 9), and
the RF (HKID 235) is ON (End Amplitude \>= 500A)

\[2\] - Dark Counts Mode is when Filaments are OFF (\[HKIDs 17/18\] \<
9), and the RF (HKID 235) is OFF (End Amplitude \< 500A)

\[3\] - EI Mode is when a Filament is ON (\[HKIDs 17 or 18\] \> 9)

### EM Counts 

EM 1 and EM 2 counts are calculated according to the logic below. If the
most recent DAC value (HKIDs 222 and 223) is zero, then ions are not
counted against the multiplier.

```
   # dummy variable to help when counting ions in science packet (packet 26) scans
   temp_counts = 0 
   #
   for each packet in the tm.drams file: 
       if the packet indicates that filament A or filament B is on (HKIDs 105 and 102):
           set the current scan type to EI
       else:
           set the current scan type to LDI 
       #
       else if the packet type is a scan status packet (type 25): 
          if we're in LDI mode: 
              count temp_counts against whichever EM is on (or both, if they're both on) 
              set temp_counts back to 0
       #
       else if the current mode is LDI mode and the current packet type is a science packet (type 26): 
           temp_counts += packet.total_ion_count() 
       # 
       else if the current mode is EI mode and the current packet type is a sum packet (type 27): 
           count packet.total_ion_count() against whichever EM is on (or both, if they're both on) 
       #
       # special-case code for handling dark count scans: 
       else if the current mode is LDI, and the current packet is a sum packet (type 27), and the packet's RF DAC value (HKID 405) < 50:
           count packet.total_ion_count() against whichver EM is on (or both, if they're both on)
```

# Laser

### Laser Shots 

Laser shots are determined by summing the number of pulses in the LEU.MAX_HKHS packets and LEU.HKHS packets.
Each of these packets is generated for each laser shot. Whether we receive the MAX_HKHS packet or the HKHS packet depends on the LEU setting. But the packets are mutually exclusive. We should never get both packet types for the same laser shot.

# GPS Valves

### GPS Valve Cycles 

GPS valves are the microvalves and GC valves in the manifolds. The high-
conductance valve (HCV) and the multi-function valve (MFV) are documented
in another entry.

Valve cycles are determined by examining the message log and counting
the number of times a valve transitions from closed to opened. When
counting valve cycles for a TID, the state of the valve cycles in the
previous TID is accounted for. The exact regexes used (as of Feb 22)
are:

```
VALVE_OPENED_REGEX = r'.*valve \d opened$'
VALVE_CLOSED_REGEX = r'.*valve \d closed$'
```

# Multifunction Valves

### MFV Cycles 

Counts the number of times the Multifuntion Valve (MFV) 
has been opened. A cycle consists of an open and close. The valve transitions from
open to closed when the MFV temperature transistion above 100C (TBC). It is considered
closed when it transition from about 100C to below 100C (TBC)

### MFV On Time 

This is the total powered time of the MFV. This is determined by
calculating the duration of time when the MFV temperature is above 100C (TBC).