| 1 |
#! /usr/bin/env bash |
| 2 |
# |
| 3 |
# $Header: /u/gcmpack/MITgcm/utils/scripts/gluemnc,v 1.13 2011/10/25 13:08:57 mlosch Exp $ |
| 4 |
# $Name: $ |
| 5 |
|
| 6 |
# This is a shell script to combine multiple MITgcm mnc output files from |
| 7 |
# different tiles into one global file. |
| 8 |
# All of the variables should be in one directory, where this script is run. |
| 9 |
# |
| 10 |
# To combine all state.0000000000.t*.nc files, |
| 11 |
# gluemnc state.0000000000.*.nc |
| 12 |
# This will result in an output file state.0000000000.glob.nc |
| 13 |
# Where glob is for global. |
| 14 |
# |
| 15 |
# You can even combine all mnc files, use |
| 16 |
# gluemnc *.nc |
| 17 |
# This will result in a series of global files, |
| 18 |
# state.0000000000.glob.nc state.0000000100.glob.nc, ... |
| 19 |
# grid.0000000000.glob.nc grid.0000000100.glob.nc, ... |
| 20 |
# diag.0000000000.glob.nc diag.0000000100.glob.nc, ... |
| 21 |
# |
| 22 |
# A lot of hard drive activity is needed. If you have a fast drive |
| 23 |
# export TMPDIR=<path of hard drive>. On some high-performance |
| 24 |
# systems, this is already done for you. |
| 25 |
# |
| 26 |
# **********WARNINGS********** |
| 27 |
# This will probably not work at all with exch2/cubed sphere. |
| 28 |
# In that case, you probably can assemble all of the tiles on a face, |
| 29 |
# but combining faces is currently not implemented. |
| 30 |
# |
| 31 |
# Be sure you have enough disk space for the copies! In this version |
| 32 |
# nothing is done to assure all of the data is copied. |
| 33 |
# |
| 34 |
# Be careful! It will be easy to exceed the 2 GB limit for the old 32-bit |
| 35 |
# version of netcdf. If you do not have large-file support or 64-bit netcdf |
| 36 |
# you will have to be clever in dividing up your tiled files, |
| 37 |
# e.g., along the time dimension before combining to global files. |
| 38 |
# The nco operator ncks is adept at shortening files to fewer snapshots. |
| 39 |
# ***************************** |
| 40 |
# |
| 41 |
# Good luck and happy gluing, |
| 42 |
# Baylor Fox-Kemper |
| 43 |
|
| 44 |
DEBUG="--dbg_lvl=0" |
| 45 |
LOGFILE="/dev/null" |
| 46 |
|
| 47 |
DIRORIG=`pwd` |
| 48 |
|
| 49 |
if [ ! ${#TMPDIR} -gt 0 ]; then |
| 50 |
TMPDIR=$DIRORIG |
| 51 |
fi |
| 52 |
|
| 53 |
export DIRNAME="$TMPDIR/gluedir.$RANDOM" |
| 54 |
mkdir $DIRNAME |
| 55 |
|
| 56 |
echo Using temporary directory $DIRNAME |
| 57 |
|
| 58 |
if [ -f xplodemnc ]; then |
| 59 |
cp xplodemnc $DIRNAME |
| 60 |
else |
| 61 |
cp `which xplodemnc` $DIRNAME |
| 62 |
fi |
| 63 |
|
| 64 |
cd $DIRNAME |
| 65 |
|
| 66 |
inone=$1 |
| 67 |
inone=${1:?"You must input mnc filenames to be glued"} |
| 68 |
|
| 69 |
for somefile in $@ |
| 70 |
do |
| 71 |
ln -s $DIRORIG/$somefile . |
| 72 |
if [ ! -s $somefile ]; then |
| 73 |
echo "Error: $somefile is missing or empty" |
| 74 |
exit 1 |
| 75 |
fi |
| 76 |
done |
| 77 |
|
| 78 |
prels=${@%.t???.nc} |
| 79 |
|
| 80 |
for somepre in $prels |
| 81 |
do |
| 82 |
inls=0 |
| 83 |
for somepres in $sprels |
| 84 |
do |
| 85 |
if [ "$somepre" = "$somepres" ]; then |
| 86 |
inls=1 |
| 87 |
fi |
| 88 |
done |
| 89 |
if [ "$inls" = "0" ]; then |
| 90 |
sprels=$sprels" "$somepre |
| 91 |
fi |
| 92 |
done |
| 93 |
|
| 94 |
prels=$sprels |
| 95 |
|
| 96 |
# ML: determine the coordinate variable (this is hack for the unlikely |
| 97 |
# case that we do not have X or Y as coordinate variables; this can |
| 98 |
# happen, when only U-point or V-point variables are written to a |
| 99 |
# diagnostics stream; I do not know if this always works, but it works for me) |
| 100 |
echo Determine a usable coordinate variable |
| 101 |
somefile=${prels}.t001.nc |
| 102 |
# first try X and Y |
| 103 |
Xcoord=X |
| 104 |
Ycoord=Y |
| 105 |
Xtest=$(ncdump -vX -l 10000 $somefile | grep "X = ") |
| 106 |
Ytest=$(ncdump -vY -l 10000 $somefile | grep "Y = ") |
| 107 |
if [ ${#Xtest} = 0 ]; then |
| 108 |
echo "X not found, trying Xp1" |
| 109 |
Xtest=$(ncdump -vXp1 -l 10000 $somefile | grep "Xp1 = ") |
| 110 |
Xcoord=Xp1 |
| 111 |
fi |
| 112 |
if [ ${#Xtest} = 0 ]; then |
| 113 |
echo "no X-coordinate found" |
| 114 |
Xcoord= |
| 115 |
fi |
| 116 |
if [ ${#Ytest} = 0 ]; then |
| 117 |
echo "Y not found, trying Yp1" |
| 118 |
Ytest=$(ncdump -vYp1 -l 10000 $somefile | grep "Yp1 = ") |
| 119 |
Ycoord=Yp1 |
| 120 |
fi |
| 121 |
if [ ${#Ytest} = 0 ]; then |
| 122 |
echo "no Y-coordinate found" |
| 123 |
Ycoord= |
| 124 |
fi |
| 125 |
if [ ${#Xcoord} = 0 ]; then |
| 126 |
echo cannot continue |
| 127 |
exit |
| 128 |
fi |
| 129 |
if [ ${#Ycoord} = 0 ]; then |
| 130 |
echo cannot continue |
| 131 |
exit |
| 132 |
fi |
| 133 |
|
| 134 |
for somepre in $prels |
| 135 |
do |
| 136 |
echo Making $somepre.glob.nc... |
| 137 |
Xsls= |
| 138 |
Ysls= |
| 139 |
|
| 140 |
for somefile in $@ |
| 141 |
do |
| 142 |
if [ "${somefile%.t???.nc}" = "$somepre" ]; then |
| 143 |
echo Scanning $somefile... |
| 144 |
Xs=$(ncdump -v${Xcoord} -l 10000 $somefile | grep "${Xcoord} = ") |
| 145 |
Xs=${Xs#*;} |
| 146 |
Xs=${Xs%;*} |
| 147 |
Xs=$(echo $Xs | sed s/' '//g) |
| 148 |
Xsls=$Xsls$Xs" " |
| 149 |
|
| 150 |
Ys=$(ncdump -v${Ycoord} -l 10000 $somefile | grep "${Ycoord} = ") |
| 151 |
Ys=${Ys#*;} |
| 152 |
Ys=${Ys%;*} |
| 153 |
Ys=$(echo $Ys | sed s/' '//g) |
| 154 |
Ysls=$Ysls$Ys" " |
| 155 |
fi |
| 156 |
done |
| 157 |
|
| 158 |
sYsls= |
| 159 |
sXsls= |
| 160 |
|
| 161 |
# Determine all the X locations |
| 162 |
countx=0 |
| 163 |
for someXs in $Xsls |
| 164 |
do |
| 165 |
inls=0 |
| 166 |
for somesXs in $sXsls |
| 167 |
do |
| 168 |
if [ "$someXs" = "$somesXs" ]; then |
| 169 |
inls=1 |
| 170 |
fi |
| 171 |
done |
| 172 |
if [ "$inls" = "0" ]; then |
| 173 |
sXsls=$sXsls$someXs" " |
| 174 |
countx=$((countx))+1 |
| 175 |
fi |
| 176 |
done |
| 177 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 178 |
echo $((countx)) tiles found in x-direction. |
| 179 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 180 |
|
| 181 |
# Determine all the Y locations |
| 182 |
county=0 |
| 183 |
for someYs in $Ysls |
| 184 |
do |
| 185 |
inls=0 |
| 186 |
for somesYs in $sYsls |
| 187 |
do |
| 188 |
if [ "$someYs" = "$somesYs" ]; then |
| 189 |
inls=1 |
| 190 |
fi |
| 191 |
done |
| 192 |
if [ "$inls" = "0" ]; then |
| 193 |
sYsls=$sYsls$someYs" " |
| 194 |
county=$((county))+1 |
| 195 |
fi |
| 196 |
done |
| 197 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 198 |
echo $((county)) tiles found in y-direction. |
| 199 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 200 |
|
| 201 |
countyy=1000 |
| 202 |
countxx=1000 |
| 203 |
|
| 204 |
cntls= |
| 205 |
for someX in $sXsls |
| 206 |
do |
| 207 |
countxx=$((countxx+1)) |
| 208 |
cntls=$cntls$countxx" " |
| 209 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 210 |
echo Prepping X tile $((countxx-1000)) |
| 211 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 212 |
|
| 213 |
for somefile in $@ |
| 214 |
do |
| 215 |
if [ "${somefile%.t???.nc}" = "$somepre" ]; then |
| 216 |
Xs=$(ncdump -v${Xcoord} -l 10000 $somefile | grep "${Xcoord} = ") |
| 217 |
Xs=${Xs#*;} |
| 218 |
Xs=${Xs%;*} |
| 219 |
Xs=$(echo $Xs | sed s/' '//g) |
| 220 |
|
| 221 |
if [ "$someX" = $Xs ]; then |
| 222 |
./xplodemnc $somefile |
| 223 |
if [ -f iter.$somefile ]; then |
| 224 |
mv iter.$somefile iter.${somefile%t???.nc}glob.nc |
| 225 |
fi |
| 226 |
for somesplit in $(ls *.$somefile) |
| 227 |
do |
| 228 |
# added by rpa to account for grid files with no T dimension defined |
| 229 |
hasT=$(ncdump -h $somesplit | grep "T =") |
| 230 |
if [[ -z "$hasT" ]]; then |
| 231 |
echo "No T record dimension found, adding one now" |
| 232 |
ncecat $DEBUG -O -u T $somesplit $somesplit > $LOGFILE |
| 233 |
fi |
| 234 |
withY=$(ncdump -h $somesplit | grep "Y =") |
| 235 |
if [ ${#withY} -gt 1 ]; then |
| 236 |
echo Changing Y to record variable in $somesplit |
| 237 |
ncpdq $DEBUG -O -a Y,T $somesplit $somesplit > $LOGFILE |
| 238 |
mv $somesplit i$countxx.$somesplit |
| 239 |
fi |
| 240 |
|
| 241 |
if [ -f $somesplit ]; then |
| 242 |
withYp1=$(ncdump -h $somesplit | grep "Yp1 =") |
| 243 |
if [ ${#withYp1} -gt 1 ]; then |
| 244 |
Yp1len=${withYp1#*= } |
| 245 |
Yp1len=$((${Yp1len% ;}-1)) |
| 246 |
# Strip off the repeated value in Yp1 |
| 247 |
echo Changing Yp1 to record variable in $somesplit |
| 248 |
ncpdq $DEBUG -O -a Yp1,T -F -d Yp1,1,$Yp1len $somesplit $somesplit > $LOGFILE |
| 249 |
mv $somesplit i$countxx.$somesplit |
| 250 |
fi |
| 251 |
fi |
| 252 |
done |
| 253 |
fi |
| 254 |
fi |
| 255 |
done |
| 256 |
done |
| 257 |
echo Tile names $cntls |
| 258 |
for countxx in $cntls |
| 259 |
do |
| 260 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 261 |
echo Combining X tile $((countxx-1000)) |
| 262 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 263 |
varls= |
| 264 |
cxfilels=$(ls i$countxx.*) |
| 265 |
oldvar= |
| 266 |
for somefile in $cxfilels |
| 267 |
do |
| 268 |
varname=`echo $somefile | sed 's/^i[0-9][0-9][0-9][0-9]\.//; s/\..*nc//'` |
| 269 |
if [ "$varname" = "$oldvar" ]; then |
| 270 |
echo $varname repeated |
| 271 |
else |
| 272 |
varls=$varls$varname" " |
| 273 |
fi |
| 274 |
oldvar=$varname |
| 275 |
done |
| 276 |
|
| 277 |
echo Found these variables to combine: $varls |
| 278 |
|
| 279 |
for somevar in $varls |
| 280 |
do |
| 281 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 282 |
echo Combining $somevar files in Y |
| 283 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 284 |
|
| 285 |
filelist=(`ls i$countxx.$somevar.$somepre.*.nc`) |
| 286 |
withY=$(ncdump -h ${filelist[0]} | grep "Y =") |
| 287 |
withYp1=$(ncdump -h ${filelist[0]} | grep "Yp1 =") |
| 288 |
|
| 289 |
ncrcat $DEBUG i$countxx.$somevar.$somepre.*.nc $somevar.$somepre.gloy.$countxx.nc > $LOGFILE |
| 290 |
echo Just combined $countxx.$somevar |
| 291 |
rm i$countxx.$somevar.$somepre.t???.nc |
| 292 |
|
| 293 |
if [ ${#withY} -gt 1 ]; then |
| 294 |
echo Changing T to record variable in $somevar.$somepre.gloy.$countxx.nc |
| 295 |
ncpdq $DEBUG -O -a T,Y $somevar.$somepre.gloy.$countxx.nc $somevar.$somepre.gloy.$countxx.nc > $LOGFILE |
| 296 |
fi |
| 297 |
|
| 298 |
if [ ${#withYp1} -gt 1 ]; then |
| 299 |
echo Changing T to record variable in $somevar.$somepre.gloy.$countxx.nc |
| 300 |
ncpdq $DEBUG -O -a T,Yp1 $somevar.$somepre.gloy.$countxx.nc $somevar.$somepre.gloy.$countxx.nc > $LOGFILE |
| 301 |
fi |
| 302 |
done |
| 303 |
done |
| 304 |
for somevar in $varls |
| 305 |
do |
| 306 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 307 |
echo Combining $somevar files in X... |
| 308 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 309 |
for somegloy in $(ls $somevar.$somepre.gloy.*.nc) |
| 310 |
do |
| 311 |
withX=$(ncdump -h $somegloy | grep "X =") |
| 312 |
withXp1=$(ncdump -h $somegloy | grep "Xp1 =") |
| 313 |
|
| 314 |
if [ ${#withX} -gt 1 ]; then |
| 315 |
echo Changing X to record variable in $somegloy |
| 316 |
ncpdq $DEBUG -O -a X,T $somegloy $somegloy > $LOGFILE |
| 317 |
fi |
| 318 |
|
| 319 |
if [ ${#withXp1} -gt 1 ]; then |
| 320 |
Xp1len=${withXp1#*= } |
| 321 |
Xp1len=$((${Xp1len% ;}-1)) |
| 322 |
# Strip off the repeated value in Xp1 |
| 323 |
echo Changing Xp1 to record variable in $somegloy |
| 324 |
echo ncpdq $DEBUG -O -a Xp1,T -F -d Xp1,1,$Xp1len $somegloy $somegloy > $LOGFILE |
| 325 |
ncpdq $DEBUG -O -a Xp1,T -F -d Xp1,1,$Xp1len $somegloy $somegloy > $LOGFILE |
| 326 |
fi |
| 327 |
done |
| 328 |
echo Combining $somevar.gloy files... |
| 329 |
ncrcat $DEBUG $somevar.$somepre.gloy.*.nc $somevar.$somepre.glob.nc > $LOGFILE |
| 330 |
# rm $somevar.$somepre.gloy.*.nc |
| 331 |
|
| 332 |
if [ ${#withX} -gt 1 ]; then |
| 333 |
echo Changing T to record variable in $somevar.$somepre.glob.nc |
| 334 |
ncpdq $DEBUG -O -a T,X $somevar.$somepre.glob.nc $somevar.$somepre.glob.nc > $LOGFILE |
| 335 |
fi |
| 336 |
|
| 337 |
if [ ${#withXp1} -gt 1 ]; then |
| 338 |
echo Changing T to record variable in $somevar.$somepre.glob.nc |
| 339 |
ncpdq $DEBUG -O -a T,Xp1 $somevar.$somepre.glob.nc $somevar.$somepre.glob.nc > $LOGFILE |
| 340 |
fi |
| 341 |
ncrcat $DEBUG -A $somevar.$somepre.glob.nc $somepre.glob.nc > $LOGFILE |
| 342 |
# rm $somevar.$somepre.glob.nc |
| 343 |
done |
| 344 |
if [ -f iter.$somepre.glob.nc ]; then |
| 345 |
ncrcat $DEBUG -A iter.$somepre.glob.nc $somepre.glob.nc > $LOGFILE |
| 346 |
fi |
| 347 |
# rm iter.$somepre.glob.nc |
| 348 |
|
| 349 |
# remove the dummy T dimension if it was added |
| 350 |
if [[ -z "$hasT" ]]; then |
| 351 |
ncwa $DEBUG -O -a T $somepre.glob.nc $somepre.glob.nc |
| 352 |
fi |
| 353 |
|
| 354 |
# another hack by rpa to accomodate grid.nc files |
| 355 |
# (there are several variables with just Z dimension that we want to keep) |
| 356 |
# varsz=$( ncdump -h $somepre.t001.nc | sed -n 's/^\s*\(double\|float\).* \(\w*\)(Z\w*).*/\2/p' ) |
| 357 |
# The OR ("\|") and "\s", "\w" only works for GNU-sed, but not for |
| 358 |
# BSD-sed or SunOS-sed, therefore we need to use some work-arounds: |
| 359 |
varsz=$( ncdump -h $somepre.t001.nc | egrep "double|float" | grep -v , | sed -n 's/.* \(.*\)(Z.*).*/\1/p' ) |
| 360 |
fixed= |
| 361 |
for varz in $varsz |
| 362 |
do |
| 363 |
# check to make sure the variable does not already exist in the glob file |
| 364 |
if [[ -z $( ncdump -h $somepre.glob.nc | grep " $varz(" ) ]] |
| 365 |
then |
| 366 |
echo "Adding variable $varz to $somepre.glob.nc" |
| 367 |
ncks $DEBUG -A -v $varz $somepre.t001.nc $somepre.glob.nc > $LOGFILE |
| 368 |
fixed='yes' |
| 369 |
fi |
| 370 |
done |
| 371 |
|
| 372 |
cp $somepre.glob.nc $DIRORIG |
| 373 |
done |
| 374 |
|
| 375 |
|
| 376 |
cd $DIRORIG |
| 377 |
rm -rf $DIRNAME |