Debian Codegraph @

athena_parts/align.pl

raw
1 ## -*- cperl -*-
2 ##
3 ## This file is part of Athena, copyright (c) 2001-2008 Bruce Ravel
4 ##
5 ## This section of the code contains subroutines associated with
6 ## data alignment.
7
8 ## pop-up a palette for interactively aligning one scan ralative to
9 ## another by comparing norm(E) or deriv(E)
10 sub align_two {
11 Echo("No data!"), return unless $current;
12 my %align_params;
13 $align_params{space} = $_[0];
14 $align_params{space} = ($align_params{space} eq 'x') ? 'em' : 'em'.$align_params{space};
15 ($align_params{space} = 'emdsss') if ($align_params{space} eq 'ems');
16 my $color = $plot_features{c1};
17
18 ##Echo("You cannot align to the Default Parameters"), return
19 Echo("No data!"), return if ($current eq "Default Parameters");
20
21 my @keys = ();
22 foreach my $k (&sorted_group_list) {
23 ($groups{$k}->{is_xmu}) and push @keys, $k;
24 };
25 Echo("You need two or more xmu groups to align"), return unless ($#keys >= 1);
26
27 $align_params{fit} = $config{align}{fit} || 'd';
28 $align_params{keys} = \@keys;
29 $align_params{standard} = $keys[0];
30 my $standard_lab = "1: " . $groups{$keys[0]}->{label};
31 if ($align_params{standard} eq $current) { # make sure $current is sensible given
32 set_properties(1, $keys[1], 0);# that $keys[0] is the standard
33 # adjust the view
34 my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0;
35 ($here < 0) and ($here = 0);
36 my $full = ($list->bbox(@skinny_list))[3] + 5;
37 $list -> yview('moveto', $here/$full);
38 };
39
40 my $ps = $project_saved;
41 my @save = ($plot_features{emin}, $plot_features{emax});
42 $plot_features{emin} = $config{align}{emin};
43 $plot_features{emax} = $config{align}{emax};
44 project_state($ps); # don't toggle if currently saved
45
46
47 $align_params{shift} = $groups{$current}->{bkg_eshift};
48 $align_params{prior_shift} = $groups{$current}->{bkg_eshift};
49
50 $fat_showing = 'align';
51 $hash_pointer = \%align_params;
52 map {$_ -> configure(-state=>'disabled')}
53 ($data_menu, $merge_menu, $anal_menu, $settings_menu);
54 $fat -> packForget;
55 my $align = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3)
56 -> pack(-fill=>'both', -expand=>1);
57 #$align -> packPropagate(0);
58 $which_showing = $align;
59
60 $align -> Label(-text=>"Data alignment",
61 -font=>$config{fonts}{large},
62 -foreground=>$config{colors}{activehighlightcolor})
63 -> pack(-side=>'top', -fill=>'x', -anchor=>'w');
64
65 ## select the alignment standard
66 my $frame = $align -> Frame(-borderwidth=>2, -relief=>'flat')
67 -> pack(-side=>'top', -fill=>'x', -pady=>8);
68 $frame -> Label(-text=>"Standard: ",
69 -foreground=>$config{colors}{activehighlightcolor},
70 )
71 -> grid(-row=>0, -column=>0, -sticky=>'e', -ipady=>2);
72
73 $widget{align_menu} = $frame -> BrowseEntry(-variable => \$standard_lab,
74 @browseentry_list,
75 -browsecmd => sub {
76 my $text = $_[1];
77 my $this = $1 if ($text =~ /^(\d+):/);
78 Echo("Failed to match in browsecmd. Yikes! Complain to Bruce."), return unless $this;
79 $this -= 1;
80 $align_params{standard}=$align_params{keys}->[$this];
81 #$standard_lab="$groups{$s}->{label} ($s)";
82 &do_eshift(\%align_params, $current);
83 })
84 -> grid(-row=>0, -column=>1, -sticky=>'w');
85 my $i = 1;
86 foreach my $s (@keys) {
87 $widget{align_menu} -> insert("end", "$i: $groups{$s}->{label}");
88 ++$i;
89 };
90
91 ## the group for alignment is the current group in the group list
92 $frame -> Label(-text=>"Other: ",
93 -foreground=>$config{colors}{activehighlightcolor},
94 )
95 -> grid(-row=>1, -column=>0, -sticky=>'e', -ipady=>2);
96 $widget{align_unknown} = $frame -> Label(-text=>$groups{$current}->{label},
97 -foreground=>$config{colors}{button},
98 )
99 -> grid(-row=>1, -column=>1, -sticky=>'w', -pady=>2, -padx=>2);
100 my $other_label;
101
102 ## select the way to plot the data
103 $frame -> Label(-text=>"Plot as: ",
104 -foreground=>$config{colors}{activehighlightcolor},
105 )
106 -> grid(-row=>2, -column=>0, -sticky=>'e', -ipady=>2);
107 my $space_label = 'xmu';
108 ($space_label = 'normalized xmu') if ($align_params{space} eq 'emn');
109 ($space_label = 'derivative') if ($align_params{space} eq 'emd');
110 ($space_label = 'smoothed derivative') if ($align_params{space} eq 'emdsss');
111 my $menu = $frame -> Optionmenu(-textvariable => \$space_label,
112 -borderwidth=>1,
113 -width=>19, -justify=>'right')
114 -> grid(-row=>2, -column=>1, -sticky=>'w');
115 foreach my $p (qw(em emn emd emdsss)) {
116 my $label = 'xmu';
117 ($label = 'normalized xmu') if ($p eq 'emn');
118 ($label = 'derivative') if ($p eq 'emd');
119 ($label = 'smoothed derivative') if ($p eq 'emdsss');
120 $menu -> command(-label => $label,
121 -command=>sub{$align_params{space}=$p;
122 $space_label=$label;
123 &do_eshift(\%align_params, $current);
124 });
125 };
126
127 ## select the fitting function
128 $frame -> Label(-text=>"Fit as: ",
129 -foreground=>$config{colors}{activehighlightcolor},
130 )
131 -> grid(-row=>3, -column=>0, -sticky=>'e', -ipady=>2);
132 my $fit_label = 'derivative';
133 ($fit_label = 'smoothed derivative') if ($align_params{fit} eq 's');
134 $menu = $frame -> Optionmenu(-textvariable => \$fit_label,
135 -borderwidth=>1,
136 -width=>19, -justify=>'right')
137 -> grid(-row=>3, -column=>1, -sticky=>'w');
138 foreach my $p (qw(d s)) {
139 my $label = 'derivative';
140 ($label = 'smoothed derivative') if ($p eq 's');
141 $menu -> command(-label => $label,
142 -command=>sub{$align_params{fit}=$p;
143 $fit_label=$label;
144 });
145 };
146
147
148 $align -> Frame(-background=>$config{colors}{darkbackground})
149 -> pack(-side=>'bottom', -expand=>1, -fill=>'both');
150 $align -> Button(-text=>'Return to the main window', @button_list,
151 -background=>$config{colors}{background2},
152 -activebackground=>$config{colors}{activebackground2},
153 -command=>sub{$groups{$current} -> make(bkg_eshift=>$align_params{shift},
154 update_bkg=>1);
155 ## tie together data and reference
156 if ($groups{$current}->{reference} and exists($groups{$groups{$current}->{reference}})) {
157 $groups{$groups{$current}->{reference}} -> make(bkg_eshift=>$align_params{shift},
158 update_bkg=>1);
159 };
160 &reset_window($align, "alignment", \@save);
161 })
162 -> pack(-side=>'bottom', -fill=>'x');
163 ## help button
164 $align -> Button(-text=>'Document section: aligning data', @button_list,
165 -command=>sub{pod_display("process::align.pod")})
166 -> pack(-side=>'bottom', -fill=>'x', -pady=>4);
167
168
169
170 ## frame with buttons in
171 $frame = $align -> Frame(-borderwidth=>2, -relief=>'groove')
172 -> pack(-side=>'bottom', -fill=>'x');
173
174 my $bbox = $frame -> Frame(-borderwidth=>2, -relief=>'flat')
175 -> pack(-side=>'bottom');
176
177 my $lab = $bbox -> Frame(-borderwidth=>2, -relief=>'flat')
178 ->grid(-columnspan=>2, -row=>0, -column=>0, -padx=>2, -pady=>4, -sticky=>'ew');
179
180 $widget{align_other_label} = $lab ->
181 Label(
182 ##-text=>"Shift \"$groups{$current}->{label}\" by ",
183 -text=>"Shift by ",
184 -foreground=>$config{colors}{activehighlightcolor},
185 )
186 -> pack(-side=>'left');
187 $widget{align_result} = $lab -> RetEntry(-textvariable=>\$align_params{shift},
188 -validate=>'key',
189 -command=>sub{
190 $groups{$current} -> make(bkg_eshift=>$align_params{shift}, update_bkg=>1); # update object then plot
191 ## tie together data and reference
192 if ($groups{$current}->{reference} and exists($groups{$groups{$current}->{reference}})) {
193 $groups{$groups{$current}->{reference}} -> make(bkg_eshift=>$align_params{shift});
194 };
195 },
196 -validatecommand=>[\&set_variable, 'al_en'],
197 -width=>6)
198 -> pack(-side=>'left');
199 $lab -> Label(-text=>" eV.",
200 -foreground=>$config{colors}{activehighlightcolor},)
201 -> pack(-side=>'left');
202
203
204
205 $widget{align_auto} = $bbox -> Button(-text=>"Auto align", @button_list,
206 -command=>sub{Echo("Not aligning: \"$groups{$current}->{label}\" is a frozen group."), return if $groups{$current}->{frozen};
207 $align_params{shift} = auto_align($align_params{standard}, $current, $align_params{fit});
208 do_eshift(\%align_params, $current)})
209 ->grid(-columnspan=>2, -row=>1, -column=>0, -padx=>2, -pady=>4, -sticky=>'ew');
210 $widget{align_replot} = $bbox -> Button(-text=>"Replot", @button_list,
211 -command=>sub{do_eshift(\%align_params, $current)})
212 ->grid(-columnspan=>2, -row=>2, -column=>0, -padx=>2, -pady=>4, -sticky=>'ew');
213
214 my $row = 3;
215 foreach my $e (5, 1, 0.5, 0.1) {
216 $widget{"align_plus".$e} =
217 $bbox -> Button(-text=>"-".$e, -width=>6, @button_list,
218 -command=>sub{Echo("Not aligning: \"$groups{$current}->{label}\" is a frozen group."), return if $groups{$current}->{frozen};
219 $align_params{shift} -= $e;
220 ($align_params{shift} = 0) if (abs($align_params{shift}) < EPSI);
221 do_eshift(\%align_params, $current)})
222 ->grid(-row=>$row, -column=>0, -padx=>2, -pady=>4);
223 $widget{"align_minus".$e} =
224 $bbox -> Button(-text=>"+".$e, -width=>6, @button_list,
225 -command=>sub{Echo("Not aligning: \"$groups{$current}->{label}\" is a frozen group."), return if $groups{$current}->{frozen};
226 $align_params{shift} += $e;
227 ($align_params{shift} = 0) if (abs($align_params{shift}) < EPSI);
228 do_eshift(\%align_params, $current)})
229 ->grid(-row=>$row, -column=>1, -padx=>2, -pady=>4);
230 ++$row;
231 };
232 $widget{align_restore} =
233 $bbox -> Button(-text=>"Restore value", @button_list,
234 -command=>sub{Echo("Not aligning: \"$groups{$current}->{label}\" is a frozen group."), return if $groups{$current}->{frozen};
235 $align_params{shift} = $align_params{prior_shift};
236 do_eshift(\%align_params, $current)})
237 ->grid(-columnspan=>2, -row=>++$row, -column=>0, -padx=>2, -pady=>4, -sticky=>'ew');
238
239 $widget{align_marked} =
240 $bbox -> Button(-text=>"Align all\nmarked groups", @button_list,
241 -command=>sub{
242 Echonow("Aligning marked groups ...");
243 $top -> Busy;
244 foreach my $g (&sorted_group_list) {
245 next unless $marked{$g};
246 next if ($align_params{standard} eq $g);
247 Echonow("Auto-aligning \"$groups{$g}->{label}\"");
248 my $sh = auto_align($align_params{standard}, $g, $align_params{fit});
249 $groups{$g} -> make(bkg_eshift=>$sh, update_bkg=>1); # update object then plot
250 ## tie together data and reference
251 if ($groups{$g}->{reference} and exists($groups{$g}->{reference})) {
252 $groups{$groups{$g}->{reference}} -> make(bkg_eshift=>$sh);
253 };
254 };
255 $align_params{shift} = $groups{$current}->{bkg_eshift};
256 do_eshift(\%align_params, $current);
257 $top -> Unbusy;
258 Echonow("Aligning marked groups ... done!");
259 })
260 ->grid(-columnspan=>2, -rowspan=>2, -row=>1, -column=>2,
261 -padx=>12, -pady=>4, -sticky=>'new');
262
263 do_eshift(\%align_params, $current);
264 $plotsel -> raise('e');
265 $top -> update;
266 };
267
268
269
270
271 ## perform the e0 shift requested by the align_two subroutine and replot
272 sub do_eshift {
273 my ($r, $gr) = @_;
274 my ($sp, $st, $sh) = ($$r{space}, $$r{standard}, $$r{shift});
275
276 Echo("Not aligning: \"$groups{$gr}->{label}\" is a frozen group.") if $groups{$gr}->{frozen};
277 Error("Alignment aborted: " . $groups{$gr}->{label} . " is not an xmu group."),
278 return unless ($groups{$gr}->{is_xmu});
279
280 my $color = $plot_features{c1};
281 my $scale = $groups{$gr}->{plot_scale};
282 my $key = $groups{$gr}->{label};
283 my $other_string = q{};
284 SWITCH: {
285 ($sp eq 'emn') and do {
286 if ($groups{$gr}->{bkg_flatten}) {
287 $other_string = "plot($gr.energy+$sh, $gr.flat, style=lines, color=$color, key=\"$key\")";
288 } else {
289 $other_string = "plot($gr.energy+$sh, $gr.norm, style=lines, color=$color, key=\"$key\")";
290 };
291 last SWITCH;
292 };
293 ($sp eq 'emd') and do {
294 $other_string = "plot($gr.energy+$sh, $scale*deriv($gr.xmu)/deriv($gr.energy), style=lines, color=$color, key=\"$key\")";
295 last SWITCH;
296 };
297 ($sp eq 'emdsss') and do {
298 $other_string = "set $gr.der = deriv(smooth(smooth(smooth($gr.xmu))))/deriv($gr.energy)\n";
299 $other_string .= "plot($gr.energy+$sh, $scale*$gr.der, style=lines, color=$color, key=\"$key\")";
300 last SWITCH;
301 };
302 ($sp eq 'em') and do {
303 $other_string = "plot($gr.energy+$sh, $gr.xmu, style=lines, color=$color, key=\"$key\")";
304 last SWITCH;
305 };
306 };
307 $groups{$gr} -> dispose("## aligning $gr to $st", $dmode);
308 $groups{$gr} -> make(bkg_eshift=>$sh, update_bkg=>1); # update object then plot
309 ## tie together data and reference
310 if ($groups{$gr}->{reference} and exists($groups{$groups{$gr}->{reference}})) {
311 $groups{$groups{$gr}->{reference}} -> make(bkg_eshift=>$sh);
312 };
313 if ($current eq $gr) {
314 my $v = $groups{$gr}->{bkg_eshift};
315 $widget{bkg_eshift} -> configure(-validate=>'none');
316 $widget{bkg_eshift} -> delete(qw/0 end/);
317 $widget{bkg_eshift} -> insert(0, $sh);
318 $widget{bkg_eshift} -> configure(-validate=>'key');
319 ##$widget{bkg_eshift} -> configure(-text=>$sh);
320 };
321 $groups{$st} -> plotE($sp,$dmode,\%plot_features, \@indicator);
322 $groups{$gr} -> dispatch_bkg($dmode) if ($$r{space} eq 'emn');
323 $groups{$gr} -> dispose($other_string, $dmode);
324 $last_plot='e';
325 ## keep detector groups aligned with their parent xmu group
326 foreach my $g (@{$groups{$gr}->{detectors}}) {
327 $groups{$g}->make(bkg_eshift=>$sh);
328 };
329 project_state(0);
330 $top -> update;
331 };
332
333
334 ## align two groups by computing a difference of derivative spectrum
335 ## and minimizing an applied e0 shift
336 sub auto_align {
337 my ($standard, $other, $how) = @_;
338 return 0 if (($standard eq 'None') or ($other eq 'None'));
339
340 $standard = $groups{$standard}->{group};
341 my $st_e0 = $groups{$standard}->{bkg_eshift};
342 my ($xmin, $xmax) = (int($groups{$standard}->{bkg_e0}-50),
343 int($groups{$standard}->{bkg_e0}+100));
344 $other = $groups{$other}->{group};
345 my $ot_e0 = $groups{$other}->{bkg_eshift};
346 my $command = "## auto aligning $other to $standard\n";
347 $command .= "guess(aa___esh=$ot_e0, aa___scale=1)\n";
348 $command .= "def($other.xmui = interp($other.energy+aa___esh, $other.xmu, $standard.energy+$st_e0),\n";
349 if ($how eq 'd') {
350 $command .= " aa___.res = deriv($standard.xmu)/deriv($standard.energy) - aa___scale*deriv($other.xmui)/deriv($other.energy))\n";
351 } else {
352 $command .= " aa___.res = smooth(smooth(smooth(deriv($standard.xmu)/deriv($standard.energy)))) - aa___scale*smooth(deriv($other.xmui)/deriv($other.energy)))\n";
353 };
354 $command .= "minimize(aa___.res, x=$standard.energy, xmin=$xmin, xmax=$xmax)\n";
355 $groups{$standard} -> dispose($command, $dmode); # do it
356 my $esh = Ifeffit::get_scalar("aa___esh");
357 ($esh=0) unless ($esh =~ /-?(\d+\.?\d*|\.\d+)/);
358 $command = "set(aa___.res = aa___.res, $other.xmui = $other.xmui)\n";
359 $command .= "unguess\n";
360 $command .= "erase aa___esh aa___scale aa___.res $other.xmui\n";
361 $command .= "## done auto aligning\n";
362 $groups{$standard} -> dispose($command, $dmode); # clean up
363 project_state(0);
364 return sprintf("%.3f",$esh); # return the e0 shift
365 };
366
367
368
369 ## END OF DATA ALIGNMENT SUBSECTION
370 ##########################################################################################