#!/usr/bin/perl -w
#
#  cresprep.pl
#
#  Script for preparing the input file for CRES code.
#  CRES home page: http://sail.zpf.fer.hr/cres,
#  contact: Sasa Ilijic, email: sasa.ilijic@fer.hr
#
#  Use this script like this:
#
#  ./cresprep.pl < cresprep.in > cres.eqs
#
#  cresprep.in - in this file you specify the names of the files
#      containing observed spectra, you specify what wavelength
#      range should be loaded from each, and specify radial velocities
#      and light factors of each component in each observed spectrum.
#  cres.eqs - this file is created and contains all loaded data
#      in the format appropriate for CRES.  The last line of this file
#      contains the total number of data points in the file.
#      This file is also called "equations file" (input file).
#
#  cresprep.in syntax is:
#
#  EXCLUDE  wavefrom waveto
#  INCLUDE  filej codej  wavestart wavestop  rv1j lf1j  rv2j lf1j
#
#  Any number of EXCLUDE and INCLUDE statements may apprar in any order
#  in the cresprep.in file, hash (#) is allowed for commenting.
#  An EXCLUDE statement applies to all INCLUDEs that follow.
#  The role of an EXCLUDE statement is to forbid loading data points
#  from the specified wavelength range no matter what follows afterwards.
#  The INCLUDE statement loads specified datapoints from file filej,
#  (unless forbidden with earlier EXCLUDEs),
#
#  filej - name of file containing an observed spectrum.  It should have
#          three numbers per line: wavelength, amplitude and error
#  codej - a string of at most 12 chars used for later reference
#          to datapoints entered through particular INCLUDE statement
#  wavestart, wavestop - only datapoints from this range will be
#          loaded, processed, and passed to cres.eqs ("equations file")
#  rv1j, lf1j - radial velocity and light factor
#          assigned to the component 1 of the stellar system
#          in the observed spectrum stored in filej
#  rv2j, lf2j - rv and lf for component 2,
#          and so on if resolving more than two components
#
#  Any number of components may be specified with the INCLUDE statement,
#  provided that the same number of components is specified in all INCLUDEs
#  in particular cresprep.in file.
#
#  Note: CRES is far more general than this script makes it appear.
#  This script is only a simple working example of a way to prepare
#  the "equations file" for CRES.  It is very likely that this script
#  will require generalisations, and not the cres.c source file.
#  This is written in Perl, make sure that the first line beginning with
#  the hash and the exclamation mark (#!) is the first in the file.
#

$ec = 0;              # equations count
$c = 299800.0;        # speed of light

while ( defined ( $line = <> ) ) {

    chomp $line;
    $line =~ s/\s*\#.*//;
    next unless $line;

    if ( $line =~ /\s*EXCLUDE/ ) {

        push @exc, [ split " ", $' ];

    } elsif ( $line =~ /\s*INCLUDE/ ) {

        @inc = split " ", $';
        open ( FP, "$inc[0]" ) or die "Could not open $inc[0], $!\n";
        WAS: while ( defined ( $was = <FP> ) ) {
            $was =~ s/\#.*//;
            next unless $was =~ /(\S+)\s+(\S+)\s+(\S+)/;
            ( $w, $a, $s ) = ( $1, $2, $3 );
            next if $w < $inc[2] or $inc[3] < $w;
            for $x ( @exc ) {
                next WAS if @$x[0] < $w and $w < @$x[1];
            }
            printf "%9.4f %6.4f %5.4f ", $w, $a, $s;
            for ( $i = 4 ; $i < $#inc ; $i += 2 ) {
                printf "%6.4f %9.4f ", $inc[$i], (1.0-$inc[$i+1]/$c)*$w;
            }
            print " \# $inc[1]\n";
            $ec++;
        }
        close FP;

    } else {
 
        die "line \"$line\" does not obey syntax\n";

    }

}

print "\# nr of data pts (eqns) in this file: $ec\n";

#
#  end of script
#

