<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:odpx="http://www.gomoos.org/schema/odpx/1"
    exclude-result-prefixes="xd"
    version="1.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Apr 8, 2011</xd:p>
            <xd:p><xd:b>Author:</xd:b> Ian Ogilvie: GMRI</xd:p>
            <xd:p></xd:p>
        </xd:desc>
    </xd:doc>
    <xsl:output method="xml"/>
    <xsl:template match="odpx:ODPX">
        <xsl:variable name="orgid">
            <xsl:value-of select="odpx:Organization/odpx:OrganizationDescription/odpx:OrganizationDescriptionAllElements/odpx:OrganizationIdentifier"/>
        </xsl:variable><xsl:text>
            </xsl:text>
        <buoys><xsl:text>
            </xsl:text>
            <xsl:for-each select="odpx:Organization/odpx:Activity">
                <xsl:variable name="buoyid">
                    <xsl:value-of select="odpx:ActivityDescription/odpx:ActivityDescriptionAllElements/odpx:ActivityIdentifier"/>
                </xsl:variable>
                <!-- Latutude from the Activity Location     -->
                <xsl:variable name="activitylat">
                    <xsl:call-template name="lat_parse">
                        <xsl:with-param name="activity_location">
                            <xsl:value-of select="odpx:ActivityLocation/odpx:where/gml:Point/gml:pos" xmlns:gml="http://www.opengis.net/gml"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <!-- Longitude from the Activity Location     -->
                <xsl:variable name="acitivitylon">
                    <xsl:call-template name="lon_parse">
                        <xsl:with-param name="activity_location">
                            <xsl:value-of select="odpx:ActivityLocation/odpx:where/gml:Point/gml:pos" xmlns:gml="http://www.opengis.net/gml"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <!-- Latutude from the SWE Data Location     -->
                <xsl:variable name="swelatitude">
                    <xsl:call-template name="latitude_parse">
                        <xsl:with-param name="swe_string">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <!-- Longitude from the SWE Data Location     -->
                <xsl:variable name="swelongitude">
                    <xsl:call-template name="longitude_parse">
                        <xsl:with-param name="swe_string">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <!-- Last row from SWE Data  -->
                <xsl:variable name="swelastrow">
                    <xsl:call-template name="lastswe_parse">
                        <xsl:with-param name="swe_string">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <!-- any row from SWE Data  Not used and hardcoded to get row 2? -->
                <xsl:variable name="sweanyrow">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                                <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                            
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(2)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="'&#xA;'" />
                    </xsl:call-template>
                </xsl:variable>
                <!-- Last update from SWE Data  -->
                <xsl:variable name="swelastupdate">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                            <xsl:value-of select="$swelastrow"/>
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(6)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:variable>
                <!-- Notes from SWE Data  -->
                <xsl:variable name="notes">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                            <xsl:value-of select="$swelastrow"/>
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(17)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="status">
                    <xsl:if test="contains($notes,'D')">Online</xsl:if>
                    <xsl:if test="contains($notes,'D') != true()">Inactive</xsl:if>
                </xsl:variable>
                <!-- Alert Expires SWE Data  -->
                <xsl:variable name="alertexpires">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                            <xsl:value-of select="$swelastrow"/>
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(3)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:variable>
                <!-- Last Detect SWE Data  -->
                <xsl:variable name="lastdetect">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                            <xsl:value-of select="$swelastrow"/>
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(5)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:variable>
                <!-- Number of detects last 24 hours SWE Data  -->
                <xsl:variable name="ndetects24hours">
                    <xsl:call-template name="columnat">
                        <xsl:with-param name="rows">
                            <xsl:value-of select="$swelastrow"/>
                        </xsl:with-param>
                        <xsl:with-param name="start_index">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                        <xsl:with-param name="end_index">
                            <xsl:value-of select="number(13)"/>
                        </xsl:with-param>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="ndets24hours">
                    <xsl:if test="$ndetects24hours = ''">0</xsl:if>
                    <xsl:if test="$ndetects24hours != ''"><xsl:value-of select="$ndetects24hours"/></xsl:if>
                </xsl:variable>
                <xsl:variable name="number_detects_list">
                    <xsl:call-template name="list_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="last_detect_list">
                    <xsl:call-template name="list_last_detect">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="max_number_detects">
                    <xsl:call-template name="get_max_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="odpx:Result/odpx:ResultTimeSeries/odpx:ResultAnyData/swe:DataArray/swe:values" xmlns:swe="http://www.opengis.net/swe/1.0.1"/>
                        </xsl:with-param>
                        <xsl:with-param name="current_max_detects">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="chart_url"> <xsl:text>http://chart.googleapis.com/chart?cht=bvs&amp;chs=250x150&amp;chco=FFFFFF&amp;chf=bg,s,093771&amp;chts=FFFFFF,12&amp;chtt=Number+Detects+Last+24+Hours&amp;chd=t:</xsl:text><xsl:value-of select="$number_detects_list"/><xsl:text>&amp;chxt=x,y,x,y&amp;chxl=0:|</xsl:text><xsl:value-of select="$last_detect_list"/><xsl:text>|1:|0|</xsl:text><xsl:value-of select="$max_number_detects"/><xsl:text>|2:|Hour+of+observation+(UTC)|3:|Detects&amp;chds=0,</xsl:text><xsl:value-of select="$max_number_detects"/><xsl:text>&amp;chm=N,FFFFFF,0,-1,10,1&amp;chxs=0,FFFFFF,12,0,lt|1,FFFFFF,10,1,lt|2,FFFFFF,12,1,lt|3,FFFFFF,12,1,lt&amp;chxp=2,90|3,50</xsl:text>
                </xsl:variable>
                <buoy name="{$buoyid}" org="{$orgid}" desc="Bioacoustic Research Program Listening Buoy" lat="{$swelatitude}" lng="{$swelongitude}" last_upd="{$swelastupdate}" status="{$status}" ><xsl:text>
                </xsl:text>
                    <obs label="Alert Expires" value="{$alertexpires}" data_type="alert_expiration"  chart_url="" /><xsl:text>
                </xsl:text>
                    <obs label="Last Detect" value="{$lastdetect}" data_type="last_detect"  chart_url="" /><xsl:text>
                </xsl:text>
                    <obs label="Number Detects Last 24 Hours" value="{$ndets24hours}" data_type="count"  chart_url="{$chart_url}" /><xsl:text>
                </xsl:text>
                </buoy><xsl:text>
            </xsl:text>
            </xsl:for-each>
        </buoys>
    </xsl:template>
<!-- End of XML output. Helper Templates below   -->
    <!-- 
        Parse a lat lon from the Activity Location   
    -->
    <xsl:template name="lat_parse">
        <xsl:param name="activity_location"/>
        <xsl:variable name="upto_lat">
            <xsl:value-of select="substring-after($activity_location,'&#x9; &#x9; &#x9; &#x9; &#x9; &#x9; &#x9; ')"/>
        </xsl:variable>
        <xsl:value-of select="substring-before($upto_lat,' ')"/>
    </xsl:template>
    <xsl:template name="lon_parse">
        <xsl:param name="activity_location"/>
        <xsl:variable name="upto_lat">
            <xsl:value-of select="substring-after($activity_location,'&#x9; &#x9; &#x9; &#x9; &#x9; &#x9; &#x9; ')"/>
        </xsl:variable>
        <xsl:variable name="lon_andmore">
            <xsl:value-of select="substring-after($upto_lat,' ')"/>
        </xsl:variable>
        <xsl:value-of select="substring-before($lon_andmore,'&#xA;')"/>
    </xsl:template>
    <!-- 
        Parse a lat lon from the SWE Data   
    -->
    <xsl:template name="latitude_parse">
        <xsl:param name="swe_string"/>
        <xsl:value-of select="substring-before($swe_string,',')"/>
    </xsl:template>
    <xsl:template name="longitude_parse">
        <xsl:param name="swe_string"/>
        <xsl:variable name="after_lat">
            <xsl:value-of select="substring-after($swe_string,',')"/>
        </xsl:variable>
        <xsl:value-of select="substring-before($after_lat,',')"/>
    </xsl:template>
    <!-- 
        Parse to get the last line from the SWE Data. 
    -->
    <xsl:template name="lastswe_parse">
        <xsl:param name="swe_string"/>
        <xsl:choose>
            <xsl:when test="contains($swe_string, '&#xA;')">
                <xsl:call-template name="lastswe_parse">
                    <xsl:with-param name="swe_string" select="substring-after($swe_string, '&#xA;')" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$swe_string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- 
        Parse to get the column at the index. 
    -->
    <xsl:template name="columnat">
        <xsl:param name="rows"/>
        <xsl:param name="start_index"/>
        <xsl:param name="end_index"/>
        <xsl:param name="separator"/>
        <xsl:choose>
            <xsl:when test="$start_index &lt; $end_index">
                <xsl:call-template name="columnat">
                    <xsl:with-param name="rows" select="substring-after($rows,$separator)" />
                    <xsl:with-param name="start_index" select="$start_index + 1" />
                    <xsl:with-param name="end_index" select="$end_index" />
                    <xsl:with-param name="separator" select="$separator" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <!--  Last column check. It won't have the separator -->
                <xsl:if test="substring-before($rows,$separator) != ''">
                    <xsl:value-of select="substring-before($rows,$separator)"/>
                </xsl:if>
                <xsl:if test="substring-before($rows,$separator) = ''">
                    <xsl:value-of select="$rows"/>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- Create a comma separated list of the number of detects in this swe value -->
    <xsl:template name="list_number_detects">
        <xsl:param name="swe_values"/>
        <xsl:choose>
            <!-- If we have a crlf then get the number of detects, move to the next row and repeat -->
            <xsl:when test="contains($swe_values, '&#xA;')">
                <xsl:call-template name="first_row_number_detects">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="$swe_values"/>
                    </xsl:with-param>
                    <xsl:with-param name="add_separator">
                        <xsl:value-of select="number(1)"/>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:call-template name="list_number_detects">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="substring-after($swe_values, '&#xA;')"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <!-- No crlf, it's the last row get the number of detects. -->
                <xsl:call-template name="first_row_number_detects">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="$swe_values"/>
                    </xsl:with-param>
                    <xsl:with-param name="add_separator">
                        <xsl:value-of select="number(1)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- 
        return the fisrt number of detects last 24 hours in a set of rows.
        looks for return &#xA; to determine last row which doesn't want a comma
    -->
    <xsl:template name="first_row_number_detects">
        <xsl:param name="swe_values"/>
        <xsl:param name="add_separator"/>
        <xsl:choose>
            <xsl:when test="contains($swe_values, '&#xA;')">
                <xsl:call-template name="columnat">
                    <xsl:with-param name="rows">
                        <xsl:value-of select="substring-before($swe_values, '&#xA;')"/>
                    </xsl:with-param>
                    <xsl:with-param name="start_index">
                        <xsl:value-of select="number(0)"/>
                    </xsl:with-param>
                    <xsl:with-param name="end_index">
                        <xsl:value-of select="number(13)"/>
                    </xsl:with-param>
                    <xsl:with-param name="separator" select="','" />
                </xsl:call-template>
                <xsl:if test="$add_separator = 1">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="columnat">
                    <xsl:with-param name="rows">
                        <xsl:value-of select="$swe_values"/>
                    </xsl:with-param>
                    <xsl:with-param name="start_index">
                        <xsl:value-of select="number(0)"/>
                    </xsl:with-param>
                    <xsl:with-param name="end_index">
                        <xsl:value-of select="number(13)"/>
                    </xsl:with-param>
                    <xsl:with-param name="separator" select="','" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- Create a comma separated list of the last_detect in this swe value -->
    <xsl:template name="list_last_detect">
        <xsl:param name="swe_values"/>
        <xsl:choose>
            <!-- If we have a crlf then get the number of detects, move to the next row and repeat -->
            <xsl:when test="contains($swe_values, '&#xA;')">
                <xsl:call-template name="first_row_last_detect">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="$swe_values"/>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:call-template name="list_last_detect">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="substring-after($swe_values, '&#xA;')"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <!-- No crlf, it's the last row get the number of detects. -->
                <xsl:call-template name="first_row_last_detect">
                    <xsl:with-param name="swe_values">
                        <xsl:value-of select="$swe_values"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- 
        return the fisrt last_detect in a set of rows.
        looks for return &#xA; to determine last row which doesn't want a comma
    -->
    <xsl:template name="first_row_last_detect">
        <xsl:param name="swe_values"/>
        <xsl:choose>
            <xsl:when test="contains($swe_values, '&#xA;')">
                <xsl:call-template name="get_hour">
                    <xsl:with-param name="timestamp">
                        <xsl:call-template name="columnat">
                            <xsl:with-param name="rows">
                                <xsl:value-of select="substring-before($swe_values, '&#xA;')"/>
                            </xsl:with-param>
                            <xsl:with-param name="start_index">
                                <xsl:value-of select="number(0)"/>
                            </xsl:with-param>
                            <xsl:with-param name="end_index">
                                <xsl:value-of select="number(8)"/>
                            </xsl:with-param>
                            <xsl:with-param name="separator" select="','" />
                        </xsl:call-template>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:text>|</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="get_hour">
                    <xsl:with-param name="timestamp">
                        <xsl:call-template name="columnat">
                            <xsl:with-param name="rows">
                                <xsl:value-of select="$swe_values"/>
                            </xsl:with-param>
                            <xsl:with-param name="start_index">
                                <xsl:value-of select="number(0)"/>
                            </xsl:with-param>
                            <xsl:with-param name="end_index">
                                <xsl:value-of select="number(8)"/>
                            </xsl:with-param>
                            <xsl:with-param name="separator" select="','" />
                        </xsl:call-template>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="get_hour">
        <xsl:param name="timestamp"/>
        <xsl:value-of select="substring($timestamp,12,2)"/>
    </xsl:template>
    <!-- Get the maximum number of detects in this swe value -->
    <xsl:template name="get_max_number_detects">
        <xsl:param name="swe_values"/>
        <xsl:param name="current_max_detects"/>
        <xsl:choose>
            <!-- If we have a crlf then get the number of detects, move to the next row and repeat -->
            <xsl:when test="contains($swe_values, '&#xA;')">
                <xsl:variable name="current_detects">
                    <xsl:call-template name="first_row_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="$swe_values"/>
                        </xsl:with-param>
                        <xsl:with-param name="add_separator">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:if test="$current_max_detects &gt; $current_detects" >
                    <xsl:call-template name="get_max_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="substring-after($swe_values, '&#xA;')"/>
                        </xsl:with-param>
                        <xsl:with-param name="current_max_detects">
                            <xsl:value-of select="$current_max_detects"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:if>
                <xsl:if test="$current_max_detects &lt;= $current_detects" >
                    <xsl:call-template name="get_max_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="substring-after($swe_values, '&#xA;')"/>
                        </xsl:with-param>
                        <xsl:with-param name="current_max_detects">
                            <xsl:value-of select="$current_detects"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <!-- No crlf, it's the last row get the number of detects. -->
                <xsl:variable name="current_detects">
                    <xsl:call-template name="first_row_number_detects">
                        <xsl:with-param name="swe_values">
                            <xsl:value-of select="$swe_values"/>
                        </xsl:with-param>
                        <xsl:with-param name="add_separator">
                            <xsl:value-of select="number(0)"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:if test="$current_max_detects &gt; $current_detects" >
                    <xsl:value-of select="$current_max_detects+1"/>
                </xsl:if>
                <xsl:if test="$current_max_detects &lt;= $current_detects" >
                    <xsl:value-of select="$current_detects+1"/>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
