1 | #!/usr/bin/env python3 |
---|
2 | ### |
---|
3 | ### Script to check water conservation in the IPSL coupled model |
---|
4 | ### |
---|
5 | ## Here, some common utilities to all scripts |
---|
6 | ## |
---|
7 | ## Warning, to install, configure, run, use any of included software or |
---|
8 | ## to read the associated documentation you'll need at least one (1) |
---|
9 | ## brain in a reasonably working order. Lack of this implement will |
---|
10 | ## void any warranties (either express or implied). Authors assumes |
---|
11 | ## no responsability for errors, omissions, data loss, or any other |
---|
12 | ## consequences caused directly or indirectly by the usage of his |
---|
13 | ## software by incorrectly or partially configured personal |
---|
14 | ## |
---|
15 | ## |
---|
16 | ## SVN information |
---|
17 | # $Author$ |
---|
18 | # $Date$ |
---|
19 | # $Revision$ |
---|
20 | # $Id: ATM_waterbudget.py 6277 2022-12-08 09:24:05Z omamce $ |
---|
21 | # $HeadURL$ |
---|
22 | |
---|
23 | ## Import system modules |
---|
24 | #import sys, os, shutil, subprocess, platform |
---|
25 | import numpy as np |
---|
26 | import configparser, re |
---|
27 | |
---|
28 | VarInt = 10 |
---|
29 | Ra = None |
---|
30 | |
---|
31 | def ftest () : |
---|
32 | print ( toto ) |
---|
33 | |
---|
34 | def setBool (chars) : |
---|
35 | '''Convert specific char string in boolean if possible''' |
---|
36 | setBool = chars |
---|
37 | if type (chars) == str : |
---|
38 | for key in configparser.ConfigParser.BOOLEAN_STATES.keys () : |
---|
39 | if chars.lower() == key : setBool = configparser.ConfigParser.BOOLEAN_STATES[key] |
---|
40 | return setBool |
---|
41 | |
---|
42 | def setNum (chars) : |
---|
43 | '''Convert specific char string in integer or real if possible''' |
---|
44 | if type (chars) == str : |
---|
45 | realnum = re.compile ("^[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?$") |
---|
46 | isNumber = realnum.match(chars.strip()) != None |
---|
47 | isInt = chars.strip().isdigit() |
---|
48 | #print (chars , isNumber , isInt) |
---|
49 | if isNumber : |
---|
50 | if isInt : setNum = int (chars) |
---|
51 | else : setNum = float (chars) |
---|
52 | else : setNum = chars |
---|
53 | else : setNum = chars |
---|
54 | return setNum |
---|
55 | |
---|
56 | def setNone (chars) : |
---|
57 | '''Convert specific char string to None if possible''' |
---|
58 | if type (chars) == str : |
---|
59 | if chars in ['None', 'NONE', 'none'] : setNone = None |
---|
60 | else : setNone = chars |
---|
61 | else : setNone = chars |
---|
62 | return setNone |
---|
63 | |
---|
64 | def unDefined (char) : |
---|
65 | if char in globals () : |
---|
66 | if globals()[char] == None : unDefined = True |
---|
67 | else : unDefined = False |
---|
68 | else : unDefined = True |
---|
69 | return unDefined |
---|
70 | |
---|
71 | def Ksum (tab) : |
---|
72 | ''' |
---|
73 | Kahan summation algorithm, also known as compensated summation |
---|
74 | https://en.wikipedia.org/wiki/Kahan_summation_algorithm |
---|
75 | ''' |
---|
76 | Ksum = 0.0 # Prepare the accumulator. |
---|
77 | comp = 0.0 # A running compensation for lost low-order bits. |
---|
78 | |
---|
79 | for xx in np.where ( np.isnan(tab), 0., tab ) : |
---|
80 | yy = xx - comp # comp is zero the first time around. |
---|
81 | tt = Ksum + yy # Alas, sum is big, y small, so low-order digits of y are lost. |
---|
82 | comp = (tt - Ksum) - yy # (tt - Ksum) cancels the high-order part of y; subtracting y recovers negative (low part of yy) |
---|
83 | Ksum = tt # Algebraically, comp should always be zero. Beware overly-aggressive optimizing compilers ! |
---|
84 | # Next time around, the lost low part will be added to y in a fresh attempt. |
---|
85 | return Ksum |
---|
86 | |
---|
87 | def Ssum (tab) : |
---|
88 | ''' |
---|
89 | Precision summation by sorting by absolute value |
---|
90 | ''' |
---|
91 | Ssum = np.sum ( tab[np.argsort(np.abs(tab))] ) |
---|
92 | return Ssum |
---|
93 | |
---|
94 | def KSsum (tab) : |
---|
95 | ''' |
---|
96 | Precision summation by sorting by absolute value, and applying Kahan summation |
---|
97 | ''' |
---|
98 | KSsum = Ksum ( tab[np.argsort(np.abs(tab))] ) |
---|
99 | return KSsum |
---|
100 | |
---|
101 | # Choosing algorithm for precise summation |
---|
102 | Psum = KSsum |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|