File mttroot/mtt/bin/trans/struc2gnuplot_txt2wish artifact 0b8b18566d part of check-in 14e7fa093e


#! /bin/sh

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
# Next line replaces shell with wish\\
exec wish "\$0" "\$@"

## main window
wm title . "MTT Viewer: ${sys}"
set parameter ""
set windownum 0

# menu bar
canvas .menubar

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

button .menubar.xmtt  -text "xMTT" -relief flat		 -command { exec xmtt }
button .menubar.quit  -text "Quit" -relief flat		 -command exit 

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

# status bar
canvas .statusbar

radiobutton .statusbar.states  -text "States"      -value states  -variable view      -relief solid -command { raise .cx }
radiobutton .statusbar.outputs -text "Outputs"     -value outputs -variable view      -relief solid -command { raise .cy }

tk_optionMenu .statusbar.plot  plottype "Single Plot" "Multi Plot" "New Window"
tk_optionMenu .statusbar.grid  grid     "Grid On" "Grid Off"
tk_optionMenu .statusbar.coord coord	"Cartesian" "Polar"

pack .statusbar.states  -expand false -fill x -side left
pack .statusbar.outputs -expand false -fill x -side left
pack .statusbar.plot    -expand false -fill x -side right
pack .statusbar.coord   -expand false -fill x -side right
pack .statusbar.grid    -expand false -fill x -side right

## states canvas
canvas		.cx
listbox		.cx.l
scrollbar	.cx.sy	-orient v
checkbutton    	.cx.t	-text		"States"	-relief flat

## outputs canvas
canvas		.cy
listbox		.cy.l
scrollbar	.cy.sy	-orient v
checkbutton    	.cy.t	-text		"Outputs"	-relief flat

## configure scrollbars
.cx.sy configure -command ".cx.l yview"
.cy.sy configure -command ".cy.l yview"
.cx.l configure -yscrollcommand ".cx.sy set"
.cy.l configure -yscrollcommand ".cy.sy set"

## bind lists
bind .cx.l <<ListboxSelect>> { set parameter [ .cx.l get anchor ] ; plot \$parameter }
bind .cy.l <<ListboxSelect>> { set parameter [ .cy.l get anchor ] ; plot \$parameter }

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" {
    if ("\$sortorder"=="normal") {
	return [lsort -ascii \$list]
    }
    if ("\$sortorder"=="reverse") {
	return [reverse_sort \$list]
    }
}

EOF

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

cat <<EOF >> ${out}
.cx.l configure -listvar xl
.cy.l configure -listvar yl

.cx.t configure -command { set xl [sort_list \$xl \$xsortorder] } -indicatoron false -onvalue "reverse" -offvalue "normal" -variable xsortorder
.cy.t configure -command { set yl [sort_list \$yl \$ysortorder] } -indicatoron false -onvalue "reverse" -offvalue "normal" -variable ysortorder

pack .menubar	      -expand false -fill x -side top
pack .statusbar	      -expand false -fill x -side bottom

pack .cx.t -expand false -fill x -side top
pack .cy.t -expand false -fill x -side top

pack .cx.sy -expand false -fill y -side right
pack .cy.sy -expand false -fill y -side right

pack .cx.l -expand true -fill both -side left
pack .cy.l -expand true -fill both -side left

pack .cx -expand true -fill both
place .cy -in .cx -relx 0 -rely 0 -relwidth 1 -relheight 1 -anchor nw -bordermode outside

## map names to column numbers
EOF

gawk '
BEGIN {
    print "proc \"plot\" \"title\" {";
}
($1 == "state") {
    printf "if (\"%s\"==\"state:%s\") { plot_var \"%s\" %d }\n", Title, $4, $4, $2+2+Ny;
}
($1 == "output") {
    printf "if (\"%s\"==\"output:%s\") { plot_var \"%s\" %d }\n", Title, $4, $4, $2+1;
}    
END {
    print  "}"
}
' Title=\$title Ny=${Ny} ${struc} >> ${out}


cat <<EOF >> ${out}

## call gnuplot
proc "plot_var" "title column" {
    set_plot_options
    puts "set term X11"
    global plottype
    if {"\$plottype" == "Multi Plot"} {
	set plot "replot"
    } elseif {"\$plottype" == "New Window"} {
	global windownum
	set windownum [ expr \$windownum + 1 ]
	puts "set terminal x11 \$windownum"
	set plot "plot"
    } else {
	set plot "plot"
    }
    puts "\$plot '${dat2}' using 1:\$column title '\$title' with lines"
}

proc "set_plot_options" "" {
    global coord;			# "Cartesian", "Polar"
    global grid;			# "Grid On", "Grid Off"
    if {"\$coord" == "Cartesian"} {
	puts "set nopolar"
    } else {
	puts "set polar"
    }
    if {"\$grid" == "Grid On"} {
	puts "set grid"
    } else {
	puts "set nogrid"
    }
}

## 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} {*}}
	}
    ]
    puts "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} {*}}
	}
    ]
    puts "save '\$filename'"
}

## print output to postscript
proc "print_postscript" "" {
    global parameter;
    if {"\$parameter"==""} {
	tk_dialog ".mtt_Print_Error" "Print Error" "Select a parameter!" "" "0" "Try Again"
    } else {
	set filename [tk_getSaveFile\
		-initialdir ".."\
		-initialfile "\$parameter.ps"\
		-defaultextension ".ps"\
		-filetypes {
		    {{Postscript} {.ps}}
		    {{All files} {*}}
		}
	]
	if {\$filename != ""} {
	    puts "set term postscript"
	    puts "set output '\$filename'"
	    puts "replot"
	    tk_dialog ".printed" "Graph Printed!" "PostScript saved as \$filename" "" "0" "Close"
	}
    }
}

EOF
chmod +x ${out}


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