| 1 |
## -*- cperl -*- |
| 2 |
## |
| 3 |
## This file is part of Athena, copyright (c) 2001-2006, 2008 Bruce Ravel |
| 4 |
## |
| 5 |
## This section of the code contains subroutines associated with |
| 6 |
## data calibration. |
| 7 |
|
| 8 |
|
| 9 |
sub calibrate { |
| 10 |
Echo("No data!"), return unless $current; |
| 11 |
my $space = $_[0]; |
| 12 |
|
| 13 |
##Echo("You cannot align to the Default Parameters"), return |
| 14 |
Echo("No data!"), return if ($current eq "Default Parameters"); |
| 15 |
|
| 16 |
my @keys = (); |
| 17 |
#sort {($list->bbox($groups{$a}->{text}))[1] <=> |
| 18 |
# ($list->bbox($groups{$b}->{text}))[1]} (keys (%marked)); |
| 19 |
foreach my $k (&sorted_group_list) { |
| 20 |
($groups{$k}->{is_xmu}) and push @keys, $k; |
| 21 |
}; |
| 22 |
|
| 23 |
($groups{$current}->{bkg_z}, $groups{$current}->{fft_edge}) |
| 24 |
= find_edge($groups{$current}->{bkg_e0}); |
| 25 |
my %cal_params = (cal_to => Xray::Absorption->get_energy($groups{$current}->{bkg_z}, |
| 26 |
$groups{$current}->{fft_edge}), |
| 27 |
e0 => $groups{$current}->{bkg_e0}, |
| 28 |
display => "deriv(E)", |
| 29 |
iterations => 0, |
| 30 |
); |
| 31 |
SWITCH: { |
| 32 |
$cal_params{display} = 'mu(E)', last SWITCH if ($config{calibrate}{calibrate_default} eq 'x'); |
| 33 |
$cal_params{display} = 'norm(E)', last SWITCH if ($config{calibrate}{calibrate_default} eq 'n'); |
| 34 |
$cal_params{display} = 'deriv(E)', last SWITCH if ($config{calibrate}{calibrate_default} eq 'd'); |
| 35 |
$cal_params{display} = 'second deriv', last SWITCH if ($config{calibrate}{calibrate_default} eq '2'); |
| 36 |
}; |
| 37 |
|
| 38 |
my $ps = $project_saved; |
| 39 |
my @save = ($plot_features{emin}, $plot_features{emax}); |
| 40 |
$plot_features{emin} = $config{calibrate}{emin}; |
| 41 |
$plot_features{emax} = $config{calibrate}{emax}; |
| 42 |
project_state($ps); # don't toggle if currently saved |
| 43 |
|
| 44 |
$fat_showing = 'calibrate'; |
| 45 |
$hash_pointer = \%cal_params; |
| 46 |
map {$_ -> configure(-state=>'disabled')} |
| 47 |
($data_menu, $merge_menu, $anal_menu, $settings_menu); |
| 48 |
$fat -> packForget; |
| 49 |
my $cal = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3) |
| 50 |
-> pack(-fill=>'both', -expand=>1); |
| 51 |
#$cal -> packPropagate(0); |
| 52 |
$which_showing = $cal; |
| 53 |
|
| 54 |
|
| 55 |
$cal -> Label(-text=>"Data calibration", |
| 56 |
-font=>$config{fonts}{large}, |
| 57 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 58 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w'); |
| 59 |
|
| 60 |
## select the data set to calibrate |
| 61 |
my $frame = $cal -> Frame(-borderwidth=>2, -relief=>'flat') |
| 62 |
-> pack(-side=>'top', -fill=>'x', -padx=>8); |
| 63 |
$frame -> Label(-text=>"Group: ", |
| 64 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 65 |
) |
| 66 |
-> grid(-row=>0, -column=>0, -sticky=>'e'); |
| 67 |
$widget{cal_group} = $frame -> Label(-text=>$groups{$current}->{label}, |
| 68 |
-foreground=>$config{colors}{button}) |
| 69 |
-> grid(-row=>0, -column=>1, -sticky=>'w', -padx=>3); |
| 70 |
|
| 71 |
|
| 72 |
$cal -> Frame(-background=>$config{colors}{darkbackground}) |
| 73 |
-> pack(-side=>'bottom', -expand=>1, -fill=>'both'); |
| 74 |
$cal -> Button(-text=>'Return to the main window', @button_list, |
| 75 |
-background=>$config{colors}{background2}, |
| 76 |
-activebackground=>$config{colors}{activebackground2}, |
| 77 |
-command=>sub{set_properties(1, $current, 0); |
| 78 |
reset_window($cal, "calibration", \@save); |
| 79 |
}) |
| 80 |
-> pack(-side=>'bottom', -fill=>'x'); |
| 81 |
|
| 82 |
## help button |
| 83 |
$cal -> Button(-text=>'Document section: energy calibration', @button_list, |
| 84 |
-command=>sub{pod_display("process::cal.pod")}) |
| 85 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>4); |
| 86 |
|
| 87 |
|
| 88 |
# pick display mode (mu, norm, deriv) |
| 89 |
#$frame = $cal -> Frame(-borderwidth=>2, -relief=>'sunken') |
| 90 |
# -> pack(-fill=>'x'); |
| 91 |
$frame -> Label(-text=>"Display: ", |
| 92 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 93 |
) |
| 94 |
-> grid(-row=>1, -column=>0, -sticky=>'e'); |
| 95 |
$widget{display} = $frame -> Optionmenu(-textvariable => \$cal_params{display}, |
| 96 |
-borderwidth=>1, |
| 97 |
-options => [qw(mu(E) norm(E) deriv(E)), 'second deriv'], |
| 98 |
-command => sub{ |
| 99 |
$widget{calib_zero} -> configure(-state=>($cal_params{display} eq 'second deriv') ? 'normal' : 'disabled'); |
| 100 |
my $str = 'em'; |
| 101 |
($str = 'emn') if ($cal_params{display} eq 'norm(E)'); |
| 102 |
($str = 'emd') if ($cal_params{display} eq 'deriv(E)'); |
| 103 |
($str = 'em2') if ($cal_params{display} eq 'second deriv'); |
| 104 |
$str .= 's' x $cal_params{iterations}; |
| 105 |
$cal_params{str} = $str; |
| 106 |
$plot_features{suppress_markers} = 1; |
| 107 |
$groups{$current}->plotE($str, $dmode, \%plot_features, \@indicator); |
| 108 |
$plot_features{suppress_markers} = 0; |
| 109 |
&cal_marker($current, $cal_params{e0}, $cal_params{str}); |
| 110 |
} ) |
| 111 |
-> grid(-row=>1, -column=>1, -sticky =>'w', -padx=>3); |
| 112 |
|
| 113 |
$frame -> Label(-text=>'Smoothing: ', |
| 114 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 115 |
) |
| 116 |
-> grid(-row=>2, -column=>0, -sticky=>'e'); |
| 117 |
$frame -> NumEntry(-width => 4, |
| 118 |
-orient => 'horizontal', |
| 119 |
-foreground => $config{colors}{foreground}, |
| 120 |
-textvariable => \$cal_params{iterations}, |
| 121 |
-minvalue => 0, |
| 122 |
-browsecmd => sub{ $widget{calib_replot}->invoke }, |
| 123 |
-command => sub{ $widget{calib_replot}->invoke }, |
| 124 |
) |
| 125 |
-> grid(-row=>2, -column=>1, -sticky =>'w', -padx=>3); |
| 126 |
$frame -> Label(-text=>'Reference at: ', |
| 127 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 128 |
) |
| 129 |
-> grid(-row=>3, -column=>0, -sticky=>'e'); |
| 130 |
$frame -> Entry(-textvariable=> \$cal_params{e0}, -width=>12) |
| 131 |
-> grid(-row=>3, -column=>1, -sticky =>'w', -padx=>3); |
| 132 |
$frame -> Label(-text=>'Calibrate to: ', |
| 133 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 134 |
) |
| 135 |
-> grid(-row=>4, -column=>0, -sticky=>'e'); |
| 136 |
$frame -> Entry(-textvariable=> \$cal_params{cal_to}, -width=>12) |
| 137 |
-> grid(-row=>4, -column=>1, -sticky =>'w', -padx=>3); |
| 138 |
|
| 139 |
## pluck button |
| 140 |
$widget{calib_select} = |
| 141 |
$frame -> Button(-text=>'Select a point', @button_list, |
| 142 |
-command=>sub{ |
| 143 |
Error("Calibration selection aborted: " . |
| 144 |
$groups{$current}->{label} . " is not an xmu group."), |
| 145 |
return unless ($groups{$current}->{is_xmu}); |
| 146 |
$cal_params{e0} = &cal_pluck($current, $cal_params{str}); |
| 147 |
my $str = 'em'; |
| 148 |
($str = 'emn') if ($cal_params{display} eq 'norm(E)'); |
| 149 |
($str = 'emd') if ($cal_params{display} eq 'deriv(E)'); |
| 150 |
($str = 'em2') if ($cal_params{display} eq 'second deriv'); |
| 151 |
$str .= 's' x $cal_params{iterations}; |
| 152 |
$cal_params{str} = $str; |
| 153 |
$plot_features{suppress_markers} = 1; |
| 154 |
$groups{$current}->plotE($str, $dmode, \%plot_features, \@indicator); |
| 155 |
$plot_features{suppress_markers} = 0; |
| 156 |
&cal_marker($current, $cal_params{e0}, $cal_params{str}); |
| 157 |
Echo("You chose $cal_params{e0}"); |
| 158 |
}) |
| 159 |
-> grid(-row=>5, -column=>0, -columnspan=>2, -sticky=>'we'); |
| 160 |
|
| 161 |
$widget{calib_zero} = |
| 162 |
$frame -> Button(-text=>'Find zero-crossing', @button_list, |
| 163 |
-state=>'disabled', |
| 164 |
-command=>sub{cal_zero($current, \%cal_params)}, |
| 165 |
) |
| 166 |
-> grid(-row=>6, -column=>0, -columnspan=>2, -sticky=>'we'); |
| 167 |
|
| 168 |
## replot button |
| 169 |
$widget{calib_replot} = |
| 170 |
$frame -> Button(-text=>'Replot', @button_list, |
| 171 |
-command=>sub{ |
| 172 |
Error("Replot aborted: " . |
| 173 |
$groups{$current}->{label} . " is not an xmu group."), |
| 174 |
return unless ($groups{$current}->{is_xmu}); |
| 175 |
my $str = 'em'; |
| 176 |
($str = 'emn') if ($cal_params{display} eq 'norm(E)'); |
| 177 |
($str = 'emd') if ($cal_params{display} eq 'deriv(E)'); |
| 178 |
($str = 'em2') if ($cal_params{display} eq 'second deriv'); |
| 179 |
$str .= 's' x $cal_params{iterations}; |
| 180 |
##print $str, $/; |
| 181 |
$cal_params{str} = $str; |
| 182 |
$plot_features{suppress_markers} = 1; |
| 183 |
$groups{$current}->plotE($str, $dmode, \%plot_features, \@indicator); |
| 184 |
$plot_features{suppress_markers} = 0; |
| 185 |
&cal_marker($current, $cal_params{e0}, $cal_params{str}); |
| 186 |
Echo("Replotted $groups{$current}->{label}"); |
| 187 |
}) |
| 188 |
-> grid(-row=>7, -column=>0, -columnspan=>2, -sticky=>'we'); |
| 189 |
|
| 190 |
## calibrate button |
| 191 |
$widget{calib_calibrate} = |
| 192 |
$frame -> Button(-text=>'Calibrate', @button_list, |
| 193 |
-command=>sub{ |
| 194 |
Echo("Calibration aborted: \"$groups{$current}->{label}\" is frozen"), return |
| 195 |
if $groups{$current}->{frozen}; |
| 196 |
Error("Calibration aborted: " . |
| 197 |
$groups{$current}->{label} . " is not an xmu group."), |
| 198 |
return unless ($groups{$current}->{is_xmu}); |
| 199 |
my $delta = $cal_params{cal_to} - $cal_params{e0}; |
| 200 |
$cal_params{e0} = $cal_params{cal_to}; |
| 201 |
$groups{$current}->{bkg_eshift} += $delta; |
| 202 |
$groups{$current}->{bkg_eshift} = sprintf("%.4f", $groups{$current}->{bkg_eshift}); |
| 203 |
if ($groups{$current}->{reference} and exists $groups{$groups{$current}->{reference}}) { |
| 204 |
$groups{$groups{$current}->{reference}}->make(bkg_eshift=>$groups{$current}->{bkg_eshift}); |
| 205 |
$groups{$groups{$current}->{reference}}->make(bkg_e0=>$groups{$groups{$current}->{reference}}->{bkg_e0}+$groups{$current}->{bkg_eshift}); |
| 206 |
}; |
| 207 |
$groups{$current}->{bkg_e0} = $cal_params{e0}; |
| 208 |
$groups{$current} -> make(update_bkg=>1); |
| 209 |
my $str = 'em'; |
| 210 |
($str = 'emn') if ($cal_params{display} eq 'norm(E)'); |
| 211 |
($str = 'emd') if ($cal_params{display} eq 'deriv(E)'); |
| 212 |
($str = 'em2') if ($cal_params{display} eq 'second deriv'); |
| 213 |
$str .= 's' x $cal_params{iterations}; |
| 214 |
$cal_params{str} = $str; |
| 215 |
$plot_features{suppress_markers} = 1; |
| 216 |
$groups{$current}->plotE($str, $dmode, \%plot_features, \@indicator); |
| 217 |
$plot_features{suppress_markers} = 0; |
| 218 |
&cal_marker($current, $cal_params{e0}, $cal_params{str}); |
| 219 |
$groups{$current}->make(update_bkg=>1); |
| 220 |
project_state(0); |
| 221 |
Echo("Calibrated to $cal_params{cal_to}"); |
| 222 |
}) |
| 223 |
-> grid(-row=>8, -column=>0, -columnspan=>2, -sticky=>'we'); |
| 224 |
|
| 225 |
|
| 226 |
$widget{calib_zero} -> configure(-state=>($cal_params{display} eq 'second deriv') ? 'normal' : 'disabled'); |
| 227 |
$groups{$current} -> dispose("set $current.deriv = deriv($current.xmu)/deriv($current.energy)\n", $dmode); |
| 228 |
my $str = 'em'; |
| 229 |
($str = 'emn') if ($cal_params{display} eq 'norm(E)'); |
| 230 |
($str = 'emd') if ($cal_params{display} eq 'deriv(E)'); |
| 231 |
($str = 'em2') if ($cal_params{display} eq 'second deriv'); |
| 232 |
$str .= 's' x $cal_params{iterations}; |
| 233 |
$cal_params{str} = $str; |
| 234 |
$plot_features{suppress_markers} = 1; |
| 235 |
$groups{$current}->plotE($str, $dmode, \%plot_features, \@indicator); |
| 236 |
$plot_features{suppress_markers} = 0; |
| 237 |
&cal_marker($current, $cal_params{e0}, $cal_params{str}); |
| 238 |
$plotsel -> raise('e'); |
| 239 |
$top -> update; |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
sub cal_pluck { |
| 245 |
my ($group, $str) = @_; |
| 246 |
Error("Calibration display aborted: " . $groups{$group}->{label} . " is not an xmu group."), |
| 247 |
return unless ($groups{$group}->{is_xmu}); |
| 248 |
Echonow("Select a point from the plot"); |
| 249 |
$groups{$group} -> dispose('cursor(crosshair=true)', 1); |
| 250 |
my ($xx, $yy) = (Ifeffit::get_scalar('cursor_x'), Ifeffit::get_scalar('cursor_y')); |
| 251 |
return sprintf("%.3f", $xx); |
| 252 |
}; |
| 253 |
|
| 254 |
## show the edge energy marker in a calibration plot, take care to |
| 255 |
## deal well with derivative |
| 256 |
sub cal_marker { |
| 257 |
my ($g, $e, $str) = @_; |
| 258 |
Error("Calibration display aborted: " . $groups{$g}->{label} . " is not an xmu group."), |
| 259 |
return unless ($groups{$g}->{is_xmu}); |
| 260 |
my $command = ""; |
| 261 |
my $xarr = "\"$g.energy+$groups{$g}->{bkg_eshift}\""; |
| 262 |
|
| 263 |
if ($str =~ /(s+)/) { |
| 264 |
my $iterations = length($1); |
| 265 |
my $suff = 'xmu'; |
| 266 |
if ($groups{$g}->{not_data}) { |
| 267 |
$suff = 'det'; |
| 268 |
} elsif ($str =~ /n/) { |
| 269 |
$suff = 'norm'; |
| 270 |
}; |
| 271 |
$groups{$g}->dispose("set $g.smooth = $g.$suff", $dmode); |
| 272 |
foreach (1 .. $iterations) { |
| 273 |
$groups{$g}->dispose("set $g.smooth = smooth($g.smooth)", $dmode); |
| 274 |
}; |
| 275 |
}; |
| 276 |
|
| 277 |
my $suff = "xmu"; |
| 278 |
($str =~ /n/) and ($suff = "norm"); |
| 279 |
($str =~ /s/) and ($suff = "smooth"); |
| 280 |
($str =~ /d/) and ($suff = "deriv"); |
| 281 |
if ($str =~ /2/) { |
| 282 |
$command .= "set $g.second = deriv($g.deriv)/deriv($g.energy)\n"; |
| 283 |
$suff = "second"; |
| 284 |
}; |
| 285 |
my $yarr = "$g.$suff"; |
| 286 |
my $yoff = $groups{$g}->{plot_yoffset}; |
| 287 |
|
| 288 |
$command .= "pmarker $xarr, $yarr, $e, $plot_features{marker}, $plot_features{markercolor}, $yoff\n"; |
| 289 |
##($str =~ /^d/) and ($command .= "erase $g.deriv\n"); |
| 290 |
|
| 291 |
$groups{$g} -> dispose($command, $dmode); |
| 292 |
$last_plot='e'; |
| 293 |
}; |
| 294 |
|
| 295 |
sub cal_zero { |
| 296 |
my ($g, $r_hash) = @_; |
| 297 |
my $str = $$r_hash{str}; |
| 298 |
$$r_hash{zero_skip_plot} ||= 0; |
| 299 |
my $suff = 'xmu'; |
| 300 |
if ($groups{$g}->{not_data}) { |
| 301 |
$suff = 'det'; |
| 302 |
} elsif ($str =~ /n/) { |
| 303 |
$suff = 'norm'; |
| 304 |
}; |
| 305 |
if ($str =~ /(s+)/) { |
| 306 |
my $iterations = length($1); |
| 307 |
$groups{$g}->dispose("set $g.smooth = $g.$suff", $dmode); |
| 308 |
foreach (1 .. $iterations) { |
| 309 |
$groups{$g}->dispose("set $g.smooth = smooth($g.smooth)", $dmode); |
| 310 |
}; |
| 311 |
$suff = 'smooth'; |
| 312 |
}; |
| 313 |
$groups{$g}->dispose("set($g.y = deriv($g.$suff)/deriv($g.energy), $g.y = deriv($g.y)/deriv($g.energy))\n", $dmode); |
| 314 |
|
| 315 |
my @x = map {$_ + $groups{$g}->{bkg_eshift}} Ifeffit::get_array("$g.energy"); |
| 316 |
my @y = Ifeffit::get_array("$g.y"); |
| 317 |
|
| 318 |
my $e0index = 0; |
| 319 |
foreach my $e (@x) { |
| 320 |
last if ($e > $$r_hash{e0}); |
| 321 |
++$e0index; |
| 322 |
}; |
| 323 |
my ($enear, $ynear) = ($x[$e0index], $y[$e0index]); |
| 324 |
my ($ratio, $i) = (1, 1); |
| 325 |
my ($above, $below) = (0,0); |
| 326 |
while (1) { # find points that bracket the zero crossing |
| 327 |
(($above, $below) = (0,0)), last unless (exists($y[$e0index + $i]) and $y[$e0index]); |
| 328 |
$ratio = $y[$e0index + $i] / $y[$e0index]; # this ratio is negative for a points bracketing the zero crossing |
| 329 |
($above, $below) = ($e0index+$i, $e0index+$i-1); |
| 330 |
last if ($ratio < 0); |
| 331 |
(($above, $below) = (0,0)), last unless exists($y[$e0index - $i]); |
| 332 |
$ratio = $y[$e0index - $i] / $y[$e0index]; # this ratio is negative for a points bracketing the zero crossing |
| 333 |
($above, $below) = ($e0index-$i+1, $e0index-$i); |
| 334 |
last if ($ratio < 0); |
| 335 |
++$i; |
| 336 |
Error("Could not find zero crossing."), return 0 if ($i == 4000); |
| 337 |
}; |
| 338 |
Error("Could not find zero crossing."), return if (($above == 0) and ($below == 0)); |
| 339 |
|
| 340 |
## linearly interpolate between points that bracket the zero crossing |
| 341 |
$$r_hash{e0} = sprintf("%.3f", $x[$below] - ($y[$below]/($y[$above]-$y[$below])) * ($x[$above] - $x[$below])); |
| 342 |
return if $$r_hash{zero_skip_plot}; |
| 343 |
|
| 344 |
$plot_features{suppress_markers} = 1; |
| 345 |
$groups{$current}->plotE($$r_hash{str}, $dmode, \%plot_features, \@indicator); |
| 346 |
$plot_features{suppress_markers} = 0; |
| 347 |
&cal_marker($current, $$r_hash{e0}, $$r_hash{str}); |
| 348 |
|
| 349 |
$groups{$g}->dispose("erase $g.y $g.smooth", $dmode); |
| 350 |
Echo("Found zero crossing at $$r_hash{e0} in group \"$groups{$g}->{label}\"."); |
| 351 |
}; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
## END OF DATA CALIBRATION SUBSECTION |
| 356 |
########################################################################################## |