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

# Replaced by mods to track.tk...doing it right!
# bind Entry <Delete> { tkEntryBackspace %W }

proc tk_st_data {} {
   global advisory_list storms units

   catch { destroy .stdata }
   set w .stdata
   toplevel $w
   wm title $w "Manually-entered storm data"
   wm geometry $w +0+0
   wm attributes $w -topmost true

   set w .stdata.type
   frame $w -bd 2 -relief sunken
   pack $w -side top -fill x

   set tksttype ""
   radiobutton $w.td -text "Tropical Depression" -variable tksttype -value "TD"
   radiobutton $w.ts -text "Tropical Storm" -variable tksttype -value "TS"
   radiobutton $w.hr -text "Hurricane" -variable tksttype -value "HR"
   label $w.nmlbl -text Name:
   entry $w.name -width 20              ; set entry_name $w.name
   pack $w.td $w.ts $w.hr -side left
   pack $w.name -side right -anchor e
   pack $w.nmlbl -side right -anchor e

   set w .stdata.dat1
   frame $w -bd 2 -relief sunken
   pack $w -side top -fill x

   label $w.tslbl -text "Timestamp (UTC):"
   entry $w.ts -width 9                 ; set entry_timestamp $w.ts
   button $w.tshelp -text "?" -command { tk_timestamp_menu }
   label $w.adlbl -text "NHC Advisory Number (if available):"
   entry $w.advno -width 4              ; set entry_advno $w.advno

   pack $w.tslbl $w.ts $w.tshelp -side left -anchor w
   pack $w.advno -side right -anchor e
   pack $w.adlbl -side right -anchor e

   set w .stdata.dat2
   frame $w -bd 2 -relief sunken
   pack $w -side top -fill x

   set w .stdata.dat2.info
   frame $w
   pack $w -side top
      label $w.infotxt -text \
         "For a stationary storm, do not enter any course or speed"
      pack $w.infotxt -side top

   set w .stdata.dat2.work
   frame $w
   pack $w -side top -fill x -expand 1

      frame $w.ltfr ; set fr $w.ltfr
         label $fr.ltlbl -text Latitude:
         entry $fr.latt -width 5    ; set entry_latt $fr.latt
         label $fr.ltlb2 -text N
         pack $fr.ltlbl $fr.latt $fr.ltlb2 -side left
      pack $fr -side left -anchor w -expand 1

      frame $w.lnfr ; set fr $w.lnfr
         label $fr.lnlbl -text Longitude:
         entry $fr.long -width 5    ; set entry_long $fr.long
         label $fr.lnlb2 -text W
         pack $fr.lnlbl $fr.long $fr.lnlb2 -side left
      pack $fr -side left -expand 1

      frame $w.crfr ; set fr $w.crfr
         label $fr.crlbl -text "Course (deg):"
         entry $fr.course -width 5  ; set entry_course $fr.course
         button $fr.crhelp -text "?" -command { tk_course_menu }
         pack $fr.crlbl $fr.course $fr.crhelp -side left
      pack $fr -side left -expand 1

      frame $w.spfr ; set fr $w.spfr
         label $fr.splbl -text "Speed ($units):"
         entry $fr.speed -width 3   ; set entry_speed $fr.speed
         pack $fr.splbl $fr.speed -side left
      pack $fr -side right -anchor e -expand 1

   set w .stdata.dat3
   frame $w -bd 2 -relief sunken
   pack $w -side top -fill x -expand 1

      frame $w.wndfr ; set fr $w.wndfr
         label $fr.lbl -text "Max. winds ($units):"
         entry $fr.ent -width 3 ; set entry_winds $fr.ent
         label $fr.sp -width 1
         pack $fr.lbl $fr.ent $fr.sp -side left
      pack $fr -side left -anchor w -expand 1

      frame $w.gstfr ; set fr $w.gstfr
         label $fr.lbl -text "Max. gusts ($units):"
         entry $fr.ent -width 3 ; set entry_gusts $fr.ent
         label $fr.sp -width 1
         pack $fr.lbl $fr.ent $fr.sp -side left
      pack $fr -side left -expand 1

      frame $w.psifr ; set fr $w.psifr
         label $fr.lbl -text "Press. (mb):"
         entry $fr.ent -width 4 ; set entry_press $fr.ent
         label $fr.sp -width 1
         pack $fr.lbl $fr.ent $fr.sp -side left
      pack $fr -side left -expand 1

      if {$units == "MPH"} { set eyeunits Miles
      } else {               set eyeunits NM }

      frame $w.eyefr ; set fr $w.eyefr
         label $fr.lbl -text "Eye diam. ($eyeunits):"
         entry $fr.ent -width 2 ; set entry_eye $fr.ent
         pack $fr.lbl $fr.ent -side left
      pack $fr -side right -anchor e -expand 1

   set w .stdata.end
   frame $w
   pack $w -side top -anchor center

   button $w.dismiss -text "Dismiss" -command { destroy .stdata }
   button $w.done -text "Done" -command [subst "test_stdata \
      $entry_name $entry_timestamp $entry_advno $entry_latt \
      $entry_long $entry_course $entry_speed $entry_winds \
      $entry_gusts $entry_press $entry_eye"]
   pack $w.dismiss $w.done -side left

   bind $entry_name      <Return> [subst "focus $entry_timestamp"]
   bind $entry_timestamp <Return> [subst "focus $entry_advno"]
   bind $entry_advno     <Return> [subst "focus $entry_latt"]
   bind $entry_latt      <Return> [subst "focus $entry_long"]
   bind $entry_long      <Return> [subst "focus $entry_course"]
   bind $entry_course    <Return> [subst "focus $entry_speed"]
   bind $entry_speed     <Return> [subst "focus $entry_winds"]
   bind $entry_winds     <Return> [subst "focus $entry_gusts"]
   bind $entry_gusts     <Return> [subst "focus $entry_press"]
   bind $entry_press     <Return> [subst "focus $entry_eye"]
   bind $entry_eye       <Return> [subst "focus $entry_name"]
   focus $entry_name
}


proc sterr {msg} {
   bell
   tk_messageBox -parent .stdata \
      -message "Data for \"$msg\" must be entered!" -type ok
}


proc test_stdata {entry_name entry_timestamp entry_advno \
   entry_latt entry_long entry_course entry_speed entry_winds \
   entry_gusts entry_press entry_eye} {

   global tksttype storms units

   set type         $tksttype
   set name         [$entry_name get]
   set timestamp    [$entry_timestamp get]
   set advno        [$entry_advno get]
   set latt         [$entry_latt get]
   set long         [$entry_long get]
   set course       [$entry_course get]
   set speed        [$entry_speed get]
   set winds        [$entry_winds get]
   set gusts        [$entry_gusts get]
   set press        [$entry_press get]
   set eye          [$entry_eye get]

   if {[string compare $advno ""] == 0} { set advno XX }
   if {[string compare $course ""] == 0} { set course XX ; set speed XX }
   if {[string compare $eye ""] == 0} { set eye XX }

   if {[string compare $type ""] == 0}      { return [sterr "Storm type"] }
   if {[string compare $name ""] == 0}      { return [sterr "Storm name"] }
   if {[string compare $timestamp ""] == 0} { return [sterr "Timestamp"] }
   if {[string compare $latt ""] == 0}      { return [sterr "Latitude"] }
   if {[string compare $long ""] == 0}      { return [sterr "Longitude"] }
   if {[string compare $speed ""] == 0}     { return [sterr "Speed"] }
   if {[string compare $winds ""] == 0}     { return [sterr "Max. winds"] }
   if {[string compare $gusts ""] == 0}     { return [sterr "Max. gusts"] }
   if {[string compare $press ""] == 0}     { return [sterr "Pressure"] }

   set name [string toupper $name]
   set timestamp [string toupper $timestamp]
   set advno [string toupper $advno]

   if {$units == "MPH"} {
      if {$speed != "XX"} { set speed [expr {round($speed / 1.15)}] }
      if {$winds != ""} { set winds [expr {round($winds / 1.15)}] }
      if {$gusts != ""} { set gusts [expr {round($gusts / 1.15)}] }
      if {$eye != "XX"} { set eye [expr {round($eye / 1.15)}] }
   }

   curr $timestamp $type $name $latt $long \
        $course $speed $press $winds $gusts $eye
   set advisory_list($name$timestamp) $advno

   set save [tk_messageBox -message "Append to $storms/$name.trk?" \
            -type yesno]

   if {$save == "yes"} {
      set f [open $storms/[string tolower $name].trk a]
      puts -nonewline $f "curr $timestamp $type $name $latt $long "
      puts $f "$course $speed $press $winds $gusts $eye"
      puts $f "set advisory_list($name$timestamp) \"$advno\""
      close $f
   }

}

proc tk_course_menu {} {
   catch {destroy .cst}
   toplevel .cst

   set tw .stdata.dat3.wndfr.ent
   set x [winfo rootx $tw]
   set y [winfo rooty $tw]
   wm geometry .cst +$x+$y
   wm attributes .cst -topmost true

   set w .cst.f1 ; frame $w ; pack $w -side top
   button $w.b1 -text "N (0.0 deg.)" -command {set cstval 0.0}
   button $w.b2 -text "NNE (22.5 deg.)" -command {set cstval 22.5}
   button $w.b3 -text "NE (45.0 deg.)" -command {set cstval 45.0}
   button $w.b4 -text "ENE (67.5 deg.)" -command {set cstval 67.5}
   pack $w.b1 $w.b2 $w.b3 $w.b4 -side left -expand 1

   set w .cst.f2 ; frame $w ; pack $w -side top
   button $w.b1 -text "E (90.0 deg.)" -command {set cstval 90.0}
   button $w.b2 -text "ESE (112.5 deg.)" -command {set cstval 112.5}
   button $w.b3 -text "SE (135.0 deg.)" -command {set cstval 135.0}
   button $w.b4 -text "SSE (157.5 deg.)" -command {set cstval 157.5}
   pack $w.b1 $w.b2 $w.b3 $w.b4 -side left -expand 1

   set w .cst.f3 ; frame $w ; pack $w -side top
   button $w.b1 -text "S (180.0 deg.)" -command {set cstval 180.0}
   button $w.b2 -text "SSW (202.5 deg.)" -command {set cstval 202.5}
   button $w.b3 -text "SW (225.0 deg.)" -command {set cstval 225.0}
   button $w.b4 -text "WSW (247.5 deg.)" -command {set cstval 247.5}
   pack $w.b1 $w.b2 $w.b3 $w.b4 -side left -expand 1

   set w .cst.f4 ; frame $w ; pack $w -side top
   button $w.b1 -text "W (270.0 deg.)" -command {set cstval 270.0}
   button $w.b2 -text "WNW (292.5 deg.)" -command {set cstval 292.5}
   button $w.b3 -text "NW (315.0 deg.)" -command {set cstval 315.0}
   button $w.b4 -text "NNW (337.5 deg.)" -command {set cstval 337.5}
   pack $w.b1 $w.b2 $w.b3 $w.b4 -side left -expand 1

   global cstval
   tkwait variable cstval
   .stdata.dat2.work.crfr.course delete 0 end
   .stdata.dat2.work.crfr.course insert end $cstval
   destroy .cst
}


proc tk_ts_puts_a {args} {
   set w .tsmenu.text

   if {[llength $args] == 1} {
      set text "[lindex $args 0]"
      $w insert end "$text\n"
   } elseif {[llength $args] == 2 && [lindex $args 0] == "-nonewline"} {
      set text "[lindex $args 1]"
      $w insert end "$text"
   }
   $w see end
}


proc tk_ts_puts_b {args} {
   set w .tsmenu.text2

   if {[llength $args] == 1} {
      set text "[lindex $args 0]"
      $w insert end "$text\n"
   } elseif {[llength $args] == 2 && [lindex $args 0] == "-nonewline"} {
      set text "[lindex $args 1]"
      $w insert end "$text"
   }
   $w see end
}


proc tk_timestamp_menu {} {
   global text_font
   set seconds [clock seconds]

   toplevel .tsmenu
# Try to figure out the best place to put the window.
# If this fails, leave it up to the window manager.
   if {[regexp "(\[0-9]+)x(\[0-9]+)\[+](\[0-9]+)\[+](\[0-9]+)" \
      [winfo geometry .stdata.dat1.tshelp] \
      a x1 y1 x2 y2] == 1} {
      eval wm geometry .tsmenu +[expr {$x1 + $x2}]+[expr {$y1 + $y2 + 60}]
   }

   set w .tsmenu
   wm attributes $w -topmost true
   text $w.text -width 70 -height 6 -font $text_font \
   -foreground black -background white -spacing3 1.2
   pack $w.text -side top
   set cmd tk_ts_puts_a

   set timezone [clock format $seconds -format %Z]
   if {[regexp ".DT" $timezone] == 1} {
      set etz "EDT" ; set egmt 4 ; set msg "appears"
   } else {
      set etz "EST" ; set egmt 5 ; set msg "does not appear"
   }
   $cmd "Daylight savings time $msg to be in effect (TZ=$timezone)."
   $cmd ""
   set ld [clock format $seconds -format %d]
   set localhour [clock format $seconds -format %H]
   set gmtdate [clock format $seconds -format %d -gmt true]
   set gmthour [clock format $seconds -format %H -gmt true]
   set gmtdiff [clock format [clock scan "00:00"] -format %H -gmt true]
   set etzdiff [expr {$gmtdiff - $egmt}]
   set nhcdate [clock format [clock scan "$etzdiff hour"] -format %d]
   set nhchour [clock format [clock scan "$etzdiff hour"] -format %H]

   $cmd "Based on the current date and time, I'm guessing that the storm data"
   $cmd -nonewline "is from the [subst $localhour]00 $timezone advisory, or "
   $cmd "[subst $nhchour]00 $etz, for day $ld ($timezone) of the"
   $cmd -nonewline "month.  In other words, it appears to be for "
   $cmd "[subst $gmtdate/$gmthour]00Z."

   button $w.default -text "Use the above value" \
      -command [subst ".stdata.dat1.ts insert end [subst $gmtdate/$gmthour]00Z ; \
               destroy $w"]
   pack $w.default -side top

# Let's use some real values to look better/cleaner
   set mont [clock format [clock seconds] -format %b]
   set monn [clock format [clock seconds] -format %m]
   set lh [subst $localhour]00
   set eh [subst $nhchour]00
   set gh [subst $gmthour]00
   set gd $gmtdate
   set ed $nhcdate

   text $w.text2 -height 8 -width 70 -font $text_font \
                  -foreground black -background white -spacing3 1.2
   pack $w.text2 -side top
   set cmd tk_ts_puts_b

   $cmd "Ok, I need you to enter the correct date/time for the advisory."
   $cmd "The format for this is as in the examples below:"
   $cmd ""
   $cmd "      $mont $ld $lh $timezone   -OR-   $monn/$ld $lh $timezone"
   $cmd "      $mont $ed $eh $etz   -OR-   $monn/$ed $eh $etz"
   $cmd "      $mont $gd $gh UTC   -OR-   $monn/$gd $gh UTC"
   $cmd "      $mont $gd $gh GMT   -OR-   $monn/$gd $gh GMT"

   frame $w.entfr
   pack $w.entfr -side top -expand 1 -fill x
   label $w.entfr.entlbl -text "Date/Time:"
   entry $w.entfr.entry -width 30
   pack $w.entfr.entlbl $w.entfr.entry -side left -anchor center
   bind $w.entfr.entry <Return> [subst "test_ts_entry $gmtdate $gmthour"]
}

proc test_ts_entry {gmtdate gmthour} {
   set dt [.tsmenu.entfr.entry get]
   if {[catch {set ds [clock scan "$dt"]} err] == 1} {
      bgerror $err
   } else {
      set ldt [clock format $ds -format "%b %d %H00"]
      set gdt [clock format $ds -format "%b %d %H00" -gmt true]
      set retval [clock format $ds -format "%d/%H00Z" -gmt true]

      set ok [tk_messageBox -message "
Local: $ldt
UTC:    $gdt
Timestamp:  $retval

Is this ok?
" -type yesno]
      if {$ok == "yes"} {
         .stdata.dat1.ts insert end [subst $gmtdate/$gmthour]00Z
         destroy .tsmenu
      }
   }
}


