Debian Codegraph @

athena_parts/deglitch.pl

raw
1 ## -*- cperl -*-
2 ##
3 ## This file is part of Athena, copyright (c) 2001-2006 Bruce Ravel
4 ##
5 ## This section of the code contains subroutines associated with
6 ## data deglitching
7
8 sub choose_a_point {
9 my $group = $_[1];
10 my $space = $_[2];
11 Error("Point selection aborted: " . $groups{$group}->{label} . " is not an xmu group."),
12 return unless ($groups{$current}->{is_xmu});
13 Echonow("Select a point by clicking on the plot");
14 my ($yoffset, $kw, $plot_scale) = ($groups{$group}->{plot_yoffset},
15 $groups{$group}->{fft_kw},
16 $groups{$group}->{plot_scale});
17 my (@x, @y);
18 if ($space eq 'emg') {
19 $plot_features{linestyle} = "linespoints3";
20 $groups{$group} -> plotE('em',$dmode,\%plot_features, \@indicator);
21 $plot_features{linestyle} = "lines";
22 @x = Ifeffit::get_array($group.".energy");
23 @y = Ifeffit::get_array($group.".xmu");
24 $groups{$group} -> dispose("set(___y = ceil($group.xmu), ___z = floor($group.xmu))");
25 $last_plot = 'e';
26 } else {
27 &plot_chie($group,1);
28 @x = Ifeffit::get_array($group.".energy");
29 @y = Ifeffit::get_array($group.".chie");
30 $groups{$group} -> dispose("set(___y = ceil($group.chie), ___z = floor($group.chie))");
31 $last_plot = 'e';
32 };
33 my $maxy = Ifeffit::get_scalar('___y');
34 my $miny = Ifeffit::get_scalar('___z');
35 $groups{$group} -> dispose('cursor(crosshair=true)', 1);
36 my ($xx, $yy) = (Ifeffit::get_scalar('cursor_x'), Ifeffit::get_scalar('cursor_y'));
37 my ($dist, $ii) = (1e10, -1);
38 foreach my $i (0 .. $#x) { # need to scale these appropriately
39 #my $px = ($x[$i] - $x[0])/($x[$#x] - $x[0]);
40 #my $py = ($y[$i] - $y[0])/($y[$#y] - $y[0]);
41 #my $xn = ($xx - $x[0])/($x[$#x] - $x[0]);
42 #my $yn = ($yy - $y[0])/($y[$#y] - $y[0]);
43 #my $d = sqrt(($px - $xn)**2 + ($py - $yn)**2);
44
45 #print join(" ", $px, $py, $xn, $yn, $d), $/;
46
47 my $px = ($x[$i] - $xx)/($x[-1] - $x[0]);
48 ##my $py = ($y[$i] - $yy)/($y[$#y] - $y[0]);
49 my $py = ($y[$i] - $yy)/($maxy - $miny);
50 my $d = sqrt($px**2 + $py**2);
51 #print join(" ", $px, $py, $d), $/;
52
53 ($d < $dist) and ($dist, $ii) = ($d, $i);
54 };
55 if ($space eq 'emg') {
56 $groups{$group} -> dispose("pmarker $group.energy, $group.xmu, $x[$ii], " .
57 "$plot_features{marker}, $plot_features{markercolor}, $yoffset", $dmode);
58 } else {
59 $groups{$group} -> dispose("pmarker $group.energy, $group.chie, $x[$ii], " .
60 "$plot_features{marker}, $plot_features{markercolor}, $yoffset", $dmode);
61 };
62 #$_[0]->raise;
63 Echo("You selected (" . sprintf("%.3f", $x[$ii]) . ", " . sprintf("%.5f", $y[$ii]) . ").");
64
65 return $ii;
66 };
67
68
69 sub deglitch_a_point {
70 my $ii = $_[0];
71 my $group = $_[1];
72 my $space = $_[2];
73 Echo("Point -1?"), return if ($ii < 0);
74 my @x = Ifeffit::get_array($group.".energy");
75 my @y = Ifeffit::get_array($group.".xmu");
76 my $str = "Removed point $ii at x=$x[$ii], y=$y[$ii]";
77 splice(@x, $ii, 1);
78 splice(@y, $ii, 1);
79 Ifeffit::put_array($group.".energy", \@x);
80 Ifeffit::put_array($group.".xmu", \@y);
81 $groups{$group} -> make(update_bkg=>1);
82 if ($space eq 'emg') {
83 $groups{$group} -> plotE($space,$dmode,\%plot_features, \@indicator);
84 $last_plot = 'e';
85 } else {
86 &plot_chie($group,0);
87 $last_plot = 'e';
88 };
89 project_state(0);
90 &refresh_properties;
91 Echo($str);
92 };
93
94
95 sub deglitch_palette {
96 Echo("You must select a data group to deglitch"), return
97 if ($current eq "Default Parameters");
98 Echo("No data!"), return unless $current;
99
100 my %degl_params;
101 my @keys = ();
102 foreach my $k (&sorted_group_list) {
103 ($groups{$k}->{is_xmu}) and push @keys, $k;
104 };
105 Echo("You need at least one group to deglitch"), return unless (@keys);
106 $degl_params{standard} = ($groups{$current}->{is_xmu}) ? $current : $keys[0];
107 $groups{$degl_params{standard}}->dispatch_bkg if $groups{$degl_params{standard}}->{update_bkg};
108 $degl_params{standard_label} = $groups{$degl_params{standard}}->{label};
109 $degl_params{deg_emin} = $groups{$degl_params{standard}}->{bkg_nor1} +
110 $groups{$degl_params{standard}}->{bkg_e0};
111 $groups{$degl_params{standard}} ->
112 dispose("set ___x = ceil($degl_params{standard}.energy)\n", 1);
113 my $maxE = Ifeffit::get_scalar("___x");
114 $degl_params{deg_emax} = $config{deglitch}{emax} ? $maxE+$config{deglitch}{emax} : $maxE*1.1;
115 $degl_params{deg_emin} = sprintf("%.3f",$degl_params{deg_emin});
116 $degl_params{deg_emax} = sprintf("%.3f",$degl_params{deg_emax});
117 $degl_params{deg_tol} = sprintf("%.4f", $groups{$degl_params{standard}}->{bkg_step} * $config{deglitch}{margin});
118 $degl_params{space} = 'emg';
119 $degl_params{space_label} = 'mu(E)';
120 my @save = ($plot_features{emin}, $plot_features{emax});
121
122 set_deglitch_params(\%degl_params);
123
124 my $point_choice = -1;
125
126 $fat_showing = 'deglitch';
127 $hash_pointer = \%degl_params;
128 map {$_ -> configure(-state=>'disabled')}
129 ($data_menu, $merge_menu, $anal_menu, $settings_menu);
130 $fat -> packForget;
131 my $degl = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3)
132 -> pack(-fill=>'both', -expand=>1);
133 #$degl -> packPropagate(0);
134 $which_showing = $degl;
135
136 $degl -> Label(-text=>"Deglitch data",
137 -font=>$config{fonts}{large},
138 -foreground=>$config{colors}{activehighlightcolor})
139 -> pack(-side=>'top', -fill=>'x', -anchor=>'w');
140
141 ## which group ...?
142 my $fr = $degl -> Frame(-borderwidth=>2, -relief=>'flat')
143 -> pack(-side=>'top', -pady=>8, -fill=>'x');
144 my $frame = $fr -> Frame(-borderwidth=>2, -relief=>'flat')
145 -> pack(-pady=>1, -fill=>'x');
146 $frame -> Label(-text=>"Group: ",
147 -foreground=>$config{colors}{activehighlightcolor},
148 )
149 -> pack(-side=>'left', -anchor=>'e', -fill=>'x');
150 $widget{deg_group} = $frame -> Label(-text=>$groups{$current}->{label},
151 -foreground=>$config{colors}{button})
152 -> pack(-side=>'left', -anchor=>'w', -fill=>'x');
153
154
155 ## this frame has all the active elements
156 $frame = $fr -> LabFrame(-label=>'Deglitch a single point',
157 -foreground=>$config{colors}{activehighlightcolor},
158 -labelside=>'acrosstop')
159 -> pack(-pady=>3, -padx=>3, -ipady=>3, -ipadx=>3, -fill=>'x');
160
161 my $upper = $frame -> Frame ()
162 -> pack(-side=>'top');
163 $upper -> Label(-text=>'Plot as:',
164 -foreground=>$config{colors}{activehighlightcolor},
165 )
166 -> pack(-side=>'left');
167 my $menu = $upper -> Optionmenu(-textvariable => \$degl_params{space_label},
168 -borderwidth=>1,
169 -width=>12, -justify=>'right')
170 -> pack(-side=>'left');
171 foreach my $sp ('mu(E)', 'chi(E)') {
172 my $how = 'emg';
173 ($how = 'k' . $plot_features{k_w} . 'e') if ($sp eq 'chi(E)');
174 $menu -> command(-label => $sp,
175 -command=>sub{$degl_params{space} = $how;
176 $degl_params{space_label} = $sp;
177 set_deglitch_params(\%degl_params);
178 if ($degl_params{space} eq 'emg') {
179 $plot_features{emin} = $save[0];
180 $groups{$degl_params{standard}} -> plotE($degl_params{space},$dmode,\%plot_features, \@indicator);
181 $last_plot = 'e';
182 map {$widget{"deg_$_"}->configure(-state=>'normal')} (qw(tol emin emax replot remove));
183 map {$grab{"deg_$_"} ->configure(-state=>'normal')} (qw(emin emax));
184 } else {
185 $plot_features{emin} = $config{deglitch}{chie_emin};
186 &plot_chie($degl_params{standard},0);
187 #$groups{$degl_params{standard}} -> plotk($degl_params{space},$dmode,\%plot_features, \@indicator);
188 $last_plot = 'e';
189 map {$widget{"deg_$_"}->configure(-state=>'disabled')} (qw(tol emin emax replot remove));
190 map {$grab{"deg_$_"} ->configure(-state=>'disabled')} (qw(emin emax));
191 };
192 $top -> update;});
193 };
194
195
196 my $fr1 = $frame -> Frame(-borderwidth=>2, -relief=>'flat')
197 -> pack(-expand=>1, -fill=>'x');
198 my $help = "Choose a point with the cursor, then press the \"Remove\" button to delete it";
199 $fr1 -> Button(-text=>"Help", @button_list,
200 -width=>1,
201 -command=>[\&Echo, $help])
202 -> pack(-side=>'right', -expand=>1, -fill=>'x');
203 $widget{deg_sreplot} = $fr1 -> Button(-text=>"Replot", @button_list,
204 -width=>1,
205 -command=>sub{
206 if ($degl_params{space} eq 'emg') {
207 $groups{$current} -> plotE('emg',$dmode,\%plot_features, \@indicator);
208 } else {
209 &plot_chie($current,0);
210 };
211 })
212 -> pack(-side=>'left', -expand=>1, -fill=>'x');
213 $widget{deg_point} = $fr1 -> Button(-text=>"Remove point", @button_list,
214 -width=>1,
215 -command=>sub{Echo("Deglitching aborted: \"$groups{$current}->{label}\" is frozen"), return
216 if $groups{$current}->{frozen};
217 &deglitch_a_point($point_choice, $degl_params{standard}, $degl_params{space});
218 $widget{deg_point}->configure(-state=>'disabled');
219 #$degl->raise;
220 $top -> update;
221 },
222 -state=>'disabled')
223 -> pack(-side=>'right', -expand=>1, -fill=>'x');
224 $widget{deg_single} = $fr1 -> Button(-text=>"Choose a point", @button_list,
225 -width=>1,
226 -command=>sub{$point_choice = &choose_a_point($degl, $degl_params{standard}, $degl_params{space});
227 $widget{deg_point}->configure(-state=>'normal')})
228 -> pack(-side=>'left', -expand=>1, -fill=>'x');
229
230 $frame = $fr -> LabFrame(-label=>'Deglitch many points',
231 -foreground=>$config{colors}{activehighlightcolor},
232 -labelside=>'acrosstop')
233 -> pack(-pady=>3, -padx=>3, -ipady=>3, -ipadx=>3, -fill=>'x');
234 my $fr2 = $frame -> Frame(-borderwidth=>2, -relief=>'flat')
235 -> pack(-side=>'bottom', -fill=>'x', -expand=>1);
236 #$fr2 -> Label(-width=>1) -> pack(-side=>'left', -fill=>'x', -expand=>1);
237 $fr2 -> Label(-text=>'Tolerance:',
238 -foreground=>$config{colors}{activehighlightcolor},
239 )
240 -> pack(-side=>'left');
241 $widget{deg_tol} = $fr2 -> Entry(-textvariable=>\$degl_params{deg_tol}, -width=>7)
242 -> pack(-side=>'left');
243 $fr2 -> Label(-width=>1) -> pack(-side=>'left', -fill=>'x', -expand=>1);
244 $fr2 -> Label(-text=>'Emin:',
245 -foreground=>$config{colors}{activehighlightcolor},
246 )
247 -> pack(-side=>'left');
248 $widget{deg_emin} = $fr2 -> Entry(-textvariable=>\$degl_params{deg_emin},
249 -validate=>'key',
250 -validatecommand=>[\&set_variable, 'deg_emin'],
251 -width=>9)
252 -> pack(-side=>'left');
253 $grab{deg_emin} = $fr2 -> Button(@pluck_button, @pluck,
254 -command=>sub{Echo("\"$groups{$current}->{label}\" is frozen"), return
255 if $groups{$current}->{frozen};
256 &pluck('deg_emin');
257 $groups{$degl_params{standard}} -> plotE('emg',$dmode,\%plot_features, \@indicator);
258 $last_plot='e';
259 })
260 -> pack(-side=>'left');
261 $fr2 -> Label(-width=>1) -> pack(-side=>'left', -fill=>'x', -expand=>1);
262 $fr2 -> Label(-text=>'Emax:',
263 -foreground=>$config{colors}{activehighlightcolor},
264 )
265 -> pack(-side=>'left');
266 $widget{deg_emax} = $fr2 -> Entry(-textvariable=>\$degl_params{deg_emax},
267 -validate=>'key',
268 -validatecommand=>[\&set_variable, 'deg_emax'],
269 -width=>9)
270 -> pack(-side=>'left');
271 $grab{deg_emax} = $fr2 -> Button(@pluck_button, @pluck,
272 -command=>sub{Echo("\"$groups{$current}->{label}\" is frozen"), return
273 if $groups{$current}->{frozen};
274 &pluck('deg_emax');
275 $groups{$degl_params{standard}} -> plotE('emg',$dmode,\%plot_features, \@indicator);
276 $last_plot='e';
277 })
278 -> pack(-side=>'left');
279 #$fr2 -> Label(-width=>1) -> pack(-side=>'left', -fill=>'x', -expand=>1);
280
281 $fr2 = $frame -> Frame(-borderwidth=>2, -relief=>'flat')
282 -> pack(-expand=>1, -fill=>'x');
283 $widget{deg_replot} =
284 $fr2 -> Button(-text=>'Replot', @button_list,
285 -width=>1,
286 -command=> sub{set_deglitch_params(\%degl_params);
287 $groups{$degl_params{standard}} -> plotE('emg',$dmode,\%plot_features, \@indicator);
288 $last_plot = 'e'; $top -> update;})
289 -> pack(-side=>'left', -expand=>1, -fill=>'x');
290 $widget{deg_remove} =
291 $fr2 -> Button(-text=>'Remove glitches', @button_list,
292 -width=>1,
293 -command=>sub{Echo("Deglitching aborted: \"$groups{$current}->{label}\" is frozen"), return
294 if $groups{$current}->{frozen};
295 &remove_glitches($degl_params{standard});
296 $top -> update;
297 })
298 -> pack(-side=>'left', -expand=>1, -fill=>'x');
299 $fr2 -> Button(-text=>"Help", @button_list,
300 -width=>1,
301 -command=>[\&Echo, "Remove all points which fall outside the tolerance margins defined by the parameters below"])
302 -> pack(-side=>'left', -expand=>1, -fill=>'x');
303
304 ## help button
305 $degl -> Button(-text=>'Document section: deglitching data', @button_list,
306 -command=>sub{pod_display("process::deg.pod")})
307 -> pack(-fill=>'x', -pady=>4);
308
309 $degl -> Button(-text=>'Return to the main window', @button_list,
310 -background=>$config{colors}{background2},
311 -activebackground=>$config{colors}{activebackground2},
312 -command=>sub{&reset_window($degl, "deglitching", \@save)})
313 -> pack(-fill=>'x');
314 $degl -> Frame(-background=>$config{colors}{darkbackground})
315 -> pack(-expand=>1, -fill=>'both');
316
317 ($groups{$degl_params{standard}}->{update_bkg}) and $groups{$degl_params{standard}}->dispatch_bkg($dmode);
318 $groups{$degl_params{standard}} -> plotE('emg',$dmode,\%plot_features, \@indicator);
319 $last_plot = 'e';
320 $plotsel -> raise('e');
321 $top -> update;
322 };
323
324
325
326 sub set_deglitch_params {
327 my $hash_pointer = $_[0];
328 my $standard = $$hash_pointer{standard};
329 ## set deg_emin
330 #my $emin = $widget{deg_emin} -> get;
331 $groups{$standard} -> make(deg_emin=>$$hash_pointer{deg_emin} ||
332 $groups{$standard}->{bkg_nor1} + $groups{$standard}->{bkg_e0});
333 ## set deg_emax
334 #my $emax = $widget{deg_emax} -> get;
335 $groups{$standard}->dispose("set ___x = ceil($standard.energy)\n", 1);
336 my $maxE = Ifeffit::get_scalar("___x") * 1.1;
337 $groups{$standard} -> make(deg_emax=>$$hash_pointer{deg_emax} || $maxE);
338 ## set deg_tol
339 #my $tol = $widget{deg_tol} -> get;
340 $groups{$standard} -> make(deg_tol=>$$hash_pointer{deg_tol} ||
341 sprintf("%.4f", $groups{$standard}->{bkg_step} * $config{deglitch}{margin}));
342 };
343
344
345 sub remove_glitches {
346 my $group = $_[0];
347 Error("Deglitching aborted: " . $groups{$group}->{label} . " is not an xmu group."),
348 return unless ($groups{$current}->{is_xmu});
349 my $noplot = $_[1];
350 my @e = Ifeffit::get_array("$group.energy");
351 my @ee = Ifeffit::get_array("$group.energy");
352 my @x = Ifeffit::get_array("$group.xmu");
353 my $tol = $groups{$group}->{deg_tol};
354 my $emin = $groups{$group}->{deg_emin};
355 my $emax = $groups{$group}->{deg_emax};
356 my @p;
357 if ($emin > $groups{$group}->{bkg_e0}) {
358 @p = Ifeffit::get_array("$group.postline");
359 } else {
360 @p = Ifeffit::get_array("$group.preline");
361 };
362 my ($i, $j) = (-1, 0);
363 foreach (@ee) {
364 ++$i;
365 next if ($_ < $emin);
366 next if ($_ > $emax);
367 my ($up, $dn) = ($p[$i] + $tol, $p[$i] - $tol); ## bug here
368 next if (($x[$i-$j] > $dn) and ($x[$i-$j] < $up));
369 splice(@e, $i-$j, 1);
370 splice(@x, $i-$j, 1);
371 ++$j;
372 };
373 Ifeffit::put_array("$group.energy", \@e);
374 Ifeffit::put_array("$group.xmu", \@x);
375 $groups{$group}->make(update_bkg=>1);
376 unless ($noplot) {
377 ($groups{$group}->plotE('emtg',$dmode,\%plot_features, \@indicator));
378 $last_plot='e';
379 &refresh_properties;
380 };
381 project_state(0);
382 };
383
384
385 sub plot_chie {
386 my $group = $_[0];
387 my $how = $_[1];
388 $groups{$group}->dispatch_bkg if $groups{$group}->{update_bkg};
389 my $e0 = $groups{$group}->{bkg_e0};
390 my $label = $groups{$group}->{label};
391 my $emin = $e0 + $plot_features{emin};
392 my $emax = $e0 + $plot_features{emax};
393 my $style = ($how) ? 'linespoints3' : 'lines';
394 my $command .= "set $group.chie = ($group.xmu-$group.bkg)*($group.energy-$e0)\n";
395 $command .= "newplot($group.energy, $group.chie,\n ";
396 $command .= "xmin=$emin, xmax=$emax, xlabel=\"E (eV)\",\n ";
397 $command .= "title=\"$label\",\n ";
398 $command .= "ylabel=\"\\gx(E)\", fg=$config{plot}{fg}, bg=$config{plot}{bg},\n ";
399 $command .= "style=$style, color=\"$config{plot}{c0}\", key=\"\\gm\")";
400 $groups{$group} -> dispose($command, $dmode);
401
402 if ($indicator[0]) {
403 foreach my $i (@indicator) {
404 next if ($i =~ /^[01]$/);
405 next if (lc($i->[1]) =~ /[r\s]/);
406 my $suff = "chie";
407 my $val = $i->[2];
408 ($val = $groups{$group}->k2e($val)+$groups{$group}->{bkg_e0})
409 if (lc($i->[1]) =~ /[kq]/);
410 next if ($val < 0);
411 my $cmd = "set(___x = ceil($group.$suff+$groups{$group}->{plot_yoffset}),";
412 $cmd .= " ___n = floor($group.$suff+$groups{$group}->{plot_yoffset}))";
413 ifeffit($cmd);
414 my $ymax = Ifeffit::get_scalar("___x") * 1.05;
415 my $ymin = Ifeffit::get_scalar("___n");
416 $groups{$group}->plot_vertical_line($val, $ymin, $ymax, $dmode, "", 0, 0, 1)
417 };
418 };
419
420
421 };
422
423
424
425 ## END OF DATA DEGLITCHING SUBSECTION
426 ##########################################################################################