# Technical details

## Snow profile alignment and similarity assessment for aggregating, clustering, and evaluating of snowpack model output for avalanche forecasting

This vignette creates a detailed link between the methods described in the paper

Herla, F., Horton, S., Mair, P., & Haegeli, P. (in review). Snow profile alignment and similarity assessment for aggregating, clustering, and evaluating of snowpack model output for avalanche forecasting.

and this companion R package. While the basic workflow and the use of the high-level functions are described in the vignette Basic workflow, this vignette describes how the (default) workflow of the package can be altered and the how the methods could be improved.

library(sarp.snowprofile.alignment)
#> Loading required package: sarp.snowprofile

## 1. Aligning snow profiles based on Dynamic Time Warping (DTW)

The individual steps of aligning snow profiles—taken from the documentation of the core function dtwSP:

1. (optional) Rescale the profiles to the same height (cf., scaleSnowHeight)
2. Resample the profiles onto the same depth grid. 2 different approaches:
• regular grid with a sampling rate that is provided by the user (recommended, cf., resampleSP).
This approach requires to rescale the profiles.
• irregular grid that includes all layer interfaces within the two profiles (i.e., set resamplingRate = NA) (cf., resampleSPpairs)
3. Compute a weighted local cost matrix from multiple layer characteristics (cf., distMatSP)
4. Match the layers of the profiles with a call to dtw (eponymous R package)
5. Align the profiles by warping the query profile onto the reference profile (cf., warpSP)
6. (optional) If the function has been called with multiple different boundary conditions (global, top-down, or bottom-up alignments), the optimal alignment as determined by simSP will be returned.

### 1.1 Preprocessing of the profiles prior to the alignment

A series of functions exist that manipulate the snow profiles prior to the alignment or the similarity assessment. All these manipulations happen by default, and can be controlled in the arguments to dtwSP.
Rescaling and resampling

• scaleSnowHeight: Scale the total snow height of a profile with a uniform scaling factor (commonly determined from the height of a second profile)
• resampleSP: Resample an individual profile
• reScaleSampleSPx: Both rescale and resample a set of profiles to identical snow heights and onto a regular grid

Reducing the number of layers

• mergeIdentLayers: Merge adjacent layers that have identical properties
• rmZeroThicknessLayers: Remove or reset layers of zero thickness (i.e., these layers originate from warping one profile onto another)

### 1.2 Computing a weighted local cost matrix from multiple layer characteristics

Computing a local cost matrix is fundamental to DTW alignments and is carried out in distMatSP.

1. Assessing the differences of individual snow layers Currently, distance functions are implemented for the layer characteristics grain type, hardness, and deposition date. The distance function for categorical grain types relies on a matrix that stores the distances between different categories. Since the similarity requirements are slighlty different for aligning profiles versus assessing their similarity, two matrices are implemented:

• grainSimilarity_align (Table 1A in the paper)
• grainSimilarity_evaluate (Table 1B in the paper)
• swissSimilarityMatrix (grain type similarity matrix defined by Lehning et al, 2001)
2. Computing a local cost matrix
First, a distance matrix is computed for each included layer characteristic that stores the distances between individual layer combinations. Then these distance matrices are combined into one resulting distance matrix (i.e., local cost matrix) by weighted averaging. Optionally, a preferential layer matching manipulation can be included into the local cost matrix.

In distMatSP all parameters related to the local cost matrix can be controlled, e.g.

• Layer characteristics to include
• Relative averaging weights of the layer characteristics
• Grain type similarity matrix
• Warping window function
• Preferential layer matching (defined by the coefficients in Table 2 in the paper: layerWeightingMat)—currently, implemented solely based on grain type information.

### 1.3 Obtaining the optimal alignment of the snow profiles

Obtaining the optimal alignment of pairs of snow profiles is the core task of this package. All functions and controls from the sections 1.1 and 1.2 above can be modified in the call to the core function dtwSP. Additional controls are, e.g.

• local slope constraint
• boundary conditions: global alignment vs. partial alignment

Partial alignments (i.e., open.end) can be started from the snow surface downwards (top.down) or from the ground upwards (bottom.up). When the function is called to align the profiles with multiple different boundary conditions (global, top-down, bottom-up), the alignment that yields the highest similarity is returned.

While DTW computes the matching between the layers, the actual alignment is carried out with a warping function warpSP. Since that warping is different for bottom-up and top-down alignments, and it is different for the two profiles, warpSP provides solutions for most combinations.