<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="microsummary-generator.xsl" type="text/xsl"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="NOAA METAR Microsummary"
           uri="urn:source:http://web.sigkill.com/noaa-metar-microsummary/">
           <!-- Release Version: $Rev: 1943 $ -->

  <pages>
    <include>http://weather\.noaa\.gov/cgi-bin/mgetmetar\.pl\?cccc=[Kk]\w{3}</include>
  </pages>

  <!-- METAR data is updated once an hour, between 45 and 59 minutes after the
       hour; so, to be sure, we check twice an hour. -->

  <update interval="30"/>

  <!-- Known bugs/TODO list:
    * For wind data, should strip out the extra 0 for winds < 10 knots

    * Strip out cloud ceilings, *except* for the lowest layer
  -->

  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">

      <!-- Grab the whole METAR string. The hairy XPath was generated using
        the awesome XPath Checker Extension.

        Format: Airport Time Wind SkyCondition Temp/Dewpoint Altimeter Remarks
        e.g. KPAO 291947Z 02005KT 5SM BR OVC010 15/13 A3004 -->
      <variable name="metarString" select="normalize-space(string(/html/body/table[4]/tbody/tr/td[2]/table/tbody/tr/td/font[2]))"/>

      <!-- Obtain the airport code out of
        KPAO 291947Z 02005KT 5SM BR OVC010 15/13 A3004, e.g. "PAO"; we
        strip the 'K' in the airport code to save space on the toolbar. -->
      <variable name="airport" select="substring(substring-before($metarString, ' '), 2)"/>

      <!-- Strip off the airport identifier and time, so we get just the
        weather part of the metar. -->
      <variable name="metarWeather" select="substring-after(substring-after($metarString, ' '), ' ')"/>

      <!-- Get just the winds; Since we'll format and show the wind ourselves,
       also get the metar string w/o wind for further processing.
       We use the KT substring as a splitting identifier to take care of 
       AUTOmated and CORRected METAR reports. -->
      <variable name="metarWind" select="normalize-space(substring-before($metarWeather, 'KT'))"/>
      <variable name="metarNoWind" select="normalize-space(substring-after($metarWeather, 'KT'))"/>

      <!-- Do some munging on the base wind string; add an '@' between the
       direction and speed. Since the format is always XXXYYKT, where X is 
       always 3 digits for wind direction and Y is 2 to N digits for wind
       speed, we grab the first three digits of the string by starting at the
       strlen() - 4 (this is to remove AUTOmated and CORRected identifiers),
       and strip the knots off by substringing whatever's between the fourth
       character and the end of the string (the "KT" is gone from the 
       substring-before() call above. -->
      <variable name="formattedWind" select="concat(substring($metarWind, string-length($metarWind) - 4, 3), '@', substring($metarWind, string-length($metarWind) - 1))"/>


      <!-- Grab the visibility data, using the 'SM' as the marker. Most of
           the time, this will simply be a number (or fraction), but is could
           contain variable degree data from the winds, 
           i.e. "33010KT 240V300 10SM" Deal with that below. -->
      <variable name="metarVisWithVarWind" select="normalize-space(substring-before($metarNoWind, 'SM'))"/>

      <!-- Swallow everything through the temperature from
        BR OVC010 15/13 A3004
        We cheat, and use the "/", since it's an easily identifiable character 
        that will always appear in the string. Be careful, though, not to
        confuse this '/' with the '/' from a fractional visibility. -->
      <variable name="metarThroughTemp" select="substring-before(substring-after($metarNoWind, 'SM'), '/')"/>

      <!-- Chop off the last three characters of the string. 3 is a bit
        of a "magic number" because temperatures are always given as two digit
        numbers, and if it gets hotter or cooler than +/-100 deg. celsius on
        Planet Earth, then we have other problems.
        
        So, 3 swallows the two digits and the space... except for when the
        current temperature is below freezing, in which case the third
        character swallowed up is an 'M', leaving a string with a space.
        This is why we normalize the string one more time; in the common
        case, it's a no-op, but in the (special?) case of colder weather, it
        snags the extra space. -->
      <variable name="metarSkyCondToTemp" select="normalize-space(substring($metarThroughTemp, 1, string-length($metarThroughTemp) - 3))"/>
      
      <value-of select="$airport"/>
      <text>: </text>
      <value-of select="$formattedWind"/>

      <!-- Munge the visibility; if we have a variable wind report, strip it
           off; if not, just dump the visibility, fractions and all. -->
      <text> v</text>
      <choose>
         <when test="string-length(substring-after($metarVisWithVarWind, ' ')) &lt; 1">
           <value-of select="$metarVisWithVarWind"/>
         </when>
         <otherwise>
           <value-of select="substring-after($metarVisWithVarWind, ' ')"/>
         </otherwise>
      </choose>
      <text> </text>
      <value-of select="$metarSkyCondToTemp"/>

      <!-- Leaves us with: Airport: formatted winds and sky condition only,
       e.g. PAO: 020@05 v5 BR OVC010 -->
      </template>
    </transform>
  </template>
</generator>
