1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
|
package Demeter::UI::Artemis::Project;
=for Copyright
.
Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
All rights reserved.
.
This file is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See The Perl
Artistic License.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
use strict;
use warnings;
use Carp;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
local $Archive::Zip::UNICODE = 1;
use Compress::Zlib;
use Cwd;
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec;
use List::MoreUtils qw(any);
use Text::Unidecode;
use YAML::Tiny;
use Safe;
use Wx qw(:everything);
use Demeter::UI::Wx::AutoSave;
use Const::Fast;
use Demeter::UI::Wx::SpecialCharacters qw($PLUSMN $PLUSMN2);
const my $PM => $PLUSMN2; # see GDS.pm, line ~61
require Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(save_project read_project modified close_project
autosave clear_autosave autosave_exists import_autosave
);
use File::Basename;
use File::Spec;
sub save_project {
my ($rframes, $fname) = @_;
## make sure we are fully up to date and serialised
my ($abort, $rdata, $rpaths) = Demeter::UI::Artemis::uptodate($rframes);
my $rgds = $rframes->{GDS}->reset_all(1,0);
my @data = @$rdata;
my @paths = @$rpaths;
my @gds = @$rgds;
my @vpaths = $rframes->{Plot}->{VPaths}->fetch_vpaths;
## get name, fom, and description + other properties
$rframes->{main} -> {currentfit} = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)")
if (not $rframes->{main} -> {currentfit});
Demeter::UI::Artemis::update_order_file();
$rframes->{main} -> {currentfit} -> set(data => \@data, paths => \@paths, gds => \@gds, vpaths => \@vpaths);
#my $save = $rframes->{main} -> {currentfit} -> fitted;
#$rframes->{main} -> {currentfit} -> fitted(1);
$rframes->{main} -> {currentfit} -> serialize(tree => File::Spec->catfile($rframes->{main}->{project_folder}, 'fits'),
folder => $rframes->{main}->{currentfit}->group,
nozip => 1,
copyfeff => 0,
);
#$rframes->{main} -> {currentfit} -> fitted($save);
Demeter->Touch(File::Spec->catfile($rframes->{main}->{project_folder}, 'fits', $rframes->{main} -> {currentfit}->group, 'keep'));
foreach my $k (keys(%$rframes)) {
next unless ($k =~ m{\Afeff});
$rframes->{$k}->make_page('Feff') if not $rframes->{$k}->{Feff};
next if (ref($rframes->{$k}->{Feff}->{feffobject}) !~ m{Feff});
my $file = File::Spec->catfile($rframes->{$k}->{Feff}->{feffobject}->workspace, 'atoms.inp');
mkpath $rframes->{$k}->{Feff}->{feffobject}->workspace if (! -d $rframes->{$k}->{Feff}->{feffobject}->workspace);
$rframes->{$k}->{Atoms}->save_file($file) if $rframes->{$k}->{Atoms}->{used};
};
if ((not $fname) or ($fname =~ m{\<untitled\>})) {
my $fd = Wx::FileDialog->new( $rframes->{main}, "Save project file", cwd, q{artemis.fpj},
"Artemis project (*.fpj)|*.fpj|All files (*)|*",
wxFD_SAVE|wxFD_CHANGE_DIR|wxFD_OVERWRITE_PROMPT);
if ($fd->ShowModal == wxID_CANCEL) {
$rframes->{main}->status("Saving project canceled.");
return;
};
$fname = $fd->GetPath;
#return if $::app->{main}->overwrite_prompt($fname); # work-around gtk's wxFD_OVERWRITE_PROMPT bug (5 Jan 2011)
};
my $po = $rframes->{main} -> {currentfit}->po;
$po -> serialize(File::Spec->catfile($rframes->{main}->{plot_folder}, 'plot.yaml'));
open(my $IN, '>'.File::Spec->catfile($rframes->{main}->{plot_folder}, 'indicators.yaml'));
foreach my $j (1..5) {
my $this = $rframes->{Plot}->{indicators}->{'group'.$j};
my $found = Demeter->mo->fetch('Indicator', $this);
print($IN $found -> serialization) if $found;
};
close $IN;
open(my $JO, '>'.File::Spec->catfile($rframes->{main}->{project_folder}, 'journal'));
print $JO $rframes->{Journal}->{journal}->GetValue;
close $JO;
opendir(my $FD, File::Spec->catfile($rframes->{main}->{project_folder}, 'fits'));
my @toss = ();
foreach my $d (readdir($FD)) {
next if ($d =~ m{\A\.});
next if -e File::Spec->catfile($rframes->{main}->{project_folder}, 'fits', $d, 'keep');
push @toss, $d; # gather all fits that lack the keep file
}; # so these can be excluded from the saved project
closedir $FD;
my $toss_regexp = join("|", '_not_a_real_folder_', @toss);
my $zip = Archive::Zip->new();
$zip->addTree( $rframes->{main}->{project_folder}, "", sub{ not m{\.sp$} and not m{$toss_regexp} });
carp('error writing zip-style project') unless ($zip->writeToFileNamed( $fname ) == AZ_OK);
undef $zip;
if ($fname !~ m{autosave\z}) {
clear_autosave();
Demeter->push_mru("artemis", File::Spec->rel2abs($fname));
&Demeter::UI::Artemis::set_mru;
$rframes->{main}->{projectname} = basename($fname, '.fpj');
$rframes->{main}->{projectpath} = File::Spec->rel2abs($fname);
$rframes->{main}->status("Saved project as ".$rframes->{main}->{projectpath});
modified(0);
};
};
sub autosave {
return if $Demeter::UI::Artemis::noautosave;
return if not Demeter->co->default("artemis", "autosave");
my $main = $Demeter::UI::Artemis::frames{main};
$main->status("Performing autosave ...", 'wait');
unlink $main->{autosave_file};
my $name = $_[0] || $main->{name}->GetValue;
$name =~ s{\s+}{_}g;
$name ||= "artemis";
$main->{autosave_file} = File::Spec->catfile(Demeter->stash_folder, $name.'.autosave');
save_project(\%Demeter::UI::Artemis::frames, $main->{autosave_file});
$main->status("Autosave done!");
};
sub clear_autosave {
return if not Demeter->co->default("artemis", "autosave");
my $main = $Demeter::UI::Artemis::frames{main};
unlink $main->{autosave_file};
$main->status("Removed autosave file.");
};
sub autosave_exists {
opendir(my $stash, Demeter->stash_folder);
my @list = readdir $stash;
closedir $stash;
return any {$_ =~ m{autosave\z} and $_ !~ m{\AAthena}} @list;
};
sub import_autosave {
my $dialog = Demeter::UI::Wx::AutoSave->new($Demeter::UI::Artemis::frames{main});
$Demeter::UI::Artemis::frames{main}->status("There are no autosave files."), return
if ($dialog == -1);
$dialog->SetFocus;
if( $dialog->ShowModal == wxID_CANCEL ) {
$Demeter::UI::Artemis::frames{main}->status("Autosave import canceled.");
opendir(my $stash, Demeter->stash_folder);
my @list = grep {$_ =~ m{autosave\z} and $_ !~ m{\AAthena}} readdir $stash;
closedir $stash;
foreach my $as (@list) { unlink File::Spec->catfile(Demeter->stash_folder, $as) };
} else {
my $this = File::Spec->catfile(Demeter->stash_folder, $dialog->GetStringSelection);
read_project(\%Demeter::UI::Artemis::frames, $this);
unlink $this;
## need to clean up the corresponding folder, if it has been left behind
};
};
## other data types:
## * empirical standard
## * structural unit
## * molecule
sub read_project {
my ($rframes, $fname) = @_;
my $debug = 0;
my $statustype = ($debug) ? 'wait' : 'wait|nobuffer';
my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
if (not $fname) {
my $fd = Wx::FileDialog->new( $rframes->{main}, "Import an Artemis project or data", cwd, q{},
"Artemis project or data (*.fpj;*.prj;*.inp;*.cif)|*.fpj;*.prj;*.inp;*.cif|" .
"Artemis project (*.fpj)|*.fpj|" .
"Athena project (*.prj)|*.prj|".
"old-style Artemis project (*.apj)|*.apj|".
"Demeter serializations (*.dpj)|*.dpj|".
"Feff or crystal data (*.inp;*.cif)|*.inp;*.cif|".
"chi(k) column data (*.chi;*.dat)|*.chi;*.dat|".
"All files (*)|*",
wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
wxDefaultPosition);
if ($fd->ShowModal == wxID_CANCEL) {
$rframes->{main}->status("Project import canceled.");
return;
};
$fname = $fd->GetPath;
if ($fname =~ m{[^[:ascii:]]}) {
$unidecoded = 1;
$deunifile = Demeter->unicopy($fname);
$rframes->{main}->status("Moved $fname to $deunifile");
$original = $fname;
$fname = $deunifile;
};
} else {
chdir(dirname($fname));
};
$fname = Demeter->follow_link($fname);
if (Demeter->is_zipproj($fname,0, 'any')) {
if (project_started($rframes)) {
my $yesno = Demeter::UI::Wx::VerbDialog->new($rframes->{main},, -1,
"Save current project before opening a new one?",
"Save project?",
"Save", 1);
my $result = $yesno->ShowModal;
if ($result == wxID_CANCEL) {
$rframes->{main}->status("File import canceled");
return;
};
save_project($rframes) if $result == wxID_YES;
close_project($rframes, 1);
};
};
## ---------- other input types ----------------------------------------------------------
if (not Demeter->is_zipproj($fname,0, 'fpj')) {
Demeter::UI::Artemis::Import('feff', $fname), return if (Demeter->is_feff($fname) or Demeter->is_atoms($fname) or Demeter->is_cif($fname));
Demeter::UI::Artemis::Import('old', $fname), return if (Demeter->is_zipproj($fname,0,'apj'));
Demeter::UI::Artemis::Import('prj', $fname), return if (Demeter->is_prj($fname) or Demeter->is_json($fname));
Demeter::UI::Artemis::Import('chi', $fname), return if (Demeter->is_data($fname));
Demeter::UI::Artemis::Import('dpj', $fname), return if (Demeter->is_zipproj($fname,0,'dpj'));
$rframes->{main}->status("$fname is not recognized as any kind of input data for Artemis", 'error');
return;
};
my $busy = Wx::BusyCursor->new();
$rframes->{main}->status("Importing project (please be patient, it may take a while...)", "wait");
# my ($volume,$directories,$fl) = File::Spec->splitpath( $rframes->{main}->{project_folder} );
# $directories =~ s{\\}{/}g;
# $directories .= $fl.'/';
# print join("|", $volume,$directories), $/;
my $wasdir = cwd;
my $projfolder = $rframes->{main}->{project_folder};
chdir $projfolder;
## ---------- unpack project file --------------------------------------------------------
$rframes->{main}->status("Opening project file $fname.", $statustype);
my $zip = Archive::Zip->new();
carp("Error reading project file $fname"), return 1 unless ($zip->read($fname) == AZ_OK);
foreach my $f ($zip->members) {
$zip->extractMember($f);
};
chdir($wasdir);
my $import_problems = q{};
%Demeter::UI::Artemis::fit_order = ();
my $order_file = File::Spec->catfile($projfolder, 'order');
my $order = Demeter->slurp($order_file);
$order =~ s{\'(\d+)\':}{$1:}g; # see comment just above update_order_file in Artemis.pm
open(my $O, '>', $order_file);
print $O $order;
close $O;
eval {local $SIG{__DIE__} = sub {}; %Demeter::UI::Artemis::fit_order = YAML::Tiny::LoadFile($order_file)};
## -------- plot and indicator yamls, journal
$rframes->{main}->status('Setting plot parameters, indicators, & journal', $statustype);
my $py = File::Spec->catfile($rframes->{main}->{plot_folder}, 'plot.yaml');
if (-e $py) {
my $rhash = {};
eval {local $SIG{__DIE__} = sub {}; $rhash = YAML::Tiny::LoadFile($py)};
delete $rhash->{nindicators};
delete $rhash->{version};
Demeter->po->set(%$rhash);
$rframes->{Plot}->populate;
};
my $iy = File::Spec->catfile($rframes->{main}->{plot_folder}, 'indicators.yaml');
if (-e $iy) {
my @list = YAML::Tiny::LoadFile($iy);
$rframes->{Plot}->{indicators}->populate(@list);
#my $rlist = [];
#eval {local $SIG{__DIE__} = sub {}; $rlist = YAML::Tiny::LoadFile($iy)};
#$rframes->{Plot}->{indicators}->populate(@$rlist);
};
my $journal = File::Spec->catfile($rframes->{main}->{project_folder}, 'journal');
if (-e $journal) {
$rframes->{Journal}->{journal}->SetValue(Demeter->slurp($journal));
};
## -------- import feff calculations from the project file
my %feffs;
my $feffdir = File::Spec->catfile($projfolder, 'feff/');
my @dirs = ();
if (-d $feffdir) {
opendir(my $FEFF, $feffdir);
@dirs = grep { $_ =~ m{\A[a-z]} } readdir($FEFF);
closedir $FEFF;
};
foreach my $d (@dirs) {
## import feff yaml
my $yaml = File::Spec->catfile($projfolder, 'feff', $d, $d.'.yaml');
my $source;
if (-e $yaml) {
my $gz = gzopen($yaml, 'rb');
my ($yy, $buffer);
$yy .= $buffer while $gz->gzreadline($buffer) > 0 ;
my @refs = YAML::Tiny::Load($yy);
$source = $refs[0]->{source};
} else {
rmtree(File::Spec->catfile($projfolder, 'feff', $d));
next;
};
my $feffobject = ($source eq 'aggregate') ?
Demeter::Feff::Aggregate->new(group=>$d) :
Demeter::Feff->new(group=>$d); # force group to be the same as before.
my $where = Cwd::realpath(File::Spec->catfile($feffdir, $d));
if (-e $yaml) {
my $gz = gzopen($yaml, 'rb');
my ($yy, $buffer);
$yy .= $buffer while $gz->gzreadline($buffer) > 0 ;
my @refs = YAML::Tiny::Load($yy);
$feffobject->read_yaml(\@refs, $where);
};
$rframes->{main}->status("Unpacking Feff calculation: ".$feffobject->name, $statustype);
if (not $feffobject->hidden) {
## import atoms.inp
my $atoms = File::Spec->catfile($projfolder, 'feff', $d, 'atoms.inp');
my ($fnum, $ifeff) = Demeter::UI::Artemis::make_feff_frame($rframes->{main}, $atoms, $feffobject->name); #, $feffobject);
if (-e $yaml) {
## import feff.inp
my $feff = File::Spec->catfile($projfolder, 'feff', $d, $d.'.inp');
my $text = $feffobject->slurp($feff);
$rframes->{$fnum}->make_page('Feff') if not $rframes->{$fnum}->{Feff};
# $rframes->{$fnum}->make_page('Paths') if not $rframes->{$fnum}->{Paths};
$rframes->{$fnum}->{Feff}->{feff}->SetValue($text);
## make Feff frame
$feffobject -> workspace(File::Spec->catfile($projfolder, 'feff', $d));
$feffs{$d} = $feffobject;
$rframes->{$fnum}->{Feff}->{feffobject} = $feffobject;
$rframes->{$fnum}->{Feff}->fill_intrp_page($feffobject);
$rframes->{$fnum}->{notebook}->ChangeSelection(2);
$rframes->{$fnum}->{Feff}->fill_ss_page($feffobject);
$rframes->{$fnum}->{Feff} ->{name}->SetValue($feffobject->name);
# $rframes->{$fnum}->{Paths}->{name}->SetValue($feffobject->name);
$rframes->{$fnum}->status("Imported crystal and Feff data from ". basename($fname));
};
# $rframes->{$fnum}->Show(0);
# $rframes->{$fnum}->Iconize(1);
# $rframes->{main}->{$fnum}->SetValue(0);
my $label = $rframes->{main}->{$fnum}->GetLabel;
$label =~ s{Hide}{Show};
$rframes->{main}->{$fnum}->SetLabel($label)
};
};
## -------- import fit history from project file
@dirs = (); # need to retrieve in historical order for fit history
foreach my $d (sort {$a<=>$b} grep {$_ =~ m{\A\d+\z}} keys(%{$Demeter::UI::Artemis::fit_order{order}})) {
next if $d eq 'current';
push @dirs, $Demeter::UI::Artemis::fit_order{order}{$d};
};
my $current = $Demeter::UI::Artemis::fit_order{order}{current};
$current = $Demeter::UI::Artemis::fit_order{order}{$current};
$current ||= $dirs[$#dirs];
my $currentfit;
my $lastfit;
my @fits;
## explanation:
## the list of fits in a project file includes some that have been fitted and some
## that have not. it is likely the one marked as the current fit has not been
## fitted. all unfitted fits are destroyed except for the current. all fitted
## fits are pushed onto the history and the current fit (fitted or not) is restored
my $count = 1;
my $folder;
foreach my $d (@dirs) {
my $fit = Demeter::Fit->new(group=>$d, interface=>"Artemis (Wx $Wx::VERSION)");
$rframes->{main}->status("Importing fit #$count into history", $statustype);# if not $count % 3;
my $regen = ($d eq $current) ? 0 : 1;
next if (not -d File::Spec->catfile($projfolder, 'fits', $d));
$fit->grab(folder=> File::Spec->catfile($projfolder, 'fits', $d), regenerate=>0); #$regen);
#$fit->deserialize(folder=> File::Spec->catfile($projfolder, 'fits', $d), regenerate=>0); #$regen);
if (($d ne $current) and ((not $fit->fitted) or ($fit->chi_square == 0))) { # discard the ones that don't actually involve a performed fit
$fit->DEMOLISH;
next;
};
$folder = File::Spec->catfile($projfolder, 'fits', $d);
++$count;
push @fits, $fit;
};
## ------- find most recent fit that was actually fit and make it current
#my $toss = 0;
#foreach my $f (reverse @fits) {
# if ($f->fitted and ($f->chi_square > 0)) {
# $current = $f->group;
# last;
# };
# ++$toss;
#};
#pop @fits foreach (1..$toss);
if (@fits) { # found some actual fits
$rframes->{main}->status("Found fit history, creating history window", $statustype);
my $found = 0;
foreach my $fit (@fits) { # take care that the one labeled as current actually exists, if not use the latest
++$found, last if ($fit->group eq $current);
};
$current = $fits[-1]->group if not $found;
foreach my $fit (@fits) {
## this attempts to weed out fit objects/folders for which the
## fit did not run to completion due to failure of sanity checks
if ($fit->fitted and ($fit->chi_square > 0)) {
$rframes->{History}->{list}->AddData($fit->name, $fit);
$rframes->{History}->add_plottool($fit);
Demeter->Touch(File::Spec->catfile($rframes->{main}->{project_folder}, 'fits', $fit->group, 'keep'));
$lastfit = $fit;
} elsif ($fit->group ne $current) {
foreach my $g ( @{ $fit->gds }) {
$g->DEMOLISH;
};
};
next unless ($fit->group eq $current);
$currentfit = $fit;
$fit->sentinal(sub{$::app->{main}->status($_[0], $statustype)});
$rframes->{main}->status("Unpacking current fit ".$fit->group, $statustype);
$currentfit->deserialize(folder=> $folder, regenerate=>0); #$regen);
#$rframes->{History}->{list}->SetSelection($rframes->{History}->{list}->GetCount-1);
#$rframes->{History}->OnSelect;
$fit->sentinal(sub{1});
$rframes->{main}->{currentfit} = $fit;
$rframes->{Plot}->{limits}->{fit}->SetValue(1);
my $current = $fit->number || 1;
#++$current;
};
};
$rframes->{main}->status("Restoring fit", $statustype);
$import_problems .= restore_fit($rframes, $currentfit, $lastfit);
## when each fit is deserialized, new GDS objects are instantiated
## for each one. for many projects, this means that many GDS
## objects then have the same name. the simplest solution to this
## problem is to just destroy all the GDS parameters and
## re-instantiate the ones from the current fit which was just
## restored. this is a bit wasteful, but GDS objects are small and
## quick to work with.
# foreach my $g (reverse @{ $currentfit->mo->GDS }) {
# delete($rframes->{GDS}->{grid}->{$g->name}) if exists($rframes->{GDS}->{grid}->{$g->name});
# $g->DEMOLISH;
# };
# $currentfit->gds( $rframes->{GDS}->reset_all );
if ($import_problems) {
Wx::MessageDialog->new($Demeter::UI::Artemis::frames{main}, $import_problems, "Warning!", wxOK|wxICON_WARNING) -> ShowModal;
};
if ($unidecoded) {
unlink $fname;
$fname = $original;
};
Demeter->push_mru("artemis", $fname);
&Demeter::UI::Artemis::set_mru;
$rframes->{main}->{projectpath} = $fname;
$rframes->{main}->{projectname} = unidecode(basename($fname, '.fpj'));
$rframes->{main}->status("Imported project $fname.");
# my $newfit = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)");
# $rframes->{main} -> {currentfit} = $newfit;
#++$Demeter::UI::Artemis::fit_order{order}{current};
modified(0);
undef $busy;
};
sub restore_fit {
my ($rframes, $fit, $lastfit) = @_;
$lastfit ||= $fit;
if (not defined($fit)) {
return q{It seems that you have not actually performed a fit in this fitting project.};
};
my $import_problems = q{};
##print '>>>>', $lastfit->group, $/;
$lastfit->deserialize(folder=>File::Spec->catfile($::app->{main}->{project_folder}, 'fits', $lastfit->group));
## -------- load up the GDS parameters
my $fom = 0;
my $grid = $rframes->{GDS}->{grid};
my $start = $rframes->{GDS}->find_next_empty_row;
$rframes->{main}->status("Restoring GDS parameters", 'wait|nobuffer');
foreach my $g (@{$fit->gds}) {
##print $g->write_gds;
if ($start >= $grid->GetNumberRows) {
$grid -> AppendRows(1,1);
$rframes->{GDS}->initialize_row( $grid->GetNumberRows - 1 );
};
$g->gds('skip') if (Demeter->is_larch and ($g->gds eq 'restrain'));
$grid -> SetCellValue($start, 0, $g->gds);
$grid -> SetCellValue($start, 1, $g->name);
if ($g->gds eq 'guess') {
my $me = (defined $g->initial and ($g->initial !~ m{\A\s*\z})) ? $g->initial : $g->mathexp;
#$me ||= $g->mathexp;
$grid -> SetCellValue($start, 2, $rframes->{GDS}->display_value($me));
} else {
my $me = (defined $g->initial and ($g->initial !~ m{\A\s*\z})) ? $g->initial : $g->mathexp;
$grid -> SetCellValue($start, 2, $rframes->{GDS}->display_value($me));
};
$grid -> {$g->name} = $g;
my $text = q{};
if ($g->bestfit or $g->error) {
if ($g->gds eq 'guess') {
$text = sprintf("%.5f %s %.5f", $g->bestfit, $PM, $g->error);
} elsif ($g->gds =~ m{(?:after|def|penalty|restrain)}) {
$text = sprintf("%.5f", $g->bestfit);
} elsif ($g->gds =~ m{(?:lguess|merge|set|skip)}) {
1;
};
};
$grid -> SetCellValue($start, 3, $text);
$rframes->{GDS}->set_type($start);
++$start;
};
$rframes->{GDS}->reset_all;
$fom = $fit->fom+1;
Demeter->mo->currentfit($fom);
my ($name, $description) = (q{}, q{});
$name = ($fit->name =~ m{\A\s*Fit\s+\d+\z}) ? 'Fit '.$fit->mo->currentfit : $fit->name;
$description = $fit->description;
$rframes->{main}->{name}->SetValue($name);
$rframes->{main}->{description}->SetValue($description);
## -------- Data and Paths
my $count = 0;
$rframes->{main}->status("Restoring data", 'wait|nobuffer');
foreach my $d (@{$fit->data}) {
##print $d->name, $/;
my ($dnum, $idata) = Demeter::UI::Artemis::make_data_frame($rframes->{main}, $d);
$rframes->{$dnum}->{pathlist}->DeletePage(0) if (($rframes->{$dnum}->{pathlist}->GetPage(0) =~ m{Panel})
and # take care in case of a project with data but no paths
(@{$fit->paths}));
#my $first = $rframes->{$dnum}->{pathlist}->GetPage(0);
#($first->DeletePage(0)) if (ref($first) =~ m{Panel});
my $datapaths = 0;
$rframes->{main}->status("Restoring paths for ".$d->name, 'wait|nobuffer');
foreach my $p (@{$fit->paths}) {
my $feff = (ref($p) =~ m{FPath}) ? $p : $fit -> mo -> fetch('Feff', $p->parentgroup);
$p->set(file=>q{}, update_path=>1);
$p->set(folder=>$feff->workspace) if (ref($p) !~ m{FPath});
next if ($p->data ne $d);
++$datapaths;
$p->parent($feff);
$p -> sp($p -> mo -> fetch('ScatteringPath', $p->spgroup)) if ($p->spgroup and not $p->sp);
my $page = Demeter::UI::Artemis::Path->new($rframes->{$dnum}->{pathlist}, $p, $rframes->{$dnum});
##Demeter->pjoin($p->name, $p->include, $p->delr);
my $n = $rframes->{$dnum}->{pathlist}->AddPage($page, $p->label, 1, 0);
$page->include_label;
$rframes->{$dnum}->{pathlist}->Check($n, $p->mark);
## disable atoms and feff run buttons for any Feff calc contributing paths to this fit
foreach my $key (keys %Demeter::UI::Artemis::frames) {
next if $key !~ m{feff};
my $frame = $Demeter::UI::Artemis::frames{$key};
next if ($p->parentgroup ne $frame->{Feff}->{feffobject}->group);
$frame->{Feff}->{toolbar}->EnableTool($frame->{Feff}->{ffid},0);
$frame->{Atoms}->{toolbar}->EnableTool($frame->{Atoms}->{atid},0);
$frame->{Atoms}->{toolbar}->EnableTool($frame->{Atoms}->{aggid},0);
};
};
$rframes->{$dnum}->{pathlist}->SetSelection(0) if $datapaths; #($#{$fit->paths} > -1);
$rframes->{$dnum}->Show(0);
$rframes->{main}->{$dnum}->SetValue(0);
if (not $count) {
$rframes->{$dnum}->Show(1);
$rframes->{main}->{$dnum}->SetValue(1);
(my $lab = $rframes->{main}->{$dnum}->GetLabel) =~ s{Show}{Hide};;
$rframes->{main}->{$dnum}->SetLabel($lab);
};
++$count;
};
## -------- VPaths from currentfit
$rframes->{main}->status("Restoring VPaths", 'wait|nobuffer') if ($#{$lastfit->vpaths} > -1);
foreach my $vp (@{$lastfit->vpaths}) {
$rframes->{Plot}->{VPaths}->{vpathlist}->Append($vp->name, $vp);
$vp->_update('all');
};
## -------- labels and suchlike
$rframes->{Log}->{name} = $fit->name;
$rframes->{Log}->put_log($lastfit);
$rframes->{Log}->SetTitle("Artemis [Log] " . $fit->name);
$rframes->{Log}->Show(0);
$rframes->{main}->{log_toggle}->SetValue(0);
if ($fit->happiness) {
Demeter::UI::Artemis::set_happiness_color($fit->color);
} else {
$rframes->{main}->{fitbutton} -> SetBackgroundColour(Wx::Colour->new($fit->co->default("happiness", "average_color")));
};
return $import_problems;
};
sub discard_fit {
my ($rframes) = @_;
$rframes->{GDS}->discard_all(1);
foreach my $d (keys %$rframes) {
next if $d !~ m{data};
$rframes->{$d}->discard_data(1);
};
};
sub find_sp {
my ($path, $rfeffs) = @_;
foreach my $f (values %$rfeffs) {
foreach my $sp (@{ $f->pathlist }) {
return $sp if ($path->spgroup eq $sp->group);
};
};
return q{};
};
sub modified {
my ($is_modified) = @_;
my $main = $Demeter::UI::Artemis::frames{main};
my $title = ($is_modified)
? 'Artemis [EXAFS data analysis] - *' . $main->{projectname} . '*'
: 'Artemis [EXAFS data analysis] - ' . $main->{projectname};
$main->{modified} = ($is_modified);
$main->SetTitle($title);
};
sub close_project {
my ($rframes, $force) = @_;
if (not $force) {
my $yesno = Demeter::UI::Wx::VerbDialog->new($rframes->{main}, -1,
"Save this project before closing?",
"Save project?",
'Save', 1);
my $result = $yesno->ShowModal;
if ($result == wxID_CANCEL) {
$rframes->{main}->status("Not closing project.");
return 0;
};
save_project($rframes) if $result == wxID_YES;
};
Demeter::UI::Artemis::set_happiness_color(Demeter->co->default("happiness", "average_color"))
if (exists $rframes->{main} -> {currentfit});
## -------- clear GDS
$rframes->{GDS}->discard_all(1);
$rframes->{GDS}->Show(0);
$rframes->{main}->{toolbar}->ToggleTool(1,0);
## -------- clear Log
$rframes->{Log}->{text}->SetValue(q{});
$rframes->{Log}->Show(0);
$rframes->{main}->{log_toggle}->SetValue(0);
## -------- clear all Data
foreach my $k (keys %$rframes) {
next unless ($k =~ m{data});
$rframes->{$k}->discard_data(1);
};
## -------- clear all Feff
foreach my $k (keys %$rframes) {
next unless ($k =~ m{feff});
Demeter::UI::Artemis::discard_feff($k, 1);
};
my $feffdir = File::Spec->catfile($rframes->{main}->{project_folder}, 'feff/');
my @dirs = ();
if (-d $feffdir) {
opendir(my $FEFF, $feffdir);
@dirs = grep { $_ =~ m{\A[a-z]} } readdir($FEFF);
closedir $FEFF;
};
foreach my $d (@dirs) {
rmtree(File::Spec->catfile($feffdir, $d));
};
## -------- clear all Paths
my @list = @{Demeter->mo->Path};
foreach my $p (@list) {
$p->DEMOLISH;
};
Demeter->mo->reset_path_index;
## -------- clear all Fits
@list = @{Demeter->mo->Fit};
foreach my $f (@list) {
$f->DEMOLISH;
};
## -------- clear plotting list
$rframes->{Plot}->{plotlist}->ClearAll;
## -------- clear history
$rframes->{History}->{list}->ClearAll;
$rframes->{History}->{log}->Clear;
$rframes->{History}->{params}->Clear;
$rframes->{History}->{params}->Append("Statistcal parameters");
$rframes->{History}->{params}->Select(0);
$rframes->{History}->{report}->Clear;
my $plottoolbox = Wx::BoxSizer->new( wxVERTICAL );
$rframes->{History}->{plottool} -> DestroyChildren;
$rframes->{History}->{plottool} -> SetSizer($plottoolbox, 1);
$rframes->{History}->{plottool} -> SetScrollbars(20, 20, 50, 50);
$rframes->{History}->{scrollbox} = $plottoolbox;
my $fittree = File::Spec->catfile($rframes->{main}->{project_folder}, 'fits');
rmtree($fittree);
mkdir $fittree;
## -------- clear Journal
$rframes->{Journal}->{journal}->SetValue(q{});
unlink File::Spec->catfile($rframes->{main}->{project_folder}, 'journal');
## -------- clear Fit text boxes
$rframes->{main}->{name}->SetValue(q{});
$rframes->{main}->{description}->SetValue(q{});
$rframes->{main}->{fitspace}->[1]->SetValue(1);
$rframes->{main}->{cvcount} = 0;
## -------- clear project name
$rframes->{main}->{projectname} = q{<untitled>};
$rframes->{main}->{projectpath} = q{};
modified(0);
return 1;
};
sub project_started {
my ($rframes) = @_;
my ($ndata, $nfeff, $ngds) = (0,0,0);
foreach my $f (keys %$rframes) {
++$ndata if ($f =~ m{data});
++$nfeff if ($f =~ m{feff});
};
my $grid = $rframes->{GDS}->{grid};
foreach my $row (0 .. $grid->GetNumberRows-1) {
++$ngds if ($grid->GetCellValue($row, 1) !~ m{\A\s*\z});
};
return $ndata || $nfeff || $ngds;
};
1;
=head1 NAME
Demeter::UI::Artemis::Project - Import and export Artemis project files
=head1 VERSION
This documentation refers to Demeter version 0.9.26.
=head1 SYNOPSIS
Import and export Artemis project files.
=head1 CONFIGURATION
=head1 DEPENDENCIES
Demeter's dependencies are in the F<Build.PL> file.
=head1 BUGS AND LIMITATIONS
Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)
Patches are welcome.
=head1 AUTHOR
Bruce Ravel, L<http://bruceravel.github.io/home>
L<http://bruceravel.github.io/demeter/>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
|