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

Last change on this file since 3594 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: 4.9 KB
Line 
1#!/bin/bash
2######################################################################
3### Script to modify the orbital parameters of a file "startfi.nc" ###
4### according to the date set in the file "run_PEM.def"            ###
5######################################################################
6set -e
7trap 'echo -e "\033[31mError: an issue occurred in the script on line $LINENO! Please review the command and try again.\033[0m"' ERR
8
9######################################################################
10# Modify here the parameters for the script
11###########################################
12# Name of the file to be modified
13name_file="startfi.nc"
14
15# Name of the file containing the orbital data
16orb_data="obl_ecc_lsp.asc"
17######################################################################
18
19
20# Check if files necessary for the script exist
21if [ ! -f "$orb_data" ]; then
22    echo "Error: file \"$orb_data\" not found!"
23    exit 1
24fi
25if [ ! -f "startfi.nc" ]; then
26    echo "Error: file \"$name_file\" not found!"
27    exit 1
28fi
29if [ ! -f "run_PEM.def" ]; then
30    echo "Error: file \"run_PEM.def\" not found!"
31    exit 1
32fi
33
34# Get the number of Earth years before present to start the PEM run from "run_PEM.def"
35eyears_bp_ini=$(grep 'year_earth_bp_ini=' run_PEM.def | cut -d '=' -f 2 | tr -d '[:space:]')
36echo "The starting date is $eyears_bp_ini Earth years according to \"run_PEM.def\"."
37
38# Get the new values for the orbital parameters
39yearlask=$(echo "$eyears_bp_ini/1000." | bc -l)
40found=false
41read -r y1 obl1 ecc1 lsp1 < "$orb_data"
42while read -r y2 obl2 ecc2 lsp2; do
43    if [ "$first_line" = true ]; then
44        first_line=false
45        continue # Skip the first line as it's already read outside the loop
46    fi
47    if [ "$(echo "$y1 >= $yearlask && $yearlask > $y2" | bc)" -eq 1 ]; then
48        found=true
49        # Calculate linear interpolations for each orbital parameter
50        new_obl=$(echo "($obl2 - $obl1)/($y2 - $y1)*($yearlask - $y1) + $obl1" | bc -l)
51        new_ecc=$(echo "($ecc2 - $ecc1)/($y2 - $y1)*($yearlask - $y1) + $ecc1" | bc -l)
52        if [ "$(echo "$lsp2 - $lsp1 > 300. || $lsp2 - $lsp1 < -300." | bc)" -eq 1 ]; then # If modulo is "crossed" through the interval
53            if [ "$(echo "$lsp1 < $lsp2" | bc -l)" -eq 1 ]; then # Lsp should be decreasing
54                lsp1=$(echo "$lsp1 + 360" | bc -l)
55            else # Lsp should be increasing
56                lsp2=$(echo "$lsp2 + 360" | bc -l)
57            fi
58        fi
59        new_Lsp=$(echo "($lsp2 - $lsp1)/($y2 - $y1)*($yearlask - $y1) + $lsp1" | bc -l)
60        echo "New obliquity    = $new_obl"
61        echo "New eccentricity = $new_ecc"
62        echo "New Lsp          = $new_Lsp"
63        echo "Done!"
64        break
65    fi
66    y1=$y2
67    obl1=$obl2
68    ecc1=$ecc2
69    lsp1=$lsp2
70done < "$orb_data"
71if [ "$found" = false ]; then
72    echo "Error: the current year could not be found in the file \"$orb_data\"!"
73    exit 1
74fi
75
76
77# Compute the modified values in the file
78if [ ! -f "$name_file" ]; then
79    echo "Error: file \"$name_file\" not found!"
80    exit 1
81fi
82year_day=669.
83halfaxe=227.94
84pi=$(echo "4.*a(1.)" | bc -l)
85degrad=$(echo "180./$pi" | bc -l)
86
87periheli=$(echo "$halfaxe*(1. - $new_ecc)" | bc -l)
88aphelie=$(echo "$halfaxe*(1. + $new_ecc)" | bc -l)
89
90tan=$(echo "s(0.5*$new_Lsp/$degrad)/c(0.5*$new_Lsp/$degrad)" | bc -l)
91zx0=$(echo "-2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l)
92if [ $(echo "$zx0 <= 0." | bc -l) -eq 1 ]; then
93    zx0=$(echo "$zx0 + 2.*$pi" | bc -l)
94fi
95peri_day=$(echo "$year_day*(1. - ($zx0 - $new_ecc*s($zx0))/(2.*$pi))" | bc -l)
96
97ztheta=$(echo "($new_iniLs - $new_Lsp)/$degrad" | bc -l)
98tan=$(echo "s(0.5*$ztheta)/c(0.5*$ztheta)" | bc -l)
99zx0=$(echo "2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l)
100xref=$(echo "$zx0 - $new_ecc*s($zx0)" | bc -l)
101new_inisol=$(echo "$peri_day + $xref*$year_day/(2.*$pi)" | bc -l)
102if [ $(echo "$new_inisol < 0." | bc -l) -eq 1 ]; then
103    new_inisol=$(echo "$new_inisol + $year_day" | bc -l)
104fi
105if [ $(echo "$new_inisol >= $year_day" | bc -l) -eq 1 ]; then
106    new_inisol=$(echo "$new_inisol - $year_day" | bc -l)
107fi
108
109# Update the netCDF file
110# controle(15) = periheli ! min. Sun-Mars distance (Mkm)  ̃206.66
111# controle(16) = aphelie  ! max. SUn-Mars distance (Mkm)  ̃249.22
112# controle(17) = peri_day ! date of perihelion (sols since N. spring)
113# controle(18) = obliquit ! Obliquity of the planet (deg)  ̃23.98
114# controle(3)  = day_ini + int(time) ! initial day
115ncap2 -O -s "controle(17)=$new_obl" \
116         -s "controle(14)=$periheli" \
117         -s "controle(15)=$aphelie" \
118         -s "controle(16)=$peri_day" \
119         -s "controle(2)=$new_inisol" \
120         $name_file $name_file
121
122echo "In \"$name_file\":"
123echo "New obliquit     = $new_obl"
124echo "New eccentricity = $new_ecc -> new periheli = $periheli"
125echo "                              -> new aphelie  = $aphelie"
126echo "New Lsp          = $new_Lsp -> new peri_day = $peri_day"
127echo "New initial Ls   = $new_iniLs -> New initial sol = $new_inisol"
128echo "Done!"
Note: See TracBrowser for help on using the repository browser.