#!/usr/local/bin/wish -f

# Copyright(c) 1998, Space Science and Engineering Center, UW-Madison
# Refer to "McIDAS Software Acquisition and Distribution Policies"
# in the file  mcidas/data/license.txt

# $Id: rt_sfcdisp.gui,v 1.29 1998/10/13 20:26:48 dglo Exp $

#	igtv.gui
#
#

set guipath $env(GUIDIR)
set auto_path "$guipath $auto_path"
setoptions
set level 1

#
#	Before we get the GUI built - set up all of the lists which will
#	be used to make the selections.
#



wm title . "McIDAS: PTDISP"
wm iconname . "Display Surface Data"
placegui .
frame .frame  -borderwidth 10

#	Create buttons which line up at the top and will pack and
#	unpack frames depending upon the users level of expertise.
#	These will not be needed for all commands , as some are
#	too easy for progressive disclosure.
#
frame .top
button .top.low -text "Less" -command {set level 1;
	pack unpack .frame.hi
	pack forget .top.low
	pack .top.hi		-side left -expand yes
	pack .frame.low		-side top -fill x -expand yes }

button .top.hi  -text "More"	-command {set level 3;
	pack forget .top.hi
	pack .top.low		-side left -expand yes
	pack .frame.low		-side top -fill x
	pack .frame.hi		-side top -fill x }

button .top.help -text "Help" -command "mkHelp"
pack .top.help		-side right -expand yes
pack .top.hi		-side left -expand yes

message .msg -width 400 -font $messfont -borderwidth 1 -text "Display Real-Time Surface Data"

frame .frame.low



#
#	 parameters
#


frame .frame.low.parm
menubutton .frame.low.parm.menu -relief raised\
	-menu .frame.low.parm.menu.options\
	-text "Parameter" -width 20

entry .frame.low.parm.entry -textvariable parm \
	-relief groove -width 7 -state disabled

menu .frame.low.parm.menu.options -tearoff 0

set fmt " "
.frame.low.parm.menu.options add radio -label "Ceiling Height" \
	-variable parm -value CIGH  -command {set fmt " "
		set unit ""}
.frame.low.parm.menu.options add radio -label "Cloud Cover of Ceiling" \
	-variable parm -value CIGC  -command {set fmt " "
		set unit ""}
.frame.low.parm.menu.options add radio -label Dewpoint  \
	-variable parm -value TD -command {set fmt "I6"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Pressure at Sea Level" \
	-variable parm -value PSL -command {set fmt "F7.1"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Pressure at Station" \
	-variable parm -value PRE -command {set fmt "F7.1"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Precipitation" \
	-variable parm -value PCP -command {set fmt "F5.2"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Snow"  \
	-variable parm -value SNO -command {set fmt "I4"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Station Elevation" \
	-variable parm -value ZS -command {set fmt "I8"
		set unit ""}
.frame.low.parm.menu.options add radio -label Temperature \
	-variable parm -value T  -command {set fmt "I6"
		set unit ""}
.frame.low.parm.menu.options add radio -label Visibility \
	-variable parm -value VIS -command {set fmt "F6.1"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Weather Symbol" \
	-variable parm -value WXSYMB -command {set fmt "SYMB"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Weather Text" \
	-variable parm -value WXTEXT -command {set fmt "A4"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Wind Direction" \
	-variable parm -value DIR -command {set fmt "I3"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Wind Flag" \
	-variable parm -value WIND -command {set fmt "FLAG"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Wind Gusts" \
	-variable parm -value GUS -command {set fmt "I5"
		set unit ""}
.frame.low.parm.menu.options add radio -label "Wind Speed" \
	-variable parm -value SPD -command {set fmt "I5"
		set unit ""}
pack .frame.low.parm.menu -side left -padx 10
pack .frame.low.parm.entry -side right -padx 10
pack .frame.low.parm -side top  -pady 2 -fill both

#
#	Contour Units
#
set unit ""
frame .frame.low.unit
menubutton .frame.low.unit.menu -text "Units" -relief raised \
	-menu .frame.low.unit.menu.choices -width 20
#
#	Unit type menu.
#
menu .frame.low.unit.menu.choices -tearoff 0
.frame.low.unit.menu.choices add cascade -label "Linear Measure" \
	-menu .frame.low.unit.menu.choices.lin
.frame.low.unit.menu.choices add cascade -label "Pressure" \
	-menu .frame.low.unit.menu.choices.pres
.frame.low.unit.menu.choices add cascade -label "Temperature" \
	-menu .frame.low.unit.menu.choices.temp
.frame.low.unit.menu.choices add cascade -label "Wind" \
	-menu .frame.low.unit.menu.choices.wind

#
#	Linear measure units.
#
menu .frame.low.unit.menu.choices.lin -tearoff 0
.frame.low.unit.menu.choices.lin add radio -label "Centimeters (CM)" \
	-value CM -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Decameters (DM)" \
	-value DM -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Feet (FT)" \
	-value FT -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Geopotential meters(GPM)" \
	-value GPM -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Inches (IN)" \
	-value IN -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Kilometers (KM)" \
	-value KM -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Meters (M)" \
	-value M -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Miles (MI)" \
	-value MI -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Millimeters (MM)" \
	-value MM -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Nautical Miles (MI)" \
	-value NMI -variable unit
.frame.low.unit.menu.choices.lin add radio -label "Yards (YD)" \
	-value YD -variable unit

#
#	Pressure units.
#
menu .frame.low.unit.menu.choices.pres -tearoff 0
.frame.low.unit.menu.choices.pres add radio -label "Hectopacals (HPA)" \
	-value HPA -variable unit
.frame.low.unit.menu.choices.pres add radio -label "Inches of Mercury (INHG)" \
	-value INHG -variable unit
.frame.low.unit.menu.choices.pres add radio -label "Millibars (MB)" \
	-value MB -variable unit

#
#	Temperature units.
#
menu .frame.low.unit.menu.choices.temp -tearoff 0
.frame.low.unit.menu.choices.temp add radio -label "Celsius (C)" \
	-value C -variable unit
.frame.low.unit.menu.choices.temp add radio -label "Fahrenheit (F)" \
	-value F -variable unit
.frame.low.unit.menu.choices.temp add radio -label "Kelvin (K)" \
	-value K -variable unit

#
#	Wind units.
#
menu .frame.low.unit.menu.choices.wind -tearoff 0
.frame.low.unit.menu.choices.wind add radio -label "Knots (KTS)" \
	-value KTS -variable unit
.frame.low.unit.menu.choices.wind add radio -label "Meters Per Second (MPS)" \
	-value MPS -variable unit
.frame.low.unit.menu.choices.wind add radio -label "Miles Per Hour (MPH)" \
	-value MPH -variable unit

entry .frame.low.unit.entry -textvariable unit -width 7 -relief groove \
	-state disabled

pack .frame.low.unit.menu -side left -padx 10
pack .frame.low.unit.entry -side right -padx 10
pack .frame.low.unit -side top -pady 2 -fill both


frame .frame.low.time
menubutton .frame.low.time.menu -relief raised -text "Time" \
	-menu .frame.low.time.menu.options -width 20
entry .frame.low.time.entry -textvariable time -relief groove\
	-width  7
menu .frame.low.time.menu.options -tearoff 0
.frame.low.time.menu.options add radio -label 0 -variable time\
	-value 0
.frame.low.time.menu.options add radio -label 1 -variable time\
	-value 1
.frame.low.time.menu.options add radio -label 2 -variable time\
	-value 2
.frame.low.time.menu.options add radio -label 3 -variable time\
	-value 3
.frame.low.time.menu.options add radio -label 4 -variable time\
	-value 4
.frame.low.time.menu.options add radio -label 5 -variable time\
	-value 5
.frame.low.time.menu.options add radio -label 6 -variable time\
	-value 6
.frame.low.time.menu.options add radio -label 7 -variable time\
	-value 7
.frame.low.time.menu.options add radio -label 8 -variable time\
	-value 8
.frame.low.time.menu.options add radio -label 9 -variable time\
	-value 9
.frame.low.time.menu.options add radio -label 10 -variable time\
	-value 10
.frame.low.time.menu.options add radio -label 11 -variable time\
	-value 11
.frame.low.time.menu.options add radio -label 12 -variable time\
	-value 12
.frame.low.time.menu.options add radio -label 13 -variable time\
	-value 13
.frame.low.time.menu.options add radio -label 14 -variable time\
	-value 14
.frame.low.time.menu.options add radio -label 15 -variable time\
	-value 15
.frame.low.time.menu.options add radio -label 16 -variable time\
	-value 16
.frame.low.time.menu.options add radio -label 17 -variable time\
	-value 17
.frame.low.time.menu.options add radio -label 18 -variable time\
	-value 18
.frame.low.time.menu.options add radio -label 19 -variable time\
	-value 19
.frame.low.time.menu.options add radio -label 20 -variable time\
	-value 20
.frame.low.time.menu.options add radio -label 21 -variable time\
	-value 21
.frame.low.time.menu.options add radio -label 22 -variable time\
	-value 22
.frame.low.time.menu.options add radio -label 23 -variable time\
	-value 23

set time [Now]
pack .frame.low.time.menu -side left -padx 10
pack .frame.low.time.entry -side right -padx 10
pack .frame.low.time -side top  -pady 2 -fill both


frame .frame.low.date -relief sunken -borderwidth 2
set today [Today]
set pday $today
set date [getnicedate $today 0 ]
set jdate [getjdate $today 0]
set offset 0
set pdate $date
scale .frame.low.date.scale  -orient horizontal\
	-from -9 -to 0 -length 200 -showvalue YES\
	-variable offset\
	-command {set pdate "[getnicedate $today $offset] "
		  set jdate "[getjdate $today $offset]"
		  set junk }
frame .frame.low.date.offday
label .frame.low.date.offday.label -text "Date to plot : "
entry .frame.low.date.offday.entry -textvariable pdate -state disabled \
	-relief flat
pack .frame.low.date.offday.label  -side left
pack .frame.low.date.offday.entry -side right
pack .frame.low.date.offday -side top
pack .frame.low.date.scale -side top
pack .frame.low.date -side top -padx 2 -pady 2


#
# Menu for map options
#
set map USA
frame .frame.low.map
entry .frame.low.map.entry -textvariable map -width 7 -relief groove \
	-state disabled
menubutton .frame.low.map.menu -text "Map" -relief raised \
	-menu .frame.low.map.menu.choices -width 20

menu .frame.low.map.menu.choices -tearoff 0
.frame.low.map.menu.choices add radio -label "Define Lat/Lon (DEFINE)" \
	-variable map -value DEFINE \
	-command {enableLatLon .frame.low.lat .frame.low.lon}
.frame.low.map.menu.choices add radio -label "Midwest USA (MID)" \
	-variable map -value MID \
	-command {disableLatLon .frame.low.lat .frame.low.lon
		set lat ""
		set lon ""}
.frame.low.map.menu.choices add radio -label "North America (NA)" \
	-variable map -value NA \
	-command {disableLatLon .frame.low.lat .frame.low.lon
		set lat ""
		set lon ""}
.frame.low.map.menu.choices add radio -label "Satellite (SAT)" \
	-variable map -value SAT \
	-command {disableLatLon .frame.low.lat .frame.low.lon
		set lat ""
		set lon ""}
.frame.low.map.menu.choices add radio -label "USA (USA)" \
	-variable map -value USA \
	-command {disableLatLon .frame.low.lat .frame.low.lon
		set lat ""
		set lon ""}
.frame.low.map.menu.choices add command -label "States" \
	-command {set stype "map"; getstate
		disableLatLon .frame.low.lat .frame.low.lon
			set lat ""
			set lon ""}

pack .frame.low.map.menu -side left -padx 10
pack .frame.low.map.entry -side right -padx 10
pack .frame.low.map -side top  -pady 2 -fill both

#
#	Latitude  Bounds
#
set lat " "
frame .frame.low.lat
label .frame.low.lat.label -text "Latitude Min Max" -width 20
entry .frame.low.lat.entry -textvariable lat -width  7  -relief sunken \
	-state disabled


pack .frame.low.lat.label -side left -padx 10
pack .frame.low.lat.entry -side right -padx 10
pack .frame.low.lat -side top  -pady 2 -fill both

#
#	Longitude Bounds
#
set lon " "
frame .frame.low.lon
label .frame.low.lon.label -text "Longitude Min Max" -width 20
entry .frame.low.lon.entry -textvariable lon -width 7 -relief sunken \
	-state disabled


pack .frame.low.lon.label -side left -padx 10
pack .frame.low.lon.entry -side right -padx 10
pack .frame.low.lon -side top  -pady 2 -fill both
#
#	Lat/Lon Bounds start out disabled.
#
.frame.low.lat.label configure -foreground gray80
.frame.low.lon.label configure -foreground gray80



ptmore;


#
# Pack all the command widgets into the command frame
#
if {$level == "1"} {
	pack append .frame \
		.frame.low		{top fill}
}
if {$level == 3} {
	pack append .frame  \
		.frame.low			{top fill} \
}




#
# Make an OK button which will execute command and destroy widget
#

frame .bottom  -relief raised
button .bottom.ok -text OK -width 10 \
	-command {
		if {$clev == "" || $clev == " "} {set colkey " "
		} else {set colkey [format "COLOR=%s" $clev]}
		if {$lat == "" || $lat == " "} {set latkey " "
		} else {set latkey [format "LAT=%s" $lat]}
		if {$lon == "" || $lon == " "} {set lonkey " "
		} else {set lonkey [format "LON=%s" $lon]}
		if {$lsize == "" || $lsize == " "} {set lsizekey " "
		} else {set lsizekey [format "LSIZE=%s" $lsize]}
		set pankey [format "PAN=%s" $panel]
		if {$mlev == "" || $mlev == " "} {set cmapkey " "
		} else {set cmapkey [format "MCOLOR=%s" $mlev]}
		if {$project == "" || $project == " "} {set prokey " "
		} else {set prokey [format "PRO=%s" $project]}
		if {$slat == "" || $slat == " "} {set slatkey " "
		} else {set slatkey [format "SLAT=%s" $slat]}
		if {$slon == "" || $slon == " "} {set slonkey " "
		} else {set slonkey [format "SLON=%s" $slon]}
		set fmtkey [format "FORMAT=%s" $fmt]
		if {$parm == "WXSYMB"} {
			set parm "WX1"
			set oldparm "WXSYMB"}
		if {$parm == "WXTEXT"} {
			set parm "WX1"
			set oldparm "WXTEXT"}

#
#	Test for special map entries.
#
		set mmap $map

		set mapkey [format "MAP=%s" $map]
		if {$map == "DEFINE"} {
			set mapkey " "
		} else {
			if {$map == "DEFAULT"} {
				set mapkey " "
			} else {
				if {$map == "SAT"} {
					set mapkey "NAV=C"
				}
			}
		}

#-------
# check mandatory fields are filled
#-------
		   set rcode 0

		   set rcode [expr $rcode + [checkmandfield $parm	\
				"Parameter"]]
		   set rcode [expr $rcode + [checkmandfield $time	\
				"Time"]]

		   if {$rcode == 0} {
		   set select [format "SELECT='TIME $time ; DAY $jdate'"]
		   set paramkey [format "PARAM=$parm \[$unit\]"]
		   set dsname RTPTSRC/SFCHOURLY
		   set labkey "TDAY=CAL"
		   RunCommand "PTDISP $dsname $paramkey $select $mapkey $colkey $latkey $lonkey  $lsizekey $pankey $cmapkey  $prokey $slatkey $slonkey $fmtkey $labkey"

		}
		if {$parm == "WX1"} {set parm $oldparm}
	}
button .bottom.cancel -text Dismiss -command {destroy .} -width 10

pack .bottom.ok	-side left -expand true -padx 20
pack .bottom.cancel -side right -expand true -padx 20

initStatus .bottom.status
if [winfo exists .bottom.status] {
	pack .bottom.status -side left -expand true -padx 20
}

#
# Pack the message, the command and the OK buttons into the base widget
#
pack append .  \
	.top				{top fill} \
	.msg				{top fill} \
	.frame				{top fill} \
	.bottom				{bottom fill}
wm minsize . 10 10


proc mkHelp {} {
	global level messfont

	catch {destroy .help}

	toplevel .help
	wm title .help "Help - Display Surface Data"

	wm minsize .help 10 10
	placehelp .help

	message .help.title  -width 450 -font $messfont -text\
	"Display Real-Time Surface Data"

	frame .help.frame -relief groove -borderwidth 2
	scrollbar .help.frame.scroll -relief sunken \
		-command ".help.frame.low yview"
	text .help.frame.low -relief flat -height 38 -width 60\
		-yscroll ".help.frame.scroll set"
	.help.frame.low insert 0.0 {
  Parameter		type of surface point
			source data to display;
                        the McIDAS-XCD server
                        specified in the Client
                        Routing Table must contain
                        the group RTPTSRC; select
                        ADDE from the File menu
                        then click Edit Client
                        Routing Table; see the 
                        Edit Client Routing Table
                        help for more information 

  Units			units for the plot;
			depends on the plotted
			parameters

  Time			time of surface point
			source data

  Date to plot		date of surface point
			source data; the default
			is the current date; move
			the slider to specify surface
			point source data from the
			past nine days

  Map			geographical boundaries
			to contour

  Latitude Min Max      minimum and maximum latitude
			boundaries; active when Map
			Boundaries is set to DEFINE

  Longitude Min Max	minimum and maximum longitude
			boundaries; active when Map
			Boundaries is set to DEFINE
}
	if {$level == "3"} {
		.help.frame.low configure -height 43 -width 60
		.help.frame.low insert end {

  Plot Color		graphics color level for
			the plot

  Label Height		height in pixels of the
			plot labels

  Screen Panel		graphics frame panel
			number for displaying plots

  Map Color		graphics color level for
			the map

  Projection		projection for the plot

  Standard Latitude	one value for polar
			stereographic; two
			values for Lambert
			comformal

  Standard Longitude	normal longitude for polar
			stereographic and Lambert
			conformal

  Additional options are available using the PTDISP
  command from the McIDAS-X Text and Command Window.
  See the McIDAS-X User's Guide for more information.
}
}

	pack .help.frame.scroll -side right -expand yes -fill both
	pack .help.frame.low -side left -fill both
	button .help.ok -text OK -width 10 -relief raise -borderwidth 2 \
		-command {destroy .help}
	pack append .help \
		.help.title {top} \
		.help.ok  {bottom }\
		.help.frame {top}
}