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

package require Img
source $libdir/imagery_links.tcl
set loadsat 0

proc getsatmap {jsmap map} {
   global imgdir map_land satimgid goeslinks c_h satimgidb tcl_platform
   global trackdir

   if {$tcl_platform(platform) == "windows"} {
      set jpegtopnm $trackdir/windows_bin/jpegtopnm.exe
      set pamscale $trackdir/windows_bin/pamscale.exe
      set pamcut $trackdir/windows_bin/pamcut.exe
      foreach fn $jpegtopnm $pamscale $pamcut {
         if {![file executable $fn]} {
            tk_messageBox -type ok -message "
WARNING:  $fn is not executable...cannot load GOES East
imagery on the tracking map.  Please fix the permissions and try again.
"
         return -1
         }
      }
   } else {
      set jpegtopnm jpegtopnm ;# rely on $PATH to find these
      set pamscale pamscale
      set pamcut pamcut
   }

   catch { .c delete $satimgid }

   # NOTE:  These do *NOT* line up perfectly---they are, however, more
   # or less a "best fit" against JStrack's tracking maps...but I'm
   # working on it...one map at a time.

   if {$jsmap == "gulf"} {
      set xzoom 1.98  ;# values for gulf map fine-tuned 18Aug07
      set yzoom 2.06  ;# now it DOES line up rather nicely
      set mapx 310
      set mapy -168
      set landmap gulf_cab2_js-land.bmp
   } elseif {$jsmap == "full"} {
      set xzoom 1.47  ;# values for full map fine-tuned 18Aug07
      set yzoom 1.55  ;# like the gulf map, now lines up nicely
      set mapx 212
      set mapy 142
      set landmap large_js-land.bmp
   } elseif {$jsmap == "eastcoast"} {
      set xzoom 1.05  ;# values for east coast map fine-tuned 18Aug07
      set yzoom 1.10  ;# blah blah blah...you know the routine!
      set mapx 147
      set mapy 287
      set landmap east_coast_js-land.bmp
   } elseif {$jsmap == "carib"} {
#     set xzoom 2.75  ;# values for Caribbean map entered 02Sep10
      set xzoom 2.76  ;# values for Caribbean map entered 02Sep10
      set yzoom 2.41  ;# lines up rather nicely....
      set mapx  -170    ;# (but still not perfect, of course)
      # Higher negative values for mapx move the image to the left
      set mapy -425
      # lower negative values for mapy move down
      set landmap carib_js-land.bmp
   }

   # yes, the next two lines change the aspect ratio of the satellite
   # image---this is required to make it line up properly with JStrack's
   # tracking maps (a flat map view of Earth isn't the same as that from
   # 22,300 miles above the equator, aka geosynchronous orbit).

   set width [expr {int(662 * $xzoom)}]
   set height [expr {int(490 * $yzoom)}]

   set MAPFN [string toupper $map.jpg]
   set fn [download_img $goeslinks(jpgbase)/$MAPFN]
   if {$fn == ""} { return -1 }

   # convert/resize $map.jpg to map.pnm
   catch { exec $jpegtopnm imagery/$map.jpg > imagery/map.pnm }
   catch { exec $pamscale -width $width -height $height imagery/map.pnm > imagery/mapb.pnm }
   catch { exec $pamcut -left 50 -top 468 imagery/map.pnm > imagery/mapida.pnm }
   catch { exec $pamscale 1.4 imagery/mapida.pnm > imagery/mapid.pnm }

   image create photo satimg -file imagery/mapb.pnm
   image create photo satid -file imagery/mapid.pnm

   # clean up our mess....
   foreach fn [glob -nocomplain imagery/*.pnm] {
      file delete $fn
   }

   set id_y $mapy
   incr id_y $height

# Calculate the maximum (lowest) Y value for the top of the ID image
# Adjust as needed to make sure the ID is not right in the middle of
# the tracking chart AND is not off of the screen completely, i.e.,
# out of the scroll region, which is from Y=0 (top) to Y=768 (bottom).
# Bug fixed on 06Jul08.

   set id_height [image height satid]

   set idymax [expr {768 - $id_height}]
   if {$id_y > $idymax} { set id_y $idymax }

   set satimgid [.c create image $mapx $mapy -image satimg -anchor nw]
   set satimgidb [.c create image $mapx $id_y -anchor sw -image satid]
   return 0 ; OK
}


