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