source: TOOLS/WATER_BUDGET/WaterUtils.py @ 6508

Last change on this file since 6508 was 6508, checked in by omamce, 13 months ago

WaterUtils?.pyVersion qui marche pour :
Water Budget
O.M.

Version qui marche pour :

  • Grille LMDZ et routage SECHIBA
  • Grille ICO avec sorties natives et routage SIMPLE : routage pas très précis.

Ne marche pas pour :

  • Grille LMDZ et routage SIMPLE : pb sur runoff
  • Grille ICO avec sorties interpolées :

# Erreurs relatives

### VALID-CM622-LR.01 - LMDZ : OK ? Sauf LIC

  • LIC : 1.e-2
  • SRF : 4.e-6
  • SRF/ATM : 2e-10
  • RUNOFF : 1.5e-4

### VALID-CM622-SIMPLE-ROUTING - LMDZ : Erreur RUNOFF, LIC

  • LIC : 1.6
  • SRF : 1e-6
  • SRF/ATM : 1.e-10
  • RUNOFF : -7

### TEST-CM72-SIMPLE-ROUTING.13 - ICO : Erreur SRF, RUNOFF, LIC

  • LIC : 150
  • SRF : 0.5
  • SRF/ATM : 4e-2
  • RUNOFF : 700

### CM71v420-LR-pd-02.ini - ICO interpolé : Erreur SRF, RUNOFF, LIC

  • LIC : 15
  • SRF : 0.7
  • SRF/ATM : -5.e-2
  • ROUTING : 3e3

### CM71v420-LR-pd-02.ini - ICO natif : Erreurs faibles RUNOFF, LIC

  • LIC : 0.3
  • SRF : 7.e-6
  • SRF/ATM : -6.e-9
  • ROUTING : 5.e-2
File size: 3.6 KB
Line 
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: omamce $
18#  $Date: 2022-12-08 10:24:05 +0100 (Thu, 08 Dec 2022) $
19#  $Revision: 6277 $
20#  $Id: ATM_waterbudget.py 6277 2022-12-08 09:24:05Z omamce $
21#  $HeadURL: svn+ssh://omamce@forge.ipsl.jussieu.fr/ipsl/forge/projets/igcmg/svn/TOOLS/WATER_BUDGET/ATM_waterbudget.py $
22
23## Import system modules
24#import sys, os, shutil, subprocess, platform
25import numpy as np
26import configparser, re
27
28VarInt = 10
29Ra     = None
30
31def ftest () :
32    print ( toto )
33
34def 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
42def 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
56def 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
64def 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
71def 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
87def 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
94def 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
102Psum = KSsum
103
104
105
106
Note: See TracBrowser for help on using the repository browser.