-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.sh
60 lines (49 loc) · 1.65 KB
/
merge.sh
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
#!/bin/bash
# ================================================================================
# Merge multiple netCDF files in time.
#
# The script will merge files that have a similar filename. Files are considered
# similar if their filenames before the last underscore match. For example,
# `file_1_a.nc` and `file_1_b.nc` are considered as one group and merged into
# `file_1.nc`. The merged files are saved in a subfolder named `merged`.
#
# Usage:
# ./merge.sh [path]
#
# Options:
# path path to netCDF files
#
# Author: Andreas Groth
# ================================================================================
export SKIP_SAME_TIME=1 # skip duplicate time steps in mergetime
path=${1:-'.'}
outpath="$path/merged"
mkdir -p "$outpath"
# colors
color='\033[0;33m' # yellow
reset='\033[0m' # color reset
# find unique filenames based on substring = prefix
declare -A prefixes=()
for infile in "$path"/*.nc; do
# extract substring before last underscore
inname=$(basename "$infile")
prefix="${inname%_*}"
# increment entry (strings of 1)
prefixes[$prefix]+=1
done
N=${#prefixes[@]}
echo "Found $N group(s) in '$path'"
n=1
# merge files with same prefix
for prefix in "${!prefixes[@]}"; do
echo -e "${color}$n/$N Merge '$prefix*.nc': ${#prefixes[$prefix]} file(s) found.${reset}"
outfile="$outpath/$prefix.nc"
if [ -e "$outfile" ]; then
ntime=$(cdo -s ntime "$outfile")
npar=$(cdo -s npar "$outfile")
echo "Skip '$outfile'. File already exists with $npar variables over $ntime timesteps."
else
cdo -f nc4 -z zip -r -mergetime "$path/$prefix"*.nc "$outfile"
fi
n=$((n + 1))
done