Debian Codegraph @

athena_parts/group_ops.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 ## groups operations and accounting and with displaying a newly
7 ## selected group
8
9
10 ## merge all marked groups by simple averaging, take care to do the
11 ## "right" thing for different spaces
12 sub merge_groups {
13 my $space = lc($_[0]);
14 my $doing_refs = $_[1] || 0;
15 my $m = 0;
16 my $curr = $current;
17 my $all_ref = 1;
18 map {$m += $_} values %marked;
19 Error("Merging aborted. There are no marked groups."), return 1 unless ($m);
20 Error("Merging aborted. There is just 1 marked group."), return 1 if ($m==1);
21 my $sp;
22 SPACE: {
23 ($sp = "energy"), last SPACE if ($space eq 'e');
24 ($sp = "energy (normalized)"), last SPACE if ($space eq 'n');
25 ($sp = "k-space"), last SPACE if ($space eq 'k');
26 ($sp = "R-space"), last SPACE if ($space eq 'r');
27 ($sp = "q-space"), last SPACE if ($space eq 'q');
28 };
29 Echo("Merging marked groups in $sp");
30 $top -> Busy(-recurse=>1,);
31 my ($group, $label) = group_name("merge");
32 ($label = " Ref ".$groups{$doing_refs}->{label}) if $doing_refs;
33 $groups{$group} = Ifeffit::Group -> new(group=>$group, label=>$label);
34 my ($file, $first, $is_detector, $is_xanes) =
35 $groups{$group}->merge($space, $config{merge}{merge_weight}, $dmode, \%groups, \%marked, \%plot_features, $list);
36 $groups{$group} -> make(file=>$file, is_xanes=>$is_xanes);
37 # make parameters same as the first in the merge list
38 $groups{$group} -> set_to_another($groups{$first});
39 $groups{$group} -> make(bkg_z => $groups{$first}->{bkg_z},
40 fft_edge => $groups{$first}->{fft_edge});
41 # because a merged group is a merge of data that has been e0
42 # shifted, the merge must NOT be e0 shifted.
43 $groups{$group} -> {bkg_eshift} = 0;
44 SWITCH: {
45 ($space eq 'e') and do {
46 $groups{$group} -> make(is_xmu=>1, is_chi=>0, is_rsp=>0, is_qsp=>0, update_bkg=>1);
47 $groups{$group} -> make(is_xmu=>0, not_data=>1) if $is_detector;
48 last SWITCH;
49 };
50 ($space eq 'n') and do {
51 $groups{$group} -> make(is_xmu=>1, is_chi=>0, is_rsp=>0, is_qsp=>0, is_nor=>1,
52 update_bkg=>1, bkg_slope=>0, bkg_int=>0, bkg_step=>1,
53 bkg_fixstep=>1, bkg_fitted_step=>1);
54 $groups{$group} -> make(is_xmu=>0, is_nor=>0, not_data=>1) if $is_detector;
55 last SWITCH;
56 };
57 ($space eq 'k') and do {
58 $groups{$group} -> dispose("set(___x = ceil($group.k))\n", 1);
59 my $maxk = Ifeffit::get_scalar("___x");
60 $groups{$group} -> make(is_xmu=>0, is_chi=>1, is_rsp=>0, is_qsp=>0, update_fft=>1,
61 update_bkg=>0, fft_kmax=>sprintf("%.2f", $maxk));
62 last SWITCH;
63 };
64 ($space eq 'r') and do {
65 $groups{$group} -> make(is_xmu=>0, is_chi=>0, is_rsp=>1, is_qsp=>0,
66 update_bft=>1, update_fft=>0, update_bkg=>0);
67 last SWITCH;
68 };
69 ($space eq 'q') and do {
70 $groups{$group} -> make(is_xmu=>0, is_chi=>0, is_rsp=>0, is_qsp=>1,
71 update_bft=>0, update_fft=>0, update_bkg=>0);
72 last SWITCH;
73 };
74 };
75 ++$line_count;
76 $groups{$group} -> make(line=>$line_count);
77 if ($sp eq 'energy') {
78 $groups{$group} -> make(bkg_spl1=>$groups{$curr}->{bkg_spl1},
79 bkg_spl2=>$groups{$curr}->{bkg_spl2});
80 # do not reset e0!!!
81 };
82 fill_skinny($list, $group, 1);
83
84 ## merge reference spectra if all groups in the merge have references
85 if ($space =~ m{[en]}) {
86 if (not $doing_refs) {
87 foreach my $m (keys %marked) {
88 next if not $marked{$m};
89 my $has_ref = $groups{$m}->{reference};
90 $all_ref = ($all_ref and $has_ref);
91 #print $groups{$m}->{label}, " $all_ref $has_ref\n";
92 };
93 if ($all_ref) {
94 my $ref_group = merge_refs($space, $group);
95 $groups{$group}->{reference} = $ref_group;
96 $groups{$ref_group}->{reference} = $group;
97 };
98 set_properties(1, $group, 0);
99 };
100 };
101 return $group if $doing_refs;
102
103 $marked{$group} = 1;
104 ($space = "d") if $is_detector;
105 plot_merge($group, $space);
106
107 Echo("Merging marked groups in $sp ... done!");
108 my $memory_ok = $groups{$group} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
109 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
110 $top->Unbusy;
111 return $group;
112 };
113
114
115 sub merge_refs {
116 my ($space, $gp) = @_;
117 my @list = ();
118 ## set the marks to the reference channels
119 foreach my $m (keys %marked) {
120 push @list, $groups{$m}->{reference} if $marked{$m};;
121 $marked{$m} = 0;
122 };
123 map { $marked{$_}=1 } @list;
124
125 my $ref_group = merge_groups($space, $gp);
126
127 ## set the marks back to the data
128 @list = ();
129 foreach my $m (keys %marked) {
130 push @list, $groups{$m}->{reference} if $marked{$m};;
131 $marked{$m} = 0;
132 };
133 map { $marked{$_}=1 } @list;
134 $top->update;
135 return $ref_group;
136 };
137
138
139
140 ## generate a copy of the current group with a unique name
141 sub copy_group {
142 Echo('No data!'), return unless ($current);
143 Echo("Cannot copy defaults."), return if ($current eq "Default Parameters");
144 my ($group, $label) = group_name("Copy of " . $groups{$current}->{label});
145 $groups{$group} = Ifeffit::Group -> new(group=>$group, label=>$label);
146 $groups{$group} -> set_to_another($groups{$current});
147 $groups{$group} -> make(is_xmu => $groups{$current}->{is_xmu},
148 is_chi => $groups{$current}->{is_chi},
149 is_rsp => $groups{$current}->{is_rsp},
150 is_qsp => $groups{$current}->{is_qsp},
151 is_nor => $groups{$current}->{is_nor},
152 is_bkg => $groups{$current}->{is_bkg},
153 is_diff => $groups{$current}->{is_diff},
154 is_pixel => $groups{$current}->{is_pixel},
155 is_xanes => $groups{$current}->{is_xanes},
156 not_data => $groups{$current}->{not_data},
157 );
158 $groups{$group} -> make(file=>$groups{$current}->{file}, update_bkg=>1);
159 my $mark = $marked{$current};
160 my $original = $current;
161 SWITCH: { # handle each space appropriately
162 (($groups{$group}->{is_xmu}) or ($groups{$group}->{is_chi})) and do {
163 my $xsuff = ($groups{$group}->{is_xmu}) ? ".energy" : ".k";
164 my $ysuff = ($groups{$group}->{is_xmu}) ? ".xmu" : ".chi";
165 my @x = Ifeffit::get_array($current.$xsuff);
166 my @y = Ifeffit::get_array($current.$ysuff);
167 Ifeffit::put_array($group.$xsuff, \@x);
168 Ifeffit::put_array($group.$ysuff, \@y);
169 if ($groups{$current}->{i0}) {
170 $groups{$group} -> make(i0 => "$group.i0");
171 my @i0 = Ifeffit::get_array($current.$ysuff);
172 Ifeffit::put_array("$group.i0", \@i0);
173 };
174 last SWITCH;
175 };
176 ($groups{$group}->{not_data}) and do {
177 my $xsuff = ".energy";
178 my $ysuff = ".det";
179 my @x = Ifeffit::get_array($current.$xsuff);
180 my @y = Ifeffit::get_array($current.$ysuff);
181 Ifeffit::put_array($group.$xsuff, \@x);
182 Ifeffit::put_array($group.$ysuff, \@y);
183 last SWITCH;
184 };
185 (($groups{$group}->{is_rsp}) or ($groups{$group}->{is_qsp})) and do {
186 my $x = ($groups{$group}->{is_rsp}) ? 'r' : 'q';
187 my @x = Ifeffit::get_array($current.".$x");
188 Ifeffit::put_array($group.".$x", \@x);
189 foreach (qw(mag pha re im)) { # copy all four arrays
190 my $suff = join("", ".chi", $x, "_", $_);
191 my @y = Ifeffit::get_array($current.$suff);
192 Ifeffit::put_array($group.$suff, \@y);
193 };
194 last SWITCH;
195 };
196 };
197 ++$line_count;
198 $groups{$group} -> make(line=>$line_count);
199 fill_skinny($list, $group, 1, 1);
200 $marked{$group} = $mark;
201 my $memory_ok = $groups{$group} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
202 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
203 return $group;
204 };
205
206
207 ## This makes detector groups out of the numerator and denominator of
208 ## an xmu group
209 sub make_detectors {
210 my $group = $groups{$current}->{group};
211 my $label = $groups{$current}->{label};
212 my $e0 = $groups{$current}->{bkg_e0};
213 my $esh = $groups{$current}->{bkg_eshift};
214 my $numerator = $groups{$current}->{numerator};
215 my $denominator = $groups{$current}->{denominator};
216 my @dets = ();
217
218 if ($numerator !~ /^\s*$/) {
219 my ($num_group, $num_label) = group_name("num $label");
220 $groups{$num_group} = Ifeffit::Group -> new(group=>$num_group, label=>$num_label);
221 $groups{$num_group} -> make(is_xmu => 0, is_chi => 0, is_rsp => 0, is_qsp => 0,
222 not_data => 1, bkg_e0 => $e0, bkg_eshift => $esh,
223 file => "Numerator of $label");
224 my $sets = "set($num_group.energy = $group.energy,\n";
225 $sets .= " $num_group.det = $numerator,\n";
226 $sets .= " ___ceil = ceil($num_group.det))";
227 $groups{$num_group} -> dispose($sets, $dmode);
228 my $scale = sprintf("%8.2e", 1/Ifeffit::get_scalar("___ceil"));
229 ++$line_count;
230 set_defaults($num_group, 'e', 0);
231 $groups{$num_group} -> make(line=>$line_count, plot_scale=>sprintf("%f",$scale));
232 fill_skinny($list, $num_group, 1);
233 push @dets, $num_group;
234 };
235 if ($denominator !~ /^\s*$/) {
236 my ($den_group, $den_label) = group_name("den $label");
237 $groups{$den_group} = Ifeffit::Group -> new(group=>$den_group, label=>$den_label);
238 $groups{$den_group} -> make(is_xmu => 0, is_chi => 0, is_rsp => 0, is_qsp => 0,
239 not_data => 1, bkg_e0 => $e0, bkg_eshift => $esh,
240 file => "Denominator of $label");
241 my $sets = "set($den_group.energy = $group.energy,\n";
242 $sets .= " $den_group.det = $denominator,\n";
243 $sets .= " ___ceil = ceil($den_group.det))";
244 $groups{$den_group} -> dispose($sets, $dmode);
245 my $scale = sprintf("%8.2e", 1/Ifeffit::get_scalar("___ceil"));
246 ++$line_count;
247 set_defaults($den_group, 'e', 0);
248 $groups{$den_group} -> make(line=>$line_count, plot_scale=>sprintf("%f",$scale));
249 fill_skinny($list, $den_group, 1);
250 push @dets, $den_group;
251 };
252
253 set_properties(1, $group, 0);
254 $groups{$current}->make(detectors=>\@dets);
255 my $memory_ok = $groups{$current}->memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
256 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
257 };
258
259
260 ## turn the background function for a group into it's own group so it
261 ## can be displayed independently of its data.
262 sub make_background {
263 my $group = $groups{$current}->{group};
264 my $e0 = $groups{$current}->{bkg_e0};
265 my $esh = $groups{$current}->{bkg_eshift};
266 my $step = $groups{$current}->{bkg_step};
267 ($groups{$current}->{update_bkg}) and $groups{$current}->dispatch_bkg($dmode);
268 my ($bkg, $label) = group_name($groups{$current}->{label}." bkg");
269 $groups{$bkg} = Ifeffit::Group -> new(group=>$bkg, label=>$label);
270 $groups{$bkg} -> set_to_another($groups{$group});
271 $groups{$bkg} -> make(is_xmu => 1, is_chi => 0, is_rsp => 0, is_qsp => 0, is_bkg => 1,
272 not_data => 0,
273 file=>"Background from ".$groups{$current}->{label});
274 #, bkg_e0 => $e0, bkg_eshift => $esh, bkg_step => $step);
275 my $cmd = "set($bkg.energy = $group.energy,\n";
276 if ($groups{$group}->{bkg_cl}) {
277 $cmd .= " $bkg.xmu = $group.f2)";
278 } else {
279 $cmd .= " $bkg.xmu = $group.bkg)";
280 };
281 $groups{$bkg} -> dispose($cmd, $dmode);
282 ++$line_count;
283 fill_skinny($list, $bkg, 1);
284 $groups{$bkg} -> plotE('em',$dmode,\%plot_features, \@indicator);
285 my $memory_ok = $groups{$bkg} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
286 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
287 };
288
289
290 ## read a string from an entry box that temporarily replaces the echo
291 ## area. $label is the descriptive label to be written before the
292 ## entry box. $r_string is a ref to the string being prompted for.
293 ## $r_arrow_buffer is a ref to a array containing a buffer of
294 ## responses accessed via the up and down arrows
295 sub get_string {
296 my ($mode, $label, $r_string, $r_arrow_buffer) = @_;
297 $top -> packPropagate(0);
298 $echo -> packForget;
299 my $prior = $top -> focusCurrent;
300 my $ren = $ebar -> Frame()
301 -> pack(-side=>'left', -expand=>1, -fill=>'x', -ipadx=>3);
302 $ren -> Label(-text=>$label,
303 -foreground=>$config{colors}{activehighlightcolor})
304 -> pack(-side=>'left');
305 my $entry = $ren -> Entry(-justify=>'center', -background=>$config{colors}{current},
306 -textvariable=>$r_string)
307 -> pack(-side=>'left', -expand=>1, -fill=>'x', -padx=>10);
308 if ($r_arrow_buffer) {
309 my $pointer = $#{$r_arrow_buffer} + 1;
310 $entry->bind("<KeyPress-Up>", # previous command in history
311 sub{ --$pointer; ($pointer=0) if ($pointer<0);
312 $entry->delete(0,'end');
313 $entry->insert(0, $$r_arrow_buffer[$pointer]); });
314 $entry->bind("<KeyPress-Down>", # next command in history
315 sub{ ++$pointer; ($pointer= $#{$r_arrow_buffer}) if
316 ($pointer>$#{$r_arrow_buffer});
317 $entry->delete(0,'end');
318 $entry->insert(0, $$r_arrow_buffer[$pointer]); });
319 };
320 my $pad = 0;
321 $entry -> bind("<KeyPress-Return>", sub{&restore_echo($ren, $mode, $entry, $prior)});
322 $ren -> Button(-text=>'OK', @button_list, -font=>$config{fonts}{small}, -borderwidth=>1,
323 -command=>[\&restore_echo, $ren, $mode, $entry, $prior])
324 -> pack(-side=>'left', -expand=>1, -fill=>'x');
325 foreach ($ren, $entry) {
326 my $this = $_;
327 $this -> bindtags([($this->bindtags)[1,0,2,3]]);
328 map {$this -> bind("<Control-$_>" => sub{$this->break;})}
329 qw(a b h i f j k l m o p r s t u w y
330 period slash minus equal semicolon
331 Key-1 Key-2 Key-3 Key-4 Key-5 Key-6);
332 map {$this -> bind("<Alt-$_>" => sub{$this->break;});
333 $this -> bind("<Meta-$_>" => sub{$this->break;});}
334 qw(b B j k o d semicolon);
335 };
336
337 $entry -> selectionRange(qw(0 end));
338 $entry -> icursor('end');
339 $top -> update;
340 $ren -> grab();
341 $entry -> focus;
342 return $ren;
343 };
344
345
346 ## destroy the get_string dialog and return the echo area
347 sub restore_echo {
348 my ($ren, $mode, $entry, $prior) = @_;
349 $ren -> grabRelease;
350 $ren -> packForget;
351 $ren -> destroy;
352 $ebar -> pack(-side=>"bottom", -fill=>'x');
353 $echo -> pack(-side=>'left', -expand=>1, -fill=>'x', -pady=>2);
354 if ($prior) { # and $prior->packInfo()) {
355 $prior -> focus;
356 } else {
357 $b_red{E} -> focus;
358 };
359 };
360
361
362
363 ## pop up a top level asking the user to provide a string as the new
364 ## name for a group. then call rename_group.
365 sub get_new_name {
366 Echo('No data!'), return unless ($current);
367 Echo("You cannot rename the defaults"),
368 return if ($current eq "Default Parameters");
369 my $newname = $groups{$current}->{label};
370 my $label = "New name for group \"" . $groups{$current}->{label} . "\": ";
371 my $dialog = get_string($dmode, $label, \$newname, \@rename_history);
372
373 my @list = &sorted_group_list;
374 my $l = $#list+1;
375 my $h = -1;
376 foreach (@list) {
377 ++$h;
378 last if ($_ eq $current);
379 };
380 ($h -= 2) if ($h > 1);
381 $list -> yview('moveto', $h/$l);
382
383 $dialog -> waitWindow; # the get_string dialog will be
384 # destroyed once the user hits ok,
385 # then we can move on...
386 rename_group($dmode, $newname);
387 };
388
389
390
391
392 ## rename a group -- actually just reset its label and display the new
393 ## label in the skinny panel
394 sub rename_group {
395 my ($mode, $newname, $which) = @_;
396 $which ||= $current;
397 Echo("Not renaming '$groups{$which}->{label}'", 0), return if ($newname =~ /^\s*$/);
398 Echo("Not renaming '$groups{$which}->{label}'", 0), return if ($newname eq $groups{$which}->{label});
399 my $oldname = $groups{$which}->{label};
400 Echo("Renaming \"$oldname\" to \"$newname\"");
401 push @rename_history, $newname;
402 my $quote_message = q{};
403 if ($newname =~ m{[\"\']}) {
404 $newname =~ s{[\"\']}{}g;
405 $quote_message = " (quote marks removed from group name)";
406 };
407 ## need to verify that this name is not already used
408 $groups{$which} -> MAKE(label=>$newname);
409 ## fix up the skinny panel entry
410 my $tag = $groups{$which}->{bindtag};
411 my @bold = (-fill => $config{colors}{activehighlightcolor},
412 #-font => $config{fonts}{large},
413 );
414 my @normal = (-fill => $config{colors}{foreground},
415 #-font => $config{fonts}{med},
416 );
417 my @rect_in = (-fill => $config{colors}{activebackground}, -outline=>$config{colors}{activebackground});
418 my @rect_out = (-fill => $config{colors}{background}, -outline=>$config{colors}{background});
419 $list -> bind($tag, '<1>' => [\&set_properties, $which, 0]);
420 $list -> bind($tag, '<3>' => [\&GroupsPopupMenu, $which, Ev('X'), Ev('Y')]);
421 ## change text size/color unless passing over the current group
422 $list -> bind($tag, '<Any-Enter>'=>sub{my $this = shift;
423 return if not exists($groups{$which}->{bindtag});
424 if ($this->itemcget('current', '-tags')->[0] ne $groups{$which}->{bindtag}) {
425 #$this->itemconfigure('current', @bold );
426 my $x = $this->find(below=>'current');
427 $this->itemconfigure($x, @rect_in,);
428 }
429 });
430 $list -> bind($tag, '<Any-Leave>'=>\&Leave);
431 $list -> itemconfigure($groups{$which}->{text}, -text=>$newname, -tag=>$tag);
432 $groups{$which}->{checkbutton} -> configure(-variable=>\$marked{$which});
433 $list -> coords($groups{$which}->{rect}, $list->bbox($groups{$which}->{text}));
434 ## deal with reference channel
435 my $ref = $groups{$which}->{reference};
436 if ($ref and
437 #not $groups{$which}->{is_ref} and
438 ($groups{$ref}->{label} =~ m{^ *Ref +$oldname$})) {
439 $groups{$ref} -> MAKE(label=>" Ref " . $newname);
440 my $tag = $groups{$ref}->{bindtag};
441 $list -> bind($tag, '<1>' => [\&set_properties, $ref, 0]);
442 $list -> bind($tag, '<3>' => [\&GroupsPopupMenu, $ref, Ev('X'), Ev('Y')]);
443 ## change text size/color unless passing over the reference group
444 $list -> bind($tag, '<Any-Enter>'=>sub{my $this = shift;
445 #print join(" ", $this->itemcget('current', '-tags')), $/;
446 #print ">>>", $groups{$ref}->{bindtag}, $/;
447 if ($this->itemcget('current', '-tags')->[0] ne $groups{$which}->{bindtag}) {
448 my $x = $this->find(below=>'current');
449 $this->itemconfigure($x, @rect_in )
450 };
451 });
452 $list -> bind($tag, '<Any-Leave>'=>\&Leave);
453 ##$list -> bind($tag, '<Any-Leave>'=>sub{shift->itemconfigure('current', @normal)});
454 $list -> itemconfigure($groups{$ref}->{text}, -text=>$groups{$ref}->{label}, -tag=>$tag);
455 $list -> coords($groups{$ref}->{rect}, $list->bbox($groups{$ref}->{text}));
456 };
457 set_properties(1, $which, 0);
458 project_state(0);
459 Echo("\"$oldname\" renamed to \"$newname\" $quote_message");
460 };
461
462
463 sub mark {
464 my $how = $_[0];
465 SWITCH: {
466 ($how eq 'all') and do {
467 map {$marked{$_} = 1} keys %marked;
468 last SWITCH;
469 };
470 ($how eq 'none') and do {
471 map {$marked{$_} = 0} keys %marked;
472 last SWITCH;
473 };
474 ($how eq 'toggle') and do {
475 map {$marked{$_} = !$marked{$_}} keys %marked;
476 last SWITCH;
477 };
478 ($how eq 'this') and do {
479 $marked{$current} = !$marked{$current};
480 last SWITCH;
481 };
482 ($how eq 'regex') and do {
483 mark_regex(1);
484 last SWITCH;
485 };
486 ($how eq 'unregex') and do {
487 mark_regex(0);
488 last SWITCH;
489 };
490 };
491 };
492
493 sub mark_regex {
494 my $mark = $_[0];
495 my $regex = "";
496 my $label = $mark ? "Mark" : "Unmark";
497 $label .= " all groups matching this perl regular expression: ";
498 my $dialog = get_string($dmode, $label, \$regex, \@regex_history);
499 $dialog -> waitWindow; # the get_string dialog will be
500 # destroyed once the user hits ok,
501 # then we can move on...
502 Echo("Aborting regex matching."), return if ($regex =~ /^\s*$/);
503 my $given = $regex;
504 my $re;
505 if ($config{general}{match_as} eq 'perl') {
506 my $is_ok = eval '$re = qr/$regex/i';
507 Error("Oops! \"$regex\" is not a valid regular expression"), return unless $is_ok;
508 } else {
509 $regex = glob_to_regex($given);
510 };
511 ##map {$marked{$_} = ($groups{$_}->{label} =~ /$regex/) ? 1 : 0} keys %marked;
512 foreach my $k (keys %marked) {
513 next unless ($groups{$k}->{label} =~ /$regex/);
514 $marked{$k} = $mark;
515 };
516 push @regex_history, $given;
517 my $what = $mark ? "Marked" : "Unmarked";
518 Echo("$what all groups matching /$given/");
519 };
520
521
522 ## reordering the groups list...
523 sub group_up { group_move(-1); };
524 sub group_down { group_move(1); };
525 sub group_move {
526 my $dir = $_[0];
527 my @keys = &sorted_group_list;
528 Error("There aren't any groups!"), return unless (@keys);
529 my $index = -1;
530 foreach (@keys) {
531 ++$index;
532 last if ($_ eq $current);
533 };
534 Echo("$groups{$current}->{label} is at the top of the list"), return if (($dir < 0) and ($index == 0));
535 Echo("$groups{$current}->{label} is at the bottom of the list"), return if (($dir > 0) and ($index == $#keys));
536
537 $index += $dir;
538 my $other = $keys[$index];
539 my $step = $config{list}{real_y}; # see $step in fill_skinny
540 ## swap $current with the one above/below
541 foreach ($current, $other) {
542 my $dist = sprintf("%fc", ($_ eq $current) ? $dir*$step : -1*$dir*$step);
543 my ($check, $text, $rect) = ($groups{$_}->{check}, $groups{$_}->{text},
544 $groups{$_}->{rect});
545 $list->move($check, 0, $dist);
546 $list->move($text, 0, $dist);
547 $list->move($rect, 0, $dist);
548 };
549
550 # finally adjust the view
551 my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0;
552 ($here < 0) and ($here = 0);
553 my $full = ($list->bbox(@skinny_list))[3] + 5;
554 $list -> yview('moveto', $here/$full);
555 project_state(0);
556 };
557
558
559 sub current_up { &current_move(-1); };
560 sub current_down { &current_move(1); };
561 sub current_move {
562 my $dir = $_[0];
563 my @keys = &sorted_group_list;
564 Error("There aren't any groups!"), return unless (@keys);
565 my $index = -1;
566 foreach (@keys) {
567 ++$index;
568 last if ($_ eq $current);
569 };
570 Echo("$groups{$current}->{label} is at the top of the list"), return if (($dir < 0) and ($index == 0));
571 Echo("$groups{$current}->{label} is at the bottom of the list"), return if (($dir > 0) and ($index == $#keys));
572 set_properties(1, $keys[$index+$dir], 0);
573 # finally adjust the view
574 my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0;
575 ($here < 0) and ($here = 0);
576 my $full = ($list->bbox(@skinny_list))[3] + 5;
577 $list -> yview('moveto', $here/$full);
578 ##project_state(0);
579 };
580
581
582 ## get rid of the current group both in the GUI and in ifeffit
583 sub delete_group {
584 #Error("You can only delete groups in the normal view."), return unless ($fat_showing eq 'normal');
585 my $debug = 0;
586 Echo('No data!'), return unless ($current);
587 my ($c, $mode, $which) = @_;
588 Echo("Cannot delete defaults."), return if ($current eq "Default Parameters");
589 my $to_delete = $which || $current;
590 my $group = $groups{$to_delete}->{group};
591 my $label = $groups{$to_delete}->{label};
592 print "Discarding $to_delete ($label)\n" if $debug;
593 my $message = "Group \"$label\" removed from project.";
594 Echonow("Removing group \"$label\" from project");
595 ## what about title lines
596
597 ## clear referent if this is a reference
598 if ($groups{$to_delete}->{reference}) {
599 if (exists($groups{$groups{$to_delete}->{reference}})) {
600 Echonow("Untying referent \"" . $groups{$groups{$to_delete}->{reference}}->{label} . "\"");
601 $groups{$groups{$to_delete}->{reference}}->MAKE(reference=>0);
602 };
603 };
604
605 my @keys = &sorted_group_list;
606 print "\tDisposing erase command to ifeffit\n" if $debug;
607 $groups{$to_delete} -> dispose("erase \@group $group\n", $mode);
608 print "\tRemoving widgets from groups list\n" if $debug;
609 $c->delete($groups{$to_delete}->{check});
610 $c->delete($groups{$to_delete}->{rect});
611 $c->delete($groups{$to_delete}->{text});
612 --$line_count;
613 print "\tDeleting groups and marked hash entries\n" if $debug;
614 delete $marked{$to_delete};
615 delete $groups{$to_delete};
616 $to_delete = "";
617 my $prev = "Default Parameters";
618 print "\tFinding location in groups list\n" if $debug;
619 while (@keys) {
620 my $this = shift @keys;
621 last if ($this eq $group);
622 $prev = $this;
623 };
624 print "\tSetting properties of newly selected group\n" if $debug;
625 my $step = '-' . $config{list}{real_y} . 'c'; # see $step in fill_skinny
626 print "\tMoving widgets of newly selected group\n" if $debug;
627 foreach (@keys) { # list only contains those below deleted group
628 my ($check, $text, $rect) = ($groups{$_}->{check}, $groups{$_}->{text},
629 $groups{$_}->{rect});
630 $c->move($check, 0, $step);
631 $c->move($text, 0, $step);
632 $c->move($rect, 0, $step);
633 };
634
635 my $h = ($list->bbox(@skinny_list))[3] || 0;
636 $h += 5;
637 ($h < 200) and ($h = 200);
638 $list -> configure(-scrollregion=>['0', '0', '150', $h]);
639 unless ($which) {
640 set_properties(1, "Default Parameters", 0), Echo($message), return "Default Parameters" unless $prev;
641 set_properties(1, $prev, 0), Echo($message), return $prev unless @keys;
642 set_properties(1, $keys[0], 0);
643 };
644
645 print "\tAlmost done\n" if $debug;
646 project_state(0);
647 Echonow("Group \"$label\" removed from project.");
648 print "\tDone!\n" if $debug;
649 return $prev;
650 };
651
652 ## delete every group in the skinny canvas if the third arg is false
653 ## delete only marked groups is the third arg is true
654 sub delete_many {
655 #Error("You can only delete groups in the normal view."), return unless ($fat_showing eq 'normal');
656 Echo('No data!'), return unless ($current);
657 my ($c, $mode, $m) = @_;
658 unless ($m) {
659 my $dialog =
660 $top -> Dialog(-bitmap => 'questhead',
661 -text => "Save this project before closing?.",
662 -title => 'Athena: Question...',
663 -buttons => ['Save', 'Just close it', 'Cancel'],
664 -default_button => 'Save');
665 my $response = $dialog->Show();
666 return if $response eq 'Cancel';
667 &save_project('all') if $response eq 'Save';
668 };
669 my $str = ($m) ? "Removing marked groups from project" : "Closing entire project";
670 if ((not $m) and ($groups{"Default Parameters"} -> vstr($ifeffit_version) > 1.02008)) {
671 $groups{"Default Parameters"} -> dispose("reset", $dmode);
672 $groups{"Default Parameters"} -> dispose("set \&screen_echo = 0", $dmode);
673 $groups{"Default Parameters"} -> dispose(&write_macros, $dmode);
674 $groups{"Default Parameters"} -> dispose("startup", $dmode);
675 #$ENV{PGPLOT_DIR} ||= '/usr/local/share/pgplot';
676 #$ENV{PGPLOT_DEV} ||= '/XSERVE';
677 #Ifeffit::Tools::reload("Ifeffit");
678 #ifeffit("\&screen_echo = 0\n");
679 };
680 Echo($str);
681 $top -> Busy(-recurse=>1,);
682 my @keys = &sorted_group_list;
683 my $show = "";
684 my $sync = Ifeffit::get_scalar('&sync_level');
685 ##$groups{"Default Parameters"} -> dispose('&sync_level = 0', $dmode);
686 my $save_groupreplot = $config{general}{groupreplot};
687 $config{general}{groupreplot} = 'none';
688 while (@keys) {
689 my $this = pop @keys;
690 next if ($m and not $marked{$this});
691 $show = delete_group($c, $mode, $this);
692 };
693 ##$groups{"Default Parameters"} -> dispose("\&sync_level = $sync\nsync()\n", $dmode);
694 @keys = &sorted_group_list;
695 $show = $keys[0] if (($show eq "Default Parameters") and @keys);
696
697 $config{general}{groupreplot} = $save_groupreplot;
698 ## there is a bug somewhere just prior to this and elements of
699 ## %group are being vivified but not blessed. this will remove
700 ## those naughty hash pairs
701 foreach my $g (keys %groups) {
702 delete($groups{$g}) unless ref($groups{$g}) =~ /Ifeffit/;
703 };
704 ($m) or $notes{journal} -> delete(qw(1.0 end));
705
706 my $h = ($list->bbox(@skinny_list))[3] || 0;
707 $h += 5;
708 ($h < 200) and ($h = 200);
709 $list -> configure(-scrollregion=>['0', '0', '150', $h]);
710
711 $str .= " ... done!";
712 ## unset this in deleting entire project
713 ($project_name = "") unless $m;
714 $plot_features{project} = $project_name;
715 project_state(0);
716 Echo($str);
717 $top->update;
718 ($m) ? set_properties(0, $show, 0) : set_properties(1, $show, 0);
719 $top->Unbusy;
720 };
721
722
723
724 sub kw_button {
725 foreach my $g (keys %marked) {
726 $groups{$g}->MAKE(update_fft=>1);
727 };
728 if ($last_plot =~ /k/) {
729 $last_plot_params->[3] = $plot_features{kw};
730 };
731 #$last_plot_params = [$current, 'marked', 'k', $str];
732 if ($last_plot eq 'kq') {
733 $b_red{kq}->invoke;
734 } elsif ($last_plot eq 'e') {
735 1; # do nothing
736 } else {
737 redo_plot();
738 };
739 section_indicators();
740 $plot_features{k_w} = $plot_features{kw};
741 };
742
743
744 sub refresh_titles {
745 my $self = $_[0];
746 return unless (ref $self =~ /Ifeffit/);
747 $self->{titles} = [];
748 my $text = $notes{titles} -> get(qw(1.0 end));
749 my @titles = ();
750 foreach (split(/\n/, $text)) {
751 next if ($_ =~ /^\s*$/);
752 ## walk through the title line counting open and closed parens,
753 ## skipping unmatched close parens
754 my $count = 0;
755 foreach my $i (0..length($_)) {
756 ++$count if (substr($_, $i, 1) eq '(');
757 --$count if ($count and (substr($_, $i, 1) eq ')'));
758 };
759 ## close all unmatched parens by appending close_parens to the string
760 $_ .= ')' x $count;
761
762 ## remove all parens
763 ## $_ =~ s/[\(\)]//g;
764
765 ## ! % and # in title lines seem to be a problem on Windows
766 $_ =~ s/[!\%\#]//g;
767
768 ## remove all unmatched open parens -- fragile!!
769 ## while (/\([^\)]*$/) {
770 ## $_ =~ s/\(//;
771 ## };
772
773 push @titles, $_;
774 };
775 $self -> MAKE(titles=>\@titles);
776 $self -> put_titles;
777 };
778
779 ## This updates the current group in ways that aren't handled by other
780 ## mechanisms
781 sub update_hook {
782 my $this = $_[0] || $current;
783
784 ## fixed step may not be up to date if the fixstep button was
785 ## pressed, then the step size was edited
786 $groups{$this} -> make(bkg_step => $widget{bkg_step}->cget('-value')) if $groups{$this}->{bkg_fixstep};
787 };
788
789
790 sub group_name {
791 #my $this = lc($_[0]);
792 my $this = $_[0];
793 $this =~ s/[^A-Za-z0-9_&. ]/_/g;
794
795 ## want to generate a unique label
796 my $label = $this;
797 my $found = 0;
798 foreach my $g (keys %groups) {
799 next unless exists $groups{$g};
800 next unless exists $groups{$g}->{label};
801 $found = 1 if ($label eq $groups{$g}->{label});
802 };
803 my $count = 2;
804 while ($found) {
805 $label = $this . " $count";
806 $found = 0;
807 foreach my $g (keys %groups) {
808 next unless exists $groups{$g};
809 next unless exists $groups{$g}->{label};
810 $found = 1 if ($label eq $groups{$g}->{label});
811 };
812 ++$count;
813 };
814
815 my $gp = four_character_random_string();
816 while (exists $groups{$gp}) { # make sure to get a unique one!
817 $gp = four_character_random_string();
818 };
819 $label =~ s{[\"\']}{}g;
820 $label =~ s{^unmacify_}{};
821 return ($gp, $label);
822 };
823
824
825 ## 4 char keyspace is almost 1/2 million large
826 ## 26^3 = 17576
827 ## 26^4 = 456976
828 ## 26^5 = 11881376
829 ## 26^6 = 308915776
830 sub four_character_random_string {
831 return chr(97 + int(rand(26))) . chr(97 + int(rand(26))) .
832 chr(97 + int(rand(26))) . chr(97 + int(rand(26)));
833 };
834
835
836
837 sub set_edge {
838 my $this = $_[0];
839 my $how = $_[1];
840 ($how = "fraction") if ($how eq "half");
841 my $message = "";
842 Echo("No data"), return unless ($this);
843 Echo("The group \"$groups{$this}->{label}\" is frozen."), return if ($groups{$this}->{frozen});
844 return unless $groups{$this}->{is_xmu};
845
846 SWITCH: {
847 # ifeffit's e0 (near peak of 1st derivative)
848 ($how eq 'edge') and do {
849 $groups{$this}->reset_e0($dmode);
850 $message = "E0 set to Ifeffit's default (near the peak of the 1st derivative) for \"$groups{$this}->{label}\"";
851 last SWITCH;
852 };
853
854 # zero-crossing nearest to ifeffit's e0
855 ($how eq 'zero') and do {
856 my %params = (e0 => $groups{$this}->{bkg_e0}, str =>'em2', zero_skip_plot => 1);
857 cal_zero($this, \%params);
858 $groups{$this}->make(bkg_e0=>$params{e0}, update_bkg=>1);
859 $message = "E0 set to zero-crossing of second derivative for \"$groups{$this}->{label}\"";
860 last SWITCH;
861 };
862
863 # energy of the half edge step
864 ($how eq 'fraction') and do {
865 $top->Busy;
866 $groups{$this}->e0_half_step($dmode, $config{bkg}{fraction});
867 $top->Unbusy;
868 $message = "E0 set to a fraction of the edge step for \"$groups{$this}->{label}\"";
869 last SWITCH;
870 };
871
872 # tabulated atomic value
873 ($how eq 'atomic') and do {
874 Echo("Cannot fetch atomic e0 values."), return unless $absorption_exists;
875 my ($z, $edge) = ($groups{$this}->{bkg_z}, $groups{$this}->{fft_edge});
876 my $en = Xray::Absorption->get_energy($z, $edge);
877 $groups{$this}->make(bkg_e0=>$en, update_bkg=>1);
878 $message = sprintf("E0 set to the atomic value for the %s edge of %s for \"$groups{$this}->{label}\"", $edge, $z);
879 last SWITCH;
880 };
881
882 };
883
884 project_state(0);
885 set_properties(1, $this, 0);
886 #autoreplot('e');
887 Echo($message)
888 };
889
890 sub set_edges {
891 my ($how, $which) = @_;
892 my $remember = $current;
893 my @list = &sorted_group_list;
894 foreach my $g (@list) {
895 next if (($which eq 'marked') and (not $marked{$g}));
896 if ($how eq "peak") {
897 set_edge_peak($g);
898 next;
899 };
900 set_edge($g, $how);
901 };
902 my $descr = "Ifeffit's default";
903 $descr = "zero-crossing of second derivative" if ($how eq 'zero');
904 $descr = "a fraction of the edge step" if ($how eq 'fraction');
905 $descr = "the atomic value" if ($how eq 'atomic');
906 set_properties(1, $remember, 0);
907 my $message = "Set E0 to $descr for $which groups.";
908 Echo($message);
909 };
910
911
912
913 sub set_edge_peak {
914 my ($group) = @_;
915 return 0 if not $groups{$group}->{is_xmu};
916 my $zpref = get_Z($config{bkg}{ledgepeak});
917 my $zthis = get_Z($groups{$group}->{bkg_z});
918 #Echo("Cannot set e0 to white line peak, the bkg->ledgepeak parameter is not set to an element symbol"),
919 # return 0 if (not $zpref);
920 Echo("Cannot set E0 to white line peak, \"$groups{$group}->{label}\" is frozen."),
921 return 0 if ($groups{$group}->{frozen});
922 #Echo("Cannot set E0 to white line peak, \"$groups{$group}->{label}\" is not an L2 or L3 edge."),
923 # return 0 if ($groups{$group}->{fft_edge} !~ /l[23]/i);
924 #Echo("Cannot set E0 to white line peak, \"$groups{$group}->{label}\" is below the cutoff Z ($config{bkg}{ledgepeak}) for this algorithm"),
925 # return 0 if ($zthis < $zpref);
926
927 $top->Busy;
928
929 set_edge($group, 'edge');
930 $groups{$group}->dispose("## computing derivative to set e0 to the white line peak\n", $dmode);
931 $groups{$group}->dispose("set $group.yd = deriv($group.xmu)/deriv($group.energy)\n", $dmode);
932 my @x = map {$_ + $groups{$group}->{bkg_eshift}} Ifeffit::get_array("$group.energy");
933 my @y = Ifeffit::get_array("$group.yd");
934 $groups{$group}->dispose("## arrays $group.energy and $group.yd were just slurped into Athena\n", $dmode);
935 $groups{$group}->dispose("erase $group.yd\n", $dmode);
936
937 my $e0index = 0;
938 foreach my $e (@x) {
939 last if ($e > $groups{$group}->{bkg_e0});
940 ++$e0index;
941 };
942 my ($enear, $ynear) = ($x[$e0index], $y[$e0index]);
943 my ($ratio, $i) = (1, 1);
944 my ($above, $below) = (0,0);
945 while (1) { # find points that bracket the zero crossing
946 (($above, $below) = (0,0)), last unless (exists($y[$e0index + $i]) and $y[$e0index]);
947 $ratio = $y[$e0index + $i] / $y[$e0index]; # this ratio is negative for a points bracketing the zero crossing
948 ($above, $below) = ($e0index+$i, $e0index+$i-1);
949 last if ($ratio < 0);
950 ++$i;
951 return 0 if ($i == 4000); # fail safe
952 };
953 my $wlpeak = sprintf("%.3f", $x[$below] - ($y[$below]/($y[$above]-$y[$below])) * ($x[$above] - $x[$below]));
954 $groups{$group}->make(bkg_e0=>$wlpeak);
955
956 project_state(0);
957 set_properties(1, $group, 0);
958 Echo("E0 set to the peak of the white line for \"$groups{$group}->{label}\"");
959 $top->Unbusy;
960 return 1;
961 };
962
963
964
965 #sub set_e0 {
966 # Echo("No data"), return unless ($current);
967 # $groups{$current}->make(bkg_e0=>$_[0], update_bkg=>1);
968 # &set_properties(0, $current);
969 # &Echo("E0 reset to the atomic value for the $_[2] edge of $_[1].")
970 #};
971
972
973 sub tie_reference {
974 Echo("No data!"), return unless $current;
975 my ($n, @g) = (0, ());
976 foreach my $k (keys %marked) {
977 if ($marked{$k}) {
978 ++$n;
979 push @g, $k;
980 };
981 };
982 Error("You need exactly two marked groups to tie data and reference channel"), return unless ($n == 2);
983 Echo("You cannot tie \"$groups{$g[0]}->{label}\". It's a frozen group."), return if $groups{$g[0]}->{frozen};
984 Echo("You cannot tie \"$groups{$g[1]}->{label}\". It's a frozen group."), return if $groups{$g[1]}->{frozen};
985 $groups{$g[0]} -> make(reference=>$g[1]);
986 $groups{$g[1]} -> make(reference=>$g[0]);
987 set_properties(1, $current, 0);
988 project_state(0);
989 };
990
991
992 ## refresh some display elements without calling set_properties
993 ## again. also perform any chores that need attention as normal course
994 ## of plotting. currently this is used to update the display of the
995 ## edge step after redoing a background removal and to perform a
996 ## memory check
997 sub refresh_properties {
998 if ($groups{$current}->{is_xmu}) {
999 $widget{bkg_step} -> configure(-validate=>'none', -state=>'normal');
1000 $widget{bkg_step} -> delete(qw/0 end/);
1001 $widget{bkg_step} -> insert(0, $groups{$current}->{bkg_step});
1002 $widget{bkg_step} -> configure(-validate=>'key', -state=>($groups{$current}->{frozen}) ? 'disabled' : 'normal');
1003 ## $widget{bkg_step} -> configure(-text=>sprintf "%.2f", $groups{$current}->{bkg_step});
1004 };
1005 my $memory_ok = $groups{$current}->memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
1006 Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1);
1007 };
1008
1009
1010 sub change_record {
1011 Echo("Change record type");
1012 my $d = $top->DialogBox(-title => "Artemis: change record type",
1013 -buttons => ["mu(E)", "xanes", "norm(E)", "detector", "Cancel"],
1014 -popover => 'cursor');
1015 my $how = $groups{$current}->{frozen} ? 'marked' : "this";
1016 $d -> add('Radiobutton',
1017 -variable => \$how,
1018 -value => "this",
1019 -text => "Change record type of \"$groups{$current}->{label}\" to:",
1020 -font => $config{fonts}{large},
1021 -foreground => $config{colors}{activehighlightcolor},)
1022 -> pack(-anchor=>'w');
1023 $d -> add('Radiobutton',
1024 -variable => \$how,
1025 -value => "marked",
1026 -text => "Change record type of marked groups to:",
1027 -font => $config{fonts}{large},
1028 -foreground => $config{colors}{activehighlightcolor},)
1029 -> pack(-anchor=>'w');
1030
1031 my $this = $d -> Show();
1032 my $add = "";
1033 Echo("Not changing record type"), return if ($this eq 'Cancel');
1034 if ($how eq 'this') {
1035 Echo("Not changing record type, \"$groups{$current}->{label}\" is frozen."), return if ($groups{$current}->{frozen});
1036 if ($this eq "mu(E)") {
1037 $groups{$current} -> make(is_xmu=>1, is_xanes=>0, is_nor=>0, not_data=>0, update_bkg=>1);
1038 } elsif ($this eq "xanes") {
1039 $groups{$current} -> make(is_xmu=>1, is_xanes=>1, is_nor=>0, not_data=>0, update_bkg=>1);
1040 } elsif ($this eq "norm(E)") {
1041 $groups{$current} -> make(is_xmu=>1, is_xanes=>0, is_nor=>1, not_data=>0, update_bkg=>1);
1042 } elsif ($this eq "detector") {
1043 $groups{$current} -> make(is_xmu=>0, is_xanes=>0, is_nor=>0, not_data=>1, update_bkg=>1);
1044 };
1045 if ($groups{$current}->{reference}) {
1046 my $ref = $groups{$current}->{reference};
1047 if ($this eq "mu(E)") {
1048 $groups{$ref} -> make(is_xmu=>1, is_xanes=>0, is_nor=>0, not_data=>0, update_bkg=>1);
1049 } elsif ($this eq "xanes") {
1050 $groups{$ref} -> make(is_xmu=>1, is_xanes=>1, is_nor=>0, not_data=>0, update_bkg=>1);
1051 } elsif ($this eq "norm(E)") {
1052 $groups{$ref} -> make(is_xmu=>1, is_xanes=>0, is_nor=>1, not_data=>0, update_bkg=>1);
1053 } elsif ($this eq "detector") {
1054 $groups{$ref} -> make(is_xmu=>0, is_xanes=>0, is_nor=>0, not_data=>1, update_bkg=>1);
1055 };
1056 $add = " and reference";
1057 };
1058 Echo("Changed \"$groups{$current}->{label}\"$add to $this");
1059 } elsif ($how eq 'marked') {
1060 foreach my $g (keys (%marked)) {
1061 next unless $marked{$g};
1062 next if ($groups{$g}->{frozen});
1063 if ($this eq "mu(E)") {
1064 $groups{$g} -> make(is_xmu=>1, is_xanes=>0, is_nor=>0, not_data=>0, update_bkg=>1);
1065 } elsif ($this eq "xanes") {
1066 $groups{$g} -> make(is_xmu=>1, is_xanes=>1, is_nor=>0, not_data=>0, update_bkg=>1);
1067 } elsif ($this eq "norm(E)") {
1068 $groups{$g} -> make(is_xmu=>1, is_xanes=>0, is_nor=>1, not_data=>0, update_bkg=>1);
1069 } elsif ($this eq "detector") {
1070 $groups{$g} -> make(is_xmu=>0, is_xanes=>0, is_nor=>0, not_data=>1, update_bkg=>1);
1071 };
1072 if ($groups{$g}->{reference}) {
1073 my $ref = $groups{$g}->{reference};
1074 if ($this eq "mu(E)") {
1075 $groups{$ref} -> make(is_xmu=>1, is_xanes=>0, is_nor=>0, not_data=>0, update_bkg=>1);
1076 } elsif ($this eq "xanes") {
1077 $groups{$ref} -> make(is_xmu=>1, is_xanes=>1, is_nor=>0, not_data=>0, update_bkg=>1);
1078 } elsif ($this eq "norm(E)") {
1079 $groups{$ref} -> make(is_xmu=>1, is_xanes=>0, is_nor=>1, not_data=>0, update_bkg=>1);
1080 } elsif ($this eq "detector") {
1081 $groups{$ref} -> make(is_xmu=>0, is_xanes=>0, is_nor=>0, not_data=>1, update_bkg=>1);
1082 };
1083 };
1084 };
1085 Echo("Changed marked groups to $this");
1086 };
1087 set_properties(0, $current, 0);
1088 project_state(0);
1089 };
1090
1091 sub freeze {
1092 my $how = $_[0];
1093 my $message;
1094 SWITCH: {
1095 ($how eq 'this') and do { # toggle this group
1096 my $verb = $groups{$current}->{frozen} ? 'Unfroze' : 'Froze';
1097 ($groups{$current}->{frozen}) ? $groups{$current}->unfreeze : $groups{$current}->freeze;
1098 freeze_chores($current);
1099 $message = "$verb \"$groups{$current}->{label}\"";
1100 last SWITCH;
1101 };
1102 ($how eq 'all') and do { # freeze all groups
1103 map {$groups{$_} -> freeze; freeze_chores($_)} (keys (%marked));
1104 $message = "Froze all groups";
1105 last SWITCH;
1106 };
1107 ($how eq 'none') and do { # unfreeze all groups
1108 map {$groups{$_} -> unfreeze; freeze_chores($_)} (keys (%marked));
1109 $message = "Unfroze all groups";
1110 last SWITCH;
1111 };
1112 ($how eq 'toggle') and do { # toggle all groups frozen-ness
1113 map {($groups{$_}->{frozen}) ? $groups{$_}->unfreeze : $groups{$_}->freeze; freeze_chores($_)} (keys (%marked));
1114 $message = "Toggled frozenness of all groups";
1115 last SWITCH;
1116 };
1117 ($how eq 'marked') and do { # freeze marked groups
1118 map {$groups{$_}->freeze if $marked{$_}; freeze_chores($_) } (keys (%marked));
1119 $message = "Froze marked groups";
1120 last SWITCH;
1121 };
1122 ($how eq 'unmarked') and do { # unfreeze marked groups
1123 map {$groups{$_}->unfreeze if $marked{$_}; freeze_chores($_) } (keys (%marked));
1124 $message = "Unfroze marked groups";
1125 last SWITCH;
1126 };
1127 ($how =~ 'regex') and do { # freeze/unfreeze groups with labels matching regex
1128 my $regex = "";
1129 my $what = ($how eq 'regex') ? 'Freeze' : 'Unfreeze';
1130 my $label = "$what all groups matching this perl regular expression: ";
1131 my $dialog = get_string($dmode, $label, \$regex, \@regex_history);
1132 $dialog -> waitWindow; # the get_string dialog will be
1133 # destroyed once the user hits ok,
1134 # then we can move on...
1135 Echo("Aborting regex matching."), return if ($regex =~ /^\s*$/);
1136 my $given = $regex;
1137 my $re;
1138 if ($config{general}{match_as} eq 'perl') {
1139 my $is_ok = eval '$re = qr/$regex/i';
1140 Error("Oops! \"$regex\" is not a valid regular expression"), return unless $is_ok;
1141 } else {
1142 $regex = glob_to_regex($given);
1143 };
1144 if ($how eq 'regex') {
1145 map { $groups{$_}->freeze if ($groups{$_}->{label} =~ /$regex/); freeze_chores($_)} keys %marked;
1146 } else {
1147 map { $groups{$_}->unfreeze if ($groups{$_}->{label} =~ /$regex/); freeze_chores($_)} keys %marked;
1148 };
1149 push @regex_history, $given;
1150 $what = ($how eq 'regex') ? 'Froze' : 'Unfroze';
1151 $message = "$what all groups matching /$given/";
1152 last SWITCH;
1153 };
1154 };
1155 set_properties(1, $current, 0);
1156 project_state(0);
1157 Echo($message);
1158 };
1159
1160 ## this is always called *after* the group is (un)frozen
1161 sub freeze_chores {
1162 my $which = $_[0];
1163 my @normal = (-fill => $config{colors}{foreground},
1164 -font => $config{fonts}{med});
1165 my @frozen = (-fill => $config{colors}{foreground},
1166 -font => $config{fonts}{medit});
1167 my $tag = $groups{$which}->{bindtag};
1168 ## toggle the font for a group list entry for freezing/unfreezing
1169 if ($groups{$which}->{frozen}) { # make the text the frozen color
1170 $list -> itemconfigure($groups{$which}->{text}, @frozen);
1171 $list -> bind($tag, '<Any-Leave>'=>sub{my $this = shift; $this->itemconfigure('current', @frozen); Leave($this)});
1172 } else { # make the text the normal color
1173 $list -> itemconfigure($groups{$which}->{text}, @normal);
1174 $list -> bind($tag, '<Any-Leave>'=>sub{my $this = shift; $this->itemconfigure('current', @normal); Leave($this)});
1175 };
1176 ## freeze/unfreeze the referent
1177 if ($groups{$which}->{reference}) {
1178 if ($groups{$which}->{frozen}) {
1179 $groups{$groups{$which}->{reference}} -> freeze;
1180 } else {
1181 $groups{$groups{$which}->{reference}} -> unfreeze;
1182 };
1183 };
1184 };
1185
1186 sub about_group {
1187 about_marked_groups({$current=>1});
1188 };
1189 sub about_marked_groups {
1190 my $r_marked = $_[0];
1191 Echo("No data!"), return unless $current;
1192 Echo("No data!"), return if ($current eq "Default Parameters");
1193 my $message = q{};
1194 my @list = sort {($list->bbox($groups{$a}->{text}))[1] <=>
1195 ($list->bbox($groups{$b}->{text}))[1]} (keys (%$r_marked));
1196 foreach my $k (@list) {
1197 next unless ($$r_marked{$k});
1198 $message .= "\n\n" . &identify_group($k);
1199 $message .= "\n\n" . &group_stats($k);
1200 $message .= "\n\n" . sprintf("epsilon_k=%.5f, epsilon_R=%.5f, recommended kmax=%.3f", $groups{$k}->chi_noise());
1201 $message .= "\n\n" . &show_mu_str unless $groups{$k}->{is_merge};
1202 $message .= "\n\n" . &nknots($k) if ($groups{$k}->{is_xmu} and not $groups{$k}->{is_xanes});
1203 $message .= "\n\n" . &groupIndex($k);
1204 $message .= "\n\n" . "This group is tied to \"" . $groups{$groups{$k}->{reference}}->{label} . "\"" if $groups{$k}->{reference};
1205 $message .= "\n\nThis groups is frozen.\n" if $groups{$k}->{frozen};
1206 $message .= "\n\n" . "-" x 60;
1207 };
1208 $message =~ s/^\n//;
1209 $message =~ s/-+$//;
1210 $message .= "\n\n";
1211 my $dialog =
1212 $top -> DialogBox(-title => 'Athena: About groups',
1213 -buttons => [qw/OK/],
1214 -default_button => 'OK');
1215 my $txt = $dialog -> Scrolled("ROText",
1216 -width=>60,
1217 -height=>12,
1218 -wrap=>'word',
1219 -scrollbars=>'oe',
1220 ) -> pack(-fill=>'y', -expand=>1);
1221 $txt -> Subwidget("yscrollbar")->configure(-background=>$config{colors}{background});
1222 $txt -> tagConfigure("text", -font=>$config{fonts}{fixedsm});
1223 $txt -> insert('end', $message, 'text');
1224 disable_mouse3($txt);
1225 my $response = $dialog->Show();
1226 };
1227
1228
1229
1230 sub identify_group {
1231 Echo("No data"), return unless ($current);
1232 my $this = $_[0] || $current;
1233 my $message;
1234 my $group = $groups{$this}->{group};
1235 my $label = "\"" . $groups{$this}->{label} . "\"";
1236 SWITCH: {
1237 my $what = "";
1238 ($groups{$this}->{is_merge}) and ($what = " merged");
1239 ($groups{$this}->{is_diff}) and ($what .= " difference");
1240 ($groups{$this}->{is_pixel}) and ($what .= " pixel");
1241
1242 ($groups{$this}->{not_data}) and do {
1243 $message = "$label is a detector or peak fit record. It can be plotted only in energy.";
1244 last SWITCH;
1245 };
1246 ($groups{$this}->{is_xanes}) and do {
1247 $message = "$label is a$what xanes record. It can be plotted only in energy.";
1248 last SWITCH;
1249 };
1250 ($groups{$this}->{is_nor}) and do {
1251 $message = "$label is a$what normalized mu(E) record. It can be plotted in any space.";
1252 last SWITCH;
1253 };
1254 ($groups{$this}->{is_bkg}) and do {
1255 $message = "$label is a$what background record. It can be plotted in any space.";
1256 last SWITCH;
1257 };
1258 ($groups{$this}->{is_xmu}) and do {
1259 $message = "$label is a$what mu(E) record. It can be plotted in any space.";
1260 last SWITCH;
1261 };
1262 ($groups{$this}->{is_chi}) and do {
1263 $message = "$label is a$what chi(k) record. It can be plotted in k-, R-, or q-space.";
1264 last SWITCH;
1265 };
1266 ($groups{$this}->{is_rsp}) and do {
1267 $message = "$label is a$what chi(R) record. It can be plotted in R- or q-space.";
1268 last SWITCH;
1269 };
1270 ($groups{$this}->{is_qsp}) and do {
1271 $message = "$label is a$what chi(q) record. It can be plotted only in q-space.";
1272 last SWITCH;
1273 };
1274 };
1275 ##Echo($message . " It's group name is \"" . $group . "\"");
1276 return $message . " It's group name is \"" . $group . "\"";
1277 };
1278
1279 sub group_stats {
1280 Echo("No data"), return unless ($current);
1281 my $this = $_[0] || $current;
1282 my $message;
1283 my $group = $groups{$this}->{group};
1284 SWITCH: {
1285 ($groups{$this}->{not_data}) and do {
1286 my @x = Ifeffit::get_array($group.".energy");
1287 $message = sprintf "This detector or peak fit record has %d points from %.3f to %.3f",
1288 $#x+1, $x[0], $x[$#x];
1289 last SWITCH;
1290 };
1291 ($groups{$this}->{is_xanes}) and do {
1292 my @x = Ifeffit::get_array($group.".energy");
1293 $message = sprintf "This xanes record has %d points from %.3f to %.3f",
1294 $#x+1, $x[0], $x[$#x];
1295 last SWITCH;
1296 };
1297 ($groups{$this}->{is_nor}) and do {
1298 my @x = Ifeffit::get_array($group.".energy");
1299 $message = sprintf "This normalized mu(E) record has %d points from %.3f to %.3f",
1300 $#x+1, $x[0], $x[$#x];
1301 last SWITCH;
1302 };
1303 ($groups{$this}->{is_bkg}) and do {
1304 my @x = Ifeffit::get_array($group.".energy");
1305 $message = sprintf "This background record has %d points from %.3f to %.3f",
1306 $#x+1, $x[0], $x[$#x];
1307 last SWITCH;
1308 };
1309 ($groups{$this}->{is_xmu}) and do {
1310 my @x = Ifeffit::get_array($group.".energy");
1311 $message = sprintf "This mu(E) record has %d points from %.3f to %.3f",
1312 $#x+1, $x[0], $x[$#x];
1313 last SWITCH;
1314 };
1315 ($groups{$this}->{is_chi}) and do {
1316 my @x = Ifeffit::get_array($group.".k");
1317 $message = sprintf "This chi(k) record has %d points from %.3f to %.3f",
1318 $#x+1, $x[0], $x[$#x];
1319 last SWITCH;
1320 };
1321 ($groups{$this}->{is_rsp}) and do {
1322 my @x = Ifeffit::get_array($group.".r");
1323 $message = sprintf "This chi(R) record has %d points from %.3f to %.3f",
1324 $#x+1, $x[0], $x[$#x];
1325 last SWITCH;
1326 };
1327 ($groups{$this}->{is_qsp}) and do {
1328 my @x = Ifeffit::get_array($group.".q");
1329 $message = sprintf "This chi(q) record has %d points from %.3f to %.3f",
1330 $#x+1, $x[0], $x[$#x];
1331 last SWITCH;
1332 };
1333 };
1334 ##Echo($message);
1335 return $message;
1336 };
1337
1338 sub show_mu_str {
1339 my $mu_str = $groups{$current}->{mu_str};
1340 $mu_str =~ s/$current\.//g;
1341 $mu_str =~ s/\(/( /g;
1342 $mu_str =~ s/\)/ )/g;
1343 ##Echo($groups{$current}->{label} . ": mu(E) was constructed from columns as: $mu_str");
1344 return "mu(E) was constructed from columns as: $mu_str";
1345 };
1346
1347
1348 sub nknots {
1349 my $this = $_[0] || $current;
1350 my $kmin = $groups{$this}->{bkg_spl1};
1351 my $kmax = $groups{$this}->{bkg_spl2};
1352 my $deltak = $kmax - $kmin;
1353 my $nidp = int( 2 * $deltak * $groups{$this}->{bkg_rbkg} / PI ) + 1;
1354 my $label = $groups{$this}->{label};
1355 return "\"$label\" uses $nidp spline knots (evenly spaced in k between $kmin and $kmax, inclusive)";
1356 ##Echo("\"$label\" uses $nidp spline knots (evenly spaced in k between $kmin and $kmax, inclusive)");
1357 };
1358
1359 sub groupIndex {
1360 my $i = 1;
1361 foreach my $g (&sorted_group_list) {
1362 return "\"$groups{$g}->{label}\" is item number $i in the groups list." if ($g eq $_[0]);
1363 ++$i;
1364 };
1365 }
1366
1367
1368 sub sorted_group_list {
1369 my @list = sort {($list->bbox($groups{$a}->{text}))[1] <=>
1370 ($list->bbox($groups{$b}->{text}))[1]} (keys (%marked));
1371 return @list;
1372 };
1373
1374 ## END OF GROUP OPERATIONS SUBSECTION
1375 ##########################################################################################