Skip to content

Instantly share code, notes, and snippets.

@dbaston
Created July 10, 2018 18:24
Show Gist options
  • Save dbaston/f6a99bfb6cb7800b2cd28ff3b6279148 to your computer and use it in GitHub Desktop.
Save dbaston/f6a99bfb6cb7800b2cd28ff3b6279148 to your computer and use it in GitHub Desktop.
extract a (vertical, time) slice from a netCDF
#!/usr/bin/env bash
set -e
display_usage() {
echo "Extract a slice from a netCDF and remove the variable"
echo "extract.sh [in] [dimension] [value] [out]"
echo ""
echo "Example: extract.sh temperature.nc elevation 2 temperature_2.nc"
}
if [ $# -ne 4 ]
then
display_usage
exit 1
fi
N=1
TEMP1=`mktemp -u --suffix=.nc`
TEMP2=`mktemp -u --suffix=.nc`
TEMP3=`mktemp -u --suffix=.nc`
ncea -d ${2},${N},${N} -F ${1} ${TEMP1}
# Drop the time dimension
ncwa --no_tmp_fl -h -a ${2} ${TEMP1} ${TEMP2}
# Drop the time variable
ncks --no_tmp_fl -h -C -O -x -v ${2} ${TEMP2} ${TEMP3}
mv ${TEMP3} ${4}
rm ${TEMP1}
rm ${TEMP2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment