Debian Codegraph @

artemis_parts/feff.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2008 Bruce Ravel
3 ##
4 ## THE FEFF CALCULATION PAGE
5
6 sub make_feff {
7 my $parent = $_[0];
8
9 my $c = $parent -> Frame(-relief=>'flat',
10 -borderwidth=>0,
11 #@window_size,
12 -highlightcolor=>$config{colors}{background});
13
14 $fefftabs = $c -> NoteBook(-backpagecolor => $config{colors}{background},
15 -inactivebackground => $config{colors}{inactivebackground},
16 -font => $config{fonts}{med},)
17 -> pack(-fill => 'both', -expand=>1, -side => 'bottom');
18 foreach (qw/Atoms feff.inp Interpretation/) {
19 $feffcard{$_} = $fefftabs -> add($_, -label=>$_, -anchor=>'center',
20 -raisecmd=>[\&set_feff_showing, $_]
21 );
22 };
23
24
25 my @start = (-foreground=>$config{colors}{activehighlightcolor},
26 -font=>$config{fonts}{med});
27
28 my $t = ""; # used for clicky help
29 ## $header{current} = $c ->
30 ## Label(@title2, -text=>"FEFF Calculation",)
31 ## -> pack(-side=>'top', -anchor=>'w', -padx=>6);
32
33 ## atoms information
34 &make_atoms($feffcard{Atoms});
35
36
37 ## feff.inp file
38 ##(my $fn = $config{intrp}{unimported}) =~ s/ italic//;
39 my $fn = $config{fonts}{fixedsm};
40 $widgets{feff_inptext} = $feffcard{'feff.inp'} ->
41 Scrolled('TextUndoQuiet',
42 -scrollbars => 'se',
43 -wrap => 'none',
44 -font => $fn)
45 -> pack(-side=>'top', -expand=>1, -fill=>'both', -padx=>3, -pady=>3);
46 $widgets{feff_inptext} -> Subwidget("xscrollbar")
47 ->configure(-background=>$config{colors}{background},
48 ($is_windows) ? () : (-width=>8));
49 $widgets{feff_inptext} -> Subwidget("yscrollbar")
50 ->configure(-background=>$config{colors}{background},
51 ($is_windows) ? () : (-width=>8));
52 $widgets{feff_inptext} -> tagConfigure("feffinp", -font=>$fn);
53 $widgets{feff_inptext} -> tagConfigure("feffwarn", -font=>$fn, -foreground=>'red3');
54 BindMouseWheel($widgets{feff_inptext});
55
56 my $bfr = $feffcard{'feff.inp'} -> Frame ()
57 -> pack(-side=>'bottom', -fill=>'x');
58
59 $widgets{help_runfeff} =
60 $bfr -> Button(-text=>"Document: Feff and it's input file", @button2_list,
61 -command=>sub{pod_display("artemis_feffinp.pod")},
62 -width=>1)
63 -> pack(-side=>'right', -fill=>'x', -expand=>1, -padx=>2, -pady=>2);
64
65 $widgets{feff_run} = $bfr -> Button(-text=>'Run Feff', @button2_list,
66 -command => sub{&run_feff($current)},
67 -width=>1)
68 -> pack(-side=>'left', -fill=>'x', -expand=>1, -padx=>2, -pady=>2);
69
70
71
72 ## Interpretation of Feff calculation
73
74 ## $t = $feffcard{Interpretation} ->
75 ## Button(@start, -activeforeground=>$config{colors}{mbutton},
76 ## -text=>'Interpretation of the FEFF calculation',
77 ## -relief=>'flat', -borderwidth=>0,
78 ## -command=>[\&Echo, $click_help{'Interpretation of the FEFF calculation'}])
79 ## -> pack(-side=>'top', -anchor=>'c');
80
81 $widgets{feff_intrp_headerbox} = $feffcard{Interpretation} ->
82 LabFrame(-label=>'Interpretation of the FEFF Calculation',
83 -foreground=>$config{colors}{activehighlightcolor},
84 -labelside=>'acrosstop')
85 -> pack(-side=>'top', -anchor=>'c', -fill=>'x');
86 $widgets{feff_intrp_header} = $widgets{feff_intrp_headerbox} ->
87 Scrolled('ROText', -height=>5, -scrollbars=>'e',
88 -cursor => $mouse_over_cursor,)
89 -> pack(-side=>'top', -anchor=>'c', -fill=>'x');
90 $widgets{feff_intrp_header} -> Subwidget("yscrollbar")
91 -> configure(-background=>$config{colors}{background},
92 ($is_windows) ? () : (-width=>8));
93 disable_mouse3($widgets{feff_intrp_header}->Subwidget("rotext"));
94 $widgets{feff_intrp_header} -> bind('<ButtonPress-3>',\&intrp_header_menu);
95 BindMouseWheel($widgets{feff_intrp_header});
96
97
98
99 $widgets{feff_intrp} = $feffcard{Interpretation} ->
100 Scrolled('HList',
101 -columns => 6,
102 -header => 1,
103 -scrollbars => 'osoe',
104 -selectmode => 'extended',
105 -selectbackground => $config{colors}{selected},
106 -font => $config{fonts}{fixed},
107 -command =>
108 sub{
109 my $which = $widgets{feff_intrp}->selectionGet();
110 my $this;
111 my $nnnn = sprintf("feff%4.4d.dat", $widgets{feff_intrp}->itemCget($which, 0, '-text'));
112 foreach my $k (sort (keys %paths)) {
113 next unless (ref($paths{$k}) =~ /Ifeffit/);
114 next unless ($paths{$k}->type eq 'path');
115 next unless ($paths{$k}->get('parent') eq $current);
116 $this = $paths{$k}->get('id'), last
117 if ($paths{$k}->get('feff') eq $nnnn);
118 };
119 ($list->selectionIncludes($this)) ?
120 $list->selectionClear($this) : $list->selectionSet($this);
121 })
122 -> pack(-side=>'top', -expand=>1, -fill=>'both', -padx=>3, -pady=>3);
123
124 $widgets{feff_intrp} -> Subwidget("xscrollbar")
125 -> configure(-background=>$config{colors}{background},
126 ($is_windows) ? () : (-width=>8));
127 $widgets{feff_intrp} -> Subwidget("yscrollbar")
128 -> configure(-background=>$config{colors}{background},
129 ($is_windows) ? () : (-width=>8));
130 $widgets{feff_intrp} -> bind('<ButtonPress-3>',\&snarf_path);
131
132
133 $intrp_styles{header} = $widgets{feff_intrp} ->
134 ItemStyle('text', -font=>$config{fonts}{smbold}, -anchor=>'w',
135 -foreground=>$config{colors}{foreground});
136 foreach my $s (qw(normal ss focus)) {
137 my $color = ($s eq 'normal') ? $config{colors}{background} : $config{intrp}{$s};
138 my @list = ('text',
139 -font => $config{fonts}{fixed},
140 -background => $color,
141 #-cursor => $mouse_over_cursor,
142 -selectbackground => $color,
143 -activebackground => $color);
144 ## styles for included paths
145 $intrp_styles{$s} = $widgets{feff_intrp} ->
146 ItemStyle(@list, -anchor => 'center', -foreground => $config{colors}{foreground});
147 $intrp_styles{$s."_amp"} = $widgets{feff_intrp} ->
148 ItemStyle(@list, -anchor => 'e', -foreground => $config{colors}{foreground});
149 $intrp_styles{$s."_path"} = $widgets{feff_intrp} ->
150 ItemStyle(@list, -anchor => 'w', -foreground => $config{colors}{foreground});
151
152 ## styles for excluded paths
153 $intrp_styles{$s."_x"} = $widgets{feff_intrp} ->
154 ItemStyle(@list, -anchor => 'center', -foreground => $config{intrp}{excluded}, -selectforeground => $config{intrp}{excluded});
155 $intrp_styles{$s."_amp_x"} = $widgets{feff_intrp} ->
156 ItemStyle(@list, -anchor => 'e', -foreground => $config{intrp}{excluded}, -selectforeground => $config{intrp}{excluded});
157 $intrp_styles{$s."_path_x"} = $widgets{feff_intrp} ->
158 ItemStyle(@list, -anchor => 'w', -foreground => $config{intrp}{excluded}, -selectforeground => $config{intrp}{excluded});
159
160 ## styles for absent paths
161 $intrp_styles{$s."_a"} = $widgets{feff_intrp} ->
162 ItemStyle(@list, -anchor => 'center', -foreground => $config{intrp}{absent}, -font => $config{intrp}{unimported}, -selectforeground => $config{intrp}{absent});
163 $intrp_styles{$s."_amp_a"} = $widgets{feff_intrp} ->
164 ItemStyle(@list, -anchor => 'e', -foreground => $config{intrp}{absent}, -font => $config{intrp}{unimported}, -selectforeground => $config{intrp}{absent});
165 $intrp_styles{$s."_path_a"} = $widgets{feff_intrp} ->
166 ItemStyle(@list, -anchor => 'w', -foreground => $config{intrp}{absent}, -font => $config{intrp}{unimported}, -selectforeground => $config{intrp}{absent});
167
168 ## styles for unimported paths
169 $intrp_styles{$s."_u"} = $widgets{feff_intrp} ->
170 ItemStyle(@list, -anchor => 'center', -foreground => $config{colors}{foreground}, -font => $config{intrp}{unimported});
171 $intrp_styles{$s."_amp_u"} = $widgets{feff_intrp} ->
172 ItemStyle(@list, -anchor => 'e', -foreground => $config{colors}{foreground}, -font => $config{intrp}{unimported});
173 $intrp_styles{$s."_path_u"} = $widgets{feff_intrp} ->
174 ItemStyle(@list, -anchor => 'w', -foreground => $config{colors}{foreground}, -font => $config{intrp}{unimported});
175
176 };
177
178
179 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(0,
180 -text=>"#",
181 -style=>$gds_styles{header},
182 -headerbackground=>$config{colors}{background});
183 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(1,
184 -text=>"Deg.",
185 -style=>$gds_styles{header},
186 -headerbackground=>$config{colors}{background});
187 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(2,
188 -text=>"Reff",
189 -style=>$gds_styles{header},
190 -headerbackground=>$config{colors}{background});
191 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(3,
192 -text=>"amp.",
193 -style=>$gds_styles{header},
194 -headerbackground=>$config{colors}{background});
195 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(4,
196 -text=>"fs",
197 -style=>$gds_styles{header},
198 -headerbackground=>$config{colors}{background});
199 $widgets{feff_intrp} -> Subwidget("hlist") -> headerCreate(5,
200 -text=>"Scattering Path",
201 -style=>$gds_styles{header},
202 -headerbackground=>$config{colors}{background});
203
204
205 BindMouseWheel($widgets{feff_intrp});
206
207 $widgets{help_intrp} =
208 $feffcard{Interpretation}
209 -> Button(-text=>'Document: Feff interpretation', @button2_list,
210 -command=>sub{pod_display("artemis_intrp.pod")} )
211 -> pack(-side=>'bottom', -fill=>'x', -pady=>2);
212
213 return $c;
214 };
215
216
217 sub set_feff_showing {
218 return unless defined($widgets{feff_inptext}->Subwidget("yscrollbar"));
219 $paths{$current}->make(feff_showing=>$_[0],
220 feff_inp_location=>
221 ($widgets{feff_inptext}->Subwidget("yscrollbar")->get())[0],
222 feff_intrp_location=>
223 ($widgets{feff_intrp} ->Subwidget("yscrollbar")->get())[0]
224 );
225 };
226
227
228
229 sub intrp_header_menu {
230 my $w = shift;
231 my $Ev = $w->XEvent;
232 my ($X, $Y) = ($Ev->X, $Ev->Y);
233 $top ->
234 Menu(-tearoff=>0,
235 -menuitems=>[
236 [ command => "View log of feff run",
237 -command => [\&display_file, 'feff', 'feff.run']],
238 [ command => "View misc.dat",
239 -command => [\&display_file, 'feff', 'misc.dat']],
240 [ command => "View files.dat",
241 -command => [\&display_file, 'feff', 'files.dat']],
242 [ command => "View paths.dat",
243 -command => [\&display_file, 'feff', 'paths.dat']],
244 ])
245 -> Post($X, $Y);
246 $w -> break;
247 return;
248 };
249
250
251
252 sub snarf_path {
253
254 ## figure out where the user clicked
255 my $w = shift;
256 my $Ev = $w->XEvent;
257 delete $w->{'shiftanchor'};
258 my $entry = $w->GetNearest($Ev->y, 1);
259 return unless (defined($entry) and length($entry));
260
261 ## select and anchor the right-clicked parameter
262 my @sel = $w->selectionGet();
263 $w->anchorSet($entry);
264 my $clicked = $w->info('anchor');
265 if (grep {/^$clicked$/} @sel) {
266 ## right click within the current extended selection
267 1;
268 } else {
269 ## right clicked outside the current extended selection
270 $w->selectionClear;
271 $w->selectionSet($entry);
272 @sel = $w->selectionGet();
273 };
274
275 ## post the message with path-appropriate text
276 my $which = $w->infoAnchor;
277 my $nnnn = sprintf("feff%4.4d.dat", $w->itemCget($which, 0, '-text'));
278 my ($X, $Y) = ($Ev->X, $Ev->Y);
279
280 my $file = File::Spec->catfile($paths{$current}->get('path'), $nnnn);
281 my $lab = "";
282 my ($this, @these) = ("", ());
283 ## the use of sort here is crufty, but it should mean that the first
284 ## example of a path in the list which uses the feffNNNN.dat file
285 ## indicated by $nnnn will be first
286
287 ## These next two blocks map the anchor and the selection on the
288 ## intrp to the corresponding paths in DPL.
289 foreach my $k (sort (keys %paths)) {
290 next unless (ref($paths{$k}) =~ /Ifeffit/);
291 next unless ($paths{$k}->type eq 'path');
292 next unless ($paths{$k}->get('parent') eq $current);
293 ($lab,$this) = ($paths{$k}->get('lab'), $paths{$k}->get('id')), last
294 if ($paths{$k}->get('feff') eq $nnnn);
295 };
296 my $anchor = $lab;
297 if ($#sel>0) {
298 $lab = "these paths";
299 my $ii = 0;
300 foreach my $s (@sel) {
301 my $nnnn = sprintf("feff%4.4d.dat", $w->itemCget($s, 0, '-text'));
302 INNER: foreach my $k (sort (keys %paths)) {
303 next unless (ref($paths{$k}) =~ /Ifeffit/);
304 next unless ($paths{$k}->type eq 'path');
305 next unless ($paths{$k}->get('parent') eq $current);
306 push(@these, $paths{$k}->get('id')), last INNER
307 if ($paths{$k}->get('feff') eq $nnnn);
308 };
309 $these[$ii] ||= 0;
310 ++$ii;
311 };
312 } else {
313 ## if the selection and the anchor are the same, put the anchor
314 ## and it's path in these arrays
315 $lab = "\"" . $lab . "\"";
316 @these = ($this);
317 @sel = ($which);
318 };
319
320 ## some menu items are greyed out for multiple selection, some are
321 ## greyed out for excluded paths
322 if ($this) {
323 $top ->
324 Menu(-tearoff=>0,
325 -menuitems=>[['cascade' => "Plot data and $lab in ...",
326 -state=>($paths{$this}->get('include')) ? 'normal' : 'disabled',
327 -tearoff => 0,
328 -menuitems=> [[ command => 'k',
329 -command => sub{&plot_path(\@these, 'k')}],
330 [ command => 'R',
331 -command => sub{&plot_path(\@these, 'R')}],
332 [ command => 'q',
333 -command => sub{&plot_path(\@these, 'q')}]]],
334 ['command' => "Jump to \"$anchor\"",
335 #-state => ($#sel>0) ? 'disabled' : 'normal',
336 -command => sub{display_page($this)}],
337 ['command' =>
338 ($paths{$this}->get('include')) ? "Exclude $lab" : "Include $lab",
339 -command => sub{
340 my $onoff = $paths{$this}->get('include');
341 foreach my $p (0..$#these) {
342 next unless exists $paths{$these[$p]};
343 if ($onoff) {
344 &select_paths('off', $these[$p], 1);
345 } else {
346 &select_paths('on', $these[$p], 1);
347 };
348 };
349 &display_properties;
350 map {$widgets{feff_intrp}->selectionSet($_)} @sel;
351 }],
352 ['command' =>
353 (grep {$_ eq $this} $list->info('selection')) ? "Deselect $lab" : "Select $lab",
354 -state=>($paths{$this}->get('include')) ? 'normal' : 'disabled',
355 -command => sub{my $how = (grep {$_ eq $this} $list->info('selection')) ? 1 : 0;
356 &path_select($how)}],
357 ['command' => "Plot $lab after fit",
358 -command => sub{
359 foreach my $p (keys %paths) {
360 next unless ($paths{$p}->type eq 'path');
361 set_plotpath($p, 0);
362 };
363 foreach my $p (0..$#these) {
364 next unless (exists $paths{$these[$p]});
365 set_plotpath($these[$p], 1);
366 };
367 }],
368 ['command' => "Mark \"$anchor\" as current",
369 #-state => (($#sel>0) or not $paths{$this}->get('include')) ? 'disabled' : 'normal',
370 -command => sub{&set_path_index($this)}],
371 "-",
372 ['cascade' => "Edit path parameters",
373 -tearoff => 0,
374 -menuitems=> [[ command => 'label',
375 -command => sub{&add_mathexp('label')}],
376 [ command => 'S02',
377 -command => sub{&add_mathexp('S02')}],
378 [ command => 'delE0',
379 -command => sub{&add_mathexp('E0')}],
380 [ command => 'delR',
381 -command => sub{&add_mathexp('delR')}],
382 [ command => 'sigma^2',
383 -command => sub{&add_mathexp('sigma^2')}],
384 [ command => 'Ei',
385 -command => sub{&add_mathexp('Ei')}],
386 [ command => '3rd',
387 -command => sub{&add_mathexp('3rd')}],
388 [ command => '4th',
389 -command => sub{&add_mathexp('4th')}],
390 ]],
391 "-",
392 [ command => "Show geometry for \"$anchor\"",
393 -command => [\&display_path_header, $this]],
394 [ command => "View \"$anchor\"",
395 #-state => ($#sel>0) ? 'disabled' : 'normal',
396 -command => [\&display_file, 'path', $nnnn]],
397 "-",
398 ['command' => "Discard $lab",
399 -command => sub{
400 foreach my $p (0..$#these) {
401 next unless exists $paths{$these[$p]};
402 my $style = 'normal';
403 $style = "focus" if ($widgets{feff_intrp} -> itemCget($sel[$p],4,'-text') =~ /\d/);
404 $style = "ss" if ($paths{$these[$p]} -> get('nleg') == 2);
405 &delete_path($these[$p]);
406 };
407 &display_properties;
408 map {$widgets{feff_intrp}->selectionSet($_)} @sel;
409 }],
410 ])
411 -> Post($X,$Y);
412 } elsif (-e $file) {
413 my $text = ($#sel>0) ? "these paths" : $nnnn;
414 $top ->
415 Menu(-tearoff=>0,
416 -menuitems=>[[ command => "Add $text to the path list",
417 -command => sub {
418 foreach my $w (0..$#sel) {
419 next if ((exists $these[$w]) and (exists $paths{$these[$w]}));
420 my $nnnn = sprintf("feff%4.4d.dat", $widgets{feff_intrp}->itemCget($sel[$w], 0, '-text'));
421 my $file = File::Spec->catfile($paths{$current}->get('path'), $nnnn);
422 add_a_path($file, 1, 0);
423 };
424 &display_properties;
425 map {$widgets{feff_intrp}->selectionSet($_)} @sel;
426 }],
427 [ command => "Add and jump to $nnnn",
428 -state => ($#sel>0) ? 'disabled' : 'normal',
429 -command => sub {
430 foreach my $w (0..$#sel) {
431 next if ((exists $these[$w]) and (exists $paths{$these[$w]}));
432 my $nnnn = sprintf("feff%4.4d.dat", $widgets{feff_intrp}->itemCget($sel[$w], 0, '-text'));
433 my $file = File::Spec->catfile($paths{$current}->get('path'), $nnnn);
434 add_a_path($file, 0, 0);
435 };
436 }],
437 (($config{histogram}{use}) ?
438 ("-",
439 [ command => "Make histogram using $nnnn",
440 -state => ($#sel>0) ? 'disabled' : 'normal',
441 -command => [\&histogram, $lab]]) : ()),
442 "-",
443 [ command => "Show geometry for $anchor",
444 -command => sub {
445 $paths{toss} = Ifeffit::Path -> new(id => "toss",
446 lab => $nnnn,
447 type => 'path',
448 feff => $nnnn,
449 parent => $current,
450 path => $paths{$current}->{path},
451 data => $paths{$current}->{data},
452 family => \%paths);
453 my $header = nnnn_header("toss", File::Spec->catfile($paths{$current}->get('path'),
454 $nnnn));
455 $paths{toss} -> make(header => $header);
456 display_path_header("toss");
457 delete $paths{toss};
458 }],
459 [ command => "View $anchor",
460 #-state => ($#sel>0) ? 'disabled' : 'normal',
461 -command => [\&display_file, 'path', $nnnn]],
462 ])
463 -> Post($X, $Y);
464 } else {
465 $top ->
466 Menu(-tearoff=>0, -disabledforeground => 'black',
467 -menuitems=>[[ command => "The file $nnnn does not exist.",
468 -state => 'disabled',
469 ],
470 [ command => "Perhaps you should rerun Feff.",
471 -state => 'disabled',
472 ]])
473 -> Post($X, $Y);
474 };
475 };
476
477
478 sub path_select {
479 my $how = $_[0];
480 my @which = $widgets{feff_intrp}->selectionGet();
481 my $this;
482 foreach my $w (@which) {
483 my $nnnn = sprintf("feff%4.4d.dat", $widgets{feff_intrp}->itemCget($w, 0, '-text'));
484 foreach my $k (sort (keys %paths)) {
485 next unless (ref($paths{$k}) =~ /Ifeffit/);
486 next unless ($paths{$k}->type eq 'path');
487 next unless ($paths{$k}->get('parent') eq $current);
488 $this = $paths{$k}->get('id'), last
489 if ($paths{$k}->get('feff') eq $nnnn);
490 };
491 ($how) ? $list->selectionClear($this) : $list->selectionSet($this);
492 };
493 };
494
495 sub populate_feff {
496 my $this = $_[0];
497
498 $fefftabs -> pageconfigure('Atoms', -state=>'disabled');
499 $fefftabs -> pageconfigure('feff.inp', -state=>'disabled');
500 $fefftabs -> pageconfigure('Interpretation', -state=>'disabled');
501
502 ## there is atoms data associated with this feff calc
503 if ($paths{$current}->{mode} & 1) {
504 $fefftabs -> pageconfigure('Atoms', -state=>'normal');
505 $atoms_params{edge} = ucfirst($paths{$current}->get('atoms_edge'));
506 $atoms_params{core} = lc($paths{$current}->get('atoms_core'));
507 $atoms_params{core} ||= "^^nothing^^";
508
509 ## foreach my $k (grep /^atoms_core/, keys(%widgets)) {
510 ## $widgets{$k} -> configure(-value => "");
511 ## my $n = (split(/_/, $k))[2];
512 ## $widgets{$k} -> configure(-value => $n);
513 ## next unless ($paths{$current}->get("atoms_elem_$n") and
514 ## ($paths{$current}->get("atoms_elem_$n") !~ /^\s*$/));
515 ## $widgets{$k} -> configure(-value => lc($paths{$current}->get("atoms_tag_$n")) ||
516 ## lc($paths{$current}->get("atoms_elem_$n")) ||
517 ## $n );
518 ## };
519
520 ## populate the table of sites
521 populate_atoms($current);
522 $atoms_params{edge} = $paths{$current}->get("atoms_edge");
523
524 ## transfer the atoms_* values to the atoms_* widgets
525 foreach my $k (grep /^atoms_/, keys(%{$paths{$current}})) {
526 next if ($k =~ /_(atoms|edge|occ|core|titles)/);
527 $widgets{$k} -> configure(-validate=>'none');
528 $widgets{$k} -> delete(0, 'end');
529 $widgets{$k} -> configure(-validate=>'key'), next
530 if (($k =~ /(a|b|c|alpha|beta|gamma)$/) and ($paths{$current}->get($k) =~ /^(\s*|0)$/));
531 $widgets{$k} -> configure(-validate=>'key'), next
532 if (($k =~ /(alpha|beta|gamma)$/) and ($paths{$current}->get($k) =~ /^(\s*|90)$/));
533 $widgets{$k} -> insert('end', $paths{$current}->get($k));
534 $widgets{$k} -> configure(-validate=>'key');
535 };
536 $widgets{atoms_titles} -> delete('1.0', 'end');
537 my $titles = $paths{$current}->get('atoms_titles');
538 $titles =~ s/<NL>/\n/g;
539 $titles ||= "";
540 $widgets{atoms_titles} -> insert('end', $titles);
541 };
542
543
544 ## there is a feff.inp file associated with this feff calc.
545 if ($paths{$current}->get('mode') & 2) {
546 $fefftabs -> pageconfigure('feff.inp', -state=>'normal');
547 $widgets{feff_inptext} -> delete('1.0', 'end');
548 if (-e $paths{$current}->get('feff.inp')) {
549 local $/ = undef;
550 open FI, $paths{$current}->get('feff.inp');
551 $widgets{feff_inptext} -> insert('1.0', <FI>, "feffinp");
552 $widgets{feff_inptext} -> FindAndReplaceAll("-regexp", "-nocase", "\r", "") if not $is_windows;
553 close FI;
554 } else {
555 $widgets{feff_inptext} -> insert('1.0', "No feff.inp file found for this Feff calculation.", "feffwarn");
556 };
557 $widgets{feff_inptext} -> yviewMoveto($paths{$current}->get('feff_inp_location')||0);
558 $widgets{feff_inptext} -> ResetUndo;
559 };
560
561 ## feff has been run and the intrp box needs to be filled
562 if ($paths{$current}->get('mode') & 4) {
563 $fefftabs -> pageconfigure('Interpretation', -state=>'normal');
564 intrp_fill($current);
565 };
566
567 if ($paths{$current}->get('feff_showing')) {
568 $fefftabs -> raise($paths{$current}->get('feff_showing'));
569 } elsif ($paths{$current}->get('mode') & 4) {
570 $fefftabs -> raise('Interpretation');
571 } elsif ($paths{$current}->get('mode') & 2) {
572 $fefftabs -> raise('feff.inp');
573 } else {
574 $fefftabs -> raise('Atoms');
575 };
576
577 };
578
579
580
581 sub intrp_fill {
582 my $which = $_[0];
583 $widgets{feff_intrp} -> delete('all');
584 $widgets{feff_intrp_header} -> delete(qw(1.0 end));
585 my $i = 1;
586 foreach my $l (split(/\n/, $paths{$which}->get('intrp'))) {
587
588 next if ($l =~ /^\#\s*$/);
589 next if ($l =~ /^\#\s*---/);
590 next if ($l =~ /^\#\s*degen/);
591 ## this is a header line
592 ($l =~ /^\#/) and do {
593 $l =~ s{\r}{};
594 $widgets{feff_intrp_header} -> insert('end', $l."\n");
595 next;
596 };
597
598 $widgets{feff_intrp}->add($i);
599 my @line = split(" ", $l);
600 shift @line;
601
602 ## determine if this line describes a path which is included,
603 ## excluded, absent, or unimported
604 my $nnnn = "feff" . $line[0] . ".dat";
605 my $style = "_u";
606 my $file = File::Spec->catfile($paths{$current}->get('path'), $nnnn);
607 if (-e $file) {
608 my $this;
609 ## the use of sort here is crufty, but it should mean that the first
610 ## example of a path in the list which uses the feffNNNN.dat file
611 ## indicated by $nnnn will be first
612 foreach my $k (sort (keys %paths)) {
613 next unless (ref($paths{$k}) =~ /Ifeffit/);
614 next unless ($paths{$k}->type eq 'path');
615 next unless ($paths{$k}->get('parent') eq $current);
616 if ($paths{$k}->get('feff') eq $nnnn) {
617 $style = ($paths{$k}->get('include')) ? "" : "_x";
618 last;
619 };
620 };
621 } else {
622 $style = "_a";
623 };
624
625 SWITCH: { # the $style variable tells us how to
626 # do the font, this switch is about
627 # the background (SS|focused|neither)
628
629 ## this is a SS path
630 ($l =~ /^2/) and do {
631 $widgets{feff_intrp} -> itemCreate($i, 0, -text=>sprintf("%-3d", shift @line),
632 -style=>$intrp_styles{"ss".$style});
633 $widgets{feff_intrp} -> itemCreate($i, 1, -text=>shift @line,
634 -style=>$intrp_styles{"ss".$style});
635 $widgets{feff_intrp} -> itemCreate($i, 2, -text=>shift @line,
636 -style=>$intrp_styles{"ss".$style});
637 $widgets{feff_intrp} -> itemCreate($i, 3, -text=>shift @line,
638 -style=>$intrp_styles{"ss_amp".$style});
639 $widgets{feff_intrp} -> itemCreate($i, 4, -text=>"",
640 -style=>$intrp_styles{"ss".$style});
641 shift @line;
642 $widgets{feff_intrp} -> itemCreate($i, 5, -text=>join(" ", @line),
643 -style=>$intrp_styles{"ss_path".$style});
644 last SWITCH;
645 };
646
647 ## this is a focussed MS path
648 ($l =~ /[1-9] :/) and do {
649 $widgets{feff_intrp} -> itemCreate($i, 0, -text=>sprintf("%-3d", shift @line),
650 -style=>$intrp_styles{"focus".$style});
651 $widgets{feff_intrp} -> itemCreate($i, 1, -text=>shift @line,
652 -style=>$intrp_styles{"focus".$style});
653 $widgets{feff_intrp} -> itemCreate($i, 2, -text=>shift @line,
654 -style=>$intrp_styles{"focus".$style});
655 $widgets{feff_intrp} -> itemCreate($i, 3, -text=>shift @line,
656 -style=>$intrp_styles{"focus_amp".$style});
657 $widgets{feff_intrp} -> itemCreate($i, 4, -text=>shift @line,
658 -style=>$intrp_styles{"focus".$style});
659 shift @line;
660 $widgets{feff_intrp} -> itemCreate($i, 5, -text=>join(" ", @line),
661 -style=>$intrp_styles{"focus_path".$style});
662 last SWITCH;
663 };
664
665 ## this is a line that could not be interpretted
666 ($l =~ /^\s*Could/) and do {
667 ## this should never happen if feff is run within Artemis,
668 ## deal with a flawed imported calculation when the time comes
669 last SWITCH;
670 };
671
672 ## this is a normal line
673 do {
674 $widgets{feff_intrp} -> itemCreate($i, 0, -text=>sprintf("%-3d", shift @line),
675 -style=>$intrp_styles{"normal".$style});
676 $widgets{feff_intrp} -> itemCreate($i, 1, -text=>shift @line,
677 -style=>$intrp_styles{"normal".$style});
678 $widgets{feff_intrp} -> itemCreate($i, 2, -text=>shift @line,
679 -style=>$intrp_styles{"normal".$style});
680 $widgets{feff_intrp} -> itemCreate($i, 3, -text=>shift @line,
681 -style=>$intrp_styles{"normal_amp".$style});
682 $widgets{feff_intrp} -> itemCreate($i, 4, -text=>"",
683 -style=>$intrp_styles{"normal".$style});
684 shift @line;
685 $widgets{feff_intrp} -> itemCreate($i, 5, -text=>join(" ", @line),
686 -style=>$intrp_styles{"normal_path".$style});
687 last SWITCH;
688 };
689 };
690
691 ++$i;
692 };
693
694 };
695
696
697 sub feff_template {
698
699 my $dialog =
700 $top -> Dialog(-bitmap => 'questhead',
701 -text => "Do you want to import an existing feff.inp file or start with a blank page?",
702 -title => 'Artemis: Question...',
703 -buttons => ['Import feff.inp', 'Blank page', 'Cancel'],
704 -default_button => 'Blank page',
705 -font => $config{fonts}{med},
706 -popover => 'cursor');
707 &posted_Dialog;
708 my $answer = $dialog->Show();
709 if ($answer eq 'Cancel') {
710 Echo("Canceled new Feff");
711 return;
712 } elsif ($answer eq 'Import feff.inp') {
713 Echo("Importing Feff file");
714 read_feff(0);
715 return;
716 } else {
717 Echo("Making Feff template");
718 };
719
720
721 ## =============================== empty data structures
722 my $cell = Xray::Xtal::Cell -> new();
723 my $keywords = Xray::Atoms -> new();
724 $keywords -> make(identity=>"the Feff template generator in Artemis $VERSION",
725 die=>0);
726
727 ## =============================== make the template
728 my $contents = "";
729 my (@cluster, @neutral);
730 my $atp = "template".$config{atoms}{feff_version};
731 ($atp = 'template8_exafs') if ($config{atoms}{feff_version} eq '8');
732 my ($default_name, $is_feff) =
733 &parse_atp($atp, $cell, $keywords, \@cluster, \@neutral, \$contents);
734
735 ## this is a new feff calc, so make an object and a space to
736 ## display it
737 my $data = $paths{$current}->data;
738 ## assign an id to this feff calc
739 my $id = $data . '.feff' . $n_feff;
740
741 ## &initialize_project(0);
742 ## make a project feff folder
743 my $project_feff_dir = &initialize_feff($id);
744
745 $paths{$id} = Ifeffit::Path -> new(id => $id,
746 type => 'feff',
747 path => File::Spec->catfile($project_folder, $id),
748 data => $data,
749 lab => 'FEFF'.$n_feff,
750 family => \%paths,
751 atoms_atoms => []
752 );
753 initialize_atoms($id);
754 $paths{$id} -> make(mode=>2);
755 my @autoparams;
756 $#autoparams = 6;
757 (@autoparams = autoparams_define($id, $n_feff, 0, 0)) if $config{autoparams}{do_autoparams};
758 $paths{$id} -> make(autoparams=>[@autoparams]);
759
760
761
762 $list -> add($id, -text=>'FEFF'.$n_feff, -style=>$list_styles{noplot});
763 $list -> setmode($id, 'close');
764 $list -> setmode($paths{$data}->get('id'), 'close')
765 if ($list -> getmode($paths{$data}->get('id')) eq 'none');
766
767 &set_fit_button('fit');
768 display_page($id);
769 project_state(0);
770 ++$n_feff;
771
772
773 ## and save it top a file in the project space
774 my $feff_file = File::Spec->catfile($project_folder, $id, "feff.inp");
775 open FEFFFILE, ">".$feff_file or die "could not open $feff_file for writing";
776 print FEFFFILE $contents;
777 close FEFFFILE;
778
779
780
781 $widgets{feff_inptext} -> Load(File::Spec->catfile($project_folder, $id, "feff.inp"));
782 $widgets{feff_inptext} -> tagAdd("feffinp", qw(1.0 end));
783 $widgets{feff_inptext} -> ResetUndo;
784 $fefftabs -> pageconfigure('feff.inp', -state=>'normal');
785 $fefftabs -> raise('feff.inp');
786
787 undef $cell;
788 undef $keywords;
789
790 Echo("Generated a template feff.inp for feff$config{atoms}{feff_version}");
791
792
793 };
794
795
796
797 # sub change_path {
798 # my $parent = $current;
799 # $parent = $paths{$current}->get('parent') if ($paths{$current}->type eq "path");
800 # ## what about case of current = data or fit|diff|bkg|res???
801 # #$parent = $paths{$current}->get('parent') if ($paths{$current}->type eq "path");
802 #
803 # #my $start = $paths{$parent}->get('path') || $current_data_dir;
804 # #(-d $start) or ($start = $current_data_dir);
805 # my $dir = "";
806 # if ($Tk::VERSION < 804) {
807 # $dir = $top -> DirSelect(-width=>40, -dir=>$current_data_dir,
808 # -title=> "Artemis: Select a directory",
809 # -text => "Select the correct path to your FEFF calculation",
810 # ) -> Show;
811 # } else {
812 # $dir = $top -> chooseDirectory;
813 # };
814 # return 0 unless $dir;
815 # ($dir =~ /^\/\//) and ($dir = substr($dir, 1)); # single leading slash
816 # ($dir =~ /\/$/) or ($dir .= '/'); # trailing slash
817 # ($is_windows) and ($dir =~ s/\//\\/g); # windows-ify slashes
818 #
819 # my $which = "";
820 # if ($paths{$current}->type =~ /(bkg|data|diff|fit|res)/) {
821 # my $d = $1;
822 # foreach my $p (keys %paths) {
823 # next unless (ref($paths{$p}) =~ /Ifeffit/);
824 # next unless $paths{$p}->type;
825 # next unless ($p =~ /feff\d$/);
826 # next unless ($paths{$p}->data eq $d);
827 # $paths{$p} -> make(path=>$dir);
828 # $paths{$p} -> make(mode=>2) if (-e $paths{$p}->get('feff.inp'));
829 # my $intrp_ok = &do_intrp($p);
830 # $paths{$p} -> make(mode=>$paths{$p}->get('mode')+4) if $intrp_ok;
831 # $which = $p;
832 # };
833 # } else {
834 # $paths{$parent} -> make(path=>$dir);
835 # $paths{$parent} -> make(mode=>2) if (-e $paths{$parent}->get('feff.inp'));
836 # my $intrp_ok = &do_intrp($parent);
837 # $paths{$parent} -> make(mode=>$paths{$parent}->get('mode')+4) if $intrp_ok;
838 # $which = $parent;
839 # };
840 #
841 # ## fixy up the path headers
842 # foreach my $p (keys %paths) {
843 # next unless (ref($paths{$p}) =~ /Ifeffit/);
844 # next unless $paths{$p}->type;
845 # next unless ($paths{$p}->get('parent') eq $which);
846 #
847 # my $fname = $paths{$p}->{feff};
848 # if (-e File::Spec->catfile($paths{$which}->get('path'), $fname)) {
849 # $paths{$p} -> make(feff=>$fname);
850 # ## try it lower case
851 # } elsif (-e File::Spec->catfile($paths{$which}->get('path'), lc($fname))) {
852 # $paths{$p} -> make(feff=>lc($fname));
853 # ## try it upper case
854 # } elsif (-e File::Spec->catfile($paths{$which}->get('path'), uc($fname))) {
855 # $paths{$p} -> make(feff=>uc($fname));
856 # ## try it capitalized
857 # } elsif (-e File::Spec->catfile($paths{$which}->get('path'), uc($fname))) {
858 # $paths{$p} -> make(feff=>ucfirst($fname));
859 # ## uh oh!
860 # } else {
861 # 1;
862 # };
863 #
864 #
865 # my $file = File::Spec->catfile($paths{$which}->get('path'), $paths{$p}->get('feff'));
866 # if (-e $file) {
867 # $paths{$p} -> make(header=>nnnn_header($p, $file));
868 # } else {
869 # $paths{$p} -> make(header=>"", include=>0);
870 # $list -> entryconfigure($p, -style => $list_styles{$paths{$p}->pathstate("disabled")});
871 # };
872 # };
873 #
874 # $current_data_dir = $dir;
875 # display_properties;
876 # };
877
878
879 sub do_intrp {
880 my $id = $_[0];
881 my $retval = 0;
882 Echo("Doing intrp for " . $paths{$id}->get('lab') . " ...");
883 # $widgets{feff_intrp} -> delete(qw(1.0 end));
884 if ((-e $paths{$id}->get('feff.inp')) and (-e $paths{$id}->get('files.dat')) and
885 (-e $paths{$id}->get('paths.dat'))) {
886 $paths{$id}->make(intrp=>$paths{$id}->intrp($config{intrp}{betamax},
887 $config{intrp}{core_token}));
888 my $data = $paths{$id}->data;
889 if (lc($paths{$data}->get('pcelem')) eq 'h') {
890 $paths{$data}->make(pcelem=>$paths{$id}->get('central'),
891 pcedge=>$paths{$id}->get('edge'));
892 };
893 #intrp_fill($id);
894 $retval = 1;
895 } else {
896 my @files = grep { ! -e $paths{$id}->get($_) } (qw(feff.inp files.dat paths.dat));
897 my $string = reverse(join(", ", @files));
898 $string =~ s/,/ro\/dna ,/;
899 $string = reverse $string;
900 ##Echo("Could not find " . $string);
901 $paths{$id}->make(intrp=>" Could not find " . $string);
902 };
903 Echo("Doing intrp for " . $paths{$id}->get('lab') . " ... done!");
904 return $retval;
905 };
906
907
908 sub fetch_nnnn {
909 my ($parent, $pathto, $f, $data) = @_;
910 my $file = File::Spec->catfile($pathto,$f);
911 next unless (-e $file);
912 my $was_mac = $paths{data0} ->
913 fix_mac($file, $stash_dir, lc($config{general}{mac_eol}), $top);
914 Echo("\"$file\" had Macintosh EOL characters and was skipped."), return if ($was_mac == -1);
915 if ($was_mac) {
916 Echo("\"$file\" had Macintosh EOL characters and was fixed.");
917 $file = $was_mac;
918 };
919
920 ##my $parent = $list -> infoParent($id);
921 my $id = $list -> addchild($parent);
922 $paths{$id} = Ifeffit::Path -> new(id => $id,
923 type => 'path',
924 # group => $id,
925 file => $f,
926 include => 1,
927 plotpath => 0,
928 parent => $parent,
929 do_k => 1,
930 data => $data||$paths{$current}->data,
931 family => \%paths);
932 $paths{$id} -> make(header=>nnnn_header($id, $file));
933 foreach my $l (split(/\n/, $paths{$parent}->get('intrp'))) {
934 my $ss = substr($f,4,4);
935 $paths{$id} -> make(feff_index=>sprintf('%d', $ss));
936 $paths{$id} -> make(intrpline=>substr($l, 2)), last
937 if ($l =~ /^\d\s+$ss/);
938 };
939 $paths{$id} -> pathlabel($config{paths}{label});
940 my @autoparams = @{$paths{$parent}->get('autoparams')};
941 foreach my $p (qw(s02 e0 delr sigma^2 ei 3rd 4th)) {
942 my $this = shift @autoparams;
943 $paths{$id} -> make($p=>$this);
944 };
945 $file_menu -> menu -> entryconfigure($save_index+4, -state=>($Tk::VERSION > 804) ? 'normal' : 'disabled'); # all paths
946 project_state(0);
947 return $id;
948 };
949
950
951 ## the chomp; chop; thing is an attempt to handle the strange
952 ## situation of running artemis on unix and having run feff on
953 ## windows. in that case a ^M (\015, \r) character gets left at the
954 ## end of each line after chomping.
955 sub nnnn_header {
956 my ($id, $file) = @_;
957 my $nnnn = sprintf("%d", substr($paths{$id}->get('feff'), 4, 4));
958 my $parent = $paths{$id}->get('parent');
959 my $header = '';
960 if (-e $paths{$parent}->get('paths.dat')) {
961 open F, $paths{$parent}->get('paths.dat');
962 PATHSDAT: while (<F>) {
963 next unless (/index, nleg, degeneracy, r/);
964 next unless (/^\s+$nnnn\b/);
965 chomp; chop if (/\r$/);
966 my @words = split(" ", $_);
967 $paths{$id} -> make(nleg => $words[1]||1,
968 reff => $words[7]||0,
969 deg => defined($words[2]) ? int($words[2]) : 0,
970 zcwif => 0);
971 $header = sprintf(" %s legs Reff=%s degeneracy=%d\n\n",
972 $paths{$id}->get('nleg'),
973 $paths{$id}->get('reff'),
974 $paths{$id}->get('deg'),);
975 my $continue = 1;
976 my $ileg = 1;
977 while ($continue) {
978 my $line = <F>;
979 last PATHSDAT unless $line;
980 chomp $line; chop $line if (/\r$/);
981 if ($line =~ /^\s*x/) {
982 } elsif ($line =~ /index, nleg, degeneracy, r/) {
983 $continue = 0;
984 } else {
985 my @parts = split("'", $line);
986 $parts[1] ||= " ";
987 $parts[2] ||= "0 0 0";
988 my $coords = sprintf("%9.5f %9.5f %9.5f %d ", split(" ", $parts[0]));
989 my @pp = split(" ", $parts[2]);
990 my $angles = ($pp[2] > 0) ?
991 sprintf(" rleg=%s beta=%7.3f eta=%7.3f\n", @pp) :
992 sprintf(" rleg=%s beta=%7.3f\n", @pp[0,1]);
993 $header .= "leg $ileg: " . $coords . $parts[1] . "\n" . $angles;
994 my @words = split(" ", $line);
995 if ($words[3] eq 0) {
996 $paths{$id} -> make(element=>$1) if ($line =~ /'(.*)\s*'/);
997 };
998 ++$ileg;
999 };
1000 };
1001 last PATHSDAT;
1002 };
1003 } else {
1004 $paths{$id} -> make(nleg=>0, reff=>0, deg=>0, element=>" ", zcwif=>0);
1005 };
1006 close F;
1007 ## the ZCWIF isn't in the header, so I have to go read files.dat
1008 ## each time through. sigh....
1009 my $filesdat = $paths{$id}->get('files.dat');
1010 my $thisnnnn = basename($file);
1011 if (-r $filesdat) {
1012 open FILES, $filesdat;
1013 ## or die "could not open $filesdat for reading in nnnn_header\n";
1014 while (<FILES>) {
1015 next unless (/^\s*$thisnnnn/);
1016 my @line = split(" ", $_);
1017 $paths{$id} -> make(zcwif=>$line[2]||0);
1018 last;
1019 };
1020 };
1021 close FILES;
1022 my $zcwif = $paths{$id}->get('zcwif');
1023 $header =~ s/degeneracy/amp=$zcwif degen/;
1024 return $header;
1025 };
1026
1027
1028
1029 ## delete the currently selected feff calc and select/anchor the previous
1030 ## what if I want to delete entire calc from a path canvas
1031 sub delete_feff {
1032 my $this = $_[0] || $current;
1033 my $force = $_[1];
1034 my $delete = $_[2] || 1; # 0=compactify folder
1035 # 1=delete folder
1036 # 2=keep folder, delete groups (restore previous fit)
1037 my $label = $paths{$this}->get('lab');
1038 Echo('\"$label\" is not a FEFF calculation.'), return unless ($this =~ /feff\d+$/);
1039 unless ($force) {
1040 my $dialog =
1041 $top -> Dialog(-bitmap => 'questhead',
1042 -text => "Are you sure you want to discard \"$label\" and all its paths?",
1043 -title => 'Artemis: Verifying...',
1044 -buttons => [qw/Discard Cancel/],
1045 -default_button => 'Discard',
1046 -font => $config{fonts}{med},
1047 -popover => 'cursor');
1048 &posted_Dialog;
1049 my $response = $dialog->Show();
1050 Echo("Not discarding \"$label\""), return unless ($response eq 'Discard');
1051 };
1052 ## remove the autoparams if they still exist
1053 my @names = (); my @indeces = ();
1054 foreach my $a (@{$paths{$this}{autoparams}}) {
1055 my $i = 0;
1056 foreach my $p (@gds) {
1057 if (lc($a) eq lc($p->{name})) {
1058 push(@names, $a);
1059 push(@indeces, $i);
1060 last;
1061 };
1062 ++$i;
1063 };
1064 };
1065 if (@names) {
1066 my $response = "";
1067 if ($force) {
1068 ## do not want to see this dialog when discarding entire project
1069 ## or data set
1070 $response = "Discard";
1071 } else {
1072 my @vars = grep {defined($_) and ($_ !~ m{^\s*$})} @names;
1073 if (@vars) {
1074 my $addendum = join(", ", @vars);
1075 my $dialog =
1076 $top -> Dialog(-bitmap => 'questhead',
1077 -text => "Do you want to discard the variables $addendum",
1078 -title => 'Artemis: Verifying...',
1079 -buttons => [qw/Discard Keep/],
1080 -default_button => 'Discard',
1081 -font => $config{fonts}{med},
1082 -popover => 'cursor');
1083 &posted_Dialog;
1084 $response = $dialog->Show();
1085 } else {
1086 $response = "Keep";
1087 };
1088 };
1089 if ($response eq 'Discard') {
1090 my $offset = 0; # needed because the list gets
1091 # shorter each time
1092 foreach my $i (@indeces) {
1093 splice(@gds, $i-$offset, 1);
1094 ++$offset;
1095 };
1096 $gds_selected{type} = "guess";
1097 $gds_selected{name} = "";
1098 $gds_selected{mathexp} = "";
1099 $gds_selected{which} = 0;
1100 repopulate_gds2();
1101 @names = grep {defined($_) and ($_ !~ m{^\s*$})} @names;
1102 Echo("Discarded variables " . join(", ", @names)) if @names;
1103 } else {
1104 Echo("Not discarding variables");
1105 };
1106 };
1107 $this = ($this =~ /feff\d+$/) ? $this : $paths{$this}->get('parent');
1108
1109 if ($delete==0) {
1110 my $project_feff_folder = File::Spec->catfile($project_folder, $this);
1111 rmtree($project_feff_folder) if (-d $project_feff_folder);
1112 } elsif ($delete==1) {
1113 ## compact this directory, but don't delete it. it needs to stick
1114 ## around in case the use should want to revert the fitting model to
1115 ## a fit that used it.
1116 feff_compactify($this);
1117 };
1118
1119 my $new = $paths{$this}->data;
1120 my $message = $paths{$this}->descriptor();
1121 $list->delete('offsprings',$this);
1122 $list->delete('entry',$this);
1123 foreach (keys %paths) {
1124 next unless (ref($paths{$_}) =~ /Ifeffit/);
1125 next unless ($paths{$_}->type eq 'path');
1126 next unless ($paths{$_}->get('parent') eq $this);
1127 #$paths{$_}->dispose($paths{$_}->blank_path, $dmode); # unset this path
1128 $paths{$_}->drop;
1129 delete $paths{$_};
1130 };
1131 delete $paths{$this};
1132
1133 if ($this eq $current) {
1134 $current = $new;
1135 project_state(0);
1136 display_page($new);
1137 };
1138 Echo("Discarded $message");
1139 };
1140
1141
1142 sub identify_feff {
1143 my $this = ($paths{$current}->type eq 'feff') ? $current : $paths{$current}->get('parent');
1144 Echo("This Feff: $this This project: $project_folder");
1145 };
1146
1147 sub rename_feff {
1148 Error("There are no feff calculations."), return unless $n_feff;
1149 my $this = $current;
1150 ($this = $paths{$current}->get('parent')) if ($paths{$current}->type eq 'path');
1151 if ($paths{$current}->type =~ /(bkg|data|diff|fit|res)/) {
1152 my $data = $current;
1153 ($data = $paths{$current}->data) if ($paths{$current}->type =~ /(bkg|diff|fit|res)/);
1154 my $n = 0;
1155 while ($n <= $n_feff) {
1156 ($this = "feff$n"), last if (($paths{$data.".feff".$n}->get('lab')) and
1157 ($paths{$data.".feff".$n}->data eq $data));
1158 ++$n;
1159 };
1160 };
1161 my $oldname = $paths{$this}->get('lab');
1162 my $newname = $_[0];
1163 unless ($_[0]) {
1164 $newname = $oldname;
1165 my $label = "New name for \"$oldname\": ";
1166 my $dialog = get_string($dmode, $label, \$newname, \@rename_buffer);
1167 $dialog -> waitWindow; # the get_string dialog will be
1168 # destroyed once the user hits ok,
1169 # then we can move on...
1170 Echo("Not renaming ". $oldname), return if ($oldname eq $newname);
1171 $newname =~ s{[\"\']}{}g;
1172 my $exists = 0;
1173 foreach my $f (&all_feff) {
1174 $exists = 1, last if ($newname eq $paths{$f}->get('lab'));
1175 };
1176 Error("There is already a Feff calculation named \"$newname\"!"), return if $exists;
1177 };
1178 push @rename_buffer, $newname;
1179 project_state(0);
1180 $paths{$this} -> make(lab=>$newname);
1181 $list -> itemConfigure($this, 0, -text=>$newname);
1182 Echo("Renamed \"$oldname\" to \"$newname\".");
1183 };
1184
1185
1186 sub all_feff {
1187 return (sort (grep /^data\d+\.feff\d+$/, (keys %paths) ));
1188 };
1189
1190
1191 sub run_feff {
1192 my $this = $_[0];
1193 my $how_many = $_[1];
1194 my $feff_folder = File::Spec->catfile($project_folder, $paths{$this}->get('id'), "");
1195 my $feff_file = File::Spec->catfile($project_folder, $paths{$this}->get('id'), "feff.inp");
1196 my $was = cwd();
1197
1198 Echo("Preparing to run FEFF ...");
1199 ## save the feff.inp file
1200 #Echo("Saving feff.inp file");
1201 #$widgets{feff_inptext} -> Save($feff_file);
1202
1203 ## autosave
1204 &save_project(0,1);
1205
1206 ## clean up the previous feff calculation
1207 opendir F, $feff_folder;
1208 my @nnnn = grep(/feff\d{4}\.dat/i, readdir F);
1209 closedir F;
1210 my $rerun_feff = 0;
1211 if (@nnnn) {
1212 Echo("Deleting old feffNNNN.dat files");
1213 map {unlink File::Spec->catfile($feff_folder, $_)} @nnnn;
1214 $rerun_feff = 1;
1215 };
1216
1217 ## make the project feff folder the current working directory
1218 chdir $feff_folder;
1219
1220 ## run feff (need to capture stdout and send to messages buffer)
1221 ## raise_palette('messages');
1222 Running("Running feff (this could take a few minutes, please be patient) ... ");
1223 ##$top -> Busy();
1224 #my $feff_messages = `feff6`; # $config{feff}{feff_executable};
1225
1226 ### run feff --- PLATFORM DEPENDENT CODE ---
1227
1228 ### ------------------------------------------------
1229 ### ---- *NIX --------------------------------------
1230 ### ------------------------------------------------
1231 $notes{messages} -> delete(qw(1.0 end));
1232 raise_palette('messages');
1233 $top->update;
1234 unless ($is_windows) { # avoid problems if feff->feff_executable isn't
1235 my $which = `which $config{feff}{feff_executable}`;
1236 chomp $which;
1237 unless (-x $which) {
1238 Echo("Uh oh! That Feff calculation did not run successfully.");
1239 $notes{messages}->insert('end', Ifeffit::ParseFeff -> error_4, 'warning');
1240 $notes{messages}->insert('end', "\n\tCurrent, incorrect value of feff->feff_executable:\n\t\t$config{feff}{feff_executable}\n\n", 'warning');
1241 $notes{messages}->see('1.0');
1242 $update->raise;
1243 return;
1244 };
1245 };
1246 ## fork the feff process
1247 my $pid = open(WRITEME, "$config{feff}{feff_executable} |");
1248 $notes{messages} -> grab;
1249 $| = 1; # unbuffer its output
1250 while (<WRITEME>) { # and display it in the message buffer
1251 $notes{messages} -> insert('end', $_);
1252 $notes{messages} -> yviewMoveto(1);
1253 $top -> update;
1254 };
1255 close WRITEME;
1256 $notes{messages} -> grabRelease;
1257 ### ------------------------------------------------
1258
1259 ### ------------------------------------------------
1260 ### ---- WINDOWS ----
1261 ### ------------------------------------------------
1262 ## the fork seems to work on Windows, bless those perl developer elves!
1263 ### ------------------------------------------------
1264
1265 ## make sure that the run log is there
1266 unless (-e File::Spec->catfile($project_folder, $paths{$this}->get('id'), "feff.run")) {
1267 open LOG, ">".File::Spec->catfile($project_folder, $paths{$this}->get('id'), "feff.run");
1268 print LOG $notes{messages} -> get('1.0', 'end');
1269 close LOG;
1270 };
1271 ##$top -> Unbusy();
1272
1273 ## uh oh! problems running feff
1274 my $fefferr = File::Spec->catfile($project_folder, $paths{$this}->get('id'), "feff.err");
1275 my $err = Ifeffit::ParseFeff -> recognize( $notes{messages}->get(qw(1.0 end)) );
1276 if (($err eq 9) or ($err eq 11)) { # atoms close together OR heap exceeded
1277 $top -> Unbusy();
1278 $notes{messages}->insert('end', "\n");
1279 $notes{messages}->insert('end', Ifeffit::ParseFeff -> describe($err), 'warning');
1280 $notes{messages}->see('end');
1281 $update->raise;
1282 } elsif ($err) {
1283 ##&display_file('file', $fefferr);
1284 $top -> Unbusy();
1285 Error("Uh oh! That Feff calculation did not run successfully.");
1286 $notes{messages}->insert('end', "\n");
1287 $notes{messages}->insert('end', Ifeffit::ParseFeff -> describe($err), 'warning');
1288 $notes{messages}->see('end');
1289 $update->raise;
1290 return;
1291 } else {
1292 Echo("Running feff ... done!");
1293 };
1294
1295 my @nnnnlist;
1296 opendir D, $feff_folder or die "cannot read directory $feff_folder\n";
1297 @nnnnlist = sort grep /feff\d{4}\.dat/i, readdir D;
1298 closedir D;
1299 unless (@nnnnlist) {
1300 Error("There are no feffNNNN.dat files! Something has gone wrong with your Feff calculation!");
1301 if ($config{feff}{feff_executable} =~ /feff7/i) {
1302 $notes{messages}->insert('end', "\n");
1303 $notes{messages}->insert('end', Ifeffit::ParseFeff -> describe(12), 'warning');
1304 $notes{messages}->see('end');
1305 $update->raise;
1306 };
1307 $top -> Unbusy();
1308 return;
1309 };
1310 $#nnnnlist = -1;
1311
1312 ## snarf the list of paths to import
1313 my $response = $how_many;
1314 unless ($how_many) {
1315 my $text = "How many feff paths do you want to import right now?";
1316 $text .= " (You seem to have run feff once before ... the best choice is probably \"No paths\")"
1317 if $rerun_feff;
1318 my $dialog =
1319 $top -> Dialog(-bitmap => 'questhead',
1320 -text => $text,
1321 -title => 'Artemis: Question...',
1322 -buttons => ['No paths',
1323 'Just the first',
1324 "The first $config{paths}{firstn}",
1325 'All paths'],
1326 -default_button => ($rerun_feff) ? 'No paths' : 'All paths',
1327 -font => $config{fonts}{med},
1328 -popover => 'cursor');
1329 &posted_Dialog;
1330 $response = $dialog->Show();
1331 Echo("Importing " . lc($response));
1332 };
1333 my $is_busy = grep (/Busy/, $top->bindtags);
1334 $is_busy or $top -> Busy();
1335 $paths{$this} -> make(path=>$feff_folder);
1336
1337 ## do intrp
1338 my $intrp_ok = &do_intrp($this);
1339 if ($intrp_ok) {
1340 my $m = $paths{$this}->get('mode');
1341 ($m += 4) unless ($m & 4);
1342 $paths{$this}->make(mode=>$m);
1343 };
1344
1345 ## set display and increment feff counter
1346 $file_menu -> menu -> entryconfigure($save_index+6, -state=>'normal');
1347 $file_menu -> menu -> entryconfigure($save_index+4, -state=>($Tk::VERSION > 804) ? 'normal' : 'disabled'); # all paths
1348 &set_fit_button('fit');
1349 display_page($this);
1350 project_state(0);
1351 $fefftabs->raise('Interpretation');
1352
1353 unless ($response eq 'No paths') {
1354 ## fetch list of feffNNNN.dat files
1355 opendir D, $feff_folder or die "cannot read directory $feff_folder\n";
1356 @nnnnlist = sort grep /feff\d{4}\.dat/i, readdir D;
1357 closedir D;
1358 ($#nnnnlist = 0) if ($response eq 'Just the first');
1359 ($#nnnnlist = $config{paths}{firstn}-1) if (($response =~ /^The first/) and
1360 ($#nnnnlist > $config{paths}{firstn}-1));
1361
1362 ## fetch all the paths to import
1363 my $i = 0;
1364 foreach my $f (sort {lc($a) cmp lc($b)} (@nnnnlist)) {
1365 (!$i) or ($i % 10) or Echo("Reading the ${i}th feffNNNN.dat file");
1366 my $kid = fetch_nnnn($this, $feff_folder, $f);
1367 $list -> entryconfigure($kid, -style=>$list_styles{$paths{$kid}->pathstate("enabled")},
1368 -text=>$paths{$kid}->get('lab'));
1369 ++$i;
1370 };
1371 };
1372 intrp_fill($this);
1373
1374 if ($err eq 11) {
1375 Error("See the message buffer for a warning about that Feff calculation.");
1376 } else {
1377 Echo("All done running FEFF.");
1378 };
1379 ## return the cwd and unbusy
1380 chdir $was;
1381 $is_busy or $top -> Unbusy();
1382 };
1383
1384
1385 ## cloning feff calculations: link and copy
1386
1387 sub clone_feff {
1388 my $type = $_[0];
1389 return unless (ref($paths{$current}) =~ /Ifeffit/);
1390 return if ($paths{$current}->type eq 'gsd');
1391 my @calcs = grep { (ref($paths{$_}) =~ /Ifeffit/) and ($paths{$_}->type eq 'feff') } (&path_list);
1392 my $data = $paths{$current}->data;
1393 my $clone = $calcs[0];
1394
1395 ## select a feff calculation to clone
1396 my $db = $top -> DialogBox(-title => "Clone a Feff calculation",
1397 -buttons => [qw(OK Cancel)],
1398 -default_button => 'OK');
1399 $db -> add('Label',
1400 -text => "Which Feff calculcation would you like",
1401 -font => $config{fonts}{med},
1402 -foreground => $config{colors}{activehighlightcolor},)
1403 -> pack();
1404 $db -> add('Label',
1405 -text => "to $type and add to " . $paths{$data}->get('lab') . "?",
1406 -font => $config{fonts}{med},
1407 -foreground => $config{colors}{activehighlightcolor},)
1408 -> pack();
1409 foreach my $c (@calcs) {
1410 $db -> add('Radiobutton', -text=>$paths{$c}->descriptor(),
1411 -value=>$c,
1412 -variable=>\$clone)
1413 -> pack(-anchor=>'w');
1414 };
1415 &posted_Dialog;
1416 my $answer = $db -> Show;
1417 Echo("Not cloning Feff calculation"), return if ($answer eq 'Cancel');
1418 Echo("Cloning Feff calculation");
1419
1420 ## assign an id to the clones feff calc and make its object
1421 my $id = $data . '.feff' . $n_feff;
1422 my $project_feff_dir = "";
1423 $paths{$id} = Ifeffit::Path -> new(id => $id,
1424 group => $id,
1425 type => 'feff',
1426 data => $data,
1427 lab => 'FEFF'.$n_feff,
1428 family => \%paths,
1429 linkto => ($type eq 'copy') ? 0 : $clone,
1430 atoms_atoms => []);
1431 ## clone the properties of the feff calc, including all the atoms_ properties
1432 foreach my $k (qw(intrp path include mode edge central lab
1433 feff.inp feff.run atoms.inp paths.dat misc.dat file.dat)) {
1434 $paths{$id} -> make($k => $paths{$clone}->get($k));
1435 };
1436 foreach my $a (keys %{$paths{$clone}}) {
1437 next unless ($a =~ /^atoms_/);
1438 $paths{$id} -> make($a => $paths{$clone}->get($a));
1439 };
1440 my @autoparams;
1441 $#autoparams = 6;
1442 (@autoparams = autoparams_define($id, $n_feff, 1, 0)) if $config{autoparams}{do_autoparams};
1443 $paths{$id} -> make(autoparams=>[@autoparams]);
1444
1445 ## a link uses the files from an existing feff calculation, a copy
1446 ## copies all files from the other feff calc
1447 if ($type eq 'copy') {
1448 $project_feff_dir = &initialize_feff($id);
1449 $paths{$id} -> make(path=>$project_feff_dir);
1450 opendir F, $paths{$clone}->get('path');
1451 my @list = grep { -f File::Spec->catfile($paths{$clone}->get('path'),$_) } readdir F;
1452 closedir F;
1453 map { copy(File::Spec->catfile($paths{$clone}->get('path'),$_),
1454 $project_feff_dir) } @list;
1455 };
1456
1457 ## put the clone in the paths list
1458 $list -> add($id, -text=>$paths{$id}->{lab}, -style=>$list_styles{noplot});
1459 $list -> setmode($id, 'close');
1460 $list -> setmode($paths{$data}->get('id'), 'close')
1461 if ($list -> getmode($paths{$data}->get('id')) eq 'none');
1462
1463 ## clone each of the paths corresponding to this path
1464 foreach my $p (&path_list) {
1465 next unless (ref($paths{$p}) =~ /Ifeffit/);
1466 next unless ($paths{$p}->type eq 'path');
1467 next unless ($paths{$p}->get('parent') eq $clone);
1468 ## add the cloned path to the list and make its object
1469 my $kid = $list -> addchild($id);
1470 $paths{$kid} = Ifeffit::Path -> new(id => $kid,
1471 type => 'path',
1472 parent => $id,
1473 plotpath => 0,
1474 do_k => 1,
1475 data => $data,
1476 family => \%paths);
1477 foreach my $k (qw(3rd 4th amp_array deg delr dphase e0 edge ei
1478 element feff header include intrpline
1479 k_array lab label nleg phase_array reff s02
1480 setpath sigma^2 zcwif)) {
1481 $paths{$kid} -> make($k => $paths{$p}->get($k));
1482 };
1483 $list -> entryconfigure($kid,
1484 -style=>$list_styles{$paths{$kid}->pathstate},
1485 -text=>$paths{$kid}->get('lab'));
1486 };
1487
1488 ## display this newly linked feff calculation
1489 ++$n_feff;
1490 $file_menu -> menu -> entryconfigure($save_index+6, -state=>'normal');
1491 $file_menu -> menu -> entryconfigure($save_index+4, -state=>($Tk::VERSION > 804) ? 'normal' : 'disabled'); # all paths
1492 display_page($id);
1493 project_state(0);
1494 };
1495
1496
1497 sub hide_branch {
1498 my ($path, $action) = @_;
1499 ##print join(" ", $path, $action), $/;
1500 if ($action eq '<Activate>') { # mouse button release
1501 if ($list -> getmode($path) eq 'close') {
1502 ## deselect any paths that are in the branch being closed
1503
1504 ## closing a feff branch
1505 if ($paths{$path}->type eq 'feff') {
1506 foreach my $p ($list->info('selection')) {
1507 next unless (ref($paths{$p}) =~ /Ifeffit/);
1508 next unless ($paths{$p}->type eq 'path');
1509 next unless ($paths{$p}->get('parent') eq $path);
1510 $list->selectionClear($p);
1511 };
1512
1513 ## closing a data branch
1514 } elsif ($paths{$path}->type eq 'data') {
1515 foreach my $p ($list->info('selection')) {
1516 my $pp = $p;
1517 ($pp = $1 . "_" . ("fit", "res", "bkg")[$2]) if $p =~ /(data\d)\.(\d)/;
1518 next unless (ref($paths{$pp}) =~ /Ifeffit/);
1519 if ($paths{$pp}->type eq 'path') {
1520 next unless ($paths{$pp}->data eq $path);
1521 $list->selectionClear($p);
1522 } elsif ($paths{$pp}->type =~ '(bkg|fit|res)') {
1523 next unless ($paths{$pp}->get('sameas') eq $path);
1524 $list->selectionClear($p);
1525 };
1526 };
1527 };
1528
1529 $list -> close($path);
1530
1531 } elsif ($list -> getmode($path) eq 'open') {
1532 $list -> open($path);
1533 };
1534 };
1535 };
1536
1537 sub feff_compactify {
1538 my $feff = $_[0] || $paths{$current}->feff;
1539 Echo($paths{$feff}->descriptor . " is not a feff calculation or path"), return
1540 unless ($paths{$feff}->type =~ /(feff|path)/);
1541 Echo("Compacting " . $paths{$feff}->descriptor . " ...");
1542 my $folder = File::Spec->catfile($project_folder, $feff);
1543 my @to_delete;
1544 opendir F, $folder;
1545 foreach my $file (readdir F) {
1546 next if ($file =~ /^\./);
1547 next if (lc($file) =~ /^(atoms\.inp|f(eff(\.(inp|run)|_run\.log)|iles\.dat)|misc\.dat|path(00\.dat|s\.dat))$/);
1548 next if &path_used($folder, $file);
1549 push @to_delete, $file;
1550 };
1551 foreach my $td (@to_delete) {
1552 unlink File::Spec->catfile($folder, $td);
1553 };
1554 my $n = $#to_delete+1;
1555 Echo("Compacting " . $paths{$feff}->descriptor . " ... (deleted $n files) done!");
1556 };
1557
1558
1559 ## END OF THE SECTION ON THE FEFF PAGE
1560