# Weather station library module.
# Rick Smereka, Copyright (C) 2006.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com
   
# Initial tcl version. May/2006, Rick Smereka

# 'wlib_get_field' return codes
const WGF_OK 0
const WGF_BLANK 1
const WGF_PARM_ERROR 2
const WGF_NO_SUCH_FIELD 3
const WGF_MEMORY_FAIL 4
const WGF_INTERNAL_ERROR 5
 
# wind direction as alpha and in degrees

set wlib_wdesc "N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW"
set wlib_wdeg "0.0 22.5 45.0 67.5 90.0 112.5 135.0 157.5 180.0 202.5 225.0"
append wlib_wdeg " 247.5 270.0 292.5 315.0 337.5"

proc wlib_wdir_desc_2_deg {ddesc ddeg} {
   # Convert a wind direction as an alpha description to
   # degrees. There are sixteen possible alpha descriptions.
   # Returns 1 upon success with the wind direction in
   # degrees loaded into the return the 'fxp' list 'ddeg'.
   # Returns 0 otherwise.
   global wlib_wdesc wlib_wdeg
   upvar $ddeg uddeg
   set uddeg ""

   if {$ddesc == ""} {
      return 0
   }

   set u_ddesc [string toupper $ddesc]
   set pos [lsearch $wlib_wdesc $u_ddesc]

   if {$pos != -1} {
      set result [fxp_iconv [lindex $wlib_wdeg $pos]]

      if {$result == ""} {
         return 0
      }
   } else {
      return 0
   }

   set uddeg $result
   return 1
}

proc wlib_get_field {line outfield which delim} {
   # Obtain a 'delim' delimited field within the text 'line' of a
   # 'WeatherLink' report. Upon success, the result field will be
   # loaded into 'outfield' Returns a 'wlib_get_field' code.
   global WGF_PARM_ERROR WGF_NO_SUCH_FIELD WGF_INTERNAL_ERROR
   global WGF_BLANK WGF_OK
   upvar $outfield uoutfield

   if {$line == ""} {
      return $WGF_PARM_ERROR
   }

   set uoutfield ""

   if {$which > [ll_words $line $delim]} {
      return $WGF_NO_SUCH_FIELD
   }

   if {![ll_word $line tfield $which $delim]} {
      return $WGF_INTERNAL_ERROR
   }

   # an empty field consists of two hyphens in a row
   if {$tfield == "--"} {
      return $WGF_BLANK
   }

   set uoutfield [trim $tfield]
   return $WGF_OK
}

proc wlib_report_trailer {fout apname ver do_copy} {
   # Place a standard trailer on a weather report with or
   # without a copyright notice. If the copyright notice
   # is turned off, the program platform will not be
   # reported. Returns 1 upon success, 0 otherwise.
   global tcl_platform

   set tbuf [get_ftime]
   puts $fout "\nLast Updated $tbuf EDT/EST."
   puts -nonewline $fout "This report was generated by $apname"

   if {$ver != ""} {
      puts -nonewline $fout " version $ver" 
   }

   if {$do_copy} {
      puts $fout " for $tcl_platform(platform)"
      set ryear [run_year]
      puts $fout "Copyright (C) $ryear, Rick Smereka."
   } else {
      puts $fout "."
   }

   return 1
}
