<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />

<xsl:template match="/">
    <html>
    <head>
        <title>Fault detected on <xsl:value-of select="/EventDetail/Line/Description" /> (<xsl:value-of select="/EventDetail/Line/AssetKey" />)</title>
        
        <style>
            th, td {
                border-spacing: 0;
                border-collapse: collapse;
                padding-left: 5px;
                padding-right: 5px
            }
            
            .fault-details {
                margin-left: 1cm
            }
        
            .fault-header {
                font-size: 120%;
                font-weight: bold;
                text-decoration: underline
            }

            table {
                border-spacing: 0;
                border-collapse: collapse
            }
        
            table.left tr th, table.left tr td {
                border: 1px solid black
            }
            
            table.center tr th, table.center tr td {
                border: 1px solid black;
                text-align: center
            }
        </style>
    </head>
    <body>
        <xsl:for-each select="/EventDetail/Faults/Fault">
            <p><span class="fault-header">Fault <xsl:value-of select="@num" /> - <format type="System.DateTime" spec="yyyy-MM-dd HH:mm:ss.fffffff"><xsl:value-of select="SummaryData[1]/Inception" /></format></span></p>
            
            <br />
            
            <div class="fault-details">
                <xsl:variable name="deDistance" select="SummaryData/DoubleEndedDistance" />
                
                <table class="left">
                    <tr>
                        <td style="border: 0; text-align: right">Station:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="StationName" /> (<xsl:value-of select="StationKey" />)</td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">DFR:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="MeterKey" /> (<xsl:value-of select="MeterMake" />)</td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">File:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="ShortFileName" /></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Breaker:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="BreakerName" /></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Fault Type:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="FaultType" /></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Inception Time:</td>
                        <xsl:for-each select="SummaryData">
                            <td><format type="System.DateTime" spec="HH:mm:ss.fffffff"><xsl:value-of select="Inception" /></format></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Fault Duration:</td>
                        <xsl:for-each select="SummaryData">
                            <td><format type="System.Double" spec="0.000"><xsl:value-of select="DurationMilliseconds" /></format> msec (<format type="System.Double" spec="0.00"><xsl:value-of select="DurationCycles" /></format> cycles)</td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Fault Current:</td>
                        <xsl:for-each select="SummaryData">
                            <td><format type="System.Double" spec="0.0"><xsl:value-of select="FaultCurrent" /></format> Amps (RMS)</td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Distance Method:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="Algorithm" /></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Single-ended Distance:</td>
                        <xsl:for-each select="SummaryData">
                            <td>Structure #
                                <structure>
                                    <xsl:attribute name="queryResultType">htmlcsv</xsl:attribute>
                                    <xsl:attribute name="returnElementType">span</xsl:attribute>
                                    <xsl:attribute name="returnFields">StrNumber</xsl:attribute>
                                    <xsl:attribute name="returnFieldNames">Structure</xsl:attribute>
                                    <xsl:attribute name="userName">tvapqpc</xsl:attribute>
                                    <xsl:attribute name="password">pqr0cksC</xsl:attribute>
                                    <xsl:attribute name="domain">TVA</xsl:attribute>
                                    <xsl:attribute name="authenticationType">NTLM</xsl:attribute>
                                    <xsl:attribute name="makeGoogleMapsLink">true</xsl:attribute>
                                    http://opsptpsnet.cha.tva.gov:8025/TLI/StructureCrawler/FaultFinder.asp?Station=<xsl:value-of select="StationKey" />&amp;Line=<xsl:value-of select="LineKey" />&amp;Mileage=<format type="System.Double" spec="0.000"><xsl:value-of select="SingleEndedDistance" /></format>
                                </structure> (<format type="System.Double" spec="0.000"><xsl:value-of select="SingleEndedDistance" /></format> miles from <xsl:value-of select="StationName" />)
                            </td>
                        </xsl:for-each>
                    </tr>
                    <xsl:if test="count($deDistance) &gt; 0">
                        <tr>
                            <td style="border: 0; text-align: right">Double-ended Distance:</td>
                            <xsl:for-each select="SummaryData">
                                <td> Structure #
                                    <xsl:if test="DoubleEndedDistance">
                                       <structure>
                                         <xsl:attribute name="queryResultType">htmlcsv</xsl:attribute>
                                         <xsl:attribute name="returnElementType">span</xsl:attribute>
                                         <xsl:attribute name="returnFields">StrNumber</xsl:attribute>
                                         <xsl:attribute name="returnFieldNames">Structure</xsl:attribute>
                                         <xsl:attribute name="userName">tvapqpc</xsl:attribute>
                                         <xsl:attribute name="password">pqr0cksC</xsl:attribute>
                                         <xsl:attribute name="domain">TVA</xsl:attribute>
                                         <xsl:attribute name="authenticationType">NTLM</xsl:attribute>
                                         <xsl:attribute name="makeGoogleMapsLink">true</xsl:attribute>
                                    http://opsptpsnet.cha.tva.gov:8025/TLI/StructureCrawler/FaultFinder.asp?Station=<xsl:value-of select="StationKey" />&amp;Line=<xsl:value-of select="LineKey" />&amp;Mileage=<format type="System.Double" spec="0.000"><xsl:value-of select="DoubleEndedDistance" /></format>
                                </structure> (<format type="System.Double" spec="0.000"><xsl:value-of select="DoubleEndedDistance" /></format> miles from <xsl:value-of select="StationName" />)
                                    </xsl:if>
                                </td>
                            </xsl:for-each>
                        </tr>
                        <tr>
                            <td style="border: 0; text-align: right">Double-ended Angle:</td>
                            <xsl:for-each select="SummaryData">
                                <td>
                                    <xsl:if test="DoubleEndedDistance">
                                        <format type="System.Double" spec="0.000"><xsl:value-of select="DoubleEndedAngle" /></format>&amp;deg;
                                    </xsl:if>
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:if>
                    <tr>
                        <td style="border: 0; text-align: right">Click for fault report:</td>
                        <xsl:for-each select="SummaryData">
                            <td><a><xsl:attribute name="href">http://chapwpqweb/faultlocation1.0/details.asp?id=<xsl:value-of select="EventID" /></xsl:attribute><xsl:value-of select="EventID" /></a></td>
                        </xsl:for-each>
                    </tr>
                </table>
                
                <br />
                
                <table class="left">
                    <tr>
                        <th style="border: 0"></th>
                        <th colspan="2">Probability of Fault Cause</th>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">DFR:</td>
                        <xsl:for-each select="SummaryData">
                            <td><xsl:value-of select="MeterKey" /> (<xsl:value-of select="MeterMake" />)</td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Tree:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="not(TreeFaultResistance)">
                                        Undetermined
                                    </xsl:when>
                                    <xsl:when test="TreeFaultResistance > 20">
                                        <span class="high-probability">High (Rf = <format type="System.Double" spec="0.00"><xsl:value-of select="TreeFaultResistance" /></format>)</span>
                                    </xsl:when>
                                    <xsl:when test="TreeFaultResistance > 10">
                                        Medium (Rf = <format type="System.Double" spec="0.00"><xsl:value-of select="TreeFaultResistance" /></format>)
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Low (Rf = <format type="System.Double" spec="0.00"><xsl:value-of select="TreeFaultResistance" /></format>)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Lightning:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="not(LightningMilliseconds)">
                                        No lightning found
                                    </xsl:when>
                                    <xsl:when test="not(LightningMilliseconds > 2)">
                                        <span class="high-probability">Confirmed at fault inception (<format type="System.Double" spec="0.00"><xsl:value-of select="LightningMilliseconds" /></format> ms)</span>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Detected, but not at fault inception (<format type="System.Double" spec="0.00"><xsl:value-of select="LightningMilliseconds" /></format> ms)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Insulation Flashover:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="not(InceptionDistanceFromPeak)">
                                        Undetermined
                                    </xsl:when>
                                    <xsl:when test="not(InceptionDistanceFromPeak > 15)">
                                        <span class="high-probability">High (<format type="System.Double" spec="0.00"><xsl:value-of select="InceptionDistanceFromPeak" /></format>&amp;deg; from peak)</span>
                                    </xsl:when>
                                    <xsl:when test="not(InceptionDistanceFromPeak > 30)">
                                        Medium (<format type="System.Double" spec="0.00"><xsl:value-of select="InceptionDistanceFromPeak" /></format>&amp;deg; from peak)
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Low (<format type="System.Double" spec="0.00"><xsl:value-of select="InceptionDistanceFromPeak" /></format>&amp;deg; from peak)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Arrester Failure:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="not(PrefaultThirdHarmonic)">
                                        Undetermined
                                    </xsl:when>
                                    <xsl:when test="PrefaultThirdHarmonic > 0.3">
                                        <span class="high-probability">High (<format type="System.Double" spec="0.00"><xsl:value-of select="PrefaultThirdHarmonic" /></format>)</span>
                                    </xsl:when>
                                    <xsl:when test="PrefaultThirdHarmonic > 0.2">
                                        Medium (<format type="System.Double" spec="0.00"><xsl:value-of select="PrefaultThirdHarmonic" /></format>)
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Low (<format type="System.Double" spec="0.00"><xsl:value-of select="PrefaultThirdHarmonic" /></format>)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Conductor Slap / Debris Midspan:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="contains(FaultType, 'N')">
                                        No (applies only to multi-phase faults)
                                    </xsl:when>
                                    <xsl:when test="not(GroundCurrentRatio)">
                                        Undetermined
                                    </xsl:when>
                                    <xsl:when test="not(GroundCurrentRatio > 0.1)">
                                        <span class="high-probability">High (<format type="System.Double" spec="0.00"><xsl:value-of select="GroundCurrentRatio" /></format>)</span>
                                    </xsl:when>
                                    <xsl:when test="not(GroundCurrentRatio > 0.5)">
                                        Medium (<format type="System.Double" spec="0.00"><xsl:value-of select="GroundCurrentRatio" /></format>)
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Low (<format type="System.Double" spec="0.00"><xsl:value-of select="GroundCurrentRatio" /></format>)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <td style="border: 0; text-align: right">Broken Conductor:</td>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <xsl:choose>
                                    <xsl:when test="not(LowPrefaultCurrentRatio)">
                                        Undetermined
                                    </xsl:when>
                                    <xsl:when test="not(LowPrefaultCurrentRatio > 0.1)">
                                        <span class="high-probability">High (<format type="System.Double" spec="0.00"><xsl:value-of select="LowPrefaultCurrentRatio" /></format>)</span>
                                    </xsl:when>
                                    <xsl:when test="not(LowPrefaultCurrentRatio > 0.5)">
                                        Medium (<format type="System.Double" spec="0.00"><xsl:value-of select="LowPrefaultCurrentRatio" /></format>)
                                    </xsl:when>
                                    <xsl:otherwise>
                                        Low (<format type="System.Double" spec="0.00"><xsl:value-of select="LowPrefaultCurrentRatio" /></format>)
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </xsl:for-each>
                    </tr>
                </table>
                
                <br />
                
                <table class="center">
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td><span style="font-weight: bold"><xsl:value-of select="MeterKey" /> - Voltage</span></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <chart>
                                    <xsl:attribute name="eventID"><xsl:value-of select="EventID" /></xsl:attribute>
                                    <xsl:attribute name="faultID"><xsl:value-of select="FaultID" /></xsl:attribute>
                                    <xsl:attribute name="yAxisTitle">Voltage (volts)</xsl:attribute>
                                    <xsl:attribute name="prefaultCycles">2</xsl:attribute>
                                    <xsl:attribute name="postfaultCycles">1</xsl:attribute>
                                    <xsl:attribute name="width">640</xsl:attribute>
                                    <xsl:attribute name="height">350</xsl:attribute>
                                    <xsl:attribute name="highlightCalculation">cycle</xsl:attribute>
                                    <series key="VA">VAN</series>
                                    <series key="VB">VBN</series>
                                    <series key="VC">VCN</series>
                                </chart>
                            </td>
                        </xsl:for-each>
                    </tr>
                </table>
                
                <br />
                
                <table class="center">
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td><span style="font-weight: bold"><xsl:value-of select="MeterKey" /> - Current</span></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <chart>
                                    <xsl:attribute name="eventID"><xsl:value-of select="EventID" /></xsl:attribute>
                                    <xsl:attribute name="faultID"><xsl:value-of select="FaultID" /></xsl:attribute>
                                    <xsl:attribute name="yAxisTitle">Current (Amps)</xsl:attribute>
                                    <xsl:attribute name="prefaultCycles">2</xsl:attribute>
                                    <xsl:attribute name="postfaultCycles">1</xsl:attribute>
                                    <xsl:attribute name="width">640</xsl:attribute>
                                    <xsl:attribute name="height">350</xsl:attribute>
                                    <xsl:attribute name="highlightCalculation">cycle</xsl:attribute>
                                    <series key="IA">IAN</series>
                                    <series key="IB">IBN</series>
                                    <series key="IC">ICN</series>
                                </chart>
                            </td>
                        </xsl:for-each>
                    </tr>
                </table>
                
                <br />
                
                <table class="center">
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td><span style="font-weight: bold"><xsl:value-of select="MeterKey" /> - Fault Distance</span></td>
                        </xsl:for-each>
                    </tr>
                    <tr>
                        <xsl:for-each select="SummaryData">
                            <td>
                                <chart>
                                    <xsl:attribute name="eventID"><xsl:value-of select="EventID" /></xsl:attribute>
                                    <xsl:attribute name="faultID"><xsl:value-of select="FaultID" /></xsl:attribute>
                                    <xsl:attribute name="yAxisTitle">Fault Distance (miles)</xsl:attribute>
                                    <xsl:attribute name="prefaultCycles">2</xsl:attribute>
                                    <xsl:attribute name="postfaultCycles">1</xsl:attribute>
                                    <xsl:attribute name="width">640</xsl:attribute>
                                    <xsl:attribute name="height">350</xsl:attribute>
                                    <xsl:attribute name="highlightCalculation">index</xsl:attribute>
                                    <xsl:attribute name="yAxisMinimum">0</xsl:attribute>
                                    <xsl:attribute name="yAxisMaximum"><xsl:value-of select="/EventDetail/Line/Length" /></xsl:attribute>
                                    <series key="Simple">Simple</series>
                                    <series key="Reactance">Reactance</series>
                                    <series key="Takagi">Takagi</series>
                                    <series key="ModifiedTakagi">Modified Takagi</series>
                                    <series key="Novosel">Novosel</series>
                                    <series key="DoubleEnded">Double-ended</series>
                                </chart>
                            </td>
                        </xsl:for-each>
                    </tr>
                </table>
            </div>
            
            <br />
        </xsl:for-each>

        <hr />
        
        <table class="center" style="width: 600px">
            <tr>
                <th style="border: 0; border-bottom: 1px solid black; border-right: 1px solid black; text-align: center">Line Parameters:</th>
                <th>Value:</th>
                <th>Per Mile:</th>
            </tr>
            <tr>
                <th>Length (Mi)</th>
                <td><pre><format type="System.Double" spec="0.##"><xsl:value-of select="/EventDetail/Line/Length" /></format></pre></td>
                <td><pre>1.0</pre></td>
            </tr>
            <tr>
                <th>
                    Pos-Seq Imp<br />
                    Z1 (Ohm)<br />
                    (LLL,LLLG,LL,LLG)
                </th>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/Z1" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/A1" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/R1" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/X1" /></format></pre>
                </td>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/Z1 div /EventDetail/Line/Length" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/A1" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/R1 div /EventDetail/Line/Length" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/Z1 div /EventDetail/Line/Length" /></format></pre>
                </td>
            </tr>
            <tr>
                <th>
                    Zero-Seq Imp<br />
                    Z0 (Ohm)
                </th>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/Z0" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/A0" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/R0" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/X0" /></format></pre>
                </td>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/Z0 div /EventDetail/Line/Length" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/A0" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/R0 div /EventDetail/Line/Length" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/X0 div /EventDetail/Line/Length" /></format></pre>
                </td>
            </tr>
            <tr>
                <th>
                    Loop Imp<br />
                    ZS (Ohm)<br />
                    (LG)
                </th>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/ZS" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/AS" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/RS" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/XS" /></format></pre>
                </td>
                <td>
                    <pre><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/ZS div /EventDetail/Line/Length" /></format>&amp;ang;<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/AS" /></format>&amp;deg;<br /><format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/RS div /EventDetail/Line/Length" /></format>+j<format type="System.Double" spec="0.####"><xsl:value-of select="/EventDetail/Line/XS div /EventDetail/Line/Length" /></format></pre>
                </td>
            </tr>
        </table>
        
        <xsl:if test="count(/EventDetail/EDNA/Point) &gt; 0">
            <p>
                <span style="font-weight: bold">EDNA Configuration:</span><br />
                
                <xsl:for-each select="/EventDetail/EDNA/Point">
                    <xsl:value-of select="." /><br />
                </xsl:for-each>
            </p>
        </xsl:if>
        
        <hr />

        <p style="font-size: .8em">
            To manage your automated fault notification subscription, or to unsubscribe, click on this <a><xsl:attribute name="href"><xsl:value-of select="/EventDetail/PQDashboard" />/Email/UpdateSettings</xsl:attribute>link</a>.
        </p>
    </body>
    </html>
</xsl:template>

</xsl:stylesheet>