Debian Codegraph @

artemis_parts/feffit.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2007 Bruce Ravel
3 ##
4 ## convert a feffit input file (and its include files) into an artemis
5 ## project
6
7
8 sub feffit_convert_input {
9 my ($file) = @_;
10
11 my @feffit = ();
12 $feffit[0] = {titles=>[], opparams=>{}, path=>[], feffcalcs=>[]};
13 my @gds = ();
14
15
16 my $pth = $current_data_dir || cwd;
17 my $types = [['Feffit input file', '*.inp'],
18 ['All files', '*'],];
19 $file ||= $top -> getOpenFile(-filetypes=>$types,
20 ##(not $is_windows) ?
21 ## (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
22 -initialdir=>$pth,
23 -title => "Artemis: Open a feffit input file");
24 return unless ($file);
25 Error("$file does not exist!"), return unless (-e $file);
26 my ($name,$path,$suffix) = fileparse($file);
27
28 open *I, $file or die "could not open $file for reading";
29 feffit_parse_file(\@feffit, \@gds, $path, *I);
30 close *I;
31 feffit_finish(\@feffit, \@gds, $path, $file);
32
33 &push_mru($file, 1, "feffit");
34 1;
35 };
36
37
38
39 sub feffit_parse_file {
40 my ($r_feffit, $r_gds, $path, $file) = @_;
41 while (<$file>) {
42 next if m{^\s*$}; # blank lines
43 next if m{^\s*[#!*%]}; # comment lines
44 chomp;
45 feffit_parse_line($r_feffit, $r_gds, $path, $_);
46 };
47 return 1;
48 };
49
50
51 sub feffit_parse_line {
52 my ($r_feffit, $r_gds, $path, $line) = @_;
53
54 ## feffit keywords
55 my @ignore = qw(format rspout kspout qspout allout bkgfile output);
56 my @opparams = qw(bkg data kmin kmax rmin rmax dk dr kw nodegen);
57 my $allre = join("|", @ignore, @opparams);
58 my $igre = join("|", @ignore);
59 my $opre = join("|", @opparams);
60 my @pathparams = qw(path id e0 s02 sigma2 delr ei third fourth);
61 my $ppre = join("|", @pathparams);
62
63 $line =~ s{^\s+}{}; # trim leading blanks
64 $line =~ s{\#.*$}{}; # trim trailing comments
65 $line =~ s{\s+$}{}; # trim trailing blanks
66 $line =~ s{nodegen}{nodegen=1};
67 #$line = lc($line);
68 my $dataset = $#{$r_feffit};
69 LINE: {
70 ($line =~ m{\Anext}i) and do {
71 ++$dataset;
72 $r_feffit->[$dataset] = {titles=>[], opparams=>{}, path=>[], feffcalcs=>[]};
73 last LINE;
74 };
75
76 ($line =~ m{\Atitle}i) and do {
77 $line =~ s{\Atitle\s*[ \t=,]\s*}{}i;
78 ## $line now contains the title line, push it onto titles list
79 push @{ $r_feffit->[$dataset]->{titles} }, $line;
80 last LINE;
81 };
82
83 ($line =~ m{\A(?:guess|local|set)}i) and do {
84 ## $line now contains the gds line, push it onto gds list
85 push @$r_gds, $line;
86 last LINE;
87 };
88
89 ($line =~ m{^end}i) and do {
90 feffit_finish($r_feffit, $r_gds, $path);
91 last LINE;
92 };
93
94 ($line =~ m{^include}i) and do {
95 $line =~ s{\Ainclude\s*[ \t=,]\s*}{}i;
96 ## $line now contains the include file, call feffit_parse_file
97 my $newfile = File::Spec->catfile($path,$line);
98 open *INC, $newfile or die "could not read from $newfile\n";
99 feffit_parse_file($r_feffit, $r_gds, $path, *INC);
100 close *INC;
101 last LINE;
102 };
103
104 ($line =~ m{^($ppre)\s*[ \t=,]\s*(\d+)\s*[ \t=,]\s*(.*)}i) and do {
105 ## push this path parameter onto its list
106 feffit_parse_pathparam($r_feffit, $line);
107 last LINE;
108 };
109
110 ($line =~ m{^(?:$allre)\s*[ \t=,]\s*}i) and do {
111 feffit_parse_opparam($r_feffit, $line, $path);
112 last LINE;
113 };
114 };
115 };
116
117
118 sub feffit_parse_pathparam {
119 my ($r_feffit, $line) = @_;
120 $line =~ s{[#!%].*$}{}; # remove end of line comments
121 my @pathparams = qw(path id e0 s02 sigma2 delr ei third fourth);
122 my $ppre = join("|", @pathparams);
123 $line =~ m{^($ppre)\s*[ \t=,]\s*(\d+)\s*[ \t=,]\s*(.*)}i;
124 my ($pp, $index, $me) = ($1, $2, $3);
125 my $dataset = $#{$r_feffit};
126 $r_feffit->[$dataset]->{path}->[$index]->{$pp} = $me;
127 };
128
129 sub feffit_parse_opparam {
130 my ($r_feffit, $line, $path) = @_;
131 $line =~ s{[#!%].*$}{}; # remove end of line comments
132 my @ignore = qw(format rspout kspout qspout allout bkgfile output);
133 my @opparams = qw(bkg data kmin kmax rmin rmax dk dr kw);
134 my $allre = join("|", @ignore, @opparams);
135 my $igre = join("|", @ignore);
136 my $opre = join("|", @opparams);
137 my %words = split(/\s*[ \t=,]\s*/, $line);
138 my $dataset = $#{$r_feffit};
139 foreach my $key (keys %words) {
140 next if (lc($key) =~ m{\A$igre\z}); # ignore some opparams
141 ## store the good opparams for this data set
142 if (lc($key) eq 'data') {
143 my $datafile = File::Spec->catfile($path, $words{$key});
144 $r_feffit->[$dataset]->{opparams}->{data} = $datafile;
145 } elsif (lc($key) eq 'bkg') {
146 if ($r_feffit->[$dataset]->{opparams}->{bkg} =~ m{^[1yt]}) {
147 $r_feffit->[$dataset]->{opparams}->{bkg} = 'yes';
148 } else {
149 $r_feffit->[$dataset]->{opparams}->{bkg} = 'no';
150 };
151 } else {
152 $r_feffit->[$dataset]->{opparams}->{$key} = $words{$key};
153 };
154 };
155 };
156
157
158 sub feffit_finish {
159 my ($r_feffit, $r_gds, $path, $inpfile) = @_;
160 feffit_cull_mkw($r_feffit);
161 feffit_find_feffcalcs($r_feffit);
162
163 my %fix = feffit_load_gds($r_gds);
164 feffit_load_data($r_feffit, $path);
165 feffit_fix_gds(%fix);
166
167 $notes{messages} -> delete(qw(1.0 end));
168 use Data::Dumper;
169 $notes{messages} -> insert('end', Data::Dumper->Dump([$r_feffit], [qw(*feffit)]));
170 $notes{messages} -> insert('end', "\n");
171 $notes{messages} -> insert('end', Data::Dumper->Dump([$r_gds], [qw(*gds)]));
172 display_file('file', $inpfile) if $inpfile;
173 $top -> update;
174 raise_palette('messages');
175 display_page('data0');
176 plot('r', 0);
177 Echo("Imported feffit input file");
178 };
179
180
181 sub feffit_cull_mkw {
182 my ($r_feffit) = @_;
183 my $first_data = $r_feffit->[0]->{opparams}->{data};
184 foreach my $i (1 .. $#{$r_feffit}) {
185 my $this_data = $r_feffit->[$i]->{opparams}->{data};
186 next if ($this_data ne $first_data);
187 my $first_kw = $r_feffit->[0]->{opparams}->{kw};
188 $first_kw .= ',' . $r_feffit->[$i]->{opparams}->{kw};
189 $r_feffit->[0]->{opparams}->{kw} = $first_kw;
190 $r_feffit->[$i] = undef;
191 };
192
193 };
194
195 sub feffit_find_feffcalcs {
196 my ($r_feffit) = @_;
197 foreach my $set (@$r_feffit) {
198 next if not defined($set);
199 my %seen;
200 my @dirlist;
201 my $list = $set->{path};
202 my $count = -1;
203 foreach my $p (@$list) {
204 next if not defined($p);
205 my $nnnn = $p->{path};
206 next if not defined($nnnn); # take care with path 0
207 my $pth = dirname($nnnn);
208 $seen{$pth} or push @dirlist, $pth;
209 ++$seen{$pth};
210 };
211 foreach my $feff (@dirlist) {
212 push @{$set->{feffcalcs}}, $feff;
213 };
214 };
215 };
216
217
218 sub feffit_load_gds {
219 my ($r_gds) = @_;
220 my %fix = ();
221 my $count = 0;
222 foreach my $text (@$r_gds) {
223 $text =~ s{[#!%].*$}{}; # remove end of line comments
224 $text =~ s{\s+$}{}; # trim trailing blanks
225 my (@words) = split(/\s*[ \t=,]\s*/, $text);
226 my $type = shift @words;
227 my $name = shift @words;
228 my $me = join("", @words);
229 ## make it a def if it's a math expression and a set if its a number
230 if (($type eq 'set') and ($me !~ m{\A[+-]?(?:\d+\.?\d*|\.\d+)\z})) {
231 $type = 'def';
232 };
233 ($type = 'skip') if ($type eq 'local'); # punt for now
234 ++$count;
235 jump_to_variable(lc($name), $type, 1, $me);
236 my $regex = join("|", qw(e0 ei s02 sigma2 third fourth dr dr1 dr2 dk dk1 dk2 etok pi));
237 if (lc($name) =~ m{\A($regex)\z}) {
238 $fix{lc($name)} = $count;
239 };
240 };
241 return %fix;
242 gds2_display(1);
243 };
244
245 sub feffit_fix_gds {
246 my %fix = @_;
247 my %replacement = ( e0 => 'enot',
248 ei => 'eimag',
249 s02 => 's_02',
250 sigma2 => 'sigsqr',
251 third => 'cumul3',
252 fourth => 'cumul4',
253 dr => 'd_r',
254 dr1 => 'dr_1',
255 dr2 => 'dr_2',
256 dk => 'd_k',
257 dk1 => 'dk_1',
258 dk2 => 'dk_2',
259 etok => 'e2k',
260 pi => 'pie',
261 );
262 foreach my $k (keys %fix) {
263 gds2_search_replace($fix{$k}, $replacement{$k});
264 };
265 gds2_display(1);
266 };
267
268
269 sub feffit_load_data {
270 my ($r_feffit, $path) = @_;
271 foreach my $set (@$r_feffit) {
272 next if not defined($set);
273 my $fname = $set->{opparams}->{data};
274
275 ## import the data
276 my $group = read_data(0, $fname, 0, 1);
277
278 ## import the operational parameter values and titles
279 foreach my $k (qw(kmin kmax rmin rmax dk)) {
280 $paths{$group}->make($k=>$set->{opparams}->{$k}) if $k;
281 };
282 $paths{$group}->make(k1=>1) if ($set->{opparams}->{kw} =~ m{1});
283 $paths{$group}->make(k2=>1) if ($set->{opparams}->{kw} =~ m{2});
284 $paths{$group}->make(k3=>1) if ($set->{opparams}->{kw} =~ m{3});
285 $paths{$group}->make(kwindow=>'Hanning');
286 $paths{$group}->make(rwindow=>'Hanning');
287
288 $widgets{op_titles} -> insert('1.0', "\n") if (@{ $set->{titles} });
289 foreach my $text (reverse @{ $set->{titles} }) {
290 $widgets{op_titles} -> insert('1.0', $text."\n");
291 };
292
293 ## import the feff calculations
294 my %pathto;
295 my $save = $config{autoparams}{do_autoparams};
296 $config{autoparams}{do_autoparams} = 0;
297 foreach my $dir (@{ $set->{feffcalcs} }) {
298 my $feffinp = File::Spec->catfile($path, $dir, "feff.inp");
299 my $id = read_feff($feffinp, 1);
300 $pathto{$dir} = $id;
301 };
302
303 ## import the feff paths and set their path parameters
304 my $default = {id=>q{}, s02=>1, e0=>0, delr=>0, sigma2=>0, third=>0, fourth=>0};
305 my $pathzero = $set->{path}->[0];
306 if (not defined($pathzero)) {
307 $pathzero = {id=>q{}, s02=>1, e0=>0, delr=>0, sigma2=>0, third=>0, fourth=>0};
308 };
309 foreach my $p (@{ $set->{path} }) {
310 next if not defined($p);
311
312 my $pth = $p->{path};
313 next if not defined($pth); # take care with path 0
314 $pth = dirname($pth);
315 my $id = $pathto{$pth};
316 $pth = $paths{$id}->{path};
317
318 my $nnnn = File::Spec->catfile($pth, basename($p->{path}));
319 next if (not -e $nnnn); # shelly's mkfit will make entries for paths that fail the crits2
320 my $key = add_a_path($nnnn, 1, 0);
321
322 $paths{$key}->make('label' => $p->{id} || $pathzero->{id} || $default->{id} );
323 $paths{$key}->make('s02' => lc($p->{s02}) || lc($pathzero->{s02}) || $default->{s02} );
324 $paths{$key}->make('e0' => lc($p->{e0}) || lc($pathzero->{e0}) || $default->{e0} );
325 $paths{$key}->make('delr' => lc($p->{delr}) || lc($pathzero->{delr}) || $default->{delr} );
326 $paths{$key}->make('sigma^2' => lc($p->{sigma2}) || lc($pathzero->{sigma2}) || $default->{sigma2});
327 $paths{$key}->make('ei' => lc($p->{ei}) || lc($pathzero->{ei}) || $default->{ei} );
328 $paths{$key}->make('3rd' => lc($p->{third}) || lc($pathzero->{third}) || $default->{third} );
329 $paths{$key}->make('4th' => lc($p->{fourth}) || lc($pathzero->{fourth}) || $default->{fourth});
330
331 };
332
333 # respect the nodegen flag
334 if ($set->{opparams}->{nodegen}) {
335 foreach my $dir (@{ $set->{feffcalcs} }) {
336 display_page($pathto{$dir});
337 set_degeneracy(1);
338 };
339 };
340
341 $config{autoparams}{do_autoparams} = $save;
342
343 display_page($group);
344 };
345 };
346
347 ## END OF THE SECTION ON FEFFIT CONVERSION
348