Index: TOOLS/WATER_BUDGET/ATM_waterbudget.py
===================================================================
--- TOOLS/WATER_BUDGET/ATM_waterbudget.py	(revision 6265)
+++ TOOLS/WATER_BUDGET/ATM_waterbudget.py	(revision 6271)
@@ -19,56 +19,87 @@
 #  $HeadURL$
 
-
-## Define Experiment
-if False : 
-    JobName="TEST-CM72-SIMPLE-ROUTING.12" ; TagName="IPSLCM7" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1970 ; YearEnd = 1979 ; PackFrequency = 10
-    ATM = 'ICO40' ; Routing = 'SIMPLE'
-
-if False :
-    JobName="TEST-CM72-SIMPLE-ROUTING.10" ; TagName="IPSLCM7" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1860 ; YearEnd = 1869 ; PackFrequency = 10
-    ATM = 'ICO40' ; Routing = 'SIMPLE'
-
-if False : 
-    JobName="VALID-CM622-LR.01" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1890 ; YearEnd = 1899 ; PackFrequency = 10
-    ATM = 'LMD144142' ; Routing = 'ORCHIDEE'
-    
-if True :
-    JobName="CM65v420-LR-SKL-pi-05" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p48ethe" ; Project="gencmip6" 
-    Freq = 'MO' ;  YearBegin = 1890 ; YearEnd = 1899 ; PackFrequency = 10
-    ORCA = 'eORCA1.2'  ; ATM = 'LMD144142' ; Routing = 'ORCHIDEE' ; NEMO=4.2 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-    
-Coupled = True
-
-import numpy as np
-##-- Some physical constants
-#-- Earth Radius
-Ra = 40.e6 / (2. * np.pi)
-#-- Gravity
-g = 9.81
-#-- Ice density (kg/m3) in LIM3
-ICE_rho_ice = 917.0
-#-- Snow density (kg/m3) in LIM3
-ICE_rho_sno = 330.0
-#-- Ocean water density (kg/m3) in NEMO
-OCE_rho_liq = 1026.
-
-###
-ICO  = False
-if 'ICO' in ATM : ICO  = True
-LMDZ = False
-if 'LMD' in ATM : LMDZ = True
-
 ###
 ## Import system modules
 import sys, os, shutil, subprocess
+import numpy as np
+import configparser, re
+
+## Creates parser
+config = configparser.ConfigParser()
+config.optionxform = str # To keep capitals
+
+config['Files']  = {}
+config['System'] = {}
+
+##-- Some physical constants
+#-- Earth Radius
+Ra = 6366197.7236758135
+#-- Gravity
+Grav = 9.81
+#-- Ice volumic mass (kg/m3) in LIM3
+ICE_rho_ice = 917.0
+#-- Snow volumic mass (kg/m3) in LIM3
+ICE_rho_sno = 330.0
+#-- Ocean water volumic mass (kg/m3) in NEMO
+OCE_rho_liq = 1026.
+#-- Water volumic mass in atmosphere
+ATM_rho = 1.0e3
+#-- Water volumic mass in surface reservoirs
+SRF_rho = 1.0e3
+#-- Water volumic mass of rivers
+RUN_rho = 1.0e3
+
+## Read experiment parameters
+ATM = None ; ORCA = None ; NEMO = None ; OCE_relax = False ; OCE_icb = False ; Coupled = False ; Routing = None
+
+# Arguments passed
+print ( "Name of Python script:", sys.argv[0] )
+IniFile =  sys.argv[1]
+print ("Input file : ", IniFile )
+config.read (IniFile)
+
+def setBool (chars) :
+    '''Convert specific char string in boolean if possible'''
+    setBool = chars
+    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]+)?$")
+        isReal = realnum.match(chars.strip()) != None
+        isInt  = chars.strip().isdigit()
+        if isReal :
+            if isInt : setNum = int   (chars)
+            else     : setNum = float (chars)
+        else : setNum = chars
+    else : setNum = chars
+    return setNum
+
+print ('[Experiment]')
+for VarName in config['Experiment'].keys() :
+    locals()[VarName] = config['Experiment'][VarName]
+    locals()[VarName] = setBool (locals()[VarName])
+    locals()[VarName] = setNum  (locals()[VarName])
+    print ( '{:25} set to : {:}'.format (VarName, locals()[VarName]) )
+
+# ###
+ICO  = ( 'ICO' in ATM )
+LMDZ = ( 'LMD' in ATM )
+    
+###
+## Import system modules
+import sys, os, shutil, subprocess
+import configparser, re
+
+config = configparser.ConfigParser()
+config['Files'] = {}
 
 # Where do we run ?
-TGCC = False ; IDRIS = False
 SysName, NodeName, Release, Version, Machine = os.uname()
-if 'irene'   in NodeName : TGCC  = True
-if 'jeanzay' in NodeName : IDRIS = True
+TGCC  = ( 'irene'   in NodeName )
+IDRIS = ( 'jeanzay' in NodeName )
 
 ## Set site specific libIGCM directories, and other specific stuff
@@ -90,8 +121,9 @@
         
     ## Creates output directory
-    TmpDir = os.path.join ( subprocess.getoutput ( 'ccc_home --cccscratch' ), f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
+    #TmpDir = os.path.join ( subprocess.getoutput ( 'ccc_home --cccscratch' ), f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
+    TmpDir = os.path.join ( '/ccc/scratch/cont003/drf/p86mart', f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
 
 if IDRIS :
-    raise Exception("Pour IDRIS : repertoires et chemins a definir") 
+    raise Exception ("Pour IDRIS : repertoires et chemins a definir") 
 
 ## Import specific module
@@ -99,5 +131,7 @@
 ## Now import needed scientific modules
 import xarray as xr
-    
+
+config['Files'][TmpDir] = TmpDir
+
 # Output file
 FileOut = f'ATM_waterbudget_{JobName}_{YearBegin}_{YearEnd}.out'
@@ -105,7 +139,8 @@
 
 # Function to print to stdout *and* output file
-def echo (string) :
-    print ( string  )
-    f_out.write ( string + '\n' )
+def echo (string, end='\n') :
+    print ( string, end=end  )
+    sys.stdout.flush ()
+    f_out.write ( string + end )
     f_out.flush ()
     return None
@@ -135,6 +170,6 @@
 
 #-- Model output directories
-if Freq == "MO" : FreqDir =  os.path.join ('Output' , 'MO' )
-if Freq == "SE" : FreqDir =  os.path.join ('Analyse', 'SE' )
+if Freq == "MO" : FreqDir = os.path.join ('Output' , 'MO' )
+if Freq == "SE" : FreqDir = os.path.join ('Analyse', 'SE' )
 dir_ATM_his = os.path.join ( R_SAVE, "ATM", FreqDir )
 dir_SRF_his = os.path.join ( R_SAVE, "SRF", FreqDir )
@@ -145,16 +180,30 @@
 
 #-- Files Names
-if Freq == 'MO' : File = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
-if Freq == 'SE' : File = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
+if Freq == 'MO' : FileCommon = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
+if Freq == 'SE' : FileCommon = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
 
 echo ('\nOpen history files' )
-file_ATM_his = os.path.join (  dir_ATM_his, f'{File}_histmth.nc' )
-file_SRF_his = os.path.join (  dir_SRF_his, f'{File}_sechiba_history.nc' )
+file_ATM_his = os.path.join (  dir_ATM_his, f'{FileCommon}_histmth.nc' )
+file_SRF_his = os.path.join (  dir_SRF_his, f'{FileCommon}_sechiba_history.nc' )
+if Routing == 'ORCHIDEE'  :
+    file_RUN_his = os.path.join (  dir_SRF_his, f'{FileCommon}_sechiba_history.nc' )
+if Routing == 'SIMPLE' :
+    file_RUN_his = os.path.join (  dir_SRF_his, f'{FileCommon}_sechiba_history.nc' )
 
 d_ATM_his = xr.open_dataset ( file_ATM_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
 d_SRF_his = xr.open_dataset ( file_SRF_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+if Routing == 'ORCHIDEE' :
+    d_RUN_his = d_SRF_his
+if Routing == 'SIMPLE' : 
+    d_RUN_his = xr.open_dataset ( file_RUN_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
 
 echo ( file_ATM_his )
 echo ( file_SRF_his )
+echo ( file_RUN_his )
+
+
+config['Files']['file_ATM_his'] = file_ATM_his
+config['Files']['file_SRF_his'] = file_SRF_his
+config['Files']['file_RUN_his'] = file_SRF_his
 
 ## Compute run length
@@ -170,5 +219,5 @@
 
 #-- Open restart files
-YearRes = YearBegin - 1        # Year of the restart of beginning of simulation
+YearRes = YearBegin - 1              # Year of the restart of beginning of simulation
 YearPre = YearBegin - PackFrequency  # Year to find the tarfile of the restart of beginning of simulation
 
@@ -219,4 +268,14 @@
         os.system ( command )
 
+config['Files']['file_ATM_beg'] = file_ATM_beg
+config['Files']['file_ATM_end'] = file_ATM_end
+config['Files']['file_SRF_beg'] = file_SRF_beg
+config['Files']['file_SRF_end'] = file_SRF_end
+if Routing == 'SIMPLE' : 
+    config['Files']['file_RUN_beg'] = file_RUN_beg
+    config['Files']['file_RUN_end'] = file_RUN_end
+config['Files']['file_DYN_beg'] = file_DYN_beg
+config['Files']['file_DYN_end'] = file_DYN_end
+        
 echo ('\nOpening ATM SRF and ICO restart files')
 d_ATM_beg = xr.open_dataset ( os.path.join (TmpDir, file_ATM_beg), decode_times=False, decode_cf=True).squeeze()
@@ -228,6 +287,4 @@
 
 for var in d_SRF_beg.variables :
-    #d_SRF_beg[var].attrs['_FillValue'] = 1.e20
-    #d_SRF_end[var].attrs['_FillValue'] = 1.e20
     d_SRF_beg[var] = d_SRF_beg[var].where (  d_SRF_beg[var]<1.e20, 0.)
     d_SRF_end[var] = d_SRF_end[var].where (  d_SRF_end[var]<1.e20, 0.)
@@ -247,30 +304,76 @@
     echo ( file_RUN_end )
 
+def ATM_stock_int (stock) :
+    '''Integrate stock on atmosphere grid'''
+    ATM_stock_int  = np.sum (  np.sort ( (stock * DYN_aire).to_masked_array().ravel()) )
+    return ATM_stock_int
+
+def ATM_flux_int (flux) :
+    '''Integrate flux on atmosphere grid'''
+    ATM_stock_int  = np.sum (  np.sort ( (flux * dtime_per_sec * ATM_aire).to_masked_array().ravel()) )
+    return ATM_stock_int
+
+def ONE_stock_int (stock) :
+    '''Sum stock '''
+    ONE_stock_int  = np.sum (  np.sort ( (stock).to_masked_array().ravel()) )
+    return ONE_stock_int
+
+def ONE_flux_int (flux) :
+    '''Sum flux '''
+    ONE_flux_int  = np.sum (  np.sort ( (flux * dtime_per_sec ).to_masked_array().ravel()) )
+    return ONE_flux_int
+    
 # ATM grid with cell surfaces
-if ICO : 
-    ATM_aire = d_ATM_his ['aire'][0]
-    ATM_fsea = d_ATM_his ['fract_ter'][Ã] + d_ATM_his ['fract_sic'][Ã]
+if ICO :
+    jpja, jpia = d_ATM_his['aire'][0].shape   
+    file_ATM_aire = os.path.join ( R_IN, 'ATM', 'GRID', f'aire_{ATM}_to_{jpia}x{jpja}.nc' )
+    config['Files']['file_ATM_aire'] = file_ATM_aire
+    echo ('Aire sur grille reguliere :', file_ATM_aire)
+    d_ATM_aire = xr.open_dataset ( file_ATM_aire, decode_times=False ).squeeze()
+    ATM_aire = lmdz.geo2point ( d_ATM_aire ['aire'].squeeze(), cumulPoles=True )
+    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
+    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0] + d_ATM_his ['fract_lic'][0] )
+    SRF_aire = lmdz.geo2point ( d_SRF_his ['Areas'] * d_SRF_his ['Contfrac'] )
+    
 if LMDZ :
-    ATM_aire = lmdz.geo2point ( d_ATM_his ['aire'][0] )
-    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_ter'][0] + d_ATM_his ['fract_sic'][0] )
-    ATM_aire[0]  = np.sum ( d_ATM_his ['aire'][0, 0] )
-    ATM_aire[-1] = np.sum ( d_ATM_his ['aire'][0,-1] )
+    ATM_aire = lmdz.geo2point ( d_ATM_his ['aire'][0], cumulPoles=True )
+    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
+    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0] )
+    SRF_aire = lmdz.geo2point ( d_SRF_his['Areas'] * d_SRF_his['Contfrac'] )
+
+SRF_aire = SRF_aire.where ( SRF_aire < 1E15, 0.)
 
 if ICO :
+    # Area on icosahedron grid
     file_DYN_aire = os.path.join ( R_IN, 'ATM', 'GRID', ATM+'_grid.nc' )
     d_DYN_aire = xr.open_dataset ( file_DYN_aire, decode_times=False).squeeze()
-    d_DYN_aire = d_DYN_aire.rename( {'cell':'cell_mesh'})
+    d_DYN_aire = d_DYN_aire.rename ( {'cell':'cell_mesh'} )
     DYN_aire   = d_DYN_aire['aire']
+
+    DYN_fsea = d_DYN_aire ['fract_oce'] + d_DYN_aire ['fract_sic']
+    DYN_flnd = 1.0 - DYN_fsea
+    
 if LMDZ :
     DYN_aire = ATM_aire
+    DYN_fsea = ATM_fsea
+    DYN_flnd = ATM_flnd
 
 #if LMDZ :
 #    d_ATM_beg = d_ATM_beg.assign ( coords={'lon':d_ATM_beg.lon*180./np.pi} )
 
-ATM_aire_tot = np.sum (ATM_aire).values.item()
-ATM_aire_sea_tot = np.sum (ATM_aire*ATM_fsea).values.item()
+ATM_aire_sea     = ATM_aire * ATM_fsea
+ATM_aire_sea_tot = ONE_stock_int ( ATM_aire_sea )
+
+ATM_aire_tot     = ONE_stock_int (ATM_aire)
+ATM_aire_sea_tot = ONE_stock_int (ATM_aire*ATM_fsea)
+
+
+echo ( 'Aire atmosphere/4pi R^2 : {:12.5f}'.format(ATM_aire_tot/(Ra*Ra*4*np.pi) ) )
+
+if (  np.abs (ATM_aire_tot/(Ra*Ra*4*np.pi) - 1.0) > 0.01 ) :
+    raise Exception ('Erreur surface interpolee sur grille reguliere')
 
 echo ( '\n------------------------------------------------------------------------------------' )
-echo ( '-- LMDZ changes in stores (for the records)' )
+echo ( '-- ATM changes in stores ' )
 
 #-- Change in precipitable water from the atmosphere daily and monthly files
@@ -280,136 +383,368 @@
 ATM_Ahyb = d_ATM_his['Ahyb'].squeeze()
 ATM_Bhyb = d_ATM_his['Bhyb'].squeeze()
-klevp1 = ATM_Ahyb.shape[0]
 
 # Surface pressure
 if ICO : 
-    ATM_ps_beg = d_DYN_beg['ps']
-    ATM_ps_end = d_DYN_end['ps']
+    DYN_ps_beg = d_DYN_beg['ps']
+    DYN_ps_end = d_DYN_end['ps']
     
 if LMDZ : 
-    ATM_ps_beg =  lmdz.geo2point ( d_DYN_beg['ps'].isel(rlonv=slice(0,-1)) )
-    ATM_ps_end =  lmdz.geo2point ( d_DYN_end['ps'].isel(rlonv=slice(0,-1)) )
+    DYN_ps_beg = lmdz.geo2point ( d_DYN_beg['ps'].isel(rlonv=slice(0,-1)) )
+    DYN_ps_end = lmdz.geo2point ( d_DYN_end['ps'].isel(rlonv=slice(0,-1)) )
     
 # 3D Pressure
-ATM_p_beg = ATM_Ahyb + ATM_Bhyb * ATM_ps_beg
-ATM_p_end = ATM_Ahyb + ATM_Bhyb * ATM_ps_end
+DYN_p_beg = ATM_Ahyb + ATM_Bhyb * DYN_ps_beg
+DYN_p_end = ATM_Ahyb + ATM_Bhyb * DYN_ps_end
+
+if ICO  : klevp1, cell_mesh        = DYN_p_beg.shape
+if LMDZ : klevp1, points_physiques = DYN_p_beg.shape
+klev = klevp1 - 1
 
 # Layer thickness
-ATM_sigma_beg = ATM_p_beg[0:-1]*0.
-ATM_sigma_end = ATM_p_end[0:-1]*0. 
+if ICO : 
+    DYN_dsigma_beg = xr.DataArray ( np.empty( (klev, cell_mesh       )), dims=('sigs', 'cell_mesh'       ), coords=(np.arange(klev), np.arange(cell_mesh) ) )
+    DYN_dsigma_end = xr.DataArray ( np.empty( (klev, cell_mesh       )), dims=('sigs', 'cell_mesh'       ), coords=(np.arange(klev), np.arange(cell_mesh) ) )
+if LMDZ : 
+    DYN_dsigma_beg = xr.DataArray ( np.empty( (klev, points_physiques)), dims=('sigs', 'points_physiques'), coords=(np.arange(klev), np.arange(points_physiques) ) )
+    DYN_dsigma_end = xr.DataArray ( np.empty( (klev, points_physiques)), dims=('sigs', 'points_physiques'), coords=(np.arange(klev), np.arange(points_physiques) ) )
 
 for k in np.arange (klevp1-1) :
-    ATM_sigma_beg[k,:] = (ATM_p_beg[k,:] - ATM_p_beg[k+1,:]) / g
-    ATM_sigma_end[k,:] = (ATM_p_end[k,:] - ATM_p_end[k+1,:]) / g
-
-ATM_sigma_beg = ATM_sigma_beg.rename ( {'klevp1':'sigs'} )
-ATM_sigma_end = ATM_sigma_end.rename ( {'klevp1':'sigs'} )
+    DYN_dsigma_beg[k,:] = DYN_p_beg[k,:] - DYN_p_beg[k+1,:]
+    DYN_dsigma_end[k,:] = DYN_p_end[k,:] - DYN_p_end[k+1,:]
 
 ##-- Vertical and horizontal integral, and sum of liquid, solid and vapor water phases
 if LMDZ :
     try : 
-        ATM_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2Ov'] + d_DYN_beg['H2Ol'] + d_DYN_beg['H2Oi']).isel(rlonv=slice(0,-1) ) )
-        ATM_wat_end = lmdz.geo3point ( (d_DYN_end['H2Ov'] + d_DYN_end['H2Ol'] + d_DYN_end['H2Oi']).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2Ov']  + d_DYN_beg['H2Ol']  + d_DYN_beg['H2Oi'] ).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_end = lmdz.geo3point ( (d_DYN_end['H2Ov']  + d_DYN_end['H2Ol']  + d_DYN_end['H2Oi'] ).isel(rlonv=slice(0,-1) ) )
     except :
-        ATM_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).isel(rlonv=slice(0,-1) ) )
-        ATM_wat_end = lmdz.geo3point ( (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_end = lmdz.geo3point ( (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).isel(rlonv=slice(0,-1) ) )
 if ICO :
-    ATM_wat_beg = ( d_DYN_beg['q'][0:4] ).sum (dim = ['nq',] ).rename ( {'lev':'sigs'} )
-    ATM_wat_end = ( d_DYN_end['q'][0:4] ).sum (dim = ['nq',] ).rename ( {'lev':'sigs'} )
-    
-ATM_mas_wat_beg = np.sum (ATM_sigma_beg * ATM_wat_beg * ATM_aire).values.item()
-ATM_mas_wat_end = np.sum (ATM_sigma_end * ATM_wat_end * ATM_aire).values.item()
-
-dATM_mas_wat = ATM_mas_wat_end - ATM_mas_wat_beg
-
-echo ( 'Variation du contenu en eau atmosphere ' )
-echo ( 'ATM_mass_beg = {:12.6e} kg - ATM_mass_end = {:12.6e} kg'.format (ATM_mas_wat_beg, ATM_mas_wat_end) )
-echo ( 'dMass(atm)   = {:12.3e} kg '.format (dATM_mas_wat) )
-echo ( 'dMass(atm)   = {:12.3e} Sv '.format (dATM_mas_wat/dtime_sec*1.E-9) )
-echo ( 'dMass(atm)   = {:12.3e}m   '.format (dATM_mas_wat/ATM_aire_sea_tot*1E-3) )
+    try :
+        DYN_wat_beg = (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).rename ( {'lev':'sigs'} )
+        DYN_wat_end = (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).rename ( {'lev':'sigs'} )
+    except : 
+        DYN_wat_beg = (d_DYN_beg['q'].isel(nq=0) + d_DYN_beg['q'].isel(nq=1) + d_DYN_beg['q'].isel(nq=2) ).rename ( {'lev':'sigs'} )
+        DYN_wat_end = (d_DYN_end['q'].isel(nq=0) + d_DYN_end['q'].isel(nq=1) + d_DYN_end['q'].isel(nq=2) ).rename ( {'lev':'sigs'} )
+    
+# Integral
+DYN_mas_wat_beg = ATM_stock_int (DYN_dsigma_beg * DYN_wat_beg) / Grav
+DYN_mas_wat_end = ATM_stock_int (DYN_dsigma_end * DYN_wat_end) / Grav
+
+dDYN_mas_wat = DYN_mas_wat_end - DYN_mas_wat_beg
+
+echo ( '\nVariation du contenu en eau atmosphere (dynamique) ' )
+echo ( 'DYN_mas_beg = {:12.6e} kg | DYN_mas_end = {:12.6e} kg'.format (DYN_mas_wat_beg, DYN_mas_wat_end) )
+echo ( 'dMass (atm)   = {:12.3e} kg '.format (dDYN_mas_wat) )
+echo ( 'dMass (atm)   = {:12.3e} Sv '.format (dDYN_mas_wat/dtime_sec*1.e-6/ATM_rho) )
+echo ( 'dMass (atm)   = {:12.3e} m  '.format (dDYN_mas_wat/ATM_aire_sea_tot/ATM_rho) )
 
 ATM_sno_beg = d_ATM_beg['SNOW01']*d_ATM_beg['FTER']+d_ATM_beg['SNOW02']*d_ATM_beg['FLIC']+d_ATM_beg['SNOW03']*d_ATM_beg['FOCE']+d_ATM_beg['SNOW04']*d_ATM_beg['FSIC']
 ATM_sno_end = d_ATM_end['SNOW01']*d_ATM_end['FTER']+d_ATM_end['SNOW02']*d_ATM_end['FLIC']+d_ATM_end['SNOW03']*d_ATM_end['FOCE']+d_ATM_end['SNOW04']*d_ATM_end['FSIC']
+
+ATM_qs_beg  = d_ATM_beg['QS01']*d_ATM_beg['FTER']+d_ATM_beg['QS02']*d_ATM_beg['FLIC']+d_ATM_beg['QS03']*d_ATM_beg['FOCE']+d_ATM_beg['QS04']*d_ATM_beg['FSIC']
+ATM_qs_end  = d_ATM_end['QS01']*d_ATM_end['FTER']+d_ATM_end['QS02']*d_ATM_end['FLIC']+d_ATM_end['QS03']*d_ATM_end['FOCE']+d_ATM_end['QS04']*d_ATM_end['FSIC']
+
+ATM_qsol_beg = d_ATM_beg['QSOL']
+ATM_qsol_end = d_ATM_end['QSOL']
+
+ATM_qs01_beg  = d_ATM_beg['QS01'] * d_ATM_beg['FTER']
+ATM_qs01_end  = d_ATM_end['QS01'] * d_ATM_end['FTER']
+ATM_qs02_beg  = d_ATM_beg['QS02'] * d_ATM_beg['FLIC']
+ATM_qs02_end  = d_ATM_end['QS02'] * d_ATM_end['FLIC']
+ATM_qs03_beg  = d_ATM_beg['QS03'] * d_ATM_beg['FOCE']
+ATM_qs03_end  = d_ATM_end['QS03'] * d_ATM_end['FOCE']
+ATM_qs04_beg  = d_ATM_beg['QS04'] * d_ATM_beg['FSIC']
+ATM_qs04_end  = d_ATM_end['QS04'] * d_ATM_end['FSIC']  
+
 if ICO :
-   ATM_sno_beg = ATM_sno_beg.rename ( {'points_physiques':'cell_mesh'} )
-   ATM_sno_end = ATM_sno_end.rename ( {'points_physiques':'cell_mesh'} )
-
-ATM_mas_sno_beg = np.sum ( ATM_sno_beg * DYN_aire ).values.item()
-ATM_mas_sno_end = np.sum ( ATM_sno_end * DYN_aire ).values.item()
-
-dATM_mas_sno = ATM_mas_sno_end - ATM_mas_sno_beg
-
-echo ( 'Variation du contenu en neige atmosphere ' )
-echo ( 'ATM_mas_sno_beg  = {:12.6e} kg  - ATM_mas_sno_end  = {:12.6e} kg'.format (ATM_mas_sno_beg, ATM_mas_sno_end) )
-echo ( 'dMass(neige atm) = {:12.3e} kg '.format (dATM_mas_sno) )
-echo ( 'dMass(neige atm) = {:12.3e} Sv '.format (dATM_mas_sno/dtime_sec*1E-6/ICE_rho_ice) )
-echo ( 'dMass(neige atm) = {:12.3e} m  '.format (dATM_mas_sno/ATM_aire_sea_tot*1E-3) )
+   ATM_sno_beg   = ATM_sno_beg .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_sno_end   = ATM_sno_end .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs_beg    = ATM_qs_beg  .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs_end    = ATM_qs_end  .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qsol_beg  = ATM_qsol_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qsol_end  = ATM_qsol_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs01_beg  = ATM_qs01_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs01_end  = ATM_qs01_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs02_beg  = ATM_qs02_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs02_end  = ATM_qs02_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs03_beg  = ATM_qs03_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs03_end  = ATM_qs03_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs04_beg  = ATM_qs04_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs04_end  = ATM_qs04_end.rename ( {'points_physiques':'cell_mesh'} ) 
+
+ATM_mas_sno_beg   = ATM_stock_int ( ATM_sno_beg  )
+ATM_mas_sno_end   = ATM_stock_int ( ATM_sno_end  )
+ATM_mas_qs_beg    = ATM_stock_int ( ATM_qs_beg   )
+ATM_mas_qs_end    = ATM_stock_int ( ATM_qs_end   )
+ATM_mas_qsol_beg  = ATM_stock_int ( ATM_qsol_beg )
+ATM_mas_qsol_end  = ATM_stock_int ( ATM_qsol_end )
+ATM_mas_qs01_beg  = ATM_stock_int ( ATM_qs01_beg )
+ATM_mas_qs01_end  = ATM_stock_int ( ATM_qs01_end )
+ATM_mas_qs02_beg  = ATM_stock_int ( ATM_qs02_beg )
+ATM_mas_qs02_end  = ATM_stock_int ( ATM_qs02_end )
+ATM_mas_qs03_beg  = ATM_stock_int ( ATM_qs03_beg )
+ATM_mas_qs03_end  = ATM_stock_int ( ATM_qs03_end )
+ATM_mas_qs04_beg  = ATM_stock_int ( ATM_qs04_beg )
+ATM_mas_qs04_end  = ATM_stock_int ( ATM_qs04_end )
+
+dATM_mas_sno  = ATM_mas_sno_end  - ATM_mas_sno_beg
+dATM_mas_qs   = ATM_mas_qs_end   - ATM_mas_qs_beg
+dATM_mas_qsol = ATM_mas_qsol_end - ATM_mas_qsol_beg
+
+dATM_mas_qs01 = ATM_mas_qs01_end - ATM_mas_qs01_beg
+dATM_mas_qs02 = ATM_mas_qs02_end - ATM_mas_qs02_beg
+dATM_mas_qs03 = ATM_mas_qs03_end - ATM_mas_qs03_beg
+dATM_mas_qs04 = ATM_mas_qs04_end - ATM_mas_qs04_beg
+
+echo ( '\nVariation du contenu en neige atmosphere (calottes)' )
+echo ( 'ATM_mas_sno_beg  = {:12.6e} kg | ATM_mas_sno_end  = {:12.6e} kg'.format (ATM_mas_sno_beg, ATM_mas_sno_end) )
+echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_sno ) )
+echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_sno/dtime_sec*1e-6/ICE_rho_ice) )
+echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_sno/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\nVariation du contenu humidite du sol' )
+echo ( 'ATM_mas_qs_beg  = {:12.6e} kg | ATM_mas_qs_end  = {:12.6e} kg'.format (ATM_mas_qs_beg, ATM_mas_qs_end) )
+echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_qs ) )
+echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_qs/dtime_sec*1e-6/ATM_rho) )
+echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_qs/ATM_aire_sea_tot/ATM_rho) )
 
 echo ( '\nVariation du contenu en eau+neige atmosphere ' )
-echo ( 'dMass(eau + neige atm) = {:12.3e} kg '.format (  dATM_mas_wat + dATM_mas_sno) )
-echo ( 'dMass(eau + neige atm) = {:12.3e} Sv '.format ( (dATM_mas_wat + dATM_mas_sno)/dtime_sec*1E-9) )
-echo ( 'dMass(eau + neige atm) = {:12.3e} m  '.format ( (dATM_mas_wat + dATM_mas_sno)/ATM_aire_sea_tot*1E-3) )
+echo ( 'dMass (eau + neige atm) = {:12.3e} kg '.format (  dDYN_mas_wat + dATM_mas_sno) )
+echo ( 'dMass (eau + neige atm) = {:12.3e} Sv '.format ( (dDYN_mas_wat + dATM_mas_sno)/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (eau + neige atm) = {:12.3e} m  '.format ( (dDYN_mas_wat + dATM_mas_sno)/ATM_aire_sea_tot/ATM_rho) )
 
 echo ( '\n------------------------------------------------------------------------------------' )
-echo ( '-- SRF changes in routing reservoirs' )
+echo ( '-- SRF changes ' )
+
+# dSoilHum_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=(maxvegetfrac*DelSoilMoist_daily)*Contfrac' ${file} -gridarea ${file}`
+# dInterce_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=DelIntercept_daily*Contfrac' ${file} -gridarea ${file}`
+# dSWE_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=DelSWE_daily*Contfrac' ${file} -gridarea ${file}`
+# dStream_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=delstreamr_daily*Contfrac' ${file} -gridarea ${file}`
+# dFastR_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=delfastr_daily*Contfrac' ${file} -gridarea ${file}`
+# dSlowR_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=delslowr_daily*Contfrac' ${file} -gridarea ${file}`
+# dFlood_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=delfloodr_daily*Contfrac' ${file} -gridarea ${file}`
+# dPond_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=delpondr_daily*Contfrac' ${file} -gridarea ${file}`
+# dLake_in_Sv=`cdo outputf,%12.8g,8 -divc,1.e9 -divc,${day2sec} -fldsum -mul -timavg -expr,'toto=dellakevol_daily*Contfrac' ${file} -gridarea ${file}`
+#echo 'dSTOCK (Sv) Soil Intercept SWE Stream FastR SlowR Lake Pond Flood=' $dSoilHum_in_Sv, $dInterce_in_Sv, $dSWE_in_Sv, $dStream_in_Sv, $dFastR_in_Sv, $dSlowR_in_Sv, $dLake_in_Sv, $dPond_in_Sv, $dFlood_in_Sv >> ${fileout}
+#dSRF_tot=`python -c "print $dSoilHum_in_Sv+$dInterce_in_Sv+$dSWE_in_Sv+$dStream_in_Sv+$dFastR_in_Sv+$dSlowR_in_Sv+$dLake_in_Sv+$dPond_in_Sv+$dFlood_in_Sv"`
+#echo 'dSTOCK (Sv) total='${dSRF_tot} >> ${fileout}
+
 
 if Routing == 'SIMPLE' :
-    RUN_mas_wat_beg = np.sum ( d_RUN_beg ['fast_reservoir'] +  d_RUN_beg ['slow_reservoir'] +  d_RUN_beg ['stream_reservoir']).values.item()
-    RUN_mas_wat_end = np.sum ( d_RUN_end ['fast_reservoir'] +  d_RUN_end ['slow_reservoir'] +  d_RUN_end ['stream_reservoir']).values.item()
+    RUN_mas_wat_beg = ONE_stock_int ( d_RUN_beg ['fast_reservoir'] +  d_RUN_beg ['slow_reservoir'] +  d_RUN_beg ['stream_reservoir'] )
+    RUN_mas_wat_end = ONE_stock_int ( d_RUN_end ['fast_reservoir'] +  d_RUN_end ['slow_reservoir'] +  d_RUN_end ['stream_reservoir'] )
 
 if Routing == 'ORCHIDEE' : 
-    RUN_mas_wat_beg = np.sum ( d_SRF_beg['fastres']  + d_SRF_beg['slowres'] + d_SRF_beg['streamres'] \
-                             + d_SRF_beg['floodres'] + d_SRF_beg['lakeres'] + d_SRF_beg['pondres'] ) .values.item()
-    RUN_mas_wat_end = np.sum ( d_SRF_end['fastres']  + d_SRF_end['slowres'] + d_SRF_end['streamres'] \
-                             + d_SRF_end['floodres'] + d_SRF_end['lakeres'] + d_SRF_end['pondres'] ) .values.item()
+    RUN_mas_wat_beg = ONE_stock_int ( d_SRF_beg['fastres']  + d_SRF_beg['slowres'] + d_SRF_beg['streamres'] \
+                                    + d_SRF_beg['floodres'] + d_SRF_beg['lakeres'] + d_SRF_beg['pondres']  )
+    RUN_mas_wat_end = ONE_stock_int ( d_SRF_end['fastres']  + d_SRF_end['slowres'] + d_SRF_end['streamres'] \
+                                    + d_SRF_end['floodres'] + d_SRF_end['lakeres'] + d_SRF_end['pondres']  )
 
 dRUN_mas_wat = RUN_mas_wat_end - RUN_mas_wat_beg
     
 echo ( '\nWater content in routing ' )
-echo ( 'RUN_mas_wat_beg = {:12.6e} kg '.format (RUN_mas_wat_beg) )
-echo ( 'RUN_mas_wat_end = {:12.6e} kg '.format (RUN_mas_wat_end) )
-echo ( 'dMass(routing)  = {:12.3e} kg '.format(dRUN_mas_wat) )
-echo ( 'dMass(routing)  = {:12.3e} Sv '.format(dRUN_mas_wat/dtime_sec*1E-9) )
-echo ( 'dMass(routing)  = {:12.3e} m  '.format(dRUN_mas_wat/ATM_aire_sea_tot*1E-3) )
-    
-SRF_mas_wat_beg = d_SRF_beg['tot_watveg_beg']+d_SRF_beg['tot_watsoil_beg'] + d_SRF_beg['snow_beg']
-SRF_mas_wat_end = d_SRF_end['tot_watveg_beg']+d_SRF_end['tot_watsoil_beg'] + d_SRF_end['snow_beg']
-
-if LMDZ : 
-    SRF_mas_wat_beg = SRF_mas_wat_beg.rename ( {'y':'points_phyiques'} )
-    SRF_mas_wat_end = SRF_mas_wat_end.rename ( {'y':'points_phyiques'} )
-if ICO : 
-    SRF_mas_wat_beg = SRF_mas_wat_beg.rename ( {'y':'cell_mesh'} )
-    SRF_mas_wat_end = SRF_mas_wat_end.rename ( {'y':'cell_mesh'} )  
-
-SRF_mas_wat_beg = np.sum ( SRF_mas_wat_beg * SRF_aire ).values.item()
-SRF_mas_wat_end = np.sum ( SRF_mas_wat_end * SRF_aire ).values.item()
-
+echo ( 'RUN_mas_wat_beg = {:12.6e} kg | RUN_mas_wat_end = {:12.6e} kg '.format (RUN_mas_wat_end, RUN_mas_wat_end) )
+echo ( 'dMass (routing)  = {:12.3e} kg '.format(dRUN_mas_wat) )
+echo ( 'dMass (routing)  = {:12.3e} Sv '.format(dRUN_mas_wat/dtime_sec*1E-9) )
+echo ( 'dMass (routing)  = {:12.3e} m  '.format(dRUN_mas_wat/ATM_aire_sea_tot*1E-3) )
+
+print ('Reading SRF restart')
+tot_watveg_beg  = d_SRF_beg['tot_watveg_beg']  ; tot_watveg_beg  = tot_watveg_beg .where (tot_watveg_beg < 1E10, 0.)
+tot_watsoil_beg = d_SRF_beg['tot_watsoil_beg'] ; tot_watsoil_beg = tot_watsoil_beg.where (tot_watsoil_beg< 1E10, 0.)
+snow_beg        = d_SRF_beg['snow_beg']        ; snow_beg        = snow_beg       .where (snow_beg       < 1E10, 0.)
+
+tot_watveg_end  = d_SRF_end['tot_watveg_beg']  ; tot_watveg_end  = tot_watveg_end .where (tot_watveg_end < 1E10, 0.)
+tot_watsoil_end = d_SRF_end['tot_watsoil_beg'] ; tot_watsoil_end = tot_watsoil_end.where (tot_watsoil_end< 1E10, 0.)
+snow_end        = d_SRF_end['snow_beg']        ; snow_end        = snow_end       .where (snow_end       < 1E10, 0.)
+
+if LMDZ :
+    tot_watveg_beg  = lmdz.geo2point (tot_watveg_beg)
+    tot_watsoil_beg = lmdz.geo2point (tot_watsoil_beg)
+    snow_beg        = lmdz.geo2point (snow_beg)
+    tot_watveg_end  = lmdz.geo2point (tot_watveg_end)
+    tot_watsoil_end = lmdz.geo2point (tot_watsoil_end)
+    snow_end        = lmdz.geo2point (snow_end)
+
+# Stock dSoilHum dInterce dSWE dStream dFastR dSlowR dLake dPond dFlood
+
+SRF_wat_beg = tot_watveg_beg + tot_watsoil_beg + snow_beg
+SRF_wat_end = tot_watveg_end + tot_watsoil_end + snow_end
+    
+if ICO :
+    tot_watveg_beg  = tot_watveg_beg .rename ( {'y':'cell_mesh'} )
+    tot_watsoil_beg = tot_watsoil_beg.rename ( {'y':'cell_mesh'} )
+    snow_beg        = snow_beg       .rename ( {'y':'cell_mesh'} )
+    tot_watveg_end  = tot_watveg_end .rename ( {'y':'cell_mesh'} )
+    tot_watsoil_end = tot_watsoil_end.rename ( {'y':'cell_mesh'} )
+    snow_end        = snow_end       .rename ( {'y':'cell_mesh'} )
+    SRF_wat_beg     = SRF_wat_beg    .rename ( {'y':'cell_mesh'} )
+    SRF_wat_end     = SRF_wat_end    .rename ( {'y':'cell_mesh'} )
+
+print ('Computing integrals')
+
+print ( ' 1/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watveg_beg   = ATM_stock_int ( tot_watveg_beg  )
+print ( ' 2/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watsoil_beg  = ATM_stock_int ( tot_watsoil_beg )
+print ( ' 3/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_snow_beg     = ATM_stock_int ( snow_beg        )
+print ( ' 4/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watveg_end   = ATM_stock_int ( tot_watveg_end  )
+print ( ' 5/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watsoil_end  = ATM_stock_int ( tot_watsoil_end )
+print ( ' 6/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_snow_end     = ATM_stock_int ( snow_end        )
+print (' -- ') ; sys.stdout.flush ()
+
+dSRF_mas_watveg   = SRF_mas_watveg_end   - SRF_mas_watveg_beg
+dSRF_mas_watsoil  = SRF_mas_watsoil_end  - SRF_mas_watsoil_beg
+dSRF_mas_snow     = SRF_mas_snow_end     - SRF_mas_snow_beg
+
+echo ('\nLes differents reservoirs')
+echo ( 'SRF_mas_watveg_beg   = {:12.6e} kg | SRF_mas_watveg_end   = {:12.6e} kg '.format (SRF_mas_watveg_beg  , SRF_mas_watveg_end   ) )
+echo ( 'SRF_mas_watsoil_beg  = {:12.6e} kg | SRF_mas_watsoil_end  = {:12.6e} kg '.format (SRF_mas_watsoil_beg , SRF_mas_watsoil_end  ) )
+echo ( 'SRF_mas_snow_beg     = {:12.6e} kg | SRF_mas_snow_end     = {:12.6e} kg '.format (SRF_mas_snow_beg    , SRF_mas_snow_end     ) )
+
+echo ( 'dMass (watveg)    = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_watveg  , dSRF_mas_watveg  /dtime_sec*1E-9, dSRF_mas_watveg  /ATM_aire_sea_tot*1E-3) )
+echo ( 'dMass (watsoil)   = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_watsoil , dSRF_mas_watsoil /dtime_sec*1E-9, dSRF_mas_watsoil /ATM_aire_sea_tot*1E-3) )
+echo ( 'dMass (sno)       = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_snow    , dSRF_mas_snow    /dtime_sec*1E-9, dSRF_mas_snow    /ATM_aire_sea_tot*1E-3) )
+
+SRF_mas_wat_beg = SRF_mas_watveg_beg + SRF_mas_watsoil_beg + SRF_mas_snow_beg 
+SRF_mas_wat_end = SRF_mas_watveg_end + SRF_mas_watsoil_end + SRF_mas_snow_end 
 dSRF_mas_wat = SRF_mas_wat_end - SRF_mas_wat_beg
 
-echo ( '\nWater content in and surface ' )
-echo ( 'SRF_mas_wat_beg  = {:12.6e} kg - SRF_mas_wat_end  = {:12.6e} kg '.format (SRF_mas_wat_beg, SRF_mas_wat_end) )
-echo ( 'dMass(water srf) = {:12.3e} kg '.format (dSRF_mas_wat) )
-echo ( 'dMass(water srf) = {:12.3e} Sv '.format (dSRF_mas_wat/dtime_sec*1E-9) )
-echo ( 'dMass(water srf) = {:12.3e} m  '.format (dSRF_mas_wat/ATM_aire_sea_tot*1E-3) )
+echo ( '\nWater content in surface ' )
+echo ( 'SRF_mas_wat_beg  = {:12.6e} kg | SRF_mas_wat_end  = {:12.6e} kg '.format (SRF_mas_wat_beg, SRF_mas_wat_end) )
+echo ( 'dMass (water srf) = {:12.3e} kg '.format (dSRF_mas_wat) )
+echo ( 'dMass (water srf) = {:12.3e} Sv '.format (dSRF_mas_wat/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (water srf) = {:12.3e} m  '.format (dSRF_mas_wat/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\nWater content in  ATM + SRF + RUN ' )
+echo ( 'mas_wat_beg = {:12.6e} kg | mas_wat_end = {:12.6e} kg '.
+           format (DYN_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg,
+                   DYN_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
+echo ( 'dMass (water atm+srf+run) = {:12.6e} kg '.format ( dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat) )
+echo ( 'dMass (water atm+srf+run) = {:12.3e} Sv '.format ((dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (water atm+srf+run) = {:12.3e} m  '.format ((dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/ATM_aire_sea_tot/ATM_rho) )
 
 echo ( '\n------------------------------------------------------------------------------------' )
-echo ( '\nWater content in  ATM + SRF + RUN ' )
-echo ( 'mas_wat_beg              = {:12.6e} kg '.format (ATM_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg) )
-echo ( 'mas_wat_end              = {:12.6e} kg '.format (ATM_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
-echo ( 'dMass(water atm+srf+run) = {:12.6e} kg '.format ( dATM_mas_wat   + dATM_mas_sno    + dRUN_mas_wat    + dSRF_mas_wat) )
-echo ( 'dMass(water atm+srf+run) = {:12.3e} Sv '.format ((dATM_mas_wat   + dATM_mas_sno    + dRUN_mas_wat    + dSRF_mas_wat)/dtime_sec*1E-9) )
-echo ( 'dMass(water atm+srf+run) = {:12.3e} m  '.format ((dATM_mas_wat   + dATM_mas_sno    + dRUN_mas_wat    + dSRF_mas_wat)/ATM_aire_sea_tot*1E-3) )
-
-
-echo ( "\n-----------------" )
-echo ( " Atm -> Oce fluxes" )
-
-ATM_wbilo_sea = np.sum ( lmdz.geo2point (d_ATM_his['wbilo_oce'] + d_ATM_his['wbilo_sic'])*dtime_per_sec*ATM_aire ).values.item()
-ATM_runoff    = np.sum ( lmdz.geo2point (d_ATM_his['runofflic'])*ATM_aire*dtime_per_sec ).values.item()
-ATM_calving   = np.sum ( lmdz.geo2point (d_ATM_his['fqcalving'])*ATM_aire*dtime_per_sec ).values.item()
-
-echo (' wbilo oce+sic         = {:12.5e} (kg) '.format ( ATM_wbilo_sea ) )
-echo (' runoff liq            = {:12.5e} (kg) '.format ( ATM_runoff    ) )
-echo (' calving               = {:12.5e} (kg) '.format ( ATM_calving   ) )
-echo (' total                 = {:12.5e} (kg) '.format ( ATM_wbilo_sea - ATM_runoff - ATM_calving ) )
+echo ( '-- ATM Fluxes ' )
+
+ATM_wbilo_oce   = lmdz.geo2point ( d_ATM_his ['wbilo_oce']   )
+ATM_wbilo_sic   = lmdz.geo2point ( d_ATM_his ['wbilo_sic']   )
+ATM_wbilo_ter   = lmdz.geo2point ( d_ATM_his ['wbilo_ter']   )
+ATM_wbilo_lic   = lmdz.geo2point ( d_ATM_his ['wbilo_lic']   )
+ATM_runofflic   = lmdz.geo2point ( d_ATM_his ['runofflic']   )
+ATM_fqcalving   = lmdz.geo2point ( d_ATM_his ['fqcalving']   )
+ATM_fqfonte     = lmdz.geo2point ( d_ATM_his ['fqfonte']     )
+ATM_precip      = lmdz.geo2point ( d_ATM_his ['precip']      )
+ATM_snowf       = lmdz.geo2point ( d_ATM_his ['snow']        )
+ATM_evap        = lmdz.geo2point ( d_ATM_his ['evap']        )
+ATM_bilo = ATM_wbilo_oce + ATM_wbilo_ter + ATM_wbilo_lic + ATM_wbilo_sic
+
+RUN_coastalflow = lmdz.geo2point ( d_RUN_his ['coastalflow'] ) 
+RUN_riverflow   = lmdz.geo2point ( d_RUN_his ['riverflow']   )
+RUN_runoff      = lmdz.geo2point ( d_RUN_his ['runoff']      )
+RUN_drainage    = lmdz.geo2point ( d_RUN_his ['drainage']    )
+RUN_riversret   = lmdz.geo2point ( d_RUN_his ['riversret']   )
+
+SRF_evap     = lmdz.geo2point ( d_SRF_his ['evap'] )
+SRF_snowf    = lmdz.geo2point ( d_SRF_his ['snowf'] )
+SRF_TWS      = lmdz.geo2point ( d_SRF_his ['TWS'] )
+SRF_subli    = lmdz.geo2point ( d_SRF_his ['subli'] )
+SRF_transpir = lmdz.geo2point ( np.sum(d_SRF_his ['transpir'], axis=1) ) ; SRF_transpir.attrs['units'] = d_SRF_his ['transpir'].attrs['units']
+
+def mmd2SI ( Var) :
+    '''Change unit from mm/d or m^3/s to kg/s if needed'''
+    if 'units' in VarT.attrs : 
+        if VarT.attrs['units'] in ['m^3/s', 'm3/s', 'm3.s-3'] :
+            VarT.values = VarT.values  * ATM_rho                 ;  VarT.attrs['units'] = 'kg/s'
+        if VarT.attrs['units'] == 'mm/d' :
+            VarT.values = VarT.values  * ATM_rho * (1e-3/86400.) ;  VarT.attrs['units'] = 'kg/s'
+
+for var in ['runoff', 'drainage', 'riversret', 'coastalflow', 'riverflow'] :
+    VarT = locals()['RUN_' + var]
+    mmd2SI (VarT)
+
+for var in ['evap', 'snowf', 'TWS', 'subli', 'transpir'] :
+    VarT = locals()['SRF_' + var]
+    mmd2SI (VarT)
+
+RUN_input  = RUN_runoff      + RUN_drainage
+RUN_output = RUN_coastalflow + RUN_riverflow
+
+ATM_wbilo_sea = ATM_wbilo_oce + ATM_wbilo_sic
+
+ATM_flx_oce       = ATM_flux_int ( ATM_wbilo_oce  )
+ATM_flx_sic       = ATM_flux_int ( ATM_wbilo_sic  )
+ATM_flx_sea       = ATM_flux_int ( ATM_wbilo_sea  )
+ATM_flx_ter       = ATM_flux_int ( ATM_wbilo_ter  )
+ATM_flx_lic       = ATM_flux_int ( ATM_wbilo_lic  )
+ATM_flx_calving   = ATM_flux_int ( ATM_fqcalving  )
+ATM_flx_qfonte    = ATM_flux_int ( ATM_fqfonte    )
+ATM_flx_precip    = ATM_flux_int ( ATM_precip     )
+ATM_flx_snowf     = ATM_flux_int ( ATM_snowf      )
+ATM_flx_evap      = ATM_flux_int ( ATM_evap       )
+ATM_flx_runlic    = ATM_flux_int ( ATM_runofflic  )
+
+RUN_flx_coastal   = ONE_flux_int ( RUN_coastalflow)
+RUN_flx_river     = ONE_flux_int ( RUN_riverflow  )
+RUN_flx_drainage  = ATM_flux_int ( RUN_drainage   )
+RUN_flx_riversret = ATM_flux_int ( RUN_riversret  )
+RUN_flx_runoff    = ATM_flux_int ( RUN_runoff     )
+RUN_flx_input     = ATM_flux_int ( RUN_input      )
+RUN_flx_output    = ONE_flux_int ( RUN_output     )
+
+ATM_flx_emp   = ATM_flx_evap - ATM_flx_precip
+ATM_flx_bilo  = ATM_flx_oce + ATM_flx_sic + ATM_flx_ter + ATM_flx_lic
+RUN_flx_bil   = RUN_flx_input - RUN_flx_output
+
+RUN_flx_rivcoa = RUN_flx_coastal + RUN_flx_river
+
+ATM_flx_bilo2 = ATM_flux_int (ATM_bilo)
+
+
+echo ('  wbilo_oce     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_oce      , ATM_flx_oce      / dtime_sec*1E-6/ATM_rho, ATM_flx_oce      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  wbilo_sic     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_sic      , ATM_flx_sic      / dtime_sec*1E-6/ATM_rho, ATM_flx_sic      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  wbilo_sic+oce {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_sea      , ATM_flx_sea      / dtime_sec*1E-6/ATM_rho, ATM_flx_sea      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  wbilo_ter     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_ter      , ATM_flx_ter      / dtime_sec*1E-6/ATM_rho, ATM_flx_ter      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  wbilo_lic     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_lic      , ATM_flx_lic      / dtime_sec*1E-6/ATM_rho, ATM_flx_lic      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  Sum wbilo_*   {:12.3e} (kg) | {:12.4e} (Sv) | {:12.4f} m '.format ( ATM_flx_bilo     , ATM_flx_bilo     / dtime_sec*1E-6/ATM_rho, ATM_flx_bilo     /ATM_aire_sea_tot/ATM_rho ))
+echo ('  E-P           {:12.3e} (kg) | {:12.4e} (Sv) | {:12.4f} m '.format ( ATM_flx_emp      , ATM_flx_emp      / dtime_sec*1E-6/ATM_rho, ATM_flx_emp      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  calving       {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_calving  , ATM_flx_calving  / dtime_sec*1E-6/ATM_rho, ATM_flx_calving  /ATM_aire_sea_tot/ATM_rho ))
+echo ('  fqfonte       {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_qfonte   , ATM_flx_qfonte   / dtime_sec*1E-6/ATM_rho, ATM_flx_qfonte   /ATM_aire_sea_tot/ATM_rho ))
+echo ('  precip        {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_precip   , ATM_flx_precip   / dtime_sec*1E-6/ATM_rho, ATM_flx_precip   /ATM_aire_sea_tot/ATM_rho ))
+echo ('  snowf         {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_snowf    , ATM_flx_snowf    / dtime_sec*1E-6/ATM_rho, ATM_flx_snowf    /ATM_aire_sea_tot/ATM_rho ))
+echo ('  evap          {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_evap     , ATM_flx_evap     / dtime_sec*1E-6/ATM_rho, ATM_flx_evap     /ATM_aire_sea_tot/ATM_rho ))
+echo ('  coastalflow   {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_coastal  , RUN_flx_coastal  / dtime_sec*1E-6/ATM_rho, RUN_flx_coastal  /ATM_aire_sea_tot/ATM_rho ))
+echo ('  riverflow     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_river    , RUN_flx_river    / dtime_sec*1E-6/ATM_rho, RUN_flx_river    /ATM_aire_sea_tot/ATM_rho ))
+echo ('  river+coastal {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_rivcoa   , RUN_flx_rivcoa   / dtime_sec*1E-6/ATM_rho, RUN_flx_rivcoa   /ATM_aire_sea_tot/ATM_rho ))
+echo ('  drainage      {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_drainage , RUN_flx_drainage / dtime_sec*1E-6/ATM_rho, RUN_flx_drainage /ATM_aire_sea_tot/ATM_rho ))
+echo ('  riversret     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_riversret, RUN_flx_riversret/ dtime_sec*1E-6/ATM_rho, RUN_flx_riversret/ATM_aire_sea_tot/ATM_rho ))
+echo ('  runoff        {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_runoff   , RUN_flx_runoff   / dtime_sec*1E-6/ATM_rho, RUN_flx_runoff   /ATM_aire_sea_tot/ATM_rho ))
+echo ('  river in      {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_input    , RUN_flx_input    / dtime_sec*1E-6/ATM_rho, RUN_flx_input    /ATM_aire_sea_tot/ATM_rho ))
+echo ('  river out     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_output   , RUN_flx_output   / dtime_sec*1E-6/ATM_rho, RUN_flx_output   /ATM_aire_sea_tot/ATM_rho ))
+echo ('  river bil     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( RUN_flx_bil      , RUN_flx_bil      / dtime_sec*1E-6/ATM_rho, RUN_flx_bil      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  runoff lic    {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_runlic   , ATM_flx_runlic   / dtime_sec*1E-6/ATM_rho, ATM_flx_runlic   /ATM_aire_sea_tot/ATM_rho ))
+
+ATM_flx_budget = -ATM_flx_sea + ATM_flx_calving + ATM_flx_runlic #+ ATM_flx_qfonte + RUN_flx_river 
+echo ('\n  Global      {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( ATM_flx_budget , ATM_flx_budget / dtime_sec*1E-9, ATM_flx_budget /ATM_aire_sea_tot/ATM_rho ))
+
+#echo ('  E-P-R         {:12.3e} (kg) | {:12.4e} (Sv) | {:12.4f} m '.format ( ATM_flx_emp      , ATM_flx_emp      / dtime_sec*1E-6/ATM_rho, ATM_flx_emp      /ATM_aire_sea_tot/ATM_rho ))
+
+
+echo ( '------------------------------------------------------------------------------------' )
+echo ( '-- SECHIBA fluxes' )
+
+
+SRF_flx_evap     = ATM_flux_int ( SRF_evap     )
+SRF_flx_snowf    = ATM_flux_int ( SRF_snowf    )
+SRF_flx_subli    = ATM_flux_int ( SRF_subli    )
+SRF_flx_transpir = ATM_flux_int ( SRF_transpir )
+
+echo ('  evap     {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( SRF_flx_evap      , SRF_flx_evap      / dtime_sec*1E-6/ATM_rho, SRF_flx_evap      /ATM_aire_sea_tot/ATM_rho ))
+echo ('  snowf    {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( SRF_flx_snowf     , SRF_flx_snowf     / dtime_sec*1E-6/ATM_rho, SRF_flx_snowf     /ATM_aire_sea_tot/ATM_rho ))
+echo ('  subli    {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( SRF_flx_subli     , SRF_flx_subli     / dtime_sec*1E-6/ATM_rho, SRF_flx_subli     /ATM_aire_sea_tot/ATM_rho ))
+echo ('  transpir {:12.3e} (kg) | {:12.4f} (Sv) | {:12.4f} m '.format ( SRF_flx_transpir  , SRF_flx_transpir  / dtime_sec*1E-6/ATM_rho, SRF_flx_transpir  /ATM_aire_sea_tot/ATM_rho ))
Index: TOOLS/WATER_BUDGET/CPL_waterbudget.py
===================================================================
--- TOOLS/WATER_BUDGET/CPL_waterbudget.py	(revision 6271)
+++ TOOLS/WATER_BUDGET/CPL_waterbudget.py	(revision 6271)
@@ -0,0 +1,806 @@
+#!/usr/bin/env python3
+###
+### Script to check water conservation in the IPSL coupled model
+###
+##  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$
+#  $Date$
+#  $Revision$
+#  $Id$
+#  $HeadURL$
+
+
+###
+## Import system modules
+import sys, os, shutil, subprocess
+import numpy as np
+import configparser, re
+
+## Creates parser
+config = configparser.ConfigParser()
+config.optionxform = str # To keep capitals
+
+config['Files']  = {}
+config['System'] = {}
+
+##-- Some physical constants
+#-- Earth Radius
+Ra = 6366197.7236758135
+#-- Gravity
+grav = 9.81
+#-- Ice volumic mass (kg/m3) in LIM3/SI3
+ICE_rho_ice = 917.0
+#-- Snow volumic mass (kg/m3) in LIM3/SI3
+ICE_rho_sno = 330.0
+#-- Water in ponds coluclic mass (kg/m3) in LIM3/SI3
+ICE_rho_pnd = 1000.
+#-- Ocean water volumic mass (kg/m3) in NEMO
+OCE_rho_liq = 1026.
+#-- Water volumic mass in atmosphere
+ATM_rho = 1.0e3
+#-- Water volumic mass in surface reservoirs
+SRF_rho = 1.0e3
+#-- Water volumic mass of rivers
+RUN_rho = 1.0e3
+
+## Read experiment parameters
+ATM = None ; ORCA = None ; NEMO = None ; OCE_relax = False ; OCE_icb = False ; Coupled = False ; Routing = None
+
+# Arguments passed
+print ( "Name of Python script:", sys.argv[0] )
+IniFile =  sys.argv[1]
+print ("Input file : ", IniFile )
+config.read (IniFile)
+
+def setBool (chars) :
+    '''Convert specific char string in boolean if possible'''
+    setBool = chars
+    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]+)?$")
+        isReal = realnum.match(chars.strip()) != None
+        isInt  = chars.strip().isdigit()
+        if isReal :
+            if isInt : setNum = int   (chars)
+            else     : setNum = float (chars)
+        else : setNum = chars
+    else : setNum = chars
+    return setNum
+
+print ('[Experiment]')
+for VarName in config['Experiment'].keys() :
+    locals()[VarName] = config['Experiment'][VarName]
+    locals()[VarName] = setBool (locals()[VarName])
+    locals()[VarName] = setNum  (locals()[VarName])
+    print ( '{:25} set to : {:}'.format (VarName, locals()[VarName]) )
+    
+###
+ICO  = ( 'ICO' in ATM )
+LMDZ = ( 'LMD' in ATM )
+    
+###
+## Import system modules
+import sys, os, shutil, subprocess
+
+# Where do we run ?
+SysName, NodeName, Release, Version, Machine = os.uname()
+TGCC  = ( 'irene'   in NodeName )
+IDRIS = ( 'jeanzay' in NodeName )
+
+## Set site specific libIGCM directories, and other specific stuff
+if TGCC :
+    CPU = subprocess.getoutput ( 'lscpu | grep "Model name"' )
+    if "Intel(R) Xeon(R) Platinum" in CPU : Machine = 'irene'
+    if "AMD"                       in CPU : Machine = 'irene-amd'
+        
+    ARCHIVE     = subprocess.getoutput ( f'ccc_home --cccstore   -d {Project} -u {User}' )
+    STORAGE     = subprocess.getoutput ( f'ccc_home --cccwork    -d {Project} -u {User}' )
+    SCRATCHDIR  = subprocess.getoutput ( f'ccc_home --cccscratch -d {Project} -u {User}' )
+    R_IN        = os.path.join ( subprocess.getoutput ( f'ccc_home --cccwork -d igcmg -u igcmg' ), 'IGCM')
+    rebuild     = os.path.join ( subprocess.getoutput ( f'ccc_home --ccchome -d igcmg -u igcmg' ), 'Tools', Machine, 'rebuild_nemo', 'bin', 'rebuild_nemo' )
+
+    ## Specific to run at TGCC.
+    # Needed before importing a NetCDF library (netCDF4, xarray, cmds, etc ...)
+    import mpi4py
+    mpi4py.rc.initialize = False
+        
+    ## Creates output directory
+    #TmpDir = os.path.join ( subprocess.getoutput ( 'ccc_home --cccscratch' ), f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
+    TmpDir = os.path.join ( '/ccc/scratch/cont003/drf/p86mart', f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
+
+if IDRIS :
+    raise Exception("Pour IDRIS : repertoires et chemins a definir") 
+
+## Import specific module
+import nemo, lmdz
+## Now import needed scientific modules
+import xarray as xr
+    
+# Output file
+FileOut = f'CPL_waterbudget_{JobName}_{YearBegin}_{YearEnd}.out'
+f_out = open ( FileOut, mode = 'w' )
+
+# Function to print to stdout *and* output file
+def echo (string, end='\n') :
+    print ( string, end=end  )
+    sys.stdout.flush ()
+    f_out.write ( string + end )
+    f_out.flush ()
+    return None
+    
+## Set libIGCM directories
+R_OUT       = os.path.join ( ARCHIVE   , 'IGCM_OUT')
+R_BUF       = os.path.join ( SCRATCHDIR, 'IGCM_OUT')
+
+L_EXP = os.path.join (TagName, SpaceName, ExperimentName, JobName)
+R_SAVE      = os.path.join ( R_OUT, L_EXP )
+R_BUFR      = os.path.join ( R_BUF, L_EXP )
+POST_DIR    = os.path.join ( R_BUF, L_EXP, 'Out' )
+REBUILD_DIR = os.path.join ( R_BUF, L_EXP, 'REBUILD' )
+R_BUF_KSH   = os.path.join ( R_BUFR, 'Out' )
+R_FIGR      = os.path.join ( STORAGE, 'IGCM_OUT', L_EXP )
+
+#if os.path.isdir (TmpDir) : shutil.rmtree ( TmpDir )
+if not os.path.isdir (TmpDir) : os.mkdir (TmpDir)
+TmpDirOCE = os.path.join (TmpDir, 'OCE')
+TmpDirICE = os.path.join (TmpDir, 'ICE')
+if not os.path.exists (TmpDirOCE) : os.mkdir (TmpDirOCE )
+if not os.path.exists (TmpDirICE) : os.mkdir (TmpDirICE )
+
+echo ( f'Working in TMPDIR : {TmpDir}' )
+
+echo ( f'\nDealing with {L_EXP}' )
+
+#-- Model output directories
+if Freq == "MO" : FreqDir =  os.path.join ('Output' , 'MO' )
+if Freq == "SE" : FreqDir =  os.path.join ('Analyse', 'SE' )
+dir_ATM_his = os.path.join ( R_SAVE, "ATM", FreqDir )
+dir_OCE_his = os.path.join ( R_SAVE, "OCE", FreqDir )
+dir_ICE_his = os.path.join ( R_SAVE, "ICE", FreqDir )
+dir_SRF_his = os.path.join ( R_SAVE, "SRF", FreqDir )
+
+echo ( f'The analysis relies on files from the following model output directories : ' )
+echo ( f'{dir_ATM_his}' )
+echo ( f'{dir_OCE_his}' )
+echo ( f'{dir_ICE_his}' )
+echo ( f'{dir_SRF_his}' )
+
+#-- Files Names
+if Freq == 'MO' : FileCommon = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
+if Freq == 'SE' : FileCommon = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
+
+echo ('\nOpen history files' )
+file_ATM_his = os.path.join (  dir_ATM_his, f'{FileCommon}_histmth.nc' )
+file_OCE_his = os.path.join (  dir_OCE_his, f'{FileCommon}_grid_T.nc' )
+file_OCE_sca = os.path.join (  dir_OCE_his, f'{FileCommon}_scalar.nc' )
+file_ICE_his = os.path.join (  dir_ICE_his, f'{FileCommon}_icemod.nc' )
+file_SRF_his = os.path.join (  dir_SRF_his, f'{FileCommon}_sechiba_history.nc' )
+file_OCE_srf = os.path.join (  dir_OCE_his, f'{FileCommon}_grid_T.nc' )
+
+d_ATM_his = xr.open_dataset ( file_ATM_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+d_OCE_his = xr.open_dataset ( file_OCE_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+d_OCE_sca = xr.open_dataset ( file_OCE_sca, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+d_ICE_his = xr.open_dataset ( file_ICE_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+if NEMO == '3.6' :d_ICE_his = d_ICE_his.rename ( {'y_grid_T':'y', 'x_grid_T':'x'} )
+d_SRF_his = xr.open_dataset ( file_SRF_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+d_OCE_srf = xr.open_dataset ( file_OCE_srf, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
+
+echo ( file_ATM_his )
+echo ( file_OCE_his )
+echo ( file_OCE_sca )
+echo ( file_ICE_his )
+echo ( file_SRF_his )
+
+## Compute run length
+dtime = ( d_ATM_his.time_counter_bounds.max() - d_ATM_his.time_counter_bounds.min() )
+echo ('\nRun length : {:8.2f} days'.format ( (dtime/np.timedelta64(1, "D")).values ) )
+dtime_sec = (dtime/np.timedelta64(1, "s")).values.item() # Convert in seconds
+
+## Compute length of each period
+dtime_per = (d_ATM_his.time_counter_bounds[:,-1] -  d_ATM_his.time_counter_bounds[:,0] )
+echo ('\nPeriods lengths (days) : {:} days'.format ( (dtime_per/np.timedelta64(1, "D")).values ) )
+dtime_per_sec = (dtime_per/np.timedelta64(1, "s")).values # In seconds
+dtime_per_sec = xr.DataArray (dtime_per_sec, dims=["time_counter", ], coords=[d_ATM_his.time_counter,] )
+
+#-- Open restart files
+YearRes = YearBegin - 1        # Year of the restart of beginning of simulation
+YearPre = YearBegin - PackFrequency  # Year to find the tarfile of the restart of beginning of simulation
+
+echo (f'Restart dates - Start : {YearRes}-12-31  /  End : {YearEnd}-12-31 ')
+
+file_restart_beg = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{YearPre}0101_{YearRes}1231_restart.tar' )
+file_restart_end = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{YearBegin}0101_{YearEnd}1231_restart.tar' )
+
+echo ( f'{file_restart_beg}' )
+echo ( f'{file_restart_end}' )
+
+file_OCE_beg = f'OCE_{JobName}_{YearRes}1231_restart.nc'
+file_OCE_end = f'OCE_{JobName}_{YearEnd}1231_restart.nc'
+file_ICE_beg = f'ICE_{JobName}_{YearRes}1231_restart_icemod.nc'
+file_ICE_end = f'ICE_{JobName}_{YearEnd}1231_restart_icemod.nc'
+
+echo ( f'{file_OCE_beg}' )
+echo ( f'{file_OCE_end}' )
+echo ( f'{file_ICE_beg}' )
+echo ( f'{file_ICE_end}' )
+
+file_ATM_beg = f'ATM_{JobName}_{YearRes}1231_restartphy.nc'
+file_ATM_end = f'ATM_{JobName}_{YearEnd}1231_restartphy.nc'
+if LMDZ :
+    file_DYN_beg = f'ATM_{JobName}_{YearRes}1231_restart.nc'
+    file_DYN_end = f'ATM_{JobName}_{YearEnd}1231_restart.nc'
+if ICO :
+    file_DYN_beg = f'ICO_{JobName}_{YearRes}1231_restart.nc'
+    file_DYN_end = f'ICO_{JobName}_{YearEnd}1231_restart.nc'
+file_SRF_beg = f'SRF_{JobName}_{YearRes}1231_sechiba_rest.nc'
+file_SRF_end = f'SRF_{JobName}_{YearEnd}1231_sechiba_rest.nc'
+liste_beg = [file_ATM_beg, file_DYN_beg, file_SRF_beg, ]
+liste_end = [file_ATM_end, file_DYN_end, file_SRF_end, ]
+echo ( f'{file_ATM_beg}')
+echo ( f'{file_ATM_end}')
+echo ( f'{file_SRF_beg}')
+echo ( f'{file_SRF_end}')
+
+if Routing == 'SIMPLE' : 
+    file_RUN_beg = f'SRF_{JobName}_{YearRes}1231_routing_restart.nc'
+    file_RUN_end = f'SRF_{JobName}_{YearEnd}1231_routing_restart.nc'
+    liste_beg.append ( file_RUN_beg )
+    liste_end.append ( file_RUN_end )
+    echo ( f'{file_RUN_beg}')
+    echo ( f'{file_RUN_end}')
+
+echo ('\nExtract restart files from tar : ATM, ICO and SRF')
+for resFile in liste_beg :
+    if not os.path.exists ( os.path.join (TmpDir, resFile) ) :
+        command =  f'cd {TmpDir} ; tar xf {file_restart_beg} {resFile}'
+        echo ( command )
+        os.system ( command )
+        
+for resFile in liste_end :
+    if not os.path.exists ( os.path.join (TmpDir, resFile) ) :
+        command =  f'cd {TmpDir} ; tar xf {file_restart_end} {resFile}'
+        echo ( command )
+        os.system ( command )
+
+echo ('\nOpening ATM SRF and ICO restart files')
+d_ATM_beg = xr.open_dataset ( os.path.join (TmpDir, file_ATM_beg), decode_times=False, decode_cf=True).squeeze()
+d_ATM_end = xr.open_dataset ( os.path.join (TmpDir, file_ATM_end), decode_times=False, decode_cf=True).squeeze()
+d_SRF_beg = xr.open_dataset ( os.path.join (TmpDir, file_SRF_beg), decode_times=False, decode_cf=True).squeeze()
+d_SRF_end = xr.open_dataset ( os.path.join (TmpDir, file_SRF_end), decode_times=False, decode_cf=True).squeeze()
+d_DYN_beg = xr.open_dataset ( os.path.join (TmpDir, file_DYN_beg), decode_times=False, decode_cf=True).squeeze()
+d_DYN_end = xr.open_dataset ( os.path.join (TmpDir, file_DYN_end), decode_times=False, decode_cf=True).squeeze()
+
+for var in d_SRF_beg.variables :
+    d_SRF_beg[var] = d_SRF_beg[var].where (  d_SRF_beg[var]<1.e20, 0.)
+    d_SRF_end[var] = d_SRF_end[var].where (  d_SRF_end[var]<1.e20, 0.)
+
+if ICO :
+    d_RUN_beg = xr.open_dataset ( os.path.join (TmpDir, file_RUN_beg), decode_times=False, decode_cf=True).squeeze()
+    d_RUN_end = xr.open_dataset ( os.path.join (TmpDir, file_RUN_end), decode_times=False, decode_cf=True).squeeze()
+
+echo ('\nExtract and rebuild OCE and ICE restarts')
+def get_ndomain (zfile) :
+    #d_zfile = xr.open_dataset (zfile, decode_times=False).squeeze()
+    #ndomain_opa = d_zfile.attrs['DOMAIN_number_total']
+    #d_zfile.close ()
+    ndomain_opa = subprocess.getoutput ( f'ls {zfile}_*.nc | wc -l' ).format()
+    return int (ndomain_opa)
+
+
+if not os.path.exists ( os.path.join (TmpDir, file_OCE_beg) ) :
+    if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart_0000.nc') ):
+        command =  f'cd {TmpDirOCE} ; tar xf {file_restart_beg}  OCE_{JobName}_{YearRes}1231_restart_*.nc'
+        echo ( command )
+        os.system ( command )
+    ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart') )
+    command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearRes}1231_restart {ndomain_opa} ; mv {file_OCE_beg} {TmpDir}'
+    echo ( command )
+    os.system ( command )
+    echo ( f'Rebuild done : {file_OCE_beg}')
+    
+if not os.path.exists ( os.path.join (TmpDir, file_OCE_end) ) :
+    if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart_0000.nc') ):
+        command =  f'cd {TmpDirOCE} ; tar xf {file_restart_end}  OCE_{JobName}_{YearEnd}1231_restart_*.nc'
+        echo ( command )
+        os.system ( command )
+    ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart') )
+    command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearEnd}1231_restart {ndomain_opa} ; mv {file_OCE_end} {TmpDir}'
+    echo ( command )
+    os.system ( command )
+    echo ( f'Rebuild done : {file_OCE_end}')
+
+if not os.path.exists ( os.path.join (TmpDir, file_ICE_beg) ) :
+    if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') ):
+        command =  f'cd {TmpDirICE} ; tar xf {file_restart_beg}  ICE_{JobName}_{YearRes}1231_restart_icemod_*.nc'
+        echo ( command )
+        os.system ( command )
+    ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') )
+    command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearRes}1231_restart_icemod {ndomain_opa} ; mv {file_ICE_beg} {TmpDir} '
+    echo ( command )
+    os.system ( command )
+    echo ( f'Rebuild done : {file_OCE_beg}')
+    
+if not os.path.exists ( os.path.join (TmpDir, file_ICE_end) ) :
+    if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearEnd}1231_restart_icemod') ):
+        command =  f'cd {TmpDirICE} ; tar xf {file_restart_end} ICE_{JobName}_{YearEnd}1231_restart_icemod_*.nc'
+        echo ( command )
+        os.system ( command )
+    ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') )
+    command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearEnd}1231_restart_icemod {ndomain_opa} ; mv {file_ICE_end} {TmpDir}'
+    echo ( command )
+    os.system ( command )
+    echo ( f'Rebuild done : {file_ICE_end}')
+
+echo ('Opening OCE and ICE restart files')
+if NEMO == 3.6 : 
+    d_OCE_beg = xr.open_dataset ( os.path.join (TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
+    d_OCE_end = xr.open_dataset ( os.path.join (TmpDir, file_OCE_end), decode_times=False, decode_cf=True).squeeze()
+    d_ICE_beg = xr.open_dataset ( os.path.join (TmpDir, file_ICE_beg), decode_times=False, decode_cf=True).squeeze()
+    d_ICE_end = xr.open_dataset ( os.path.join (TmpDir, file_ICE_end), decode_times=False, decode_cf=True).squeeze()
+if NEMO == 4.0 or NEMO == 4.2 : 
+    d_OCE_beg = xr.open_dataset ( os.path.join (TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
+    d_OCE_end = xr.open_dataset ( os.path.join (TmpDir, file_OCE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
+    d_ICE_beg = xr.open_dataset ( os.path.join (TmpDir, file_ICE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
+    d_ICE_end = xr.open_dataset ( os.path.join (TmpDir, file_ICE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
+    
+echo ( file_ATM_beg )
+echo ( file_ATM_end )
+echo ( file_DYN_beg )
+echo ( file_DYN_end )
+echo ( file_SRF_beg )
+echo ( file_SRF_end )
+if Routing == 'SIMPLE' :
+    echo ( file_RUN_beg )
+    echo ( file_RUN_end )
+echo ( file_OCE_beg )
+echo ( file_OCE_end )
+echo ( file_ICE_beg )
+echo ( file_ICE_end )
+
+def ATM_stock_int (stock) :
+    '''Integrate stock on atmosphere grid'''
+    ATM_stock_int  = np.sum (  np.sort ( (stock * DYN_aire).to_masked_array().ravel()) )
+    return ATM_stock_int
+
+def OCE_stock_int (stock) :
+    '''Integrate stock on ocean grid'''
+    OCE_stock_int  = np.sum (  np.sort ( (stock * OCE_aire).to_masked_array().ravel()) )
+    return OCE_stock_int
+
+def ONE_stock_int (stock) : 
+    '''Sum stock for field already integrated by cell'''
+    ONE_stock_int  = np.sum (  np.sort ( (stock).to_masked_array().ravel()) )
+    return ONE_stock_int
+
+# ATM grid with cell surfaces
+# ATM grid with cell surfaces
+if ICO :
+    jpja, jpia = d_ATM_his['aire'][0].shape   
+    file_ATM_aire = os.path.join ( R_IN, 'ATM', 'GRID', f'aire_{ATM}_to_{jpia}x{jpja}.nc' )
+    d_ATM_aire = xr.open_dataset ( file_ATM_aire, decode_times=False ).squeeze()
+    ATM_aire = lmdz.geo2point ( d_ATM_aire ['aire'].squeeze(), cumulPoles=True )
+    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
+    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0] + d_ATM_his ['fract_lic'][0] )
+    SRF_aire = lmdz.geo2point ( d_SRF_his ['Areas'] * d_SRF_his ['Contfrac'] )
+    
+if LMDZ :
+    ATM_aire = lmdz.geo2point ( d_ATM_his ['aire'][0], cumulPoles=True )
+    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
+    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0]  )
+    SRF_aire = lmdz.geo2point ( d_SRF_his ['Areas'] * d_SRF_his ['Contfrac'] )
+
+if ICO :
+    # Area on icosahedron grid
+    file_DYN_aire = os.path.join ( R_IN, 'ATM', 'GRID', ATM+'_grid.nc' )
+    d_DYN_aire = xr.open_dataset ( file_DYN_aire, decode_times=False).squeeze()
+    d_DYN_aire = d_DYN_aire.rename ( {'cell':'cell_mesh'} )
+    DYN_aire   = d_DYN_aire['aire']
+
+    DYN_fsea = d_DYN_aire ['fract_oce'] + d_DYN_aire ['fract_sic']
+    DYN_flnd = 1.0 - DYN_fsea
+    
+if LMDZ :
+    DYN_aire = ATM_aire
+    DYN_fsea = ATM_fsea
+    DYN_flnd = ATM_flnd
+
+#if LMDZ :
+#    d_ATM_beg = d_ATM_beg.assign ( coords={'lon':d_ATM_beg.lon*180./np.pi} )
+
+# Get mask and surfaces
+sos = d_OCE_his ['sos'][0].squeeze()
+OCE_msk = nemo.lbc_mask ( xr.where ( sos>0, 1., 0.0 ), cd_type = 'T' )
+so = sos = d_OCE_his ['sos'][0].squeeze()
+OCE_msk3 = nemo.lbc_mask ( xr.where ( so>0., 1., 0. ), cd_type = 'T', sval = 0. )
+
+# lbc_mask removes the duplicate points (periodicity and north fold)
+OCE_aire = nemo.lbc_mask ( d_OCE_his ['area'] * OCE_msk, cd_type = 'T', sval = 0.0 )
+ICE_aire = OCE_aire
+
+ATM_aire_tot = ONE_stock_int (ATM_aire)
+SRF_aire_tot = ONE_stock_int (SRF_aire)
+OCE_aire_tot = ONE_stock_int (OCE_aire)
+ICE_aire_tot = ONE_stock_int (ICE_aire)
+
+ATM_aire_sea     = ATM_aire * ATM_fsea
+ATM_aire_sea_tot = ONE_stock_int ( ATM_aire_sea )
+
+echo ( '\n------------------------------------------------------------------------------------' )
+echo ( '-- NEMO change in stores (for the records)' )
+#-- Note that the total number of days of the run should be diagnosed rather than assumed
+#-- Here the result is in Sv
+#
+#-- Change in ocean volume in freshwater equivalent
+
+OCE_ssh_beg = d_OCE_beg['sshn']
+OCE_ssh_end = d_OCE_end['sshn']
+OCE_sum_ssh_beg = OCE_stock_int ( OCE_ssh_beg )
+OCE_sum_ssh_end = OCE_stock_int ( OCE_ssh_end )
+
+OCE_mas_wat_beg = OCE_sum_ssh_beg * OCE_rho_liq
+OCE_mas_wat_end = OCE_sum_ssh_end * OCE_rho_liq
+
+echo ( 'OCE_sum_ssh_beg = {:12.6e} m^3 | OCE_sum_ssh_end = {:12.6e} m^3'.format (OCE_sum_ssh_beg, OCE_sum_ssh_end) )
+dOCE_vol_liq = ( OCE_sum_ssh_end - OCE_sum_ssh_beg )
+dOCE_mas_liq = dOCE_vol_liq * OCE_rho_liq
+dOCE_mas_wat = dOCE_mas_liq
+
+echo ( 'dOCE vol    = {:12.3e} m^3'.format (dOCE_vol_liq) )
+echo ( 'dOCE ssh    = {:12.3e} m  '.format (dOCE_vol_liq/OCE_aire_tot) )
+echo ( 'dOCE mass   = {:12.3e} kg '.format (dOCE_mas_liq) )
+echo ( 'dOCE mass   = {:12.3e} Sv '.format (dOCE_mas_liq/dtime_sec*1E-6/OCE_rho_liq) )
+
+## Glace et neige
+if NEMO == 3.6 :
+    ICE_ice_beg = d_ICE_beg['v_i_htc1']+d_ICE_beg['v_i_htc2']+d_ICE_beg['v_i_htc3']+d_ICE_beg['v_i_htc4']+d_ICE_beg['v_i_htc5']
+    ICE_ice_end = d_ICE_end['v_i_htc1']+d_ICE_end['v_i_htc2']+d_ICE_end['v_i_htc3']+d_ICE_end['v_i_htc4']+d_ICE_end['v_i_htc5']
+
+    ICE_sno_beg = d_ICE_beg['v_s_htc1']+d_ICE_beg['v_s_htc2']+d_ICE_beg['v_s_htc3']+d_ICE_beg['v_s_htc4']+d_ICE_beg['v_s_htc5']
+    ICE_sno_end = d_ICE_end['v_s_htc1']+d_ICE_end['v_s_htc2']+d_ICE_end['v_s_htc3']+d_ICE_end['v_s_htc4']+d_ICE_end['v_s_htc5']
+    
+    ICE_pnd_beg = 0.0 ; ICE_pnd_end = 0.0
+    ICE_fzl_beg = 0.0 ; ICE_fzl_end = 0.0
+
+    ICE_mas_wat_beg = OCE_stock_int ( (ICE_ice_beg*ICE_rho_ice + ICE_sno_beg*ICE_rho_sno) )
+    ICE_mas_wat_end = OCE_stock_int ( (ICE_ice_end*ICE_rho_ice + ICE_sno_end*ICE_rho_sno) )
+    
+if NEMO == 4.0 or NEMO == 4.2 :
+    ICE_ice_beg = d_ICE_beg ['v_i']  ; ICE_ice_end = d_ICE_end ['v_i']
+    ICE_sno_beg = d_ICE_beg ['v_s']  ; ICE_sno_end = d_ICE_end ['v_s']
+    ICE_pnd_beg = d_ICE_beg ['v_ip'] ; ICE_pnd_end = d_ICE_end ['v_ip']
+    ICE_fzl_beg = d_ICE_beg ['v_il'] ; ICE_fzl_end = d_ICE_end ['v_il']
+    
+    ICE_mas_wat_beg = OCE_stock_int ( d_ICE_beg['snwice_mass'] )
+    ICE_mas_wat_end = OCE_stock_int ( d_ICE_end['snwice_mass'] )
+    
+    
+ICE_vol_ice_beg = OCE_stock_int ( ICE_ice_beg )
+ICE_vol_ice_end = OCE_stock_int ( ICE_ice_end )
+
+ICE_vol_sno_beg = OCE_stock_int ( ICE_sno_beg )
+ICE_vol_sno_end = OCE_stock_int ( ICE_sno_end )
+
+ICE_vol_pnd_beg = OCE_stock_int ( ICE_pnd_beg )
+ICE_vol_pnd_end = OCE_stock_int ( ICE_pnd_end )
+
+ICE_vol_fzl_beg = OCE_stock_int ( ICE_fzl_beg )
+ICE_vol_fzl_end = OCE_stock_int ( ICE_fzl_end )
+
+#-- Converting to freswater volume
+dICE_vol_ice = ICE_vol_ice_end - ICE_vol_ice_beg
+dICE_mas_ice = dICE_vol_ice * ICE_rho_ice
+
+dICE_vol_sno = ICE_vol_sno_end - ICE_vol_sno_beg
+dICE_mas_sno = dICE_vol_sno * ICE_rho_sno
+
+dICE_vol_pnd = ICE_vol_pnd_end - ICE_vol_pnd_beg
+dICE_mas_pnd = dICE_vol_pnd * ICE_rho_pnd
+
+dICE_vol_fzl= ICE_vol_fzl_end - ICE_vol_fzl_beg
+dICE_mas_fzl = dICE_vol_fzl * ICE_rho_pnd
+
+if NEMO == 3.6 :
+    dICE_mas_wat = dICE_mas_ice + dICE_mas_sno
+    dSEA_mas_wat = dOCE_mas_wat + dICE_mas_ice + dICE_mas_sno
+
+if NEMO == 4.0 or NEMO == 4.2 :
+    dICE_mas_wat = ICE_mas_wat_end - ICE_mas_wat_beg
+    dSEA_mas_wat = dOCE_mas_wat + dICE_mas_wat
+
+echo ( 'ICE_vol_ice_beg = {:12.6e} m^3 | ICE_vol_ice_end = {:12.6e} m^3'.format (ICE_vol_ice_beg, ICE_vol_ice_end) )
+echo ( 'ICE_vol_sno_beg = {:12.6e} m^3 | ICE_vol_sno_end = {:12.6e} m^3'.format (ICE_vol_sno_beg, ICE_vol_sno_end) )
+echo ( 'ICE_vol_pnd_beg = {:12.6e} m^3 | ICE_vol_pnd_end = {:12.6e} m^3'.format (ICE_vol_pnd_beg, ICE_vol_pnd_end) )
+echo ( 'ICE_vol_fzl_beg = {:12.6e} m^3 | ICE_vol_fzl_end = {:12.6e} m^3'.format (ICE_vol_fzl_beg, ICE_vol_fzl_end) )
+
+echo ( 'dICE_vol_ice   = {:12.3e} m^3'.format (dICE_vol_ice) )
+echo ( 'dICE_vol_sno   = {:12.3e} m^3'.format (dICE_vol_sno) )
+echo ( 'dICE_vol_pnd   = {:12.3e} m^3'.format (dICE_vol_pnd) )
+echo ( 'dICE_mas_ice   = {:12.3e} m^3'.format (dICE_mas_ice) )
+echo ( 'dICE_mas_sno   = {:12.3e} m^3'.format (dICE_mas_sno) )  
+echo ( 'dICE_mas_pnd   = {:12.3e} m^3'.format (dICE_mas_pnd) )  
+echo ( 'dICE_mas_fzl   = {:12.3e} m^3'.format (dICE_mas_fzl) )  
+echo ( 'dICE_mas_wat   = {:12.3e} m^3'.format (dICE_mas_wat) ) 
+
+
+SEA_mas_wat_beg = OCE_mas_wat_beg + ICE_mas_wat_beg
+SEA_mas_wat_end = OCE_mas_wat_end + ICE_mas_wat_end
+
+echo ( '\n------------------------------------------------------------')
+echo ( 'Variation du contenu en eau ocean + glace ' )
+echo ( 'dMass (ocean)   = {:12.6e} kg '.format(dSEA_mas_wat) )
+echo ( 'dVol  (ocean)   = {:12.3e} Sv '.format(dSEA_mas_wat/dtime_sec*1E-6/OCE_rho_liq) )
+echo ( 'dVol  (ocean)   = {:12.3e} m  '.format(dSEA_mas_wat*1E-3/OCE_aire_tot) )
+
+
+echo ( '\n------------------------------------------------------------------------------------' )
+echo ( '-- ATM changes in stores ' )
+
+#-- Change in precipitable water from the atmosphere daily and monthly files
+#-- Compute sum weighted by gridcell area (kg/m2) then convert to Sv
+
+# ATM vertical grid
+ATM_Ahyb = d_ATM_his['Ahyb'].squeeze()
+ATM_Bhyb = d_ATM_his['Bhyb'].squeeze()
+klevp1 = ATM_Ahyb.shape[0]
+
+# Surface pressure
+if ICO : 
+    DYN_ps_beg = d_DYN_beg['ps']
+    DYN_ps_end = d_DYN_end['ps']
+    
+if LMDZ : 
+    DYN_ps_beg =  lmdz.geo2point ( d_DYN_beg['ps'].isel(rlonv=slice(0,-1)) )
+    DYN_ps_end =  lmdz.geo2point ( d_DYN_end['ps'].isel(rlonv=slice(0,-1)) )
+    
+# 3D Pressure
+DYN_p_beg = ATM_Ahyb + ATM_Bhyb * DYN_ps_beg
+DYN_p_end = ATM_Ahyb + ATM_Bhyb * DYN_ps_end
+
+# Layer thickness
+DYN_sigma_beg = DYN_p_beg[0:-1]*0.
+DYN_sigma_end = DYN_p_end[0:-1]*0. 
+
+for k in np.arange (klevp1-1) :
+    DYN_sigma_beg[k,:] = (DYN_p_beg[k,:] - DYN_p_beg[k+1,:]) / grav
+    DYN_sigma_end[k,:] = (DYN_p_end[k,:] - DYN_p_end[k+1,:]) / grav
+
+DYN_sigma_beg = DYN_sigma_beg.rename ( {'klevp1':'sigs'} )
+DYN_sigma_end = DYN_sigma_end.rename ( {'klevp1':'sigs'} )
+
+##-- Vertical and horizontal integral, and sum of liquid, solid and vapor water phases
+if LMDZ :
+    try : 
+        DYN_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2Ov'] + d_DYN_beg['H2Ol'] + d_DYN_beg['H2Oi']).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_end = lmdz.geo3point ( (d_DYN_end['H2Ov'] + d_DYN_end['H2Ol'] + d_DYN_end['H2Oi']).isel(rlonv=slice(0,-1) ) )
+    except :
+        DYN_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).isel(rlonv=slice(0,-1) ) )
+        DYN_wat_end = lmdz.geo3point ( (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).isel(rlonv=slice(0,-1) ) )
+if ICO :
+    try :
+        DYN_wat_beg = (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).rename ( {'lev':'sigs'} )
+        DYN_wat_end = (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).rename ( {'lev':'sigs'} )
+    except : 
+        DYN_wat_beg = (d_DYN_beg['q'].isel(nq=0) + d_DYN_beg['q'].isel(nq=1) + d_DYN_beg['q'].isel(nq=2) ).rename ( {'lev':'sigs'} )
+        DYN_wat_end = (d_DYN_end['q'].isel(nq=0) + d_DYN_end['q'].isel(nq=1) + d_DYN_end['q'].isel(nq=2) ).rename ( {'lev':'sigs'} )
+    
+DYN_mas_wat_beg = ATM_stock_int (DYN_sigma_beg * DYN_wat_beg)
+DYN_mas_wat_end = ATM_stock_int (DYN_sigma_end * DYN_wat_end)
+
+dDYN_mas_wat = DYN_mas_wat_end - DYN_mas_wat_beg
+
+echo ( '\nVariation du contenu en eau atmosphere (dynamique) ' )
+echo ( 'DYN_mas_beg = {:12.6e} kg | DYN_mas_end = {:12.6e} kg'.format (DYN_mas_wat_beg, DYN_mas_wat_end) )
+echo ( 'dMass (atm)   = {:12.3e} kg '.format (dDYN_mas_wat) )
+echo ( 'dMass (atm)   = {:12.3e} Sv '.format (dDYN_mas_wat/dtime_sec*1.e-6/ATM_rho) )
+echo ( 'dMass (atm)   = {:12.3e} m  '.format (dDYN_mas_wat/ATM_aire_sea_tot/ATM_rho) )
+
+ATM_sno_beg = d_ATM_beg['SNOW01']*d_ATM_beg['FTER']+d_ATM_beg['SNOW02']*d_ATM_beg['FLIC']+d_ATM_beg['SNOW03']*d_ATM_beg['FOCE']+d_ATM_beg['SNOW04']*d_ATM_beg['FSIC']
+ATM_sno_end = d_ATM_end['SNOW01']*d_ATM_end['FTER']+d_ATM_end['SNOW02']*d_ATM_end['FLIC']+d_ATM_end['SNOW03']*d_ATM_end['FOCE']+d_ATM_end['SNOW04']*d_ATM_end['FSIC']
+
+ATM_qs_beg  = d_ATM_beg['QS01']*d_ATM_beg['FTER']+d_ATM_beg['QS02']*d_ATM_beg['FLIC']+d_ATM_beg['QS03']*d_ATM_beg['FOCE']+d_ATM_beg['QS04']*d_ATM_beg['FSIC']
+ATM_qs_end  = d_ATM_end['QS01']*d_ATM_end['FTER']+d_ATM_end['QS02']*d_ATM_end['FLIC']+d_ATM_end['QS03']*d_ATM_end['FOCE']+d_ATM_end['QS04']*d_ATM_end['FSIC']
+
+ATM_qsol_beg = d_ATM_beg['QSOL']
+ATM_qsol_end = d_ATM_end['QSOL']
+
+ATM_qs01_beg  = d_ATM_beg['QS01'] * d_ATM_beg['FTER']
+ATM_qs01_end  = d_ATM_end['QS01'] * d_ATM_end['FTER']
+ATM_qs02_beg  = d_ATM_beg['QS02'] * d_ATM_beg['FLIC']
+ATM_qs02_end  = d_ATM_end['QS02'] * d_ATM_end['FLIC']
+ATM_qs03_beg  = d_ATM_beg['QS03'] * d_ATM_beg['FOCE']
+ATM_qs03_end  = d_ATM_end['QS03'] * d_ATM_end['FOCE']
+ATM_qs04_beg  = d_ATM_beg['QS04'] * d_ATM_beg['FSIC']
+ATM_qs04_end  = d_ATM_end['QS04'] * d_ATM_end['FSIC']  
+
+if ICO :
+   ATM_sno_beg   = ATM_sno_beg .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_sno_end   = ATM_sno_end .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs_beg    = ATM_qs_beg  .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs_end    = ATM_qs_end  .rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qsol_beg  = ATM_qsol_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qsol_end  = ATM_qsol_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs01_beg  = ATM_qs01_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs01_end  = ATM_qs01_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs02_beg  = ATM_qs02_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs02_end  = ATM_qs02_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs03_beg  = ATM_qs03_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs03_end  = ATM_qs03_end.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs04_beg  = ATM_qs04_beg.rename ( {'points_physiques':'cell_mesh'} )
+   ATM_qs04_end  = ATM_qs04_end.rename ( {'points_physiques':'cell_mesh'} ) 
+
+echo ('Computing atmosphere integrals')
+ATM_mas_sno_beg   = ATM_stock_int ( ATM_sno_beg  )
+ATM_mas_sno_end   = ATM_stock_int ( ATM_sno_end  )
+ATM_mas_qs_beg    = ATM_stock_int ( ATM_qs_beg   )
+ATM_mas_qs_end    = ATM_stock_int ( ATM_qs_end   )
+ATM_mas_qsol_beg  = ATM_stock_int ( ATM_qsol_beg )
+ATM_mas_qsol_end  = ATM_stock_int ( ATM_qsol_end )
+ATM_mas_qs01_beg  = ATM_stock_int ( ATM_qs01_beg )
+ATM_mas_qs01_end  = ATM_stock_int ( ATM_qs01_end )
+ATM_mas_qs02_beg  = ATM_stock_int ( ATM_qs02_beg )
+ATM_mas_qs02_end  = ATM_stock_int ( ATM_qs02_end )
+ATM_mas_qs03_beg  = ATM_stock_int ( ATM_qs03_beg )
+ATM_mas_qs03_end  = ATM_stock_int ( ATM_qs03_end )
+ATM_mas_qs04_beg  = ATM_stock_int ( ATM_qs04_beg )
+ATM_mas_qs04_end  = ATM_stock_int ( ATM_qs04_end )
+
+dATM_mas_sno  = ATM_mas_sno_end  - ATM_mas_sno_beg
+dATM_mas_qs   = ATM_mas_qs_end   - ATM_mas_qs_beg
+dATM_mas_qsol = ATM_mas_qsol_end - ATM_mas_qsol_beg
+
+dATM_mas_qs01 = ATM_mas_qs01_end - ATM_mas_qs01_beg
+dATM_mas_qs02 = ATM_mas_qs02_end - ATM_mas_qs02_beg
+dATM_mas_qs03 = ATM_mas_qs03_end - ATM_mas_qs03_beg
+dATM_mas_qs04 = ATM_mas_qs04_end - ATM_mas_qs04_beg
+
+echo ( '\nVariation du contenu en neige atmosphere (calottes)' )
+echo ( 'ATM_mas_sno_beg  = {:12.6e} kg | ATM_mas_sno_end  = {:12.6e} kg'.format (ATM_mas_sno_beg, ATM_mas_sno_end) )
+echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_sno ) )
+echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_sno/dtime_sec*1e-6/ICE_rho_ice) )
+echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_sno/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\nVariation du contenu humidite du sol' )
+echo ( 'ATM_mas_qs_beg  = {:12.6e} kg | ATM_mas_qs_end  = {:12.6e} kg'.format (ATM_mas_qs_beg, ATM_mas_qs_end) )
+echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_qs ) )
+echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_qs/dtime_sec*1e-6/ATM_rho) )
+echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_qs/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\nVariation du contenu en eau+neige atmosphere ' )
+echo ( 'dMass (eau + neige atm) = {:12.3e} kg '.format (  dDYN_mas_wat + dATM_mas_sno) )
+echo ( 'dMass (eau + neige atm) = {:12.3e} Sv '.format ( (dDYN_mas_wat + dATM_mas_sno)/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (eau + neige atm) = {:12.3e} m  '.format ( (dDYN_mas_wat + dATM_mas_sno)/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\n------------------------------------------------------------------------------------' )
+echo ( '-- SRF changes ' )
+
+if Routing == 'SIMPLE' :
+    RUN_mas_wat_beg = ONE_stock_int ( d_RUN_beg ['fast_reservoir'] +  d_RUN_beg ['slow_reservoir'] +  d_RUN_beg ['stream_reservoir'])
+    RUN_mas_wat_end = ONE_stock_int ( d_RUN_end ['fast_reservoir'] +  d_RUN_end ['slow_reservoir'] +  d_RUN_end ['stream_reservoir'])
+
+if Routing == 'ORCHIDEE' : 
+    RUN_mas_wat_beg = ONE_stock_int ( d_SRF_beg['fastres']  + d_SRF_beg['slowres'] + d_SRF_beg['streamres'] \
+                                    + d_SRF_beg['floodres'] + d_SRF_beg['lakeres'] + d_SRF_beg['pondres'] )
+    RUN_mas_wat_end = ONE_stock_int ( d_SRF_end['fastres']  + d_SRF_end['slowres'] + d_SRF_end['streamres'] \
+                                    + d_SRF_end['floodres'] + d_SRF_end['lakeres'] + d_SRF_end['pondres'] )
+
+dRUN_mas_wat = RUN_mas_wat_end - RUN_mas_wat_beg
+    
+echo ( '\nWater content in routing ' )
+echo ( 'RUN_mas_wat_beg = {:12.6e} kg | RUN_mas_wat_end = {:12.6e} kg '.format (RUN_mas_wat_end, RUN_mas_wat_end) )
+echo ( 'dMass (routing)  = {:12.3e} kg '.format(dRUN_mas_wat) )
+echo ( 'dMass (routing)  = {:12.3e} Sv '.format(dRUN_mas_wat/dtime_sec*1E-9) )
+echo ( 'dMass (routing)  = {:12.3e} m  '.format(dRUN_mas_wat/OCE_aire_tot*1E-3) )
+
+print ('Reading SRF restart')
+tot_watveg_beg  = d_SRF_beg['tot_watveg_beg']  ; tot_watveg_beg  = tot_watveg_beg .where (tot_watveg_beg < 1E10, 0.)
+tot_watsoil_beg = d_SRF_beg['tot_watsoil_beg'] ; tot_watsoil_beg = tot_watsoil_beg.where (tot_watsoil_beg< 1E10, 0.)
+snow_beg        = d_SRF_beg['snow_beg']        ; snow_beg        = snow_beg       .where (snow_beg       < 1E10, 0.)
+
+tot_watveg_end  = d_SRF_end['tot_watveg_beg']  ; tot_watveg_end  = tot_watveg_end .where (tot_watveg_end < 1E10, 0.)
+tot_watsoil_end = d_SRF_end['tot_watsoil_beg'] ; tot_watsoil_end = tot_watsoil_end.where (tot_watsoil_end< 1E10, 0.)
+snow_end        = d_SRF_end['snow_beg']        ; snow_end        = snow_end       .where (snow_end       < 1E10, 0.)
+
+if LMDZ :
+    tot_watveg_beg  = lmdz.geo2point (tot_watveg_beg)
+    tot_watsoil_beg = lmdz.geo2point (tot_watsoil_beg)
+    snow_beg        = lmdz.geo2point (snow_beg)
+    tot_watveg_end  = lmdz.geo2point (tot_watveg_end)
+    tot_watsoil_end = lmdz.geo2point (tot_watsoil_end)
+    snow_end        = lmdz.geo2point (snow_end)
+
+SRF_wat_beg = tot_watveg_beg + tot_watsoil_beg + snow_beg
+SRF_wat_end = tot_watveg_end + tot_watsoil_end + snow_end
+
+#SRF_mas_wat_beg = d_SRF_beg['tot_watveg_beg']+d_SRF_beg['tot_watsoil_beg'] + d_SRF_beg['snow_beg']
+#SRF_mas_wat_end = d_SRF_end['tot_watveg_beg']+d_SRF_end['tot_watsoil_beg'] + d_SRF_end['snow_beg']
+
+#if LMDZ :
+#    tot_watveg_beg  = tot_watveg_beg .rename ( {'y':'points_phyiques'} )
+#    tot_watsoil_beg = tot_watsoil_beg.rename ( {'y':'points_phyiques'} )
+#    snow_beg        = snow_beg       .rename ( {'y':'points_phyiques'} )
+#    tot_watveg_end  = tot_watveg_end .rename ( {'y':'points_phyiques'} )
+#    tot_watsoil_end = tot_watsoil_end.rename ( {'y':'points_phyiques'} )
+#    snow_end        = snow_end       .rename ( {'y':'points_phyiques'} )
+#    SRF_wat_beg     = SRF_wat_beg    .rename ( {'y':'points_phyiques'} )
+#    SRF_wat_end     = SRF_wat_end    .rename ( {'y':'points_phyiques'} )
+if ICO :
+    tot_watveg_beg  = tot_watveg_beg .rename ( {'y':'cell_mesh'} )
+    tot_watsoil_beg = tot_watsoil_beg.rename ( {'y':'cell_mesh'} )
+    snow_beg        = snow_beg       .rename ( {'y':'cell_mesh'} )
+    tot_watveg_end  = tot_watveg_end .rename ( {'y':'cell_mesh'} )
+    tot_watsoil_end = tot_watsoil_end.rename ( {'y':'cell_mesh'} )
+    snow_end        = snow_end       .rename ( {'y':'cell_mesh'} )
+    SRF_wat_beg     = SRF_wat_beg    .rename ( {'y':'cell_mesh'} )
+    SRF_wat_end     = SRF_wat_end    .rename ( {'y':'cell_mesh'} )  
+
+print ('Computing integrals')
+
+print ( ' 1/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watveg_beg   = ATM_stock_int ( tot_watveg_beg  )
+print ( ' 2/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watsoil_beg  = ATM_stock_int ( tot_watsoil_beg )
+print ( ' 3/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_snow_beg     = ATM_stock_int ( snow_beg        )
+print ( ' 4/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watveg_end   = ATM_stock_int ( tot_watveg_end  )
+print ( ' 5/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_watsoil_end  = ATM_stock_int ( tot_watsoil_end )
+print ( ' 6/6', end='' ) ; sys.stdout.flush ()
+SRF_mas_snow_end     = ATM_stock_int ( snow_end        )
+print (' -- ') ; sys.stdout.flush ()
+
+dSRF_mas_watveg   = SRF_mas_watveg_end   - SRF_mas_watveg_beg
+dSRF_mas_watsoil  = SRF_mas_watsoil_end  - SRF_mas_watsoil_beg
+dSRF_mas_snow     = SRF_mas_snow_end     - SRF_mas_snow_beg
+
+echo ('\nLes differents reservoirs')
+echo ( 'SRF_mas_watveg_beg   = {:12.6e} kg | SRF_mas_watveg_end   = {:12.6e} kg '.format (SRF_mas_watveg_beg , SRF_mas_watveg_end) )
+echo ( 'SRF_mas_watsoil_beg  = {:12.6e} kg | SRF_mas_watsoil_end  = {:12.6e} kg '.format (SRF_mas_watsoil_beg, SRF_mas_watsoil_end) )
+echo ( 'SRF_mas_snow_beg     = {:12.6e} kg | SRF_mas_snow_end     = {:12.6e} kg '.format (SRF_mas_snow_beg   , SRF_mas_snow_end) )
+
+echo ( 'dMass (watveg)  = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_watveg , dSRF_mas_watveg /dtime_sec*1E-9, dSRF_mas_watveg /OCE_aire_tot*1E-3) )
+echo ( 'dMass (watsoil) = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_watsoil, dSRF_mas_watsoil/dtime_sec*1E-9, dSRF_mas_watsoil/OCE_aire_tot*1E-3) )
+echo ( 'dMass (sno)     = {:12.3e} kg | {:12.2e} Sv | {:12.2e} m '.format (dSRF_mas_snow   , dSRF_mas_snow   /dtime_sec*1E-9, dSRF_mas_snow   /OCE_aire_tot*1E-3  ) )
+
+SRF_mas_wat_beg = SRF_mas_watveg_beg + SRF_mas_watsoil_beg + SRF_mas_snow_beg 
+SRF_mas_wat_end = SRF_mas_watveg_end + SRF_mas_watsoil_end + SRF_mas_snow_end
+dSRF_mas_wat = SRF_mas_wat_end - SRF_mas_wat_beg
+
+echo ( '\nWater content in surface ' )
+echo ( 'SRF_mas_wat_beg  = {:12.6e} kg | SRF_mas_wat_end  = {:12.6e} kg '.format (SRF_mas_wat_beg, SRF_mas_wat_end) )
+echo ( 'dMass (water srf) = {:12.3e} kg '.format (dSRF_mas_wat) )
+echo ( 'dMass (water srf) = {:12.3e} Sv '.format (dSRF_mas_wat/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (water srf) = {:12.3e} m  '.format (dSRF_mas_wat/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\nWater content in  ATM + SRF + RUN ' )
+echo ( 'mas_wat_beg = {:12.6e} kg | mas_wat_end = {:12.6e} kg '.
+           format (DYN_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg,
+                   DYN_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
+echo ( 'dMass (water atm+srf+run) = {:12.6e} kg '.format ( dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat) )
+echo ( 'dMass (water atm+srf+run) = {:12.3e} Sv '.format ((dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/dtime_sec*1E-6/ATM_rho) )
+echo ( 'dMass (water atm+srf+run) = {:12.3e} m  '.format ((dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/ATM_aire_sea_tot/ATM_rho) )
+
+echo ( '\n------------------------------------------------------------------------------------' )
+echo ( '-- Change in all components' )
+echo ( 'mas_wat_beg = {:12.6e} kg | mas_wat_end = {:12.6e} kg'.
+           format (SEA_mas_wat_beg + DYN_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg,
+                   SEA_mas_wat_end + DYN_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
+echo ( 'dMass (water CPL)         = {:12.3e} kg '.format ( dSEA_mas_wat + dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat) )
+echo ( 'dMass (water CPL)         = {:12.3e} Sv '.format ((dSEA_mas_wat + dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/dtime_sec*1E-9) )
+echo ( 'dMass (water CPL)         = {:12.3e} m  '.format ((dSEA_mas_wat + dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat)/OCE_aire_tot*1E-3) )
+
+
Index: TOOLS/WATER_BUDGET/OCE_waterbudget.py
===================================================================
--- TOOLS/WATER_BUDGET/OCE_waterbudget.py	(revision 6265)
+++ TOOLS/WATER_BUDGET/OCE_waterbudget.py	(revision 6271)
@@ -19,41 +19,17 @@
 #  $HeadURL$
 
-
-## Define Experiment
-## -----------------
-
-if False : 
-    JobName="TEST-CM72-SIMPLE-ROUTING.12" ; TagName="IPSLCM7" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1970 ; YearEnd = 1979 ; YearInc = 10 ; PackFrequency=10
-    #Freq = 'MO' ; YearBegin = 1852 ; YearEnd = 1852 ; PackFrequency = 1
-    ORCA = 'eORCA1.2' ; NEMO=3.6 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-
-if False : 
-    JobName="TEST-CM72-SIMPLE-ROUTING.10" ; TagName="IPSLCM7" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1860 ; YearEnd = 1869 ; PackFrequency = 10
-    ORCA = 'eORCA1.2' ; NEMO=3.6 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-
-if False : 
-    JobName="VALID-CM622-LR.01" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1890 ; YearEnd = 1899 ; PackFrequency = 10
-    ORCA = 'eORCA1.2' ; NEMO=3.6 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-
-if False : 
-    JobName="VALID-CM622-SIMPLE-ROUTING" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1898 ; YearEnd = 1898 ; PackFrequency = 1
-    ORCA = 'eORCA1.2' ; NEMO=3.6 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-    
-if False : 
-    JobName="VALID-CM622-NORESTART" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p86caub" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1890 ; YearEnd = 1899 ; PackFrequency = 10
-    ORCA = 'eORCA1.2' ; NEMO=3.6 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-
-if True :
-    JobName="CM65v420-LR-SKL-pi-05" ; TagName="IPSLCM6" ; SpaceName="DEVT" ; ExperimentName="piControl" ; User="p48ethe" ; Project="gencmip6" 
-    Freq = 'MO' ; YearBegin = 1890 ; YearEnd = 1899 ; PackFrequency = 10
-    ORCA = 'eORCA1.2' ; NEMO=4.2 ; OCE_relax = False ; OCE_icb = False ; Coupled = True
-    #/ccc/store/cont003/gencmip6/p48ethe/IGCM_OUT/IPSLCM6/DEVT/piControl/CM65v420-LR-SKL-pi-05
-
+###
+## Import system modules
+import sys, os, shutil, subprocess
 import numpy as np
+import configparser, re
+
+## Creates parser
+config = configparser.ConfigParser()
+config.optionxform = str # To keep capitals
+
+config['Files']  = {}
+config['System'] = {}
+
 ##-- Some physical constants
 #-- Earth Radius
@@ -70,13 +46,44 @@
 ICE_rho_pnd = 1000.
 
-##
-## Import system modules
-import sys, os, shutil, subprocess
+## Read experiment parameters
+ATM = None ; ORCA = None ; NEMO = None ; OCE_relax = False ; OCE_icb = False ; Coupled = False ; Routing = None
+
+# Arguments passed
+print ( "Name of Python script:", sys.argv[0] )
+IniFile =  sys.argv[1]
+print ("Input file : ", IniFile )
+config.read (IniFile)
+
+def setBool (chars) :
+    '''Convert specific char string in boolean if possible'''
+    setBool = chars
+    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]+)?$")
+        isReal = realnum.match(chars.strip()) != None
+        isInt  = chars.strip().isdigit()
+        if isReal :
+            if isInt : setNum = int   (chars)
+            else     : setNum = float (chars)
+        else : setNum = chars
+    else : setNum = chars
+    return setNum
+
+print ('[Experiment]')
+for VarName in config['Experiment'].keys() :
+    locals()[VarName] = config['Experiment'][VarName]
+    locals()[VarName] = setBool (locals()[VarName])
+    locals()[VarName] = setNum  (locals()[VarName])
+    print ( '{:25} set to : {:}'.format (VarName, locals()[VarName]) )
 
 # Where do we run ?
-TGCC = False ; IDRIS = False
-SysName, NodeName, Release, Version, Machine = os.uname ()
-if 'irene'   in NodeName : TGCC  = True
-if 'jeanzay' in NodeName : IDRIS = True
+SysName, NodeName, Release, Version, Machine = os.uname()
+TGCC  = ( 'irene'   in NodeName )
+IDRIS = ( 'jeanzay' in NodeName )
 
 ## Set site specific libIGCM directories, and other specific stuff
@@ -154,11 +161,11 @@
 
 #-- Files Names
-if Freq == 'MO' : File = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
-if Freq == 'SE' : File = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
+if Freq == 'MO' : FileCommon = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
+if Freq == 'SE' : FileCommon = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
 
 echo ('\nOpen history files' )
-file_OCE_his = os.path.join ( dir_OCE_his, f'{File}_grid_T.nc' )
-file_OCE_sca = os.path.join ( dir_OCE_his, f'{File}_scalar.nc' )
-file_ICE_his = os.path.join ( dir_ICE_his, f'{File}_icemod.nc' )
+file_OCE_his = os.path.join ( dir_OCE_his, f'{FileCommon}_grid_T.nc' )
+file_OCE_sca = os.path.join ( dir_OCE_his, f'{FileCommon}_scalar.nc' )
+file_ICE_his = os.path.join ( dir_ICE_his, f'{FileCommon}_icemod.nc' )
 
 d_OCE_his = xr.open_dataset ( file_OCE_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
@@ -177,5 +184,5 @@
 
 ## Compute length of each period
-dtime_per = (d_OCE_his.time_counter_bounds[:,-1] -  d_OCE_his.time_counter_bounds[:,0] )
+dtime_per = ( d_OCE_his.time_counter_bounds[:,-1] - d_OCE_his.time_counter_bounds[:,0] )
 echo ('\nPeriods lengths (days) : ')
 echo (' {:}'.format ( (dtime_per/np.timedelta64(1, "D")).values ) )
@@ -220,5 +227,4 @@
         echo ( command )
         os.system ( command )
-    print ('1.2')
     echo ('extract ndomain' )
     ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart') )
@@ -287,8 +293,22 @@
 ICE_aire = OCE_aire
 
-OCE_aire_tot = np.sum (OCE_aire).values.item()
-ICE_aire_tot = np.sum (ICE_aire).values.item()
-
-
+def OCE_stock_int (stock) :
+    '''Integrate stock on ocean grid'''
+    OCE_stock_int = np.sum (  np.sort ( (stock * OCE_aire ).to_masked_array().ravel()) )
+    return OCE_stock_int
+
+def ONE_stock_int (stock) :
+    '''Sum stock'''
+    ONE_stock_int = np.sum (  np.sort ( (stock * OCE_aire).to_masked_array().ravel()) )
+    return ONE_stock_int
+
+def OCE_flux_int (flux) :
+    '''Integrate flux on oce grid'''
+    OCE_flux_int = np.sum (  np.sort ( (flux * OCE_aire * dtime_per_sec).to_masked_array().ravel()) )
+    return OCE_flux_int
+
+OCE_aire_tot = ONE_stock_int ( OCE_aire )
+ICE_aire_tot = ONE_stock_int ( ICE_aire )
+    
 echo ( '\n------------------------------------------------------------------------------------' )
 echo ( '-- NEMO change in stores (for the records)' )
@@ -300,12 +320,13 @@
 OCE_ssh_beg = d_OCE_beg['sshn']
 OCE_ssh_end = d_OCE_end['sshn']
-OCE_sum_ssh_beg = np.sum ( OCE_ssh_beg * OCE_aire ).values.item()
-OCE_sum_ssh_end = np.sum ( OCE_ssh_end * OCE_aire ).values.item()
+
+OCE_sum_ssh_beg = OCE_stock_int ( OCE_ssh_beg )
+OCE_sum_ssh_end = OCE_stock_int ( OCE_ssh_end )
 
 if NEMO == 3.6 :
     OCE_e3tn_beg = d_OCE_beg['fse3t_n']
     OCE_e3tn_end = d_OCE_end['fse3t_n']
-    OCE_sum_e3tn_beg = np.sum ( OCE_e3tn_beg * OCE_aire * OCE_msk3).values.item()
-    OCE_sum_e3tn_end = np.sum ( OCE_e3tn_end * OCE_aire * OCE_msk3).values.item()
+    OCE_sum_e3tn_beg = OCE_stock_int ( OCE_e3tn_beg * OCE_msk3)
+    OCE_sum_e3tn_end = OCE_stock_int ( OCE_e3tn_end * OCE_msk3)
 
 echo ( 'OCE_sum_ssh_beg = {:12.6e} m^3  - OCE_sum_ssh_end = {:12.6e} m^3'.format (OCE_sum_ssh_beg, OCE_sum_ssh_end) )
@@ -329,5 +350,4 @@
     echo ( 'dOCE e3tn   mass   = {:12.3e} kg '.format ( dOCE_e3tn_mas) )
 
-
 ## Glace et neige
 if NEMO == 3.6 :
@@ -348,31 +368,23 @@
     
 if NEMO == 4.0 or NEMO == 4.2 :
-    ICE_ice_beg = d_ICE_beg['v_i']
-    ICE_ice_end = d_ICE_end['v_i']
-
-    ICE_sno_beg = d_ICE_beg['v_s']
-    ICE_sno_end = d_ICE_end['v_s']
-
-    ICE_pnd_beg = d_ICE_beg['v_ip'] # Ice ponds
-    ICE_pnd_end = d_ICE_end['v_ip']
-
-    ICE_fzl_beg = d_ICE_beg['v_il'] # Frozen Ice Ponds
-    ICE_fzl_end = d_ICE_end['v_il']
-    
-    ICE_mas_wat_beg =  np.sum ( d_ICE_beg['snwice_mass']*ICE_aire ).values.item()
-    ICE_mas_wat_end =  np.sum ( d_ICE_end['snwice_mass']*ICE_aire ).values.item()
-    
-    
-ICE_vol_ice_beg = np.sum ( ICE_ice_beg*ICE_aire ).values.item()
-ICE_vol_ice_end = np.sum ( ICE_ice_end*ICE_aire ).values.item()
-
-ICE_vol_sno_beg = np.sum ( ICE_sno_beg*ICE_aire ).values.item()
-ICE_vol_sno_end = np.sum ( ICE_sno_end*ICE_aire ).values.item()
-
-ICE_vol_pnd_beg = np.sum ( ICE_pnd_beg*ICE_aire ).values.item()
-ICE_vol_pnd_end = np.sum ( ICE_pnd_end*ICE_aire ).values.item()
-
-ICE_vol_fzl_beg = np.sum ( ICE_fzl_beg*ICE_aire ).values.item()
-ICE_vol_fzl_end = np.sum ( ICE_fzl_end*ICE_aire ).values.item()
+    ICE_ice_beg = d_ICE_beg['v_i']  ; ICE_ice_end = d_ICE_end['v_i']
+    ICE_sno_beg = d_ICE_beg['v_s']  ; ICE_sno_end = d_ICE_end['v_s'] 
+    ICE_pnd_beg = d_ICE_beg['v_ip'] ; ICE_pnd_end = d_ICE_end['v_ip'] # Ice ponds
+    ICE_fzl_beg = d_ICE_beg['v_il'] ; ICE_fzl_end = d_ICE_end['v_il'] # Frozen Ice Ponds
+    
+    ICE_mas_wat_beg =  OCE_stock_int ( d_ICE_beg['snwice_mass'] )
+    ICE_mas_wat_end =  OCE_stock_int ( d_ICE_end['snwice_mass'] )
+    
+ICE_vol_ice_beg = OCE_stock_int ( ICE_ice_beg )
+ICE_vol_ice_end = OCE_stock_int ( ICE_ice_end )
+
+ICE_vol_sno_beg = OCE_stock_int ( ICE_sno_beg )
+ICE_vol_sno_end = OCE_stock_int ( ICE_sno_end )
+
+ICE_vol_pnd_beg = OCE_stock_int ( ICE_pnd_beg )
+ICE_vol_pnd_end = OCE_stock_int ( ICE_pnd_end )
+
+ICE_vol_fzl_beg = OCE_stock_int ( ICE_fzl_beg )
+ICE_vol_fzl_end = OCE_stock_int ( ICE_fzl_end )
 
 #-- Converting to freswater volume
@@ -397,8 +409,8 @@
     dSEA_mas_wat = dOCE_mas_wat + dICE_mas_wat
 
-echo ( 'ICE_vol_ice_beg = {:12.6e} m^3 - ICE_vol_ice_end = {:12.6e} m^3'.format (ICE_vol_ice_beg, ICE_vol_ice_end) )
-echo ( 'ICE_vol_sno_beg = {:12.6e} m^3 - ICE_vol_sno_end = {:12.6e} m^3'.format (ICE_vol_sno_beg, ICE_vol_sno_end) )
-echo ( 'ICE_vol_pnd_beg = {:12.6e} m^3 - ICE_vol_pnd_end = {:12.6e} m^3'.format (ICE_vol_pnd_beg, ICE_vol_pnd_end) )
-echo ( 'ICE_vol_fzl_beg = {:12.6e} m^3 - ICE_vol_fzl_end = {:12.6e} m^3'.format (ICE_vol_fzl_beg, ICE_vol_fzl_end) )
+echo ( 'ICE_vol_ice_beg = {:12.6e} m^3 | ICE_vol_ice_end = {:12.6e} m^3'.format (ICE_vol_ice_beg, ICE_vol_ice_end) )
+echo ( 'ICE_vol_sno_beg = {:12.6e} m^3 | ICE_vol_sno_end = {:12.6e} m^3'.format (ICE_vol_sno_beg, ICE_vol_sno_end) )
+echo ( 'ICE_vol_pnd_beg = {:12.6e} m^3 | ICE_vol_pnd_end = {:12.6e} m^3'.format (ICE_vol_pnd_beg, ICE_vol_pnd_end) )
+echo ( 'ICE_vol_fzl_beg = {:12.6e} m^3 | ICE_vol_fzl_end = {:12.6e} m^3'.format (ICE_vol_fzl_beg, ICE_vol_fzl_end) )
 
 echo ( 'dICE_vol_ice   = {:12.3e} m^3'.format (dICE_vol_ice) )
@@ -410,10 +422,9 @@
 echo ( 'dICE_mas_fzl   = {:12.3e} m^3'.format (dICE_mas_fzl) )  
 
-
 echo ( '\n------------------------------------------------------------')
 echo ( 'Variation du contenu en eau ocean + glace ' )
-echo ( 'dMass(ocean)   = {:12.6e} kg '.format(dSEA_mas_wat) )
-echo ( 'dVol (ocean)   = {:12.3e} Sv '.format(dSEA_mas_wat/dtime_sec*1E-9) )
-echo ( 'dVol (ocean)   = {:12.3e} m  '.format(dSEA_mas_wat*1E-3/OCE_aire_tot) )
+echo ( 'dMass (ocean)   = {:12.6e} kg '.format(dSEA_mas_wat) )
+echo ( 'dVol  (ocean)   = {:12.3e} Sv '.format(dSEA_mas_wat/dtime_sec*1E-9) )
+echo ( 'dVol  (ocean)   = {:12.3e} m  '.format(dSEA_mas_wat*1E-3/OCE_aire_tot) )
 
 
@@ -438,10 +449,4 @@
 echo ( '\n------------------------------------------------------------------------------------' )
 echo ( '-- Checks in NEMO - from budget_modipsl.sh (Clement Rousset)' )
-
-def OCE_flux_int (flux, aire=OCE_aire, dt=dtime_per_sec) :
-    '''Integrate flux on ocean grid'''
-    OCE_flux_int  = np.sum ( flux * aire * dt).values.item()
-    #OCE_flux_int  = np.sum ( flux.to_masked_array()[:,:] * aire.to_masked_array()[np.newaxis,:,:] * dt.to_masked_array()[:,np.newaxis,np.newaxis])
-    return OCE_flux_int
 
 # Read variable and computes integral over space and time
@@ -496,32 +501,35 @@
 OCE_mas_emp = OCE_mas_emp_oce - OCE_mas_wfxice - OCE_mas_wfxsnw - ICE_mas_wfxpnd - ICE_mas_wfxsub_err
 
+OCE_mas_all = OCE_mas_emp_oce +OCE_mas_emp_ice - OCE_mas_runoffs - OCE_mas_iceshelf
 
 echo ('\n-- Fields:' ) 
-echo ('  EMPMR      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_empmr     , OCE_mas_empmr      / dtime_sec*1E-9 ))
-echo ('  WFOB       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfob      , OCE_mas_wfob       / dtime_sec*1E-9 ))
-echo ('  EMP_OCE    {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_emp_oce   , OCE_mas_emp_oce    / dtime_sec*1E-9 ))
-echo ('  EMP_ICE    {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_emp_ice   , OCE_mas_emp_ice    / dtime_sec*1E-9 ))
-echo ('  ICEBERG    {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_iceberg   , OCE_mas_iceberg    / dtime_sec*1E-9 ))
-echo ('  ICESHELF   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_iceshelf  , OCE_mas_iceshelf   / dtime_sec*1E-9 ))
-echo ('  CALVING    {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_calving   , OCE_mas_calving    / dtime_sec*1E-9 ))
-echo ('  FRIVER     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_friver    , OCE_mas_friver     / dtime_sec*1E-9 ))  
-echo ('  RUNOFFS    {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_runoffs   , OCE_mas_runoffs    / dtime_sec*1E-9 ))
-echo ('  WFXICE     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxice    , OCE_mas_wfxice     / dtime_sec*1E-9 ))
-echo ('  WFXSNW     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxsnw    , OCE_mas_wfxsnw     / dtime_sec*1E-9 ))
-echo ('  WFXSUB     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxsub    , OCE_mas_wfxsub     / dtime_sec*1E-9 ))
-echo ('  WFXPND     {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxpnd    , ICE_mas_wfxpnd     / dtime_sec*1E-9 ))
-echo ('  WFXSNW_SUB {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxsnw_sub, ICE_mas_wfxsnw_sub / dtime_sec*1E-9 ))
-echo ('  WFXSNW_PRE {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxsnw_pre, ICE_mas_wfxsnw_pre / dtime_sec*1E-9 ))
-echo ('  WFXSUB_ERR {:12.3e} (kg) {:12.3e} (Sv) '.format ( ICE_mas_wfxsub_err, ICE_mas_wfxsub_err / dtime_sec*1E-9 ))
-echo ('  EVAP_OCE   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_evap_oce  , OCE_mas_evap_oce   / dtime_sec*1E-9 ))
-echo ('  EVAP_ICE   {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_evap_ice  , ICE_mas_evap_ice   / dtime_sec*1E-9 ))
-echo ('  SNOW_OCE   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_snow_oce  , OCE_mas_snow_oce   / dtime_sec*1E-9 ))
-echo ('  SNOW_ICE   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_snow_ice  , OCE_mas_snow_ice   / dtime_sec*1E-9 ))
-echo ('  RAIN       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_rain      , OCE_mas_rain       / dtime_sec*1E-9 ))
-echo ('  FWB        {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_fwb       , OCE_mas_fwb        / dtime_sec*1E-9 ))
-echo ('  SSR        {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_ssr       , OCE_mas_ssr        / dtime_sec*1E-9 ))
-echo ('  WFCORR     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfcorr    , OCE_mas_wfcorr     / dtime_sec*1E-9 ))
-echo ('  BERG_ICB   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_berg_icb  , OCE_mas_berg_icb   / dtime_sec*1E-9 ))
-echo ('  CALV_ICB   {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_calv_icb  , OCE_mas_calv_icb   / dtime_sec*1E-9 ))
+echo ('OCE+ICE budget {:12.3e} (kg) {:12.3e} (Sv) '.format ( OCE_mas_all       , OCE_mas_all        / dtime_sec*1E-9 ))
+echo ('  EMPMR        {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_empmr     , OCE_mas_empmr      / dtime_sec*1E-9 ))
+echo ('  WFOB         {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfob      , OCE_mas_wfob       / dtime_sec*1E-9 ))
+echo ('  EMP_OCE      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_emp_oce   , OCE_mas_emp_oce    / dtime_sec*1E-9 ))
+echo ('  EMP_ICE      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_emp_ice   , OCE_mas_emp_ice    / dtime_sec*1E-9 ))
+echo ('  EMP          {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_emp       , OCE_mas_emp        / dtime_sec*1E-9 ))
+echo ('  ICEBERG      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_iceberg   , OCE_mas_iceberg    / dtime_sec*1E-9 ))
+echo ('  ICESHELF     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_iceshelf  , OCE_mas_iceshelf   / dtime_sec*1E-9 ))
+echo ('  CALVING      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_calving   , OCE_mas_calving    / dtime_sec*1E-9 ))
+echo ('  FRIVER       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_friver    , OCE_mas_friver     / dtime_sec*1E-9 ))  
+echo ('  RUNOFFS      {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_runoffs   , OCE_mas_runoffs    / dtime_sec*1E-9 ))
+echo ('  WFXICE       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxice    , OCE_mas_wfxice     / dtime_sec*1E-9 ))
+echo ('  WFXSNW       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxsnw    , OCE_mas_wfxsnw     / dtime_sec*1E-9 ))
+echo ('  WFXSUB       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfxsub    , OCE_mas_wfxsub     / dtime_sec*1E-9 ))
+echo ('  WFXPND       {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxpnd    , ICE_mas_wfxpnd     / dtime_sec*1E-9 ))
+echo ('  WFXSNW_SUB   {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxsnw_sub, ICE_mas_wfxsnw_sub / dtime_sec*1E-9 ))
+echo ('  WFXSNW_PRE   {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_wfxsnw_pre, ICE_mas_wfxsnw_pre / dtime_sec*1E-9 ))
+echo ('  WFXSUB_ERR   {:12.3e} (kg) {:12.3e} (Sv) '.format ( ICE_mas_wfxsub_err, ICE_mas_wfxsub_err / dtime_sec*1E-9 ))
+echo ('  EVAP_OCE     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_evap_oce  , OCE_mas_evap_oce   / dtime_sec*1E-9 ))
+echo ('  EVAP_ICE     {:12.3e} (kg) {:12.3f} (Sv) '.format ( ICE_mas_evap_ice  , ICE_mas_evap_ice   / dtime_sec*1E-9 ))
+echo ('  SNOW_OCE     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_snow_oce  , OCE_mas_snow_oce   / dtime_sec*1E-9 ))
+echo ('  SNOW_ICE     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_snow_ice  , OCE_mas_snow_ice   / dtime_sec*1E-9 ))
+echo ('  RAIN         {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_rain      , OCE_mas_rain       / dtime_sec*1E-9 ))
+echo ('  FWB          {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_fwb       , OCE_mas_fwb        / dtime_sec*1E-9 ))
+echo ('  SSR          {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_ssr       , OCE_mas_ssr        / dtime_sec*1E-9 ))
+echo ('  WFCORR       {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_wfcorr    , OCE_mas_wfcorr     / dtime_sec*1E-9 ))
+echo ('  BERG_ICB     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_berg_icb  , OCE_mas_berg_icb   / dtime_sec*1E-9 ))
+echo ('  CALV_ICB     {:12.3e} (kg) {:12.3f} (Sv) '.format ( OCE_mas_calv_icb  , OCE_mas_calv_icb   / dtime_sec*1E-9 ))
 
 echo     ('\n===>  Comparing ===>' ) 
