File mttroot/mtt/bin/trans/struc2gnuplot_txt2wish artifact 1d1598cc62 part of check-in 05b94328ac


#! /bin/sh
# -*-tcl-*-

sys=$1

out=${sys}_gnuplot.wish

struc=${sys}_struc.txt
dat2=${sys}_odes.dat2

Nx=`mtt_getsize ${sys} x`
Ny=`mtt_getsize ${sys} y`

## write header
cat <<EOF > ${out}
#!/bin/sh
# -*-tcl-*- Next line replaces shell with wish\\
exec wish "\$0" "\$@"

## main window
wm title . "MTT: ${sys}"

# menu bar
canvas .menubar

menubutton .menubar.file -text "File" -underline 0 -menu .menubar.file.menu
menu .menubar.file.menu
.menubar.file.menu add command -label "Open    (Gnuplot template)" -underline 0   -command open_plotfile
.menubar.file.menu add command -label "Save    (Gnuplot template)" -underline 0   -command save_plotfile
.menubar.file.menu add separator
.menubar.file.menu add command -label "Print     (PostScript)"   -underline 0   -command print_postscript
.menubar.file.menu add separator
.menubar.file.menu add command -label "Quit"			  -underline 0   -command exit

menubutton .menubar.edit -text "Edit" -underline 0 -menu .menubar.edit.menu
menu .menubar.edit.menu
.menubar.edit.menu add cascade -label "Axes"         -underline 0 -menu .menubar.edit.menu.axes
.menubar.edit.menu add cascade -label "Contour (3D)" -underline 0 -menu .menubar.edit.menu.contour
.menubar.edit.menu add cascade -label "Data style"   -underline 0 -menu .menubar.edit.menu.datastyle
.menubar.edit.menu add cascade -label "Grid"         -underline 0 -menu .menubar.edit.menu.grid
.menubar.edit.menu add cascade -label "Key"          -underline 0 -menu .menubar.edit.menu.key
.menubar.edit.menu add cascade -label "Timestamp"    -underline 0 -menu .menubar.edit.menu.time

menu .menubar.edit.menu.axes
.menubar.edit.menu.axes add command -label "Cartesian" -underline 0 -command { do_cmd "set nopolar ; replot" }
.menubar.edit.menu.axes add command -label "Polar"     -underline 0 -command { do_cmd "set   polar ; replot" }
.menubar.edit.menu.axes add cascade -label "Ranges"    -underline 0 -menu .menubar.edit.menu.axes.range

menu .menubar.edit.menu.axes.range
.menubar.edit.menu.axes.range add command -label "Set X"   -underline 4 -command { do_cmd "set xrange \[[get_value "X axis range? min:max" "*:*"]\] ; replot" }
.menubar.edit.menu.axes.range add command -label "Set Y"   -underline 4 -command { do_cmd "set yrange \[[get_value "Y axis range? min:max" "*:*"]\] ; replot" }
.menubar.edit.menu.axes.range add command -label "Set Z"   -underline 4 -command { do_cmd "set zrange \[[get_value "Z axis range? min:max" "*:*"]\] ; replot" }

menu .menubar.edit.menu.contour
.menubar.edit.menu.contour add command -label "Show Contour" -underline 0 -command { do_cmd "set   dgrid3d ; set   contour ; replot" }
.menubar.edit.menu.contour add command -label "Hide Contour" -underline 0 -command { do_cmd "set nodgrid3d ; set nocontour ; replot" }
.menubar.edit.menu.contour add cascade -label "Surface"      -underline 0 -menu .menubar.edit.menu.contour.surface

menu .menubar.edit.menu.contour.surface
.menubar.edit.menu.contour.surface add command -label "Show Surface" -underline 0 -command { do_cmd "set   surface ; replot" }
.menubar.edit.menu.contour.surface add command -label "Hide Surface" -underline 0 -command { do_cmd "set nosurface ; replot" }

menu .menubar.edit.menu.datastyle
.menubar.edit.menu.datastyle add command -label "Dots"    -underline 0 -command { do_cmd "set data style dots   ; replot" }
.menubar.edit.menu.datastyle add command -label "Lines"   -underline 0 -command { do_cmd "set data style lines  ; replot" }
.menubar.edit.menu.datastyle add command -label "Points"  -underline 0 -command { do_cmd "set data style points ; replot" }

menu .menubar.edit.menu.grid
.menubar.edit.menu.grid add cascade -label "Show Grid" -underline 0 -menu .menubar.edit.menu.grid.show
.menubar.edit.menu.grid add cascade -label "Hide Grid" -underline 0 -menu .menubar.edit.menu.grid.hide

menu .menubar.edit.menu.grid.show
.menubar.edit.menu.grid.show add command -label "Show all ticks" -underline 5 -command { do_cmd "set grid xtics ytics ztics ; replot" }
.menubar.edit.menu.grid.show add command -label "Show X ticks" -underline 5 -command { do_cmd "set grid xtics ; replot" }
.menubar.edit.menu.grid.show add command -label "Show Y ticks" -underline 5 -command { do_cmd "set grid ytics ; replot" }
.menubar.edit.menu.grid.show add command -label "Show Z ticks" -underline 5 -command { do_cmd "set grid ztics ; replot" }

menu .menubar.edit.menu.grid.hide
.menubar.edit.menu.grid.hide add command -label "Hide all ticks" -underline 5 -command { do_cmd "set grid noxtics noytics noztics ; replot" }
.menubar.edit.menu.grid.hide add command -label "Hide X ticks" -underline 5 -command { do_cmd "set grid noxtics ; replot" }
.menubar.edit.menu.grid.hide add command -label "Hode Y ticks" -underline 5 -command { do_cmd "set grid noytics ; replot" }
.menubar.edit.menu.grid.hide add command -label "Hide Z ticks" -underline 5 -command { do_cmd "set grid noztics ; replot" }

menu .menubar.edit.menu.key
.menubar.edit.menu.key add command -label "Show Key"   -underline 0 -command { do_cmd "set   key ; replot" }
.menubar.edit.menu.key add command -label "Hide Key"   -underline 0 -command { do_cmd "set nokey ; replot" }
.menubar.edit.menu.key add cascade -label "Position"   -underline 0 -menu .menubar.edit.menu.key.posn

menu .menubar.edit.menu.key.posn
.menubar.edit.menu.key.posn add command -label "Left"    -underline 0 -command { do_cmd "set key left    ; replot" }
.menubar.edit.menu.key.posn add command -label "Right"   -underline 0 -command { do_cmd "set key right   ; replot" }
.menubar.edit.menu.key.posn add command -label "Top"     -underline 0 -command { do_cmd "set key top     ; replot" }
.menubar.edit.menu.key.posn add command -label "Bottom"  -underline 0 -command { do_cmd "set key bottom  ; replot" }
.menubar.edit.menu.key.posn add command -label "Outside" -underline 0 -command { do_cmd "set key outside ; replot" }
.menubar.edit.menu.key.posn add command -label "Below"   -underline 3 -command { do_cmd "set key below   ; replot" }

menu .menubar.edit.menu.time
.menubar.edit.menu.time add command -label "Show Timestamp"   -underline 0 -command { do_cmd "set   timestamp ; replot" }
.menubar.edit.menu.time add command -label "Hide Timestamp"   -underline 0 -command { do_cmd "set notimestamp ; replot" }

menubutton .menubar.tool -text "Tools" -underline 0 -menu .menubar.tool.menu
menu .menubar.tool.menu
.menubar.tool.menu add cascade -label "gnuplot command" -underline 0 -menu .menubar.tool.menu.gcmd
.menubar.tool.menu add cascade -label "Sort List" -underline 0 -menu .menubar.tool.menu.sort
.menubar.tool.menu add command -label "X-MTT"     -underline 0 -command { exec xmtt & }

menu .menubar.tool.menu.gcmd
.menubar.tool.menu.gcmd add command -label "Show Command" -underline 0 -command show_gnuplot_prompt
.menubar.tool.menu.gcmd add command -label "Hide Command" -underline 0 -command hide_gnuplot_prompt

menu .menubar.tool.menu.sort
.menubar.tool.menu.sort add command -label "Left to Right" -underline 0 -command {
    set sortorder "normal"
    set xl [sort_list \$xl \$sortorder ]
    set yl [sort_list \$yl \$sortorder ]
}
.menubar.tool.menu.sort add command -label "Right to Left" -underline 0 -command {
    set sortorder "reverse"
    set xl [sort_list \$xl \$sortorder]
    set yl [sort_list \$yl \$sortorder]
}

button .menubar.quit  -text "Quit" -relief flat -command exit 

pack .menubar.file    -expand false -fill x -side left
pack .menubar.edit    -expand false -fill x -side left
pack .menubar.tool    -expand false -fill x -side left
pack .menubar.quit    -expand false -fill x -side right

# list bar
canvas .listbar

radiobutton .listbar.states  -text "States"   -value states  -variable view -command { .parameters.list configure -listvar xl }
radiobutton .listbar.outputs -text "Outputs"  -value outputs -variable view -command { .parameters.list configure -listvar yl }

checkbutton .listbar.sort -text "Toggle Sort" -relief flat -indicatoron false -onvalue "reverse" -offvalue "normal" -variable sortorder -command {    
    set xl [sort_list \$xl \$sortorder ]
    set yl [sort_list \$yl \$sortorder ]
}

pack .listbar.states  -expand false -fill x -side left
pack .listbar.outputs -expand false -fill x -side left
pack .listbar.sort    -expand false -fill x -side right

# side bar
canvas .plotbar

tk_optionMenu .plotbar.select select  "Select X" "Select Y" "Select Z"

tk_optionMenu .plotbar.dim   dimension "2D" "3D"

tk_optionMenu .plotbar.plot  plottype "Single" "Multi"

pack .plotbar.dim     -expand true -fill x -side right
pack .plotbar.plot    -expand true -fill x -side right
pack .plotbar.select  -expand true -fill x -side right

# button bar
canvas .buttonbar

button .buttonbar.replot -text "Refresh"   -relief raised -command { do_cmd "replot" }

button .buttonbar.newwin -text "New Plot"  -relief raised -command {
    set windownum [expr 1 + \$windownum]
    do_cmd "set term x11 \$windownum"
}

pack .buttonbar.newwin  -expand true -side right -fill x
pack .buttonbar.replot  -expand true -side right -fill x

# rotation bar
canvas .rotationbar
scale .rotationbar.xrot  -label "Rotate X" -orient h -from "0" -to "180" -command { do_rotate_3d "xrot" }
scale .rotationbar.zrot  -label "Rotate Z" -orient h -from "0" -to "360" -command { do_rotate_3d "zrot" }
pack .rotationbar.xrot  -side left -fill x -expand true
pack .rotationbar.zrot  -side left -fill x -expand true
.rotationbar.xrot set 30
.rotationbar.zrot set 60


proc "enable_rotationbar" "" {
}
proc "disable_rotationbar" "" {
}

## parameter list canvas
canvas		.parameters
listbox		.parameters.list -background oldlace -foreground black
scrollbar	.parameters.yscrollbar	-orient v

## configure scrollbars
.parameters.yscrollbar configure -command ".parameters.list yview"
.parameters.list configure -yscrollcommand ".parameters.yscrollbar set"

## bind lists
bind .parameters.list <<ListboxSelect>> {
    set parameter [ .parameters.list get anchor ]
    plot \$parameter
    set select "Select Y"
}

proc "reverse_name" "name" {
    set delim "_"
    set split_name [split \$name \$delim]
    set reversed_name [lindex \$split_name 0]
    for {set i 1} {\$i<[llength \$split_name]} {incr i} {
        set reversed_name [lindex \$split_name \$i]\$delim\$reversed_name
    }
    return \$reversed_name
}

proc "reverse_sort" "list" {
    set temp_list {}
    set delim " "
    foreach name [split \$list \$delim] {
	set temp_list \$temp_list\$delim[reverse_name \$name]
    }
    set temp_list [lsort -ascii \$temp_list]
    set list {}
    foreach name [split \$temp_list \$delim] {
	set list \$list\$delim[reverse_name \$name]
    }
    return \$list
}

proc "sort_list" "list sortorder" {
    switch -- \$sortorder {
	normal	{ return [lsort -ascii \$list] }
	reverse { return [reverse_sort \$list] }
    }
}

EOF

## create states and outputs lists
gawk '
($1 == "state") {
    name=sys"__";
    label=$4;
    sub (name, "", label);
    printf "lappend xl %s\n", label
}
($1 == "output") {
    name=sys"__";
    label=$4;
    sub (name, "", label);
    printf "lappend yl %s\n", label
}' sys=${sys} ${struc} >> ${out}

cat <<EOF >> ${out}
lappend xl Time
lappend yl Time
.parameters.list configure -listvar yl

pack .menubar			-expand false -fill x    -side top
pack .listbar			-expand false -fill x    -side top
pack .rotationbar		-expand false -fill x    -side bottom
pack .plotbar			-expand false -fill x    -side bottom
pack .buttonbar			-expand false -fill x    -side bottom
pack .parameters.yscrollbar	-expand false -fill y    -side right
pack .parameters.list		-expand true  -fill both -side left
pack .parameters		-expand true  -fill both

## map names to column numbers
proc "get_state_column" "title" {
    switch -- \$title {
EOF

	gawk '($1 == "state") {
	    name=sys"__";
	    label=$4;
	    sub (name, "", label);
	    printf "\t\t%s\t{ return %d }\n", label, $2+2+Ny;
	}
	' Ny=${Ny} sys=${sys} ${struc} >> ${out}
	
cat <<EOF >> ${out}
    }
}
    
proc "get_output_column" "title" {
    switch -- \$title {
EOF

	gawk '($1 == "output") {
	    name=sys"__";
	    label=$4;
	    sub (name, "", label);
	    printf "\t\t%s\t{ return %d }\n", label, $2+1;
	}    
	' Ny=${Ny} sys=${sys} ${struc} >> ${out}

cat <<EOF >> ${out}
    }
}

proc "plot" "title" {
    global dimension
    global select
    global view
    global xcolumn
    global ycolumn
    global zcolumn
    global xtitle
    global ytitle
    global ztitle
    if {"\$title" == "Time"} {
	set column 1
    } else {
	switch -- \$view {
	    states	{ set column [get_state_column  \$title] }
	    outputs	{ set column [get_output_column \$title] }
	}
    } 
    switch -- \$select {
	"Select X" {
	    set xcolumn \$column
	    set xtitle  \$title
	}
	"Select Y" {
	    set ycolumn \$column
	    set ytitle  \$title
	}
	"Select Z" {
	    set zcolumn \$column
	    set ztitle  \$title
	    set dimension "3D"
	}
    }
    plot_var
}

## call gnuplot
proc "plot_var" "" {
    global xcolumn
    global ycolumn
    global zcolumn
    global xtitle
    global ytitle
    global ztitle
    global dimension
    global plottype
    global lastplot
    switch -- \$dimension {
	2D	{
	    disable_rotationbar
	    switch -- \$plottype {
		Single	{
		    do_cmd "set xlabel '\$xtitle'"
		    do_cmd "set ylabel '\$ytitle'"
		    do_cmd "set nokey"
		    do_cmd "plot '${dat2}' using \$xcolumn:\$ycolumn title 'y:\$ytitle  x:\$xtitle' "
		}
		Multi	{
		    do_cmd "set xlabel ''"
		    do_cmd "set ylabel ''"
		    do_cmd "set key"
		    if {"\$lastplot" != "2D"} {
			do_cmd "plot '${dat2}' using \$xcolumn:\$ycolumn title 'y:\$ytitle  x:\$xtitle' "
		    } else {
			do_cmd "replot '${dat2}' using \$xcolumn:\$ycolumn title 'y:\$ytitle  x:\$xtitle' "	    
		    }
		}
	    }
	}
	3D	{
	    enable_rotationbar
	    switch -- \$plottype {
		Single	{
		    do_cmd "set xlabel '\$xtitle'"
		    do_cmd "set ylabel '\$ytitle'"
		    do_cmd "set zlabel '\$ztitle'"
		    do_cmd "set nokey"
		    do_cmd "splot '${dat2}' using \$xcolumn:\$ycolumn:\$zcolumn title 'z:\$ztitle  y:\$ytitle  x:\$xtitle' "
		}
		Multi	{
		    do_cmd "set xlabel ''"
		    do_cmd "set ylabel ''"
		    do_cmd "set zlabel ''"
		    do_cmd "set key"
		    if {"\$lastplot" != "3D"} {
			do_cmd "splot '${dat2}' using \$xcolumn:\$ycolumn:\$zcolumn title 'z:\$ztitle  y:\$ytitle  x:\$xtitle' "
		    } else {
			do_cmd "replot '${dat2}' using \$xcolumn:\$ycolumn:\$zcolumn title 'z:\$ztitle  y:\$ytitle  x:\$xtitle' "
		    }
		}
	    }
	}
    }
    set lastplot \$dimension
}
## open gnuplot plot file
proc "open_plotfile" "" {
    global parameter
    if {"\$parameter"==""} {
	set name "${sys}.plt"
    } else {
	set name "\$parameter.plt"
    }
    set filename [tk_getOpenFile\
	-initialdir ".."\
	-initialfile "\$name"\
	-defaultextension ".plt"\
	-filetypes {
	    {{Gnuplot} {.plt}}
	    {{All files} {*}}
	}
    ]
    do_cmd "load '\$filename'"
}

## save gnuplot plot file
proc "save_plotfile" "" {
    global parameter
    if {"\$parameter"==""} {
	set name "${sys}.plt"
    } else {
	set name "\$parameter.plt"
    }
    set filename [tk_getSaveFile\
	-initialdir ".."\
	-initialfile "\$name"\
	-defaultextension ".plt"\
	-filetypes {
	    {{Gnuplot} {.plt}}
	    {{All files} {*}}
	}
    ]
    do_cmd "save '\$filename'"
}

## print output to postscript
proc "print_postscript" "" {
    global parameter
    global windownum
    set filename [tk_getSaveFile\
		      -initialdir ".."\
		      -initialfile "\$parameter.ps"\
		      -defaultextension ".ps"\
		      -filetypes {
			  {{Postscript} {.ps}}
			  {{All files} {*}}
		      }
		 ]
    if {\$filename != ""} {
	do_cmd "set term postscript"
	do_cmd "set output '\$filename'"
	do_cmd "replot"
	do_cmd "set term x11 \$windownum"
	tk_dialog ".printed" "Graph Printed!" "PostScript saved as \$filename" "" "0" "Close"
    }
}

proc "get_value" "string initial" {
    frame .f
    frame .f.buttons
    global get_value_retval
    global get_value_status
    set get_value_retval ""
    set get_value_status ""

    button	.f.buttons.ok		-text "ok"	-command {
	set get_value_status "ok"
	set get_value_retval [.f.entry get ]
	destroy .f
    }
    button	.f.buttons.cancel	-text "Cancel"	-command {
	set get_value_status "cancel"
	destroy .f
    }
    entry	.f.entry
    label	.f.label	-text "\$string"

    .f.entry insert 0 "\$initial"
    bind .f.entry <<Return>> {
	set get_value_status "ok"
	set get_value_retval [.f.entry get ]
	destroy .f
    }

    pack .f.buttons.ok		-side left   -expand false
    pack .f.buttons.cancel	-side left   -expand false
    pack .f.label		-side top    -expand false
    pack .f.entry		-side top    -expand false
    pack .f.buttons		-side top    -expand false
    place .f -in .parameters -relx 0 -rely 0 -relwidth 1 -relheight 1 -anchor nw -bordermode outside
    tkwait variable get_value_retval
    if {"\$get_value_status" == "ok"} {
	return "\$get_value_retval"
    } else {
	return ""
    }
}

proc "get_scale" "string min max" {
    frame .f
    frame .f.buttons
    global get_scale_retval
    global get_scale_status
    set get_scale_retval ""
    set get_scale_status ""

    button	.f.buttons.ok		-text "ok"	-command {
	set get_scale_status "ok"
	set get_scale_retval [.f.scale get ]
	destroy .f
    }
    button	.f.buttons.cancel	-text "Cancel"	-command {
	set get_scale_status "cancel"
	destroy .f
    }
    scale	.f.scale	-orient h	-label "\$string"	-from "\$min"	-to "\$max"

    pack .f.buttons.ok		-side left   -expand false
    pack .f.buttons.cancel	-side left   -expand false
    pack .f.scale		-side top    -expand false
    pack .f.buttons		-side top    -expand false
    place .f -in .parameters -relx 0 -rely 0 -relwidth 1 -relheight 1 -anchor nw -bordermode outside
    tkwait variable get_value_retval
    if {"\$get_scale_status" == "ok"} {
	return "\$get_scale_retval"
    } else {
	return ""
    }
}

proc "do_rotate_3d" "var val" {
    switch -- \$var {
	xrot	{
	    do_cmd "set view \$val,,, ; replot"
	}
	zrot	{
	    do_cmd "set view ,\$val,, ; replot"
	}
    }
}

proc "show_gnuplot_prompt" "" {
    global gnuplot_command
    destroy .gcmd
    canvas .gcmd
    label .gcmd.l -text ">"
    entry .gcmd.e -textvariable gnuplot_command
    button .gcmd.b -text "<-" -relief flat -command { set gnuplot_command "" }
    pack .gcmd.b -side right
    pack .gcmd.l -side left
    pack .gcmd.e -side top    -fill x -expand true
    pack .gcmd -expand false -fill x -side bottom -before .rotationbar
    focus .gcmd.e
    bind .gcmd.e <<Return>> {
	do_cmd "[.gcmd.e get]"
    }
}
proc "hide_gnuplot_prompt" "" {
    destroy .gcmd
}

proc "do_cmd" "string" {
    global gnuplot_command
    set gnuplot_command "\$string"
    puts "\$string"
}

event add <<Return>> <Return>
    
# Defaults
set parameter ""
set windownum 0
set view outputs
set lastplot ""
set select "Select Y"
set xcolumn 1 ; set xtitle "Time"
set ycolumn 1 ; set ytitle "Time"
set zcolumn 1 ; set ztitle "Time"

do_cmd "set mouse"
do_cmd "set timestamp"
do_cmd "set grid"
do_cmd "set nopolar"
do_cmd "set term X11 0"
do_cmd "set key"
do_cmd "set data style lines"

EOF
chmod +x ${out}


MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]