source: TOOLS/WATER_BUDGET/CPL_waterbudget.py @ 6446

Last change on this file since 6446 was 6277, checked in by omamce, 18 months ago

O.M. : WATER_BUDGET

Switch to .ini file to read configuratio
Correct SECHIBA budget

  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 38.5 KB
Line 
1#!/usr/bin/env python3
2###
3### Script to check water conservation in the IPSL coupled model
4###
5##  Warning, to install, configure, run, use any of included software or
6##  to read the associated documentation you'll need at least one (1)
7##  brain in a reasonably working order. Lack of this implement will
8##  void any warranties (either express or implied).  Authors assumes
9##  no responsability for errors, omissions, data loss, or any other
10##  consequences caused directly or indirectly by the usage of his
11##  software by incorrectly or partially configured personal
12##
13##
14## SVN information
15#  $Author$
16#  $Date$
17#  $Revision$
18#  $Id$
19#  $HeadURL$
20
21
22###
23## Import system modules
24import sys, os, shutil, subprocess
25import numpy as np
26import configparser, re
27
28## Creates parser
29config = configparser.ConfigParser()
30config.optionxform = str # To keep capitals
31
32config['Files']  = {}
33config['System'] = {}
34
35##-- Some physical constants
36#-- Earth Radius
37Ra = 6366197.7236758135
38#-- Gravity
39grav = 9.81
40#-- Ice volumic mass (kg/m3) in LIM3/SI3
41ICE_rho_ice = 917.0
42#-- Snow volumic mass (kg/m3) in LIM3/SI3
43ICE_rho_sno = 330.0
44#-- Water in ponds coluclic mass (kg/m3) in LIM3/SI3
45ICE_rho_pnd = 1000.
46#-- Ocean water volumic mass (kg/m3) in NEMO
47OCE_rho_liq = 1026.
48#-- Water volumic mass in atmosphere
49ATM_rho = 1.0e3
50#-- Water volumic mass in surface reservoirs
51SRF_rho = 1.0e3
52#-- Water volumic mass of rivers
53RUN_rho = 1.0e3
54
55## Read experiment parameters
56ATM = None ; ORCA = None ; NEMO = None ; OCE_relax = False ; OCE_icb = False ; Coupled = False ; Routing = None
57
58# Arguments passed
59print ( "Name of Python script:", sys.argv[0] )
60IniFile =  sys.argv[1]
61print ("Input file : ", IniFile )
62config.read (IniFile)
63
64def setBool (chars) :
65    '''Convert specific char string in boolean if possible'''
66    setBool = chars
67    for key in configparser.ConfigParser.BOOLEAN_STATES.keys () :
68        if chars.lower() == key : setBool = configparser.ConfigParser.BOOLEAN_STATES[key]
69    return setBool
70
71def setNum (chars) :
72    '''Convert specific char string in integer or real if possible'''
73    if type (chars) == str :
74        realnum = re.compile ("^[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?$")
75        isReal = realnum.match(chars.strip()) != None
76        isInt  = chars.strip().isdigit()
77        if isReal :
78            if isInt : setNum = int   (chars)
79            else     : setNum = float (chars)
80        else : setNum = chars
81    else : setNum = chars
82    return setNum
83
84print ('[Experiment]')
85for VarName in config['Experiment'].keys() :
86    locals()[VarName] = config['Experiment'][VarName]
87    locals()[VarName] = setBool (locals()[VarName])
88    locals()[VarName] = setNum  (locals()[VarName])
89    print ( '{:25} set to : {:}'.format (VarName, locals()[VarName]) )
90   
91###
92ICO  = ( 'ICO' in ATM )
93LMDZ = ( 'LMD' in ATM )
94   
95###
96## Import system modules
97import sys, os, shutil, subprocess
98
99# Where do we run ?
100SysName, NodeName, Release, Version, Machine = os.uname()
101TGCC  = ( 'irene'   in NodeName )
102IDRIS = ( 'jeanzay' in NodeName )
103
104## Set site specific libIGCM directories, and other specific stuff
105if TGCC :
106    CPU = subprocess.getoutput ( 'lscpu | grep "Model name"' )
107    if "Intel(R) Xeon(R) Platinum" in CPU : Machine = 'irene'
108    if "AMD"                       in CPU : Machine = 'irene-amd'
109       
110    ARCHIVE     = subprocess.getoutput ( f'ccc_home --cccstore   -d {Project} -u {User}' )
111    STORAGE     = subprocess.getoutput ( f'ccc_home --cccwork    -d {Project} -u {User}' )
112    SCRATCHDIR  = subprocess.getoutput ( f'ccc_home --cccscratch -d {Project} -u {User}' )
113    R_IN        = os.path.join ( subprocess.getoutput ( f'ccc_home --cccwork -d igcmg -u igcmg' ), 'IGCM')
114    rebuild     = os.path.join ( subprocess.getoutput ( f'ccc_home --ccchome -d igcmg -u igcmg' ), 'Tools', Machine, 'rebuild_nemo', 'bin', 'rebuild_nemo' )
115
116    ## Specific to run at TGCC.
117    # Needed before importing a NetCDF library (netCDF4, xarray, cmds, etc ...)
118    import mpi4py
119    mpi4py.rc.initialize = False
120       
121    ## Creates output directory
122    #TmpDir = os.path.join ( subprocess.getoutput ( 'ccc_home --cccscratch' ), f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
123    TmpDir = os.path.join ( '/ccc/scratch/cont003/drf/p86mart', f'WATER_{JobName}_{YearBegin}_{YearEnd}' )
124
125if IDRIS :
126    raise Exception("Pour IDRIS : repertoires et chemins a definir") 
127
128## Import specific module
129import nemo, lmdz
130## Now import needed scientific modules
131import xarray as xr
132   
133# Output file
134FileOut = f'CPL_waterbudget_{JobName}_{YearBegin}_{YearEnd}.out'
135f_out = open ( FileOut, mode = 'w' )
136
137# Function to print to stdout *and* output file
138def echo (string, end='\n') :
139    print ( string, end=end  )
140    sys.stdout.flush ()
141    f_out.write ( string + end )
142    f_out.flush ()
143    return None
144   
145## Set libIGCM directories
146R_OUT       = os.path.join ( ARCHIVE   , 'IGCM_OUT')
147R_BUF       = os.path.join ( SCRATCHDIR, 'IGCM_OUT')
148
149L_EXP = os.path.join (TagName, SpaceName, ExperimentName, JobName)
150R_SAVE      = os.path.join ( R_OUT, L_EXP )
151R_BUFR      = os.path.join ( R_BUF, L_EXP )
152POST_DIR    = os.path.join ( R_BUF, L_EXP, 'Out' )
153REBUILD_DIR = os.path.join ( R_BUF, L_EXP, 'REBUILD' )
154R_BUF_KSH   = os.path.join ( R_BUFR, 'Out' )
155R_FIGR      = os.path.join ( STORAGE, 'IGCM_OUT', L_EXP )
156
157#if os.path.isdir (TmpDir) : shutil.rmtree ( TmpDir )
158if not os.path.isdir (TmpDir) : os.mkdir (TmpDir)
159TmpDirOCE = os.path.join (TmpDir, 'OCE')
160TmpDirICE = os.path.join (TmpDir, 'ICE')
161if not os.path.exists (TmpDirOCE) : os.mkdir (TmpDirOCE )
162if not os.path.exists (TmpDirICE) : os.mkdir (TmpDirICE )
163
164echo ( f'Working in TMPDIR : {TmpDir}' )
165
166echo ( f'\nDealing with {L_EXP}' )
167
168#-- Model output directories
169if Freq == "MO" : FreqDir =  os.path.join ('Output' , 'MO' )
170if Freq == "SE" : FreqDir =  os.path.join ('Analyse', 'SE' )
171dir_ATM_his = os.path.join ( R_SAVE, "ATM", FreqDir )
172dir_OCE_his = os.path.join ( R_SAVE, "OCE", FreqDir )
173dir_ICE_his = os.path.join ( R_SAVE, "ICE", FreqDir )
174dir_SRF_his = os.path.join ( R_SAVE, "SRF", FreqDir )
175
176echo ( f'The analysis relies on files from the following model output directories : ' )
177echo ( f'{dir_ATM_his}' )
178echo ( f'{dir_OCE_his}' )
179echo ( f'{dir_ICE_his}' )
180echo ( f'{dir_SRF_his}' )
181
182#-- Files Names
183if Freq == 'MO' : FileCommon = f'{JobName}_{YearBegin}0101_{YearEnd}1231_1M'
184if Freq == 'SE' : FileCommon = f'{JobName}_SE_{YearBegin}0101_{YearEnd}1231_1M'
185
186echo ('\nOpen history files' )
187file_ATM_his = os.path.join (  dir_ATM_his, f'{FileCommon}_histmth.nc' )
188file_OCE_his = os.path.join (  dir_OCE_his, f'{FileCommon}_grid_T.nc' )
189file_OCE_sca = os.path.join (  dir_OCE_his, f'{FileCommon}_scalar.nc' )
190file_ICE_his = os.path.join (  dir_ICE_his, f'{FileCommon}_icemod.nc' )
191file_SRF_his = os.path.join (  dir_SRF_his, f'{FileCommon}_sechiba_history.nc' )
192file_OCE_srf = os.path.join (  dir_OCE_his, f'{FileCommon}_grid_T.nc' )
193
194d_ATM_his = xr.open_dataset ( file_ATM_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
195d_OCE_his = xr.open_dataset ( file_OCE_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
196d_OCE_sca = xr.open_dataset ( file_OCE_sca, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
197d_ICE_his = xr.open_dataset ( file_ICE_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
198if NEMO == '3.6' :d_ICE_his = d_ICE_his.rename ( {'y_grid_T':'y', 'x_grid_T':'x'} )
199d_SRF_his = xr.open_dataset ( file_SRF_his, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
200d_OCE_srf = xr.open_dataset ( file_OCE_srf, use_cftime=True, decode_times=True, decode_cf=True ).squeeze()
201
202echo ( file_ATM_his )
203echo ( file_OCE_his )
204echo ( file_OCE_sca )
205echo ( file_ICE_his )
206echo ( file_SRF_his )
207
208## Compute run length
209dtime = ( d_ATM_his.time_counter_bounds.max() - d_ATM_his.time_counter_bounds.min() )
210echo ('\nRun length : {:8.2f} days'.format ( (dtime/np.timedelta64(1, "D")).values ) )
211dtime_sec = (dtime/np.timedelta64(1, "s")).values.item() # Convert in seconds
212
213## Compute length of each period
214dtime_per = (d_ATM_his.time_counter_bounds[:,-1] -  d_ATM_his.time_counter_bounds[:,0] )
215echo ('\nPeriods lengths (days) : {:} days'.format ( (dtime_per/np.timedelta64(1, "D")).values ) )
216dtime_per_sec = (dtime_per/np.timedelta64(1, "s")).values # In seconds
217dtime_per_sec = xr.DataArray (dtime_per_sec, dims=["time_counter", ], coords=[d_ATM_his.time_counter,] )
218
219#-- Open restart files
220YearRes = YearBegin - 1        # Year of the restart of beginning of simulation
221YearPre = YearBegin - PackFrequency  # Year to find the tarfile of the restart of beginning of simulation
222
223echo (f'Restart dates - Start : {YearRes}-12-31  /  End : {YearEnd}-12-31 ')
224
225file_restart_beg = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{YearPre}0101_{YearRes}1231_restart.tar' )
226file_restart_end = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{YearBegin}0101_{YearEnd}1231_restart.tar' )
227
228echo ( f'{file_restart_beg}' )
229echo ( f'{file_restart_end}' )
230
231file_OCE_beg = f'OCE_{JobName}_{YearRes}1231_restart.nc'
232file_OCE_end = f'OCE_{JobName}_{YearEnd}1231_restart.nc'
233file_ICE_beg = f'ICE_{JobName}_{YearRes}1231_restart_icemod.nc'
234file_ICE_end = f'ICE_{JobName}_{YearEnd}1231_restart_icemod.nc'
235
236echo ( f'{file_OCE_beg}' )
237echo ( f'{file_OCE_end}' )
238echo ( f'{file_ICE_beg}' )
239echo ( f'{file_ICE_end}' )
240
241file_ATM_beg = f'ATM_{JobName}_{YearRes}1231_restartphy.nc'
242file_ATM_end = f'ATM_{JobName}_{YearEnd}1231_restartphy.nc'
243if LMDZ :
244    file_DYN_beg = f'ATM_{JobName}_{YearRes}1231_restart.nc'
245    file_DYN_end = f'ATM_{JobName}_{YearEnd}1231_restart.nc'
246if ICO :
247    file_DYN_beg = f'ICO_{JobName}_{YearRes}1231_restart.nc'
248    file_DYN_end = f'ICO_{JobName}_{YearEnd}1231_restart.nc'
249file_SRF_beg = f'SRF_{JobName}_{YearRes}1231_sechiba_rest.nc'
250file_SRF_end = f'SRF_{JobName}_{YearEnd}1231_sechiba_rest.nc'
251liste_beg = [file_ATM_beg, file_DYN_beg, file_SRF_beg, ]
252liste_end = [file_ATM_end, file_DYN_end, file_SRF_end, ]
253echo ( f'{file_ATM_beg}')
254echo ( f'{file_ATM_end}')
255echo ( f'{file_SRF_beg}')
256echo ( f'{file_SRF_end}')
257
258if Routing == 'SIMPLE' : 
259    file_RUN_beg = f'SRF_{JobName}_{YearRes}1231_routing_restart.nc'
260    file_RUN_end = f'SRF_{JobName}_{YearEnd}1231_routing_restart.nc'
261    liste_beg.append ( file_RUN_beg )
262    liste_end.append ( file_RUN_end )
263    echo ( f'{file_RUN_beg}')
264    echo ( f'{file_RUN_end}')
265
266echo ('\nExtract restart files from tar : ATM, ICO and SRF')
267for resFile in liste_beg :
268    if not os.path.exists ( os.path.join (TmpDir, resFile) ) :
269        command =  f'cd {TmpDir} ; tar xf {file_restart_beg} {resFile}'
270        echo ( command )
271        os.system ( command )
272       
273for resFile in liste_end :
274    if not os.path.exists ( os.path.join (TmpDir, resFile) ) :
275        command =  f'cd {TmpDir} ; tar xf {file_restart_end} {resFile}'
276        echo ( command )
277        os.system ( command )
278
279echo ('\nOpening ATM SRF and ICO restart files')
280d_ATM_beg = xr.open_dataset ( os.path.join (TmpDir, file_ATM_beg), decode_times=False, decode_cf=True).squeeze()
281d_ATM_end = xr.open_dataset ( os.path.join (TmpDir, file_ATM_end), decode_times=False, decode_cf=True).squeeze()
282d_SRF_beg = xr.open_dataset ( os.path.join (TmpDir, file_SRF_beg), decode_times=False, decode_cf=True).squeeze()
283d_SRF_end = xr.open_dataset ( os.path.join (TmpDir, file_SRF_end), decode_times=False, decode_cf=True).squeeze()
284d_DYN_beg = xr.open_dataset ( os.path.join (TmpDir, file_DYN_beg), decode_times=False, decode_cf=True).squeeze()
285d_DYN_end = xr.open_dataset ( os.path.join (TmpDir, file_DYN_end), decode_times=False, decode_cf=True).squeeze()
286
287for var in d_SRF_beg.variables :
288    d_SRF_beg[var] = d_SRF_beg[var].where (  d_SRF_beg[var]<1.e20, 0.)
289    d_SRF_end[var] = d_SRF_end[var].where (  d_SRF_end[var]<1.e20, 0.)
290
291if ICO :
292    d_RUN_beg = xr.open_dataset ( os.path.join (TmpDir, file_RUN_beg), decode_times=False, decode_cf=True).squeeze()
293    d_RUN_end = xr.open_dataset ( os.path.join (TmpDir, file_RUN_end), decode_times=False, decode_cf=True).squeeze()
294
295echo ('\nExtract and rebuild OCE and ICE restarts')
296def get_ndomain (zfile) :
297    #d_zfile = xr.open_dataset (zfile, decode_times=False).squeeze()
298    #ndomain_opa = d_zfile.attrs['DOMAIN_number_total']
299    #d_zfile.close ()
300    ndomain_opa = subprocess.getoutput ( f'ls {zfile}_*.nc | wc -l' ).format()
301    return int (ndomain_opa)
302
303
304if not os.path.exists ( os.path.join (TmpDir, file_OCE_beg) ) :
305    if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart_0000.nc') ):
306        command =  f'cd {TmpDirOCE} ; tar xf {file_restart_beg}  OCE_{JobName}_{YearRes}1231_restart_*.nc'
307        echo ( command )
308        os.system ( command )
309    ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart') )
310    command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearRes}1231_restart {ndomain_opa} ; mv {file_OCE_beg} {TmpDir}'
311    echo ( command )
312    os.system ( command )
313    echo ( f'Rebuild done : {file_OCE_beg}')
314   
315if not os.path.exists ( os.path.join (TmpDir, file_OCE_end) ) :
316    if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart_0000.nc') ):
317        command =  f'cd {TmpDirOCE} ; tar xf {file_restart_end}  OCE_{JobName}_{YearEnd}1231_restart_*.nc'
318        echo ( command )
319        os.system ( command )
320    ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart') )
321    command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearEnd}1231_restart {ndomain_opa} ; mv {file_OCE_end} {TmpDir}'
322    echo ( command )
323    os.system ( command )
324    echo ( f'Rebuild done : {file_OCE_end}')
325
326if not os.path.exists ( os.path.join (TmpDir, file_ICE_beg) ) :
327    if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') ):
328        command =  f'cd {TmpDirICE} ; tar xf {file_restart_beg}  ICE_{JobName}_{YearRes}1231_restart_icemod_*.nc'
329        echo ( command )
330        os.system ( command )
331    ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') )
332    command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearRes}1231_restart_icemod {ndomain_opa} ; mv {file_ICE_beg} {TmpDir} '
333    echo ( command )
334    os.system ( command )
335    echo ( f'Rebuild done : {file_OCE_beg}')
336   
337if not os.path.exists ( os.path.join (TmpDir, file_ICE_end) ) :
338    if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearEnd}1231_restart_icemod') ):
339        command =  f'cd {TmpDirICE} ; tar xf {file_restart_end} ICE_{JobName}_{YearEnd}1231_restart_icemod_*.nc'
340        echo ( command )
341        os.system ( command )
342    ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') )
343    command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearEnd}1231_restart_icemod {ndomain_opa} ; mv {file_ICE_end} {TmpDir}'
344    echo ( command )
345    os.system ( command )
346    echo ( f'Rebuild done : {file_ICE_end}')
347
348echo ('Opening OCE and ICE restart files')
349if NEMO == 3.6 : 
350    d_OCE_beg = xr.open_dataset ( os.path.join (TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
351    d_OCE_end = xr.open_dataset ( os.path.join (TmpDir, file_OCE_end), decode_times=False, decode_cf=True).squeeze()
352    d_ICE_beg = xr.open_dataset ( os.path.join (TmpDir, file_ICE_beg), decode_times=False, decode_cf=True).squeeze()
353    d_ICE_end = xr.open_dataset ( os.path.join (TmpDir, file_ICE_end), decode_times=False, decode_cf=True).squeeze()
354if NEMO == 4.0 or NEMO == 4.2 : 
355    d_OCE_beg = xr.open_dataset ( os.path.join (TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
356    d_OCE_end = xr.open_dataset ( os.path.join (TmpDir, file_OCE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
357    d_ICE_beg = xr.open_dataset ( os.path.join (TmpDir, file_ICE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
358    d_ICE_end = xr.open_dataset ( os.path.join (TmpDir, file_ICE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()
359   
360echo ( file_ATM_beg )
361echo ( file_ATM_end )
362echo ( file_DYN_beg )
363echo ( file_DYN_end )
364echo ( file_SRF_beg )
365echo ( file_SRF_end )
366if Routing == 'SIMPLE' :
367    echo ( file_RUN_beg )
368    echo ( file_RUN_end )
369echo ( file_OCE_beg )
370echo ( file_OCE_end )
371echo ( file_ICE_beg )
372echo ( file_ICE_end )
373
374def ATM_stock_int (stock) :
375    '''Integrate stock on atmosphere grid'''
376    ATM_stock_int  = np.sum (  np.sort ( (stock * DYN_aire).to_masked_array().ravel()) )
377    return ATM_stock_int
378
379def OCE_stock_int (stock) :
380    '''Integrate stock on ocean grid'''
381    OCE_stock_int  = np.sum (  np.sort ( (stock * OCE_aire).to_masked_array().ravel()) )
382    return OCE_stock_int
383
384def ONE_stock_int (stock) : 
385    '''Sum stock for field already integrated by cell'''
386    ONE_stock_int  = np.sum (  np.sort ( (stock).to_masked_array().ravel()) )
387    return ONE_stock_int
388
389# ATM grid with cell surfaces
390# ATM grid with cell surfaces
391if ICO :
392    jpja, jpia = d_ATM_his['aire'][0].shape   
393    file_ATM_aire = os.path.join ( R_IN, 'ATM', 'GRID', f'aire_{ATM}_to_{jpia}x{jpja}.nc' )
394    d_ATM_aire = xr.open_dataset ( file_ATM_aire, decode_times=False ).squeeze()
395    ATM_aire = lmdz.geo2point ( d_ATM_aire ['aire'].squeeze(), cumulPoles=True )
396    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
397    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0] + d_ATM_his ['fract_lic'][0] )
398    SRF_aire = lmdz.geo2point ( d_SRF_his ['Areas'] * d_SRF_his ['Contfrac'] )
399   
400if LMDZ :
401    ATM_aire = lmdz.geo2point ( d_ATM_his ['aire'][0], cumulPoles=True )
402    ATM_fsea = lmdz.geo2point ( d_ATM_his ['fract_oce'][0] + d_ATM_his ['fract_sic'][0] )
403    ATM_flnd = lmdz.geo2point ( d_ATM_his ['fract_ter'][0]  )
404    SRF_aire = lmdz.geo2point ( d_SRF_his ['Areas'] * d_SRF_his ['Contfrac'] )
405
406if ICO :
407    # Area on icosahedron grid
408    file_DYN_aire = os.path.join ( R_IN, 'ATM', 'GRID', ATM+'_grid.nc' )
409    d_DYN_aire = xr.open_dataset ( file_DYN_aire, decode_times=False).squeeze()
410    d_DYN_aire = d_DYN_aire.rename ( {'cell':'cell_mesh'} )
411    DYN_aire   = d_DYN_aire['aire']
412
413    DYN_fsea = d_DYN_aire ['fract_oce'] + d_DYN_aire ['fract_sic']
414    DYN_flnd = 1.0 - DYN_fsea
415   
416if LMDZ :
417    DYN_aire = ATM_aire
418    DYN_fsea = ATM_fsea
419    DYN_flnd = ATM_flnd
420
421#if LMDZ :
422#    d_ATM_beg = d_ATM_beg.assign ( coords={'lon':d_ATM_beg.lon*180./np.pi} )
423
424# Get mask and surfaces
425sos = d_OCE_his ['sos'][0].squeeze()
426OCE_msk = nemo.lbc_mask ( xr.where ( sos>0, 1., 0.0 ), cd_type = 'T' )
427so = sos = d_OCE_his ['sos'][0].squeeze()
428OCE_msk3 = nemo.lbc_mask ( xr.where ( so>0., 1., 0. ), cd_type = 'T', sval = 0. )
429
430# lbc_mask removes the duplicate points (periodicity and north fold)
431OCE_aire = nemo.lbc_mask ( d_OCE_his ['area'] * OCE_msk, cd_type = 'T', sval = 0.0 )
432ICE_aire = OCE_aire
433
434ATM_aire_tot = ONE_stock_int (ATM_aire)
435SRF_aire_tot = ONE_stock_int (SRF_aire)
436OCE_aire_tot = ONE_stock_int (OCE_aire)
437ICE_aire_tot = ONE_stock_int (ICE_aire)
438
439ATM_aire_sea     = ATM_aire * ATM_fsea
440ATM_aire_sea_tot = ONE_stock_int ( ATM_aire_sea )
441
442echo ( '\n------------------------------------------------------------------------------------' )
443echo ( '-- NEMO change in stores (for the records)' )
444#-- Note that the total number of days of the run should be diagnosed rather than assumed
445#-- Here the result is in Sv
446#
447#-- Change in ocean volume in freshwater equivalent
448
449OCE_ssh_beg = d_OCE_beg['sshn']
450OCE_ssh_end = d_OCE_end['sshn']
451OCE_sum_ssh_beg = OCE_stock_int ( OCE_ssh_beg )
452OCE_sum_ssh_end = OCE_stock_int ( OCE_ssh_end )
453
454OCE_mas_wat_beg = OCE_sum_ssh_beg * OCE_rho_liq
455OCE_mas_wat_end = OCE_sum_ssh_end * OCE_rho_liq
456
457echo ( '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) )
458dOCE_vol_liq = ( OCE_sum_ssh_end - OCE_sum_ssh_beg )
459dOCE_mas_liq = dOCE_vol_liq * OCE_rho_liq
460dOCE_mas_wat = dOCE_mas_liq
461
462echo ( 'dOCE vol    = {:12.3e} m^3'.format (dOCE_vol_liq) )
463echo ( 'dOCE ssh    = {:12.3e} m  '.format (dOCE_vol_liq/OCE_aire_tot) )
464echo ( 'dOCE mass   = {:12.3e} kg '.format (dOCE_mas_liq) )
465echo ( 'dOCE mass   = {:12.3e} Sv '.format (dOCE_mas_liq/dtime_sec*1E-6/OCE_rho_liq) )
466
467## Glace et neige
468if NEMO == 3.6 :
469    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']
470    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']
471
472    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']
473    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']
474   
475    ICE_pnd_beg = 0.0 ; ICE_pnd_end = 0.0
476    ICE_fzl_beg = 0.0 ; ICE_fzl_end = 0.0
477
478    ICE_mas_wat_beg = OCE_stock_int ( (ICE_ice_beg*ICE_rho_ice + ICE_sno_beg*ICE_rho_sno) )
479    ICE_mas_wat_end = OCE_stock_int ( (ICE_ice_end*ICE_rho_ice + ICE_sno_end*ICE_rho_sno) )
480   
481if NEMO == 4.0 or NEMO == 4.2 :
482    ICE_ice_beg = d_ICE_beg ['v_i']  ; ICE_ice_end = d_ICE_end ['v_i']
483    ICE_sno_beg = d_ICE_beg ['v_s']  ; ICE_sno_end = d_ICE_end ['v_s']
484    ICE_pnd_beg = d_ICE_beg ['v_ip'] ; ICE_pnd_end = d_ICE_end ['v_ip']
485    ICE_fzl_beg = d_ICE_beg ['v_il'] ; ICE_fzl_end = d_ICE_end ['v_il']
486   
487    ICE_mas_wat_beg = OCE_stock_int ( d_ICE_beg['snwice_mass'] )
488    ICE_mas_wat_end = OCE_stock_int ( d_ICE_end['snwice_mass'] )
489   
490   
491ICE_vol_ice_beg = OCE_stock_int ( ICE_ice_beg )
492ICE_vol_ice_end = OCE_stock_int ( ICE_ice_end )
493
494ICE_vol_sno_beg = OCE_stock_int ( ICE_sno_beg )
495ICE_vol_sno_end = OCE_stock_int ( ICE_sno_end )
496
497ICE_vol_pnd_beg = OCE_stock_int ( ICE_pnd_beg )
498ICE_vol_pnd_end = OCE_stock_int ( ICE_pnd_end )
499
500ICE_vol_fzl_beg = OCE_stock_int ( ICE_fzl_beg )
501ICE_vol_fzl_end = OCE_stock_int ( ICE_fzl_end )
502
503#-- Converting to freswater volume
504dICE_vol_ice = ICE_vol_ice_end - ICE_vol_ice_beg
505dICE_mas_ice = dICE_vol_ice * ICE_rho_ice
506
507dICE_vol_sno = ICE_vol_sno_end - ICE_vol_sno_beg
508dICE_mas_sno = dICE_vol_sno * ICE_rho_sno
509
510dICE_vol_pnd = ICE_vol_pnd_end - ICE_vol_pnd_beg
511dICE_mas_pnd = dICE_vol_pnd * ICE_rho_pnd
512
513dICE_vol_fzl= ICE_vol_fzl_end - ICE_vol_fzl_beg
514dICE_mas_fzl = dICE_vol_fzl * ICE_rho_pnd
515
516if NEMO == 3.6 :
517    dICE_mas_wat = dICE_mas_ice + dICE_mas_sno
518    dSEA_mas_wat = dOCE_mas_wat + dICE_mas_ice + dICE_mas_sno
519
520if NEMO == 4.0 or NEMO == 4.2 :
521    dICE_mas_wat = ICE_mas_wat_end - ICE_mas_wat_beg
522    dSEA_mas_wat = dOCE_mas_wat + dICE_mas_wat
523
524echo ( '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) )
525echo ( '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) )
526echo ( '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) )
527echo ( '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) )
528
529echo ( 'dICE_vol_ice   = {:12.3e} m^3'.format (dICE_vol_ice) )
530echo ( 'dICE_vol_sno   = {:12.3e} m^3'.format (dICE_vol_sno) )
531echo ( 'dICE_vol_pnd   = {:12.3e} m^3'.format (dICE_vol_pnd) )
532echo ( 'dICE_mas_ice   = {:12.3e} m^3'.format (dICE_mas_ice) )
533echo ( 'dICE_mas_sno   = {:12.3e} m^3'.format (dICE_mas_sno) ) 
534echo ( 'dICE_mas_pnd   = {:12.3e} m^3'.format (dICE_mas_pnd) ) 
535echo ( 'dICE_mas_fzl   = {:12.3e} m^3'.format (dICE_mas_fzl) ) 
536echo ( 'dICE_mas_wat   = {:12.3e} m^3'.format (dICE_mas_wat) ) 
537
538
539SEA_mas_wat_beg = OCE_mas_wat_beg + ICE_mas_wat_beg
540SEA_mas_wat_end = OCE_mas_wat_end + ICE_mas_wat_end
541
542echo ( '\n------------------------------------------------------------')
543echo ( 'Variation du contenu en eau ocean + glace ' )
544echo ( 'dMass (ocean)   = {:12.6e} kg '.format(dSEA_mas_wat) )
545echo ( 'dVol  (ocean)   = {:12.3e} Sv '.format(dSEA_mas_wat/dtime_sec*1E-6/OCE_rho_liq) )
546echo ( 'dVol  (ocean)   = {:12.3e} m  '.format(dSEA_mas_wat*1E-3/OCE_aire_tot) )
547
548
549echo ( '\n------------------------------------------------------------------------------------' )
550echo ( '-- ATM changes in stores ' )
551
552#-- Change in precipitable water from the atmosphere daily and monthly files
553#-- Compute sum weighted by gridcell area (kg/m2) then convert to Sv
554
555# ATM vertical grid
556ATM_Ahyb = d_ATM_his['Ahyb'].squeeze()
557ATM_Bhyb = d_ATM_his['Bhyb'].squeeze()
558klevp1 = ATM_Ahyb.shape[0]
559
560# Surface pressure
561if ICO : 
562    DYN_ps_beg = d_DYN_beg['ps']
563    DYN_ps_end = d_DYN_end['ps']
564   
565if LMDZ : 
566    DYN_ps_beg =  lmdz.geo2point ( d_DYN_beg['ps'].isel(rlonv=slice(0,-1)) )
567    DYN_ps_end =  lmdz.geo2point ( d_DYN_end['ps'].isel(rlonv=slice(0,-1)) )
568   
569# 3D Pressure
570DYN_p_beg = ATM_Ahyb + ATM_Bhyb * DYN_ps_beg
571DYN_p_end = ATM_Ahyb + ATM_Bhyb * DYN_ps_end
572
573# Layer thickness
574DYN_sigma_beg = DYN_p_beg[0:-1]*0.
575DYN_sigma_end = DYN_p_end[0:-1]*0. 
576
577for k in np.arange (klevp1-1) :
578    DYN_sigma_beg[k,:] = (DYN_p_beg[k,:] - DYN_p_beg[k+1,:]) / grav
579    DYN_sigma_end[k,:] = (DYN_p_end[k,:] - DYN_p_end[k+1,:]) / grav
580
581DYN_sigma_beg = DYN_sigma_beg.rename ( {'klevp1':'sigs'} )
582DYN_sigma_end = DYN_sigma_end.rename ( {'klevp1':'sigs'} )
583
584##-- Vertical and horizontal integral, and sum of liquid, solid and vapor water phases
585if LMDZ :
586    try : 
587        DYN_wat_beg = lmdz.geo3point ( (d_DYN_beg['H2Ov'] + d_DYN_beg['H2Ol'] + d_DYN_beg['H2Oi']).isel(rlonv=slice(0,-1) ) )
588        DYN_wat_end = lmdz.geo3point ( (d_DYN_end['H2Ov'] + d_DYN_end['H2Ol'] + d_DYN_end['H2Oi']).isel(rlonv=slice(0,-1) ) )
589    except :
590        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) ) )
591        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) ) )
592if ICO :
593    try :
594        DYN_wat_beg = (d_DYN_beg['H2O_g'] + d_DYN_beg['H2O_l'] + d_DYN_beg['H2O_s']).rename ( {'lev':'sigs'} )
595        DYN_wat_end = (d_DYN_end['H2O_g'] + d_DYN_end['H2O_l'] + d_DYN_end['H2O_s']).rename ( {'lev':'sigs'} )
596    except : 
597        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'} )
598        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'} )
599   
600DYN_mas_wat_beg = ATM_stock_int (DYN_sigma_beg * DYN_wat_beg)
601DYN_mas_wat_end = ATM_stock_int (DYN_sigma_end * DYN_wat_end)
602
603dDYN_mas_wat = DYN_mas_wat_end - DYN_mas_wat_beg
604
605echo ( '\nVariation du contenu en eau atmosphere (dynamique) ' )
606echo ( 'DYN_mas_beg = {:12.6e} kg | DYN_mas_end = {:12.6e} kg'.format (DYN_mas_wat_beg, DYN_mas_wat_end) )
607echo ( 'dMass (atm)   = {:12.3e} kg '.format (dDYN_mas_wat) )
608echo ( 'dMass (atm)   = {:12.3e} Sv '.format (dDYN_mas_wat/dtime_sec*1.e-6/ATM_rho) )
609echo ( 'dMass (atm)   = {:12.3e} m  '.format (dDYN_mas_wat/ATM_aire_sea_tot/ATM_rho) )
610
611ATM_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']
612ATM_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']
613
614ATM_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']
615ATM_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']
616
617ATM_qsol_beg = d_ATM_beg['QSOL']
618ATM_qsol_end = d_ATM_end['QSOL']
619
620ATM_qs01_beg  = d_ATM_beg['QS01'] * d_ATM_beg['FTER']
621ATM_qs01_end  = d_ATM_end['QS01'] * d_ATM_end['FTER']
622ATM_qs02_beg  = d_ATM_beg['QS02'] * d_ATM_beg['FLIC']
623ATM_qs02_end  = d_ATM_end['QS02'] * d_ATM_end['FLIC']
624ATM_qs03_beg  = d_ATM_beg['QS03'] * d_ATM_beg['FOCE']
625ATM_qs03_end  = d_ATM_end['QS03'] * d_ATM_end['FOCE']
626ATM_qs04_beg  = d_ATM_beg['QS04'] * d_ATM_beg['FSIC']
627ATM_qs04_end  = d_ATM_end['QS04'] * d_ATM_end['FSIC'] 
628
629if ICO :
630   ATM_sno_beg   = ATM_sno_beg .rename ( {'points_physiques':'cell_mesh'} )
631   ATM_sno_end   = ATM_sno_end .rename ( {'points_physiques':'cell_mesh'} )
632   ATM_qs_beg    = ATM_qs_beg  .rename ( {'points_physiques':'cell_mesh'} )
633   ATM_qs_end    = ATM_qs_end  .rename ( {'points_physiques':'cell_mesh'} )
634   ATM_qsol_beg  = ATM_qsol_beg.rename ( {'points_physiques':'cell_mesh'} )
635   ATM_qsol_end  = ATM_qsol_end.rename ( {'points_physiques':'cell_mesh'} )
636   ATM_qs01_beg  = ATM_qs01_beg.rename ( {'points_physiques':'cell_mesh'} )
637   ATM_qs01_end  = ATM_qs01_end.rename ( {'points_physiques':'cell_mesh'} )
638   ATM_qs02_beg  = ATM_qs02_beg.rename ( {'points_physiques':'cell_mesh'} )
639   ATM_qs02_end  = ATM_qs02_end.rename ( {'points_physiques':'cell_mesh'} )
640   ATM_qs03_beg  = ATM_qs03_beg.rename ( {'points_physiques':'cell_mesh'} )
641   ATM_qs03_end  = ATM_qs03_end.rename ( {'points_physiques':'cell_mesh'} )
642   ATM_qs04_beg  = ATM_qs04_beg.rename ( {'points_physiques':'cell_mesh'} )
643   ATM_qs04_end  = ATM_qs04_end.rename ( {'points_physiques':'cell_mesh'} ) 
644
645echo ('Computing atmosphere integrals')
646ATM_mas_sno_beg   = ATM_stock_int ( ATM_sno_beg  )
647ATM_mas_sno_end   = ATM_stock_int ( ATM_sno_end  )
648ATM_mas_qs_beg    = ATM_stock_int ( ATM_qs_beg   )
649ATM_mas_qs_end    = ATM_stock_int ( ATM_qs_end   )
650ATM_mas_qsol_beg  = ATM_stock_int ( ATM_qsol_beg )
651ATM_mas_qsol_end  = ATM_stock_int ( ATM_qsol_end )
652ATM_mas_qs01_beg  = ATM_stock_int ( ATM_qs01_beg )
653ATM_mas_qs01_end  = ATM_stock_int ( ATM_qs01_end )
654ATM_mas_qs02_beg  = ATM_stock_int ( ATM_qs02_beg )
655ATM_mas_qs02_end  = ATM_stock_int ( ATM_qs02_end )
656ATM_mas_qs03_beg  = ATM_stock_int ( ATM_qs03_beg )
657ATM_mas_qs03_end  = ATM_stock_int ( ATM_qs03_end )
658ATM_mas_qs04_beg  = ATM_stock_int ( ATM_qs04_beg )
659ATM_mas_qs04_end  = ATM_stock_int ( ATM_qs04_end )
660
661dATM_mas_sno  = ATM_mas_sno_end  - ATM_mas_sno_beg
662dATM_mas_qs   = ATM_mas_qs_end   - ATM_mas_qs_beg
663dATM_mas_qsol = ATM_mas_qsol_end - ATM_mas_qsol_beg
664
665dATM_mas_qs01 = ATM_mas_qs01_end - ATM_mas_qs01_beg
666dATM_mas_qs02 = ATM_mas_qs02_end - ATM_mas_qs02_beg
667dATM_mas_qs03 = ATM_mas_qs03_end - ATM_mas_qs03_beg
668dATM_mas_qs04 = ATM_mas_qs04_end - ATM_mas_qs04_beg
669
670echo ( '\nVariation du contenu en neige atmosphere (calottes)' )
671echo ( 'ATM_mas_sno_beg  = {:12.6e} kg | ATM_mas_sno_end  = {:12.6e} kg'.format (ATM_mas_sno_beg, ATM_mas_sno_end) )
672echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_sno ) )
673echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_sno/dtime_sec*1e-6/ICE_rho_ice) )
674echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_sno/ATM_aire_sea_tot/ATM_rho) )
675
676echo ( '\nVariation du contenu humidite du sol' )
677echo ( 'ATM_mas_qs_beg  = {:12.6e} kg | ATM_mas_qs_end  = {:12.6e} kg'.format (ATM_mas_qs_beg, ATM_mas_qs_end) )
678echo ( 'dMass (neige atm) = {:12.3e} kg '.format (dATM_mas_qs ) )
679echo ( 'dMass (neige atm) = {:12.3e} Sv '.format (dATM_mas_qs/dtime_sec*1e-6/ATM_rho) )
680echo ( 'dMass (neige atm) = {:12.3e} m  '.format (dATM_mas_qs/ATM_aire_sea_tot/ATM_rho) )
681
682echo ( '\nVariation du contenu en eau+neige atmosphere ' )
683echo ( 'dMass (eau + neige atm) = {:12.3e} kg '.format (  dDYN_mas_wat + dATM_mas_sno) )
684echo ( 'dMass (eau + neige atm) = {:12.3e} Sv '.format ( (dDYN_mas_wat + dATM_mas_sno)/dtime_sec*1E-6/ATM_rho) )
685echo ( 'dMass (eau + neige atm) = {:12.3e} m  '.format ( (dDYN_mas_wat + dATM_mas_sno)/ATM_aire_sea_tot/ATM_rho) )
686
687echo ( '\n------------------------------------------------------------------------------------' )
688echo ( '-- SRF changes ' )
689
690if Routing == 'SIMPLE' :
691    RUN_mas_wat_beg = ONE_stock_int ( d_RUN_beg ['fast_reservoir'] +  d_RUN_beg ['slow_reservoir'] +  d_RUN_beg ['stream_reservoir'])
692    RUN_mas_wat_end = ONE_stock_int ( d_RUN_end ['fast_reservoir'] +  d_RUN_end ['slow_reservoir'] +  d_RUN_end ['stream_reservoir'])
693
694if Routing == 'SECHIBA' : 
695    RUN_mas_wat_beg = ONE_stock_int ( d_SRF_beg['fastres']  + d_SRF_beg['slowres'] + d_SRF_beg['streamres'] \
696                                    + d_SRF_beg['floodres'] + d_SRF_beg['lakeres'] + d_SRF_beg['pondres'] )
697    RUN_mas_wat_end = ONE_stock_int ( d_SRF_end['fastres']  + d_SRF_end['slowres'] + d_SRF_end['streamres'] \
698                                    + d_SRF_end['floodres'] + d_SRF_end['lakeres'] + d_SRF_end['pondres'] )
699
700dRUN_mas_wat = RUN_mas_wat_end - RUN_mas_wat_beg
701   
702echo ( '\nWater content in routing ' )
703echo ( 'RUN_mas_wat_beg = {:12.6e} kg | RUN_mas_wat_end = {:12.6e} kg '.format (RUN_mas_wat_end, RUN_mas_wat_end) )
704echo ( 'dMass (routing)  = {:12.3e} kg '.format(dRUN_mas_wat) )
705echo ( 'dMass (routing)  = {:12.3e} Sv '.format(dRUN_mas_wat/dtime_sec*1E-9) )
706echo ( 'dMass (routing)  = {:12.3e} m  '.format(dRUN_mas_wat/OCE_aire_tot*1E-3) )
707
708print ('Reading SRF restart')
709tot_watveg_beg  = d_SRF_beg['tot_watveg_beg']  ; tot_watveg_beg  = tot_watveg_beg .where (tot_watveg_beg < 1E10, 0.)
710tot_watsoil_beg = d_SRF_beg['tot_watsoil_beg'] ; tot_watsoil_beg = tot_watsoil_beg.where (tot_watsoil_beg< 1E10, 0.)
711snow_beg        = d_SRF_beg['snow_beg']        ; snow_beg        = snow_beg       .where (snow_beg       < 1E10, 0.)
712
713tot_watveg_end  = d_SRF_end['tot_watveg_beg']  ; tot_watveg_end  = tot_watveg_end .where (tot_watveg_end < 1E10, 0.)
714tot_watsoil_end = d_SRF_end['tot_watsoil_beg'] ; tot_watsoil_end = tot_watsoil_end.where (tot_watsoil_end< 1E10, 0.)
715snow_end        = d_SRF_end['snow_beg']        ; snow_end        = snow_end       .where (snow_end       < 1E10, 0.)
716
717if LMDZ :
718    tot_watveg_beg  = lmdz.geo2point (tot_watveg_beg)
719    tot_watsoil_beg = lmdz.geo2point (tot_watsoil_beg)
720    snow_beg        = lmdz.geo2point (snow_beg)
721    tot_watveg_end  = lmdz.geo2point (tot_watveg_end)
722    tot_watsoil_end = lmdz.geo2point (tot_watsoil_end)
723    snow_end        = lmdz.geo2point (snow_end)
724
725SRF_wat_beg = tot_watveg_beg + tot_watsoil_beg + snow_beg
726SRF_wat_end = tot_watveg_end + tot_watsoil_end + snow_end
727
728#SRF_mas_wat_beg = d_SRF_beg['tot_watveg_beg']+d_SRF_beg['tot_watsoil_beg'] + d_SRF_beg['snow_beg']
729#SRF_mas_wat_end = d_SRF_end['tot_watveg_beg']+d_SRF_end['tot_watsoil_beg'] + d_SRF_end['snow_beg']
730
731#if LMDZ :
732#    tot_watveg_beg  = tot_watveg_beg .rename ( {'y':'points_phyiques'} )
733#    tot_watsoil_beg = tot_watsoil_beg.rename ( {'y':'points_phyiques'} )
734#    snow_beg        = snow_beg       .rename ( {'y':'points_phyiques'} )
735#    tot_watveg_end  = tot_watveg_end .rename ( {'y':'points_phyiques'} )
736#    tot_watsoil_end = tot_watsoil_end.rename ( {'y':'points_phyiques'} )
737#    snow_end        = snow_end       .rename ( {'y':'points_phyiques'} )
738#    SRF_wat_beg     = SRF_wat_beg    .rename ( {'y':'points_phyiques'} )
739#    SRF_wat_end     = SRF_wat_end    .rename ( {'y':'points_phyiques'} )
740if ICO :
741    tot_watveg_beg  = tot_watveg_beg .rename ( {'y':'cell_mesh'} )
742    tot_watsoil_beg = tot_watsoil_beg.rename ( {'y':'cell_mesh'} )
743    snow_beg        = snow_beg       .rename ( {'y':'cell_mesh'} )
744    tot_watveg_end  = tot_watveg_end .rename ( {'y':'cell_mesh'} )
745    tot_watsoil_end = tot_watsoil_end.rename ( {'y':'cell_mesh'} )
746    snow_end        = snow_end       .rename ( {'y':'cell_mesh'} )
747    SRF_wat_beg     = SRF_wat_beg    .rename ( {'y':'cell_mesh'} )
748    SRF_wat_end     = SRF_wat_end    .rename ( {'y':'cell_mesh'} ) 
749
750print ('Computing integrals')
751
752print ( ' 1/6', end='' ) ; sys.stdout.flush ()
753SRF_mas_watveg_beg   = ATM_stock_int ( tot_watveg_beg  )
754print ( ' 2/6', end='' ) ; sys.stdout.flush ()
755SRF_mas_watsoil_beg  = ATM_stock_int ( tot_watsoil_beg )
756print ( ' 3/6', end='' ) ; sys.stdout.flush ()
757SRF_mas_snow_beg     = ATM_stock_int ( snow_beg        )
758print ( ' 4/6', end='' ) ; sys.stdout.flush ()
759SRF_mas_watveg_end   = ATM_stock_int ( tot_watveg_end  )
760print ( ' 5/6', end='' ) ; sys.stdout.flush ()
761SRF_mas_watsoil_end  = ATM_stock_int ( tot_watsoil_end )
762print ( ' 6/6', end='' ) ; sys.stdout.flush ()
763SRF_mas_snow_end     = ATM_stock_int ( snow_end        )
764print (' -- ') ; sys.stdout.flush ()
765
766dSRF_mas_watveg   = SRF_mas_watveg_end   - SRF_mas_watveg_beg
767dSRF_mas_watsoil  = SRF_mas_watsoil_end  - SRF_mas_watsoil_beg
768dSRF_mas_snow     = SRF_mas_snow_end     - SRF_mas_snow_beg
769
770echo ('\nLes differents reservoirs')
771echo ( 'SRF_mas_watveg_beg   = {:12.6e} kg | SRF_mas_watveg_end   = {:12.6e} kg '.format (SRF_mas_watveg_beg , SRF_mas_watveg_end) )
772echo ( 'SRF_mas_watsoil_beg  = {:12.6e} kg | SRF_mas_watsoil_end  = {:12.6e} kg '.format (SRF_mas_watsoil_beg, SRF_mas_watsoil_end) )
773echo ( 'SRF_mas_snow_beg     = {:12.6e} kg | SRF_mas_snow_end     = {:12.6e} kg '.format (SRF_mas_snow_beg   , SRF_mas_snow_end) )
774
775echo ( '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) )
776echo ( '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) )
777echo ( '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  ) )
778
779SRF_mas_wat_beg = SRF_mas_watveg_beg + SRF_mas_watsoil_beg + SRF_mas_snow_beg
780SRF_mas_wat_end = SRF_mas_watveg_end + SRF_mas_watsoil_end + SRF_mas_snow_end
781dSRF_mas_wat = SRF_mas_wat_end - SRF_mas_wat_beg
782
783echo ( '\nWater content in surface ' )
784echo ( 'SRF_mas_wat_beg  = {:12.6e} kg | SRF_mas_wat_end  = {:12.6e} kg '.format (SRF_mas_wat_beg, SRF_mas_wat_end) )
785echo ( 'dMass (water srf) = {:12.3e} kg '.format (dSRF_mas_wat) )
786echo ( 'dMass (water srf) = {:12.3e} Sv '.format (dSRF_mas_wat/dtime_sec*1E-6/ATM_rho) )
787echo ( 'dMass (water srf) = {:12.3e} m  '.format (dSRF_mas_wat/ATM_aire_sea_tot/ATM_rho) )
788
789echo ( '\nWater content in  ATM + SRF + RUN ' )
790echo ( 'mas_wat_beg = {:12.6e} kg | mas_wat_end = {:12.6e} kg '.
791           format (DYN_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg,
792                   DYN_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
793echo ( 'dMass (water atm+srf+run) = {:12.6e} kg '.format ( dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat) )
794echo ( '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) )
795echo ( '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) )
796
797echo ( '\n------------------------------------------------------------------------------------' )
798echo ( '-- Change in all components' )
799echo ( 'mas_wat_beg = {:12.6e} kg | mas_wat_end = {:12.6e} kg'.
800           format (SEA_mas_wat_beg + DYN_mas_wat_beg + ATM_mas_sno_beg + RUN_mas_wat_beg + SRF_mas_wat_beg,
801                   SEA_mas_wat_end + DYN_mas_wat_end + ATM_mas_sno_end + RUN_mas_wat_end + SRF_mas_wat_end) )
802echo ( 'dMass (water CPL)         = {:12.3e} kg '.format ( dSEA_mas_wat + dDYN_mas_wat + dATM_mas_sno + dRUN_mas_wat + dSRF_mas_wat) )
803echo ( '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) )
804echo ( '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) )
805
806
Note: See TracBrowser for help on using the repository browser.