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

Last change on this file since 3594 was 3579, checked in by jbclement, 4 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
RevLine 
[3088]1#!/bin/bash
2################################################################
3### Script to concatenate "diagpem.nc" outputs into one file ###
4################################################################
[3579]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
[3088]7
[3579]8################################################################
9# Modify here the parameters for the script
10###########################################
[3088]11# Output file name
12output_file="diagpem_concat.nc"
13
14# Frequency of PEM outputs
15ecritpem=1
16
[3214]17# Number of initial PCM calls
18nPCM_ini=4
19
[3088]20# Number of GCM calls between each PEM call
[3214]21nPCM=2
[3088]22
23# List of NetCDF files to concatenate
24directory="diags"
25files=($directory/diagpem*.nc)
26output_file=($directory/$output_file)
[3579]27################################################################
[3088]28
[3210]29
[3088]30# Checking if everything is ok
[3145]31if [ ! -d "$directory" ]; then
32    echo "Error: directory \"$diags\" not found!"
33    exit 1
34fi
[3088]35if [ ${#files[@]} -lt 2 ]; then
[3145]36    echo "Error: there are not enough files for concatenation!"
37    exit 1
[3088]38fi
39if [ -f "$output_file" ]; then
40    rm $output_file
41fi
42
43# Loop to concatenate the NetCDF files
[3214]44newTime=$((nPCM_ini + 1))
[3088]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
[3214]71    newTime=$((newTime + nPCM))
[3088]72done
73
[3143]74echo "Concatenation of \"$directory/diagpem*.nc\" files into \"$output_file\" is complete!"
Note: See TracBrowser for help on using the repository browser.