| 1 |
package Ifeffit::Files; # -*- cperl -*- |
| 2 |
###################################################################### |
| 3 |
## Ifeffit::Files: Object oriented tools for performing file checks |
| 4 |
## |
| 5 |
## Athena is copyright (c) 2001-2006 Bruce Ravel |
| 6 |
## [email protected] |
| 7 |
## http://feff.phys.washington.edu/~ravel/ |
| 8 |
## |
| 9 |
## Ifeffit is copyright (c) 1992-2006 Matt Newville |
| 10 |
## [email protected] |
| 11 |
## http://cars9.uchicago.edu/~newville/ifeffit/ |
| 12 |
## |
| 13 |
## The latest version of Athena can always be found at |
| 14 |
## http://feff.phys.washington.edu/~ravel/software/exafs |
| 15 |
## |
| 16 |
## ------------------------------------------------------------------- |
| 17 |
## All rights reserved. This program is free software; you can |
| 18 |
## redistribute it and/or modify it provided that the above notice |
| 19 |
## of copyright, these terms of use, and the disclaimer of |
| 20 |
## warranty below appear in the source code and documentation, and |
| 21 |
## that none of the names of The Naval Research Laboratory, The |
| 22 |
## University of Chicago, University of Washington, or the authors |
| 23 |
## appear in advertising or endorsement of works derived from this |
| 24 |
## software without specific prior written permission from all |
| 25 |
## parties. |
| 26 |
## |
| 27 |
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 29 |
## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 |
## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 31 |
## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 32 |
## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 33 |
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 34 |
## OTHER DEALINGS IN THIS SOFTWARE. |
| 35 |
## ------------------------------------------------------------------- |
| 36 |
###################################################################### |
| 37 |
|
| 38 |
## This file contains all the various checks of imported data files |
| 39 |
## that Athena and Artemis need to do in order to properly read data |
| 40 |
## from a wide variety of sources. These things are segregated to |
| 41 |
## the file for organizational purposes. |
| 42 |
|
| 43 |
|
| 44 |
use strict; |
| 45 |
use vars qw($VERSION $cvs_info $module_version @ISA @EXPORT @EXPORT_OK @buffer); |
| 46 |
use constant EPSI => 0.00001; |
| 47 |
use File::Basename; |
| 48 |
use File::Copy; |
| 49 |
use Ifeffit; |
| 50 |
use Xray::Xtal; |
| 51 |
use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); |
| 52 |
use Compress::Zlib; |
| 53 |
|
| 54 |
require Exporter; |
| 55 |
|
| 56 |
@ISA = qw(Exporter AutoLoader Ifeffit); |
| 57 |
@EXPORT_OK = qw(); |
| 58 |
|
| 59 |
|
| 60 |
use vars qw($elem_regex); |
| 61 |
$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])'; |
| 62 |
my $num_regex = '-?(\d+\.?\d*|\.\d+)'; |
| 63 |
|
| 64 |
|
| 65 |
## this should be called immediately after a disposal of |
| 66 |
## "read_data". It checks the $column_label ifeffit global variable, |
| 67 |
## which ifeffit sets to "--undefined--" when it is thinks that it was |
| 68 |
## given a file that was not actually data. |
| 69 |
sub is_datafile { |
| 70 |
shift; |
| 71 |
my $col_string = Ifeffit::get_string('$column_label'); |
| 72 |
return (not ($col_string =~ /^(--undefined--|\s*)$/)); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
## ================================================================= |
| 78 |
## Recognize a record file |
| 79 |
## check the first line of a file to verify that it is a record |
| 80 |
sub is_record { |
| 81 |
shift; |
| 82 |
my $file = $_[0]; |
| 83 |
my $verbose = $_[1]; |
| 84 |
my $gz = gzopen($file, "rb") or die "could not open $file as a record\n"; |
| 85 |
my $first; |
| 86 |
$gz->gzreadline($first); |
| 87 |
$gz->gzclose(); |
| 88 |
my $is_proj = ($first =~ /Athena (record|project) file/) ? $1 : 0; |
| 89 |
if ($verbose) { |
| 90 |
my $passfail = ($is_proj) ? 'athena ' : 'not athena'; |
| 91 |
printf "%s\n\t%s is_project=%s\n", $a, $passfail, $is_proj; |
| 92 |
}; |
| 93 |
return $is_proj; |
| 94 |
}; |
| 95 |
sub is_athena { is_record(@_) }; |
| 96 |
|
| 97 |
## an artemis project file is a valid zip file with a file called |
| 98 |
## HORAE in it. |
| 99 |
sub is_artemis { |
| 100 |
shift; |
| 101 |
my $file = $_[0]; |
| 102 |
my $verbose = $_[1]; |
| 103 |
Archive::Zip::setErrorHandler( \&is_zip_error_handler ); |
| 104 |
my $zip = Archive::Zip->new(); |
| 105 |
my $is_zipstyle = ($zip->read($file) == AZ_OK); |
| 106 |
my $horae = ($is_zipstyle) ? $zip->membersMatching( '^HORAE$' ) : 0; |
| 107 |
undef $zip; |
| 108 |
Archive::Zip::setErrorHandler( undef ); |
| 109 |
if ($verbose) { |
| 110 |
my $passfail = ($is_zipstyle and $horae) ? 'artemis ': 'not artemis'; |
| 111 |
printf "\t%s is_zipfile=%s horae=%s\n", $passfail, $is_zipstyle, $horae; |
| 112 |
}; |
| 113 |
return ($is_zipstyle, $horae); |
| 114 |
}; |
| 115 |
sub is_zip_error_handler { 1; }; |
| 116 |
|
| 117 |
## an atoms.inp file is identified by having a valid space group |
| 118 |
## symbol and by having an atoms list with at least one valid line of |
| 119 |
## atoms |
| 120 |
sub is_atoms { |
| 121 |
shift; |
| 122 |
my $a = $_[0]; |
| 123 |
my $verbose = $_[1]; |
| 124 |
open A, $a or die "could not open $a: $!"; |
| 125 |
my ($space_test, $atoms_test, $toss) = (0,0,0); |
| 126 |
my $switch = 0; |
| 127 |
A: while (<A>) { |
| 128 |
next if /^\s*$/; # skip blanks |
| 129 |
next if /^\s*[!\#%*]/; # skip comment lines |
| 130 |
$switch = 1, next if (/^\s*ato/); |
| 131 |
if ($switch) { |
| 132 |
my @line = split(" ", $_); |
| 133 |
($atoms_test=1), last A if ( (lc($line[0]) =~ /^$elem_regex$/) and |
| 134 |
($line[1] =~ /^$num_regex$/) and |
| 135 |
($line[2] =~ /^$num_regex$/) and |
| 136 |
($line[3] =~ /^$num_regex$/)); |
| 137 |
} else { |
| 138 |
|
| 139 |
my @line = split(" ", $_); |
| 140 |
LINE: foreach my $word (@line) { |
| 141 |
last LINE if (lc($word) eq 'title'); |
| 142 |
if (lc($word) =~ /space/) { |
| 143 |
my $lline = lc($_); |
| 144 |
my $space = substr($_, index($lline,"space")+6); |
| 145 |
$space =~ s/^[\s=,]+//; |
| 146 |
$space = substr($space, 0, 10); # next 10 characters |
| 147 |
$space =~ s/[!\#%*].*$//; # trim off comments |
| 148 |
($space_test, $toss) = Xray::Xtal::Cell::canonicalize_symbol($space); |
| 149 |
}; |
| 150 |
}; |
| 151 |
}; |
| 152 |
}; |
| 153 |
close A; |
| 154 |
if ($verbose) { |
| 155 |
my $passfail = ($atoms_test && $space_test) ? 'atoms ': 'not atoms'; |
| 156 |
printf "\t%s atoms_test=%d space_test=%s\n", $passfail, $atoms_test, $space_test; |
| 157 |
}; |
| 158 |
return ($space_test && $atoms_test) ? 1 : 0; |
| 159 |
}; |
| 160 |
|
| 161 |
## an atoms.inp file is identified by having a valid space group |
| 162 |
## symbol and by having an atoms list with at least one valid line of |
| 163 |
## atoms |
| 164 |
sub is_cif { |
| 165 |
shift; |
| 166 |
my $a = $_[0]; |
| 167 |
return 1 if (basename($a) =~ /cif$/i); |
| 168 |
return 0; |
| 169 |
}; |
| 170 |
|
| 171 |
## a feff.inp file is identified by having a potentials list and at |
| 172 |
## least two valid potentials line, the absorber and one other. |
| 173 |
sub is_feff { |
| 174 |
shift; |
| 175 |
my $a = $_[0]; |
| 176 |
my $verbose = $_[1]; |
| 177 |
open A, $a or die "could not open $a: $!"; |
| 178 |
my $switch = 0; |
| 179 |
my ($abs_test, $scat_test) = (0,0); |
| 180 |
A: while (<A>) { |
| 181 |
chomp; |
| 182 |
next if /^\s*$/; # skip blanks |
| 183 |
next if /^\s*[!\#%*]/; # skip comment lines |
| 184 |
$switch = 1, next if (/^\s*pot/i); |
| 185 |
if ($switch) { |
| 186 |
last A if (/^\s*[a-zA-Z]/); |
| 187 |
my @line = split(" ", $_); |
| 188 |
($abs_test=$_), next A if (($line[0] =~ /^0$/) and |
| 189 |
($line[1] =~ /^\d+$/) and |
| 190 |
(lc($line[2]) =~ /^$elem_regex$/)); |
| 191 |
($scat_test=$_), next A if (($line[0] =~ /^\d+$/) and |
| 192 |
($line[1] =~ /^\d+$/) and |
| 193 |
(lc($line[2]) =~ /^$elem_regex$/)); |
| 194 |
}; |
| 195 |
} |
| 196 |
close A; |
| 197 |
if ($verbose) { |
| 198 |
my $passfail = ($abs_test && $scat_test) ? 'feff ': 'not feff'; |
| 199 |
printf "\t%s abs_test =%s\n\t scat_test=%s\n", |
| 200 |
$passfail, $abs_test, $scat_test; |
| 201 |
}; |
| 202 |
return ($abs_test && $scat_test) ? 1 : 0; |
| 203 |
}; |
| 204 |
|
| 205 |
## a data file is data if ifeffit recognizes it as such and returns a |
| 206 |
## column_label string |
| 207 |
sub is_data { |
| 208 |
shift; |
| 209 |
my $a = $_[0]; |
| 210 |
my $verbose = $_[1]; |
| 211 |
ifeffit("read_data(file=$a, group=a)\n"); |
| 212 |
my $col_string = Ifeffit::get_string('$column_label'); |
| 213 |
if ($verbose) { |
| 214 |
my $passfail = ($col_string =~ /^(\s*|--undefined--)$/) ? |
| 215 |
'not data' : 'data ' ; |
| 216 |
printf "\t%s col_string=%s\n", $passfail, $col_string; |
| 217 |
}; |
| 218 |
return ($col_string =~ /^(\s*|--undefined--)$/) ? 1 : 0; |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
sub is_feffnnnn { |
| 223 |
return 1; |
| 224 |
}; |
| 225 |
|
| 226 |
## this returns 0 is it is a two column file, -1 if it is tagged by |
| 227 |
## athena as a multicolumn file, or the number of columns that ifeffit |
| 228 |
## reports |
| 229 |
sub is_multicolumn { |
| 230 |
shift; |
| 231 |
my $file = $_[0]; |
| 232 |
open F, $file or die "could not open $file as a record\n"; |
| 233 |
my $first = <F>; |
| 234 |
close F; |
| 235 |
## look for the tag from Athena 0.8.025 and later |
| 236 |
return -1 if ($first =~ /Athena multicolumn data file/); |
| 237 |
return 0 if ($first =~ /Athena data file/); |
| 238 |
## else rely on Ifeffit to recognize the situation |
| 239 |
ifeffit("read_data(file=\"$file\", group=t___oss)\n"); |
| 240 |
my $ncol = Ifeffit::get_scalar('&n_arrays_read'); |
| 241 |
ifeffit("erase \@group t___oss\n"); |
| 242 |
return ($ncol==2) ? 0 : $ncol; |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
## ================================================================= |
| 247 |
## Recognize xanes data (i.e. data of limited energy extent) |
| 248 |
sub is_xanes { |
| 249 |
shift; |
| 250 |
my ($data, $cutoff) = @_; |
| 251 |
## open the data with ifeffit |
| 252 |
ifeffit("read_data(file=\"$data\", group=t___oss)\n"); |
| 253 |
my $suff = (split(" ", Ifeffit::get_string('$column_label')))[0]; |
| 254 |
ifeffit("em___in = floor(t___oss.$suff)\n"); |
| 255 |
ifeffit("em___ax = ceil(t___oss.$suff)\n"); |
| 256 |
my ($emin, $emax) = (Ifeffit::get_scalar('em___in'), |
| 257 |
Ifeffit::get_scalar('em___ax')); |
| 258 |
ifeffit("erase \@group t___oss\n"); |
| 259 |
my $span = ($emax-$emin); |
| 260 |
($span *= 1000) if ($span < 2); # keV, presumably |
| 261 |
return 1 if ( $span < $cutoff ); |
| 262 |
return 0; |
| 263 |
}; |
| 264 |
|
| 265 |
## ================================================================= |
| 266 |
## Recognize feff's xmu.dat file |
| 267 |
|
| 268 |
sub is_xmudat { |
| 269 |
shift; |
| 270 |
my ($file, $top) = @_; |
| 271 |
open F, $file or die "could not open $file as data (is_xmudat)\n"; |
| 272 |
my $first = <F>; |
| 273 |
close F, return 0 unless ($first =~ /Feff/); |
| 274 |
my $mu = 0; |
| 275 |
while (<F>) { |
| 276 |
#(/Mu=([- ]?\d*\.\d+E\+\d+)/) and ($mu = sprintf("%.4f",$1)); |
| 277 |
($mu = (split(/[ =]+/, $_))[2]) if $_ =~ /Mu=/; |
| 278 |
last if not (/^\s*\#/); |
| 279 |
}; |
| 280 |
my @line = split(" ", $_); |
| 281 |
close F; |
| 282 |
return 0 unless ($#line == 5); # xmu.dat is a 6-column file |
| 283 |
return $mu if $mu; |
| 284 |
my $dialog = $top->Dialog(-text => "$file appears to be an xmu.dat file from Feff. Is that correct?", |
| 285 |
-bitmap => 'question', |
| 286 |
-title => 'Is this an xmu.dat file?', |
| 287 |
-default_button=> 'Yes', -buttons => [qw/Yes No Cancel/]); |
| 288 |
my $answer = $dialog->Show(); |
| 289 |
## need to do the right thing for cancel |
| 290 |
return ($answer eq 'Yes') ? 0.01 : 0 ; |
| 291 |
}; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
## ================================================================= |
| 296 |
## Recognize data that is a function of pixel position |
| 297 |
|
| 298 |
sub is_pixel { |
| 299 |
shift; |
| 300 |
my $data = shift; |
| 301 |
open DATA, $data or die "could not open $data as data (is_pixel)\n"; |
| 302 |
my $i = 0; |
| 303 |
my $first; |
| 304 |
my $is_pixel = 1; |
| 305 |
DATA: while (<DATA>) { |
| 306 |
chomp; |
| 307 |
next if (/^\s*$/); |
| 308 |
#next if (/^\s*[^0-9\-]/); |
| 309 |
my @line = split(" ", $_); |
| 310 |
foreach my $c (@line) { |
| 311 |
next DATA unless ($c =~ /-?(\d+\.?\d*|\.\d+)/); |
| 312 |
}; |
| 313 |
my $pixel = $line[0]; |
| 314 |
close DATA, return 0 unless ($pixel =~ /^\d+$/); |
| 315 |
($first = $pixel) unless defined $first; |
| 316 |
$is_pixel &&= ($pixel == $i+$first); |
| 317 |
##print join(" ", '$i $first $pixel $is_pixel', |
| 318 |
## $i, $first, $pixel, $is_pixel), $/; |
| 319 |
++$i; |
| 320 |
close DATA, return 0 unless $is_pixel; |
| 321 |
close DATA, return 1 if ($i == 100); |
| 322 |
}; |
| 323 |
close DATA; |
| 324 |
return 1; |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
## ================================================================= |
| 329 |
## Recognize and handle files from NSLS beamline X10C |
| 330 |
|
| 331 |
## sub is_x10c { |
| 332 |
## shift; |
| 333 |
## my $data = shift; |
| 334 |
## open D, $data or die "could not open $data as data (is_x10c)\n"; |
| 335 |
## my $first = <D>; |
| 336 |
## close D, return 0 unless (uc($first) =~ /^EXAFS/); |
| 337 |
## my $lines = 0; |
| 338 |
## while (<D>) { |
| 339 |
## close D, return 1 if (uc($first) =~ /^\s+DATA START/); |
| 340 |
## ++$lines; |
| 341 |
## #close D, return 0 if ($lines > 40); |
| 342 |
## }; |
| 343 |
## close D; |
| 344 |
## }; |
| 345 |
## |
| 346 |
## |
| 347 |
## ## deal with files from X10C by streaming the file, fixing the |
| 348 |
## ## problems, and putting the fixed up file in the stash directory. |
| 349 |
## sub fix_x10c { |
| 350 |
## shift; |
| 351 |
## my ($data, $stash_dir) = @_; |
| 352 |
## my ($nme, $pth, $suffix) = fileparse($data); |
| 353 |
## my $new = File::Spec->catfile($stash_dir, $nme); |
| 354 |
## ($new = File::Spec->catfile($stash_dir, "toss")) if (length($new) > 127); |
| 355 |
## open D, $data or die "could not open $data as data (fix_x10c)\n"; |
| 356 |
## open N, ">".$new or die "could not write to $new (fix_x10c)\n"; |
| 357 |
## my $header = 1; |
| 358 |
## my $null = chr(0).'+'; |
| 359 |
## while (<D>) { |
| 360 |
## $_ =~ s/$null//g; # clean up nulls |
| 361 |
## print N "# " . $_ if $header; # comment headers |
| 362 |
## ($header = 0), next if (uc($_) =~ /^\s+DATA START/); |
| 363 |
## next if ($header); |
| 364 |
## $_ =~ s/([eE][-+]\d{1,2})-/$1 -/g; # clean up 5th column |
| 365 |
## print N $_; |
| 366 |
## }; |
| 367 |
## close N; |
| 368 |
## close D; |
| 369 |
## return $new; |
| 370 |
## } |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
## ================================================================= |
| 375 |
## Check for monotonically increasing data in energy |
| 376 |
|
| 377 |
## return 0 if these data are monotonically increasing in energy and |
| 378 |
## return the number of points found out of order if not (yeah, yeah, |
| 379 |
## the logic is backwards -- deal!). also return the list of points |
| 380 |
## that aren't monotonic. $evkev says what kind of data these are -- |
| 381 |
## encoder and lambda data need to be monotonic *decreasing* in those |
| 382 |
## values so that they will be increasing in energy when converted |
| 383 |
sub monotonic_data { |
| 384 |
shift; |
| 385 |
my ($group, $xaxis, $evkev) = @_; |
| 386 |
my @x = Ifeffit::get_array($xaxis); |
| 387 |
my @points; |
| 388 |
my $ok = 0; |
| 389 |
my $prev = $x[0]; |
| 390 |
if ($evkev =~ /enc|lambda/) { |
| 391 |
foreach (1 .. $#x) { |
| 392 |
if ($x[$_] >= $prev) {++$ok; push @points, $_+1} |
| 393 |
$prev = $x[$_]; |
| 394 |
}; |
| 395 |
} else { |
| 396 |
foreach (1 .. $#x) { |
| 397 |
if ($x[$_] <= $prev) {++$ok; push @points, $_+1} |
| 398 |
$prev = $x[$_]; |
| 399 |
}; |
| 400 |
}; |
| 401 |
return $ok, @points; |
| 402 |
}; |
| 403 |
|
| 404 |
## ================================================================= |
| 405 |
## Check to see if k-grid of input chi(k) data is uniform |
| 406 |
|
| 407 |
## check to see that input chi(k) data is on a rigorously uniform |
| 408 |
## k-grid. also check that the first point is either 0 or 0.05. |
| 409 |
## return 0 if this data fails either test. |
| 410 |
sub uniform_k_grid { |
| 411 |
shift; |
| 412 |
my $group = $_[0]; |
| 413 |
my @x = Ifeffit::get_array("$group.k"); |
| 414 |
return 0 unless ((abs($x[0]) < EPSI) or ($x[0]-0.05 < EPSI)); |
| 415 |
my $ok = 1; |
| 416 |
my $prev = sprintf "%.3f", $x[1] - $x[0]; |
| 417 |
foreach (2 .. $#x) { |
| 418 |
my $this = sprintf "%.3f", $x[$_] - $x[$_-1]; |
| 419 |
$ok = ($this eq $prev) ? 1 : 0; |
| 420 |
$prev = $this; |
| 421 |
}; |
| 422 |
return $ok; |
| 423 |
}; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
## ================================================================= |
| 429 |
## Recognize and handle backwards data |
| 430 |
|
| 431 |
## check to see if the x-axis array is in descending order. If it is, |
| 432 |
## then reverse all of this groups arrays before actually doing |
| 433 |
## anything with them. Ifeffit needs its data in strictly ascending |
| 434 |
## order. This should be called after a disposal of "read_data" and |
| 435 |
## before anything is done with the columns |
| 436 |
sub backwards_data { |
| 437 |
shift; |
| 438 |
my ($group, $xaxis) = @_; |
| 439 |
my $col_string = Ifeffit::get_string('$column_label'); |
| 440 |
my @cols = split(" ", $col_string); |
| 441 |
my @x = Ifeffit::get_array($xaxis); |
| 442 |
#print join(" ", @x[0..5]), $/; |
| 443 |
my $is_backwards = (($x[0] > $x[1]) and ($x[1] > $x[2]) and ($x[2] > $x[3]) and |
| 444 |
($x[3] > $x[4]) and ($x[4] > $x[5])); |
| 445 |
return 0 unless $is_backwards; |
| 446 |
#print "it's backwards!\n"; |
| 447 |
foreach (@cols) { |
| 448 |
@x = Ifeffit::get_array("$group.$_"); |
| 449 |
@x = reverse @x; |
| 450 |
Ifeffit::put_array("$group.$_", \@x); |
| 451 |
}; |
| 452 |
return 1; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
1; |
| 459 |
__END__ |