# JStrack:  copyright 1997/2010 by Jim Graham, N5IAL, all rights reserved.
#

# Called by the user to get a listing of forecasts available

proc fc_index {{name ""}} {
   global st_names

   set name [string toupper $name]
   if {[test_st_data] == -1} { return }
   if {$name != "" && [test_st_data $name] == -1} { return }

   if {$name == ""} {
      foreach name $st_names { display_indexes $name ; puts "" }
   } else {
      display_indexes $name
   }
}


# Called by the user to get info for a specific forecast (or forecasts)

proc fc_info {name timestamp {fc_ts *}} {
   global forecasts fc_tm_idx

   set name [string toupper $name]
   if {[test_st_data] == -1} return
   if {$name != "" && [test_st_data $name] == -1} { return }

   set timestamp [string toupper $timestamp]
   set fc_ts [string toupper $fc_ts]

   if {[array get fc_tm_idx $name$timestamp] == ""} {
      puts "No forecast data loaded for $name at $timestamp"
   }
   foreach i $fc_tm_idx($name$timestamp) {
      set idx "$name $timestamp $i"
      set xt $forecasts($idx,xt)
      eval plot_fc_info $name $timestamp $i $forecasts($idx) $xt
   }
}


# This procedure calls puts_stinfo to print the latest data for the
# storm (name) as of the NHC advisory (timestamp).

proc st_info {name timestamp {cmd "puts"}} {
   global stinfo

   set name [string toupper $name]
   if {[test_st_data] == -1} { return }
   if {$name != "" && [test_st_data $name] == -1 } { return }

   set timestamp [string toupper $timestamp]

   puts_stinfo $stinfo($name$timestamp) $cmd
}


# Like fc_index, called by the user to display a listing of past/current
# positions for the storm (name/timestamp only)

proc st_index {{name ""}} {
   global st_names st_times advisory_list

   set name [string toupper $name]
   if {[test_st_data] == -1} { return }
   if {$name != "" && [test_st_data $name] == -1} { return }

   if {$name == ""} {
      foreach name $st_names {
         foreach timestamp $st_times($name) {
            set idx $name$timestamp
            set advtxt ""
            catch { set advtext "  (Advisory $advisory_list($idx))" }
            puts "$name $timestamp$advtext"
         }
      }
      puts ""
   } else {
      foreach timestamp $st_times($name) {
         set idx $name$timestamp
         set advtxt ""
         catch { set advtext "  (Advisory $advisory_list($idx))" }
         puts "$name $timestamp$advtext"
      }
   }
}


# Called by the user to produce a chart showing a history of the storm.
# This could probably be done a lot better...but at least it works.

proc st_hist {name {cmd "puts"}} {
   global st_times stinfo units advisory_list

   if {$units == "KT"} { set du "    (NM/KT)"
   } else {              set du "(Miles/MPH)" }

   set name [string toupper $name]


   if {[test_st_data] == -1} { return }
   if {$name != "" && [test_st_data $name] == -1} { return }

   set lineno 0
   set last_press -1
   $cmd ".----------------------------------------------------------------------------."
   foreach timestamp $st_times($name) {
      incr lineno
      set pe 0
      if {[string compare $advisory_list($name$timestamp) "PE"] == 0} {
         set pe 1
      }

      if {$lineno == 1} {
         $cmd "| [format "%37s" $name]                          $du |"
         $cmd "| Time     | Type | Lat.    Long.  | Dir/speed | Pressure    | wnd/gst | eye |"
         $cmd "|----------|------|----------------|-----------|-------------|---------|-----|"
      } elseif {$cmd == "puts" && $lineno == 18} {
         set lineno 1
         $cmd "`----------------------------------------------------------------------------'"
         puts "Press RETURN to continue"
         gets stdin
         $cmd ".----------------------------------------------------------------------------."
         $cmd "| [format "%37s" $name]                          $du |"
         $cmd "| Time     | Type | Lat.    Long.  | Dir/speed | Pressure    | wnd/gst | eye |"
         $cmd "|----------|------|----------------|-----------|-------------|---------|-----|"
      }
      set cat " "
      foreach {type xa xb latt long course speed press winds gusts eye} \
         $stinfo($name$timestamp) { break }
      set type [get_st_type $name $timestamp]

      if {$type == "HR"} { set cat "[st_cat $winds]" }
      set latt [format "%4.1f" $latt]N
      set long [format "%5.1f" $long]W
      if {$pe == 0} {
         if {$course == "XX"} {
            set course "   "
         } else {
            set course [format "%3.0f" $course]
         }
         if {$speed == "XX"} {
            set speed "  "
         } else {
            if {$units == "MPH"} { set speed [expr {round($speed * 1.15)}] }
            set speed [format "%2d" $speed]
         }
         set pdir "   "
         if {$last_press != -1 && [string compare $press "XX"]} {
            if {$press > $last_press} {
               set pdir "(R)"
            } elseif {$press < $last_press} {
               set pdir "(F)"
            }
         }
         set last_press $press
         set press  [format "%4d"  $press]
         if {$units == "MPH"} {
            set winds [expr {round($winds * 1.15)}]
            if {[string compare $gusts XX]} {
               set gusts [expr {round($gusts * 1.15)}]
            }
         }
         set winds  [format "%3d"  $winds]
         if {[string compare $gusts XX]} {
            set gusts  [format "%3d"  $gusts]
         } else {
            set gusts "   "
         }
         if {$eye == "XX"} {
            set eye "   "
         } else {
            if {$units == "MPH"} { set eye [expr {round($eye * 1.15)}] }
            set eye [format "%3d" $eye]
         }
         $cmd -nonewline "| $timestamp | $type $cat |  $latt  $long | "
         if {[string compare $course "   "]} {
            $cmd -nonewline "$course at $speed "
         } else {
            $cmd -nonewline "          "
         }
         $cmd "| $press MB $pdir | $winds/$gusts | $eye |"
      } else {
         $cmd "| $timestamp | $type   |  $latt  $long |           |             |         |     |"
      }
   }
   $cmd "`----------------------------------------------------------------------------'"
}

