-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnccombine
executable file
·62 lines (55 loc) · 2.33 KB
/
nccombine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
usage="nccombine OUTPUT INPUT1 [INPUT2 INPUT3...]"
doc="This script merges a NetCDF file along an arbitrary dimension using
'mppnccombine', or along the time or record dimension using 'ncrcat'.
Usage
$usage
Flags
-h Print this message.
"
# Parse args
which ncks &>/dev/null || raise "ncks not found. mppncdivide requires the NetCDF Operators (NCO) tools."
dir=${0%/*}
source $dir/header.sh
while [ $# -ne 0 ]; do
case $1 in
-h) echo "$doc" >&2 && exit 0 ;;
-*) raise "Unknown flag ${1}." ;;
*) [ -z "$output" ] && output="$1" || inputs+=("$1") ;;
esac
shift
done
[ ${#inputs[@]} -eq 0 ] && raise "No input inputs found."
[[ "$output" =~ .nc$ ]] || raise "File $output does not end in '.nc' extension." # output file
# Get the concatenation dimension by inspecting attributes
for input in "${inputs[@]}"; do
idimname=$(ncdump -h "${inputs[0]}" | grep domain_decomposition | cut -d: -f1 | xargs)
if [ -z "$idimname" ]; then
raise "File $input does not have dimension with domain_decomposition attribute. Perhaps your script deleted it!"
elif [ -n "$dimname" ] && [ "$dimname" != "$idimname" ]; then
raise "File $input has different domain_decomposition dimension, ${idimname}, from dimension from previous file, ${dimname}."
fi
dimname=$idimname
done
# Test whether concatenation dimension is unlimited
ncdump -h "${inputs[0]}" | grep 'UNLIMITED' | grep $dimname &>/dev/null \
&& unlimited=true || unlimited=false
# Combine inputs in one of two ways
# Found out that mppnccombine is *slower* than ncrcat! So if possible, always
# use ncrcat and do not fix unlimited dimensions!
# ./mppncdivide -f -d=time test.nc; rm tmp.nc; time ${dir}/mppnccombine.x tmp.nc test.*.nc
# ./mppncdivide -d=time test.nc; rm tmp.nc; time ncrcat -O test.*.nc tmp.nc
rm "$output" 2>/dev/null
if $unlimited; then
which ncrcat &>/dev/null \
|| raise "ncrcat not found. Please install the NetCDF Operators (NCO) or use the '-f' flag."
ncrcat -O -h "${inputs[@]}" "$output" \
|| raise "ncrcat failed."
# Combine with mppnccombine
else
[ -x ${dir}/mppnccombine.x ] \
|| raise "${dir}/mppnccombine.x executable not found. Please edit ${dir}/Makefile for your system, then generate mppnccombine.x by typing 'make' in the terminal."
${dir}/mppnccombine.x "$output" "${inputs[@]}" \
|| raise "mppnccombine.x failed."
fi
exit 0