#!/usr/bin/perl

# Get input arguments
$paramfile=$ARGV[0];
$parammode=$ARGV[1];

# Define the extraction tables for the inversion programmes
if ( "$parammode" eq "p2z" )
   { @list = ('GRID.GEO_ZMIN',
              'GRID.GEO_NZ',
              'GRID.GEO_DZ'); }
              
if ( "$parammode" eq "ref" )
   { @list = ('GRID.REF_R'); }

if ( "$parammode" eq "def_anomaly" )
   { @list = ('ANOMALY.BOX_XMIN',
              'ANOMALY.BOX_XMAX',
              'ANOMALY.BOX_YMIN',
              'ANOMALY.BOX_YMAX',
              'ANOMALY.BOX_ZMIN',
              'ANOMALY.BOX_ZMAX',
              'ANOMALY.NFILTER',
              'ANOMALY.BOUND_XY',
              'ANOMALY.BOUND_Z'); }

if ( "$parammode" eq "rotate_grid" )
   { @list = ('GRID.ROT_NX',
              'GRID.ROT_NY',
              'GRID.ROT_DX',
              'GRID.ROT_DY',
              'GRID.CLON',
              'GRID.CLAT',
              'GRID.CROT'); }

if ( "$parammode" eq "rotate_lalo" )
   { @list = ('GRID.GEO_NX',
              'GRID.GEO_NY',
              'GRID.GEO_DX',
              'GRID.GEO_DY',
              'GRID.GEO_XMIN',
              'GRID.GEO_YMIN',
              'GRID.CLON',
              'GRID.CLAT',
              'GRID.CROT'); }

if ( "$parammode" eq "add2p" )
   { @list = ('NUMERICS.TRANS_XY',
              'NUMERICS.PS_CHANGE' ); }

if ( "$parammode" eq "coastline" )
   { @list = ('GRID.CLON',
              'GRID.CLAT',
              'GRID.CROT'); }

if ( "$parammode" eq "prep_iteration" )
   { @list = ('NUMERICS.ALPHA'); }

if ( "$parammode" eq "inp_dir" )
   { @list = ('DATA.INP_DIR'); };

if ( "$parammode" eq "run_dir" )
   { @list = ('DATA.RUN_DIR'); };

if ( "$parammode" eq "out_dir" )
   { @list = ('DATA.OUT_DIR'); };

if ( "$parammode" eq "n_of_iteration" )
   { @list = ('NUMERICS.NOFITER'); };

if ( "$parammode" eq "save_iteration" )
   { @list = ('NUMERICS.SAVEITER'); };

if ( "$parammode" eq "date" )
   { @list = ('DATA.DATE'); };

# Read the parameter file
@zeilen = `more $paramfile`;

# Set the first parameter which should be retrieved
foreach $listentry (@list)
{
  # Get section and name of the parameter
  @param = split /\./, "$listentry";
  $found=0;

  # Loop over all variables 
  foreach $zeile ( @zeilen )
  { 
 
    # -------------- BEGIN ------------------------------------
    if ( $zeile =~ 'BEGIN' )
    {
      @field = split / /, "$zeile";
      $_=$field[1];s/\s+//g;$bmode=$_;
    }

    # -------------- ASSIGNMENT -------------------------------
    if ( ($zeile =~ '=') && ($zeile =~ ';') )
    { 
      @field1 = split /=/, "$zeile";
      @field2 = split /;/, "$field1[1]";
      $_=$field1[0];s/\s+//g;$field1[0]=$_;
      $_=$field2[0];s/\s+//g;$field2[0]=$_;
      @field  = ( $field1[0], $field2[0] );
 
      if ( ("$bmode" eq "$param[0]") && ("$field[0]" eq "$param[1]" ) )
      {
         $found=1;
         print "$field[0] $field[1] \n";
      }
    }

    # -------------- END --------------------------------------
    if ( $zeile =~ 'END' )
    {
      @field = split / /, "$zeile";
      $_=$field[1];s/\s+//g;$emode=$_;
      if ( "$bmode" ne "$emode" )
      {
         die "Invalid structure of parameter file... $bmode\n";
      }
    }
  }
 if ( $found == 0 )
 {
   die("Parameter $listentry not found... Stop");
 }
}
