Debian Codegraph @

artemis_parts/project.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2006, 2008, 2010 Bruce Ravel
3 ##
4
5
6
7 ###===================================================================
8 ### project description subsystem
9 ###===================================================================
10
11 ## arg 1: flag for querying user for a file name
12 ## arg 2: autosave flag
13 sub save_project {
14 my ($query, $auto) = @_;
15 ##local $Tk::FBox::a;
16 ##local $Tk::FBox::b;
17
18 ## --- respond to an autosave event
19 ## autosave disabled
20 return if ($auto and (lc($config{general}{autosave_policy}) eq 'none'));
21 ## realsave rather than autosave
22 ($auto = 0) if ($auto and (lc($config{general}{autosave_policy}) eq 'realsave'));
23 my $file;
24 if ($auto) {
25 $file = $autosave_filename;
26 Echo("Performing autosave ...");
27 } elsif ($query or (not $project_name)) {
28 my $path = $current_data_dir || cwd;
29 my ($init, $suffix) = ('artemis.apj', '');
30 ($init, $path, $suffix) = fileparse($project_name) if $project_name;
31 my $types = [['Artemis projects', '.apj'],
32 ['All Files', '*'],];
33 $file = $top -> getSaveFile(-filetypes=>$types,
34 ##(not $is_windows) ?
35 ## (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
36 -initialdir=>$path,
37 -initialfile=>$init,
38 -title => "Artemis: Save project");
39 return unless ($file);
40 my $name = &push_mru($file, 1, "project");
41 #my ($name, $pth, $suff) = fileparse($file);
42 #$current_data_dir = $pth;
43 $project_name = $file; # reset deault project name
44 Echo("Saving project ...");
45 } else {
46 $file = $project_name;
47 Echo("Saving project ...");
48 };
49
50 my $dir = dirname($file);
51 if (-e $dir and -w $dir) {
52 Error("Cannot write to \"$file\". Check the permissions of that file or directory."), return if (-e $file and not -w $file);
53 };
54
55 $top -> Busy;
56
57 if (($paths{$current}->type eq 'feff') and ($paths{$current}->{mode} > 1)) {
58 ## save the feff.inp file
59 my $feff_file = File::Spec->catfile($project_folder, $paths{$current}->get('id'), "feff.inp");
60 Echo("Saving feff.inp file");
61 $widgets{feff_inptext} -> Save($feff_file);
62 };
63
64 read_titles if ($current_canvas eq 'op');
65
66 my $description = &save_description;
67 ##@-fp-@ save_fingerprint($description);
68
69 ## save the journal
70 open J, ">".File::Spec->catfile($project_folder, "descriptions", "journal.artemis");
71 print J $notes{journal}->get(qw(1.0 end));
72 close J;
73
74 ## zip up project folder
75 &zip_project($file);
76 $top -> Unbusy;
77 if ($auto) {
78 Echo("Performing autosave ... done!");
79 } else {
80 unlink $autosave_filename, Echo("Removed stale autosave file.")
81 if (-e $autosave_filename);
82 Echo("Saved project to $file.");
83 };
84 project_state(1) unless $auto;
85 };
86
87 sub save_description {
88 $Data::Dumper::Indent = 0;
89
90 my $description = File::Spec->catfile($project_folder, 'descriptions', 'artemis');
91
92 ## keep one level of backup
93 ##rename($description, $description.".bak") if (-f $description);
94
95 open PROJ, ">".$description or do {
96 Error("You cannot write the artemis description file."); return
97 };
98 print PROJ "# Artemis project file -- Artemis version $Ifeffit::Path::VERSION\n";
99 print PROJ $paths{data0} -> project_header($project_folder);
100
101 ### loop over all data and save parameters and titles
102 ##my @data = ('gsd', &every_data);
103 my @data = (&every_data);
104 foreach my $p (@data) {
105 my @titles;
106 if ($paths{$p}->type eq 'data') {
107 foreach my $t (split(/\n/, $paths{$p}->get('titles'))) {
108 push @titles, $t;
109 };
110 };
111 my @args;
112 foreach my $k (keys %{$paths{$p}}) {
113 next if ($k eq 'titles');
114 next if ($k eq 'inc_mapping');
115 next if ($k eq 'included');
116 next if ($k eq 'from_project');
117 next if ($k eq 'autoparams');
118 next if ($k eq 'family');
119 next if ($k =~ 'do_[krq]');
120 ## these next two prevent saving difference spectrum information
121 ## to the project
122 next if ($k =~ 'diff_(list|mapping|paths)');
123 next if ($k =~ '(fit|made)_diff');
124 push @args, $k, $paths{$p}->get($k);
125 };
126 print PROJ
127 Data::Dumper->Dump([$p], [qw/old_path/]), $/,
128 Data::Dumper->Dump([\@args], [qw/*args/]), $/;
129 ($p =~ /^data\d+$/) and print PROJ Data::Dumper->Dump([\@titles], [qw/*strings/]), $/;
130 print PROJ "[record]", $/, $/;
131 };
132
133 ##my @allkeys = (keys %paths);
134 my @allkeys = &path_list;
135 ## save the feff's and paths
136 foreach my $path (&all_feff) {
137 # my $path = "data" . $nd . ".feff" . $nf;
138 next unless (exists $paths{$path});
139 my @args;
140 foreach my $k (keys %{$paths{$path}}) { # save the feff parent
141 next if ($k eq 'titles');
142 next if ($k eq 'intrp');
143 next if ($k eq 'from_project');
144 next if ($k eq 'family');
145 push @args, $k, $paths{$path}->get($k);
146 };
147 print PROJ
148 Data::Dumper->Dump([$path], [qw/old_path/]), $/,
149 Data::Dumper->Dump([\@args], [qw/*args/]), $/,
150 "[record]", $/, $/;
151 #my @paths = grep {/feff$nf\.\d+/} @allkeys;
152 my @paths = grep {/$path\.\d+/} @allkeys;
153 # and save all the path children of this parent
154 foreach my $p (@paths) {
155 my @args;
156 foreach my $k (keys %{$paths{$p}}) {
157 next if ($k eq 'titles');
158 next if ($k eq 'header');
159 next if ($k eq 'group');
160 next if ($k eq 'fit_index');
161 next if ($k eq 'from_project');
162 next if ($k eq 'family');
163 push @args, $k, $paths{$p}->get($k);
164 };
165 my @header; # save headers separately
166 foreach my $h (split(/\n/, $paths{$p}->get('header'))) {
167 push @header, $h;
168 };
169 print PROJ
170 Data::Dumper->Dump([$p], [qw/old_path/]), $/,
171 Data::Dumper->Dump([\@args], [qw/*args/]), $/,
172 Data::Dumper->Dump([\@header], [qw/*strings/]), $/,
173 "[record]", $/, $/;
174 };
175 };
176
177 ## save the parameter list
178 foreach my $p (@gds) {
179 printf PROJ ("\@parameter = ('%s','%s','%s','%s',%d);\n",
180 $p->name, $p->type, $p->mathexp, $p->note, $p->autonote);
181 };
182 print PROJ "\n";
183
184 ## save the plotting options
185 print PROJ Data::Dumper->Dump([\%plot_features], [qw/*plot_features/]), $/, $/;
186
187 ## save the extra plotting features
188 my @this;
189 @this[0..5] = @extra[0..5];
190 $this[5] = "";
191 foreach (7 .. $#extra) {
192 $this[$_]->[0] = $extra[$_]->[1];
193 $this[$_]->[1] = $extra[$_]->[2];
194 };
195 print PROJ Data::Dumper->Dump([\@this], [qw/*extra/]), $/, $/;
196
197 ## my @journal;
198 ## foreach my $j (split(/\n/, $notes{journal}->get(qw(1.0 end)))) {
199 ## push @journal, $j;
200 ## };
201 ## print PROJ Data::Dumper->Dump([\@journal], [qw/*journal/]), $/, $/;
202
203 ## save the properties
204 print PROJ Data::Dumper->Dump([\%props], [qw/*props/]), $/, $/;
205
206 print PROJ "\n1;\n# Local Variables:\n# truncate-lines: t\n# End:\n";
207 close PROJ;
208 $Data::Dumper::Indent = 2;
209 return $description;
210
211 };
212
213 sub save_fingerprint {
214 my $description = $_[0];
215 local( $/, *P );
216 open( P, $description ) or die "could not open $description for fingerprinting\n";
217 my $fingerprint = md5_hex(<P>);
218 close P;
219 my $folder = dirname($description);
220 open F, ">".File::Spec->catfile($folder, '...fp');
221 print F $fingerprint;
222 close F;
223 };
224
225 sub compare_fingerprint {
226 my ($fp_file, $description) = @_;
227 open( F, $fp_file ) or die "could not open $description for fp_file\n";
228 my $prior = <F>;
229 close F;
230 open( D, $description ) or die "could not open $description for fingerprinting\n";
231 my $this = md5_hex(<D>);
232 close D;
233 ##print " $prior $this\n";
234 return ($prior eq $this);
235 };
236
237
238 ## check to see if arg is a zip-style project
239 sub is_project {
240 return 0;
241 };
242
243
244 ## check the first line of a file to verify that it is a record
245 ## (i.e. old-style project)
246 sub is_old_project {
247 my $file = $_[0];
248 open F, $file or die "could not open $file as an Artemis project\n";
249 my $first = <F>;
250 close F;
251 return ($first =~ /Artemis project file/) ? 1 : 0;
252 };
253
254 sub is_athena_record {
255 my $file = $_[0];
256 open F, $file or die "could not open $file as an Athena record\n";
257 my $first = <F>;
258 close F;
259 if ($first =~ /Athena project file/) {
260 return -1;
261 } elsif ($first =~ /Athena record file/) {
262 return 1;
263 } else {
264 return 0;
265 };
266 };
267
268
269
270
271 sub date_of_file {
272 my $month = (qw/January February March April May June July
273 August September October November December/)[(localtime)[4]];
274 my $year = 1900 + (localtime)[5];
275 return sprintf "This file created at %2.2u:%2.2u:%2.2u on %s %s, %s",
276 reverse((localtime)[0..2]), (localtime)[3], $month, $year;
277 # ^^^ this gives hour:min:sec
278 };
279
280
281 sub open_project {
282 my $file = $_[0];
283 #my ($name, $pth, $suffix) = fileparse($file);
284 #$current_data_dir = $pth;
285 ## activate op widgets
286 map {($_ =~ /^op/) and $widgets{$_}->configure(-state=>'normal')} (keys %widgets);
287 map {$grab{$_}->configure(-state=>'normal')} (keys %grab);
288
289 my $is_busy = grep (/Busy/, $top->bindtags);
290 $top -> Busy unless $is_busy;
291 my $from_version;
292 if ($is_windows) {
293 Echo("Reading project file with direct evaluations");
294 $from_version = read_record_on_windows($file);
295 } else {
296 Echo("Reading project file in a safe compartment");
297 $from_version = read_record($file)
298 };
299 ## set $n_feff to one larger than the largest index used in this
300 ## project
301 my $nn = 0;
302 opendir P, $project_folder;
303 foreach (readdir P) {
304 next unless (/data\d+\.feff(\d)/);
305 ($nn = $1) if ($1 > $nn);
306 };
307 closedir P;
308 ++$nn;
309 $n_feff = $nn;
310
311 ## read journal into the journal space
312 do {
313 local $/ = undef;
314 open J, File::Spec->catfile($project_folder, "descriptions", "journal.artemis");
315 $notes{journal}->insert('1.0', <J>);
316 close J;
317 };
318
319 ## insert sets and guesses
320 repopulate_gds2();
321
322 ## try to clean up the problem of null entries
323 foreach my $p (keys %paths) {
324 next if (ref($paths{$p}) =~ /Ifeffit/);
325 delete $paths{$p};
326 Echo("Null path $p discarded.");
327 };
328 foreach my $d (keys %paths) {
329 next if ($paths{$d}->type =~ /(feff|gsd|path)/);
330 #next unless ($paths{$d}->type =~ /(bkg|data|diff|fit|res)/);
331 $paths{$d} -> make(do_k=>1);
332 };
333 foreach my $p (keys %paths) { # flag paths for updating
334 next unless ($paths{$p}->type eq 'path');
335 $paths{$p} -> make(do_k=>1);
336 };
337
338 unless ($from_version =~ /DR/) {
339 # fix up order of paths for projects from before 0.5.002
340 if (($from_version < 0.5002) or ($from_version > 2000)) {
341 Echo("Fixing project file from before version 0.5.002");
342 my @allkeys = (keys %paths);
343 my @feffs = grep {/feff\d+$/} @allkeys;
344 foreach my $f (@feffs) {
345 my $n = substr($f, 4);
346 my @paths = grep {/feff$n\.\d+/} @allkeys;
347 @paths = sort {$paths{$a}->get('lab') cmp $paths{$b}->get('lab')} @paths;
348 my @cache;
349 foreach my $p (@paths) { # remember the mapping between the old and new objects
350 push @cache, $paths{$p};
351 };
352 foreach my $p (@paths) { # delete 'em all from the list
353 $list -> delete('entry', $p);
354 };
355 foreach my $p (@paths) { # replace 'em all in the list and map old to new
356 my $kid = $list ->
357 addchild($f, -text=>$paths{$p}->get('lab'),
358 -style=>$list_styles{$paths{$p}->pathstate});
359 $paths{$kid} = shift @cache;
360 $paths{$kid} -> make(id=>$kid);
361 };
362 foreach my $p (@paths) { # get rid of the old objects
363 delete $paths{$p};
364 };
365 };
366 Echo("Fixing project file from before version 0.5.002 ... done!");
367 };
368 };
369
370 $fefftabs -> raise("Interpretation");
371
372 ## --- look for situation of a fits/fits0001 folder that is not a
373 ## real project fit folder from 0.7.010
374 my $is_old_fits = 1;
375 $is_old_fits = 0 if ($from_version =~ /DR/);
376 $is_old_fits = 0 if ($from_version =~ /^0.8/);
377 if ($is_old_fits and (-d File::Spec->catfile($project_folder, "fits", "fit0001"))) {
378 Echo("Cleaning up fit directory from an old version of Artemis that does not support fit history.");
379 rmtree(File::Spec->catfile($project_folder, "fits", "fit0001"), 0, 0);
380 };
381
382 ## --- is this project old enough not to have a tmp/ folder?
383 mkdir File::Spec->catfile($project_folder, "tmp") unless (-d File::Spec->catfile($project_folder, "tmp"));
384
385 ## --- is this project old enough not to have a readme file?
386 my $readme_file = $paths{data0} -> find('artemis', 'readme');
387 copy($readme_file, File::Spec->catfile($project_folder, "README"))
388 if ((-e $readme_file) and (! -e File::Spec->catfile($project_folder, "README")));
389
390 ## --- look for old fits in the fits folder
391 opendir F, File::Spec->catfile($project_folder, "fits");
392 my @fits = sort( grep {/fit\d+/ and -d File::Spec->catfile($project_folder, "fits", $_)} readdir(F) );
393 closedir F;
394
395 ## restore label to the fit branch according to whether the last
396 ## operation involving this data set was a fit or a sum
397 if (@fits) {
398 foreach my $d (&all_data) {
399 my $fsfile = File::Spec->catfile($project_folder, "fits", $fits[$#fits], "$d.fs");
400 my $lastfit = 'fit';
401 if (-e $fsfile) {
402 open FS, $fsfile;
403 $lastfit = <FS>;
404 $lastfit =~ s/\s$//; # remove the carriage return if it's there
405 close FS;
406 };
407 $list -> entryconfigure($d.".0", -text=>($lastfit =~ /fit/) ? "Fit" : "Sum");
408 };
409 };
410
411 foreach my $f (@fits) {
412 open L, File::Spec->catfile($project_folder, "fits", $f, 'label');
413 my $label = <L>;
414 $label =~ s/\s$//; # remove the carriage return if it's there
415 close L;
416 $fit{count} = sprintf("%d", substr($f,3));
417 $fit{count_full} ||= $fit{count};
418 my $log = Ifeffit::ArtemisLog -> new(File::Spec->catfile($project_folder, "fits", $f, 'log'));
419
420 foreach my $d (&all_data) {
421 ##$list -> entryconfigure($d.".0", -text=>"Fit [$fit{count_full}]");
422 my $fname = $d . ".fit";
423 ## handle situation of this data not having this fit
424 next unless (-e File::Spec->catfile($project_folder, "fits", $f, $fname));
425 $paths{$d.'.0'} ||= Ifeffit::Path -> new(id => $d.".0",
426 type => 'fit',
427 group => $d.'_fit',
428 sameas => $d,
429 lab => 'Fit',
430 parent => 0,
431 family => \%paths);
432 $list -> show('entry', $d.".0");
433 ##my $id = $list->addchild($d.".0");
434 $list->add($d.".0.".$fit{count});
435 my $id = $d.".0.".$fit{count};
436 $paths{$id} = Ifeffit::Path -> new(id => $id,
437 type => 'fit',
438 group => $d.'_fit_'.$fit{count},
439 sameas => $d,
440 lab => $label,
441 value => $log->get('Figure of merit') || 0,
442 folder => $f,
443 filename => $fname,
444 parent => $d.".0",
445 family => \%paths);
446 $paths{$d.".0"} -> make(thisfit=>$id);
447 $list -> entryconfigure($id, -style=>$list_styles{enabled}, -text=>$label);
448 $paths{$id} -> make(fitfile=>File::Spec->catfile($project_folder, "fits", $f, $fname));
449 my $bname = File::Spec->catfile($project_folder, "fits", $f, $d.".bkg");
450 $paths{$id} -> make(bkgfile=>(-e $bname) ? $bname : "");
451 $paths{$id} -> make(resfile=>File::Spec->catfile($project_folder, "fits", $f, $d.".res"));
452 ## restore FT and fit parameters
453 my $ftfile = File::Spec->catfile($project_folder, "fits", $f, $d.".FT");
454 if (-e $ftfile) {
455 open FT, $ftfile;
456 foreach (<FT>) {
457 chomp;
458 my ($k, $v) = split(/=/, $_);
459 $v =~ s/\s//g; # remove the carriage return if it's there
460 $paths{$id} -> make($k=>$v);
461 };
462 close FT;
463 } else { # this is a project from before the FT file existed, so
464 # use the corresponding data's parameters
465 foreach my $k (qw(kmin kmax dk kwindow rmin rmax dr rwindow)) {
466 $paths{$id} -> make($k => $paths{$d}->get($k));
467 };
468 };
469 };
470 undef $log;
471 };
472
473 ## is at least one data set included in the fit?
474 my $ok = 0;
475 foreach my $d (&all_data) {
476 $ok++ if $paths{$d}->get('include');
477 };
478 ## if not, turn on data0
479 unless ($ok) {
480 $paths{data0}->make(include=>1);
481 toggle_data('data0');
482 #foreach my $p (keys %paths) {
483 # next unless ($paths{$p}->get('parent') eq 'data0');
484 # next unless ($paths{$p}->type eq 'feff');
485 # $paths{$p}->make(include=>1);
486 #};
487 };
488
489 ## display data
490 my $first = &first_data;
491 $widgets{op_titles} -> delete('1.0', 'end');
492 $widgets{op_titles} -> insert('end', $paths{$first}->get('titles'));
493 display_page($first);
494 $file_menu->menu->entryconfigure($save_index, -state=>'normal'); # data
495 $file_menu -> menu -> entryconfigure($save_index+6, -state=>'normal');
496 $file_menu -> menu -> entryconfigure($save_index+4, -state=>($Tk::VERSION > 804) ? 'normal' : 'disabled'); # all paths
497 (-e $paths{$first}->get('file')) and plot('r', 0);
498 $top -> Unbusy unless $is_busy;
499 Echo("Read project description \`$file\'.");
500 project_state(1);
501 };
502
503
504
505
506 sub read_path {
507 my ($r_old_path, $r_args, $r_strings) = @_;
508 my $group;
509 ## ...................................................................
510 ## need to clean up old_path string for projects from before 0.5.009
511 ## we can be confident that a project from that era is a single data
512 ## set project. all that should be necessary is appending "data0"
513 ## to the old_path string.
514 ($$r_old_path = 'data0.' . $$r_old_path) if ($$r_old_path =~ /^feff/);
515 ## ...................................................................
516
517 if ($$r_old_path eq 'gsd') { # --- GSD ---
518 $paths{gsd} = Ifeffit::Path -> new(id => 'gsd',
519 type => 'gsd',
520 from_project => 1,
521 family => \%paths);
522 while (@$r_args) {
523 my ($k, $v) = (shift @$r_args, shift @$r_args);
524 next if ($k =~ /^(id|type)$/);
525 $paths{gsd} -> make($k=>$v);
526 };
527 foreach my $p (@{$paths{gsd}->{order}}) {
528 push @gds, Ifeffit::Parameter->new(name => $p,
529 type => $paths{gsd}->{$p}->{choice},
530 mathexp => $paths{gsd}->{$p}->{value},
531 bestfit => '',
532 error => '',
533 modified => 0,
534 note => "$p: ");
535 };
536 } elsif ($$r_old_path =~ /^data\d+$/) { # --- DATA ---
537 my $this = $$r_old_path; ##'data' . $n_data++;
538 $paths{$this} = Ifeffit::Path -> new(id => $this,
539 group => $this,
540 type => 'data',
541 sameas => 0,
542 from_project => 1,
543 family => \%paths);
544 $paths{$this.".0"} = Ifeffit::Path -> new(id => $this.".0",
545 type => 'fit',
546 from_project => 1,
547 group => $this."_fit",
548 sameas => $this, lab=>'Fit',
549 parent => 0,
550 family => \%paths);
551 my $saw_include = 0; # deal with project files pre-0.6.000
552 while (@$r_args) {
553 my ($k, $v) = (shift @$r_args, shift @$r_args);
554 next if ($k =~ /^(id|type|group|sameas|chib)$/); # chib is an old,
555 # discarded atribute
556 ($saw_include = 1) if ($k eq 'include');
557 if ($k eq 'kweight') { # treat kweight specially for backwards
558 SWITCH: { # compatability for before 0.5.009
559 $paths{$this} -> make(k1=>1), last SWITCH if ($v eq 1);
560 $paths{$this} -> make(k2=>1), last SWITCH if ($v eq 2);
561 $paths{$this} -> make(k3=>1), last SWITCH if ($v eq 3);
562 $paths{$this} -> make(karb_use=>1, karb=>$v);
563 };
564 } else {
565 $paths{$this} -> make($k=>$v);
566 };
567 };
568 $paths{$this} -> make(pcpath=>'None') unless exists $paths{$paths{$this}->get('pcpath')};
569 $paths{$this} -> make(include=>1) unless $saw_include;
570 $paths{$this} -> make(lab=>"Data") unless $paths{$this}->get('lab');
571
572 unless ($this eq 'data0') {
573 my $style = ($paths{$this}->get('include')) ? $list_styles{enabled} : $list_styles{disabled};
574 $list -> add($this, -text=>$paths{$this}->get('lab'), -style=>$style);
575
576 $list -> add($this.".0", -text=>'Fit', -style=>$style,);
577 $list -> setmode($this.'.0', 'close');
578 $list -> hide('entry', $this.".0");
579 };
580 $list -> entryconfigure($paths{$this}->get('id'), -text=>$paths{$this}->get('lab'));
581 $list -> setmode($paths{$this}->get('id'), 'close');
582 $paths{$this}->make(use_bkg=>0);
583 $temp{op_include} = 1;
584 $temp{op_plot} = $paths{$this}->get('plot') || 0;
585 ++$n_data;
586 my $group = $paths{$this}->get('group');
587
588 if ($paths{$this}->get('file')) {
589 ## fixy up path information to accommodate zip-style projects
590 my $fl = $paths{$this}->get('file');
591 $fl =~ s/\\+/\//g; # convert \ to /
592 $paths{$this}->make(file=>$fl);
593 my $thisfile = basename($paths{$this}->get('file'));
594 $paths{$this} -> make(file=>File::Spec->catfile($project_folder, "chi_data", $thisfile));
595
596 my $file = $paths{$this}->get('file');
597 if (-e $file) {
598 if (&is_athena_record($file) == 1) {
599 Echo("Reading $file as an Athena record");
600 if ($is_windows) {
601 read_athena_record_on_windows($file, 1, 0);
602 } else {
603 read_athena_record($file, 1, 0);
604 };
605 } else {
606 $paths{$this} -> dispose("read_data(file=\"$file\", type=chi, group=$group)\n", 1);
607 };
608 };
609 };
610 # solve a problem lingering from early-version project files, this
611 # problem will only exist in a single data set project file
612 if (lc($paths{data0}->get('pcedge')) =~ /([a-z]{1,2}) (k|l(1|2|3|i{1,3}))/) {
613 $paths{data0} -> make(pcelem=>ucfirst($1), pcedge=>ucfirst($2));
614 };
615 my $titles = "";
616 my @all_titles = @$r_strings;
617 #($#all_titles = 25) if ($#all_titles > 25);
618 map {$titles .= $_."\n"} (@all_titles);
619 $paths{$this} -> make(titles=>$titles);
620 $fit{count_full} = $paths{$this}->get('count_full');
621 } elsif ($$r_old_path =~ /^(data\d+)\.(bkg|diff|fit|res)/) {
622 1; ## why is there one of these in the project??
623 } elsif ($$r_old_path =~ /feff(\d+)$/) { # --- FEFF CALC ---
624 my $this = $$r_old_path;
625 ($n_feff < $1) and ($n_feff = $1+1); # bump up feff counter
626 $paths{$this} = Ifeffit::Path -> new(id => $this,
627 type => 'feff',
628 group => $this,
629 from_project => 1,
630 family => \%paths,);
631 while (@$r_args) {
632 my ($k, $v) = (shift @$r_args, shift @$r_args);
633 next if ($k =~ /^(id|type|group)$/);
634 ## the next line addresses a change in the atoms portion of the
635 ## feff calc data structure that happened when the atoms page
636 ## was redesigned in DR004
637 next if ($k =~ /atoms_(elem|tag|occ|x|y|z)/);
638 $paths{$this} -> make($k=>$v);
639 };
640 ## fixy up path information to accommodate zip-style projects
641 if ($paths{$this}->get('linkto')) {
642 my $l = $paths{$this}->get('linkto');
643 $paths{$this} -> make(path=>$paths{$l}->get('path'));
644 } else {
645 $paths{$this} -> make(path=>File::Spec->catfile($project_folder, $paths{$this}->get('id'), ""));
646 };
647 my $mode = 0;
648 if (-e $paths{$this}->get('atoms.inp')) {
649 $mode += 1;
650 &import_atoms($paths{$this}->get('atoms.inp'), 1, $this);
651 };
652 $paths{$this}->verify_feffinp;
653 $mode += 2 if (-e $paths{$this}->get('feff.inp'));
654 $paths{$this} -> make(lab=>'FEFF'.$n_feff) unless ($paths{$this}->get('lab'));
655 $paths{$this} -> make(include=>1) unless (exists $paths{$this}->{include});
656 my $parent = $paths{$this}->data;
657 my $style = ($paths{$parent}->get('include')) ? $list_styles{noplot} : $list_styles{noplotdis};
658 $list -> add($paths{$this}->get('id'), -text=>$paths{$this}->get('lab'), -style=>$style);
659 my $intrp_ok = &do_intrp($this);
660 $mode += 4 if $intrp_ok;
661 $paths{$this} -> make(mode=>$mode);
662 $list -> setmode($paths{$this}->get('id'), 'close');
663 ## does this project predate autoparams??
664 unless ($paths{$this}->get('autoparams')) {
665 my @autoparams;
666 $#autoparams = 6;
667 (@autoparams = autoparams_define($this, $n_feff, 0, 1)) if $config{autoparams}{do_autoparams};
668 $paths{$this} -> make(autoparams=>[@autoparams]);
669 };
670 $n_feff = $1+1;
671 ## feff parents do not have strings
672 } elsif ($$r_old_path =~ /((data\d+)\.feff\d+)\.(\d+)/) { # --- PATH ----
673 my $this = $$r_old_path;
674 my $kid = $list -> addchild($1);
675 $paths{$kid} = Ifeffit::Path -> new(id => $kid,
676 type => 'path',
677 parent => $1,
678 data => $2,
679 from_project => 1,
680 intrpline => "",
681 zcwif => 0,
682 family => \%paths);
683 while (@$r_args) {
684 my ($k, $v) = (shift @$r_args, shift @$r_args);
685 next if ($k =~ /^(id|type|group|parent|data)$/);
686 $paths{$kid} -> make($k=>$v);
687 };
688 my $data = $paths{$kid}->data;
689 ($paths{$kid}->make(deg => int $paths{$kid}->get('deg'))) if
690 ($paths{$kid}->get('deg') == int $paths{$kid}->get('deg'));
691 my $fi = substr($paths{$kid}->get('file'),4,4);
692 $paths{$kid} -> make(feff_index=>sprintf('%d', $fi));
693 my $save_deg = $paths{$kid}->get('deg');
694 my $header = "";
695 map {$header .= $_."\n"} (@$r_strings);
696 my $file = File::Spec->catfile($paths{$kid}->get('path'), $paths{$kid}->get('feff'));
697 ## take care that the file actually exists...
698 unless (-e $file) {
699 delete $paths{$kid};
700 $list -> delete('entry', $kid);
701 return;
702 };
703 $paths{$kid} -> make(header=>nnnn_header($kid, $file),
704 deg=>$save_deg);
705 ## plotpath was introduced at DR006
706 $paths{$kid} -> make(plotpath=>0) unless $paths{$kid}->get('plotpath');
707 $paths{$kid} -> make(is_ss => 1) if ($paths{$kid}->get("nleg") == 2);
708 $paths{$kid} -> make(intrpline => $paths{$kid}->intrpline);
709 $paths{$kid}->make(is_col => 1) if ($paths{$kid}->get("intrpline") =~ /\d :/);
710 my $style = $list_styles{$paths{$kid}->pathstate};
711 $style = ($paths{$paths{$kid}->data}->get('include')) ? $style : $list_styles{disabled};
712 $list -> entryconfigure($kid,
713 -style=> $style,
714 -text => $paths{$kid}->get('lab')
715 );
716
717 };
718
719 };
720
721
722
723
724 sub delete_project {
725 my $restore = $_[0];
726 Echo("Closing project ... ");
727 my $is_busy = grep (/Busy/, $top->bindtags);
728 $top -> Busy unless $is_busy;
729
730 $widgets{athena_return} -> invoke() if ($current_canvas eq 'athena');
731
732 ## get rid of all $artemis_titleN strings
733 ifeffit("show \@strings\n");
734 my ($lines, @response) = (Ifeffit::get_scalar('&echo_lines')||0, ());
735 if ($lines) {
736 map {push @response, Ifeffit::get_echo()} (1 .. $lines);
737 };
738 foreach (@response) {
739 $paths{data0} -> dispose("erase $1") if (/^\s*(\$artemis_title\d+)/);
740 };
741
742 foreach my $d (&every_data) {
743 $paths{$d}->delete_titles;
744 unless ($paths{$d}->get('include')) {
745 $paths{$d}->make(include=>1);
746 &toggle_data($d);
747 };
748 };
749 my $first = first_data;
750 $list -> entryconfigure($first, -text=>'Data');
751 $list -> setmode($first, 'none');
752 $current = $first;
753 display_page($first);
754 $top->update;
755 clear_gds2();
756 ## erase data from ifeffit's memory
757 foreach my $k (keys %paths) {
758 next if ($k eq 'gsd');
759 next unless (ref($paths{$k}) =~ /Ifeffit/);
760 ## erase fits from tree
761 $list -> delete('offsprings', $k) if ($k =~ /^data\d+\.0$/);
762 delete_feff($k,1,2) if ($paths{$k}->type eq 'feff');
763 };
764 $paths{data0} -> dispose("erase \@paths all", $dmode);
765 $paths{data0} -> dispose("erase \@arrays", $dmode);
766 $paths{data0} -> drop_all;
767 $#gds = -1;
768 $gds_selected{type} = "";
769 $gds_selected{name} = "";
770 $gds_selected{mathexp} = "";
771 $gds_selected{which} = 0;
772
773 ## reset the extra plotting features
774 $extra[0] = 0;
775 $extra[1] = 0;
776 $extra[2] = 0;
777 $extra[3] = 0;
778 $extra[4] = 0;
779 $extra[5] = "";
780 $widgets{plot_extra} -> raise('main');
781
782 foreach my $d (&all_data) {
783 foreach ($list->infoChildren($d)) {
784 if (/data\d+$/) { # clear out data
785 1;
786 } elsif (/feff\d+$/) { # clear out feff calc
787 1; #$list->delete('entry',$_);
788 } elsif (/data(\d+)\.\d+/) { # clear out fit head diff, bkg, res
789 $list->delete('entry',$_) unless ($1 == 0);
790 };
791 };
792 clear_op($d);
793 };
794 $widgets{show_chi} -> invoke();
795 map {$widgets{$_} -> configure(-state=>'disabled')} (qw(show_chi show_mu));
796 ##--bkg-- clear_athena;
797 ##--bkg-- $widgets{data_notebook} -> raise('chi');
798 ##--bkg-- $widgets{data_notebook} -> pageconfigure('bkg', -state=>'disabled');
799 $list -> hide('entry', "data0.0");
800
801 ## undefine the paths hash, then reset globals
802 undef %paths;
803 ($n_feff, $n_data) = (0, 0);
804 map {($_ =~ /^op/) and $widgets{$_}->configure(-state=>'disabled')} (keys %widgets);
805 $paths{data0} = Ifeffit::Path -> new(id => 'data0',
806 group => 'data0',
807 type => 'data',
808 sameas => 0,
809 lab => 'Data',
810 kwindow => $config{data}{kwindow},
811 rwindow => $config{data}{rwindow},
812 family => \%paths);
813 $paths{gsd} = Ifeffit::Path -> new(id=>'gsd', type=>'gsd', family=>\%paths);
814 $list->focus();
815 project_state(1);
816 $project_name = "";
817 ## disable the save cascades
818 map { $file_menu->menu->entryconfigure($_, -state=>'disabled') }
819 ($save_index .. $save_index+4, $save_index+6);
820 $data_menu->menu->entryconfigure(2, -state=>'disabled');
821 $fit_menu ->menu->entryconfigure(4, -state=>'disabled');
822 $widgets{op_include} -> configure(-text=>"Include data in the fit?");
823 $widgets{op_plot} -> configure(-text=>"Plot data after the fit?");
824 map {$widgets{$_} -> deselect()} qw(op_do_bkg op_include op_plot); # op_use_bkg
825 map {($_ =~ /^op/) and $widgets{$_}->configure(-state=>'disabled')} (keys %widgets);
826 map {$grab{$_}->configure(-state=>'disabled')} (keys %grab);
827 ## then clear out the journal ...
828 $notes{journal}->delete(qw(1.0 end));
829 $widgets{atoms_titles}->delete(qw(1.0 end));
830 ## ... and reset the poperties
831 $props{'Project title'} = "<insert a title for your project here>";
832 $props{'Comment'} = "";
833 $props{'Prepared by'} = "<insert your name and/or the name of your computer here>";
834 ##$props{'Prepared by'} = ($is_windows) ? "<insert your name and/or the name of your computer here>" :
835 ## join("\@", $ENV{USER}, $ENV{HOST});
836 $props{Contact} = "<insert your email address and/or phone number here>";
837 $props{Environment} = (split(/\n/, $paths{data0} -> project_header))[1];
838 $props{Environment} =~ s/\# /Artemis $VERSION /;
839 $props{'Last fit'} = q{};
840 $props{'Information content'} = q{};
841 $props{'Project location'} = q{};
842 $props{Started} = $paths{data0} -> date_of_file;
843 ## clean up the GDS page
844 &gds2_clear_highlights;
845 ## delete autosave file
846 unlink $autosave_filename if (-e $autosave_filename);
847 Echo("Removed stale autosave file.");
848
849 if ($restore) {
850 Echo("Closing project ... done!");
851 return;
852 };
853
854 # finally, delete the stash directory
855 rmtree($project_folder);
856 ## $0 does icky, utf8-y things on windows due to the backslashes
857 if ($is_windows) {
858 $current_data_dir = $ENV{IFEFFIT_DIR};
859 } else {
860 ($current_data_dir = dirname($0)) if sub_directory($current_data_dir, $project_folder);
861 };
862 $project_folder = "";
863 ## then reset some global variables...
864 &set_temp;
865 %fit = (index=>1, count=>0, count_full=>0, new=>1, label=>"", comment=>"", fom=>0);
866 ## %log_params ???
867 ## and reinitialize the project
868 &initialize_project(0);
869 my $chdir_to = $current_data_dir || dirname($0) || $ENV{IFEFFIT_DIR};
870 $chdir_to = Cwd::abs_path($chdir_to);
871 chdir $chdir_to;
872 Echo("Closing project ... done!");
873 $top -> Unbusy unless $is_busy;
874 };
875
876
877 sub compactify_project {
878 Echo("Compacting project ...");
879 $top->Busy;
880 foreach my $p (keys %paths) {
881 next unless ($paths{$p}->type eq 'feff');
882 feff_compactify($p);
883 };
884 $top->Unbusy;
885 Echo("Compacting project ... done!");
886 project_state(0);
887 &display_properties if ($paths{$current}->type eq 'feff');
888 my $message = "All unused files from all Feff calculations have been deleted. This makes your project file much smaller.\n\nTo recover files that were deleted, simply rerun the Feff calculation and, when it finishes, choose the option to import no paths.";
889 &posted_Dialog;
890 my $dialog =
891 $top -> Dialog(-bitmap => 'info',
892 -text => $message,
893 -title => 'Artemis: Compacting project...',
894 -buttons => [qw/OK/],
895 -font => $config{fonts}{med},
896 -default_button => 'OK')
897 -> Show();
898 Echo(q{});
899 };
900
901
902 ## END OF THE PROJECT FILE SUBSYSTEM
903