1 | #!/bin/bash |
---|
2 | ################################################################ |
---|
3 | ### Script to concatenate "diagpem.nc" outputs into one file ### |
---|
4 | ################################################################ |
---|
5 | set -e |
---|
6 | trap 'echo -e "\033[31mError: an issue occurred in the script on line $LINENO! Please review the command and try again.\033[0m"' ERR |
---|
7 | |
---|
8 | ################################################################ |
---|
9 | # Modify here the parameters for the script |
---|
10 | ########################################### |
---|
11 | # Output file name |
---|
12 | output_file="diagpem_concat.nc" |
---|
13 | |
---|
14 | # Frequency of PEM outputs |
---|
15 | ecritpem=1 |
---|
16 | |
---|
17 | # Number of initial PCM calls |
---|
18 | nPCM_ini=4 |
---|
19 | |
---|
20 | # Number of GCM calls between each PEM call |
---|
21 | nPCM=2 |
---|
22 | |
---|
23 | # List of NetCDF files to concatenate |
---|
24 | directory="diags" |
---|
25 | files=($directory/diagpem*.nc) |
---|
26 | output_file=($directory/$output_file) |
---|
27 | ################################################################ |
---|
28 | |
---|
29 | |
---|
30 | # Checking if everything is ok |
---|
31 | if [ ! -d "$directory" ]; then |
---|
32 | echo "Error: directory \"$diags\" not found!" |
---|
33 | exit 1 |
---|
34 | fi |
---|
35 | if [ ${#files[@]} -lt 2 ]; then |
---|
36 | echo "Error: there are not enough files for concatenation!" |
---|
37 | exit 1 |
---|
38 | fi |
---|
39 | if [ -f "$output_file" ]; then |
---|
40 | rm $output_file |
---|
41 | fi |
---|
42 | |
---|
43 | # Loop to concatenate the NetCDF files |
---|
44 | newTime=$((nPCM_ini + 1)) |
---|
45 | for file in "${files[@]}"; do |
---|
46 | # Extract the 'Time' variable into a temporary file |
---|
47 | ncks -v Time "$file" > tmp_Time.txt |
---|
48 | |
---|
49 | # Extract the 'data' block containing the Time values |
---|
50 | data_block=$(awk '/data:/ {flag=1} flag; /\}/ {flag=0}' tmp_Time.txt | sed '1d;$d') |
---|
51 | data_block=$(echo "$data_block" | sed -e 's/^[ \t]*//') |
---|
52 | data_block=$(echo "$data_block" | sed 's/Time =//') |
---|
53 | data_block=$(echo "$data_block" | sed 's/;$//') |
---|
54 | IFS=', ' read -ra Time_values <<< "$data_block" |
---|
55 | |
---|
56 | # Remove the temporary file |
---|
57 | rm tmp_Time.txt |
---|
58 | |
---|
59 | # Updating the variable 'Time' with the number of the year |
---|
60 | for ((i=0; i < ${#Time_values[@]}; i++)); do |
---|
61 | ncap2 -O -s "Time($i)=$newTime" $file $file |
---|
62 | newTime=$((newTime + ecritpem)) |
---|
63 | done |
---|
64 | |
---|
65 | # Concatenate the NetCDF file to the output |
---|
66 | if [ ! -f "$output_file" ]; then |
---|
67 | cp $file $output_file |
---|
68 | else |
---|
69 | ncrcat -O $output_file $file -o $output_file |
---|
70 | fi |
---|
71 | newTime=$((newTime + nPCM)) |
---|
72 | done |
---|
73 | |
---|
74 | echo "Concatenation of \"$directory/diagpem*.nc\" files into \"$output_file\" is complete!" |
---|