source: trunk/LMDZ.COMMON/libf/evolution/deftank/concat_diagpem.sh @ 3603

Last change on this file since 3603 was 3579, checked in by jbclement, 3 weeks ago

PEM:
Improvement of the Bash script tools in the deftank with an automatic error detection which ends the script with a message.
JBC

  • Property svn:executable set to *
File size: 2.3 KB
Line 
1#!/bin/bash
2################################################################
3### Script to concatenate "diagpem.nc" outputs into one file ###
4################################################################
5set -e
6trap '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
12output_file="diagpem_concat.nc"
13
14# Frequency of PEM outputs
15ecritpem=1
16
17# Number of initial PCM calls
18nPCM_ini=4
19
20# Number of GCM calls between each PEM call
21nPCM=2
22
23# List of NetCDF files to concatenate
24directory="diags"
25files=($directory/diagpem*.nc)
26output_file=($directory/$output_file)
27################################################################
28
29
30# Checking if everything is ok
31if [ ! -d "$directory" ]; then
32    echo "Error: directory \"$diags\" not found!"
33    exit 1
34fi
35if [ ${#files[@]} -lt 2 ]; then
36    echo "Error: there are not enough files for concatenation!"
37    exit 1
38fi
39if [ -f "$output_file" ]; then
40    rm $output_file
41fi
42
43# Loop to concatenate the NetCDF files
44newTime=$((nPCM_ini + 1))
45for 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))
72done
73
74echo "Concatenation of \"$directory/diagpem*.nc\" files into \"$output_file\" is complete!"
Note: See TracBrowser for help on using the repository browser.