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

# Wipe the chart clean, and create empty copies of variables.

proc wipeclean {{name ""}} {
   global stinfo forecasts st_names st_times watchwarn stfile
   global fc_tm_idx advisory_list

   .warnings configure -foreground black

   # create the watchwarn() list if it doesn't exist
   if {[info exists watchwarn()] == 0} { set watchwarn() [list] }
   set ucname [string toupper $name]*
   set tmplist [list]
   if {[string length $name] && [llength $watchwarn()]} {
      foreach idx $watchwarn() {
         if {[regexp $ucname $idx]} { lappend tmplist $idx }
      }
      array unset watchwarn $ucname
      set watchwarn() [list]
      set watchwarn() $tmplist
   } else {
      set watchwarn() [list]
   }

   if {$name == ""} {
      all_wipeclean
      return
   }

   set name [string toupper [file tail $name]]
   set idx "STORM_$name*"
   foreach i [array names stinfo        "$idx"]  { unset stinfo($i) }
   foreach i [array names stfile        "$idx"]  { unset stfile($i) }
   foreach i [array names advisory_list "$idx"]  { unset advisory_list($i) }
   foreach i [array names stradii       "$idx"]  { unset stradii($i) }
   foreach i [array names fc_stradii    "$idx"]  { unset fc_stradii($i) }

   set idx "$name*"
   foreach i [array names st_times  "$idx"]  { unset st_times($i) }
   foreach i [array names forecasts "$idx"]  { unset forecasts($i) }
   foreach i [array names fc_tm_idx "$idx"]  { unset fc_tm_idx($i) }

   set old_st_names $st_names
   set st_names [list]
   foreach nm $old_st_names {
      if {[string compare $nm $name]} { lappend st_names $nm }
   }
   tk_wipe $name
}


proc all_wipeclean {} {
   global stinfo forecasts st_names st_times watchwarn
   global fc_tm_idx advisory_list

   array unset watchwarn
   set watchwarn() [list]

   catch { unset stinfo }         ;  array set stinfo {}
   catch { unset advisory_list }  ;  array set advisory_list {}
   catch { unset forecasts }      ;  array set forecasts {}
   catch { unset st_times }       ;  array set st_times {}
   catch { unset fc_tm_idx }      ;  array set fc_tm_idx {}
   catch { unset stradii }        ;  array set stradii {}
   catch { unset fc_stradii }     ;  array set fc_stradii {}
   catch { unset stfile }         ;  array set stfile {}
   set st_names [list]
   tk_wipe
}


# reload multiple storms

proc mreload {args} {
   global st_names
   if {[llength $args] > 0} {
      foreach i $args { reload $i }
   } else {
      if {[llength $st_names] > 0} {
         foreach i $st_names { reload $i }
      }
   }
}


# reload the data for a given storm---simplified (a LOT) to kill errors.

proc reload {{name ""}} {
   global advisory_list current_storm_name storms stfile watchwarn st_names

   set stlist [list]
   if {[string length $name]} {
      set stlist [list $name]
   } else {
      set stlist $st_names
   }
   foreach name $stlist {
      wipeclean $name
      set lcname [string tolower $name]
      set filename "$storms/$lcname.trk"
      if {[file exists $filename]} {
         set stfile($lcname) $lcname
         source $filename
         newtrk_curr
         plot_last $current_storm_name
      } else {
         puts "Can't find $filename ... $name not loaded."
      }
   }
   set fakeset ""  ;# prevents the last result from being displayed
}




