# Analyze Measures and SPC

Let the `Coolant Temperature` measure run for at least 15 minutes before starting, so there are enough records to analyze.

:::action[Review the Measures analysis screen]

1. Navigate to **Ops → Analysis → Measures**.
2. Select the `Coolant Temperature` measure for Machine 1, choose today, set granularity to **Hour**, and click **Go**.
3. Review the four summary cards: **Avg Measure Value** should sit near 22, and the **Total Samples** card should show about 60 samples per event.
4. In **Measure Value with Rolling Average**, find the slow 30-minute swing you built into the simulated signal.
5. Check the **Measure Value Distribution** histogram. It should be centered on 22.

:::

The two tables at the bottom help you trust the data before you report on it:

- **Outliers (z-score > 2)** lists windows whose value was unusually far from the mean, largest first.
- **Gap Analysis** flags windows with a larger-than-normal gap since the previous record, which usually means collection stopped: the tag was disabled, the gateway restarted, or the source went bad.

:::action[View Measure Records]

1. Copy the following script and execute it in the Script Console.

```python
measureId = system.kanoa.utilities.getFieldValue('measureId', system.kanoa.measures.getMeasures({'measureName': 'Coolant Temperature'}))

end = system.date.now()
start = system.date.addHours(end, -1)
summary = system.kanoa.utilities.convertDatasetToJSON(system.kanoa.measures.getMeasureSummary({'measureId': measureId, 'tStampStart': start, 'tStampEnd': end}))
for row in summary:
    for k,v in row.items():
    	print k,v
```

:::

## SPC Checks

With a Kanoa Quality license, a measure can be checked against statistical process control rules every time a window is written, instead of waiting for someone to look at a chart.

::::action[Enable SPC on the measure]

1. In **Configuration → Quality → Attributes**, create an attribute named `Coolant Temperature`.
2. Click **View Attribute Settings** and, on the **Tolerances** tab, add a tolerance with the asset set to **Machine 1** and the item left blank, so it applies to every item on Machine 1:

   | LSL | LCL | Target | UCL | USL |
   |---|---|---|---|---|
   | 18 | 20 | 22 | 24 | 26 |

3. In **Configuration → Ops → Measures**, link the attribute to the `Coolant Temperature` measure and save.
4. On the measure tag, trigger `spec/refresh` to load the new limits, then confirm the `spec` folder shows them.
5. On the measure tag, set `spcCheckEnabled` to `true`.
6. Push the process out of control: change the `Coolant Temp` expression's base value from `22` to `25` for a few minutes.
7. Watch `outOfControl` turn `true` and `ruleViolations` list the rules that fired, then set the base value back to `22`.

:::info
Out-of-control conditions also appear in the **notifications** widget, so you can put an alert on an operator screen or dashboard for the line.
:::

::::

The rule set (`western_electric`, `nelson`, or `both`) and the chart type (`individuals` for single readings, `xbar` for subgroups) are described in the [Measures reference](../../../kanoa-ops/measures.md#spc-rules). Use `individuals` for a sensor value like this one.

:::check

Copy this script into the Ignition Designer Script Console (**Tools → Script Console**) and run it after the SPC steps. It checks there are enough `Coolant Temperature` records to analyze, the `Coolant Temperature` attribute and its Machine 1 tolerance, the link from the measure, the limits in the measure tag's `spec` folder, that `spcCheckEnabled` is on, that SPC flagged the measure out of control, and that `Coolant Temp` is back near 22. Each check prints PASS or FAIL with what to fix. It doesn't change anything.

<details>
<summary>Show the check script</summary>

::check-script{src="ops/10-measures/2-analyze-measures.py" title="checkLesson-analyzeMeasures.py"}

</details>

:::
