#/usr/bin/ksh
#
# Script to compare old and revised ECMWF BUFR libaries,
# as used at ROPP9.0 and ROPP9.1.
#

IFILE0=data2/ropp_test_bufr # Metop-A data
IFILE0=data2/bfrPrf_KOM5.2018.182.00.04.G27_0001.0007_bufr # K5 data
IFILE0=data2/A_IUTA14BAWX160008_C_BAWX_20170616025049_bufr # FY3C data
IFILE0=data2/A_IUTA14BAWX160008_C_BAWX_20170616025049_new_bufr # FY3D data
IFILE0=data2/T_IUTF14_C_DEMS_20180223075850_G05_ROSA_MT1_bufr # MT-ROSA data
IFILE0=data2/GA-AI-3-NRT+2011_052_1100_G23_zdif_006_bufr # GRACE-A data
IFILE0=data2/GB-AI-3-NRT+2011_052_1100_G23_zdif_006_bufr # GRACE-B data
IFILE0=data2/GC-AI-3-NRT+2011_052_1100_G23_zdif_006_bufr # GRACE-C FO data
IFILE0=data2/GD-AI-3-NRT+2011_052_1100_G23_zdif_006_bufr # GRACE-D FO data

#------------------------------------------------------
#0. Cntl: Old ECMWF BUFR: bufr_000387, built with gfortran
#------------------------------------------------------

#if false ; then  #  bypass the control

# Move the appropriate files are placed in $BUFR_TABLES, in case
# the other BUFR lib was built last.  Note that we have to use the MetDB files,
# because decbufr is only an element of the MetDB BUFR library.

#SRC_DIR=libs/bufr_000387/bufrtables
#TGT_DIR=$BUFR_LIBRARY

#for file in C0000000000098013001.TXT ; do
#  cp $SRC_DIR/$file $TGT_DIR
#done

SRC_DIR=libs/bufr-24.0.2/extra
TGT_DIR=$BUFR_TABLES

for file in bufr_codefig bufr_origcentre ; do
  cp $SRC_DIR/$file $TGT_DIR
done


IFILE=$IFILE0

# Dump the bufr
EXEC=$ROPP_ROOT/gfortran/bin/decbufr

TFILE1=`echo $IFILE0 |sed -es/'_bufr'/'_decbufr_old'/`

echo "$EXEC  -d  $IFILE  > $TFILE1 2>&1"
      $EXEC  -d  $IFILE  > $TFILE1 2>&1

# Convert to nc
EXEC=$ROPP_ROOT/gfortran/bin/bufr2ropp

OFILE=`echo $IFILE0 |sed -es/'_bufr'/'_nc_old'/`

TFILE=`echo $IFILE0 |sed -es/'_bufr'/'_txt_old'/`

echo "$EXEC  $IFILE  -o $OFILE  -d    > $TFILE 2>&1"
      $EXEC  $IFILE  -o $OFILE  -d    > $TFILE 2>&1


echo "grep 'Originating Centre' $TFILE1"
echo "grep 'Originating Sub-Centre' $TFILE1"
echo "grep 'SATELLITE IDENTIFIER' $TFILE1"
echo "grep 'SATELLITE INSTRUMENTS' $TFILE1"
echo "grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1"

      grep 'Originating Centre' $TFILE1
      grep 'Originating Centre' $TFILE1 >> $TFILE

      grep 'Originating Sub-Centre' $TFILE1
      grep 'Originating Sub-Centre' $TFILE1 >> $TFILE

      grep 'SATELLITE IDENTIFIER' $TFILE1
      grep 'SATELLITE IDENTIFIER' $TFILE1 >> $TFILE

      grep 'SATELLITE INSTRUMENTS' $TFILE1
      grep 'SATELLITE INSTRUMENTS' $TFILE1 >> $TFILE

      grep 'SATELLITE CLASSIFICATION' $TFILE1
      grep 'SATELLITE CLASSIFICATION' $TFILE1 >> $TFILE

      grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1
      grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1 >> $TFILE


echo "ncks -H -Q -vleo_id,gns_id,occ_id $OFILE"
      ncks -H -Q -vleo_id,gns_id,occ_id $OFILE
      ncks -H -Q -vleo_id,gns_id,occ_id $OFILE >> $TFILE

#fi  #  bypass the control

#------------------------------------------------------
#1. Test: New ECMWF BUFR: bufr_000387a, built with ifort12
#------------------------------------------------------

#if false ; then  #  bypass the test

# Move the appropriate files are placed in $BUFR_TABLES, in case
# the other BUFR lib was built last.  Note that we have to use the MetDB files,
# because decbufr is only an element of the MetDB BUFR library.

#SRC_DIR=libs/bufr_000387a/bufrtables
#TGT_DIR=$BUFR_LIBRARY

#for file in C0000000000098013001.TXT ; do
#  cp $SRC_DIR/$file $TGT_DIR
#done

SRC_DIR=libs/bufr-24.0.2a/extra
TGT_DIR=$BUFR_TABLES

for file in bufr_codefig bufr_origcentre ; do
  cp $SRC_DIR/$file $TGT_DIR
done


IFILE=$IFILE0

# Dump the bufr
EXEC=$ROPP_ROOT/ifort12/bin/decbufr

TFILE1=`echo $IFILE0 |sed -es/'_bufr'/'_decbufr_new'/`

echo "$EXEC  -d  $IFILE  > $TFILE1 2>&1"
      $EXEC  -d  $IFILE  > $TFILE1 2>&1

# Convert to nc
EXEC=$ROPP_ROOT/ifort12/bin/bufr2ropp

OFILE=`echo $IFILE0 |sed -es/'_bufr'/'_nc_new'/`

TFILE=`echo $IFILE0 |sed -es/'_bufr'/'_txt_new'/`

echo "$EXEC  $IFILE  -o $OFILE  -d    > $TFILE 2>&1"
      $EXEC  $IFILE  -o $OFILE  -d    > $TFILE 2>&1


echo "grep 'Originating Centre' $TFILE1"
echo "grep 'Originating Sub-Centre' $TFILE1"
echo "grep 'SATELLITE IDENTIFIER' $TFILE1"
echo "grep 'SATELLITE INSTRUMENTS' $TFILE1"
echo "grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1"

      grep 'Originating Centre' $TFILE1
      grep 'Originating Centre' $TFILE1 >> $TFILE

      grep 'Originating Sub-Centre' $TFILE1
      grep 'Originating Sub-Centre' $TFILE1 >> $TFILE

      grep 'SATELLITE IDENTIFIER' $TFILE1
      grep 'SATELLITE IDENTIFIER' $TFILE1 >> $TFILE

      grep 'SATELLITE INSTRUMENTS' $TFILE1
      grep 'SATELLITE INSTRUMENTS' $TFILE1 >> $TFILE

      grep 'SATELLITE CLASSIFICATION' $TFILE1
      grep 'SATELLITE CLASSIFICATION' $TFILE1 >> $TFILE

      grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1
      grep 'IDENTIFICATION OF ORIGINATING/GENERATING CENTRE' $TFILE1 >> $TFILE


echo "ncks -H -Q -vleo_id,gns_id,occ_id $OFILE"
      ncks -H -Q -vleo_id,gns_id,occ_id $OFILE
      ncks -H -Q -vleo_id,gns_id,occ_id $OFILE >> $TFILE

#fi  #  bypass the test
