#!perl #
#
# Subroutine file 'sep.pl' to read data in the form of the SEP header
# or parameter files.
#
# Version: 5.20
# Date: 1998, November 2
#
# Coded by: Ludek Klimes
#     Department of Geophysics, Charles University Prague,
#     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
#     E-mail: klimes@seis.karlov.mff.cuni.cz
#
#.......................................................................
#
# This file consists of the following PERL subroutines:
#     RSEP1...Subroutine designed to read a SEP-like parameter or header
#             file and to store the parameter names and values for
#             future use.
#             RSEP1
#     RSEP3...Subroutine designed to read the value of a given text,
#             integer or real-valued parameter from a previously stored
#             contents of SEP-like parameter or header file.
#             RSEP3
#
#.......................................................................
#
#                                                    
# Form of the SEP (Stanford Exploration Project) parameter files:
#     Refer to Fortran file sep.for.
#
#=======================================================================
#
# 
#
# Subroutine RSEP1($FILE)
# ~~~~~~~~~~~~~~~~~~~~~~~
# Subroutine designed to read a SEP-like parameter or header file and to
# store the parameter names and values for future use.
#
# Input:
#     $FILE...String containing the name of the input SEP parameter
#             file to be read.
#             If $FILE=' ', no action is done.
#
# No output.
#
#-----------------------------------------------------------------------
#
sub RSEP1 {
  package Sep;
  $FILE=$_[0];
#
  if ($FILE eq ' ') {
    @SEPSTR=();
  } else {
    open(LU,"<$FILE");
#   Reading the SEP file into string array @SEPSTR
    @SEPSTR=;
    close(LU) || die "Error when closing '$FILE'";
  }
}
#=======================================================================
#
# 
#
# Subroutine RSEP3($NAME,$OUT,$DEF)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Subroutine designed to read the value of a given text, integer or
# real-valued parameter from a previously stored contents of SEP-like
# parameter or header file.
#
# Input:
#     $NAME...String containing the name of the parameter.  Except for
#             its case, it should match the parameter name in the input
#             SEP parameter file.
#     $DEF... Default value of the parameter.
#
# Output:
#     $OUT... Value of the parameter.
#
#-----------------------------------------------------------------------
#
sub RSEP3 {
  package Sep;
  $NAME=$_[0]; $DEF=$_[2];
  #---------------------------------------------------------------------
  # Converting the parameter name to the lowercase string to be searched
  $nameeq=' '."\L$NAME\E".'=';
  #
  # Setting the default value
  $OUT=$DEF;
  #
  # Loop over lines
  foreach $SEPSTR (@SEPSTR) {
    $i=index($SEPSTR,'#',0)-1;
    if ($i==-2) {
      $i=length($SEPSTR);
    }
#   Converting string $SEPSTR to lowercase
    $sepstr="\L$SEPSTR\E";
    $i=rindex($sepstr,$nameeq,$i);
    if ($i>-1) {
      # Line contains string $nameeq
      $i=$i+length($nameeq);
      if (substr($SEPSTR,$i,1) eq "'") {
        # Parameter value is a string in apostrophes
        $i=$i+1;
        $j=index($SEPSTR,"'",$i);
      } elsif (substr($SEPSTR,$i,1) eq '"') {
        # Parameter value is a string in double quotes
        $i=$i+1;
        $j=index($SEPSTR,'"',$i);
      } else {
        # Parameter value is terminated by ' ' or ',' or end of line or '#'
        $j=index($SEPSTR,' ',$i);
        $k=index($SEPSTR,',',$i);
        if ($j<=-1 || ($k>-1 && $j>$k)) {
          $j=$k;
        }
        if ($j<=-1) {
          $j=length($SEPSTR)-1;
        }
        $k=index($SEPSTR,'#',0);
        if ($k>-1 && $j>$k) {
          $j=$k;
        }
      }
      $j=$j-$i;
      if ($j>0) {
        $OUT=substr($SEPSTR,$i,$j);
      }
    }
  }
  #---------------------------------------------------------------------
  $_[1]=$OUT;
}
#=======================================================================
1;                                                               #