eccodes/definitions/create_legacy_wmo_defs.sh

85 lines
2.0 KiB
Bash
Raw Permalink Normal View History

2023-06-25 11:17:55 +00:00
:
set -eu
# Usage:
# create_legacy_def.sh $paramId $paramId...
#
2023-06-25 11:27:04 +00:00
# This script will insert the WMO GRIB2 representation
# for each given paramId into the files:
2023-06-25 11:17:55 +00:00
# definitions/grib2/paramId.legacy.def
# definitions/grib2/shortName.legacy.def
2023-06-25 11:27:04 +00:00
# etc.
# This is run for those GRIB2 parameters which had
2023-06-25 11:17:55 +00:00
# a WMO representation which later became deprecated.
#
# Assumptions:
# The ecCodes tools grib_set and grib_get are available
2023-06-25 11:27:04 +00:00
# TMPDIR is defined and points to a temporary directory
2023-06-25 11:17:55 +00:00
#
if [ $# -eq 0 ]; then
echo "Usage: $0 pid1 pid2 ..." 2>&1
exit 1
fi
# Input list of paramId values
pids="$@"
sample2=samples/GRIB2.tmpl
temp=$TMPDIR/temp.create_legacy_def.grib
defs=definitions/grib2
create_legacy_def()
{
_name=$1
_dir=$2
_file=$3
_legacy=$(echo $_file | sed -e 's/\.def/.legacy.def/')
#echo "Doing |$dir| f=|$file| l=|$legacy|"
2023-06-25 11:27:04 +00:00
# First check the given name exists in the file
2023-06-25 11:17:55 +00:00
grep -q "^#$_name$" $_dir/$_file
if [ $? -eq 0 ]; then
_name="#$_name"
found=1
2023-06-25 11:27:04 +00:00
# Copy the parameter definition from the file to its legacy version
2023-06-25 11:17:55 +00:00
echo "$_name" >> $_dir/$_legacy
sed -e "1,/^$_name$/d" -e '/#/,$d' $_dir/$_file | tr -d '\t' >> $_dir/$_legacy
fi
}
create_legacy_defs()
{
name="$1"
create_legacy_def "$name" $defs paramId.def
create_legacy_def "$name" $defs shortName.def
create_legacy_def "$name" $defs name.def
create_legacy_def "$name" $defs units.def
2023-06-25 11:27:04 +00:00
create_legacy_def "$name" $defs cfVarName.def
2023-06-25 11:17:55 +00:00
}
# For each paramId, get its full name
count=0
for pid in $pids; do
2023-06-25 11:27:04 +00:00
echo "Doing paramId=$pid ..."
2023-06-25 11:17:55 +00:00
set +e
grib_set -s paramId=$pid $sample2 $temp 2>/dev/null
status=$?
set -e
if [ $status -ne 0 ]; then
grib_set -s stepType=accum,paramId=$pid $sample2 $temp
fi
name=$(grib_get -p name $temp)
count=$((count+1))
rm -f $temp
create_legacy_defs "$name"
done
echo "Number of legacy parameters added: $count"
if [ $count -gt 0 ]; then
echo "Files updated. Check directory $defs"
fi
rm -f $temp