| 1 |
#!/bin/bash |
| 2 |
# This is a shell script to combine multiple MITgcm mnc output files from |
| 3 |
# different tiles into one global file. |
| 4 |
# All of the variables should be in one directory, where this script is run. |
| 5 |
# |
| 6 |
# To combine all state.0000000000.t*.nc files, |
| 7 |
# gluemnc state.0000000000.*.nc |
| 8 |
# This will result in an output file state.0000000000.glob.nc |
| 9 |
# Where glob is for global. |
| 10 |
# |
| 11 |
# You can even combine all mnc files, use |
| 12 |
# gluemnc *.nc |
| 13 |
# This will result in a series of global files, |
| 14 |
# state.0000000000.glob.nc state.0000000100.glob.nc, ... |
| 15 |
# grid.0000000000.glob.nc grid.0000000100.glob.nc, ... |
| 16 |
# diag.0000000000.glob.nc diag.0000000100.glob.nc, ... |
| 17 |
# |
| 18 |
# **********WARNINGS********** |
| 19 |
# This will probably not work at all with exch2/cubed sphere. |
| 20 |
# In that case, you probably can assemble all of the tiles on a face, |
| 21 |
# but combining faces is currently not implemented. |
| 22 |
# |
| 23 |
# Be sure you have enough disk space for the copies! In this version |
| 24 |
# nothing is done to assure all of the data is copied. |
| 25 |
# |
| 26 |
# Be careful! It will be easy to exceed the 2 GB limit for the old 32-bit |
| 27 |
# version of netcdf. If you do not have large-file support or 64-bit netcdf |
| 28 |
# you will have to be clever in dividing up your tiled files, |
| 29 |
# e.g., along the time dimension before combining to global files. |
| 30 |
# ***************************** |
| 31 |
# |
| 32 |
# Good luck and happy gluing, |
| 33 |
# Baylor Fox-Kemper |
| 34 |
|
| 35 |
DEBUG=--dbg_lvl=0 |
| 36 |
|
| 37 |
mkdir gluedir |
| 38 |
ln -s `which xplodemnc` gluedir |
| 39 |
cd gluedir |
| 40 |
|
| 41 |
inone=$1 |
| 42 |
inone=${1:?"You must input mnc filenames to be glued"} |
| 43 |
|
| 44 |
for somefile in $@ |
| 45 |
do |
| 46 |
ln -s ../$somefile . |
| 47 |
if [ ! -s $somefile ]; then |
| 48 |
echo "Error: $somefile is missing or empty" |
| 49 |
exit 1 |
| 50 |
fi |
| 51 |
done |
| 52 |
|
| 53 |
prels=${@%.t???.nc} |
| 54 |
|
| 55 |
for somepre in $prels |
| 56 |
do |
| 57 |
inls=0 |
| 58 |
for somepres in $sprels |
| 59 |
do |
| 60 |
if [ "$somepre" = "$somepres" ]; then |
| 61 |
inls=1 |
| 62 |
fi |
| 63 |
done |
| 64 |
if [ "$inls" = "0" ]; then |
| 65 |
sprels=$sprels" "$somepre |
| 66 |
fi |
| 67 |
done |
| 68 |
|
| 69 |
prels=$sprels |
| 70 |
|
| 71 |
for somepre in $prels |
| 72 |
do |
| 73 |
echo Making $somepre.glob.nc... |
| 74 |
Xsls= |
| 75 |
Ysls= |
| 76 |
|
| 77 |
for somefile in $@ |
| 78 |
do |
| 79 |
if [ "${somefile%.t???.nc}" = "$somepre" ]; then |
| 80 |
echo Scanning $somefile... |
| 81 |
Xs=$(ncdump -vX -l 10000 $somefile | grep "X = ") |
| 82 |
Xs=${Xs#*;} |
| 83 |
Xs=${Xs%;*} |
| 84 |
Xs=$(echo $Xs | sed s/' '//g) |
| 85 |
Xsls=$Xsls$Xs" " |
| 86 |
|
| 87 |
Ys=$(ncdump -vY -l 10000 $somefile | grep "Y = ") |
| 88 |
Ys=${Ys#*;} |
| 89 |
Ys=${Ys%;*} |
| 90 |
Ys=$(echo $Ys | sed s/' '//g) |
| 91 |
Ysls=$Ysls$Ys" " |
| 92 |
fi |
| 93 |
done |
| 94 |
|
| 95 |
sYsls= |
| 96 |
sXsls= |
| 97 |
|
| 98 |
# Determine all the X locations |
| 99 |
countx=0 |
| 100 |
for someXs in $Xsls |
| 101 |
do |
| 102 |
inls=0 |
| 103 |
for somesXs in $sXsls |
| 104 |
do |
| 105 |
if [ "$someXs" = "$somesXs" ]; then |
| 106 |
inls=1 |
| 107 |
fi |
| 108 |
done |
| 109 |
if [ "$inls" = "0" ]; then |
| 110 |
sXsls=$sXsls$someXs" " |
| 111 |
countx=$((countx))+1 |
| 112 |
fi |
| 113 |
done |
| 114 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 115 |
echo $((countx)) tiles found in x-direction. |
| 116 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 117 |
|
| 118 |
# Determine all the Y locations |
| 119 |
county=0 |
| 120 |
for someYs in $Ysls |
| 121 |
do |
| 122 |
inls=0 |
| 123 |
for somesYs in $sYsls |
| 124 |
do |
| 125 |
if [ "$someYs" = "$somesYs" ]; then |
| 126 |
inls=1 |
| 127 |
fi |
| 128 |
done |
| 129 |
if [ "$inls" = "0" ]; then |
| 130 |
sYsls=$sYsls$someYs" " |
| 131 |
county=$((county))+1 |
| 132 |
fi |
| 133 |
done |
| 134 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 135 |
echo $((county)) tiles found in y-direction. |
| 136 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 137 |
|
| 138 |
countyy=1000 |
| 139 |
countxx=1000 |
| 140 |
|
| 141 |
cntls= |
| 142 |
for someX in $sXsls |
| 143 |
do |
| 144 |
countxx=$((countxx+1)) |
| 145 |
cntls=$cntls$countxx" " |
| 146 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 147 |
echo Prepping X tile $((countxx-1000)) |
| 148 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 149 |
|
| 150 |
for somefile in $@ |
| 151 |
do |
| 152 |
if [ "${somefile%.t???.nc}" = "$somepre" ]; then |
| 153 |
Xs=$(ncdump -vX -l 10000 $somefile | grep "X = ") |
| 154 |
Xs=${Xs#*;} |
| 155 |
Xs=${Xs%;*} |
| 156 |
Xs=$(echo $Xs | sed s/' '//g) |
| 157 |
|
| 158 |
if [ "$someX" = $Xs ]; then |
| 159 |
./xplodemnc $somefile |
| 160 |
for somesplit in $(ls *.$somefile) |
| 161 |
do |
| 162 |
withY=$(ncdump -h $somesplit | grep "Y =") |
| 163 |
if [ ${#withY} -gt 1 ]; then |
| 164 |
echo Changing Y to record variable in $somesplit |
| 165 |
ncpdq $DEBUG -O -a Y,T $somesplit $somesplit |
| 166 |
mv $somesplit $countxx.$somesplit |
| 167 |
fi |
| 168 |
|
| 169 |
withYp1=$(ncdump -h $somesplit | grep "Yp1 =") |
| 170 |
if [ ${#withYp1} -gt 1 ]; then |
| 171 |
Yp1len=${withYp1#*= } |
| 172 |
Yp1len=$((${Yp1len% ;}-1)) |
| 173 |
# Strip off the repeated value in Yp1 |
| 174 |
echo Changing Yp1 to record variable in $somesplit |
| 175 |
ncpdq $DEBUG -O -a Yp1,T -F -d Yp1,1,$Yp1len $somesplit $somesplit |
| 176 |
mv $somesplit $countxx.$somesplit |
| 177 |
fi |
| 178 |
done |
| 179 |
fi |
| 180 |
fi |
| 181 |
done |
| 182 |
done |
| 183 |
echo Tile names $cntls |
| 184 |
for countxx in $cntls |
| 185 |
do |
| 186 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 187 |
echo Combining X tile $((countxx-1000)) |
| 188 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 189 |
varls= |
| 190 |
cxfilels=$(ls $countxx.*) |
| 191 |
oldvar= |
| 192 |
for somefile in $cxfilels |
| 193 |
do |
| 194 |
varname=${somefile%.*} |
| 195 |
varname=${varname%.*} |
| 196 |
varname=${varname%.*} |
| 197 |
varname=${varname%.*} |
| 198 |
varname=${varname#*.} |
| 199 |
if [ "$varname" = "$oldvar" ]; then |
| 200 |
echo $varname repeated |
| 201 |
else |
| 202 |
varls=$varls$varname" " |
| 203 |
fi |
| 204 |
oldvar=$varname |
| 205 |
done |
| 206 |
|
| 207 |
echo Found these variables to combine: $varls |
| 208 |
|
| 209 |
for somevar in $varls |
| 210 |
do |
| 211 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 212 |
echo Combining $somevar files in Y |
| 213 |
echo YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY |
| 214 |
|
| 215 |
withY=$(ncdump -h $countxx.$somevar.$somepre.*.nc | grep "Y =") |
| 216 |
withYp1=$(ncdump -h $countxx.$somevar.$somepre.*.nc | grep "Yp1 =") |
| 217 |
|
| 218 |
ncrcat $DEBUG $countxx.$somevar.$somepre.*.nc $somevar.$somepre.gloy.$countxx.nc |
| 219 |
echo Just combined $countxx.$somevar |
| 220 |
rm $countxx.$somevar.$somepre.t???.nc |
| 221 |
|
| 222 |
if [ ${#withY} -gt 1 ]; then |
| 223 |
echo Changing T to record variable in $somevar.$somepre.gloy.$countxx.nc |
| 224 |
ncpdq $DEBUG -O -a T,Y $somevar.$somepre.gloy.$countxx.nc $somevar.$somepre.gloy.$countxx.nc |
| 225 |
fi |
| 226 |
|
| 227 |
if [ ${#withYp1} -gt 1 ]; then |
| 228 |
echo Changing T to record variable in $somevar.$somepre.gloy.$countxx.nc |
| 229 |
ncpdq $DEBUG -O -a T,Yp1 $somevar.$somepre.gloy.$countxx.nc $somevar.$somepre.gloy.$countxx.nc |
| 230 |
fi |
| 231 |
done |
| 232 |
done |
| 233 |
for somevar in $varls |
| 234 |
do |
| 235 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 236 |
echo Combining $somevar files in X... |
| 237 |
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 238 |
for somegloy in $(ls $somevar.$somepre.gloy.*.nc) |
| 239 |
do |
| 240 |
withX=$(ncdump -h $somegloy | grep "X =") |
| 241 |
withXp1=$(ncdump -h $somegloy | grep "Xp1 =") |
| 242 |
|
| 243 |
if [ ${#withX} -gt 1 ]; then |
| 244 |
echo Changing X to record variable in $somegloy |
| 245 |
ncpdq $DEBUG -O -a X,T $somegloy $somegloy |
| 246 |
fi |
| 247 |
|
| 248 |
if [ ${#withXp1} -gt 1 ]; then |
| 249 |
Xp1len=${withXp1#*= } |
| 250 |
Xp1len=$((${Xp1len% ;}-1)) |
| 251 |
# Strip off the repeated value in Xp1 |
| 252 |
echo Changing Xp1 to record variable in $somegloy |
| 253 |
echo ncpdq $DEBUG -O -a Xp1,T -F -d Xp1,1,$Xp1len $somegloy $somegloy |
| 254 |
ncpdq $DEBUG -O -a Xp1,T -F -d Xp1,1,$Xp1len $somegloy $somegloy |
| 255 |
fi |
| 256 |
done |
| 257 |
echo Combining $somevar.gloy files... |
| 258 |
ncrcat $DEBUG $somevar.$somepre.gloy.*.nc $somevar.$somepre.glob.nc |
| 259 |
rm $somevar.$somepre.gloy.*.nc |
| 260 |
|
| 261 |
if [ ${#withX} -gt 1 ]; then |
| 262 |
echo Changing T to record variable in $somevar.$somepre.glob.nc |
| 263 |
ncpdq $DEBUG -O -a T,X $somevar.$somepre.glob.nc $somevar.$somepre.glob.nc |
| 264 |
fi |
| 265 |
|
| 266 |
if [ ${#withXp1} -gt 1 ]; then |
| 267 |
echo Changing T to record variable in $somevar.$somepre.glob.nc |
| 268 |
ncpdq $DEBUG -O -a T,Xp1 $somevar.$somepre.glob.nc $somevar.$somepre.glob.nc |
| 269 |
fi |
| 270 |
ncrcat $DEBUG -A $somevar.$somepre.glob.nc $somepre.glob.nc |
| 271 |
rm $somevar.$somepre.glob.nc |
| 272 |
done |
| 273 |
done |
| 274 |
|
| 275 |
cp *.glob.nc .. |
| 276 |
cd .. |
| 277 |
rm -rf gluedir |