Ignore:
Timestamp:
Jul 26, 2011 12:47:57 PM (10 years ago)
Author:
toby
Message:

liveplot: export to gnuplot; plot I/sigma(I); change title & size of labels; improve tick location; make autoticks standard; expgui: new menu item to create InstParm? file; InstEdit?: bug fixes; remove fprime since not in GSAS anymore; fix 2Theta max bug for CW instruments; updates to doc files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sandbox/liveplot

    r1144 r1157  
    1414# process command line arguments
    1515set exitstat 0
    16 set expnam [lindex $argv 0]
     16set expnam [file root [lindex $argv 0]]
    1717if {$expnam == ""} {catch {puts "error -- no experiment name"}; set exitstat 1}
    1818if $exitstat {
     
    2020    exit
    2121}
    22 
     22# work in .EXP directory (in case files will be exported).
     23catch {
     24    cd [file dirname $expnam]
     25}
    2326# get name of script
    2427set expgui(script) [info script]
     
    4245    set graph(legend) [lindex $argv 2]
    4346}
     47set plot_title "(None)"
    4448
    4549set graph(backsub) 0
     
    7983set expgui(fadetime) 10
    8084set expgui(hklbox) 1
    81 set expgui(autotick) 0
     85set expgui(autotick) 1
    8286set expgui(phaselabel) 0
    8387set expgui(pixelregion) 5
     
    358362    }
    359363    ApplyMag
    360     set maxdiff  [set diffvec(max)]
    361     set cmin [set calcvec(min)]
    362     set omin [set obsvec(min)]
    363     set cmax [set calcvec(max)]
    364     set omax [set obsvec(max)]
    365     set expgui(min) [expr {$omin < $cmin ? $omin : $cmin}]
    366     set expgui(max) [expr {$omax > $cmax ? $omax : $cmax}]
    367     set ymin1 [expr {$cmin - 1.1*$maxdiff}]
    368     set ymin2 [expr {$omin - 1.1*$maxdiff}]
    369     if {$ymin1 < $ymin2} {
    370         diffvec set [diffvec + $ymin1]
    371     } {
    372         diffvec set [diffvec + $ymin2]
    373     }
    374364    plotdata
    375365}
     
    454444    }
    455445    ApplyMag
    456     global obsvec calcvec diffvec
    457     set maxdiff  [set diffvec(max)]
    458     set cmin [set calcvec(min)]
    459     set omin [set obsvec(min)]
    460     set cmax [set calcvec(max)]
    461     set omax [set obsvec(max)]
    462     set expgui(min) [expr {$omin < $cmin ? $omin : $cmin}]
    463     set expgui(max) [expr {$omax > $cmax ? $omax : $cmax}]
    464     set ymin1 [expr {$cmin - 1.1*$maxdiff}]
    465     set ymin2 [expr {$omin - 1.1*$maxdiff}]
    466     if {$ymin1 < $ymin2} {
    467         diffvec set [diffvec + $ymin1]
    468     } {
    469         diffvec set [diffvec + $ymin2]
    470     }
    471    
     446    plotdata
     447}
     448
     449proc SetTitle {} {
     450    set bx .title
     451    toplevel $bx
     452    wm title $bx "Edit plot title"
     453    wm iconname $bx "Edit plot title"
     454    grid [label $bx.txt -text "Enter plot title below"] -row 1 -column 1
     455    grid [entry $bx.title -width 70] -row 2 -column 1
     456    grid [frame $bx.a] -row 3 -column 1
     457    grid [button $bx.a.save -text "Save" \
     458              -command "set plot_title \"\[$bx.title get\]\"; destroy $bx" \
     459             ] -row 1 -column 1
     460    grid [button $bx.a.quit -text "Quit" \
     461              -command "destroy $bx" \
     462             ] -row 1 -column 2
     463    putontop $bx
     464    tkwait window $bx
     465    afterputontop
    472466    plotdata
    473467}
     
    575569    global expnam hst peakinfo xunits yunits cycle reflns modtime
    576570    global lasthst graph expgui box
     571    global obsvec calcvec diffvec
     572
     573    if {$graph(yunits) == 2} {
     574        set yunits {Intensity/sigma(I)}
     575        wifdvec set $::weightlist
     576        wifdvec expr sqrt(wifdvec)
     577        obsvec set [obsvec * wifdvec]
     578        calcvec set [calcvec * wifdvec]
     579        bckvec set {}
     580        diffvec set [obsvec - calcvec]
     581        wifdvec set {}
     582        $box element config 1 -label {} -line 0 -hide 1
     583    } else {
     584        $box element config 1 -label bckgr -line 1 -hide 0
     585    }
     586
     587    set cmin [set calcvec(min)]
     588    set omin [set obsvec(min)]
     589    set cmax [set calcvec(max)]
     590    set omax [set obsvec(max)]
     591    set expgui(min) [expr {$omin < $cmin ? $omin : $cmin}]
     592    set expgui(max) [expr {$omax > $cmax ? $omax : $cmax}]
    577593
    578594    foreach p $::expmap(phaselist) {
     
    595611        return
    596612    }
    597     $box config -title "$expnam cycle $cycle Hist $hst"
     613    if {$::plot_title == "(None)"} {
     614        $box config -title "$expnam cycle $cycle Hist $hst"
     615    } else {
     616        $box config -title $::plot_title
     617    }
    598618    $box xaxis config -title $xunits
    599619    $box yaxis config -title $yunits
     
    625645    calcvec notify now
    626646    bckvec notify now
    627     diffvec notify now
    628647    wifdvec notify now
    629648    # now deal with peaks
    630     for {set i 1} {$i < 10} {incr i} {
    631         if {$expgui(autotick)} {
    632             set div [expr {( $expgui(max) - $expgui(min) )/40.}]
    633             set ymin [expr {$expgui(min) - ($i+1) * $div}]
    634             set ymax [expr {$expgui(min) - $i * $div}]
    635         } else {
    636             set ymin $peakinfo(min$i)
    637             set ymax $peakinfo(max$i)
    638         }
    639         set j 0
    640         if [set peakinfo(flag$i)] {
    641             foreach X $reflns($i) {
    642                 incr j
    643                 catch {
    644                     $box marker create line -name peaks${i}_$j
    645                 }
    646                 $box marker config peaks${i}_$j  -under 1 \
    647                         -coords "$X $ymin $X $ymax"
    648                 catch {
    649                     $box marker config peaks${i}_$j \
    650                             $graph(MarkerColorOpt) [list $peakinfo(color$i)]
    651                     if {$peakinfo(dashes$i) && ! $expgui(autotick)} {
    652                         $box marker config peaks${i}_$j -dashes "5 5"
    653                     }
    654                 }
     649    #for {set i 1} {$i < 10} {incr i}
     650    set k 0
     651    set cmin $expgui(min)
     652    foreach i $::expmap(phaselist) {
     653        if {[set peakinfo(flag$i)]} {
     654            incr k
     655            if {$expgui(autotick)} {
     656                set div [expr {( $expgui(max) - $expgui(min) )/40.}]
     657                set ymin [expr {$expgui(min) - ($k+1) * $div}]
     658                set ymax [expr {$expgui(min) - $k * $div}]
     659                if {$cmin > $ymin} {set cmin $ymin}
     660            } else {
     661                set ymin $peakinfo(min$i)
     662                set ymax $peakinfo(max$i)
     663            }
     664            set j 0
     665            foreach X $reflns($i) {
     666                incr j
     667                catch {
     668                    $box marker create line -name peaks${i}_$j
     669                }
     670                $box marker config peaks${i}_$j  -under 1 \
     671                    -coords "$X $ymin $X $ymax"
     672                catch {
     673                    $box marker config peaks${i}_$j \
     674                        $graph(MarkerColorOpt) [list $peakinfo(color$i)]
     675                    if {$peakinfo(dashes$i) && ! $expgui(autotick)} {
     676                        $box marker config peaks${i}_$j -dashes "5 5"
     677                    }
     678                }
    655679            }
    656680            catch {$box element create phase$i}
    657681            catch {
    658682                $box element config phase$i -color $peakinfo(color$i) \
    659                         -label $graph(label$i)
     683                    -label $graph(label$i)
    660684            }
    661685        } else {
     
    664688        }
    665689    }
     690    # offset the difference pattern
     691    set maxdiff  [set diffvec(max)]
     692    if {$expgui(autotick)} {
     693        diffvec set [diffvec + [expr {$cmin - $maxdiff}]]
     694    } else {
     695        diffvec set [diffvec + [expr {$cmin - 1.1*$maxdiff}]]
     696    }
     697    diffvec notify now
     698
    666699    # force an update of the plot as BLT may not
    667700    $box config -title [$box cget -title]
     
    11251158        eval $box y2axis config $graph(ElementHideOption)
    11261159        $box element config 0 -label ""
     1160        # clear the vector
     1161        wifdvec set {}
     1162        wifdvec notify now
    11271163    }
    11281164}
     
    20492085setlegend $box $graph(legend)
    20502086
     2087set ps2pdfcmd {}
     2088set gnuplotpath {}
     2089# find gnuplot
     2090if {$::tcl_platform(platform) == "windows"} {
     2091    set gnuplotpath [file join $::expgui(gsasexe) "gnuplot/gnuplot.exe"]
     2092    if {![file exists $gnuplotpath]} {
     2093        set gnuplotpath [auto_execok gnuplot.exe]
     2094    }
     2095} else {
     2096    set ::env(PATH) "${::expgui(gsasexe)}:${::env(PATH)}"
     2097    auto_reset
     2098    set gnuplotpath [auto_execok gnuplot]
     2099}
     2100# find postscript conversion routine
     2101if {$::tcl_platform(platform) == "windows"} {
     2102    set gspath [file join $command(exedir) "gs/bin/gswin32c.exe"]
     2103    if {![file exists $gspath]} {
     2104        set gspath [auto_execok gswin32c.exe]
     2105    }
     2106    if {[file exists $gspath]} {
     2107        set ps2pdfcmd "$gspath -q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\"\$pdfoutname\" -c save pop -f \"\$psoutname\""
     2108    }
     2109} else {
     2110    # look for ghostscript
     2111    set gspath [auto_execok gs]
     2112    if {[file exists $gspath]} {
     2113        set ps2pdfcmd "$gspath -q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\$pdfoutname -c save pop -f \$psoutname"
     2114    } else {
     2115        set gspath [auto_execok pstopdf]; # this is on Mac only
     2116        if {[file exists $gspath]} {
     2117            set ps2pdfcmd "$gspath \$psoutname -o \$pdfoutname"
     2118        }
     2119    }
     2120}
     2121set gnuplotexport {}
     2122catch {
     2123    source [file join $expgui(scriptdir) gnuplot.tcl]
     2124}
     2125
     2126#======================================================================
     2127# make menus
    20512128frame .a -bd 3 -relief groove
    20522129pack [menubutton .a.file -text File -underline 0 -menu .a.file.menu] -side left
     
    20572134.a.file.menu add command -label "Update Plot" \
    20582135        -command {set cycle [getcycle];readdata .g}
     2136.a.file.menu add command -label "Set Plot Title" \
     2137        -command SetTitle
    20592138.a.file.menu add cascade -label "Export plot" -menu .a.file.menu.export
    20602139menu .a.file.menu.export
     
    20652144    .a.file.menu.export add command -label "to Grace" -command exportgrace
    20662145}
     2146if {$gnuplotpath == {}} {
     2147    .a.file.menu.export add cascade -label "gnuplot program not found"
     2148} elseif {$gnuplotexport == ""} {
     2149    .a.file.menu.export add cascade -label "file gnuplot.tcl not found"
     2150} elseif {$ps2pdfcmd == ""} {
     2151    .a.file.menu.export add cascade -label "gs or pstopdf program not found"
     2152} else {
     2153    .a.file.menu.export add command -label "to PDF" -command ExportPDF
     2154}
     2155
    20672156.a.file.menu add command -label Quit -command "destroy ."
    20682157.a.file.menu.export add command -label "as .csv file" \
     
    20972186set maglist {}
    20982187set mag0 1.0
    2099 # display magnification routines -- not currently in use
     2188# display magnification routines
    21002189proc ShowMag {} {
    21012190    global blt_version tcl_platform peakinfo expgui
     
    22802369}
    22812370
     2371# create a PDF using GNUPLOT & GhostScript
     2372proc ExportPDF {} {
     2373    set msg {}
     2374    if {$::gnuplotpath == {}} {
     2375        append msg "\n\tThe gnuplot program was not found"
     2376    }
     2377    if {$::gnuplotexport == ""} {
     2378        append msg "\n\tfile gnuplot.tcl not found in $::expgui(scriptdir)"
     2379    }
     2380    if {$::ps2pdfcmd == ""} {
     2381        append msg "\n\tNo PDF conversion routine (ghostscript or pstopdf on Mac was found"
     2382    }
     2383    if {$msg != ""} {
     2384        MyMessageBox -parent . -title "Gnuplot/GS Error" \
     2385                -message $msg \
     2386                -icon error -type OK -default OK
     2387        return 0
     2388    }
     2389
     2390    set pdfoutname [tk_getSaveFile -title "Select output file" -parent . \
     2391                        -defaultextension ".pdf" \
     2392                        -initialdir [pwd] \
     2393                        -initialfile "[file rootname $::expnam].pdf"]
     2394    if { $pdfoutname == "" } { return }
     2395    set rootname [file root $pdfoutname]
     2396    set csvoutname "$rootname.csv"
     2397    set gplotname "$rootname.gplot"
     2398    set psoutname "$rootname.ps"
     2399    foreach filename [list $pdfoutname $csvoutname $gplotname $psoutname] {
     2400        catch {file -force delete $filename}
     2401    }
     2402    waitmsg "running gnuplot, Please wait"
     2403    Graph2Gnuplot .g $gplotname $psoutname $csvoutname
     2404    if {![file exists $gplotname]} {
     2405        set msg "\n  Output file for gnuplot not created! ($gplotname)"
     2406    }
     2407    if {![file exists $csvoutname]} {
     2408        append msg "\n   Output file for gnuplot not created! ($csvoutname)"
     2409    }
     2410    if {$msg == ""} {
     2411        if {[catch { exec $::gnuplotpath $gplotname } err]} {
     2412            append msg "Error in running gnuplot: $err"
     2413        }
     2414        if {![file exists $psoutname]} {
     2415            append msg "\n   Output file from gnuplot not created! ($psoutname)"
     2416        }
     2417    }
     2418    if {$msg == ""} {
     2419        if {[catch { eval exec [subst $::ps2pdfcmd]} err]} {
     2420            append msg "Error in PDF conversion: $err"
     2421        }
     2422        if {![file exists $pdfoutname]} {
     2423            append msg "\n   PDF file not created! ($pdfoutname)"
     2424        }
     2425    }
     2426    donewaitmsg
     2427    if {$msg == ""} {
     2428        # clean up
     2429        foreach filename [list $csvoutname $gplotname $psoutname] {
     2430            catch {file -force delete $filename}
     2431        }
     2432        MyMessageBox -parent . -title "PDF created" \
     2433            -message "Success: PDF file created ($pdfoutname)" \
     2434            -type OK -default OK
     2435        return 1
     2436    } else {
     2437        # something failed
     2438        MyMessageBox -parent . -title "Gnuplot/GS Error" \
     2439                -message "Error: $msg" \
     2440                -icon error -type OK -default OK
     2441        return 0
     2442    }
     2443}
     2444
     2445# resize graph font only
     2446proc ResizeGraphFont {graph font} {
     2447    foreach w {legend xaxis yaxis xaxis yaxis \
     2448                   x2axis y2axis x2axis y2axis} \
     2449        o {-font -tickfont -tickfont -titlefont -titlefont \
     2450               -tickfont -tickfont -titlefont -titlefont} {
     2451                   catch {
     2452                       $graph $w configure $o $font
     2453                   }
     2454               }
     2455    catch {
     2456        set curfont [$graph cget -font]
     2457        if {[string tolower [lindex $curfont 0]] == "symbol"} {
     2458            $graph configure -font "Symbol [lrange $font 1 end]"
     2459        } else {
     2460            $graph configure -font $font
     2461        }
     2462    }
     2463}
     2464
    22822465if {$program == "liveplot"} {
    22832466    .a.mag.menu add command -label "Add region" -command "AddMag"
     
    23302513            -variable graph(yunits) -value 1 \
    23312514            -command {set cycle [getcycle];readdata .g}
     2515    .a.options.menu.yunits add radiobutton -label "I/Sigma(I)" \
     2516            -variable graph(yunits) -value 2 \
     2517            -command {set cycle [getcycle];readdata .g}
    23322518    .a.options.menu add command -label "HKL labeling" -command setlblopts
    23332519    .a.options.menu add checkbutton -label "Subtract background" \
     
    23542540}
    23552541if {$program == "liveplot"} {
     2542    .a.options.menu add cascade -menu  .a.options.menu.gfont \
     2543        -label "Graph font"
     2544    menu .a.options.menu.gfont
     2545    foreach f {12 14 16 18 20 22 24 26 28 32 36 40} {
     2546        .a.options.menu.gfont add radiobutton \
     2547            -command "ResizeGraphFont .g \[list Helvetica -$f\]" \
     2548            -label $f -font "Helvetica -$f"
     2549    }
    23562550    .a.options.menu add checkbutton -label "Raise on update" \
    23572551            -variable graph(autoraise)
     
    25132707expload $expnam.EXP
    25142708mapexp
    2515 
     2709if {$expgui(autotick)} {
     2710    foreach i $::expmap(phaselist) {
     2711        set peakinfo(flag$i) 1
     2712    }
     2713}
     2714       
    25162715# fill the histogram menu
    25172716if {[llength $expmap(powderlist)] > 15} {
Note: See TracChangeset for help on using the changeset viewer.