Monday, August 15, 2011

[Bash] manipulate variables from different netcdf files using nco

#!/bin/bash
# compute the toal precipitation (L+S)
# usage:
#           getTotalPrec.sh RainFile SnowFile
#    assuming the variable name for rain  : pr
#                                                 snow : prsn
#copyright @ http://scriptdemo.blogspot.com

ncVarNameRain="pr"
ncVarNameSnow="prsn"
if [ $# -eq 2 ]; then
   RainFile=$1; SnowFile=$2
else
   sed -n '3,6p' getTotalPrec.sh
   exit
fi

[ ! -e ${RainFile} ] && echo "${RainFile} does not exist!" && exit
[ ! -e ${SnowFile} ] && echo "${SnowFile} does not exist!" && exit
oriP="ori_${RainFile}"
cp ${RainFile} ${oriP}
eval "ncks -A -v ${ncVarNameSnow} ${SnowFile} ${oriP}"
eval "ncap2 -s 'prtot=${ncVarNameRain}+${ncVarNameSnow}' ${oriP} new${oriP}"
ncks -v prtot new${oriP} total_${oriP} && rm -f new${oriP}
eval "ncrename -v prtot,${ncVarNameRain} total_${oriP} && rm -f ${oriP}"

No comments:

ShowCalendar