2 # @file bshow_param.tcl
4 # @brief Procedures to read and write parameter files
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20200615
14 set project_filter "*"
28 ## @brief Menu for opening and closing parameter files
31 proc setupParameterMenu {} {
34 .menuBar add cascade -menu .menuBar.param -label "Micrograph" -underline 0
35 menu .menuBar.param -tearoff 0
36 .menuBar.param add command -label "Read parameters" \
37 -command { openParam } \
38 -underline 0 -accelerator "Ctrl-r"
39 .menuBar.param add command -label "Append parameters" \
40 -command { appendParam } \
41 -underline 0 -accelerator "Ctrl-a"
42 .menuBar.param add command -label "Write parameters" \
43 -command { writeParam } \
44 -underline 0 -accelerator "Ctrl-w"
45 .menuBar.param add command -label "Edit parameters" \
46 -command { editParam } \
47 -underline 0 -accelerator "Ctrl-e"
48 .menuBar.param add command -label "Sorted micrographs" \
49 -command { sortedMgTable } \
51 .menuBar.param add command -label "Change image" \
52 -command { selectAFile } \
53 -underline 0 -accelerator "Ctrl-a"
54 .menuBar.param add command -label "Calculate lambda" \
55 -command { calculateLambda } \
56 -underline 0 -accelerator "Ctrl-l"
57 .menuBar.param add command -label "Fit CTF" \
58 -command { CTF $theimg } \
59 -underline 0 -accelerator "Ctrl-c"
60 .menuBar.param add command -label "Particles" \
61 -command { Particles } \
62 -underline 0 -accelerator "Ctrl-p"
63 .menuBar.param add command -label "Filaments" \
64 -command { Filaments } \
65 -underline 0 -accelerator "Ctrl-f"
66 .menuBar.param add command -label "Helix" \
68 -underline 0 -accelerator "Ctrl-h"
69 .menuBar.param add command -label "Crystallography" \
70 -command { Crystallography } \
71 -underline 0 -accelerator "Ctrl-x"
72 .menuBar.param add command -label "Tomography" \
73 -command { Tomography } \
74 -underline 0 -accelerator "Ctrl-t"
78 ## @brief Opens a micrograph parameter file for reading.
82 global paramfile paramdir
84 if { [string length $currdir] < 2 } { set currdir [pwd] }
86 {"STAR files" {.star .STAR} }
87 {"XML files" {.xml .XML} }
88 {"EMX files" {.emx .EMX} }
90 set paramfile [tk_getOpenFile -title "Open parameter file" -filetypes $types \
91 -initialdir $currdir -defaultextension .star]
92 if { [string length $paramfile] < 1 } { return }
97 ## @brief Opens a micrograph parameter file for reading.
99 # @param file parameter file name.
101 proc loadParam { file } {
102 global paramfile paramdir
104 if ![file exists $file] {
105 tk_messageBox -message "Error: File $file not found" -type ok
108 set project_item "all"
111 set paramdir [file dirname $paramfile]
112 set paramfile [file tail $paramfile]
113 # puts "$paramdir $paramfile"
117 ## @brief Appends one or more micrograph parameter files.
120 proc appendParam { } {
121 global paramfile paramdir
122 set currdir $paramdir
123 if { [string length $currdir] < 2 } { set currdir [pwd] }
125 {"STAR files" {.star .STAR} }
126 {"XML files" {.xml .XML} }
127 {"EMX files" {.emx .EMX} }
129 set addfiles [tk_getOpenFile -title "Append parameter files" -filetypes $types \
130 -multiple true -initialdir $currdir -defaultextension .star]
131 if { [string length $addfiles] < 1 } { return }
133 # loadParam $paramfile
138 proc tag_line { w } {
139 set cur [$w index current]
141 $w tag add seline "$cur linestart" "$cur lineend"
142 $w tag configure seline -background yellow
145 ## @brief Checks if the parameter file exist, otherwise save it.
148 proc checkParamFile { } {
149 global paramfile paramdir
150 puts "checkParamFile: $paramfile $paramdir"
151 if { ![file exists $paramfile] } {
152 set pf "$paramdir/$paramfile"
153 if { ![file exists $pf] } {
159 ## @brief Checks parameter file name and constructs new file name.
162 proc newParamFile { suffix } {
163 global paramfile project_root
164 if [string length $project_root] {
165 set newfile $project_root
167 set newfile [file rootname $paramfile]
169 if ![string length $newfile] {
170 tk_messageBox -message "Please write a parameter file first" -type ok
172 append newfile $suffix
177 proc setupFileList { w img_files } {
178 global filename project_filter
179 set project_filter [$w.filter.e get]
180 $w.file.list delete 1.0 end
181 foreach name $img_files {
182 if [string match "*$project_filter*" $name] {
183 $w.file.list insert end "$name"
184 # puts "$filename $name"
185 if [string length $filename] {
186 if [regexp $filename $name] {
188 tag_line $w.file.list
192 $w.file.list insert end "\n"
195 $w.file.list delete [$w.file.list index current] end
196 if ![string length $filename] {
197 $w.file.list mark set current 1.0
198 tag_line $w.file.list
200 # puts [$w.file.list index current]
203 ## @brief Dialog box to select an image file.
206 proc selectAFile { } {
207 global filename imgtype
208 global project_item project_filter
209 if { [Bmg exists] } {
210 set img_files [Bmg get all image_filenames]
211 } elseif { [Bmodel exists] } {
212 set img_files [Bmodel get all image_filenames]
217 set oldfilename $filename
218 if { [llength $img_files] < 1 } {
219 puts "Error: No image files found!"
220 if { [Bmodel exists] } {
221 Bmodel set all map $filename
223 } elseif { [llength $img_files] < 2 } {
224 set list [split $img_files ":"]
225 set filename [lindex $list 0]
226 set imgtype [lindex $list 1]
227 set project_item "[lindex $list 2]:[lindex $list 3]"
228 # puts "$filename ($imgtype) $project_item"
233 wm title $w "Select an image"
234 wm iconname $w "Select"
236 label $w.msg -justify left \
237 -text "Click on the desired file name and then on Select"
238 pack $w.msg -side top -fill x -pady 2m
241 scrollbar $w.file.xscroll -command "$w.file.list xview" -orient horizontal
242 scrollbar $w.file.yscroll -command "$w.file.list yview"
243 text $w.file.list -width 80 -height 20 -relief sunken -borderwidth 3 \
244 -xscrollcommand "$w.file.xscroll set" \
245 -yscrollcommand "$w.file.yscroll set" -wrap none
246 pack $w.file.xscroll -side bottom -expand 0 -fill x
247 pack $w.file.yscroll -side right -expand 0 -fill y
248 pack $w.file.list -side left -expand 1 -fill both
249 pack $w.file -side top -fill both -pady 2m -expand 1
252 labelframe $w.filter -text "File filter" -labelanchor nw
254 $w.filter.e insert 0 $project_filter
255 pack $w.filter.e -side left -expand yes -fill x
258 button $w.buttons.ok -text Select -command "setSelectionAndDestroy $w"
259 button $w.buttons.cancel -text Cancel -command { destroy .wsel }
260 pack $w.filter -side bottom -fill x -padx 2 -pady 2
261 pack $w.buttons -side bottom -fill x -pady 2m
262 pack $w.filter -side top -expand 1
263 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
265 setupFileList $w $img_files
267 bind $w.filter.e <Return> "setupFileList $w {$img_files}"
268 bind $w.file.list <1> "tag_line $w.file.list"
269 # bind $w.file.list <Double-1> "setSelectionAndDestroy $w"
270 # bind $w.file.list <Double-1> {
271 # tag_line $w.file.list
272 # setSelectionAndDestroy $w
278 # puts "$oldfilename --- $filename"
279 # puts "$filename ($imgtype) $project_item"
280 if { [string compare $oldfilename $filename] != 0 } {
281 loadImage $filename -1
283 updateParametersAndImage
286 proc setSelectionAndDestroy { w } {
290 set item [$w.file.list get -- seline.first seline.last]
291 set list [split $item ":"]
292 set filename [lindex $list 0]
293 set imgtype [lindex $list 1]
294 set project_item "[lindex $list 2]:[lindex $list 3]"
295 # puts "$filename ($imgtype) $project_item"
299 proc updateClassParam { } {
300 global project_item theimg
301 if [regexp "Class" $project_item] {
302 set n [Bimage get $theimg nimages]
303 for { set i 0 } { $i < $n } { incr i } {
304 Bimage set $theimg select $i [Bmg get $project_item select [expr $i + 1]]
309 proc updateParametersAndImage { } {
310 global filename paramfile modelfile
311 global theimg imgtype
312 global project_item mg_sel
314 # puts "Updating parameters and image: $project_item"
316 if { [Bmg exists] } {
318 if [regexp "Reconstruction" $project_item] { Bmg set all active 1 }
319 if { $imgtype == "frame" } {
320 if { [Bimage get $theimg nslices] > 1 } { switchImage }
322 set wc [getControlWindow $theimg]
323 set n [$wc.image.scale get]
324 set mg_item [micrographItem $n]
325 set mg_sel [Bmg get $mg_item select]
326 wm title . [list $filename ":" $paramfile]
327 } elseif { [Bmodel exists] } {
328 wm title . [list $filename ":" $modelfile]
331 # Bimage set $theimg label $project_item
335 if { [Bmg exists] } {
344 } elseif { [Bmodel exists] } {
349 proc getNeighborImageOfType { dir } {
351 global theimg imgtype
354 set wc [getControlWindow $theimg]
355 set img_num [expr [$wc.image.scale get] + $dir]
356 set img_max [$wc.image.scale cget -to]
358 if { $img_num >= 0 && $img_num <= $img_max } {
359 $wc.image.scale set $img_num
360 updateParametersAndImage
364 if { [Bmg exists] } {
365 set img_files [Bmg get all image_filenames $imgtype]
366 } elseif { [Bmodel exists] } {
367 set img_files [Bmodel get all image_filenames]
376 foreach name $img_files {
377 set list [split $name ":"]
378 if [string equal $imgtype [lindex $list 1]] {
379 set f [lindex $list 0]
383 if [string equal $filename $f] {
398 if [string length $f] {
399 if { [lindex $list 2] == "Micrograph" } {
400 loadMicrograph [lindex $list 3]
403 set project_item "[lindex $list 2]:[lindex $list 3]"
404 loadImage $filename -1
405 updateParametersAndImage
410 proc getNeighborImageOfType2 { dir } {
412 global theimg imgtype
415 set wc [getControlWindow $theimg]
416 set img_num [expr [$wc.image.scale get] + $dir]
418 if { [Bmg exists] } {
419 set img_files [Bmg get all image_filenames]
420 } elseif { [Bmodel exists] } {
421 set img_files [Bmodel get all image_filenames]
432 foreach name $img_files {
433 set list [split $name ":"]
435 if [string equal $imgtype [lindex $list 1]] {
436 set f [lindex $list 0]
437 set curr_item "[lindex $list 2]:[lindex $list 3]"
442 if [string equal $project_item $curr_item] {
447 if [string length $prevfile] { set f $prevfile }
448 if [string length $prevlist] { set list $prevlist }
457 if [string length $f] {
458 set project_item "[lindex $list 2]:[lindex $list 3]"
459 if [string equal $filename $f] {
462 loadImage $filename -1
464 $wc.image.scale set $img_num
465 updateParametersAndImage
469 ## @brief Sets up micrograph or reconstruction id and total.
471 # @param w window in which to set the parameters.
473 proc setupItemID { w } {
476 set idx [Bmg get $project_item index_of_item]
477 set nmg [Bmg get all number_of_item]
479 if ![winfo exists $w] {
483 if [regexp "Reconstruction" $project_item] {
484 label $w.tag -text "Reconstruction" -anchor e
486 label $w.tag -text "Micrograph" -anchor e
489 label $w.str -width 50 -text $project_item -relief sunken -anchor w
490 entry $w.idx -width 5 -validate key -vcmd { string is integer %P }
492 label $w.nmg -width 5 -text " / $nmg" -anchor w
494 pack $w.tag $w.str $w.idx $w.nmg -side left -padx 2
496 bind $w.idx <Return> { setMicrograph %W }
499 ## @brief Updates micrograph or reconstruction id.
501 # @param w window in which to set the parameters.
503 proc updateItemID { w } {
504 global project_item theimg
505 if ![winfo exists $w] { return }
506 if [regexp "Micrograph" $project_item] {
507 set item $project_item
508 } elseif [regexp "Reconstruction" $project_item] {
509 set item $project_item
511 set wc [getControlWindow $theimg]
512 set n [$wc.image.scale get]
513 set item [micrographItem $n]
515 set idx [Bmg get $item index_of_item]
516 # puts "$project_item $item $idx"
517 $w.str config -text "$item"
522 ## @brief Loads a micrograph based on id.
524 # @param id micrograph id .
526 proc loadMicrograph { id } {
527 global project_item theimg filename imgtype
528 set oldfilename $filename
529 set mg_item "Micrograph:$id"
530 if [regexp "Micrograph" $project_item] {
531 set project_item $mg_item
533 set filename [Bmg get $mg_item filename $imgtype]
534 # puts "$mg_item $filename"
535 if { [string compare $oldfilename $filename] != 0 } {
536 loadImage $filename -1
538 if { $imgtype == "mg" } {
539 set n [Bmg get $mg_item img_num]
540 set wc [getControlWindow $theimg]
541 $wc.image.scale set $n
543 updateParametersAndImage
547 ## @brief Sets up micrograph navigation and selection buttons.
549 # @param w window in which to set up the buttons.
550 # @param typelabel type of navigation (micrograph).
552 proc setupMicrographButtons { w typelabel } {
555 if ![winfo exists $w] {
559 button $w.prev -text "Previous $typelabel" \
560 -relief raised -command "getNeighborImageOfType -1"
561 button $w.next -text "Next $typelabel" \
562 -relief raised -command "getNeighborImageOfType 1"
563 checkbutton $w.select -text "Select" -variable mg_sel \
564 -command { selectMicrograph }
565 pack $w.prev $w.next $w.select \
566 -side left -pady 2 -padx 2 -ipadx 2 -ipady 2
569 ## @brief Sets up micrograph thickness fields.
571 # @param w window in which to set the parameters.
573 proc setupMicrographThickness { w } {
574 global project_item theimg direct_beam lambda thickness
576 if ![regexp "Micrograph" $project_item] { return }
578 if ![winfo exists $w] {
582 label $w.ttag -text "Thickness:" -width 10 -anchor e
583 label $w.thick -width 6 -text $thickness -anchor w
584 label $w.dbtag -text "Direct beam/Dose:" -width 16 -anchor e
585 label $w.direct -width 6 -text $direct_beam -anchor w
586 button $w.dbset -text Set -command "directBeam" -relief raised
587 label $w.ltag -text "Lambda:" -width 8 -anchor e
588 label $w.lambda -width 6 -text $lambda -anchor w
589 button $w.lamcalc -text Calculate -command "calculateLambda" -relief raised
591 pack $w.ttag $w.thick $w.dbtag $w.direct $w.dbset $w.ltag \
592 $w.lambda $w.lamcalc -side left -padx 1m
594 updateMicrographThickness $w
597 ## @brief Updates micrograph thickness fields.
599 # @param w window in which to set the parameters.
601 proc updateMicrographThickness { w } {
602 global project_item theimg direct_beam lambda thickness
603 if ![winfo exists $w] { return }
604 set wc [getControlWindow $theimg]
605 set n [$wc.image.scale get]
606 set mg_item [micrographItem $n]
608 if { $lambda < 100 } {
609 set lambda [getLambda]
612 set db [Bmg get $mg_item dose]
619 set intensity [Bmg get $mg_item intensity]
621 if { $intensity > 0.001 && $direct_beam > 0.01 } {
622 set thickness [expr $lambda * log($direct_beam/$intensity)]
628 $w.thick config -text $td
629 $w.direct config -text $db
630 $w.lambda config -text $lambda
633 ## @brief Creates micrograph parameters from the image.
636 proc createMgParam { } {
637 global filename theimg imgtype
638 global paramfile project_item
639 if [Bmg exists] { return }
640 set id_name [file rootname [file tail $filename]]
641 # puts "New ID: $id_name"
642 set nz [Bimage get $theimg nslices]
643 if { $imgtype == "" } { setImageType }
644 if { $imgtype == "mg" || $imgtype == "frame" } {
645 if { $nz > 1 } { switchImage }
646 set nz [Bimage get $theimg nslices]
648 set nimg [Bimage get $theimg nimages]
649 Bmg create $filename $imgtype
657 set project_item "Reconstruction:$id_name"
658 Bmg set $project_item id $id_name
661 set project_item "Micrograph:$id_name"
662 Bmg set $project_item id $id_name
663 if { $nimg > 1 && $imgtype != "frame" } {
664 set project_item "Field:$id_name"
665 Bmg set $project_item id $id_name
668 # puts "New item: $project_item"
669 set ori [Bimage get $theimg origin]
670 set ps [Bimage get $theimg pixel_size 0]
671 Bmg set $project_item origin [lindex $ori 0] [lindex $ori 1] [lindex $ori 2]
672 Bmg set $project_item pixel_size [lindex $ps 0] [lindex $ps 1] [lindex $ps 2]
673 # puts "Pixel size: $ps"
674 # Bimage set $theimg label $project_item
677 ## @brief Returns the current project item.
679 # @param n micrograph index (first = 0)
681 proc micrographItem { n } {
684 if { ![Bmg exists] && ![Bmodel exists] } { return "" }
686 if [regexp "Model" $project_item] {
688 } elseif [regexp "Reconstruction" $project_item] {
690 } elseif [regexp "Class" $project_item] {
692 } elseif [regexp "Micrograph" $project_item] {
693 set id [Bmg get $project_item id $n]
694 return "Micrograph:$id"
695 # return $project_item
696 } elseif [regexp "Field" $project_item] {
697 set id [Bmg get $project_item id $n]
698 return "Micrograph:$id"
700 puts "Error: project_item not defined! ($project_item)"
704 proc selectMicrograph { } {
705 global project_item theimg mg_sel
706 set mg_item $project_item
707 if ![regexp "Micrograph" $mg_item] {
708 set wc [getControlWindow $theimg]
709 set n [$wc.image.scale get]
710 set mg_item [micrographItem $n]
712 # puts "selectMicrograph: $mg_item"
713 Bmg set $mg_item select $mg_sel
716 ## @brief Updates an existing project for micrograph parameters in memory.
719 proc setMgParam { } {
720 global filament_width filament_node_radius
721 global marker_radius fom_cutoff
722 global theimg imgtype
723 global filename project_item
724 if ![Bmg exists] { createMgParam }
725 # puts "setMgParam: $project_item"
726 # Bimage set $theimg label $project_item
727 if { [Bimage get $theimg nslices] > 1 } { # Converting from mg to rec
729 if { [string first "mg" $imgtype] > -1 } {
731 puts "Trying to convert to reconstruction"
733 # puts "Reconstruction: $imgtype $filename"
735 if { [string first "rec" $imgtype] > -1 } { set imgtype "mg" }
737 # puts "Bmg set $project_item filename $filename $imgtype"
738 Bmg set $project_item filename $filename $imgtype
739 set ps [getPixelSizeEntry]
740 Bmg set $project_item pixel_size [lindex $ps 0] [lindex $ps 1] [lindex $ps 2]
741 # if { [Bimage get $theimg nslices] < 2 } { # Micrograph parameters only
742 # if [winfo exists .wctf] { setCTFparam }
744 # puts "setMgParam: $filename"
746 if [winfo exists .wfil] {
747 set filament_width [.wfil.fil_width.entry get]
748 set filament_node_radius [.wfil.node_radius.entry get]
749 Bmg set $project_item filament_width $filament_width
750 Bmg set $project_item filament_node_radius $filament_node_radius
752 if [winfo exists .wtomo] {
753 set marker_radius [.wtomo.marker_radius.e get]
754 Bmg set $project_item marker_radius $marker_radius
758 ## @brief Saves the micrograph data base to a parameter file.
761 proc writeParam { } {
763 global paramfile paramdir
764 set currdir $paramdir
765 if { [string length $currdir] < 2 } { set currdir [pwd] }
766 if { [string first $currdir ".."] > -1 } { set currdir [pwd] }
767 puts "Current directory: $currdir"
769 set thename [file rootname [file tail $filename]]
770 if { [string length $paramfile] < 1 } {
771 set paramfile $thename
772 if { ![string match "*.star" $paramfile] } {
773 append paramfile ".star"
776 set paramfile [file tail $paramfile]
778 {"STAR files" {.star .STAR} }
779 {"XML files" {.xml .XML} }
780 {"EMX files" {.emx .EMX} }
782 set paramfile [tk_getSaveFile -filetypes $types \
783 -initialfile $paramfile -initialdir $currdir -defaultextension .star]
784 if { [string length $paramfile] < 1 } { return }
785 set paramfile [relativePath $paramdir $paramfile]
787 wm title . [list $filename ":" $paramfile]
788 # puts "Writing $paramfile"
790 # puts "Writing .bshow"
794 ## @brief Edits a text parameter file.
798 global paramfile paramdir
799 editTextFile $paramfile
804 ## @brief Sets up the window for the sorted micrograph table.
807 proc sortedMgTable { } {
808 if ![Bmg exists] { return }
812 if ![winfo exists $w] {
816 text $w.table -relief sunken -bd 2 -width 40\
817 -yscrollcommand "$w.yscroll set" \
818 -setgrid 1 -highlightthickness 0 -pady 2 -padx 3 -font $cour12
819 scrollbar $w.yscroll -command "$w.table yview" \
820 -highlightthickness 0 -orient vertical
821 grid $w.table -in $w.frame -padx 1 -pady 1 \
822 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
823 grid $w.yscroll -in $w.frame -padx 1 -pady 1 \
824 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
825 grid rowconfig $w.frame 0 -weight 1 -minsize 0
826 grid columnconfig $w.frame 0 -weight 1 -minsize 0
828 # label $w.header -font $helv12 -anchor w \
829 # -text " Mg $mg_sort"
831 label $w.header.tag -text " Mg "
832 tk_optionMenu $w.header.menu mg_sort \
833 "Intensity" "Defocus" "Water ring" "Particles"
834 $w.header.menu configure -width 10
837 button $w.buttons.update -text Update -command "updateSortedMgTable"
838 button $w.buttons.close -text Close -command "destroy $w"
840 pack $w.header -side top -fill x -padx 5
841 pack $w.header.tag $w.header.menu -side left -pady 2 -anchor w
842 pack $w.frame -expand yes -fill both -padx 5 -pady 5
844 pack $w.buttons.update $w.buttons.close -side left -expand 1
846 pack $w.buttons -side bottom -fill x -pady 2m
848 $w.table tag configure selmg -background #a0b7ce
853 wm title $w "Sorted Micrographs"
854 wm iconname $w "Sorted"
858 ## @brief Updates the sorted micrograph table.
861 proc updateSortedMgTable {} {
862 global project_item mg_sort
863 if ![winfo exists .wsort] { return }
864 # puts "Updating sorted micrograph table parameters"
865 set m [Bmg sort $project_item $mg_sort]
867 foreach {id val} $m {
868 append table [format " %s %7.3f\n" $id $val]
870 .wsort.table delete 1.0 end
871 .wsort.table insert 1.0 $table
872 bind .wsort.table <Double-Button-1> { setMicrographFromTable %W }
876 ## @brief Loads a micrograph based on id.
878 # @param w dialog window with micrograph id.
880 proc setMicrographFromTable { w } {
881 global project_item theimg filename imgtype
882 set oldfilename $filename
884 set item [$w get -- seline.first seline.last]
885 loadMicrograph [lindex $item 0]
888 ## @brief Loads a micrograph based on id.
890 # @param w dialog window with micrograph id or number.
892 proc setMicrograph { w } {
895 set n [Bmg get $project_item number_of_item]
896 if { $idx > $n } { set idx $n }
897 set id [Bmg get $project_item id_from_index $idx]
902 ## @brief Dialog box to set the direct beam/dose
904 proc directBeam { } {
906 global project_item theimg
907 set wc [getControlWindow $theimg]
908 set n [$wc.image.scale get]
909 set mg_item [micrographItem $n]
910 set db [Bmg get $mg_item dose]
912 if ![winfo exists $w] {
915 setupEntry $w.db "Direct beam/Dose" double $db \
916 "Direct beam/Applied dose in electrons/angstrom square"
918 checkbutton $w.all -text "Set dose for all micrographs" \
919 -variable dball -anchor w
921 pack $w.db $w.all -side top -fill x -pady 2 -padx 2
924 button $w.buttons.ok -text OK -command "setDirectBeam $w"
925 button $w.buttons.cancel -text Cancel -command "destroy $w"
926 pack $w.buttons -side bottom -fill x -pady 2m
927 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
929 bind $w <Return> "setTilts .wtilt"
935 wm iconname $w "Dose"
938 ## @brief Sets the direct beam/dose.
940 # @param w the window to destroy.
942 proc setDirectBeam { w } {
943 global project_item theimg dball
944 set db [expr [$w.db.e get]]
947 Bmg set "all" dose $db
949 set wc [getControlWindow $theimg]
950 set n [$wc.image.scale get]
951 set mg_item [micrographItem $n]
952 Bmg set $mg_item dose $db
954 updateMicrographThickness .wbox.thick
957 ## @brief Returns lambda calculated from current parameters.
960 global lambda volt focal_length obj_apert slit
961 if { $obj_apert < 1 } {
964 set lambda [Bmg lambda [expr 1000 * $volt] \
965 [expr 1e7 * $focal_length] [expr 1e4 * $obj_apert] $slit]
969 ## @brief Returns thickness if direct beam intensity is specified.
971 proc getThickness { intensity } {
972 global imgtype thickness lambda direct_beam
973 if { $imgtype != "mg" } { return thickness }
974 if { $direct_beam < 0.1 } { return thickness }
975 if { $intensity < 0.1 } { return thickness }
976 set lambda [getLambda]
977 set thickness [expr $lambda * log($direct_beam/$intensity)]
981 proc panelLambda { w } {
982 global volt focal_length obj_apert slit lambda
984 set lambda [getLambda]
986 setupEntry $w.volt "Voltage " double $volt \
987 "The microscope acceleration voltage in kV."
989 setupEntry $w.focal "Focal length " double $focal_length \
990 "The microscope focal length in mm."
992 setupEntry $w.objap "Objective aperture" double $obj_apert \
993 "The objective aperture diameter in µm."
995 setupEntry $w.slit "Slit width " double $slit \
996 "The energy filter slit width in eV."
998 setupEntry $w.lambda "Lambda " double $lambda \
999 "The calculated proportionality parameter."
1001 pack $w.volt $w.focal $w.objap $w.slit $w.lambda \
1002 -side top -fill x -pady 2 -padx 5
1004 bind $w.volt.e <Return> "doCalculateLambda $w"
1005 bind $w.focal.e <Return> "doCalculateLambda $w"
1006 bind $w.objap.e <Return> "doCalculateLambda $w"
1007 bind $w.slit.e <Return> "doCalculateLambda $w"
1010 ## @brief Calculates lamda from microscope parameters.
1012 proc calculateLambda { } {
1013 global paramfile volt focal_length obj_apert slit lambda
1015 if ![winfo exists $w] {
1021 button $w.buttons.do -text Calculate \
1022 -command "doCalculateLambda $w"
1023 button $w.buttons.close -text Close -command "destroy $w"
1024 pack $w.buttons -side bottom -fill x -pady 2m
1025 pack $w.buttons.do $w.buttons.close -side left -expand 1
1030 wm title $w "Lambda"
1031 wm iconname $w "Lambda"
1034 ## @brief Calculates lamda from microscope parameters.
1036 # @param w dialog window for parameters.
1038 proc doCalculateLambda { w } {
1039 global paramfile volt focal_length obj_apert slit lambda
1040 set volt [$w.volt.e get]
1041 set focal_length [$w.focal.e get]
1042 set obj_apert [$w.objap.e get]
1043 set slit [$w.slit.e get]
1044 set lambda [getLambda]
1045 # puts "Lambda = $lambda"
1050 ## @brief Updates the lamda value from the global variable.
1052 # @param w dialog window for update.
1054 proc updateLambda { w } {
1056 if ![winfo exists $w] { return }
1057 $w.lambda.e delete 0 end
1058 $w.lambda.e insert 0 $lambda
1061 ## @brief Updates the thickness from the global variable.
1063 # @param w dialog window for update.
1065 proc updateThickness { w } {
1067 if ![winfo exists $w] { return }
1068 $w.thick.e delete 0 end
1069 $w.thick.e insert 0 $thickness