--- source_files: - DTS Viewer/DTS.Viewer.Modules/DTS.Viewer.AddCalculatedChannel/Model/CalculatedChannelCreator.cs generated_at: "2026-04-17T15:59:43.455218+00:00" model: "zai-org/GLM-5-FP8" schema_version: 1 sha256: "0bdba55cc7806845" --- # CalculatedChannelCreator Documentation ## 1. Purpose The `CalculatedChannelCreator` class is a factory responsible for creating derived data channels from existing sensor input channels. It performs mathematical transformations—including 3D IR-TRACC displacement calculations, aggregate operations (SUM, AVE, Resultant, HIC), and binary operations (integration, differentiation, trigonometric functions)—on channel data and persists the results to memory-mapped files. This module exists to support post-processing of crash test data, enabling engineers to synthesize new measurement channels from raw sensor inputs. --- ## 2. Public Interface ### `CreateChannels` (static method) ```csharp public static Test.Module.CalculatedChannel[] CreateChannels( string testId, string folder, Calculation calculation, List inputChannels, string channelName, int startingNumber, List allChannels, int clipLength, out List errorList, int defaultEncoding) ``` **Behavior:** Main entry point for creating calculated channels. Dispatches to appropriate private methods based on the `Calculation` enum value. Returns an array of `Test.Module.CalculatedChannel` objects on success, or `null` if validation fails. Populates `errorList` with validation errors. ### `ValidateChannelName` (static method) ```csharp public static bool ValidateChannelName( string channelName, List inputChannels, List allChannels, out List errorList) ``` **Behavior:** Validates that the channel name is non-empty and unique across both input channels and all channels. Returns `true` if valid; `false` otherwise. If `allChannels` is `null`, validation is bypassed and returns `true`. ### `ThreeDIRTraccType` (public enum) ```csharp public enum ThreeDIRTraccType { Thorax, Abdomen, LowerThorax } ``` **Behavior:** Specifies the anatomical location for 3D IR-TRACC calculations, which affects the constants (`δ` and `D0`) used in the displacement formulas. --- ## 3. Invariants 1. **Channel Number Offset:** All calculated channels use `ChannelNumberCalculationChannelIndicator` (constant value `100000`) as a base offset for their channel numbers. 2. **Sample Rate Compatibility:** For aggregate and 3D IR-TRACC operations, the maximum sample rate among input channels must be an integer multiple of each individual channel's sample rate. Otherwise, `NotSupportedException` is thrown. 3. **3D IR-TRACC Input Count:** `Create3DIRTraccChannels` asserts that exactly 3 input channels are provided. 4. **Aggregate Operation Input Count:** `CreateChannelsAggregateOperation` asserts that at least 2 input channels are provided (assertion message says "at least 1" but checks `1 < inputChannels.Count`). 5. **File Overwrite Safety:** `CreatePersistentInformationObject` deletes any existing file at the target filepath before creating a new persistent channel, using `GC.Collect()` followed by `FileUtils.DeleteFileOrMove`. 6. **Engineering Units Padding:** When persisting engineering units, the string is padded to even length for word-alignment if its length is odd. --- ## 4. Dependencies ### This Module Depends On: - `DTS.Common.Enums.Sensors` - For `SensorConstants` (δ and D0 values) - `DTS.Common.Utilities.Logging` - For `APILogger.Log` - `DTS.Common.Utils` - For `FileUtils` - `DTS.Serialization` - For `SliceRaw.File` format handling - `DTS.Slice.Control` - Unclear specific usage (imported but no direct reference visible) - `DTS.Common` - For `Constants.ADC_MIDPOINT`, `ZeroMethodType` - `DTS.Common.Events` - For `PageErrorEvent`, `PageErrorArg` - `DTS.Common.Calculations` - For `HeadInjuryCriterion` - `DTS.Common.Utilities.Math.Nhtsa` - For `Integration`, `Differentiation` - `Prism.Ioc` - For `ContainerLocator` - `Prism.Events` - For `IEventAggregator` - `Test.Module` namespace - For `Channel`, `CalculatedChannel`, `AnalogInputChannel` - `Event.Module` namespace - For `Channel`, `AnalogInputChannel` ### What Depends On This Module: - **Unclear from source alone** - No consumers are shown in this file. --- ## 5. Gotchas ### Critical Bugs Identified: 1. **Copy-Paste Error in `PerformCalculation` (line ~378):** ```csharp var stepRPot1 = Convert.ToInt32(Math.Ceiling(indexAtCurrentTimeIRTracc) - actualIndexAtCurrentTimeIRTracc); ``` Uses `indexAtCurrentTimeIRTracc` instead of `indexAtCurrentTimeRPot1`. This causes incorrect interpolation for the R-Pot1 channel. 2. **Assignment Instead of Subtraction in `PerformCalculation` (line ~403):** ```csharp incrementRPot2 = (valueRPot2AtPoint = rPot2EUData[actualIndexAtCurrentTimeRPot2 - 1]) / rateRPot2; ``` This is an assignment (`=`) inside parentheses, overwriting `valueRPot2AtPoint`, when it should be a subtraction (`-`). 3. **Unused Variable in `PerformCalculationsAggregate` (line ~151):** ```csharp var timeAtIndex = ``` Variable is declared but never assigned or used. This is a syntax error or incomplete implementation. ### Non-Obvious Behaviors: 4. **Debug/Test Data Injection:** The method calls `DiskUtility.ReplaceDataIfNeeded(ref irTraccEUData, "DISPLEU.txt")` and similar for other channels. This appears to be a debug feature that can override channel data from disk files. Production behavior is unclear. 5. **Explicit `GC.Collect()` Call:** In `CreatePersistentInformationObject`, `GC.Collect()` is called before file deletion to handle file locks. This is a heavy operation that may cause performance issues. 6. **ScaleEuData Logic Appears Inverted:** When `max > short.MaxValue`, the scale factor calculation `scaleFactor = max / short.MaxValue` produces a value ≥ 1, which would amplify data that's already too large, potentially causing overflow when casting to `short`. 7. **HIC Unit Conversion:** For HIC calculations, if engineering units are not "g", the code multiplies by `9.80665` to convert from m/s² to g's. This assumes the input is in m/s², which may not always be true. 8. **Commented-Out Code:** There is a commented-out `DeleteChannelFile` method and associated logic for deferred file deletion, suggesting historical changes to file handling behavior.