Debian Codegraph @

athena_parts/sa_fluo.pl

raw
1 ## -*- cperl -*-
2 ##
3 ## This file is part of Athena, copyright (c) 2001-2006, 2009 Bruce Ravel
4
5
6 ## possible features:
7 ## * plot buttons for E, k , R
8 ## * checkbutton for constraining phi+theta=90
9 ## * weight percentages in formula
10
11
12 sub sa {
13
14 ## do not change modes unless there is xmu data
15 Echo("No data!"), return unless $current;
16 Echo("No data!"), return if ($current eq "Default Parameters");
17 my @keys = ();
18 foreach my $k (&sorted_group_list) {
19 (($groups{$k}->{is_xmu}) or ($groups{$k}->{is_chi})) and push @keys, $k;
20 };
21 Error("You need at least one xmu or chi group to do self-absorption corrections"), return unless (@keys);
22
23 #$top -> Busy;
24
25 ## you must define a hash which will contain the parameters needed
26 ## to perform the task. the $hash_pointer global variable will point
27 ## to this hash for use in set_properties. you might draw these
28 ## values from configuration parameters, as in the commented out
29 ## example
30 my %safluo_params = (angle_in => $config{sa}{angle_in},
31 angle_out => $config{sa}{angle_out},
32 formula => "",
33 thickness => $config{sa}{thickness},
34 algorithm => $config{sa}{algorithm});
35 my %explain = (fluo => "Correct XANES spectra, mu(E), and EXAFS, chi(k). The sample is presumed to be infinitely thick.",
36 booth => "Correct EXAFS spectra, chi(k), for samples of any thickness.",
37 troger => "Correct EXAFS spectra, chi(k), but only for thick samples.",
38 atoms => "Correct EXAFS spectra, chi(k), using corrections to S0^2 and sigma^2, but only for thick samples.",
39 );
40 my $grey = $config{colors}{disabledforeground};
41 my $black = $config{colors}{foreground};
42 my $blue = $config{colors}{activehighlightcolor};
43 ## The Athena standard for analysis chores that need a specialized
44 ## plotting range is to save the plotting range from the main view
45 ## and restore it when the main view is restored
46 my $ps = $project_saved;
47 my @save = ($plot_features{emin}, $plot_features{emax});
48 $plot_features{emin} = $config{sa}{emin};
49 $plot_features{emax} = $config{sa}{emax};
50 project_state($ps); # don't toggle if currently saved
51
52 ## these two global variables must be set before this view is
53 ## displayed. these are used at the level of set_properties to
54 ## perform chores appropriate to this dialog when changing the
55 ## current group
56 $fat_showing = 'sa';
57 $hash_pointer = \%safluo_params;
58
59 ## disable many menus. this makes the chore of managing the views
60 ## much easier. the idea is that the main view is "home base". if
61 ## you want to do a different analysis chore, you must first return
62 ## to the main view
63 map {$_ -> configure(-state=>'disabled')}
64 ($data_menu, $merge_menu, $anal_menu, $settings_menu);
65
66 ## this removes the currently displayed view without destroying its
67 ## contents
68 $fat -> packForget;
69
70 ## define the parent Frame for this analysis chore and pack it in
71 ## the correct location
72 my $safluo = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3)
73 -> pack(-fill=>'both', -expand=>1);
74
75 ## global variable identifying which Frame is showing
76 $which_showing = $safluo;
77
78 ## the standard label along the top identifying this analysis chore
79 $safluo -> Label(-text => "Self Absorption Corrections",
80 -font => $config{fonts}{large},
81 -foreground => $config{colors}{activehighlightcolor})
82 -> pack(-side=>'top', -fill=>'x', -anchor=>'w');
83
84 ## a good solution to organizing widgets is to stack frames, so
85 ## let's make a frame for the standard and the other. note that the
86 ## "labels" are actually flat buttons which display hints in the
87 ## echo area
88 my $top = $safluo -> Frame(-borderwidth=>2, -relief=>'flat')
89 -> pack(-side=>'top', -fill=>'x');
90 my $frame = $top -> LabFrame(-label => 'Algorithm',
91 -foreground => $config{colors}{activehighlightcolor},
92 -labelside => 'acrosstop')
93 -> pack(-side=>'left', -fill=>'both', -expand=>1);
94 $widget{safluo_fluo} = $frame -> Radiobutton(-text => 'XANES (Fluo)',
95 -selectcolor => $config{colors}{single},
96 -foreground => $config{colors}{activehighlightcolor},
97 -activeforeground => $config{colors}{activehighlightcolor},
98 -command => sub{$widget{safluo_thickness} -> configure(-state=>'disabled');
99 map { $widget{$_}->configure(-foreground=>$grey) } (qw(safluo_thickness safluo_thickness_lab safluo_thickness_lab2));
100 $widget{safluo_make} -> configure(-state=>'disabled');
101 $groups{$current}->plotE('emn', $dmode, \%plot_features, \@indicator)
102 if $groups{$current}->{is_xmu};
103 $last_plot = 'e';
104 $plotsel->raise('e') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
105 Echo($explain{fluo});
106 },
107 -value => 'fluo',
108 -variable => \$safluo_params{algorithm})
109 -> pack(-side=>'top', -anchor=>'w', -padx=>2, -pady=>2);
110 $frame -> Radiobutton(-text => 'EXAFS (Booth)',
111 -selectcolor => $config{colors}{single},
112 -foreground => $config{colors}{activehighlightcolor},
113 -activeforeground => $config{colors}{activehighlightcolor},
114 -command => sub{$widget{safluo_thickness} -> configure(-state=>'normal');
115 map { $widget{$_}->configure(-foreground=>$blue) } (qw(safluo_thickness_lab safluo_thickness_lab2));
116 $widget{safluo_thickness}->configure(-foreground=>$black);
117 $widget{safluo_make} -> configure(-state=>'disabled');
118 my $str = 'k'.$plot_features{k_w};
119 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
120 $last_plot='k';
121 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
122 Echo($explain{booth});
123 },
124 -value => 'booth',
125 -variable => \$safluo_params{algorithm})
126 -> pack(-side=>'top', -anchor=>'w', -padx=>2, -pady=>2);
127 $frame -> Radiobutton(-text => 'EXAFS (Troger)',
128 -selectcolor => $config{colors}{single},
129 -foreground => $config{colors}{activehighlightcolor},
130 -activeforeground => $config{colors}{activehighlightcolor},
131 -command => sub{$widget{safluo_thickness} -> configure(-state=>'disabled');
132 map { $widget{$_}->configure(-foreground=>$grey) } (qw(safluo_thickness safluo_thickness_lab safluo_thickness_lab2));
133 $widget{safluo_make} -> configure(-state=>'disabled');
134 my $str = 'k'.$plot_features{k_w};
135 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
136 $last_plot='k';
137 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
138 Echo($explain{troger});
139 },
140 -value => 'troger',
141 -variable => \$safluo_params{algorithm})
142 -> pack(-side=>'top', -anchor=>'w', -padx=>2, -pady=>2);
143 $frame -> Radiobutton(-text => 'EXAFS (Atoms)',
144 -selectcolor => $config{colors}{single},
145 -foreground => $config{colors}{activehighlightcolor},
146 -activeforeground => $config{colors}{activehighlightcolor},
147 -command => sub{$widget{safluo_thickness} -> configure(-state=>'disabled');
148 map { $widget{$_}->configure(-foreground=>$grey) } (qw(safluo_thickness safluo_thickness_lab safluo_thickness_lab2));
149 $widget{safluo_make} -> configure(-state=>'disabled');
150 my $str = 'k'.$plot_features{k_w};
151 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
152 $last_plot='k';
153 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
154 Echo($explain{atoms});
155 },
156 -value => 'atoms',
157 -variable => \$safluo_params{algorithm})
158 -> pack(-side=>'top', -anchor=>'w', -padx=>2, -pady=>2);
159
160 $frame = $top -> Frame(-borderwidth=>2, -relief=>'flat')
161 -> pack(-side=>'right', -fill=>'x');
162 $frame -> Label(-text=>"Group:",
163 -foreground=>$config{colors}{activehighlightcolor},)
164 -> grid(-row=>0, -column=>0, -sticky=>'e', -pady=>3);
165 $widget{safluo_group} = $frame -> Label(-anchor=>'w')
166 -> grid(-row=>0, -column=>1, -columnspan=>4, -sticky=>'ew', -pady=>3);
167
168 $frame -> Label(-text=>"Element:",
169 -foreground=>$config{colors}{activehighlightcolor},)
170 -> grid(-row=>1, -column=>0, -sticky=>'e', -pady=>3);
171 $widget{safluo_elem} = $frame -> Label(-width=>5, -anchor=>'w')
172 -> grid(-row=>1, -column=>1, -sticky=>'w', -pady=>3);
173 $frame -> Label(-text=>" ",
174 -foreground=>$config{colors}{activehighlightcolor},)
175 -> grid(-row=>1, -column=>2, -sticky=>'e', -pady=>3);
176 $frame -> Label(-text=>"Edge:",
177 -foreground=>$config{colors}{activehighlightcolor},)
178 -> grid(-row=>1, -column=>3, -sticky=>'e', -pady=>3);
179 $widget{safluo_edge} = $frame -> Label(-width=>3, -anchor=>'w')
180 -> grid(-row=>1, -column=>4, -sticky=>'w', -pady=>3);
181
182 $frame -> Label(-text=>"Formula:",
183 -foreground=>$config{colors}{activehighlightcolor},)
184 -> grid(-row=>2, -column=>0, -sticky=>'e', -pady=>3);
185 $widget{safluo_formula}= $frame -> Entry(-textvariable=>\$safluo_params{formula})
186 -> grid(-row=>2, -column=>1, -columnspan=>4, -sticky=>'ew', -pady=>3);
187 $widget{safluo_formula} -> bind("<KeyPress-Return>"=>sub{dispatch_sa(\%safluo_params)});
188
189 $frame -> Label(-text=>"Angle in:",
190 -foreground=>$config{colors}{activehighlightcolor},)
191 -> grid(-row=>3, -column=>0, -sticky=>'e', -pady=>3);
192 $widget{safluo_angle_in} = $frame -> NumEntry(-width => 4,
193 -orient => 'horizontal',
194 -foreground => $config{colors}{foreground},
195 -textvariable => \$safluo_params{angle_in},
196 -minvalue => 0,
197 -maxvalue => 90,
198 )
199 -> grid(-row=>3, -column=>1, -sticky=>'w', -pady=>3);
200 $widget{safluo_angle_in} -> bind("<KeyPress-Return>"=>sub{dispatch_sa(\%safluo_params)});
201 $frame -> Label(-text=>"Angle out:",
202 -foreground=>$config{colors}{activehighlightcolor},)
203 -> grid(-row=>3, -column=>3, -sticky=>'e', -pady=>3);
204 $widget{safluo_angle_out} = $frame -> NumEntry(-width => 4,
205 -orient => 'horizontal',
206 -foreground => $config{colors}{foreground},
207 -textvariable => \$safluo_params{angle_out},
208 -minvalue => 0,
209 -maxvalue => 90,
210 )
211 -> grid(-row=>3, -column=>4, -sticky=>'w', -pady=>3);
212 $widget{safluo_angle_out} -> bind("<KeyPress-Return>"=>sub{dispatch_sa(\%safluo_params)});
213 $widget{safluo_thickness_lab} = $frame -> Label(-text=>"Thickness:",
214 -foreground=>$config{colors}{activehighlightcolor},)
215 -> grid(-row=>4, -column=>0, -sticky=>'e', -pady=>3);
216 $widget{safluo_thickness} = $frame -> Entry(-width => 8,
217 -foreground => $config{colors}{foreground},
218 -textvariable => \$safluo_params{thickness},
219 -validate => 'key',
220 -validatecommand => [\&set_variable, 'safluo_thickness']
221 )
222 -> grid(-row=>4, -column=>1, -sticky=>'ew', -pady=>3);
223 $widget{safluo_thickness} -> bind("<KeyPress-Return>"=>sub{dispatch_sa(\%safluo_params)});
224 $widget{safluo_thickness_lab2} = $frame -> Label(-text => "mic.",
225 -foreground => $config{colors}{activehighlightcolor},)
226 -> grid(-row=>4, -column=>2, -sticky=>'w', -pady=>3);
227
228
229
230 ## this is a spacer frame which pushes all the widgets to the top
231 #$safluo -> Frame(-background=>$config{colors}{darkbackground})
232 # -> pack(-side=>'bottom', -expand=>1, -fill=>'both');
233
234 ## at the bottom of the frame, there are full width buttons for
235 ## returning to the main view and for going to the appropriate
236 ## document section
237 $safluo -> Button(-text=>'Return to the main window', @button_list,
238 -background=>$config{colors}{background2},
239 -activebackground=>$config{colors}{activebackground2},
240 -command=>sub{## restore the main view
241 ##$groups{$current} -> dispose("erase s___a.resid s___a.postline s___a___nor\n", $dmode);
242 $groups{$current} -> dispose("erase s___a___nor\n", $dmode);
243 $groups{$current} -> dispose("erase \@group s___a\n", $dmode);
244 &reset_window($safluo, "self absorption", \@save);
245 })
246 -> pack(-side=>'bottom', -fill=>'x');
247 ## help button
248 $safluo -> Button(-text=>'Document section: self absorption corrections', @button_list,
249 -command=>sub{pod_display("process::sa.pod") })
250 -> pack(-side=>'bottom', -fill=>'x', -pady=>4);
251
252
253 ## now begin setting up the widgets you need for your new analysis
254 ## feature
255
256 ## now a new frame buttons
257 $frame = $safluo -> Frame(-borderwidth=>2, -relief=>'flat')
258 -> pack(-side=>'top', -fill=>'x', -pady=>8);
259 $widget{safluo_plot} = $frame -> Button(-text=>"Plot data and correction", @button_list,
260 -command=>sub{dispatch_sa(\%safluo_params)})
261 -> pack(-fill=>'x');
262 $frame -> Button(-text=>'Plot information depth', @button_list,
263 -command=>sub{dispatch_sa(\%safluo_params, 'info')})
264 -> pack(-fill=>'x');
265 $widget{safluo_make} = $frame -> Button(-text=>"Make corrected data group", @button_list,
266 -command=>sub{sa_group($current,\%safluo_params)})
267 -> pack(-fill=>'x');
268
269
270 $frame = $safluo -> LabFrame(-label=>'Feedback',
271 -foreground=>$config{colors}{activehighlightcolor},
272 -labelside=>'acrosstop')
273 -> pack(-pady=>3, -padx=>3, -ipady=>3, -ipadx=>3, -fill=>'both', -expand=>1);
274 $widget{safluo_feedback} = $frame -> Scrolled('ROText',
275 -height => 1,
276 -width => 1,
277 -scrollbars => 'osoe',
278 -wrap => 'none',
279 -font => $config{fonts}{entry})
280 -> pack(-fill=>'both', -padx=>2, -pady=>2, -expand=>1);
281 $widget{safluo_feedback} -> Subwidget("xscrollbar") -> configure(-background=>$config{colors}{background});
282 $widget{safluo_feedback} -> Subwidget("yscrollbar") -> configure(-background=>$config{colors}{background});
283 $widget{safluo_feedback} -> tagConfigure('margin', -lmargin1=>4, -lmargin2=>4);
284 $widget{safluo_feedback} -> tagConfigure('error', -lmargin1=>4, -lmargin2=>4, -foreground=>'red3');
285 BindMouseWheel($widget{safluo_feedback});
286 ## disable mouse-3
287 my @swap_bindtags = $widget{safluo_feedback}->Subwidget('rotext')->bindtags;
288 $widget{safluo_feedback} -> Subwidget('rotext') -> bindtags([@swap_bindtags[1,0,2,3]]);
289 $widget{safluo_feedback} -> Subwidget('rotext') -> bind('<Button-3>' => sub{$_[0]->break});
290 $widget{safluo_feedback} -> tagConfigure("text", -font=>$config{fonts}{fixedsm});
291
292
293 ## disable thickness for Fluo, Troger, Atoms correction
294 unless ($safluo_params{algorithm} eq 'booth') {
295 $widget{safluo_thickness} -> configure(-state=>'disabled');
296 ##map { print $_, " ", ref $widget{$_}, $/ }
297 foreach my $w ('safluo_thickness', 'safluo_thickness_lab', 'safluo_thickness_lab2') {
298 #print $w, " ", ref $widget{$w}, " ", $config{colors}{disabledforeground}, $/;
299 $widget{$w}->configure(-foreground=>$grey);
300 };
301 };
302
303 ## insert group dependent data
304 $widget{safluo_group} -> configure(-text=>$groups{$current}->{label});
305 $widget{safluo_elem} -> configure(-text=>$groups{$current}->{bkg_z});
306 $widget{safluo_edge} -> configure(-text=>$groups{$current}->{fft_edge});
307
308 ## configure the buttons, insert the formula if it exists
309 my $is_xmu = $groups{$current}->{is_xmu};
310 $widget{safluo_plot} -> configure(-state=>($is_xmu) ? 'normal' : 'disabled');
311 $widget{safluo_make} -> configure(-state=>'disabled');
312 $widget{safluo_formula} -> focus;
313
314 ## disable the Fluo button for chi data
315 unless ($is_xmu) {
316 ($safluo_params{algorithm} = "booth") if ($safluo_params{algorithm} eq 'fluo');
317 $widget{safluo_fluo} -> configure(-state=>'disabled');
318 };
319
320 foreach my $k (qw(formula thickness angle_in angle_out)) {
321 $safluo_params{$k} = $groups{$current}->{"sa_$k"} if (exists $groups{$current}->{"sa_$k"} and
322 $groups{$current}->{"sa_$k"} !~ /^\s*$/);
323 };
324
325 ## make a nice plot
326 if ($safluo_params{algorithm} eq 'fluo') {
327 $groups{$current}->plotE('emn', $dmode, \%plot_features, \@indicator) if $is_xmu;
328 $last_plot = 'e';
329 $plotsel->raise('e') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
330 } else {
331 my $str = 'k'.$plot_features{k_w};
332 $groups{$current}->plotk($str, $dmode, \%plot_features, \@indicator) if $is_xmu;
333 $last_plot = 'k';
334 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
335 };
336
337 $top -> update;
338
339 ## and finally....
340 #$top -> Unbusy;
341
342 };
343
344
345 sub dispatch_sa {
346 my $rparams = $_[0];
347 my $algorithm = $_[1] || 'correction';
348 unless ($$rparams{formula}) {
349 $widget{safluo_feedback} -> insert('end', "\nInput error:\n\tYou did not specify a chemical formula", ['error']);
350 Error("You did not specify a chemical formula");
351 return;
352 };
353 if ($$rparams{thickness} <= 0) {
354 $widget{safluo_feedback} -> insert('end', "\nInput error:\n\tThe thickness must be a positive number", ['error']);
355 Error("The thickness must be a positive number");
356 return;
357 };
358 SWITCH: {
359 sa_info_depth($rparams), last SWITCH if ($algorithm eq 'info');
360 do_safluo($rparams), last SWITCH if ($$rparams{algorithm} eq 'fluo');
361 do_sabooth($rparams), last SWITCH if ($$rparams{algorithm} eq 'booth');
362 do_satroger($rparams), last SWITCH if ($$rparams{algorithm} eq 'troger');
363 do_saatoms($rparams), last SWITCH if ($$rparams{algorithm} eq 'atoms');
364 };
365 };
366
367
368 sub do_safluo {
369 my $rparams = $_[0];
370 Echo("Doing Fluo correction");
371 $top -> Busy;
372
373 my $e0shift = $groups{$current}->{bkg_eshift};
374 my ($ok, $efluo, $rcount) = sa_feedback($rparams);
375 unless ($ok) {
376 Error("Error parsing formula.");
377 $top -> Unbusy;
378 return;
379 };
380
381 my $eplus = $groups{$current}->{bkg_e0} + $groups{$current}->{bkg_nor1} + $e0shift;
382 my $enominal = Xray::Absorption -> get_energy($groups{$current}->{bkg_z}, $groups{$current}->{fft_edge});
383 ($eplus = $enominal + 10) if ($eplus < $enominal);
384 my ($barns_fluo, $barns_plus) = (0,0);
385 my $mue_plus = 0;
386
387 if ($ok) {
388 foreach my $k (keys(%$rcount)) {
389
390 ## compute contribution to mu_total at the fluo energy
391 $barns_fluo += $$rcount{$k} * Xray::Absorption -> cross_section($k, $efluo);
392
393 if (lc($k) eq lc(get_symbol($groups{$current}->{bkg_z}))) {
394 ## compute contribution to mu_abs at the above edge energy
395 $mue_plus = $$rcount{$k} * Xray::Absorption -> cross_section($k, $eplus);
396 } else {
397 ## compute contribution to mu_back at the above edge energy
398 $barns_plus += $$rcount{$k} * Xray::Absorption -> cross_section($k, $eplus);
399 };
400 };
401 };
402
403 unless ($mue_plus > 0) {
404 $widget{safluo_feedback} -> insert('end', "\nUnable to compute cross section of absorber above the edge", ['error']);
405 $top -> Unbusy;
406 return;
407 };
408
409 my $mut_fluo = $barns_fluo;
410 my $mub_plus = $barns_plus;
411 my $beta = sprintf("%.6f", $mut_fluo/$mue_plus);
412 my $gammaprime = sprintf("%.6f", $mub_plus/$mue_plus);
413 my $angle_ratio = sprintf("%.6f", sin(PI*$$rparams{angle_in}/180) / sin(PI*$$rparams{angle_out}/180));
414
415 my @energy = Ifeffit::get_array($current.".energy");
416 my @mub = ();
417 foreach my $e (@energy) {
418 my $barns = 0;
419 foreach my $k (keys(%$rcount)) {
420 next if (lc($k) eq lc(get_symbol($groups{$current}->{bkg_z})));
421 $barns += Xray::Absorption -> cross_section($k, $e+$e0shift) * $$rcount{$k};
422 };
423 push @mub, $barns;
424 };
425 $groups{$current} -> dispose("## inserted s___a.mub into ifeffit's memory...", $dmode);
426 Ifeffit::put_array("s___a.mub", \@mub);
427 my $suff = ($groups{$current}->{bkg_flatten}) ? 'flat' : 'norm';
428 my $cmd = "## compute self absorption corrected data array using method of Haskel's Fluo\n";
429 $cmd .= "set(s___a.energy = $current.energy+$e0shift,\n";
430 $cmd .= " s___a.num = $current.$suff * ($beta*$angle_ratio + s___a.mub/$mue_plus),\n";
431 $cmd .= " s___a.den = ($beta*$angle_ratio + $gammaprime + 1) - $current.$suff,\n";
432 $cmd .= " s___a.sacorr = s___a.num / s___a.den,\n";
433 $cmd .= " ___x = max(ceil(s___a.sacorr), abs(floor(s___a.sacorr))) )";
434 $groups{$current} -> dispose($cmd, $dmode);
435 my $maxval = Ifeffit::get_scalar("___x");
436
437 $groups{$current} -> plotE('emn', $dmode, \%plot_features, \@indicator);
438 my $color = $plot_features{c1};
439 $groups{$current} -> dispose("plot(s___a.energy, s___a.sacorr, style=lines, color=\"$color\", key=\"SA corrected\")", $dmode);
440 $last_plot='e';
441 $plotsel->raise('e') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
442 if ($maxval > 30) {
443 my $message = "
444 Yikes!
445
446 This correction seems to be numerically unstable.
447 Among the common reasons for this are:
448
449 1. Providing the wrong chemical formula
450
451 2. Having data from a sample that is not in the
452 infinitely thick limit (the Fluo algorithm
453 is not valid in the thin sample limit)
454
455 3. Not including the matrix containing the sample
456 in the formula for the stoichiometry (for
457 instance, the formula for an aqueous solution
458 must include the amount of H2O relative to the
459 sample)
460
461 ";
462 $widget{safluo_feedback} -> insert('1.0', $message, ['error']);
463 };
464
465 $groups{$current} -> MAKE(sa_formula => $$rparams{formula},
466 sa_thickness => $$rparams{thickness},
467 sa_angle_in => $$rparams{angle_in},
468 sa_angle_out => $$rparams{angle_out},
469 );
470 project_state(0);
471 $widget{safluo_make} -> configure(-state=>'normal');
472
473 Echo("Doing Fluo correction ... done!");
474 $top -> Unbusy;
475 };
476
477
478 ## all calculations done in microns!!
479 sub do_sabooth {
480 my $rparams = $_[0];
481 Echo("Doing Booth/Bridges correction");
482 $top -> Busy;
483
484 my $e0shift = $groups{$current}->{bkg_eshift};
485 my ($ok, $efluo, $rcount) = sa_feedback($rparams);
486 unless ($ok) {
487 Error("Error parsing formula.");
488 $top -> Unbusy;
489 return;
490 };
491
492
493 my ($barns_fluo, $barns_plus) = (0,0);
494 my $mue_plus = 0;
495
496 my ($barns, $amu) = (0,0);
497 foreach my $el (keys(%$rcount)) {
498 $barns += Xray::Absorption -> cross_section($el, $efluo) * $$rcount{$el};
499 $amu += Xray::Absorption -> get_atomic_weight($el) * $$rcount{$el};
500 };
501 my $muf = sprintf("%.6f", $barns / $amu / 1.6607143);
502
503 unless ($muf > 0) {
504 $widget{safluo_feedback} -> delete('1.0', 'end');
505 $widget{safluo_feedback} -> insert('end', "\nUnable to compute cross section of absorber at the fluorescence energy", ['error']);
506 Error("Unable to compute cross section of absorber at the fluorescence energy");
507 $top -> Unbusy;
508 return;
509 };
510
511 $groups{$current} -> dispose("erase \@group s___a", $dmode);
512
513 $groups{$current}->dispatch_bkg if $groups{$current}->{update_bkg};
514 my @k = Ifeffit::get_array($current.".k");
515 my @mut = ();
516 my @mua = ();
517 my $abs = ucfirst( lc(get_symbol($groups{$current}->{bkg_z})) );
518 my $amuabs = Xray::Absorption -> get_atomic_weight($abs);
519 foreach my $kk (@k) {
520 my ($barns, $amu) = (0,0);
521 my $e = $groups{$current}->k2e($kk) + $groups{$current}->{bkg_e0} + $e0shift;
522 foreach my $el (keys(%$rcount)) {
523 ##next if (lc($el) eq lc(get_symbol($groups{$current}->{bkg_z})));
524 $barns += Xray::Absorption -> cross_section($el, $e) * $$rcount{$el};
525 $amu += Xray::Absorption -> get_atomic_weight($el) * $$rcount{$el};
526 };
527 ## 1 amu = 1.6607143 x 10^-24 gm
528 push @mua, $$rcount{$abs} * Xray::Absorption -> cross_section($abs, $e) / $amu / 1.6607143;
529 push @mut, $barns / $amu / 1.6607143;
530 };
531 $groups{$current} -> dispose("## inserted s___a.mut and s___a.mua into ifeffit's memory...", $dmode);
532 $groups{$current} -> dispose("## compute self absorption corrected data array using method of Booth and Bridges", $dmode);
533 Ifeffit::put_array("s___a.mut", \@mut);
534 Ifeffit::put_array("s___a.mua", \@mua);
535
536 my $dd = $$rparams{thickness} * 10e-4;
537
538 # my $avg = 0;
539 # map { $avg += $_ } @mua;
540 # $avg /= ($#mua + 1);
541 # $groups{$current} -> dispose("set ___x = ceil($current.chi)", 1);
542 # my $chimax = Ifeffit::get_scalar("___x");
543
544
545 my $angle_ratio = sprintf("%.6f", sin(PI*$$rparams{angle_in}/180) / sin(PI*$$rparams{angle_out}/180));
546 my $precmd = "set(s___a.alpha = s___a.mut + $angle_ratio*$muf,\n";
547 $precmd .= " s___a.exparg = $dd*s___a.alpha/sin(pi*$$rparams{angle_in}/180),\n";
548 $precmd .= " s___a.beta = s___a.mua * exp(-1 * s___a.exparg) * s___a.exparg,\n";
549 $precmd .= " s___a.gamma = 1 - exp(-1 * s___a.exparg),\n";
550 $precmd .= " s___a.term1 = s___a.gamma*(s___a.alpha - s___a.mua*($current.chi+1)) + s___a.beta,\n";
551 $precmd .= " s___a.term2 = 4*s___a.alpha*s___a.beta*s___a.gamma*$current.chi,\n";
552 $precmd .= " s___a.sqrtarg = s___a.term1**2 + s___a.term2)\n";
553
554
555 $groups{$current} -> dispose($precmd, $dmode);
556 $groups{$current} -> dispose("set(___x = floor(s___a.beta), ___xx = floor(s___a.sqrtarg))", 1);
557 my $betamin = Ifeffit::get_scalar("___x");
558 my $isneg = Ifeffit::get_scalar("___xx");
559 my $thickcheck = ($betamin < 10e-7) || ($isneg < 0);
560 my $message;
561
562 my $cmd = "";
563 if ($thickcheck > 0.005) {
564 $widget{safluo_feedback} -> insert('end', "\nYou are in the thick sample limit.\nUsing the thick limit approximation.\n", ['margin']);
565 $cmd .= "## thick limit\n";
566 $cmd .= "set(s___a.s = s___a.mua/s___a.alpha,\n";
567 $cmd .= " s___a.denom = (1 - s___a.s*($current.chi + 1)),\n";
568 $cmd .= " s___a.chi = $current.chi / s___a.denom)\n";
569 $message = "(thick sample limit)";
570 } else {
571 $widget{safluo_feedback} -> insert('end', "\nYou are in the thin sample regime.\nUsing the nearly exact expression.\n", ['margin']);
572 $cmd .= "## thin regime\n";
573 $cmd .= "set(s___a.chi = (-1 * s___a.term1 + sqrt(s___a.sqrtarg)) / (2*s___a.beta))\n";
574 $message = "(thin sample regime)";
575 };
576
577 $groups{$current} -> dispose($cmd, $dmode);
578
579 my $str = 'k'.$plot_features{k_w};
580 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
581 my $color = $plot_features{c1};
582 $groups{$current} -> dispose("plot($current.k, \"s___a.chi*$current.k**$plot_features{k_w}\", style=lines, color=\"$color\", key=\"SA corrected\")", $dmode);
583 $last_plot='k';
584 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
585
586 $groups{$current} -> MAKE(formula=>$$rparams{formula});
587 project_state(0);
588 $widget{safluo_make} -> configure(-state=>'normal');
589
590 Echo("Doing Booth/Bridges correction $message ... done!");
591 $top -> Unbusy;
592 };
593
594
595 sub do_satroger {
596 my $rparams = $_[0];
597 Echo("Doing Troger et al. correction");
598 $top -> Busy;
599
600 my $e0shift = $groups{$current}->{bkg_eshift};
601 my ($ok, $efluo, $rcount) = sa_feedback($rparams);
602 unless ($ok) {
603 Error("Error parsing formula.");
604 $top -> Unbusy;
605 return;
606 };
607
608 my ($barns, $amu) = (0,0);
609 foreach my $el (keys(%$rcount)) {
610 $barns += Xray::Absorption -> cross_section($el, $efluo) * $$rcount{$el};
611 $amu += Xray::Absorption -> get_atomic_weight($el) * $$rcount{$el};
612 };
613 my $muf = sprintf("%.6f", $barns / $amu / 1.6607143);
614 my $angle_ratio = sprintf("%.6f", sin(PI*$$rparams{angle_in}/180) / sin(PI*$$rparams{angle_out}/180));
615
616 my @k = Ifeffit::get_array($current.".k");
617 my @mut = ();
618 my @mua = ();
619 my $abs = ucfirst( lc(get_symbol($groups{$current}->{bkg_z})) );
620 foreach my $kk (@k) {
621 my $barns = 0;
622 my $e = $groups{$current}->k2e($kk) + $groups{$current}->{bkg_e0} + $e0shift;
623 foreach my $el (keys(%$rcount)) {
624 $barns += Xray::Absorption -> cross_section($el, $e) * $$rcount{$el};
625 };
626 ## 1 amu = 1.6607143 x 10^-24 gm
627 push @mut, $barns / $amu / 1.6607143;
628 push @mua, $$rcount{$abs} * Xray::Absorption -> cross_section($abs, $e) / $amu / 1.6607143;
629 };
630 $groups{$current} -> dispose("## compute self absorption corrected data array using method of Troger et al", $dmode);
631 $groups{$current} -> dispose("## inserted s___a.mut and s___a.mua into ifeffit's memory...", $dmode);
632 Ifeffit::put_array("s___a.mut", \@mut);
633 Ifeffit::put_array("s___a.mua", \@mua);
634
635 my $sets = "set(s___a.alpha = s___a.mut + $angle_ratio*$muf,";
636 $sets .= " s___a.s = s___a.mua / s___a.alpha,";
637 $sets .= " s___a.chi = $current.chi / (1 - s___a.s) )";
638 $groups{$current} -> dispose($sets, $dmode);
639
640 my $str = 'k'.$plot_features{k_w};
641 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
642 my $color = $plot_features{c1};
643 $groups{$current} -> dispose("plot($current.k, \"s___a.chi*$current.k**$plot_features{k_w}\", style=lines, color=\"$color\", key=\"SA corrected\")", $dmode);
644 $last_plot='k';
645 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
646
647 $groups{$current} -> MAKE(formula=>$$rparams{formula});
648 project_state(0);
649 $widget{safluo_make} -> configure(-state=>'normal');
650
651 Echo("Doing Troger correction ... done!");
652 $top -> Unbusy;
653 };
654
655
656
657 sub do_saatoms {
658 my $rparams = $_[0];
659 Echo("Doing Atoms correction");
660 $top -> Busy;
661
662 my $e0shift = $groups{$current}->{bkg_eshift};
663 my ($ok, $efluo, $rcount) = sa_feedback($rparams);
664 unless ($ok) {
665 Error("Error parsing formula.");
666 $top -> Unbusy;
667 return;
668 };
669
670 my $mm_sigsqr = Xray::FluorescenceEXAFS->mcmaster($groups{$current}->{bkg_z},
671 $groups{$current}->{fft_edge});
672 my $i0_sigsqr = Xray::FluorescenceEXAFS->i_zero($groups{$current}->{bkg_z},
673 $groups{$current}->{fft_edge},
674 {nitrogen=>1,argon=>0,krypton=>0});
675 my ($self_amp, $self_sigsqr) = Xray::FluorescenceEXAFS->self($groups{$current}->{bkg_z},
676 $groups{$current}->{fft_edge},
677 $rcount);
678 my $net = sprintf("%.6f", $self_sigsqr+$i0_sigsqr+$i0_sigsqr);
679
680 my $answer .= sprintf("\nSelf amplitude : %6.3f\n", $self_amp);
681 $answer .= sprintf("Self : %8.5f A^2\n", $self_sigsqr);
682 $answer .= sprintf("Normalization : %8.5f A^2\n", $mm_sigsqr);
683 $answer .= sprintf("I0 : %8.5f A^2\n", $i0_sigsqr);
684 $answer .= sprintf(" net sigma^2 : %8.5f A^2\n", $net);
685 $widget{safluo_feedback} -> insert('end', $answer, ['margin']);
686
687 $groups{$current} -> dispose("set(s___a.chi = $self_amp * $current.chi * exp($net*$current.k^2))", $dmode);
688 my $str = 'k'.$plot_features{k_w};
689 $groups{$current} -> plotk($str, $dmode, \%plot_features, \@indicator);
690 my $color = $plot_features{c1};
691 $groups{$current} -> dispose("plot($current.k, \"s___a.chi*$current.k**$plot_features{k_w}\", style=lines, color=\"$color\", key=\"SA corrected\")", $dmode);
692 $last_plot='k';
693 $plotsel->raise('k') unless ($plotsel->raised() =~ /(Stack|Ind|PF)/);
694
695 $groups{$current} -> MAKE(formula=>$$rparams{formula});
696 project_state(0);
697 $widget{safluo_make} -> configure(-state=>'normal');
698
699 Echo("Doing Atoms correction ... done!");
700 $top -> Unbusy;
701 };
702
703
704
705
706 sub sa_group {
707 my ($parent, $rparams) = @_;
708 my ($group, $label) = ("SA ".$groups{$parent}->{label}, "");
709 ($group, $label) = group_name($group);
710 $label = "SA ".$groups{$parent}->{label};
711 $groups{$group} = Ifeffit::Group -> new(group=>$group, label=>$label);
712 ## copy the titles
713 my $line = Xray::Absorption->get_Siegbahn_full($$rparams{line});
714 my $method = "";
715 SWITCH: {
716 $method = "Haskel's Fluo", last SWITCH if ($$rparams{algorithm} eq 'fluo');
717 $method = "Booth and Bridges", last SWITCH if ($$rparams{algorithm} eq 'booth');
718 $method = "Troger et al.", last SWITCH if ($$rparams{algorithm} eq 'troger');
719 $method = "correction terms from Atoms", last SWITCH if ($$rparams{algorithm} eq 'atoms');
720 };
721 push @{$groups{$group}->{titles}},
722 "Self absorption correction of \"$groups{$parent}->{label}\" using method of $method";
723 if ($$rparams{algorithm} eq 'booth') {
724 push @{$groups{$group}->{titles}},
725 "+ $groups{$parent}->{bkg_z} $groups{$parent}->{fft_edge} edge, computed using the $groups{$parent}->{bkg_z} $line line",
726 "+ Formula=$$rparams{formula}, Thickness=$$rparams{thickness} microns",
727 "+ Incident angle=$$rparams{angle_in} degrees, Outgoing angle=$$rparams{angle_out} degrees";
728 } else {
729 push @{$groups{$group}->{titles}},
730 "+ $groups{$parent}->{bkg_z} $groups{$parent}->{fft_edge} edge, computed using the $groups{$parent}->{bkg_z} $line line",
731 "+ Formula=$$rparams{formula}, Incident/Outgoing angles: [$$rparams{angle_in}, $$rparams{angle_out}] degrees";
732 };
733 $groups{$group} -> make(file=>"Self absorption correction of \"$groups{$parent}->{label}\"");
734 foreach (@{$groups{$parent}->{titles}}) {
735 push @{$groups{$group}->{titles}}, $_;
736 };
737 $groups{$group} -> put_titles;
738 $groups{$group} -> set_to_another($groups{$parent});
739 $groups{$group} -> make(is_rsp => 0, is_qsp => 0, is_bkg => 0, not_data => 0, bkg_eshift=>0);
740 if ($$rparams{algorithm} eq 'fluo') {
741 ## xanes correction
742 $groups{$group} -> make(is_xmu => 1, is_chi => 0);
743 $groups{$group} -> dispose("set($group.energy = s___a.energy, $group.xmu = s___a.sacorr)", $dmode);
744 } else {
745 ## exafs correction
746 $groups{$group} -> make(is_xmu => 0, is_chi => 1);
747 $groups{$group} -> dispose("set($group.k = $parent.k, $group.chi = s___a.chi)", $dmode);
748 };
749 ++$line_count;
750 fill_skinny($list, $group, 1, 1);
751 Echo("Saved absorption corrected data group.");
752 my $memory_ok = $groups{$group} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
753 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
754 };
755
756
757
758
759 ## do some chores common to all algorithms
760 sub sa_feedback {
761 my $rparams = $_[0];
762
763 $widget{safluo_feedback} -> delete('1.0', 'end');
764 SWITCH: {
765 $$rparams{line} = 'Ka1', last SWITCH if (lc($groups{$current}->{fft_edge}) eq 'k');
766 $$rparams{line} = 'La1', last SWITCH if (lc($groups{$current}->{fft_edge}) eq 'l3');
767 $$rparams{line} = 'Lb1', last SWITCH if (lc($groups{$current}->{fft_edge}) eq 'l2');
768 $$rparams{line} = 'Lb3', last SWITCH if (lc($groups{$current}->{fft_edge}) eq 'l1');
769 $$rparams{line} = 'Ma', last SWITCH if (lc($groups{$current}->{fft_edge}) =~ /^m/);
770 };
771 my $efluo = Xray::Absorption -> get_energy($groups{$current}->{bkg_z}, $$rparams{line});
772
773 my %count;
774 my $ok = parse_formula($$rparams{formula}, \%count);
775 my $answer = "\nEdge energy = " . $groups{$current}->{bkg_e0} . $/;
776 $answer .= sprintf("The dominant fluorescence line is %s (%s)\n",
777 Xray::Absorption -> get_Siegbahn_full($$rparams{line}),
778 Xray::Absorption -> get_IUPAC($$rparams{line}));
779 $answer .= sprintf("Fluorescence energy = %.2f\n", $efluo);
780 if ($ok) {
781 $answer .= "\n element number \n";
782 $answer .= " --------- ----------------\n";
783 foreach my $k (sort (keys(%count))) {
784 if ($count{$k} > 0.001) {
785 $answer .= sprintf(" %-2s %11.3f\n", $k, $count{$k});
786 } else {
787 $answer .= sprintf(" %-2s %g\n", $k, $count{$k});
788 };
789 };
790 $widget{safluo_feedback} -> insert('end', $answer, ['margin']);
791 } else {
792 $widget{safluo_feedback} -> insert('end', "\nInput error:\n\t".$count{error}, ['error']);
793 return
794 };
795
796 return ($ok, $efluo, \%count);
797 };
798
799 sub sa_info_depth {
800 my $rparams = $_[0];
801 Echo("Computing information depth");
802 $top -> Busy;
803
804 my $e0shift = $groups{$current}->{bkg_eshift};
805 my ($ok, $efluo, $rcount) = sa_feedback($rparams);
806 unless ($ok) {
807 Error("Error parsing formula.");
808 $top -> Unbusy;
809 return;
810 };
811
812 my ($barns, $amu) = (0,0);
813 foreach my $el (keys(%$rcount)) {
814 $barns += Xray::Absorption -> cross_section($el, $efluo) * $$rcount{$el};
815 $amu += Xray::Absorption -> get_atomic_weight($el) * $$rcount{$el};
816 };
817 my $muf = sprintf("%.6f", $barns / $amu / 1.6607143);
818 my $angle_ratio = sprintf("%.6f", sin(PI*$$rparams{angle_in}/180) / sin(PI*$$rparams{angle_out}/180));
819
820 my @k = Ifeffit::get_array($current.".k");
821 my $kmax = ($k[$#k] > $plot_features{kmax}) ? $plot_features{kmax} : $k[$#k];
822 my @mut = ();
823 foreach my $kk (@k) {
824 my ($barns, $amu) = (0,0);
825 my $e = $groups{$current}->k2e($kk) + $groups{$current}->{bkg_e0} + $e0shift;
826 foreach my $el (keys(%$rcount)) {
827 ##next if (lc($el) eq lc(get_symbol($groups{$current}->{bkg_z})));
828 $barns += Xray::Absorption -> cross_section($el, $e) * $$rcount{$el};
829 $amu += Xray::Absorption -> get_atomic_weight($el) * $$rcount{$el};
830 };
831 ## 1 amu = 1.6607143 x 10^-24 gm
832 push @mut, $barns / $amu / 1.6607143;
833 };
834
835 $groups{$current} -> dispose("## inserted s___a.mut into ifeffit's memory...", $dmode);
836 Ifeffit::put_array("s___a.mut", \@mut);
837 my $sets = "set(s___a.alpha = s___a.mut + $angle_ratio*$muf,";
838 $sets .= " s___a.info = 10000*sin(pi*$$rparams{angle_in}/180) / s___a.alpha)";
839 $groups{$current} -> dispose($sets, $dmode);
840 my $command = "($current.k, s___a.info, xmin=$plot_features{kmin}, xmax=$kmax, ";
841 $command .= "xlabel=k (\\A\\u-1\\d), ylabel=\"Depth (\\gmm)\", ";
842 my $screen = "fg=$config{plot}{fg}, bg=$config{plot}{bg}, ";
843 $screen .= ($config{plot}{grid} eq $config{plot}{bg}) ? "nogrid, " :
844 "grid, gridcolor=$config{plot}{grid}, ";
845 $command .= $screen;
846 $command .= "style=lines, color=blue, key=\"\\gl(k)\", title=\"Information Depth\")";
847 $command = wrap("newplot", " ", $command) . $/;
848 $groups{$current} -> dispose($command, $dmode);
849 $top -> Unbusy;
850 Echo("Computing information depth ... done!");
851 };
852
853
854 ## END OF SELF-ABSORPTION SUBSECTION
855 ##########################################################################################