Skip to content

Instantly share code, notes, and snippets.

@mikecharles
Created January 11, 2017 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecharles/e57988361573a19df514004fb5875427 to your computer and use it in GitHub Desktop.
Save mikecharles/e57988361573a19df514004fb5875427 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment