4 # @brief Procedures to do CTF fitting
6 # @author Bernard Heymann
7 # @date Created: 20020729
8 # @date Modified: 20170712
32 ## @brief Dialog box to manipulate CTF parameters
34 # @param currimg Image identifier.
36 proc CTF { currimg } {
37 global project_item theimg imgtype
39 global plot_width plot_height
40 global ctf_win_x ctf_win_y
41 global ctf_scale_x ctf_scale_y
42 global volt Cs amp_fac base_type env_type ctf_hires ctf_lores
44 if ![Bimage exists $theimg] {
45 tk_dialog .dialog "No image in memory!" \
46 {Please read in an appropriate image first.} \
51 if { [string first "ps" $imgtype] < 0 } {
52 set i [tk_dialog .dialog "No power spectrum!" \
53 {No power spectrum was found. Please transform the image first to a power spectrum.} \
54 info 0 {Cancel} {Proceed anyway}]
55 if { $i < 1 } { return }
59 # puts "canvas = $c image = $theimg"
60 set filename [Bimage get $theimg filename]
61 set width [Bimage get $theimg width]
62 set height [Bimage get $theimg height]
65 if ![Bmg exists] { createMgParam }
67 set wc [getControlWindow $theimg]
68 set n [$wc.image.scale get]
69 set mg_item [micrographItem $n]
71 puts "project_item: $mg_item file: $filename imgtype: $imgtype"
72 # puts [Bmg get $mg_item filename ps]
73 if ![string length [Bmg get $mg_item filename ps]] {
74 Bmg set $mg_item filename $filename ps
75 puts "Power spectrum: $filename"
78 setCTFPixelSize $theimg
81 if ![winfo exists $w] {
83 wm geometry $w [format "%dx%d" $ctf_win_x $ctf_win_y]
85 if { $ctf_scale_y < 0.1 } {
86 # set range [expr [.max.scale get] - [.min.scale get]]
87 set range [expr [Bimage get $theimg max] - [Bimage get $theimg min]]
88 if { $range <= 0 } { set range $plot_height }
89 set ctf_scale_y [expr int($plot_height/$range + 1)]
92 # set hires [expr int(200 * [.pixel_size.entry get]) / 100.0]
93 set ps [Bimage get $theimg pixel_size $n]
94 set hires [expr int(200 * [lindex $ps 0]) / 100.0]
95 if { $hires > $ctf_hires } { set ctf_hires $hires }
96 if { $ctf_lores < [expr 2 * $ctf_hires] } {
97 set ctf_lores [expr 2 * $ctf_hires]
100 menu $w.menuBar -tearoff 0
101 $w.menuBar add cascade -menu $w.menuBar.box -label "CTF" -underline 0
102 menu $w.menuBar.box -tearoff 0
103 $w configure -menu $w.menuBar
104 $w.menuBar.box add command -label "Help" -underline 0 \
105 -command { showURL "file://$Bsoft/doc/bshow/bshow_ctf.html" }
106 $w.menuBar.box add command -label "Write curves" \
107 -command { writeCTF } -underline 0
108 $w.menuBar.box add command -label "Save window" \
109 -command [list capture $w png t.png] -underline 0
110 # -command { canvas2Photo $w } -underline 0
111 $w.menuBar.box add command -label "Close" \
112 -command "destroy $w" -underline 0
115 canvas $g -width $plot_width -height $plot_height -background white \
116 -relief sunken -borderwidth 2
118 setupItemID $w.itemid
122 label $w.pp.origin_tag -font $helv12 -text "Origin" -bg cyan
123 entry $w.pp.ox_entry -width 6 -validate key -vcmd { string is double %P }
124 $w.pp.ox_entry insert 0 20
125 entry $w.pp.oy_entry -width 6 -validate key -vcmd { string is double %P }
126 $w.pp.oy_entry insert 0 20
127 label $w.pp.scale_tag -font $helv12 -text "Scale" -bg cyan
128 entry $w.pp.sx_entry -width 6 -validate key -vcmd { string is double %P }
129 $w.pp.sx_entry insert 0 $ctf_scale_x
130 entry $w.pp.sy_entry -width 6 -validate key -vcmd { string is double %P }
131 $w.pp.sy_entry insert 0 $ctf_scale_y
132 label $w.pp.res_tag -font $helv12 -text "Resolution" -bg cyan
133 entry $w.pp.lores_entry -width 6 -validate key -vcmd { string is double %P }
134 $w.pp.lores_entry insert 0 $ctf_lores
135 entry $w.pp.hires_entry -width 6 -validate key -vcmd { string is double %P }
136 $w.pp.hires_entry insert 0 $ctf_hires
137 label $w.pp.val_tag -font $helv12 -text "Value" -bg cyan
138 label $w.pp.value -width 6 -text "" -relief sunken -bd 1 -anchor w
140 # Placement in frames
141 pack $w.pp.origin_tag $w.pp.ox_entry $w.pp.oy_entry \
142 $w.pp.scale_tag $w.pp.sx_entry $w.pp.sy_entry \
143 $w.pp.res_tag $w.pp.lores_entry $w.pp.hires_entry \
145 -side left -ipadx 3 -ipady 2
147 # These scales are used for CTF parameters
148 scale $w.def_avg_scale -orient horizontal -from 0.02 -to 20 \
149 -command { setDefocusAverage } -label "Defocus average (um)"\
151 $w.def_avg_scale set 2
153 scale $w.def_dev_scale -orient horizontal -from 0 -to 5 \
154 -command { setDefocusDeviation } -label "Defocus deviation (um)"\
156 $w.def_dev_scale set 0
158 scale $w.ast_ang_scale -orient horizontal -from -90 -to 90 \
159 -command { setAstigmatismAngle } -label "Astigmatism angle (degrees)"
160 $w.ast_ang_scale set 0
163 frame $w.vca -bg cyan
164 label $w.vca.volt_tag -font $helv12 -text "Voltage (kV)" -bg cyan
165 entry $w.vca.volt_entry -width 6 -validate key -vcmd { string is double %P }
166 $w.vca.volt_entry insert 0 $volt
167 label $w.vca.cs_tag -font $helv12 -text "Cs (mm)" -bg cyan
168 entry $w.vca.cs_entry -width 6 -validate key -vcmd { string is double %P }
169 $w.vca.cs_entry insert 0 $Cs
170 label $w.vca.amp_tag -font $helv12 -text "Amp" -bg cyan
171 entry $w.vca.amp_entry -width 6 -validate key -vcmd { string is double %P }
172 $w.vca.amp_entry insert 0 $amp_fac
173 pack $w.vca.volt_tag $w.vca.volt_entry $w.vca.cs_tag $w.vca.cs_entry \
174 $w.vca.amp_tag $w.vca.amp_entry -side left -ipadx 3
176 frame $w.base_eq -bg green
177 label $w.base_eq.tag -font $helv12 -text "Baseline" -bg green
178 entry $w.base_eq.entry
179 $w.base_eq.entry insert 0 {0.2*exp(-5000*$s2) + 0.14*exp(-300*$s2) + 0.4}
181 frame $w.base_buttons -bg green
182 label $w.base_buttons.tag -font $helv12 -text " Type" -bg green
183 tk_optionMenu $w.base_buttons.type base_type "1" "2" "3" "4" "5" "6"
184 checkbutton $w.base_buttons.subtract -text "Subtract" \
185 -variable sub_base -relief raised -command "drawCTF" -bg green
187 # Placement in frames
188 pack $w.base_eq.tag -side left
189 pack $w.base_eq.entry -side left -fill x -expand yes -ipady 2
190 pack $w.base_buttons.tag $w.base_buttons.type -side left -ipadx 2
191 pack $w.base_buttons.subtract -side left
193 frame $w.env_eq -bg orange
194 label $w.env_eq.tag -font $helv12 -text "Envelope" -bg orange
195 entry $w.env_eq.entry
196 $w.env_eq.entry insert 0 {0.2*exp(-1000*$s2)}
198 frame $w.env_buttons -bg orange
199 label $w.env_buttons.tag -font $helv12 -text " Type" -bg orange
200 tk_optionMenu $w.env_buttons.type env_type "1" "2" "3" "4"
202 # Placement in frames
203 pack $w.env_eq.tag -side left
204 pack $w.env_eq.entry -side left -fill x -expand yes -ipady 2
205 pack $w.env_buttons.tag $w.env_buttons.type -side left -ipadx 2
207 # Buttons to do CTF fits
208 frame $w.fit -bg cyan
209 label $w.fit.tag -text "Fit " -bg cyan
210 button $w.fit.quick -text "Quick" \
211 -relief raised -command "autoCTFfit 0"
212 button $w.fit.baseline -text "Baseline" \
213 -relief raised -command "autoCTFfit 1"
214 button $w.fit.envelope -text "Envelope" \
215 -relief raised -command "autoCTFfit 2"
216 button $w.fit.defocus -text "Defocus" \
217 -relief raised -command "autoCTFfit 3"
218 button $w.fit.astig -text "Astigmatism" \
219 -relief raised -command "autoCTFfit 4"
220 pack $w.fit.tag $w.fit.quick $w.fit.baseline $w.fit.envelope \
221 $w.fit.defocus $w.fit.astig -side left -ipadx 5
223 # Defocus range for quick fit
224 frame $w.def -bg cyan
225 label $w.def.tag -text "Defocus:" -bg cyan
226 label $w.def.start_tag -text "min" -bg cyan
227 entry $w.def.start -width 4
228 label $w.def.end_tag -text "max" -bg cyan
229 entry $w.def.end -width 4
230 label $w.def.inc_tag -text "inc" -bg cyan
231 entry $w.def.inc -width 4
232 $w.def.start insert 0 0.1
233 $w.def.end insert 0 20
234 $w.def.inc insert 0 0.1
235 pack $w.def.tag $w.def.start_tag $w.def.start $w.def.end_tag \
236 $w.def.end $w.def.inc_tag $w.def.inc -side left -ipadx 5
238 # Checkbutton to update the plot and show rings
240 checkbutton $w.checks.update -text "updateCTF" -variable cont_update \
242 checkbutton $w.checks.rings -text "Show rings" -variable show_rings \
243 -command [list drawEllipse $theimg]
244 pack $w.checks.update $w.checks.rings -side left -padx 2
245 setupMicrographButtons $w.checks "micrograph"
247 # Placement in window
248 pack $w.itemid -side top -fill x -ipady 1
249 pack $w.plot -side top -fill both -expand yes
250 pack $w.pp $w.def_avg_scale $w.def_dev_scale $w.ast_ang_scale -side top -fill x -ipady 1
251 pack $w.vca $w.base_eq $w.base_buttons $w.env_eq $w.env_buttons -side top -fill x -ipady 1
252 pack $w.checks -side bottom -fill x -ipady 1
253 pack $w.fit $w.def -side left -fill x -ipady 1
260 wm title $w [list "CTF:" $filename "(" $n ")"]
263 bind $g <Motion> "showFrequency %W %x %y"
264 bind $w <Return> "updateCTF"
266 bind $w <Control-w> { destroy $w }
267 bind $w <Command-w> { destroy $w }
268 .menuBar.window entryconfigure "CTF" -state normal
271 ## @brief Dialog box to set the pixel size for CTF fitting
273 # @param theimg Image identifier.
275 proc setCTFPixelSize { theimg } {
277 # set wc [getControlWindow $theimg]
278 set ps [getPixelSizeEntry]
279 # puts "pixel size = $pixel_size"
280 if { [lindex $ps 0] < 100 } { return }
282 if ![winfo exists $w] {
286 # label $w.pxs.tag -font $helv12 -text "Set a reasonable pixel size"
287 # entry $w.pxs.entry -width 6 -validate key -vcmd { string is double %P }
288 # $w.pxs.entry insert 0 $pixel_size
290 setupVectorEntry $w.pxs "Pixel size (Å)" double $ps "Image pixel size in Å/pixel"
293 button $w.buttons.ok -text OK -command "setPixelSize_and_DestroyWindow $w $theimg"
294 button $w.buttons.cancel -text Cancel -command "destroy $w"
296 pack $w.pxs.tag $w.pxs.entry -side left -pady 5
297 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
298 pack $w.pxs $w.buttons -side top -fill x -pady 2m
300 bind $w <Return> "setPixelSize_and_DestroyWindow $w $theimg"
305 wm title $w "Pixel Size"
306 wm iconname $w "Pixel Size"
310 ## @brief Sets the pixel size.
312 # @param w the window to destroy.
313 # @param theimg Image identifier.
315 proc setPixelSize_and_DestroyWindow { w theimg } {
316 set wc [getControlWindow $theimg]
317 set img_num [$wc.image.scale get]
318 set ps [$w.pxs.entry get]
320 Bimage set $theimg pixel_size $img_num [lindex $ps 0] [lindex $ps 1] [lindex $ps 2]
325 ## @brief Sets the reciprocal space step size.
327 # @param theimg Image identifier.
329 proc setStep { theimg } {
331 set pixel_size [Bimage get $theimg pixel_size]
332 set width [Bimage get $theimg width]
333 set step [expr 1.0 / ([lindex $pixel_size 0] * $width)]
336 ## @brief Updates all the CTF curves
340 global theimg project_item
342 global ctf_win_x ctf_win_y
344 if [winfo exists $w] {
345 updateItemID $w.itemid
349 set ctf_win_x [winfo width $w]
350 set ctf_win_y [winfo height $w]
352 set c [getImageCanvas $theimg]
357 ## @brief Sets the defocus average
359 # @param def_avg Defocus average.
361 proc setDefocusAverage { def_avg } {
362 global project_item theimg
363 set wc [getControlWindow $theimg]
364 set n [$wc.image.scale get]
365 set mg_item [micrographItem $n]
367 Bmg set $mg_item defocus [expr 1e4 * [$w.def_avg_scale get]]
371 ## @brief Sets the defocus deviation
373 # @param def_dev Defocus deviation.
375 proc setDefocusDeviation { def_dev } {
376 global project_item theimg
377 set wc [getControlWindow $theimg]
378 set n [$wc.image.scale get]
379 set mg_item [micrographItem $n]
381 Bmg set $mg_item defocus_deviation [expr 1e4 * [$w.def_dev_scale get]]
385 ## @brief Sets the astigmatism angle
387 # @param ast_ang Astigmatism angle in degrees.
389 proc setAstigmatismAngle { ast_ang } {
390 global project_item theimg
391 set wc [getControlWindow $theimg]
392 set n [$wc.image.scale get]
393 set mg_item [micrographItem $n]
395 Bmg set $mg_item astigmatism_angle [$w.ast_ang_scale get]
399 ## @brief Calculates the wavelength
402 proc getWavelength {} {
403 set volt [expr 1000 * [.wctf.vca.volt_entry get]]
404 return [expr 12.26 / sqrt($volt * (1 + $volt * 0.9788e-6))]
407 ## @brief Returns the first zero at the average defocus
409 proc getFirstZero { } {
410 global project_item theimg
411 set wc [getControlWindow $theimg]
412 set n [$wc.image.scale get]
413 set mg_item [micrographItem $n]
414 # puts "Getting the first zero"
415 set firstzero [Bmg get $mg_item zero]
416 if !$firstzero { set firstzero 0 }
420 ## @brief Returns all the zeroes at the average defocus
423 global project_item theimg
424 set wc [getControlWindow $theimg]
425 set Cs [expr 1e7 * [.wctf.vca.cs_entry get]]
426 set lambda [getWavelength]
427 # puts "Wavelength $lambda angstrom"
428 set invl2Cs [expr 1.0 / ($lambda * $lambda * $Cs)]
429 set inv2l3Cs [expr 2.0 / ($lambda * $lambda * $lambda * $Cs)]
430 set def_avg [expr 1e4 * [.wctf.def_avg_scale get]]
431 set ctf_fz [expr $def_avg * $invl2Cs]
432 # set max_s [expr 0.5 / [.pixel_size.entry get]]
433 set max_s [expr 0.5 / [$wc.pixel_size.x get]]
434 set nz [expr lambda*def_avg*max_s*max_s - $lambda*$lambda*$lambda*$Cs*max_s*max_s*max_s*max_s/2]
435 if { $nz < 1 } { set nz 1 }
437 for {set i 0} {$i < $nz} {incr i 1} {
438 set nt [expr ($i+1)*$inv2l3Cs]
439 set ctf_fz2 [expr $ctf_fz * $ctf_fz]
440 if { $ctf_fz2 > $nt } {
441 append zeroes " " [expr sqrt(ctf_fz - sqrt(ctf_fz2 - $nt))]
447 ## @brief Calculates the radial power spectrum
451 global project_item theimg
453 set wc [getControlWindow $theimg]
454 set n [$wc.image.scale get]
455 set mg_item [micrographItem $n]
456 set rps [Bmg rps $mg_item]
457 if { [lsearch $rps "Radial"] > -1 } {
458 tk_messageBox -message $rps \
459 -icon question -type ok \
460 -detail "The defocus or Cs values may be invalid!"
463 if { [llength $rps] < 1 } {
464 tk_messageBox -message "The radial average was not calculated!" \
465 -icon question -type ok \
466 -detail "Project item $mg_item"
468 set wri [lindex $rps end]
469 set rps [lreplace $rps end end]
472 ## @brief Calculates the contrast transfer function
479 set Cs [expr 1e7 * [.wctf.vca.cs_entry get]]
480 set ampfac [.wctf.vca.amp_entry get]
481 set phifac [expr sqrt(1 - $ampfac * $ampfac)]
482 set base_eq [.wctf.base_eq.entry get]
483 set env_eq [.wctf.env_eq.entry get]
484 set def_avg [expr 1e4 * [.wctf.def_avg_scale get]]
485 set lambda [getWavelength]
486 set hpil3Cs [expr 0.5 * $PI * $lambda * $lambda * $lambda * $Cs];
487 set pil [expr $PI * $lambda];
494 for {set i 0} {$i < $n} {incr i 1} {
495 set s [expr $i * $step]
496 set s2 [expr $s * $s]
497 set s3 [expr $s * $s2]
498 set s4 [expr $s2 * $s2]
499 set dphi [expr $hpil3Cs*$s4 - $pil*$def_avg*$s2]
500 set amp [expr $ampfac*cos($dphi) - $phifac*sin($dphi)]
502 set b [expr $base_eq]
503 set c [expr $amp * $amp]
510 ## @brief Returns the RMSD between the experimental and calculated RPS curves.
514 global step base envl
516 global ctf_hires ctf_lores
517 set wc [getControlWindow $theimg]
518 set pixel_size [$wc.pixel_size.x get]
519 set lores [.wctf.pp.lores_entry get]
520 set hires [.wctf.pp.hires_entry get]
521 if { $hires < [expr 2.1 * $pixel_size] } {
522 set hires [expr 2.1 * $pixel_size]
523 .wctf.pp.hires_entry delete 0 end
524 .wctf.pp.hires_entry insert 0 $hires
527 if { $lores < $hires } {
528 set lores [expr 2 * $hires]
529 .wctf.pp.lores_entry delete 0 end
530 .wctf.pp.lores_entry insert 0 $lores
533 set nr [llength $rps]
534 set ilo [expr int(1.0 / ($lores*$step))]
535 set ihi [expr int(1.0 / ($hires*$step))]
536 if { $ihi <= $ilo } { set ihi [expr $ilo + 1] }
537 if { $ilo < 1 } { set ilo 1 }
538 if { $ihi >= $nr } { set ihi [expr $nr - 1] }
541 for {set i $ilo} {$i <= $ihi} {incr i 1} {
542 set c [expr [lindex $base $i] + [lindex $envl $i] * [lindex $ctf $i]]
543 set d [expr [lindex $rps $i] - $c]
545 # set w [expr 1.0 - [lindex $ctf $i]]
547 set R [expr $R + $d * $d * $w]
552 set R [expr sqrt($R/$n)]
559 ## @brief Attempts an automatic fit of the baseline, envelope and CTF functions
561 proc autoCTFfit { level } {
562 global project_item theimg
563 global cont_update base_type env_type
564 set wc [getControlWindow $theimg]
565 set n [$wc.image.scale get]
566 set mg_item [micrographItem $n]
569 set lo_res [$w.pp.lores_entry get]
570 set hi_res [$w.pp.hires_entry get]
571 set def_start [expr [$w.def.start get] * 1e4]
572 set def_end [expr [$w.def.end get] * 1e4]
573 set def_inc [expr [$w.def.inc get] * 1e4]
574 # puts "Fitting the CTF"
575 # puts "base_type=$base_type"
576 set ps [Bmg get $mg_item pixel_size]
577 if { [lindex $ps 0] > 1.5 } { # Make sure we are not trying to fit the water ring
578 if { $base_type > 3 } {
579 set base_type [expr $base_type - 3]
582 set old_env_type [Bmg get $mg_item envelope_type]
583 Bmg set $mg_item baseline_type $base_type
584 Bmg set $mg_item envelope_type $env_type
585 set result [Bmg ctf_fit $mg_item $level $lo_res $hi_res $def_start $def_end $def_inc]
586 if { $level == 1 && $env_type != $old_env_type } {
587 set result [Bmg ctf_fit $mg_item 2 $lo_res $hi_res]
590 if { [string length $result] < 2 } { return }
591 $w.base_eq.entry delete 0 end
592 $w.base_eq.entry insert 0 [lindex $result 0]
593 $w.env_eq.entry delete 0 end
594 $w.env_eq.entry insert 0 [lindex $result 1]
595 $w.def_avg_scale set [expr 1e-4 * [lindex $result 2]]
596 $w.def_dev_scale set [expr 1e-4 * [lindex $result 3]]
597 $w.ast_ang_scale set [lindex $result 4]
598 if { $level == 4 } { set cont_update 1 }
602 ## @brief Draws a number of ellipses at the CTF zeroes.
604 # @param theimg Image identifier.
606 proc drawEllipse { theimg } {
607 global PI show_rings step
608 set c [getImageCanvas $theimg]
610 if { $show_rings < 1 } { return }
611 set wc [getControlWindow $theimg]
612 set pixel_size [$wc.pixel_size.x get]
613 set Cs [expr 1e7 * [.wctf.vca.cs_entry get]]
614 set phi [expr asin([.wctf.vca.amp_entry get]) / $PI]
615 set lambda [getWavelength]
616 # puts "Wavelength $lambda angstrom"
617 set invl2Cs [expr 1.0 / ($lambda * $lambda * $Cs)]
618 set inv2l3Cs [expr 2.0 / ($lambda * $lambda * $lambda * $Cs)]
619 set width [Bimage get $theimg width]
620 set height [Bimage get $theimg height]
621 set scale [$wc.scale.scale get]
622 # set originx [expr $scale * ($width + 1) / 2]
623 # set originy [expr $scale * ($height + 2) / 2]
624 set originx [expr $scale * $width / 2]
625 set originy [expr $scale * $height / 2]
626 set def_avg [expr 1e4 * [.wctf.def_avg_scale get]]
627 set def_dev [expr 1e4 * [.wctf.def_dev_scale get]]
628 set ast_ang [expr [.wctf.ast_ang_scale get] * $PI / 180]
629 if { $def_dev > [expr 0.99 * $def_avg] } {
630 set def_dev [expr 0.99 * $def_avg]
631 .wctf.def_dev_scale set [expr $def_dev * 1e-4]
633 set ellip2 [expr ( $def_avg + $def_dev ) / ( $def_avg - $def_dev ) ]
634 set fz [expr $def_avg * $invl2Cs]
635 set fz2 [expr $fz * $fz]
636 set nmax [expr int($lambda * $def_avg / (4 * $pixel_size * $pixel_size))]
638 if { $nmax > 10 } { set nmax 10 }
639 set astart [expr $PI/2]
640 set aend [expr $astart + $show_rings * $PI + 0.05]
641 set astep [expr $PI/36]
642 for { set n 1 } { $n < $nmax } { incr n 1 } {
644 set t [expr ($n - $phi) * $inv2l3Cs]
646 set r [expr ($scale / $step) * sqrt($fz - sqrt($fz2 - $t))]
648 # puts "Pixel distance $r"
650 set da [expr $a - $ast_ang]
651 set re [expr $r * sqrt( ($ellip2 + 1) / \
652 (2 * ($ellip2 * cos($da) * cos($da) + sin($da) * sin($da))) ) ]
653 set xl [expr $re*cos($a) + $originx]
654 set yl [expr $re*sin($a) + $originy]
655 set yl [expr $scale * $height - $yl]
656 for {set a $astart} {$a <= $aend} {set a [expr $a + $astep]} {
657 set da [expr $a - $ast_ang]
658 set re [expr $r * sqrt( ($ellip2 + 1) / \
659 (2 * ($ellip2 * cos($da) * cos($da) + sin($da) * sin($da))) ) ]
660 set x [expr $re*cos($a) + $originx]
661 set y [expr $re*sin($a) + $originy]
662 # y is flipped because the canvas origin is upper left
663 set y [expr $scale * $height - $y]
664 $c create line $xl $yl $x $y -dash 2 -fill yellow -smooth yes -width 1 -tags ellipse
671 ## @brief Draws four curves: baseline, envelope, CTF and RPS
675 global cont_update sub_base
677 global base envl ctf rps wri
679 global ctf_scale_x ctf_scale_y
680 if { $cont_update || [string length $rps] < 2 } {
692 set orix [.wctf.pp.ox_entry get]
693 set oriy [expr $plot_height - [.wctf.pp.oy_entry get]]
694 set ctf_scale_x [.wctf.pp.sx_entry get]
695 set ctf_scale_y [.wctf.pp.sy_entry get]
696 # set wri [lindex $rps end]
697 # set rps [lreplace $rps end end]
699 for {set i 1} {$i < $n} {incr i 1} {
700 set x [expr $orix + $ctf_scale_x * $i]
701 if { [string length $rps] > 2 } {
702 set y [lindex $rps $i]
706 set b [lindex $base $i]
707 set e [lindex $envl $i]
708 set cv [lindex $ctf $i]
711 set cv [expr $e * $cv]
714 set cv [expr $b + $e * $cv]
717 if { [string length $rps] > 2 } {
718 set y [expr $oriy - $ctf_scale_y * $y]
719 append rps_line " " $x " " $y
721 set b [expr $oriy - $ctf_scale_y * $b]
722 set e [expr $oriy - $ctf_scale_y * $e]
723 set cv [expr $oriy - $ctf_scale_y * $cv]
724 append base_line " " $x " " $b
725 append env_line " " $x " " $e
726 append ctf_line " " $x " " $cv
728 $c create line $base_line -fill green -smooth yes -width 1 -tags ctf
729 $c create line $env_line -fill orange -smooth yes -width 1 -tags ctf
730 $c create line $ctf_line -fill red -smooth yes -width 1 -tags ctf
731 if { [string length $rps_line] > 2 } {
732 $c create line $rps_line -fill blue -smooth yes -width 1.5 -tags rps
734 $c create line $orix 0 $orix [expr 2 * $oriy] \
735 -fill black -smooth yes -width 1 -tags xscale
736 $c create line $orix $oriy [expr $orix + $ctf_scale_x * $n] $oriy \
737 -fill black -smooth yes -width 1 -tags xscale
739 for {set i 0} {$i < $n} {incr i 50} {
740 set x [expr $orix + $ctf_scale_x * $i]
741 if { $i } { set res [format "%5.1f" [expr 1.0 /($i*$step)]] }
742 $c create text $x [expr $oriy + 10] -text $res -fill black -tags xscale
743 $c create line $x $oriy $x [expr $oriy - 5] -fill black -smooth yes -width 1 -tags xscale
745 set lores [.wctf.pp.lores_entry get]
746 set x [expr $orix + $ctf_scale_x/($lores*$step)]
747 # puts "$oriy $plot_height"
748 $c create line $x 0 $x $plot_height \
749 -fill green -smooth yes -width 1 -tags xscale
750 set hires [.wctf.pp.hires_entry get]
751 set x [expr $orix + $ctf_scale_x/($hires*$step)]
752 $c create line $x 0 $x $plot_height \
753 -fill green -smooth yes -width 1 -tags xscale
755 # set Z [expr 1.0 / [getFirstZero]]
757 $c create text [expr $orix + 300] 30 -text [format "R = %10.6f" $R] -anchor w -fill black -tags rps
758 $c create text [expr $orix + 300] 50 -text [format "First zero = %10.2f A" $Z] -anchor w -fill black -tags rps
759 $c create text [expr $orix + 300] 70 -text [format "Water ring index = %8.4f" $wri] -anchor w -fill black -tags rps
762 ## @brief Shows the spatial frequency at that point where the mouse is moved.
764 # @param c Canvas object.
766 # @param y Coordinates on the canvas.
768 proc showFrequency { c x y } {
771 set orix [.wctf.pp.ox_entry get]
772 set r [expr $ctf_scale_x * 1.0 / (($x - $orix)*$step)]
774 $w.pp.value config -text [format "%6.2f" $r]
777 ## @brief Saves CTF curves to a text file
782 global step base envl
784 if { [string length $rps] < 2 } { return }
785 set filename [Bimage get $theimg filename]
786 set ctffile "[file rootname [file tail $filename]].txt"
787 set ctffile [tk_getSaveFile -initialfile $ctffile -defaultextension .txt]
788 if { [string length $ctffile] < 1 } { return }
789 set fctf [open $ctffile w]
790 set wc [getControlWindow $theimg]
791 set pixel_size [$wc.pixel_size.x get]
794 puts $fctf "File name: $filename"
795 puts $fctf "Pixel size: $pixel_size angstrom/pixel"
796 puts $fctf "Acceleration voltage: [$w.vca.volt_entry get] kV"
797 puts $fctf "Cs: [$w.vca.cs_entry get] mm"
798 puts $fctf "Amplitude contribution: [$w.vca.amp_entry get]"
799 puts $fctf "Defocus average: [$w.def_avg_scale get] um"
800 puts $fctf "Defocus deviation: [$w.def_dev_scale get] um"
801 puts $fctf "Astigmatism angle: [$w.ast_ang_scale get] degrees"
802 puts $fctf "Baseline: [$w.base_eq.entry get]"
803 puts $fctf "Envelope: [$w.env_eq.entry get]"
804 puts $fctf "First zero: [getFirstZero] angstrom"
806 puts $fctf "s Baseline Envelope CTF RPS"
807 for {set i 0} {$i < $n} {incr i 1} {
808 set s [expr $step * $i]
809 set b [lindex $base $i]
810 set e [lindex $envl $i]
811 set c [lindex $ctf $i]
812 set r [lindex $rps $i]
813 puts $fctf "$s $b $e $c $r"
820 ## @brief Updates CTF parameters from the micrograph parameters in memory.
823 proc getCTFparam { } {
824 global project_item theimg imgtype
826 global volt Cs amp_fac base_type env_type
828 if ![winfo exists $w] { return }
829 if ![Bmg exists] { return }
830 set wc [getControlWindow $theimg]
831 set n [$wc.image.scale get]
832 set mg_item [micrographItem $n]
833 if { $imgtype != "ps" } { return }
834 # if [regexp "Field" $project_item] { return }
835 # puts "Updating CTF parameters: $project_item"
836 set volt [expr 0.001 * [Bmg get $mg_item volt]]
837 set Cs [expr 1e-7 * [Bmg get $mg_item Cs]]
838 set amp_fac [Bmg get $mg_item amp_fac]
839 set defocus [expr 1e-4 * [Bmg get $mg_item defocus]]
840 # puts "Updating CTF parameters: defocus = $defocus"
841 set def_dev [expr 1e-4 * [Bmg get $mg_item defocus_deviation]]
842 set ast_ang [Bmg get $mg_item astigmatism_angle]
843 set base_eq [Bmg get $mg_item baseline]
844 set base_type [Bmg get $mg_item baseline_type]
845 set env_eq [Bmg get $mg_item envelope]
846 set env_type [Bmg get $mg_item envelope_type]
847 # puts "Baseline type = $base_type"
848 if { $volt < 1 } { set volt 120 }
849 if { $Cs < 0.000001 } { set Cs 2 }
850 if { $amp_fac < 0 } { set amp_fac 0 }
851 if { $amp_fac > 1 } { set amp_fac 1 }
852 if { $defocus < 0.001 } { set defocus 2 }
853 if { [string length $base_eq] < 1 } { set base_eq {0.2*exp(-5000*$s2) + 0.14*exp(-300*$s2) + 0.4} }
854 if { [string length $env_eq] < 1 } { set env_eq {0.2*exp(-1000*$s2)} }
855 set filename [Bmg get $mg_item filename ps]
856 # puts "Updating CTF parameters: $mg_item"
857 # puts "Power spectrum filename: -$filename-"
858 wm title $w [list "CTF:" $filename "(" $n ")"]
859 updateItemID $w.itemid
860 $w.vca.volt_entry delete 0 end
861 $w.vca.volt_entry insert 0 $volt
862 $w.vca.cs_entry delete 0 end
863 $w.vca.cs_entry insert 0 $Cs
864 $w.vca.amp_entry delete 0 end
865 $w.vca.amp_entry insert 0 $amp_fac
866 $w.def_avg_scale set $defocus
867 $w.def_dev_scale set $def_dev
868 $w.ast_ang_scale set $ast_ang
869 $w.base_eq.entry delete 0 end
870 $w.base_eq.entry insert 0 $base_eq
871 $w.env_eq.entry delete 0 end
872 $w.env_eq.entry insert 0 $env_eq
875 # puts "Updated CTF parameters"
878 ## @brief Saves CTF parameters to the micrograph parameters in memory.
881 proc setCTFparam { } {
883 if ![winfo exists $w] { return }
884 global project_item theimg
885 global volt Cs amp_fac base_type env_type
886 global ctf_hires ctf_lores
887 set wc [getControlWindow $theimg]
888 set n [$wc.image.scale get]
889 set mg_item [micrographItem $n]
890 set volt [$w.vca.volt_entry get]
891 set Cs [$w.vca.cs_entry get]
892 set amp_fac [$w.vca.amp_entry get]
893 set ctf_lores [$w.pp.lores_entry get]
894 set ctf_hires [$w.pp.hires_entry get]
895 if ![Bmg exists] { createMgParam }
896 set filename [Bmg get $mg_item filename ps]
897 wm title $w [list "CTF:" $filename "(" $n ")"]
898 Bmg set $mg_item volt [expr 1000 * $volt]
899 Bmg set $mg_item Cs [expr 1e7 * $Cs]
900 Bmg set $mg_item amp_fac $amp_fac
901 Bmg set $mg_item defocus [expr 1e4 * [$w.def_avg_scale get]]
902 Bmg set $mg_item defocus_deviation [expr 1e4 * [$w.def_dev_scale get]]
903 Bmg set $mg_item astigmatism_angle [$w.ast_ang_scale get]
904 Bmg set $mg_item baseline_type $base_type
905 Bmg set $mg_item baseline [$w.base_eq.entry get]
906 Bmg set $mg_item envelope_type $env_type
907 Bmg set $mg_item envelope [$w.env_eq.entry get]
908 Bmg get $mg_item zero
909 # puts "Setting CTF parameters: base_type=$base_type"