| 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 |
## difference spectra |
| 7 |
|
| 8 |
sub difference { |
| 9 |
Echo("No data!"), return unless $current; |
| 10 |
my $space = $_[0]; |
| 11 |
#$space = ($space eq 'x') ? 'em' : 'em'.$space; |
| 12 |
my %sp = (e=>'energy', n=>'energy', k=>'k', r=>'R', 'q'=>'q'); |
| 13 |
|
| 14 |
Echo("No data!"), return if ($current eq "Default Parameters"); |
| 15 |
|
| 16 |
## make the list of groups that can be difference-d in the requested space |
| 17 |
my @allkeys = &sorted_group_list; |
| 18 |
my @keys = (); |
| 19 |
my $header = ""; |
| 20 |
my $ysuff; |
| 21 |
SWITCH: { |
| 22 |
($space eq 'e') and do { |
| 23 |
foreach my $k (@allkeys) { |
| 24 |
($groups{$k}->{is_xmu}) and push @keys, $k; |
| 25 |
}; |
| 26 |
$header = "Compute the Difference of mu(E) Spectra"; |
| 27 |
$ysuff = 'xmu'; |
| 28 |
last SWITCH; |
| 29 |
}; |
| 30 |
($space eq 'n') and do { |
| 31 |
foreach my $k (@allkeys) { |
| 32 |
(($groups{$k}->{is_xmu}) or ($groups{$k}->{is_nor})) and push @keys, $k; |
| 33 |
}; |
| 34 |
$header = "Compute the Difference of Normalized Spectra"; |
| 35 |
$ysuff = $groups{$keys[0]}->{bkg_flatten} ? 'flat' : 'norm'; |
| 36 |
last SWITCH; |
| 37 |
}; |
| 38 |
($space eq 'k') and do { |
| 39 |
foreach my $k (@allkeys) { |
| 40 |
(($groups{$k}->{is_xmu}) or ($groups{$k}->{is_nor}) or ($groups{$k}->{is_chi})) |
| 41 |
and push @keys, $k; |
| 42 |
}; |
| 43 |
$header = "Compute the Difference of chi(k)"; |
| 44 |
$ysuff = 'chi'; |
| 45 |
last SWITCH; |
| 46 |
}; |
| 47 |
($space eq 'r') and do { |
| 48 |
foreach my $k (@allkeys) { |
| 49 |
($groups{$k}->{is_qsp}) or push @keys, $k; |
| 50 |
}; |
| 51 |
$header = "Compute the Difference of chi(R)"; |
| 52 |
if ($plot_features{r_mag}) {$ysuff = "chir_mag"} |
| 53 |
elsif ($plot_features{r_re}) {$ysuff = "chir_re"} |
| 54 |
elsif ($plot_features{r_im}) {$ysuff = "chir_im"} |
| 55 |
elsif ($plot_features{r_pha}) {$ysuff = "chir_pha"}; |
| 56 |
last SWITCH; |
| 57 |
}; |
| 58 |
1 and do { # difference in q, any'll do |
| 59 |
foreach my $k (@allkeys) { |
| 60 |
($groups{$k}->{not_data}) or push @keys, $k; |
| 61 |
}; |
| 62 |
$header = "Compute the Difference of chi(q)"; |
| 63 |
if ($plot_features{'q_mag'}) {$ysuff = 'chiq_mag'} |
| 64 |
elsif ($plot_features{'q_re'}) {$ysuff = "chiq_re"} |
| 65 |
elsif ($plot_features{'q_im'}) {$ysuff = "chiq_im"} |
| 66 |
elsif ($plot_features{'q_pha'}) {$ysuff = "chiq_pha"}; |
| 67 |
last SWITCH; |
| 68 |
}; |
| 69 |
}; |
| 70 |
Echo("You need two or more groups that can display in $sp{$space} to compute that difference spectrum"), |
| 71 |
return unless ($#keys >= 1); |
| 72 |
|
| 73 |
|
| 74 |
my %diff_params = (standard => $keys[0], |
| 75 |
standard_label => "1: ".$groups{$keys[0]}->{label}, |
| 76 |
keys => \@keys, |
| 77 |
space => $space, |
| 78 |
xnot => 0, |
| 79 |
xsuff => $sp{$space}, |
| 80 |
ysuff => $ysuff, |
| 81 |
xmin => 0, |
| 82 |
xmax => 0, |
| 83 |
invert => 0, |
| 84 |
components => 0, |
| 85 |
list => $list, |
| 86 |
groups => \%groups, |
| 87 |
noplot => 0, |
| 88 |
## The options are |
| 89 |
## e, n, d mu, normalized mu, or deriv of mu |
| 90 |
## kw, 0, 1, 2, 3, e chi(k) with k-weight or chi(E) with k-weight |
| 91 |
## rm, rp, rr, ri chi(R), magnitude, phase, real, imaginary |
| 92 |
## qm, qp, qr, qi chi(q), magnitude, phase, real, imaginary |
| 93 |
); |
| 94 |
if ($space =~ /[en]/) { |
| 95 |
$diff_params{xnot} = $groups{$keys[0]}->{bkg_e0}; |
| 96 |
$diff_params{xmin} = $config{diff}{emin}; |
| 97 |
$diff_params{xmax} = $config{diff}{emax}; |
| 98 |
} elsif ($space =~ /[kq]/) { |
| 99 |
$diff_params{xmin} = $config{diff}{kmin}; |
| 100 |
$diff_params{xmax} = $config{diff}{kmax}; |
| 101 |
} else { |
| 102 |
$diff_params{xmin} = $config{diff}{rmin}; |
| 103 |
$diff_params{xmax} = $config{diff}{rmax}; |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
if ($diff_params{standard} eq $current) { # make sure $current is sensible given |
| 108 |
set_properties(1, $keys[1], 0); # that $keys[0] is the standard |
| 109 |
# adjust the view |
| 110 |
my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0; |
| 111 |
($here < 0) and ($here = 0); |
| 112 |
my $full = ($list->bbox(@skinny_list))[3] + 5; |
| 113 |
$list -> yview('moveto', $here/$full); |
| 114 |
}; |
| 115 |
|
| 116 |
$fat_showing = 'diff'; |
| 117 |
$hash_pointer = \%diff_params; |
| 118 |
map {$_ -> configure(-state=>'disabled')} |
| 119 |
($data_menu, $merge_menu, $anal_menu, $settings_menu); |
| 120 |
$fat -> packForget; |
| 121 |
my $diff = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3) |
| 122 |
-> pack(-fill=>'both', -expand=>1); |
| 123 |
#$diff -> packPropagate(0); |
| 124 |
$which_showing = $diff; |
| 125 |
|
| 126 |
$diff -> Label(-text=>$header, |
| 127 |
-font=>$config{fonts}{large}, |
| 128 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 129 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w'); |
| 130 |
|
| 131 |
## select the difference standard |
| 132 |
my $frame = $diff -> Frame(-borderwidth=>2, -relief=>'flat') |
| 133 |
-> pack(-side=>'top', -fill=>'x'); |
| 134 |
$frame -> Label(-text=>"Standard: ", |
| 135 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 136 |
) |
| 137 |
-> grid(-row=>0, -column=>0); |
| 138 |
|
| 139 |
|
| 140 |
$widget{diff_menu} = $frame -> BrowseEntry(-variable => \$diff_params{standard_label}, |
| 141 |
@browseentry_list, |
| 142 |
-browsecmd => sub { |
| 143 |
my $text = $_[1]; |
| 144 |
my $this = $1 if ($text =~ /^(\d+):/); |
| 145 |
Echo("Failed to match in browsecmd. Yikes! Complain to Bruce."), return unless $this; |
| 146 |
$this -= 1; |
| 147 |
Error("Difference group aborted: You selected the same data group twice!"), |
| 148 |
return if ($diff_params{keys}->[$this] eq $current); |
| 149 |
$diff_params{standard}=$diff_params{keys}->[$this]; |
| 150 |
##$diff_params{standard_label} = $groups{$diff_params{standard}}->{label}; |
| 151 |
($diff_params{xnot} = $groups{$diff_params{standard}}->{bkg_e0}) |
| 152 |
if ($diff_params{space} =~ /[en]/); |
| 153 |
$groups{$diff_params{standard}} -> |
| 154 |
plot_difference($groups{$current}, \%diff_params, $dmode, \%plot_features); |
| 155 |
$last_plot=$space; |
| 156 |
}) |
| 157 |
-> grid(-row=>0, -column=>1); |
| 158 |
my $i = 1; |
| 159 |
foreach my $s (@keys) { |
| 160 |
$widget{diff_menu} -> insert("end", "$i: $groups{$s}->{label}"); |
| 161 |
++$i; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
## select the other group |
| 166 |
$frame -> Label(-text=>"Other: ", |
| 167 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 168 |
) |
| 169 |
-> grid(-row=>1, -column=>0); |
| 170 |
$widget{diff_unknown} = $frame -> Label(-text=>$groups{$current}->{label}, |
| 171 |
-foreground=>$config{colors}{button}) |
| 172 |
-> grid(-row=>1, -column=>1, -sticky=>'w', -pady=>2, -padx=>2); |
| 173 |
|
| 174 |
$frame -> Checkbutton(-text => 'Invert difference spectra', |
| 175 |
-selectcolor => $config{colors}{single}, |
| 176 |
-variable => \$diff_params{invert}, |
| 177 |
-command => sub{$widget{diff_replot}->invoke}, |
| 178 |
) |
| 179 |
-> grid(-row=>2, -column=>0, -columnspan=>2, -sticky=>'w', -pady=>2, -padx=>2); |
| 180 |
$frame -> Checkbutton(-text => 'Plot spectra', |
| 181 |
-selectcolor => $config{colors}{single}, |
| 182 |
-variable => \$diff_params{components}, |
| 183 |
-command => sub{$widget{diff_replot}->invoke}, |
| 184 |
) |
| 185 |
-> grid(-row=>3, -column=>0, -columnspan=>2, -sticky=>'w', -pady=>2, -padx=>2); |
| 186 |
|
| 187 |
$diff -> Frame(-background=>$config{colors}{darkbackground}) |
| 188 |
-> pack(-side=>'bottom', -expand=>1, -fill=>'both'); |
| 189 |
$diff -> Button(-text=>'Return to the main window', @button_list, |
| 190 |
-background=>$config{colors}{background2}, |
| 191 |
-activebackground=>$config{colors}{activebackground2}, |
| 192 |
-command=>sub{ |
| 193 |
$groups{$diff_params{standard}} -> |
| 194 |
dispose("erase \@group diff___diff\n", $dmode); |
| 195 |
&reset_window($diff, "difference spectra", 0); |
| 196 |
}) |
| 197 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>5); |
| 198 |
|
| 199 |
## help button |
| 200 |
$diff -> Button(-text=>'Document section: difference spectra', @button_list, |
| 201 |
-command=>sub{pod_display("analysis::diff.pod")}) |
| 202 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>4); |
| 203 |
|
| 204 |
$frame = $diff -> Frame() |
| 205 |
-> pack(-side=>'bottom', -fill=>'x', -ipadx=>5); |
| 206 |
$widget{diff_savemarked} = $frame -> |
| 207 |
Button(-text=>'Make difference groups from all MARKED groups', @button_list, |
| 208 |
-command=>sub{ |
| 209 |
$diff_params{noplot} = 1; |
| 210 |
$groups{$diff_params{standard}} -> plot_difference($groups{$current}, \%diff_params, $dmode, \%plot_features); |
| 211 |
&diff_marked($space, \%diff_params); |
| 212 |
$diff_params{noplot} = 0; |
| 213 |
}) |
| 214 |
-> pack(-side=>'left', -fill=>'x', -expand=>1, -pady=>0, -padx=>0); |
| 215 |
$widget{diff_savemarkedi} = $frame -> |
| 216 |
Checkbutton(-text=>'Integrate?', |
| 217 |
-selectcolor=>$config{colors}{single}, |
| 218 |
-variable=>\$diff_params{integrate_marked}) |
| 219 |
-> pack(-side=>'right', -pady=>0, -padx=>0); |
| 220 |
|
| 221 |
|
| 222 |
$widget{diff_save} = $diff -> |
| 223 |
Button(-text=>'Make difference group', @button_list, |
| 224 |
-command=>sub{&make_diff_group($space, $diff_params{standard}, $current, \%diff_params); }) |
| 225 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>5, -padx=>0); |
| 226 |
|
| 227 |
$widget{diff_replot} = $diff -> |
| 228 |
Button(-text=>'Replot', @button_list, |
| 229 |
-command=>sub{$groups{$diff_params{standard}} -> |
| 230 |
plot_difference($groups{$current}, \%diff_params, $dmode, \%plot_features); |
| 231 |
}) |
| 232 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>5, -padx=>0); |
| 233 |
|
| 234 |
## integration range |
| 235 |
$frame = $diff -> LabFrame(-label=>'Integrate difference spectra', |
| 236 |
-labelside=>'acrosstop', |
| 237 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 238 |
) |
| 239 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>5); |
| 240 |
my $fr = $frame -> Frame() |
| 241 |
-> pack(-side=>'top', -pady=>3); |
| 242 |
$fr -> Label(-text=> "Integration range:", -anchor=>'e', |
| 243 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 244 |
) |
| 245 |
-> pack(-side=>'left', -pady=>3, -fill=>'x'); |
| 246 |
$widget{diff_xmin} = $fr -> Entry(-width=>7, -textvariable=>\$diff_params{xmin}, |
| 247 |
#-validate=>'all', |
| 248 |
#-validatecommand=>[\&set_peak_variable, 'xmin'], |
| 249 |
) |
| 250 |
-> pack(-side=>'left', -pady=>3); |
| 251 |
$grab{diff_xmin} = $fr -> Button(@pluck_button, @pluck, |
| 252 |
-command=>sub{&pluck("diff_xmin"); |
| 253 |
my $e = $widget{diff_xmin}->get(); |
| 254 |
($e = sprintf("%.3f", $e-$groups{$diff_params{standard}}->{bkg_e0})) |
| 255 |
if ($diff_params{space} =~ /[en]/); |
| 256 |
$widget{diff_xmin}->delete(0, 'end'); |
| 257 |
$widget{diff_xmin}->insert(0, $e); |
| 258 |
if ($diff_params{xmin} > $diff_params{xmax}) { |
| 259 |
($diff_params{xmin}, $diff_params{xmax}) = |
| 260 |
($diff_params{xmax}, $diff_params{xmin}); |
| 261 |
}; |
| 262 |
$groups{$diff_params{standard}} -> |
| 263 |
plot_difference($groups{$current}, \%diff_params, |
| 264 |
$dmode, \%plot_features); |
| 265 |
}) |
| 266 |
-> pack(-side=>'left', -pady=>3); |
| 267 |
$fr -> Label(-text=> "to", |
| 268 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 269 |
-> pack(-side=>'left', -pady=>6); |
| 270 |
$widget{diff_xmax} = $fr -> Entry(-width=>7, -textvariable=>\$diff_params{xmax}, |
| 271 |
#-validate=>'all', |
| 272 |
#-validatecommand=>[\&set_peak_variable, 'xmax'], |
| 273 |
) |
| 274 |
-> pack(-side=>'left', -pady=>3); |
| 275 |
$grab{diff_xmax} = $fr -> Button(@pluck_button, @pluck, |
| 276 |
-command=>sub{&pluck("diff_xmax"); |
| 277 |
my $e = $widget{diff_xmax}->get(); |
| 278 |
($e = sprintf("%.3f", $e-$groups{$diff_params{standard}}->{bkg_e0})) |
| 279 |
if ($diff_params{space} =~ /[en]/); |
| 280 |
$widget{diff_xmax}->delete(0, 'end'); |
| 281 |
$widget{diff_xmax}->insert(0, $e); |
| 282 |
if ($diff_params{xmin} > $diff_params{xmax}) { |
| 283 |
($diff_params{xmin}, $diff_params{xmax}) = |
| 284 |
($diff_params{xmax}, $diff_params{xmin}); |
| 285 |
}; |
| 286 |
$groups{$diff_params{standard}} -> |
| 287 |
plot_difference($groups{$current}, \%diff_params, |
| 288 |
$dmode, \%plot_features); |
| 289 |
}) |
| 290 |
-> pack(-side=>'left', -pady=>3); |
| 291 |
my $color = $config{colors}{activehighlightcolor}; |
| 292 |
$widget{diff_integrate} = $fr -> |
| 293 |
Button(-text=>'Integrate', @button_list, |
| 294 |
-command=>sub{ |
| 295 |
Echonow("Integrating ..."); |
| 296 |
$top -> Busy(-recurse=>1); |
| 297 |
($diff_params{xmin},$diff_params{xmax}) = ($diff_params{xmax},$diff_params{xmin}) |
| 298 |
if ($diff_params{xmin} > $diff_params{xmax}); |
| 299 |
$diff_params{integral} = |
| 300 |
sprintf("%.7f", |
| 301 |
integrate( \&diff_interpolate, |
| 302 |
$diff_params{xnot} + $diff_params{xmin}, |
| 303 |
$diff_params{xnot} + $diff_params{xmax}, |
| 304 |
6, 1e-8, \%diff_params )); |
| 305 |
$widget{diff_integral_label} -> configure(-foreground=>$color); |
| 306 |
$top -> Unbusy; |
| 307 |
Echonow("Integrating ... done!"); |
| 308 |
}) |
| 309 |
-> pack(-side=>'left', -pady=>3, -padx=>6); |
| 310 |
$fr = $frame -> Frame() |
| 311 |
-> pack(-side=>'bottom', -pady=>3); |
| 312 |
$widget{diff_integral_label} = $fr -> |
| 313 |
Label(-text=> "Intgerated area: ", |
| 314 |
-foreground=>$config{colors}{disabledforeground}) |
| 315 |
-> pack(-side=>'left', -pady=>3, -padx=>12); |
| 316 |
$diff_params{diff_integral_label} = $widget{diff_integral_label}; |
| 317 |
$widget{diff_integral} = $fr -> Label(-width=>12, |
| 318 |
-textvariable=>\$diff_params{integral}) |
| 319 |
-> pack(-side=>'left', -pady=>3); |
| 320 |
|
| 321 |
|
| 322 |
$groups{$diff_params{standard}} -> |
| 323 |
plot_difference($groups{$current}, \%diff_params, $dmode, \%plot_features); |
| 324 |
$last_plot=$space; |
| 325 |
$plotsel -> raise(lc(substr($sp{$space}, 0, 1))); |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
## make a new group object and copy the diff___diff arrays to this new |
| 330 |
## group name |
| 331 |
sub make_diff_group { |
| 332 |
my ($space, $standard, $other, $rhash) = @_; |
| 333 |
Error("Difference group aborted: You selected the same data group twice!"), |
| 334 |
return if ($standard eq $other); |
| 335 |
##my $invert = ($$rhash{invert}) ? "-1*" : ""; |
| 336 |
Echo ("Saving $standard - $other in $space space"); |
| 337 |
## get a group name |
| 338 |
my $group = join(" ", "Diff", $groups{$standard}->{label}, $groups{$other}->{label}); |
| 339 |
my $label; |
| 340 |
($group, $label) = group_name($group); |
| 341 |
$groups{$group} = Ifeffit::Group -> new(group=>$group, label=>$label); |
| 342 |
my $command = q{}; |
| 343 |
## make objects and copy the arrays from diff___diff to the new group |
| 344 |
SWITCH: { |
| 345 |
($space eq 'e') and do { |
| 346 |
$groups{$group} -> make(is_xmu=>1, is_chi=>0, is_rsp=>0, is_qsp=>0, |
| 347 |
is_diff=>1, bkg_flatten=>0, |
| 348 |
bkg_e0 =>$groups{$standard}->{bkg_e0}, |
| 349 |
bkg_pre1=>$groups{$standard}->{bkg_pre1}, |
| 350 |
bkg_pre2=>$groups{$standard}->{bkg_pre2}, |
| 351 |
bkg_nor1=>$groups{$standard}->{bkg_nor1}, |
| 352 |
bkg_nor2=>$groups{$standard}->{bkg_nor2}, |
| 353 |
bkg_spl1=>$groups{$standard}->{bkg_spl1}, |
| 354 |
bkg_spl2=>$groups{$standard}->{bkg_spl2}, |
| 355 |
fft_kmax=>$groups{$standard}->{bkg_spl2}, |
| 356 |
); |
| 357 |
$groups{$group} -> make(bkg_spl1e=>$groups{$group}->k2e($groups{$group}->{bkg_spl1})); |
| 358 |
$groups{$group} -> make(bkg_spl2e=>$groups{$group}->k2e($groups{$group}->{bkg_spl2})); |
| 359 |
$command = "set($group.energy = $standard.energy,\n"; |
| 360 |
$command .= " $group.xmu = diff___diff.xmu)\n"; |
| 361 |
#$command .= "erase \@group diff___diff\n"; |
| 362 |
$groups{$group} -> dispose($command, $dmode); |
| 363 |
last SWITCH; |
| 364 |
}; |
| 365 |
($space eq 'n') and do { |
| 366 |
$groups{$group} -> make(is_xmu=>1, is_chi=>0, is_rsp=>0, is_qsp=>0, is_nor=>1, |
| 367 |
is_diff=>1, bkg_flatten=>0, |
| 368 |
bkg_e0 =>$groups{$standard}->{bkg_e0}, |
| 369 |
bkg_pre1=>$groups{$standard}->{bkg_pre1}, |
| 370 |
bkg_pre2=>$groups{$standard}->{bkg_pre2}, |
| 371 |
bkg_nor1=>$groups{$standard}->{bkg_nor1}, |
| 372 |
bkg_nor2=>$groups{$standard}->{bkg_nor2}, |
| 373 |
bkg_spl1=>$groups{$standard}->{bkg_spl1}, |
| 374 |
bkg_spl2=>$groups{$standard}->{bkg_spl2}, |
| 375 |
fft_kmax=>$groups{$standard}->{bkg_spl2}, |
| 376 |
); |
| 377 |
$groups{$group} -> make(bkg_spl1e=>$groups{$group}->k2e($groups{$group}->{bkg_spl1})); |
| 378 |
$groups{$group} -> make(bkg_spl2e=>$groups{$group}->k2e($groups{$group}->{bkg_spl2})); |
| 379 |
$command = "set($group.energy = $standard.energy,\n"; |
| 380 |
my $ysuff = $groups{$standard}->{bkg_flatten} ? 'flat' : 'norm'; |
| 381 |
##my $ysuff = "norm"; |
| 382 |
$command .= " $group.xmu = diff___diff.$ysuff)\n"; |
| 383 |
#$command .= "erase \@group diff___diff\n"; |
| 384 |
$groups{$group} -> dispose($command, $dmode); |
| 385 |
last SWITCH; |
| 386 |
}; |
| 387 |
($space eq 'k') and do { |
| 388 |
$groups{$group} -> dispose("___x = ceil($standard.k)\n", 1); |
| 389 |
my $maxk = Ifeffit::get_scalar("___x"); |
| 390 |
$groups{$group} -> make(is_xmu=>0, is_chi=>1, is_rsp=>0, is_qsp=>0, |
| 391 |
is_diff=>1, |
| 392 |
update_bkg=>0, fft_kmax=>sprintf("%.2f", $maxk)); |
| 393 |
$command = "set($group.k = $standard.k,\n"; |
| 394 |
$command .= " $group.chi = diff___diff.chi)\n"; |
| 395 |
#$command .= "erase \@group diff___diff\n"; |
| 396 |
$groups{$group} -> dispose($command, $dmode); |
| 397 |
last SWITCH; |
| 398 |
}; |
| 399 |
($space eq 'r') and do { |
| 400 |
$groups{$group} -> make(is_xmu=>0, is_chi=>0, is_rsp=>1, is_qsp=>0, |
| 401 |
is_diff=>1); |
| 402 |
$command = "set($group.r = $standard.r,\n"; |
| 403 |
$command .= " $group.chir_mag = diff___diff.chir_mag,\n"; |
| 404 |
$command .= " $group.chir_pha = diff___diff.chir_pha,\n"; |
| 405 |
$command .= " $group.chir_re = diff___diff.chir_re,\n"; |
| 406 |
$command .= " $group.chir_im = diff___diff.chir_im)\n"; |
| 407 |
#$command .= "erase \@group diff___diff\n"; |
| 408 |
$groups{$group} -> dispose($command, $dmode); |
| 409 |
last SWITCH; |
| 410 |
}; |
| 411 |
($space eq 'q') and do { |
| 412 |
$groups{$group} -> make(is_xmu=>0, is_chi=>0, is_rsp=>0, is_qsp=>1, |
| 413 |
is_diff=>1); |
| 414 |
$command = "set($group.q = $standard.q\n"; |
| 415 |
$command .= " $group.chiq_mag = diff___diff.chiq_mag,\n"; |
| 416 |
$command .= " $group.chiq_pha = diff___diff.chiq_pha,\n"; |
| 417 |
$command .= " $group.chiq_re = diff___diff.chiq_re,\n"; |
| 418 |
$command .= " $group.chiq_im = diff___diff.chiq_im)\n"; |
| 419 |
#$command .= "erase \@group diff___diff\n"; |
| 420 |
$groups{$group} -> dispose($command, $dmode); |
| 421 |
last SWITCH; |
| 422 |
}; |
| 423 |
}; |
| 424 |
$groups{$group} -> make(bkg_z => $groups{$standard}->{bkg_z}, |
| 425 |
fft_edge => $groups{$standard}->{fft_edge}); |
| 426 |
## titles and the "file" |
| 427 |
push @{$groups{$group}->{titles}}, "Difference between $groups{$standard}->{label} and $groups{$other}->{label} in $space space"; |
| 428 |
if ($$rhash{integral}) { |
| 429 |
push @{$groups{$group}->{titles}}, |
| 430 |
sprintf("Integrated area from %s to %s : %s", |
| 431 |
$$rhash{xmin}, $$rhash{xmax}, $$rhash{integral}); |
| 432 |
}; |
| 433 |
$groups{$group} -> put_titles(); |
| 434 |
$groups{$group} -> make(file => "Difference between $groups{$standard}->{label} and $groups{$other}->{label} in $space space"); |
| 435 |
## and display it |
| 436 |
++$line_count; |
| 437 |
$groups{$group} -> make(line=>$line_count); |
| 438 |
fill_skinny($list, $group, 1); |
| 439 |
Echo(@done); |
| 440 |
my $memory_ok = $groups{$group} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0); |
| 441 |
Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1); |
| 442 |
}; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
sub diff_interpolate { |
| 447 |
my $e = shift; |
| 448 |
my $rhash = shift; |
| 449 |
my $stan = $$rhash{standard}; |
| 450 |
my $xsuff = $$rhash{xsuff}; |
| 451 |
my $ysuff = $$rhash{ysuff}; |
| 452 |
$groups{$current} -> dispose("set ___x = splint($stan.$xsuff, diff___diff.$ysuff, $e)", 1); |
| 453 |
return Ifeffit::get_scalar('___x'); |
| 454 |
}; |
| 455 |
|
| 456 |
|
| 457 |
# adapted from Mastering Algorithms with Perl by Orwant, Hietaniemi, |
| 458 |
# and Macdonald Chapter 16, p 632 to pass the diff_params hash to the |
| 459 |
# function |
| 460 |
# |
| 461 |
# integrate() uses the Romberg algorithm to estimate the definite integral |
| 462 |
# of the function $func (provided as a code reference) from $lo to $hi. |
| 463 |
# |
| 464 |
# The subroutine will compute roughly ($steps + 1) * ($steps + 2) / 2 |
| 465 |
# estimates for the integral, of which the last will be the most accurate. |
| 466 |
# |
| 467 |
# integrate() returns early if intermediate estimates change by less |
| 468 |
# than $epsilon. |
| 469 |
# |
| 470 |
sub integrate { |
| 471 |
my ($func, $lo, $hi, $steps, $epsilon, $rhash) = @_; |
| 472 |
my ($h) = $hi - $lo; |
| 473 |
my ($i, $j, @r, $sum); |
| 474 |
my @est; |
| 475 |
|
| 476 |
# Our initial estimate. |
| 477 |
$est[0][0] = ($h / 2) * ( &{$func}( $lo, $rhash ) + &{$func}( $hi, $rhash ) ); |
| 478 |
|
| 479 |
# Compute each row of the Romberg array. |
| 480 |
for ($i = 1; $i <= $steps; $i++) { |
| 481 |
|
| 482 |
$h /= 2; |
| 483 |
$sum = 0; |
| 484 |
|
| 485 |
# Compute the first column of the current row. |
| 486 |
for ($j = 1; $j < 2 ** $i; $j += 2) { |
| 487 |
$sum += &{$func}( $lo + $j * $h, $rhash ); |
| 488 |
} |
| 489 |
$est[$i][0] = $est[$i-1][0] / 2 + $sum * $h; |
| 490 |
|
| 491 |
# Compute the rest of the columns in this row. |
| 492 |
for ($j = 1; $j <= $i; $j++) { |
| 493 |
$est[$i][$j] = ($est[$i][$j-1] - $est[$i-1][$j-1]) |
| 494 |
/ (4**$j - 1) + $est[$i][$j-1]; |
| 495 |
} |
| 496 |
|
| 497 |
# Are we close enough? |
| 498 |
return $est[$i][$i] if $epsilon and |
| 499 |
abs($est[$i][$i] - $est[$i-1][$i-1]) <= $epsilon; |
| 500 |
} |
| 501 |
return $est[$steps][$steps]; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
sub diff_marked { |
| 506 |
|
| 507 |
my ($space, $rhash) = @_; |
| 508 |
my $stan = $$rhash{standard}; |
| 509 |
my @areas; |
| 510 |
my @titles; |
| 511 |
my $j = 0; |
| 512 |
$top -> Busy(-recurse=>1); |
| 513 |
foreach my $g (&sorted_group_list) { |
| 514 |
next unless $marked{$g}; |
| 515 |
next if ($g eq $stan); |
| 516 |
## verify this group |
| 517 |
my $ok = 1; |
| 518 |
SWITCH: { |
| 519 |
($$rhash{space} =~ /[en]/) and do { |
| 520 |
($ok = 0) unless $groups{$g}->{is_xmu}; |
| 521 |
last SWITCH; |
| 522 |
}; |
| 523 |
($$rhash{space} eq 'k') and do { |
| 524 |
($ok = 0) unless ($groups{$g}->{is_xmu} or $groups{$g}->{is_chi}); |
| 525 |
last SWITCH; |
| 526 |
}; |
| 527 |
($$rhash{space} eq 'r') and do { |
| 528 |
($ok = 0) if $groups{$g}->{is_qsp}; |
| 529 |
last SWITCH; |
| 530 |
}; |
| 531 |
}; |
| 532 |
next unless $ok; |
| 533 |
|
| 534 |
++$j; |
| 535 |
set_properties(0, $g, 0); |
| 536 |
|
| 537 |
## integrate |
| 538 |
if ($$rhash{integrate_marked}) { |
| 539 |
($$rhash{xmin},$$rhash{xmax}) = ($$rhash{xmax},$$rhash{xmin}) |
| 540 |
if ($$rhash{xmin} > $$rhash{xmax}); |
| 541 |
$$rhash{integral} = |
| 542 |
sprintf("%.7f", |
| 543 |
integrate( \&diff_interpolate, |
| 544 |
$$rhash{xnot} + $$rhash{xmin}, |
| 545 |
$$rhash{xnot} + $$rhash{xmax}, |
| 546 |
6, 1e-8, $rhash )); |
| 547 |
my $color = $config{colors}{activehighlightcolor}; |
| 548 |
$widget{diff_integral_label} -> configure(-foreground=>$color); |
| 549 |
push @areas, $$rhash{integral}; |
| 550 |
push @titles, "point number $j: " . $groups{$g}->{label}; |
| 551 |
}; |
| 552 |
|
| 553 |
## make difference group |
| 554 |
&make_diff_group($space, $$rhash{standard}, $current, $rhash); |
| 555 |
}; |
| 556 |
unless ($j) { |
| 557 |
$top -> Unbusy; |
| 558 |
Echo("No valid marked groups!"); |
| 559 |
return; |
| 560 |
}; |
| 561 |
|
| 562 |
if ($$rhash{integrate_marked}) { |
| 563 |
##print join(" ", @areas), $/; |
| 564 |
my @x = (); |
| 565 |
my $i = 0; |
| 566 |
foreach (@areas) { push @x, ++$i }; |
| 567 |
my ($group, $label) = group_name("d___iff"); |
| 568 |
Ifeffit::put_array("$group.energy", \@x); |
| 569 |
Ifeffit::put_array("$group.det", \@areas); |
| 570 |
$groups{$group} = Ifeffit::Group -> new(group=>$group, |
| 571 |
label=>"Integrated areas"); |
| 572 |
$groups{$group} -> make(is_xmu => 0, is_chi => 0, is_rsp => 0, is_qsp => 0, |
| 573 |
not_data => 1, bkg_e0 => 1, bkg_eshift => 0); |
| 574 |
push @{$groups{$group}->{titles}}, "Integrated areas from $$rhash{xmin} to $$rhash{xmax}"; |
| 575 |
foreach my $t (@titles) { push @{$groups{$group}->{titles}}, $t }; |
| 576 |
$groups{$group} -> put_titles(); |
| 577 |
$groups{$group} -> make(file => "Integrated areas"); |
| 578 |
my @save = ($plot_features{emin}, $plot_features{emax}); |
| 579 |
($plot_features{emin}, $plot_features{emax}) = (0, $i); |
| 580 |
$groups{$group} -> plotE('em',$dmode,\%plot_features, \@indicator); |
| 581 |
($plot_features{emin}, $plot_features{emax}) = @save; |
| 582 |
## and display it |
| 583 |
++$line_count; |
| 584 |
$groups{$group} -> make(line=>$line_count); |
| 585 |
fill_skinny($list, $group, 1); |
| 586 |
}; |
| 587 |
$top -> Unbusy; |
| 588 |
}; |
| 589 |
|
| 590 |
## END OF DIFFERENCE SPECTRA SUBSECTION |
| 591 |
########################################################################################## |