Remove grib records from grib files where the forecast hour (fhr) doesn't belong in that file
#!/bin/sh
ROOT_DIR=/path/to/data
START_DATE='19950101'
END_DATE='20161231'
cd $ROOT_DIR
# Loop over all dates within the given date range
curr_date=$START_DATE
until [[ $curr_date == $(date --date "$END_DATE 1 day" +%Y%m%d) ]] ; do
yyyy=${curr_date:0:4} ; mm=${curr_date:4:2} ; dd=${curr_date:6:2}
dir="$yyyy/$mm/$dd/00"
if [[ -d $dir ]] ; then
# Loop over all files in $dir
(
cd $dir
find . -name '*.grb' -print0 | while IFS= read -r -d $'\0' file ; do
# Get the fhr
fhr=$(echo $file | perl -ne '/_f(\d+)_/ && printf("%.0f", $1)')
# Remove grib records that don't have the correct fhr
if [[ "$fhr" == '0' ]] ; then
fhr_str=":anl:"
else
fhr_str=":${fhr}hr fcst:"
fi
cmd="wgrib $file | grep '$fhr_str' | wgrib -i $file -grib -o temp.grb >& /dev/null && mv -f temp.grb $file"
# echo $cmd
eval $cmd
done
)
fi
curr_date=$(date --date "$curr_date 1 day" +%Y%m%d)
done