| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Xray::Xtal; |
| 5 |
use Ifeffit; |
| 6 |
use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); |
| 7 |
|
| 8 |
my $elem_regex = '([bcfhiknopsuvwy]|a[cglmrstu]|b[aehikr]|c[adeflmorsu]|dy|e[rsu]|f[emr]|g[ade]|h[aefgos]|i[nr]|kr|l[airu]|m[dgnot]|n[abdeiop]|os|p[abdmortu]|r[abefhnu]|s[bcegimnr]|t[abcehilm]|xe|yb|z[nr])'; |
| 9 |
my $num_regex = '-?(\d+\.?\d*|\.\d+)'; |
| 10 |
|
| 11 |
## if the display is messed up, comment the next line and uncomment |
| 12 |
## the following line |
| 13 |
my ($red, $green, $reset) = ("[31;1m", "[32;1m", "[33m[0m"); |
| 14 |
## my ($red, $green, $reset) = ("", "", ""); |
| 15 |
|
| 16 |
print "usage: testinp <file(s)>\n" unless @ARGV; |
| 17 |
|
| 18 |
|
| 19 |
foreach my $a (@ARGV) { |
| 20 |
|
| 21 |
## now test if this is an athena project file |
| 22 |
my $is_proj = test_athena($a); |
| 23 |
my $passfail = ($is_proj) ? |
| 24 |
$green.'athena '.$reset : |
| 25 |
$red.'not athena'.$reset; |
| 26 |
printf "%s\n\t%s is_project=%s\n", |
| 27 |
$a, $passfail, $is_proj; |
| 28 |
|
| 29 |
## now test if this is an athena project file |
| 30 |
my ($is_zipfile, $horae) = test_artemis($a); |
| 31 |
$passfail = ($is_zipfile and $horae) ? |
| 32 |
$green.'artemis '.$reset : |
| 33 |
$red.'not artemis'.$reset; |
| 34 |
printf "\t%s is_zipfile=%s horae=%s\n", |
| 35 |
$passfail, $is_zipfile, $horae; |
| 36 |
|
| 37 |
## test if this is a atoms.inp file |
| 38 |
my ($space_test, $atoms_test) = test_atoms($a); |
| 39 |
$passfail = ($atoms_test && $space_test) ? |
| 40 |
$green.'atoms '.$reset : $red.'not atoms'.$reset; |
| 41 |
printf "\t%s atoms_test=%d space_test=%s\n", |
| 42 |
$passfail, $atoms_test, $space_test; |
| 43 |
|
| 44 |
## now test if this is a feff.inp file |
| 45 |
my ($abs_test, $scat_test) = test_feff($a); |
| 46 |
$passfail = ($abs_test && $scat_test) ? |
| 47 |
$green.'feff '.$reset : $red.'not feff'.$reset; |
| 48 |
printf "\t%s abs_test =%s\n\t scat_test=%s\n", |
| 49 |
$passfail, $abs_test, $scat_test; |
| 50 |
|
| 51 |
## now test if this is a data file |
| 52 |
my $col_string = ($is_zipfile) ? " " : test_data($a); |
| 53 |
$passfail = ($col_string =~ /^(\s*|--undefined--)$/) ? |
| 54 |
$red.'not data'.$reset : |
| 55 |
$green.'data '.$reset ; |
| 56 |
printf "\t%s col_string=%s\n", |
| 57 |
$passfail, $col_string; |
| 58 |
|
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
## an atoms.inp file is identified by having a valid space group |
| 64 |
## symbol and by having an atoms list with at least one valid line of |
| 65 |
## atoms |
| 66 |
sub test_atoms { |
| 67 |
my $a = $_[0]; |
| 68 |
open A, $a or die "could not open $a: $!"; |
| 69 |
my ($space_test, $atoms_test, $toss) = (0,0,0); |
| 70 |
my $switch = 0; |
| 71 |
A: while (<A>) { |
| 72 |
next if /^\s*$/; # skip blanks |
| 73 |
next if /^\s*[!\#%*]/; # skip comment lines |
| 74 |
$switch = 1, next if (/^\s*ato/); |
| 75 |
if ($switch) { |
| 76 |
my @line = split(" ", $_); |
| 77 |
($atoms_test=1), last A if ( (lc($line[0]) =~ /^$elem_regex$/) and |
| 78 |
($line[1] =~ /^$num_regex$/) and |
| 79 |
($line[2] =~ /^$num_regex$/) and |
| 80 |
($line[3] =~ /^$num_regex$/)); |
| 81 |
} else { |
| 82 |
|
| 83 |
my @line = split(" ", $_); |
| 84 |
LINE: foreach my $word (@line) { |
| 85 |
last LINE if (lc($word) eq 'title'); |
| 86 |
if (lc($word) =~ /space/) { |
| 87 |
my $lline = lc($_); |
| 88 |
my $space = substr($_, index($lline,"space")+6); |
| 89 |
$space =~ s/^[\s=,]+//; |
| 90 |
$space = substr($space, 0, 10); # next 10 characters |
| 91 |
$space =~ s/[!\#%*].*$//; # trim off comments |
| 92 |
($space_test, $toss) = Xray::Xtal::Cell::canonicalize_symbol($space); |
| 93 |
}; |
| 94 |
}; |
| 95 |
}; |
| 96 |
}; |
| 97 |
close A; |
| 98 |
return ($space_test, $atoms_test); |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
## a feff.inp file is identified by having a potentials list and at |
| 103 |
## least two valid potentials line, the absorber and one other. |
| 104 |
sub test_feff { |
| 105 |
my $a = $_[0]; |
| 106 |
open A, $a or die "could not open $a: $!"; |
| 107 |
my $switch = 0; |
| 108 |
my ($abs_test, $scat_test) = (0,0); |
| 109 |
A: while (<A>) { |
| 110 |
chomp; |
| 111 |
next if /^\s*$/; # skip blanks |
| 112 |
next if /^\s*[!\#%*]/; # skip comment lines |
| 113 |
$switch = 1, next if (/^\s*pot/i); |
| 114 |
if ($switch) { |
| 115 |
last A if (/^\s*[a-zA-Z]/); |
| 116 |
my @line = split(" ", $_); |
| 117 |
($abs_test=$_), next A if (($line[0] =~ /^0$/) and |
| 118 |
($line[1] =~ /^\d+$/) and |
| 119 |
(lc($line[2]) =~ /^$elem_regex$/)); |
| 120 |
($scat_test=$_), next A if (($line[0] =~ /^\d+$/) and |
| 121 |
($line[1] =~ /^\d+$/) and |
| 122 |
(lc($line[2]) =~ /^$elem_regex$/)); |
| 123 |
}; |
| 124 |
} |
| 125 |
close A; |
| 126 |
return ($abs_test, $scat_test); |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
## a data file is data if ifeffit recognizes it as such and returns a |
| 131 |
## column_label string |
| 132 |
sub test_data { |
| 133 |
my $a = $_[0]; |
| 134 |
ifeffit("read_data(file=$a, group=a)\n"); |
| 135 |
return Ifeffit::get_string('$column_label'); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
## an athena project file is so marked in the first line |
| 140 |
sub test_athena { |
| 141 |
my $file = $_[0]; |
| 142 |
open F, $file or die "could not open $file as a record\n"; |
| 143 |
my $first = <F>; |
| 144 |
close F; |
| 145 |
return ($first =~ /Athena (record|project) file/) ? $1 : 0; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
## an artemis project file is a valid zip file with a file called |
| 150 |
## HORAE in it. |
| 151 |
sub test_artemis { |
| 152 |
my $file = $_[0]; |
| 153 |
Archive::Zip::setErrorHandler( \&is_zip_error_handler ); |
| 154 |
my $zip = Archive::Zip->new(); |
| 155 |
my $is_zipstyle = ($zip->read($file) == AZ_OK); |
| 156 |
my $horae = ($is_zipstyle) ? $zip->membersMatching( '^HORAE$' ) : 0; |
| 157 |
undef $zip; |
| 158 |
Archive::Zip::setErrorHandler( undef ); |
| 159 |
return ($is_zipstyle, $horae); |
| 160 |
}; |
| 161 |
|
| 162 |
sub is_zip_error_handler { 1; }; |