#!/local/bin/perl

while (<>) {
    chomp;
    ($lambda,$f) = split;
    $i = int (($lambda - 380)/5);
    $source[$i] = $f;
}

open (X,"<cie.x")
    or die "Can't open X: $!";
while (<X>) {
    chomp;
    ($lambda,$f) = split;
    $i = int (($lambda - 380)/5);
    $fX[$i] = $f;
}
close X;

open (Y,"<cie.y")
    or die "Can't open Y: $!";
while (<Y>) {
    chomp;
    ($lambda,$f) = split;
    $i = int (($lambda - 380)/5);
    $fY[$i] = $f;
}
close Y;

open (Z,"<cie.z")
    or die "Can't open Z: $!";
while (<Z>) {
    chomp;
    ($lambda,$f) = split;
    $i = int (($lambda - 380)/5);
    $fZ[$i] = $f;
}
close Z;

$X = $Y = $Z = 0.0;
for ($i=0; $i<=$#source; $i++) {
    $X += $fX[$i] * $source[$i];
    $Y += $fY[$i] * $source[$i];
    $Z += $fZ[$i] * $source[$i];
}

$denom = $X + $Y + $Z;
$x = $X/$denom;
$y = $Y/$denom;
$z = $Z/$denom;

print "X=$X Y=$Y Z=$Z\n";
print "x=$x y=$y z=$z\n";


