File: Import.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (851 lines) | stat: -rw-r--r-- 28,293 bytes parent folder | download
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
package Demeter::UI::Artemis::Import;

#use Demeter; # qw(:plotwith=gnuplot);
use Demeter::UI::Artemis::Project;
use Demeter::UI::Wx::SpecialCharacters qw(:all);
use Demeter::UI::Common::Prj;

## -------- convenience parameters
#use Const::Fast;
my $rdemeter = \$Demeter::UI::Artemis::demeter;
my $rframes  = \%Demeter::UI::Artemis::frames;
my $make_data_frame = \&Demeter::UI::Artemis::make_data_frame;
my $make_feff_frame = \&Demeter::UI::Artemis::make_feff_frame;

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
local $Archive::Zip::UNICODE = 1;
use Carp;
use Chemistry::Elements qw(get_Z);
use Cwd;
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec;
use List::MoreUtils qw{any};
use Text::Unidecode;

use Wx qw(:everything);
use base qw( Exporter );
our @EXPORT = qw(Import prjrecord);


sub Import {
  my ($which, $fname, @args) = @_;
  my %args = @args;
  $args{postcrit} ||= Demeter->co->default('pathfinder', 'postcrit');
  my $retval = q{};
 SWITCH: {
    read_project($rframes, $fname),             last SWITCH if  ($which eq 'fpj');
    $retval = _prj($fname),                     last SWITCH if (($which eq 'prj') or ($which eq 'athena'));
    $retval = _old($fname),                     last SWITCH if  ($which eq 'old');
    $retval = _feff($fname),                    last SWITCH if  ($which eq 'feff');
    $retval = _external_feff($fname),           last SWITCH if  ($which eq 'external');
    $retval = _chi($fname),                     last SWITCH if  ($which eq 'chi');
    $retval = _dpj($fname, 0, $args{postcrit}), last SWITCH if  ($which eq 'dpj');
    $retval = _feffit($fname),                  last SWITCH if  ($which eq 'feffit');
  };
  $::app->heap_check;
  return $retval;
};


sub prjrecord {
  my ($fname, $choice) = @_;
  my $file = $fname;
  my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
  if (not $fname) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import an Athena project", cwd, q{},
				  "Athena project (*.prj)|*.prj|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("Data import canceled.");
      return;
    };
    $file = $fd->GetPath;
    if ($file =~ m{[^[:ascii:]]}) {
      $unidecoded = 1;
      $deunifile = Demeter->unicopy($file);
      $rframes->{main}->status("Moved $file to $deunifile");
      $original = $file;
      $file = $deunifile;
    };
  }
  $file = Demeter->follow_link($file);
  if ((not Demeter->is_prj($file)) and (not Demeter->is_json($file))) {
    $rframes->{main}->status("$file is not an Athena project file.", 'error');
    return (q{}, q{}, -1);
  };
  ##
  my $selection = 0;
  $rframes->{prj} =  Demeter::UI::Common::Prj->new($rframes->{main}, $file, 'single', $choice);
  my $result = $rframes->{prj} -> ShowModal;

  if (
      ($result == wxID_CANCEL) or     # cancel button clicked
      ($rframes->{prj}->{record} == -1)  # import button without selecting a group
     ) {
    return (q{}, q{}, 0);
  };

  return ($file, $rframes->{prj}->{prj}, $rframes->{prj}->{record}, $unidecoded, $original);
};

##############################################################################################################
## the "private" functions for specific import chores follow ...
##############################################################################################################

sub _check_number_of_data_sets {
  return 1 if Demeter->is_larch;
  my $max = Demeter->dd->fetch_scalar('&max_data_sets');
  my $count = 0;
  foreach my $k (sort keys(%Demeter::UI::Artemis::frames)) { # sorting will order them in their order in the Data list
    ++$count if ($k =~ m{\Adata});
  };
  ##$rframes->{main}->status(sprintf("%d data sets, %d max", $count, $max));
  return 1 if ($count < $max);
  $Demeter::UI::Artemis::frames{main}->status("You may not import more data.  Ifeffit only allows for $max data sets.", 'alert');
  return 0;
};


sub _prj {
  my ($fname) = @_;
  return 0 if (not _check_number_of_data_sets());
  my $choice = 1;
  if ($fname =~ m{(\s+<(\d?)>)\z}) {
    $choice = $2;
    $fname =~ s{$1}{};
  };
  my ($file, $prj, $record, $unidecoded, $original) = prjrecord($fname, $choice);

  if (defined($record) and ($record < 0)) {
    return;
  };
  if ((not $prj) or (not $record)) {
    $rframes->{main}->status("Data import canceled.");
    return;
  };

  my $data = $prj->record($record);
  $data->frozen(0);
  my $ref;
  my $toss = $data->bkg_stan;
  if ($data->bkg_stan ne 'None') {
    foreach my $i (0 .. $#{$prj->entries}) {
      if ($prj->entries->[$i]->[1] eq $data->bkg_stan) {
	$ref = $prj->record($i+1);
	$data->bkg_stan($ref->group);
      };
    };
    ## clean up a few straggler arrays in Ifeffit/Larch and the spare Data object
    #$data->dispense('process', 'erase', {items=>"\@group $toss"});
    #$toss = Demeter->mo->fetch('Data', $toss);
    #$toss->DESTROY;
  };

  ## refuse to move forward for actinides above Am
  if (get_Z($data->bkg_z) > 95) {
    $prj->DESTROY;
    $rframes->{prj} -> Destroy;
    delete $rframes->{prj};
    my $error = Wx::MessageDialog->new($rframes->{main},
				       "The version of Feff you are using cannot calculate for absorbers above Z=95.",
				       "Error importing data",
				       wxOK|wxICON_EXCLAMATION);
    my $result = $error->ShowModal;
    $rframes->{main}->status("The version of Feff you are using cannot calculate for absorbers above Z=95.", 'alert');
    return 0;
  };

  my ($dnum, $idata) = &$make_data_frame($rframes->{main}, $data);
  $data->po->start_plot;
  $data->plot('k');
  $rframes->{$dnum} -> Show(1);
  $rframes->{main}->{$dnum}->SetValue(1);
  (my $lab = $rframes->{main}->{$dnum}->GetLabel) =~ s{Show}{Hide};
  $rframes->{main}->{$dnum}->SetLabel($lab);
  $prj->DESTROY;
  $rframes->{prj} -> Destroy;
  delete $rframes->{prj};
  if ($unidecoded) {
    unlink $file;
    $file = $original;
  };
  Demeter->push_mru("athena", $file, $record);
  autosave();
  chdir dirname($file);
  $rframes->{main}->status("Importing data \"" . $data->name . "\" from $file.");
};


sub _feff {
  my ($fname) = @_;
  ## also yaml data
  my $file = $fname;
  my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
  if (not $file) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import crystal data", cwd, q{},
				  "input and CIF files (*.inp;*.cif)|*.inp;*.cif|input file (*.inp)|*.inp|CIF file (*.cif)|*.cif|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("Crystal/Feff data import canceled.");
      return;
    };
    $file = $fd->GetPath;
    if ($file =~ m{[^[:ascii:]]}) {
      $unidecoded = 1;
      $deunifile = Demeter->unicopy($file);
      $rframes->{main}->status("Moved $file to $deunifile");
      $original = $file;
      $file = $deunifile;
    };
  };
  if (not -e $file) {
    $rframes->{main}->status("$file does not exist.");
    return;
  };
  if (not (Demeter->is_feff($file) or Demeter->is_atoms($file) or Demeter->is_cif($file))) {
    $rframes->{main}->status("$file does not seem to be a Feff input file, an Atoms input file, or a CIF file", 'error');
    return;
  };

  my ($fnum, $ifeff) = &$make_feff_frame($rframes->{main}, $file);
  return if (not defined($fnum));
  $rframes->{$fnum} -> Show(1);
  autosave();
  if ($unidecoded) {
    unlink $file;
    $file = $original;
  };
  Demeter -> push_mru("feff", $file);
  chdir dirname($file);
  $rframes->{$fnum}->status("Imported crystal data from " . basename($file));
  $rframes->{main}->{$fnum}->SetValue(1);
};


sub _chi {
  my ($fname) = @_;
  return 0 if (not _check_number_of_data_sets());
  my $file = $fname;
  my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
  if (not $fname) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import $CHI(k) data", cwd, q{},
				  "Chi data (*.chi)|*.chi|Data files (*.dat)|*.dat|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("$CHI(k) import canceled.");
      return;
    };
    $file = $fd->GetPath;
    if ($file =~ m{[^[:ascii:]]}) {
      $unidecoded = 1;
      $deunifile = Demeter->unicopy($file);
      $rframes->{main}->status("Moved $file to $deunifile");
      $original = $file;
      $file = $deunifile;
    };
  };
  if (not Demeter->is_data($file)) {
    $rframes->{main}->status("$file is not a column data file.", 'error');
    return;
  };
  my $data = Demeter::Data->new(datatype=>'chi', file=>$file);
  $data->_update('data');
  my ($dnum, $idata) = &$make_data_frame($rframes->{main}, $data);
  $data->po->start_plot;
  $data->plot('k');
  $data->plot_window('k') if $data->po->plot_win;
  $rframes->{$dnum} -> Show(1);
  $rframes->{main}->{$dnum}->SetValue(1);
  autosave();
  if ($unidecoded) {
    unlink $file;
    $file = $original;
  };
  Demeter->push_mru("chik", $file);
  chdir dirname($file);
  $rframes->{main}->status("Imported $file as $CHI(k) data.");
};

sub _dpj {
  my ($fname, $nomru, $postcrit) = @_;
  my $file = $fname;
  my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
  $nomru ||= 0;
  if (not $fname) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import a Demeter fit serialization file", cwd, q{},
				  "Fit serialization (*.dpj)|*.dpj|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status(".dpj file import canceled.");
      return;
    };
    $file = $fd->GetPath;
    if ($file =~ m{[^[:ascii:]]}) {
      $unidecoded = 1;
      $deunifile = Demeter->unicopy($file);
      $rframes->{main}->status("Moved $file to $deunifile");
      $original = $file;
      $file = $deunifile;
    };
  };
  $file = Demeter->follow_link($file);
  if (not Demeter->is_zipproj($file,0, 'dpj')) {
    $rframes->{main}->status("$file is not a demeter fit serialization.", 'error');
    return;
  };
  if ($rframes->{main}->{modified}) {
    return if not close_project($rframes);
  };

  my $zip = Archive::Zip->new;
  if ($zip->read($file) != AZ_OK) {
    $rframes->{main}->status("$CHI(k) import canceled.");
    return;
  };
  if (not defined($zip->memberNamed('FIT.SERIALIZATION'))) {
    $rframes->{main}->status("$file is not a fit serialization.");
    return;
  };

  ## -------- make a new Fit object
  my $fit = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)");
  $rframes->{main}->{currentfit} = $fit;
  $rframes->{Plot}->{limits}->{fit}->SetValue(1);
  $fit->mo->currentfit(1);
  my $projfolder = $rframes->{main}->{project_folder};
  my $fitdir = File::Spec->catfile($projfolder, 'fits', $fit->group);
  mkpath($fitdir);

  foreach my $file ($zip->memberNames) {
  SWITCH: {
      ($file =~ m{([a-z]+)\.bin\z}) and do {
	#print "found feff $1\n";
	my $feffdir = File::Spec->catfile($rframes->{main}->{project_folder}, 'feff', $1);
	mkpath $feffdir;
	$zip->extractMember("$1.bin",   File::Spec->catfile($feffdir, "phase.bin"));
	$zip->extractMember("$1.files", File::Spec->catfile($feffdir, "files.dat"));
	$zip->extractMember("$1.yaml",  File::Spec->catfile($feffdir, "$1.yaml"));

	my $feffobject = Demeter::Feff->new(yaml=>File::Spec->catfile($feffdir, "$1.yaml"), group=>$1); # force group to be the same as before
	$feffobject -> workspace($feffdir);
	$feffobject -> postcrit($postcrit);
	$feffobject -> make_feffinp('full');
	my $feff = File::Spec->catfile($feffdir, "$1.inp");
	rename(File::Spec->catfile($feffdir, "feff.inp"), $feff);
	## import atoms.inp
	#my $atoms = File::Spec->catfile($projfolder, 'feff', $d, 'atoms.inp');
	my ($fnum, $ifeff) = &$make_feff_frame($rframes->{main}, q{}, $feffobject->name, $feffobject);

	## import feff.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
	$rframes->{$fnum}->{Feff}->{feffobject} = $feffobject;
	$rframes->{$fnum}->{Feff}->fill_intrp_page($feffobject);
	$rframes->{$fnum}->{notebook}->ChangeSelection(2);

	$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));
	my $label = $rframes->{main}->{$fnum}->GetLabel;
	$label =~ s{Hide}{Show};
	$rframes->{main}->{$fnum}->SetLabel($label);

	$rframes->{$fnum}->{Feff}->fill_ss_page($feffobject);
	last SWITCH;
      };
      (any {$file eq $_} (qw(plot.yaml vpaths.yaml paths.yaml fit.yaml gds.yaml log structure.yaml))) and do {
	$zip->extractMember($file,  File::Spec->catfile($fitdir, $file));
	last SWITCH;
      };
      ($file =~ m{([a-z]+)\.fit\z}) and do {
	$zip->extractMember("$1.fit",  File::Spec->catfile($fitdir, "$1.fit"));
	$zip->extractMember("$1.yaml", File::Spec->catfile($fitdir, "$1.yaml"));
      };
      ## skip Readme and FIT.SERIALIZATION
    };
  };
  $fit->deserialize(folder=>$fitdir, regenerate=>0); #$regen);
  my $import_problems .= Demeter::UI::Artemis::Project::restore_fit($rframes, $fit, $fit);
  if ($import_problems) {
    Wx::MessageDialog->new($rframes->{main}, $import_problems, "Warning!", wxOK|wxICON_WARNING) -> ShowModal;
  };
  if ($fit->fitted) {
    $rframes->{History}->{list}->AddData($fit->name, $fit);
      $rframes->{History}->add_plottool($fit);
    $rframes->{History}->{list}->SetSelection($rframes->{History}->{list}->GetCount-1);
    $rframes->{History}->OnSelect;
  };
  if ($unidecoded) {
    unlink $file;
    $file = $original;
  };
  if (not $nomru) {
    $fit->push_mru("fit_serialization", $file) ;
    $rframes->{main}->{projectpath} = $file;
    $rframes->{main}->{projectname} = unidecode(basename($file, '.dpj'));
    $rframes->{main}->status("Imported fit serialization $file.");
  };
  my $newfit = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)");
  $rframes->{main} -> {currentfit} = $newfit;
  modified(0);

};


## need better reaction in case of absent phase.bin file.
sub _external_feff {
  my ($fname, $noshow) = @_;
  my $file = $fname;
  $noshow ||= 0;

  my $message = <<EOH
Importing an external Feff calculation is usually a
bad idea!

If your external Feff calculation was made using a
different version of Feff than that used by Artemis,
then importing a Feff calculation is likely to fail
in ways that may crash Artemis.

If you rerun Feff after importing an external Feff
calculation, your fits are likely to fail in surprising
ways.

Atoms or Feff input files are typically imported by
selecting "Import project or data" from the File
menu and the execution of Feff is managed by Artemis.

Think carefully about whether you want to continue.
EOH
    ;
  my $okcancel = Wx::MessageDialog->new($datapage,
					$message,
					"Caution!",
					wxYES_NO);
  if ($okcancel->ShowModal != wxID_YES) {
    $rframes->{main}->status("Not importing an external Feff calculation.");
    return;
  };

  if (not $fname) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import an Atoms or Feff input file", cwd, q{},
				  "Atoms/Feff input (*.inp)|*.inp|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("$CHI(k) import canceled.");
      return;
    };
    $file = $fd->GetPath;
  };

  my ($atoms_file, $feff_file) = (q{}, q{});
  ($atoms_file, $feff_file) = ($file, File::Spec->catfile(dirname($file), 'feff.inp')) if (Demeter->is_atoms($file));
  ($atoms_file, $feff_file) = (q{}, $file)                                             if (Demeter->is_feff($file));
  ($atoms_file = File::Spec->catfile(dirname($file), 'atoms.inp')) if ((not $atoms_file) and (-e File::Spec->catfile(dirname($file), 'atoms.inp')));

  if (not -e $feff_file) {
    my $message = <<EOH
Error importing external Feff calculation.

It is unclear which file is the Feff input file.
It is likely that you selected the Atoms input
file in the file selection dialog.  You should
select the Feff input file instead.

EOH
      ;
    my $error = Wx::MessageDialog->new($rframes->{main},
				       $message,
				       "Error importing Feff calculation",
				       wxOK|wxICON_ERROR);
    my $result = $error->ShowModal;
    $rframes->{main}->status("Importing external Feff calculation aborted.");
    return 0;
  };

  my ($filename, $pathto, $suffix) = fileparse($file, qr{\.inp});
  if (lc($filename) eq 'feff') {
    my @dirs = File::Spec->splitdir($pathto);
    $filename = $dirs[-1];
    $filename = $dirs[-2] if ($filename =~ m{\A\s*\z});
  };
  my $efeff = Demeter::Feff::External -> new(screen=>0, name=>$filename);
  my $destination = File::Spec->catfile($rframes->{main}->{project_folder}, 'feff', $efeff->group);
  $efeff->workspace($destination);
  $efeff->file($feff_file);
  if (not $efeff->is_complete) {
    my $message = "Error importing external Feff calculation:\n\n"
                . $efeff->problem
		. "\nTherefore $file cannot be imported as an external Feff calculation.\n";
    my $error = Wx::MessageDialog->new($rframes->{main},
				       $message,
				       "Error importing Feff calculation",
				       wxOK|wxICON_ERROR);
    my $result = $error->ShowModal;
    $rframes->{main}->status("Importing external Feff calculation aborted.");
    return 0;
  };


  copy($atoms_file, File::Spec->catfile($destination, 'atoms.inp')) if $atoms_file;
  copy($feff_file, File::Spec->catfile($destination, $efeff->group.'.inp'));
  $efeff->freeze(File::Spec->catfile($destination, $efeff->group.'.yaml'));

  ## import atoms.inp, create Feff frame
  my ($fnum, $ifeff) = &$make_feff_frame($rframes->{main}, $atoms_file, $efeff->name, $efeff);

  ## import feff.inp
  my $text = $efeff->slurp($feff_file);
  $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);

  ## fill in Feff frame
  $rframes->{$fnum}->{Feff}->{feffobject} = $efeff;
  $rframes->{$fnum}->{Feff}->fill_intrp_page($efeff);
  $rframes->{$fnum}->{notebook}->ChangeSelection(2);

  $rframes->{$fnum}->{Feff} ->{name}->SetValue($efeff->name);
  $rframes->{$fnum}->{Paths}->{name}->SetValue($efeff->name);

  $rframes->{$fnum} -> Show(not $noshow);
  $rframes->{main}->{$fnum}->SetValue(not $noshow);
  my $word = ($noshow) ? 'Show ' : 'Hide ';
  $rframes->{main}->{$fnum}->SetLabel($word.emph($efeff->name));

  ## disable atoms tab is not $atoms_file (how?)

  Demeter->push_mru("externalfeff", $file);
  autosave();
  chdir dirname($file);
  modified(1);
  $rframes->{main}->status("Imported $file as a Feff calculation.");
  return $efeff;
};


sub _old {
  my ($file) = @_;
  $file ||= q{};
  my ($unidecoded, $deunifile, $original) = (0, qw{}, qw{});
  if (not -e $file) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import an old-style Artemis project", cwd, q{},
				  "old-style Artemis project (*.apj)|*.apj|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("old-style Artemis import canceled.");
      return;
    };
    $file = $fd->GetPath;
    if ($file =~ m{[^[:ascii:]]}) {
      $unidecoded = 1;
      $deunifile = Demeter->unicopy($file);
      $rframes->{main}->status("Moved $file to $deunifile");
      $original = $file;
      $file = $deunifile;
    };
  };
  $file = Demeter->follow_link($file);

  if (not Demeter->is_zipproj($file,0, 'apj')) {
    $rframes->{main}->status("$file is not an old style fitting project file.", 'error');
    return;
  };

  my $busy = Wx::BusyCursor->new();
  $rframes->{main}->status("Converting old-style project to Demeter fit serialization", 'wait');

  my $tempfit = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)");
  my $dpj = File::Spec->catfile($tempfit->stash_folder, basename($file, '.apj').".dpj");
  my $journal = q{};
  eval "require Demeter::Fit::Horae"; # delay importing this until now, when it's needed
  my $result = Demeter::Fit::Horae::apj2dpj($tempfit, $file, $dpj, \$journal);
  $tempfit   -> DEMOLISH;

  if (ref($result) !~ m{Fit}) {
    $rframes->{main}->status($result, 'alert');
    undef $busy;
    return;
  };

  $rframes->{main}->status("Importing Demeter fit serialization", 'wait');
  Import('dpj', $dpj, postcrit=>0);
  unlink $dpj;

  if ($unidecoded) {
    unlink $file;
    $file = $original;
  };

  Demeter->push_mru("old_artemis", $file);
  $rframes->{main}->{projectpath} = $file;
  $rframes->{main}->{projectname} = unidecode(basename($file, '.apj'));
  $rframes->{Journal}->{journal} -> SetValue($journal);

  modified(1);
  $rframes->{main}->status("Imported old-style Artemis project $file");
  undef $busy;
};


sub _feffit {
  my ($fname) = @_;
  my $file = $fname;
  if (not $fname) {
    my $fd = Wx::FileDialog->new( $rframes->{main}, "Import a Feffit input file", cwd, q{},
				  "Feffit input (*.inp)|*.inp|All files (*)|*",
				  wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR|wxFD_PREVIEW,
				  wxDefaultPosition);
    if ($fd->ShowModal == wxID_CANCEL) {
      $rframes->{main}->status("Feffit import canceled.");
      return;
    };
    $file = $fd->GetPath;
  };
  if (not -e $file) {
    $rframes->{main}->status("$file does not exist.");
    return;
  };
  if (not -r $file) {
    $rframes->{main}->status("$file cannot be read.");
    return;
  };
  $file = Demeter->follow_link($file);

  ## -------- want to skip autosave during the intermediate steps of the feffit import
  $Demeter::UI::Artemis::noautosave = 1;
  eval "require Demeter::Fit::Feffit";
  my $inp = Demeter::Fit::Feffit->new(file=>$file);
  my $fit = $inp -> convert;
  my $mds = 0;
  my %datae_id = (); # \ disentangle relations
  my %feffs = ();    # / between objects

  ## -------- figure out how many Feff calculations are involved
  my %folders_seen = ();
  foreach my $p (@ {$fit->paths} ) {
    ++$folders_seen{$p->folder};
  };

  ## -------- import each as a Demeter::Feff::External
  eval "require Demeter::Feff::External";
  foreach my $f (keys %folders_seen) {
    my $inp = File::Spec->catfile($f, 'feff.inp');
    if (not -e $inp) {
      $Demeter::UI::Artemis::noautosave = 0;
      $inp->DESTROY;
      $fit->DESTROY;
      my $error = Wx::MessageDialog->new($rframes->{main},
					 "Cannot import Feff calculation from $f. Could not find the 'feff.inp' file.",
					 "Error importing feffit file",
					 wxOK|wxICON_ERROR);
      my $result = $error->ShowModal;
      $rframes->{main}->status("Importing feffit.inp file aborted.");
      return 0;
    };
    my $ef = _external_feff($inp, 1);
    return 0 if not $ef;
    ## -------- check that external feff is complete
    $feffs{$f} = $ef;
  };

  ## -------- import the data and populate data windows
  foreach my $d (@ {$fit->data} ) {
    if (not -e $d->file) {
      $Demeter::UI::Artemis::noautosave = 0;
      $feffs{$_}->DESTROY foreach keys(%feffs);
      $inp->DESTROY;
      $fit->DESTROY;
      my $error = Wx::MessageDialog->new($rframes->{main},
					 "Cannot find data file ".$d->file,
					 "Error importing feffit.inp file",
					 wxOK|wxICON_ERROR);
      my $result = $error->ShowModal;
      $rframes->{main}->status("Importing feffit.inp file aborted.");
      return 0;
    };
    my ($dnum, $idata) = &$make_data_frame($rframes->{main}, $d);
    $d->_update('fft');
    $rframes->{$dnum} -> populate($d);
    my $show = ($mds) ? 0 : 1;
    $rframes->{$dnum} -> Show($show);
    $datae_id{$d->group} = $dnum;
    $rframes->{main}->{$dnum}->SetValue($show);
    ++$mds;
  };

  ## -------- reassign theory to each Path, blanking folder and file attributes, setting sp attribute
  foreach my $p (@ {$fit->paths} ) {
    my ($file, $folder) = ($p->file, $p->folder);
    my $thisnnnn = File::Spec->catfile($folder, $file);
    if (not -e $thisnnnn) {
      $Demeter::UI::Artemis::noautosave = 0;
      $feffs{$_}->DESTROY foreach keys(%feffs);
      $inp->DESTROY;
      $fit->DESTROY;
      my $error = Wx::MessageDialog->new($rframes->{main},
					 "Cannot find feffNNNN.dat file ".$thisnnnn,
					 "Error importing feffit.inp calculation file",
					 wxOK|wxICON_ERROR);
      my $result = $error->ShowModal;
      $rframes->{main}->status("Importing feffit.inp file aborted.");
      return 0;
    };
    $p -> set(file=>q{}, folder=>q{});
    $p -> parent($feffs{$folder});
    foreach my $sp (@{ Demeter->mo->ScatteringPath }) {
      if ($sp->fromnnnn eq File::Spec->catfile($folder, $file)) {
	$p->sp($sp);
	last;
      };
    };
    ## -------- populate data frame with paths
    my $book = $rframes->{$datae_id{$p->data->group}}->{pathlist};
    $book->DeletePage(0) if ($rframes->{$datae_id{$p->data->group}}->{pathlist}->GetPage(0) =~ m{Panel});
    my $page = Demeter::UI::Artemis::Path->new($book, $p, $rframes->{$datae_id{$p->data->group}});
    $book->AddPage($page, $p->label, 1, 0);
    $page->include_label;
  };

  ## -------- populate the GDS window
  foreach my $thisgds (@ {$fit->gds} ) {
    $rframes->{GDS}->put_gds($thisgds);
    $rframes->{GDS}->{grid} -> {$thisgds->name} = $thisgds;
    $thisgds->push_ifeffit;
  };

  $rframes->{Journal}->{journal}->SetValue($file . ":\n\n" . $fit->slurp($file));

  ## -------- clean up and finish up
  $inp->DESTROY;
  $fit->DESTROY;
  $Demeter::UI::Artemis::noautosave = 0;
  autosave;
  Demeter->push_mru("feffit", $file);
  chdir dirname($file);
  my $newfit = Demeter::Fit->new(interface=>"Artemis (Wx $Wx::VERSION)");
  $rframes->{main} -> {currentfit} = $newfit;
  ++$Demeter::UI::Artemis::fit_order{order}{current};
  modified(1);
  $rframes->{main}->status("Imported old-skool Feffit input: $file");
};

1;

=head1 NAME

Demeter::UI::Artemis::Import - Import various kinds of data into Artemis

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This module exports the C<Import> method, which is used to import
various kinds of data into Artemis.

=head1 METHODS

=over 4

=item C<Import>

Import a specific data type.

  import($type, $filename);

If the filename is omitted, the user will be prompted for a file using
the file selection dialog.  The type can be one of:

=over 4

=item C<prj> or C<athena>

An Athena project file

=item C<chi>

A column data file containing chi(k) data.  If the data are on a
non-standard grid, they will be rebinned onto the proper grid.

=item C<dpj>

A serialization file from a Fit object.  This sort of file is
generated from a free-standing script using Demeter.

=item C<feff>

A F<feff.inp> file, an F<atoms.inp> file, or a CIF file.

=item C<external>

An entire Feff calculation.  Note that a F<feff.inp> file by itself can be imported using 

=item C<old>

An project file from the Tk version of Artemis.

=back

=item C<prjrecord>

Post a dialog prompting for a record in an Athena project file.

=back

=head1 DEPENDENCIES

Demeter's dependencies are in the F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

Not handling local parameters from feffit.inp files

=item *

While single-data-set fits with multiple k-weights does work
correctly, MDS+MKW fits will not be imported properly from a
feffit.inp file

=back

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 (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