#!/usr/bin/env python3 ### ### Script to check water conservation in the IPSL coupled model ### ## Here, some common utilities to all scripts ## ## Warning, to install, configure, run, use any of included software or ## to read the associated documentation you'll need at least one (1) ## brain in a reasonably working order. Lack of this implement will ## void any warranties (either express or implied). Authors assumes ## no responsability for errors, omissions, data loss, or any other ## consequences caused directly or indirectly by the usage of his ## software by incorrectly or partially configured personal ## ## ## SVN information # $Author: omamce $ # $Date: 2022-12-08 10:24:05 +0100 (Thu, 08 Dec 2022) $ # $Revision: 6277 $ # $Id: ATM_waterbudget.py 6277 2022-12-08 09:24:05Z omamce $ # $HeadURL: svn+ssh://omamce@forge.ipsl.jussieu.fr/ipsl/forge/projets/igcmg/svn/TOOLS/WATER_BUDGET/ATM_waterbudget.py $ ## Import system modules #import sys, os, shutil, subprocess, platform import numpy as np import configparser, re VarInt = 10 Ra = None def ftest () : print ( toto ) def setBool (chars) : '''Convert specific char string in boolean if possible''' setBool = chars if type (chars) == str : for key in configparser.ConfigParser.BOOLEAN_STATES.keys () : if chars.lower() == key : setBool = configparser.ConfigParser.BOOLEAN_STATES[key] return setBool def setNum (chars) : '''Convert specific char string in integer or real if possible''' if type (chars) == str : realnum = re.compile ("^[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?$") isNumber = realnum.match(chars.strip()) != None isInt = chars.strip().isdigit() #print (chars , isNumber , isInt) if isNumber : if isInt : setNum = int (chars) else : setNum = float (chars) else : setNum = chars else : setNum = chars return setNum def setNone (chars) : '''Convert specific char string to None if possible''' if type (chars) == str : if chars in ['None', 'NONE', 'none'] : setNone = None else : setNone = chars else : setNone = chars return setNone def unDefined (char) : if char in globals () : if globals()[char] == None : unDefined = True else : unDefined = False else : unDefined = True return unDefined def Ksum (tab) : ''' Kahan summation algorithm, also known as compensated summation https://en.wikipedia.org/wiki/Kahan_summation_algorithm ''' Ksum = 0.0 # Prepare the accumulator. comp = 0.0 # A running compensation for lost low-order bits. for xx in np.where ( np.isnan(tab), 0., tab ) : yy = xx - comp # comp is zero the first time around. tt = Ksum + yy # Alas, sum is big, y small, so low-order digits of y are lost. comp = (tt - Ksum) - yy # (tt - Ksum) cancels the high-order part of y; subtracting y recovers negative (low part of yy) Ksum = tt # Algebraically, comp should always be zero. Beware overly-aggressive optimizing compilers ! # Next time around, the lost low part will be added to y in a fresh attempt. return Ksum def Ssum (tab) : ''' Precision summation by sorting by absolute value ''' Ssum = np.sum ( tab[np.argsort(np.abs(tab))] ) return Ssum def KSsum (tab) : ''' Precision summation by sorting by absolute value, and applying Kahan summation ''' KSsum = Ksum ( tab[np.argsort(np.abs(tab))] ) return KSsum # Choosing algorithm for precise summation Psum = KSsum