| 1 |
#!/bin/sh |
| 2 |
exec perl -x $0 ${1+"$@"} |
| 3 |
#!perl |
| 4 |
|
| 5 |
# fdf2fit - script to convert Origin-4 .fdf fit parameter files to ACE/gr's |
| 6 |
# Version 0.3a |
| 7 |
# Author Yu. V. Ralchenko <[email protected]> |
| 8 |
|
| 9 |
($input_file,$output_file) = @ARGV; |
| 10 |
|
| 11 |
# read from STDIN if input file name is '-' |
| 12 |
$input_file = 'STDIN' if $input_file eq '-'; |
| 13 |
|
| 14 |
open (INP,$input_file) or die "Can't open file $input_file: $!\n"; |
| 15 |
|
| 16 |
while (<INP>) { |
| 17 |
/^Function Name=([^;]+)/i && (($function_name = $1) =~ s/\s+$//) && next; |
| 18 |
/^Brief Description=([^;]+)/i && (($description = $1) =~ s/\s+$//) && next; |
| 19 |
/^Number Of Parameters=([^;]+)/i && (($numb_of_pars = $1) =~ s/\s+$//) && next; |
| 20 |
/^Number Of Independent Variables=(\d+)/i && ($1 > 1) && (print STDERR "\nMore than 1 independent variable!\n\n") && exit; |
| 21 |
if (s/^Names=(.+)/\1/i) { |
| 22 |
s/\s+//g; |
| 23 |
@a=split(/,/); |
| 24 |
foreach $ind (0..$numb_of_pars-1) { |
| 25 |
my $cin = 'orig2acegr'.$ind; |
| 26 |
$var_new{$a[$ind]} = $cin; |
| 27 |
} |
| 28 |
next; |
| 29 |
} elsif (s/^Initial Values=(.+)/\1/i) { |
| 30 |
s/\(.+?\)|\cM//g; |
| 31 |
chomp; |
| 32 |
@a=split(/,/); |
| 33 |
foreach $ind (0..$numb_of_pars-1) { |
| 34 |
my $cin = 'orig2acegr'.$ind; |
| 35 |
$ini_new{$cin} = $a[$ind] if $a[$ind] ne '--'; |
| 36 |
} |
| 37 |
next; |
| 38 |
} elsif (s/^Meanings=(.+)/\1/i) { |
| 39 |
s/\(.+?\)|\cM//g; |
| 40 |
chomp; |
| 41 |
@a=split(/,/); |
| 42 |
foreach $ind (0..$numb_of_pars-1) { |
| 43 |
my $cin = 'orig2acegr'.$ind; |
| 44 |
$mea_new{$cin} = $a[$ind]; |
| 45 |
} |
| 46 |
next; |
| 47 |
} elsif (s/^Lower Bounds=(.+)/\1/i) { |
| 48 |
s/\(.+?\)|\cM//g; |
| 49 |
chomp; |
| 50 |
@a=split(/,/); |
| 51 |
foreach $ind (0..$numb_of_pars-1) { |
| 52 |
my $cin = 'orig2acegr'.$ind; |
| 53 |
$low_new{$cin} = (($a[$ind] =~ /--/) ? '' : $a[$ind]); |
| 54 |
} |
| 55 |
next; |
| 56 |
} elsif (s/^Upper Bounds=(.+)/\1/i) { |
| 57 |
s/\(.+?\)|\cM//g; |
| 58 |
chomp; |
| 59 |
@a=split(/,/); |
| 60 |
foreach $ind (0..$numb_of_pars-1) { |
| 61 |
my $cin = 'orig2acegr'.$ind; |
| 62 |
$upp_new{$cin} = (($a[$ind] =~ /--/) ? '' : $a[$ind]); |
| 63 |
} |
| 64 |
next; |
| 65 |
} elsif (/Formula/i) { |
| 66 |
while (<INP>) {last unless /^\s*$/}; |
| 67 |
tr/\r//; |
| 68 |
#chomp; |
| 69 |
s/^y\s*=\s*//i; |
| 70 |
foreach $key (sort keys %var_new) { |
| 71 |
s/\b$key\b/$var_new{$key}/ig; |
| 72 |
} |
| 73 |
s/orig2acegr/a/g; |
| 74 |
s/\s+$//; |
| 75 |
$formula = 'y = '.$_; |
| 76 |
last; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (!defined $output_file) { |
| 81 |
($output_file = $input_file) =~ s/\.fdf$/\.fit/i; |
| 82 |
open (OUT,">$output_file") or die "Can't open file $output_file: $!\n"; |
| 83 |
$OUT='OUT'; |
| 84 |
} elsif ($output_file ne '-') { |
| 85 |
open (OUT,">$output_file") or die "Can't open file $output_file: $!\n"; |
| 86 |
$OUT='OUT'; |
| 87 |
} else { |
| 88 |
$OUT='STDOUT'; |
| 89 |
} |
| 90 |
|
| 91 |
print $OUT "#fit name \"$function_name\"\n"; |
| 92 |
print $OUT "fit title \"$description\"\n"; |
| 93 |
print $OUT "fit with $numb_of_pars parameters\n"; |
| 94 |
print $OUT "fit formula \"$formula\"\n"; |
| 95 |
|
| 96 |
foreach $ind (0..$numb_of_pars-1) { |
| 97 |
$var='a'.$ind; |
| 98 |
$old='orig2acegr'.$ind; |
| 99 |
print $OUT "#$var description \"",$mea_new{$old},"\"\n"; |
| 100 |
print $OUT ($ini_new{$old}?'':'#'),$var," = $ini_new{$old}\n"; |
| 101 |
print $OUT (($low_new{$old} ? '' : '#'),$var,"min = $low_new{$old}\n"); |
| 102 |
print $OUT (($upp_new{$old} ? '' : '#'),$var,"max = $upp_new{$old}\n"); |
| 103 |
print $OUT $var," constraints "; |
| 104 |
print $OUT (($low_new{$old}=~/^\s*$/ and $upp_new{$old}=~/^\s*$/) ? "off\n" : "on\n"); |
| 105 |
} |
| 106 |
|
| 107 |
exit 0; |