#!/bin/bash

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

#                                                 2007-10-10/vk
#
# This script is called by src/AppMod/examcback.cc. It extracts
# one GRIB msg from input file and calls 'grib_dump' to examine
# its metadata.

#set -x

GRIBFILE=$1
OFFSET=$2
LENGTH=$3


echo " Extracted GRIB message: FIRST OCTET $OFFSET, LENGTH $LENGTH"

TMPFILE=$METVIEW_TMPDIR/mv_grib_dump_tmp.A_$$
TMPFILEB=$METVIEW_TMPDIR/mv_grib_dump_tmp.B_$$

#-- why does 'tail' issue 'write error' with this:
#tail --bytes=+$OFFSET $GRIBFILE | head --bytes=$LENGTH > $TMPFILE

#-- no error msgs with these:
tail -c +$OFFSET "$GRIBFILE" > $TMPFILEB
head -c $LENGTH $TMPFILEB > $TMPFILE
rm $TMPFILEB

#-- 'grib_dump' is from 'grib_api' tool box
$METVIEW_GRIB_DUMP -O $TMPFILE

rm $TMPFILE

