Bsoft  2.1.0
Bernard's software package
bshow_param.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_param.tcl
3 #
4 # @brief Procedures to read and write parameter files
5 #
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20200615
9 
10 set paramfile ""
11 set paramdir [pwd]
12 set project_root ""
13 set project_item ""
14 set project_filter "*"
15 set fom_cutoff 0
16 set tilt_axis 0
17 set tilt_angle 0
18 set tilt_offset 0
19 set show_tilt_axis 1
20 set mg_sort ""
21 set focal_length 3.4
22 set obj_apert 200.0
23 set slit 0.0
24 set direct_beam 0.0
25 set thickness 500.0
26 set lambda 2200.0
27 
28 ## @brief Menu for opening and closing parameter files
29 #
30 
31 proc setupParameterMenu {} {
32  global theimg
33 
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 } \
50  -underline 0
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" \
67  -command { 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"
75 }
76 
77 
78 ## @brief Opens a micrograph parameter file for reading.
79 #
80 
81 proc openParam { } {
82  global paramfile paramdir
83  set currdir $paramdir
84  if { [string length $currdir] < 2 } { set currdir [pwd] }
85  set types {
86  {"STAR files" {.star .STAR} }
87  {"XML files" {.xml .XML} }
88  {"EMX files" {.emx .EMX} }
89  }
90  set paramfile [tk_getOpenFile -title "Open parameter file" -filetypes $types \
91  -initialdir $currdir -defaultextension .star]
92  if { [string length $paramfile] < 1 } { return }
93  loadParam $paramfile
94  Update 0
95 }
96 
97 ## @brief Opens a micrograph parameter file for reading.
98 #
99 # @param file parameter file name.
100 
101 proc loadParam { file } {
102  global paramfile paramdir
103  global project_item
104  if ![file exists $file] {
105  tk_messageBox -message "Error: File $file not found" -type ok
106  return
107  }
108  set project_item "all"
109  set paramfile $file
110  Bmg read $paramfile
111  set paramdir [file dirname $paramfile]
112  set paramfile [file tail $paramfile]
113 # puts "$paramdir $paramfile"
114  selectAFile
115 }
116 
117 ## @brief Appends one or more micrograph parameter files.
118 #
119 
120 proc appendParam { } {
121  global paramfile paramdir
122  set currdir $paramdir
123  if { [string length $currdir] < 2 } { set currdir [pwd] }
124  set types {
125  {"STAR files" {.star .STAR} }
126  {"XML files" {.xml .XML} }
127  {"EMX files" {.emx .EMX} }
128  }
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 }
132  puts $addfiles
133 # loadParam $paramfile
134  Bmg add $addfiles
135  Update 0
136 }
137 
138 proc tag_line { w } {
139  set cur [$w index current]
140  $w tag delete seline
141  $w tag add seline "$cur linestart" "$cur lineend"
142  $w tag configure seline -background yellow
143 }
144 
145 ## @brief Checks if the parameter file exist, otherwise save it.
146 #
147 
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] } {
154  writeParam
155  }
156  }
157 }
158 
159 ## @brief Checks parameter file name and constructs new file name.
160 #
161 
162 proc newParamFile { suffix } {
163  global paramfile project_root
164  if [string length $project_root] {
165  set newfile $project_root
166  } else {
167  set newfile [file rootname $paramfile]
168  }
169  if ![string length $newfile] {
170  tk_messageBox -message "Please write a parameter file first" -type ok
171  } else {
172  append newfile $suffix
173  }
174  return $newfile
175 }
176 
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] {
187  $w.file.list see end
188  tag_line $w.file.list
189 # puts $filename
190  }
191  }
192  $w.file.list insert end "\n"
193  }
194  }
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
199  }
200 # puts [$w.file.list index current]
201 }
202 
203 ## @brief Dialog box to select an image file.
204 #
205 
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]
213  } else {
214  return
215  }
216 # puts "$img_files"
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
222  }
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"
229  } else {
230  set w .wsel
231  catch {destroy $w}
232  toplevel $w
233  wm title $w "Select an image"
234  wm iconname $w "Select"
235 
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
239 
240  frame $w.file
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
250 
251  ## Filter
252  labelframe $w.filter -text "File filter" -labelanchor nw
253  entry $w.filter.e
254  $w.filter.e insert 0 $project_filter
255  pack $w.filter.e -side left -expand yes -fill x
256 
257  frame $w.buttons
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
264 
265  setupFileList $w $img_files
266 
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
273 # }
274 
275  lower . .wsel
276  tkwait window $w
277  }
278 # puts "$oldfilename --- $filename"
279 # puts "$filename ($imgtype) $project_item"
280  if { [string compare $oldfilename $filename] != 0 } {
281  loadImage $filename -1
282  }
283  updateParametersAndImage
284 }
285 
286 proc setSelectionAndDestroy { w } {
287  global filename
288  global imgtype
289  global project_item
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"
296  destroy $w
297 }
298 
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]]
305  }
306  }
307 }
308 
309 proc updateParametersAndImage { } {
310  global filename paramfile modelfile
311  global theimg imgtype
312  global project_item mg_sel
313 
314 # puts "Updating parameters and image: $project_item"
315 
316  if { [Bmg exists] } {
317  Bmg set all active 0
318  if [regexp "Reconstruction" $project_item] { Bmg set all active 1 }
319  if { $imgtype == "frame" } {
320  if { [Bimage get $theimg nslices] > 1 } { switchImage }
321  }
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]
329  }
330 
331 # Bimage set $theimg label $project_item
332  updatePixelsize
333  updateOrigins
334 
335  if { [Bmg exists] } {
336  getCTFparam
337  updateClassParam
338  updateBoxParam
339  updateNodeParam
340  updateTomoTable
341  updateMarkerParam
342  updateSpotParam
343  updateLayerLineParam
344  } elseif { [Bmodel exists] } {
345  objectsDrawAll
346  }
347 }
348 
349 proc getNeighborImageOfType { dir } {
350  global filename
351  global theimg imgtype
352  global project_item
353 
354  set wc [getControlWindow $theimg]
355  set img_num [expr [$wc.image.scale get] + $dir]
356  set img_max [$wc.image.scale cget -to]
357 
358  if { $img_num >= 0 && $img_num <= $img_max } {
359  $wc.image.scale set $img_num
360  updateParametersAndImage
361  return
362  }
363 
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]
368  } else {
369  return
370  }
371 
372  set f ""
373  set prevfile ""
374  set prevlist ""
375  set nxt 0
376  foreach name $img_files {
377  set list [split $name ":"]
378  if [string equal $imgtype [lindex $list 1]] {
379  set f [lindex $list 0]
380  if { $nxt } {
381  break
382  }
383  if [string equal $filename $f] {
384  if { $dir > 0 } {
385  set nxt 1
386  set f ""
387  } else {
388  set f $prevfile
389  set list $prevlist
390  break
391  }
392  }
393  set prevfile $f
394  set prevlist $list
395  }
396  }
397 
398  if [string length $f] {
399  if { [lindex $list 2] == "Micrograph" } {
400  loadMicrograph [lindex $list 3]
401  } else {
402  set filename $f
403  set project_item "[lindex $list 2]:[lindex $list 3]"
404  loadImage $filename -1
405  updateParametersAndImage
406  }
407  }
408 }
409 
410 proc getNeighborImageOfType2 { dir } {
411  global filename
412  global theimg imgtype
413  global project_item
414 
415  set wc [getControlWindow $theimg]
416  set img_num [expr [$wc.image.scale get] + $dir]
417 
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]
422  } else {
423  return
424  }
425 
426  set f ""
427  set curr_item ""
428  set prevfile ""
429  set prevlist ""
430  set nxt 0
431 # puts $project_item
432  foreach name $img_files {
433  set list [split $name ":"]
434 # puts $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]"
438  if { $nxt } {
439  break
440  }
441 # puts $curr_item
442  if [string equal $project_item $curr_item] {
443 # puts $curr_item
444  if { $dir > 0 } {
445  set nxt 1
446  } else {
447  if [string length $prevfile] { set f $prevfile }
448  if [string length $prevlist] { set list $prevlist }
449  break
450  }
451  }
452  set prevfile $f
453  set prevlist $list
454  }
455  }
456 
457  if [string length $f] {
458  set project_item "[lindex $list 2]:[lindex $list 3]"
459  if [string equal $filename $f] {
460  } else {
461  set filename $f
462  loadImage $filename -1
463  }
464  $wc.image.scale set $img_num
465  updateParametersAndImage
466  }
467 }
468 
469 ## @brief Sets up micrograph or reconstruction id and total.
470 #
471 # @param w window in which to set the parameters.
472 
473 proc setupItemID { w } {
474  global project_item
475 
476  set idx [Bmg get $project_item index_of_item]
477  set nmg [Bmg get all number_of_item]
478 
479  if ![winfo exists $w] {
480  frame $w
481  }
482 
483  if [regexp "Reconstruction" $project_item] {
484  label $w.tag -text "Reconstruction" -anchor e
485  } else {
486  label $w.tag -text "Micrograph" -anchor e
487  }
488 
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 }
491  $w.idx insert 0 $idx
492  label $w.nmg -width 5 -text " / $nmg" -anchor w
493 
494  pack $w.tag $w.str $w.idx $w.nmg -side left -padx 2
495 
496  bind $w.idx <Return> { setMicrograph %W }
497 }
498 
499 ## @brief Updates micrograph or reconstruction id.
500 #
501 # @param w window in which to set the parameters.
502 
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
510  } else {
511  set wc [getControlWindow $theimg]
512  set n [$wc.image.scale get]
513  set item [micrographItem $n]
514  }
515  set idx [Bmg get $item index_of_item]
516 # puts "$project_item $item $idx"
517  $w.str config -text "$item"
518  $w.idx delete 0 end
519  $w.idx insert 0 $idx
520 }
521 
522 ## @brief Loads a micrograph based on id.
523 #
524 # @param id micrograph id .
525 
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
532  }
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
537  }
538  if { $imgtype == "mg" } {
539  set n [Bmg get $mg_item img_num]
540  set wc [getControlWindow $theimg]
541  $wc.image.scale set $n
542  }
543  updateParametersAndImage
544 }
545 
546 
547 ## @brief Sets up micrograph navigation and selection buttons.
548 #
549 # @param w window in which to set up the buttons.
550 # @param typelabel type of navigation (micrograph).
551 
552 proc setupMicrographButtons { w typelabel } {
553  global mg_sel
554 
555  if ![winfo exists $w] {
556  frame $w
557  }
558 
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
567 }
568 
569 ## @brief Sets up micrograph thickness fields.
570 #
571 # @param w window in which to set the parameters.
572 
573 proc setupMicrographThickness { w } {
574  global project_item theimg direct_beam lambda thickness
575 
576  if ![regexp "Micrograph" $project_item] { return }
577 
578  if ![winfo exists $w] {
579  frame $w
580  }
581 
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
590 
591  pack $w.ttag $w.thick $w.dbtag $w.direct $w.dbset $w.ltag \
592  $w.lambda $w.lamcalc -side left -padx 1m
593 
594  updateMicrographThickness $w
595 }
596 
597 ## @brief Updates micrograph thickness fields.
598 #
599 # @param w window in which to set the parameters.
600 
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]
607 
608  if { $lambda < 100 } {
609  set lambda [getLambda]
610  }
611 
612  set db [Bmg get $mg_item dose]
613  if { $db > 0.01 } {
614  set direct_beam $db
615  } else {
616  set db "nd"
617  }
618 
619  set intensity [Bmg get $mg_item intensity]
620 
621  if { $intensity > 0.001 && $direct_beam > 0.01 } {
622  set thickness [expr $lambda * log($direct_beam/$intensity)]
623  set td "$thickness"
624  } else {
625  set td "nd"
626  }
627 
628  $w.thick config -text $td
629  $w.direct config -text $db
630  $w.lambda config -text $lambda
631 }
632 
633 ## @brief Creates micrograph parameters from the image.
634 #
635 
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]
647  }
648  set nimg [Bimage get $theimg nimages]
649  Bmg create $filename $imgtype
650  if { $nz > 1 } {
651  Bmg set all active 1
652  if { $nimg > 1 } {
653  set imgtype "part"
654  } else {
655  set imgtype "rec"
656  }
657  set project_item "Reconstruction:$id_name"
658  Bmg set $project_item id $id_name
659  } else {
660  Bmg set all active 0
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
666  }
667  }
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
675 }
676 
677 ## @brief Returns the current project item.
678 #
679 # @param n micrograph index (first = 0)
680 
681 proc micrographItem { n } {
682  global project_item
683 
684  if { ![Bmg exists] && ![Bmodel exists] } { return "" }
685 
686  if [regexp "Model" $project_item] {
687  return $project_item
688  } elseif [regexp "Reconstruction" $project_item] {
689  return $project_item
690  } elseif [regexp "Class" $project_item] {
691  return $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"
699  } else {
700  puts "Error: project_item not defined! ($project_item)"
701  }
702 }
703 
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]
711  }
712 # puts "selectMicrograph: $mg_item"
713  Bmg set $mg_item select $mg_sel
714 }
715 
716 ## @brief Updates an existing project for micrograph parameters in memory.
717 #
718 
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
728  Bmg set all active 1
729  if { [string first "mg" $imgtype] > -1 } {
730  set imgtype "rec"
731  puts "Trying to convert to reconstruction"
732  }
733 # puts "Reconstruction: $imgtype $filename"
734  } else {
735  if { [string first "rec" $imgtype] > -1 } { set imgtype "mg" }
736  }
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 }
743 # }
744 # puts "setMgParam: $filename"
745  setBoxSize
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
751  }
752  if [winfo exists .wtomo] {
753  set marker_radius [.wtomo.marker_radius.e get]
754  Bmg set $project_item marker_radius $marker_radius
755  }
756 }
757 
758 ## @brief Saves the micrograph data base to a parameter file.
759 #
760 
761 proc writeParam { } {
762  global filename
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"
768  createMgParam
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"
774  }
775  }
776  set paramfile [file tail $paramfile]
777  set types {
778  {"STAR files" {.star .STAR} }
779  {"XML files" {.xml .XML} }
780  {"EMX files" {.emx .EMX} }
781  }
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]
786  setMgParam
787  wm title . [list $filename ":" $paramfile]
788 # puts "Writing $paramfile"
789  Bmg write $paramfile
790 # puts "Writing .bshow"
791  writeSettingsFile
792 }
793 
794 ## @brief Edits a text parameter file.
795 #
796 
797 proc editParam { } {
798  global paramfile paramdir
799  editTextFile $paramfile
800 }
801 
802 
803 
804 ## @brief Sets up the window for the sorted micrograph table.
805 #
806 
807 proc sortedMgTable { } {
808  if ![Bmg exists] { return }
809  global cour12 helv12
810  global mg_sort
811  set w .wsort
812  if ![winfo exists $w] {
813  toplevel $w
814 
815  frame $w.frame
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
827 
828 # label $w.header -font $helv12 -anchor w \
829 # -text " Mg $mg_sort"
830  frame $w.header
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
835 
836  frame $w.buttons
837  button $w.buttons.update -text Update -command "updateSortedMgTable"
838  button $w.buttons.close -text Close -command "destroy $w"
839 
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
843 
844  pack $w.buttons.update $w.buttons.close -side left -expand 1
845 
846  pack $w.buttons -side bottom -fill x -pady 2m
847 
848  $w.table tag configure selmg -background #a0b7ce
849  } else {
850  wm deiconify $w
851  raise $w
852  }
853  wm title $w "Sorted Micrographs"
854  wm iconname $w "Sorted"
855  updateSortedMgTable
856 }
857 
858 ## @brief Updates the sorted micrograph table.
859 #
860 
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]
866  set table ""
867  foreach {id val} $m {
868  append table [format " %s %7.3f\n" $id $val]
869  }
870  .wsort.table delete 1.0 end
871  .wsort.table insert 1.0 $table
872  bind .wsort.table <Double-Button-1> { setMicrographFromTable %W }
873 }
874 
875 
876 ## @brief Loads a micrograph based on id.
877 #
878 # @param w dialog window with micrograph id.
879 
880 proc setMicrographFromTable { w } {
881  global project_item theimg filename imgtype
882  set oldfilename $filename
883  tag_line $w;
884  set item [$w get -- seline.first seline.last]
885  loadMicrograph [lindex $item 0]
886 }
887 
888 ## @brief Loads a micrograph based on id.
889 #
890 # @param w dialog window with micrograph id or number.
891 
892 proc setMicrograph { w } {
893  global project_item
894  set idx [$w get]
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]
898 # puts "$idx $id"
899  loadMicrograph $id
900 }
901 
902 ## @brief Dialog box to set the direct beam/dose
903 
904 proc directBeam { } {
905  global helv12
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]
911  set w .wdb
912  if ![winfo exists $w] {
913  toplevel $w
914 
915  setupEntry $w.db "Direct beam/Dose" double $db \
916  "Direct beam/Applied dose in electrons/angstrom square"
917 
918  checkbutton $w.all -text "Set dose for all micrographs" \
919  -variable dball -anchor w
920 
921  pack $w.db $w.all -side top -fill x -pady 2 -padx 2
922 
923  frame $w.buttons
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
928 
929  bind $w <Return> "setTilts .wtilt"
930  } else {
931  wm deiconify $w
932  raise $w
933  }
934  wm title $w "Dose"
935  wm iconname $w "Dose"
936 }
937 
938 ## @brief Sets the direct beam/dose.
939 #
940 # @param w the window to destroy.
941 
942 proc setDirectBeam { w } {
943  global project_item theimg dball
944  set db [expr [$w.db.e get]]
945  destroy $w
946  if { $dball } {
947  Bmg set "all" dose $db
948  } else {
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
953  }
954  updateMicrographThickness .wbox.thick
955 }
956 
957 ## @brief Returns lambda calculated from current parameters.
958 #
959 proc getLambda {} {
960  global lambda volt focal_length obj_apert slit
961  if { $obj_apert < 1 } {
962  set obj_apert 200
963  }
964  set lambda [Bmg lambda [expr 1000 * $volt] \
965  [expr 1e7 * $focal_length] [expr 1e4 * $obj_apert] $slit]
966  return $lambda
967 }
968 
969 ## @brief Returns thickness if direct beam intensity is specified.
970 #
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)]
978  return thickness
979 }
980 
981 proc panelLambda { w } {
982  global volt focal_length obj_apert slit lambda
983 
984  set lambda [getLambda]
985 
986  setupEntry $w.volt "Voltage " double $volt \
987  "The microscope acceleration voltage in kV."
988 
989  setupEntry $w.focal "Focal length " double $focal_length \
990  "The microscope focal length in mm."
991 
992  setupEntry $w.objap "Objective aperture" double $obj_apert \
993  "The objective aperture diameter in µm."
994 
995  setupEntry $w.slit "Slit width " double $slit \
996  "The energy filter slit width in eV."
997 
998  setupEntry $w.lambda "Lambda " double $lambda \
999  "The calculated proportionality parameter."
1000 
1001  pack $w.volt $w.focal $w.objap $w.slit $w.lambda \
1002  -side top -fill x -pady 2 -padx 5
1003 
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"
1008 }
1009 
1010 ## @brief Calculates lamda from microscope parameters.
1011 #
1012 proc calculateLambda { } {
1013  global paramfile volt focal_length obj_apert slit lambda
1014  set w .wlam
1015  if ![winfo exists $w] {
1016  toplevel $w
1017 
1018  panelLambda $w
1019 
1020  frame $w.buttons
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
1026  } else {
1027  wm deiconify $w
1028  raise $w
1029  }
1030  wm title $w "Lambda"
1031  wm iconname $w "Lambda"
1032 }
1033 
1034 ## @brief Calculates lamda from microscope parameters.
1035 #
1036 # @param w dialog window for parameters.
1037 
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"
1046  updateLambda $w
1047 # destroy $w
1048 }
1049 
1050 ## @brief Updates the lamda value from the global variable.
1051 #
1052 # @param w dialog window for update.
1053 
1054 proc updateLambda { w } {
1055  global lambda
1056  if ![winfo exists $w] { return }
1057  $w.lambda.e delete 0 end
1058  $w.lambda.e insert 0 $lambda
1059 }
1060 
1061 ## @brief Updates the thickness from the global variable.
1062 #
1063 # @param w dialog window for update.
1064 
1065 proc updateThickness { w } {
1066  global thickness
1067  if ![winfo exists $w] { return }
1068  $w.thick.e delete 0 end
1069  $w.thick.e insert 0 $thickness
1070 }
1071