| #!/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}" |
Showing posts with label nco. Show all posts
Showing posts with label nco. Show all posts
Monday, August 15, 2011
[Bash] manipulate variables from different netcdf files using nco
Saturday, September 25, 2010
Pack netcdf file
| #!/bin/bash # A simple usage of ncpdq from nco (http://nco.sourceforge.net) # Please check more details from the nco website. # Usage: packnc.sh myncfile(s) # NOTE: There will be a loss of precision in type conversion # (converted to short by default) # Copyright by http://scriptdemo.blogspot.com/ IsNCPDQ=`which ncpdq` if [ ${#IsNCPDQ} -eq 0 ]; then echo "ncpdq can not be found in the path..." echo "Please double check your NetCDF Operator (NCO) Path" exit fi if [ $# -eq 0 ]; then sed -n '4p' packnc.sh exit else Nfiles=$* for ncfile in ${Nfiles} do [ -f ${ncfile} ] && ncpdq -P all_xst ${ncfile} tmpout_${ncfile} && mv tmpout_${ncfile} ${ncfile} done fi |
Friday, March 12, 2010
NCO example III
#!/bin/bash
# add a new row for the north pole with an average value of the last row
# Usage:
# NorthPole.sh ori_file.nc [ncvar mod_file.nc]
# NetCdf Operator (NCO) package is required
#
GuessVar(){
if [ -e $1 ]; then
ncvar=`ncdump -h $1 | grep "(time, lat, lon)" | awk '{print $2}' | awk -F\( '{print $1}'`
if [ ${#ncvar} -eq 0 ]; then
echo "Failed in guessing the variable name"
exit
else
echo " I guess the potential variable in the $1 is $ncvar"
fi
else
echo "$1 can not be found!!!"
exit
fi
}
CheckLength()
{
NL=`echo $1 | awk -F\. '{print NF}'` return $NL
}
#------------------------------------------------------------------------------------------------
if [ $# -lt 1 ]; then
sed -n '2,7p' NorthPole.sh
exit
else
if [ $# -eq 1 ]; then
if [ -e $1 ]; then
ncfile_ori=$1
ncfile_mod="mod_"${ncfile_ori}
GuessVar $ncfile_ori
else
echo "$1 can not be found!!!"
exit
fi
elif [ $# -eq 2 ]; then
ncfile_ori=$1
CheckLength $2
if [ $? -eq 1 ]; then
ncvar=$2
ncfile_mod="mod_"${ncfile_ori}
else
ncfile_mod=$2
GuessVar $ncfile_ori
fi
elif [ $# -eq 3 ]; then
ncfile_ori=$1
ncvar=$2
ncfile_mod=$3
fi
fi
echo "original nc file: $ncfile_ori, output ncfile: $ncfile_mod, variable: $ncvar"
myaddstr=" -s 'defdim(\"new_lat\",\$lat.size+1)' \
-s 'new_var[\$time,\$new_lat,\$lon]=0.0f'\
-s 'new_lat[\$new_lat]=0.0d'\
-s 'new_lat(0:\$lat.size-1)=lat'\
-s 'new_lat(\$lat.size)=90.0d' \
-s 'new_var(:,0:\$lat.size-1,0:\$lon.size-1)=${ncvar}' \
-s '*nnt=\$time.size; *nny=\$lat.size-1; *nnx=\$lon.size-1; *indt=0;' \
-s 'for (indt==0; indt<nnt; indt++) new_var(indt,nny+1,:)=${ncvar}(indt,nny,0:nnx).avg();'"
eval "ncap2 -O -v ${myaddstr} ${ncfile_ori} ${ncfile_mod}"
#rename variables
ncwa -O -a lat ${ncfile_mod} tmp_${ncfile_mod}
ncks -O -x -v lat tmp_${ncfile_mod} ${ncfile_mod}
ncrename -d new_lat,lat -v new_var,${ncvar} -v new_lat,lat ${ncfile_mod} && rm -f tmp_${ncfile_mod}
ncatted -a standard_name,lat,o,c,"latitude" ${ncfile_mod}
ncatted -a long_name,lat,o,c,"latitude" ${ncfile_mod}
ncatted -a units,lat,o,c,"degrees_north" ${ncfile_mod}
ncatted -a axis,lat,o,c,"Y" ${ncfile_mod}
# add a new row for the north pole with an average value of the last row
# Usage:
# NorthPole.sh ori_file.nc [ncvar mod_file.nc]
# NetCdf Operator (NCO) package is required
#
GuessVar(){
if [ -e $1 ]; then
ncvar=`ncdump -h $1 | grep "(time, lat, lon)" | awk '{print $2}' | awk -F\( '{print $1}'`
if [ ${#ncvar} -eq 0 ]; then
echo "Failed in guessing the variable name"
exit
else
echo " I guess the potential variable in the $1 is $ncvar"
fi
else
echo "$1 can not be found!!!"
exit
fi
}
CheckLength()
{
NL=`echo $1 | awk -F\. '{print NF}'` return $NL
}
#------------------------------------------------------------------------------------------------
if [ $# -lt 1 ]; then
sed -n '2,7p' NorthPole.sh
exit
else
if [ $# -eq 1 ]; then
if [ -e $1 ]; then
ncfile_ori=$1
ncfile_mod="mod_"${ncfile_ori}
GuessVar $ncfile_ori
else
echo "$1 can not be found!!!"
exit
fi
elif [ $# -eq 2 ]; then
ncfile_ori=$1
CheckLength $2
if [ $? -eq 1 ]; then
ncvar=$2
ncfile_mod="mod_"${ncfile_ori}
else
ncfile_mod=$2
GuessVar $ncfile_ori
fi
elif [ $# -eq 3 ]; then
ncfile_ori=$1
ncvar=$2
ncfile_mod=$3
fi
fi
echo "original nc file: $ncfile_ori, output ncfile: $ncfile_mod, variable: $ncvar"
myaddstr=" -s 'defdim(\"new_lat\",\$lat.size+1)' \
-s 'new_var[\$time,\$new_lat,\$lon]=0.0f'\
-s 'new_lat[\$new_lat]=0.0d'\
-s 'new_lat(0:\$lat.size-1)=lat'\
-s 'new_lat(\$lat.size)=90.0d' \
-s 'new_var(:,0:\$lat.size-1,0:\$lon.size-1)=${ncvar}' \
-s '*nnt=\$time.size; *nny=\$lat.size-1; *nnx=\$lon.size-1; *indt=0;' \
-s 'for (indt==0; indt<nnt; indt++) new_var(indt,nny+1,:)=${ncvar}(indt,nny,0:nnx).avg();'"
eval "ncap2 -O -v ${myaddstr} ${ncfile_ori} ${ncfile_mod}"
#rename variables
ncwa -O -a lat ${ncfile_mod} tmp_${ncfile_mod}
ncks -O -x -v lat tmp_${ncfile_mod} ${ncfile_mod}
ncrename -d new_lat,lat -v new_var,${ncvar} -v new_lat,lat ${ncfile_mod} && rm -f tmp_${ncfile_mod}
ncatted -a standard_name,lat,o,c,"latitude" ${ncfile_mod}
ncatted -a long_name,lat,o,c,"latitude" ${ncfile_mod}
ncatted -a units,lat,o,c,"degrees_north" ${ncfile_mod}
ncatted -a axis,lat,o,c,"Y" ${ncfile_mod}
Thursday, March 11, 2010
NCO example II ncap2
| addpole.nco: defdim("new_y",$y.size+1); new_uas[$new_y,$x]=0.0f; new_y[$new_y]=0.0f; new_y(0:$y.size-1)=y; new_y($y.size)=90.0f; new_uas(0:$y.size-1,0:$x.size-1)=uas; *idx=0; *meanpole=0.0; while (idx<$x.size) { meanpole+=uas($new_y.size-2,idx); idx++; } meanpole=meanpole/idx; new_uas($new_y.size-1,0:$x.size-1)=meanpole; |
ncap2 -v -S addpole.nco ori.nc modified.nc
Wednesday, January 20, 2010
NCO example
| #!/bin/bash # usage: # OBCjob.sh obcfile.nc OBCType VarType # @ http://scriptdemo.blogspot.com if [ $# != 3 ]; then sed -n '1,3p' OBCjob.sh exit fi obcfile=$1 obctype=$2 vartype=$3 # increase ncap2 -v -S mygrow${vartype}.nco $obcfile aa.nc ncdump aa.nc > aa1 && rm -f aa.nc sed '/x\ =\ 129\ /d' aa1 > aa0 && rm -f aa1 ncgen -o aa.nc aa0 && rm -f aa0 # ncrename -O -d new_x,x aa.nc case "$vartype" in 'TS') ncrename -O -v new_vosaline,vosaline aa.nc ncrename -O -v new_votemper,votemper aa.nc ;; 'U') ncrename -O -v new_vozocrtx,vozocrtx aa.nc ;; 'V') ncrename -O -v new_vomecrty,vomecrty aa.nc ;; *) echo "vartype=$vartype is not defined" exit ;; esac # case "$obctype" in 'N'|'n') if [ ! -e north.nc ]; then ncks -F -d y,111,111 -v nav_lon,nav_lat coordinates_arc5.nc north.nc ncwa -a y north.nc north2.nc mv north2.nc north.nc fi ncks -A -v nav_lon,nav_lat north.nc aa.nc && mv aa.nc new_$obcfile ;; 'S'|'s') if [ ! -e south.nc ]; then ncks -F -d y,2,2 -v nav_lon,nav_lat coordinates_arc5.nc south.nc ncwa -a y south.nc south2.nc mv south2.nc south.nc fi ncks -A -v nav_lon,nav_lat south.nc aa.nc && mv aa.nc new_$obcfile ;; *) echo "obctype=$obctype is not defined" exit ;; esac |
$cat mygrowTS.nco
defdim("new_x",$x.size+2);
new_vosaline[$time_counter,$z,$new_x]=0.0f;
new_votemper[$time_counter,$z,$new_x]=0.0f;
new_vosaline(0:$time_counter.size-1,0:$z.size-1,1:$x.size)=vosaline;
new_votemper(0:$time_counter.size-1,0:$z.size-1,1:$x.size)=votemper;
Subscribe to:
Posts (Atom)