#!/usr/bin/env python3 ### ### Script to check water conservation in NEMO ### ## 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 = { 'Author' : "$Author$", 'Date' : "$Date$", 'Revision': "$Revision$", 'Id' : "$Id: ATM_waterbudget.py 6508 2023-06-13 10:58:38Z omamce $", 'HeadURL' : "$HeadUrl: svn+ssh://omamce@forge.ipsl.jussieu.fr/ipsl/forge/projets/igcmg/svn/TOOLS/WATER_BUDGET/ATM_waterbudget.py $" } ### ## Import system modules import sys, os, shutil#, subprocess, platform import configparser, re from pathlib import Path ## Import needed scientific modules import numpy as np, xarray as xr # Check python version if sys.version_info < (3, 8, 0) : print ( f'Python version : {platform.python_version()}' ) raise Exception ( "Minimum Python version is 3.8" ) ## Import local module import WaterUtils as wu import libIGCM_sys import nemo, lmdz from WaterUtils import VarInt, Rho, Ra, Grav, ICE_rho_ice, ICE_rho_sno, OCE_rho_liq, ATM_rho, SRF_rho, RUN_rho, ICE_rho_pnd, YearLength ## Creates parser for reading .ini input file ## ------------------------------------------- config = configparser.ConfigParser ( interpolation=configparser.ExtendedInterpolation() ) config.optionxform = str # To keep capitals ## Experiment parameters ## --------------------- ATM=None ; ATM_HIS='latlon' ; SRF_HIS='latlon' ; RUN_HIS='latlon' ; ORCA=None ; NEMO=None ; OCE_relax=False OCE_icb=False ; Coupled=False ; Routing=None ; TestInterp=None TarRestartPeriod_beg=None ; TarRestartPeriod_end=None ; Comment=None ; Period=None ; Title=None YearBegin=None ; YearEnd=None ; DateBegin=None ; DateEnd=None ## ARCHIVE=None ; STORAGE=None ; SCRATCHDIR=None ; R_IN=None ; rebuild=None ; TmpDir=None FileDir=None ; FileOut=None dir_ATM_his=None ; dir_SRF_his=None ; dir_OCE_his=None ; dir_ICE_his=None FileCommon=None ; file_ATM_his=None ; file_SRF_his=None ; file_RUN_his=None file_OCE_his=None ; file_ICE_his=None ; file_OCE_sca=None tar_restart_beg=None ; tar_restart_end=None ; file_ATM_beg=None ; file_ATM_end=None ; file_DYN_beg=None file_DYN_end=None ; file_SRF_beg=None ; file_SRF_end=None file_RUN_beg=None ; file_RUN_end=None ; file_RUN_end=None ; file_OCE_beg=None file_ICE_beg=None ; file_OCE_beg=None file_OCE_end=None ; file_ICE_beg=None ; file_OCE_end=None ; file_ICE_end=None tar_restart_beg_ATM=None ; tar_restart_beg_DYN=None ; tar_restart_beg_SRF=None tar_restart_beg_RUN=None ; tar_restart_beg_OCE=None ; tar_restart_beg_ICE=None tar_restart_end_ATM=None ; tar_restart_end_DYN=None ; tar_restart_end_SRF=None tar_restart_end_RUN=None ; tar_restart_end_OCE=None ; tar_restart_end_ICE=None ContinueOnError=False ; ErrorCount=0 ## ## Precision of history file reading ## --------------------------------- # Default is float (full precision). Degrade the precision by using np.float32 # Restart file are always read at the full precision readPrec=float ## Read command line arguments ## --------------------------- print ( "Name of Python script:", sys.argv[0] ) IniFile = sys.argv[1] # Test existence of IniFile if not os.path.exists (IniFile ) : raise FileExistsError ( f"File not found : {IniFile = }" ) if 'full' in IniFile : FullIniFile = IniFile else : FullIniFile = 'full_' + IniFile print ("Input file : ", IniFile ) config.read (IniFile) FullIniFile = 'full_' + IniFile # Reading config.card if possible ## ------------------------------- ConfigCard = None if 'Experiment' in config.keys () : ## Read Experiment on Config file if possible if 'ConfigCard' in config['Experiment'].keys () : ConfigCard = config['Experiment']['ConfigCard'] print ( f'{ConfigCard=}' ) if ConfigCard : ## Read config card if it exists # Text existence of ConfigCard if os.path.exists ( ConfigCard ) : print ( f'Reading Config Card : {ConfigCard}' ) ## Creates parser for reading .ini input file MyReader = configparser.ConfigParser (interpolation=configparser.ExtendedInterpolation() ) MyReader.optionxform = str # To keep capitals MyReader.read (ConfigCard) for VarName in ['JobName', 'ExperimentName', 'SpaceName', 'LongName', 'ModelName', 'TagName'] : if VarName in MyReader['UserChoices'].keys() : locals()[VarName] = MyReader['UserChoices'][VarName] exec ( f'{VarName} = wu.setBool ({VarName})' ) exec ( f'{VarName} = wu.setNum ({VarName})' ) exec ( f'{VarName} = wu.setNone ({VarName})' ) exec ( f'wu.{VarName} = {VarName}' ) print ( f' {VarName:21} set to : {locals()[VarName]:}' ) for VarName in ['PackFrequency'] : if VarName in MyReader['Post'].keys() : locals()[VarName] = MyReader['Post'][VarName] exec ( f'{VarName} = wu.setBool ({VarName})' ) exec ( f'{VarName} = wu.setNum ({VarName})' ) exec ( f'{VarName} = wu.setNone ({VarName})' ) exec ( f'wu.{VarName} = {VarName}' ) print ( f' {VarName:21} set to : {locals()[VarName]:}' ) else : raise FileExistsError ( f"File not found : {ConfigCard = }" ) ## Reading config file ## ------------------- for Section in ['Config', 'Experiment', 'libIGCM', 'Files', 'Physics' ] : if Section in config.keys () : print ( f'\nReading [{Section}]' ) for VarName in config[Section].keys() : locals()[VarName] = config[Section][VarName] exec ( f'{VarName} = wu.setBool ({VarName})' ) exec ( f'{VarName} = wu.setNum ({VarName})' ) exec ( f'{VarName} = wu.setNone ({VarName})' ) exec ( f'wu.{VarName} = {VarName}' ) print ( f' {VarName:21} set to : {locals()[VarName]}' ) print ( f'\nConfig file readed : {IniFile} ' ) ## ## Reading prec if wu.unDefined ( 'readPrec' ) : readPrec = np.float64 else : if readPrec in ["float", "float64", "r8", "double", "" ] : readPrec = float if readPrec in [ "float32", "r4", "single", "" ] : readPrec = np.float32 if readPrec in [ "float16", "r2", "half" , "" ] : readPrec = np.float16 ## Some physical constants ## ======================= if wu.unDefined ( 'Ra' ) : Ra = wu.Ra #-- Earth Radius (m) if wu.unDefined ( 'Grav' ) : Grav = wu.Grav #-- Gravity (m^2/s if wu.unDefined ( 'ICE_rho_ice' ) : ICE_rho_ice = wu.ICE_rho_ice #-- Ice volumic mass (kg/m3) in LIM3 if wu.unDefined ( 'ICE_rho_sno') : ICE_rho_sno = wu.ICE_rho_sno #-- Snow volumic mass (kg/m3) in LIM3 if wu.unDefined ( 'OCE_rho_liq' ) : OCE_rho_liq = wu.OCE_rho_liq #-- Ocean water volumic mass (kg/m3) in NEMO if wu.unDefined ( 'ATM_rho' ) : ATM_rho = wu.ATM_rho #-- Water volumic mass in atmosphere (kg/m^3) if wu.unDefined ( 'SRF_rho' ) : SRF_rho = wu.SRF_rho #-- Water volumic mass in surface reservoir (kg/m^3) if wu.unDefined ( 'RUN_rho' ) : RUN_rho = wu.RUN_rho #-- Water volumic mass of rivers (kg/m^3) if wu.unDefined ( 'ICE_rho_pnd' ) : ICE_rho_pnd = wu.ICE_rho_pnd #-- Water volumic mass in ice ponds (kg/m^3) if wu.unDefined ( 'YearLength' ) : YearLength = wu.YearLength #-- Year length (s) ## Set libIGCM and machine dependant values ## ---------------------------------------- if not 'Files' in config.keys () : config['Files'] = {} config['Physics'] = { 'Ra':str(Ra), 'Grav':str(Grav), 'ICE_rho_ice':str(ICE_rho_ice), 'ICE_rho_sno':str(ICE_rho_sno), 'OCE_rho_liq':str(OCE_rho_liq), 'ATM_rho':str(ATM_rho), 'SRF_rho':str(SRF_rho), 'RUN_rho':str(RUN_rho)} config['Config'] = { 'ContinueOnError':ContinueOnError, 'TestInterp':str(TestInterp), 'readPrec':str(readPrec) } ## -------------------------- ICO = ( 'ICO' in wu.ATM ) LMDZ = ( 'LMD' in wu.ATM ) mm = libIGCM_sys.config ( TagName=TagName, SpaceName=SpaceName, ExperimentName=ExperimentName, JobName=JobName, User=User, Group=Group, ARCHIVE=None, SCRATCHDIR=None, STORAGE=None, R_IN=None, R_OUT=None, R_FIG=None, rebuild=None, TmpDir=None, R_SAVE=None, R_FIGR=None, R_BUFR=None, R_BUF_KSH=None, REBUILD_DIR=None, POST_DIR=None ) globals().update(mm) config['Files']['TmpDir'] = TmpDir config['libIGCM'] = { 'ARCHIVE':ARCHIVE, 'STORAGE':STORAGE, 'TmpDir':TmpDir, 'R_IN':R_IN, 'rebuild':rebuild } ## Defines begining and end of experiment ## -------------------------------------- if wu.unDefined ( 'DateBegin' ) : DateBegin = f'{YearBegin}0101' config['Experiment']['DateBegin'] = DateBegin else : YearBegin, MonthBegin, DayBegin = wu.SplitDate ( DateBegin ) DateBegin = wu.FormatToGregorian ( DateBegin) config['Experiment']['YearBegin'] = str(YearBegin) if wu.unDefined ( 'DateEnd' ) : DateEnd = f'{YearEnd}1231' config['Experiment']['DateEnd'] = str(DateEnd) else : YearEnd, MonthEnd, DayEnd = wu.SplitDate ( DateEnd ) DateEnd = wu.FormatToGregorian ( DateEnd) config['Experiment']['DateEnd'] = str(DateEnd) if wu.unDefined ( 'PackFrequency' ) : PackFrequency = YearEnd - YearBegin +1 config['Experiment']['PackFrequency'] = f'{PackFrequency}' if type ( PackFrequency ) == str : if 'Y' in PackFrequency : PackFrequency = PackFrequency.replace ( 'Y', '') if 'M' in PackFrequency : PackFrequency = PackFrequency.replace ( 'M', '') PackFrequency = int ( PackFrequency ) print ( f'{YearBegin=} {DateBegin=}' ) print ( f'{YearEnd =} {DateEnd =}' ) print ( f'{PackFrequency=}' ) ## Output file with water budget diagnostics ## ----------------------------------------- if wu.unDefined ( 'FileOut' ) : FileOut = f'OCE_waterbudget_{JobName}_{YearBegin}_{YearEnd}' if readPrec == np.float32 : FileOut = f'{FileOut}_float32' FileOut = f'{FileOut}.out' config['Files']['FileOut'] = FileOut f_out = open ( FileOut, mode = 'w' ) ## Useful functions ## ---------------- # Degrades precision if readPrec == float : def rprec (tab) : return tab else : def rprec (tab) : return tab.astype(readPrec).astype(float) def kg2Sv (val, rho=ATM_rho) : '''From kg to Sverdrup''' return val/dtime_sec*1.0e-6/rho def kg2myear (val, rho=ATM_rho) : '''From kg to m/year''' return val/OCE_aire_tot/rho/NbYear def var2prt (var, small=False, rho=ATM_rho) : if small : return var , kg2Sv(var, rho=rho)*1000., kg2myear(var, rho=rho)*1000. else : return var , kg2Sv(var, rho=rho) , kg2myear(var, rho=rho) def prtFlux (Desc, var, Form='F', small=False, rho=ATM_rho, width=15) : if small : if Form in ['f', 'F'] : ff=" {:14.6e} kg | {:12.4f} mSv | {:12.4f} mm/year " if Form in ['e', 'E'] : ff=" {:14.6e} kg | {:12.4e} mSv | {:12.4e} mm/year " else : if Form in ['f', 'F'] : ff=" {:14.6e} kg | {:12.4f} Sv | {:12.4f} m/year " if Form in ['e', 'E'] : ff=" {:14.6e} kg | {:12.4e} Sv | {:12.4e} m/year " echo ( (' {:>{width}} = ' +ff).format (Desc, *var2prt(var, small, rho=rho), width=width ) ) return None def echo (string, end='\n') : '''Function to print to stdout *and* output file''' print ( str(string), end=end ) sys.stdout.flush () f_out.write ( str(string) + end ) f_out.flush () return None ## Set libIGCM directories ## ----------------------- if wu.unDefined ('R_OUT' ) : R_OUT = os.path.join ( ARCHIVE , 'IGCM_OUT' ) if wu.unDefined ('R_BUF' ) : R_BUF = os.path.join ( SCRATCHDIR, 'IGCM_OUT' ) if wu.unDefined ('L_EXP' ) : L_EXP = os.path.join (TagName, SpaceName, ExperimentName, JobName) if wu.unDefined ('R_SAVE' ) : R_SAVE = os.path.join ( R_OUT, L_EXP ) if wu.unDefined ('R_BUFR' ) : R_BUFR = os.path.join ( R_BUF, L_EXP ) if wu.unDefined ('POST_DIR' ) : POST_DIR = os.path.join ( R_BUFR, 'Out' ) if wu.unDefined ('REBUILD_DIR') : REBUILD_DIR = os.path.join ( R_BUFR, 'REBUILD' ) if wu.unDefined ('R_BUF_KSH' ) : R_BUF_KSH = os.path.join ( R_BUFR, 'Out' ) if wu.unDefined ('R_FIGR' ) : R_FIGR = os.path.join ( STORAGE, 'IGCM_OUT', L_EXP ) config['libIGCM'].update ( { 'R_OUT':R_OUT, 'R_BUF':R_BUF, 'L_EXP':L_EXP, 'R_BUFR':R_BUFR, 'POST_DIR':POST_DIR, 'REBUILD_DIR':REBUILD_DIR, 'R_BUF_KSH':R_BUF_KSH, 'R_FIGR':R_FIGR, 'rebuild':rebuild } ) ## Set directory to extract files ## ------------------------------ if wu.unDefined ( 'FileDir' ) : FileDir = os.path.join ( TmpDir, f'WATER_{JobName}' ) config['Files']['FileDir'] = FileDir if not os.path.isdir ( FileDir ) : os.makedirs ( FileDir ) ##- Set directories to rebuild ocean and ice restart files if wu.unDefined ( 'FileDirOCE' ) : FileDirOCE = os.path.join ( FileDir, 'OCE' ) if wu.unDefined ( 'FileDirICE' ) : FileDirICE = os.path.join ( FileDir, 'ICE' ) if not os.path.exists ( FileDirOCE ) : os.mkdir ( FileDirOCE ) if not os.path.exists ( FileDirICE ) : os.mkdir ( FileDirICE ) echo (' ') echo ( f'JobName : {JobName}' ) echo ( f'Comment : {Comment}' ) echo ( f'TmpDir : {TmpDir}' ) echo ( f'FileDir : {FileDir}' ) echo ( f'FileDirOCE : {FileDirOCE}' ) echo ( f'FileDirICE : {FileDirICE}' ) echo ( f'\nDealing with {L_EXP}' ) ## Creates model output directory names ## ------------------------------------ if Freq == "MO" : FreqDir = os.path.join ( 'Output' , 'MO' ) if Freq == "SE" : FreqDir = os.path.join ( 'Analyse', 'SE' ) if wu.unDefined ( 'dir_OCE_his' ) : dir_OCE_his = os.path.join ( R_SAVE, "OCE", FreqDir ) config['Files']['dir_OCE_his'] = dir_OCE_his if wu.unDefined ( 'dir_ICE_his' ) : dir_ICE_his = os.path.join ( R_SAVE, "ICE", FreqDir ) config['Files']['dir_OCE_his'] = dir_OCE_his echo ( f'The analysis relies on files from the following model output directories : ' ) echo ( f'{dir_OCE_his}' ) echo ( f'{dir_ICE_his}' ) #-- Creates files names if wu.unDefined ( 'Period ' ) : if Freq == 'MO' : Period = f'{DateBegin}_{DateEnd}_1M' if Freq == 'SE' : Period = f'SE_{DateBegin}_{DateEnd}_1M' config['Files']['Period'] = Period config['Files']['DateBegin'] = DateBegin config['Files']['DateBegin'] = DateEnd echo ( f'Period : {Period}' ) if wu.unDefined ( 'FileCommon' ) : FileCommon = f'{JobName}_{Period}' config['Files']['FileCommon'] = FileCommon if wu.unDefined ( 'Title' ) : Title = f'{JobName} : {Freq} : {DateBegin} - {DateEnd}' config['Files']['Title'] = Title echo ('\nOpen history files' ) if wu.unDefined ('file_OCE_his' ) : file_OCE_his = os.path.join ( dir_OCE_his, f'{FileCommon}_grid_T.nc' ) file_OCE_his = file_OCE_his if wu.unDefined ('file_OCE_sca' ) : file_OCE_sca = os.path.join ( dir_OCE_his, f'{FileCommon}_scalar.nc' ) config['Files']['file_OCE_sca'] = file_OCE_sca if wu.unDefined ( 'file_ICE_hi' ) : file_ICE_his = os.path.join ( dir_ICE_his, f'{FileCommon}_icemod.nc' ) config['Files']['file_ICE_his'] = file_ICE_his 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'} ) echo ( f'{file_OCE_his = }' ) echo ( f'{file_OCE_sca = }' ) echo ( f'{file_ICE_his = }' ) ## Compute run length ## ------------------ dtime = ( d_OCE_his.time_counter_bounds.max() - d_OCE_his.time_counter_bounds.min() ) echo ( f'\nRun length : {(dtime/np.timedelta64(1, "D")).values:8.2f} days' ) dtime_sec = (dtime/np.timedelta64(1, "s")).values.item() # Convert in seconds ## Compute length of each period ## ----------------------------- dtime_per = ( d_OCE_his.time_counter_bounds[:,-1] - d_OCE_his.time_counter_bounds[:,0] ) echo ( f'\nPeriods lengths (days) : ') echo ( f' {(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_OCE_his.time_counter,] ) dtime_per_sec.attrs['unit'] = 's' ## Number of years NbYear = dtime_sec / YearLength ##-- Extract restart files from tar if wu.unDefined ('TarRestartDate_beg' ) : TarRestartDate_beg = wu.DateMinusOneDay ( DateBegin ) if wu.unDefined ('TarRestartDate_end' ) : TarRestartDate_end = wu.FormatToGregorian ( DateEnd ) if wu.unDefined ( 'TarRestartPeriod_beg' ) : TarRestartPeriod_beg_DateEnd = TarRestartDate_beg TarRestartPeriod_beg_DateBeg = wu.DateAddYear ( TarRestartPeriod_beg_DateEnd, -PackFrequency ) TarRestartPeriod_beg_DateBeg = wu.DatePlusOneDay ( TarRestartPeriod_beg_DateBeg ) TarRestartPeriod_beg = f'{TarRestartPeriod_beg_DateBeg}_{TarRestartPeriod_beg_DateEnd}' echo (f'Tar period for initial restart : {TarRestartPeriod_beg}') config['Files']['TarRestartPeriod_beg'] = TarRestartPeriod_beg if wu.unDefined ( 'TarRestartPeriod_end' ) : TarRestartPeriod_end_DateEnd = TarRestartDate_end TarRestartPeriod_end_DateBeg = wu.DateAddYear ( TarRestartPeriod_end_DateEnd, -PackFrequency ) TarRestartPeriod_end_DateBeg = wu.DatePlusOneDay ( TarRestartPeriod_end_DateBeg ) TarRestartPeriod_end = f'{TarRestartPeriod_end_DateBeg}_{TarRestartPeriod_end_DateEnd}' echo (f'Tar period for final restart : {TarRestartPeriod_end}') config['Files']['TarRestartPeriod_end'] = TarRestartPeriod_end echo (f'Restart dates - Start : {TarRestartPeriod_beg} / End : {TarRestartPeriod_end}') if wu.unDefined ( 'tar_restart_beg' ) : tar_restart_beg = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_beg}_restart.tar' ) config['Files']['tar_restart_beg'] = tar_restart_beg if wu.unDefined ( 'tar_restart_end' ) : tar_restart_end = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_end}_restart.tar' ) config['Files']['tar_restart_end'] = tar_restart_end echo ( f'{tar_restart_beg = }' ) echo ( f'{tar_restart_end = }' ) # if wu.unDefined ('tar_restart_beg_ATM' ) : tar_restart_beg_ATM = tar_restart_beg if wu.unDefined ('tar_restart_beg_DYN' ) : tar_restart_beg_DYN = tar_restart_beg if wu.unDefined ('tar_restart_beg_SRF' ) : tar_restart_beg_SRF = tar_restart_beg if wu.unDefined ('tar_restart_beg_RUN' ) : tar_restart_beg_RUN = tar_restart_beg if wu.unDefined ('tar_restart_beg_OCE' ) : tar_restart_beg_OCE = tar_restart_beg if wu.unDefined ('tar_restart_beg_ICE' ) : tar_restart_beg_ICE = tar_restart_beg if wu.unDefined ('tar_restart_end_ATM' ) : tar_restart_end_ATM = tar_restart_end if wu.unDefined ('tar_restart_end_DYN' ) : tar_restart_end_DYN = tar_restart_end if wu.unDefined ('tar_restart_end_SRF' ) : tar_restart_end_SRF = tar_restart_end if wu.unDefined ('tar_restart_end_RUN' ) : tar_restart_end_RUN = tar_restart_end if wu.unDefined ('tar_restart_end_OCE' ) : tar_restart_end_OCE = tar_restart_end if wu.unDefined ('tar_restart_end_ICE' ) : tar_restart_end_ICE = tar_restart_end if wu.unDefined ( 'file_OCE_beg' ) : file_OCE_beg = f'{FileDir}/OCE_{JobName}_{TarRestartDate_beg}_restart.nc' config['Files']['file_OCE_beg'] = file_OCE_beg if wu.unDefined ( 'file_OCE_end' ) : file_OCE_end = f'{FileDir}/OCE_{JobName}_{TarRestartDate_end}_restart.nc' config['Files']['file_OCE_end'] = file_OCE_end if wu.unDefined ( 'file_ICE_beg' ) : file_ICE_beg = f'{FileDir}/ICE_{JobName}_{TarRestartDate_beg}_restart_icemod.nc' config['Files']['file_ICE_beg'] = file_ICE_beg if wu.unDefined ( 'file_ICE_end' ) : file_ICE_end = f'{FileDir}/ICE_{JobName}_{TarRestartDate_end}_restart_icemod.nc' config['Files']['file_ICE_end'] = file_ICE_end echo ( f'{file_OCE_beg}' ) echo ( f'{file_OCE_end}' ) echo ( f'{file_ICE_beg}' ) echo ( f'{file_ICE_end}' ) liste_beg = [file_OCE_beg, file_ICE_beg ] liste_end = [file_ATM_end, file_ICE_end ] 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) def extract_and_rebuild ( file_name=file_OCE_beg, tar_restart=tar_restart_end, FileDirComp=FileDirOCE, ErrorCount=ErrorCount ) : '''Extract restart file from tar. Rebuild ocean files if needed''' echo ( f'----------') if os.path.exists ( file_name ) : echo ( f'-- File ready : {file_name = }' ) else : echo ( f'-- Extracting {file_name = }' ) base_resFile = Path (file_name).stem # basename, and remove suffix # Try to extract the rebuilded file if os.path.exists ( tar_restart ) : command = f'cd {FileDirComp} ; tar xf {tar_restart} {base_resFile}.nc' echo ( command ) try : os.system ( command ) except : if not os.path.exists ( os.path.join (FileDir, f'{base_resFile}_0000.nc') ): command = f'cd {FileDirComp} ; tar xf {tar_restart_end} {base_file}_*.nc' echo ( command ) ierr = os.system ( command ) if ierr == 0 : echo ( f'tar done : {base_resFile}.nc') else : raise Exception ( f'command failed : {command}' ) echo ( f'extract ndomain' ) ndomain_opa = get_ndomain ( os.path.join (FileDir, f'{base_file}') ) command = f'cd {FileDirComp} ; {rebuild} {base_resFile} {ndomain_opa} ; mv {base_resFile}.nc {FileDir}' echo ( command ) ierr = os.system ( command ) if ierr == 0 : echo ( f'Rebuild done : {base_resFile}.nc') else : raise Exception ( f'command failed : {command}' ) else : echo ( f'tar done : {base_resFile}') command = f'cd {FileDirComp} ; mv {base_resFile}.nc {FileDir}' ierr = os.system ( command ) if ierr == 0 : echo ( f'command done : {command}' ) else : raise Exception ( f'command failed : {command = }' ) else : echo ( f'****** Tar restart file {tar_restart = } not found ' ) if ContinueOnError : ErrorCount += 1 else : raise Exception ( f'****** tar file not found {tar_restart = } - Stopping' ) return ErrorCount ErrorCount += extract_and_rebuild ( file_name=file_OCE_beg, tar_restart=tar_restart_beg, FileDirComp=FileDirOCE ) ErrorCount += extract_and_rebuild ( file_name=file_OCE_end, tar_restart=tar_restart_end, FileDirComp=FileDirOCE ) ErrorCount += extract_and_rebuild ( file_name=file_ICE_beg, tar_restart=tar_restart_beg, FileDirComp=FileDirICE ) ErrorCount += extract_and_rebuild ( file_name=file_ICE_end, tar_restart=tar_restart_end, FileDirComp=FileDirICE ) echo ('Opening OCE and ICE restart files') if NEMO == 3.6 : d_OCE_beg = xr.open_dataset ( os.path.join (FileDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() d_OCE_end = xr.open_dataset ( os.path.join (FileDir, file_OCE_end), decode_times=False, decode_cf=True).squeeze() d_ICE_beg = xr.open_dataset ( os.path.join (FileDir, file_ICE_beg), decode_times=False, decode_cf=True).squeeze() d_ICE_end = xr.open_dataset ( os.path.join (FileDir, 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 (FileDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() d_OCE_end = xr.open_dataset ( os.path.join (FileDir, file_OCE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() d_ICE_beg = xr.open_dataset ( os.path.join (FileDir, file_ICE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() d_ICE_end = xr.open_dataset ( os.path.join (FileDir, file_ICE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() ## Write the full configuration config_out = open (FullIniFile, 'w') config.write (config_out ) config_out.close () # Get mask and surfaces sos = d_OCE_his ['sos'][0].squeeze() OCE_msk = nemo.lbc_mask ( xr.where ( sos>0., 1., 0. ), cd_type = 'T', sval = 0. ) 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 def OCE_stock_int (stock) : '''Integrate stock on ocean grid''' OCE_stock_int = wu.Psum ( (stock * OCE_aire ).to_masked_array().ravel() ) return OCE_stock_int def ONE_stock_int (stock) : '''Sum stock''' ONE_stock_int = wu.Psum ( stock.to_masked_array().ravel() ) return ONE_stock_int def OCE_flux_int (flux) : '''Integrate flux on oce grid''' OCE_flux_int = wu.Psum ( (flux * OCE_aire * dtime_per_sec).to_masked_array().ravel() ) return OCE_flux_int def ONE_flux_int (flux) : '''Integrate flux on oce grid''' OCE_flux_int = wu.Psum ( (flux * 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)' ) #-- 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 ) if NEMO == 3.6 : OCE_e3tn_beg = d_OCE_beg['fse3t_n'] OCE_e3tn_end = d_OCE_end['fse3t_n'] 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 ( f'OCE_sum_ssh_beg = {OCE_sum_ssh_beg:12.6e} m^3 - OCE_sum_ssh_end = {OCE_sum_ssh_end:12.6e} m^3' ) dOCE_ssh_vol = ( OCE_sum_ssh_end - OCE_sum_ssh_beg ) dOCE_ssh_mas = dOCE_ssh_vol * OCE_rho_liq if NEMO == 3.6 : echo ( f'OCE_sum_e3tn_beg = {OCE_sum_e3tn_beg:12.6e} m^3 - OCE_sum_e3tn_end = {OCE_sum_e3tn_end:12.6e} m^3' ) dOCE_e3tn_vol = ( OCE_sum_e3tn_end - OCE_sum_e3tn_beg ) dOCE_e3tn_mas = dOCE_e3tn_vol * OCE_rho_liq dOCE_vol_wat = dOCE_ssh_vol ; dOCE_mas_wat = dOCE_ssh_mas echo ( f'dOCE vol = {dOCE_vol_wat :12.3e} m^3' ) echo ( f'dOCE ssh = {dOCE_vol_wat/OCE_aire_tot:12.3e} m ' ) prtFlux ( 'dOCE mass ', dOCE_mas_wat, 'e' ) if NEMO == 3.6 : echo ( f'dOCE e3tn vol = {dOCE_e3tn_vol:12.3e} m^3' ) prtFlux ( 'dOCE e3tn mass', dOCE_e3tn_mas, 'e' ) ## 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 = ICE_ice_beg*ICE_rho_ice + ICE_sno_beg*ICE_rho_sno ICE_mas_wat_end = 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 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 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 ( f'ICE_vol_ice_beg = {ICE_vol_ice_beg:12.6e} m^3 | ICE_vol_ice_end = {ICE_vol_ice_end:12.6e} m^3' ) echo ( f'ICE_vol_sno_beg = {ICE_vol_sno_beg:12.6e} m^3 | ICE_vol_sno_end = {ICE_vol_sno_end:12.6e} m^3' ) echo ( f'ICE_vol_pnd_beg = {ICE_vol_pnd_beg:12.6e} m^3 | ICE_vol_pnd_end = {ICE_vol_pnd_end:12.6e} m^3' ) echo ( f'ICE_vol_fzl_beg = {ICE_vol_fzl_beg:12.6e} m^3 | ICE_vol_fzl_end = {ICE_vol_fzl_end:12.6e} m^3' ) echo ( f'dICE_vol_ice = {dICE_vol_ice:12.3e} m^3' ) echo ( f'dICE_vol_sno = {dICE_vol_sno:12.3e} m^3' ) echo ( f'dICE_vol_pnd = {dICE_vol_pnd:12.3e} m^3' ) echo ( f'dICE_mas_ice = {dICE_mas_ice:12.3e} m^3' ) echo ( f'dICE_mas_sno = {dICE_mas_sno:12.3e} m^3' ) echo ( f'dICE_mas_pnd = {dICE_mas_pnd:12.3e} m^3' ) echo ( f'dICE_mas_fzl = {dICE_mas_fzl:12.3e} m^3' ) echo ( '\n------------------------------------------------------------') echo ( 'Change in water content (ocean + ice) ' ) prtFlux ( 'dMass (ocean)', dSEA_mas_wat, 'e', True ) ### ### And now, working on fluxes !! # if coupled: # emp_oce = evap - precip (including blowing snow) - calving # emp_ice = evap - precip (excluding blowing snow) # emp_ice = wfx_spr(<0) + wfx_sub(>0) + wfx_snw_sub(v4.0.6) - wfx_err_sub(<0) # runoffs = rivers + icebergs # empmr = emp_oce - wfx_ice - wfx_snw - wfx_pnd(v4.0.6) - wfx_err_sub - runoffs # doce+ice = - evap + precip + calving (emp_oce) # + rivers + icebergs (friver+iceberg ou runoffs) # + iceshelf (iceshelf) # - emp_ice (emp_ice) # dice = - emp_ice - wfx_snw - wfx_ice - wfx_pnd(v4.0.6) - wfx_err_sub #OCE_emp = evap - precip (including blowing snow) - calving #ICE_emp = wfx_spr(<0) + wfx_sub(>0) + wfx_snw_sub(v4.0.6) - wfx_err_sub(<0) echo ( '\n------------------------------------------------------------------------------------' ) echo ( '-- Checks in NEMO - from budget_modipsl.sh (Clément Rousset)' ) # Read variable and computes integral over space and time OCE_empmr = rprec (d_OCE_his['wfo'] ) ; OCE_mas_empmr = OCE_flux_int ( OCE_empmr ) OCE_wfob = rprec (d_OCE_his['wfob'] ) ; OCE_mas_wfob = OCE_flux_int ( OCE_wfob ) OCE_emp_oce = rprec (d_OCE_his['emp_oce'] ) ; OCE_mas_emp_oce = OCE_flux_int ( OCE_emp_oce ) OCE_emp_ice = rprec (d_OCE_his['emp_ice'] ) ; OCE_mas_emp_ice = OCE_flux_int ( OCE_emp_ice ) OCE_iceshelf = rprec (d_OCE_his['iceshelf']) ; OCE_mas_iceshelf = OCE_flux_int ( OCE_iceshelf ) OCE_calving = rprec (d_OCE_his['calving'] ) ; OCE_mas_calving = OCE_flux_int ( OCE_calving ) OCE_iceberg = rprec (d_OCE_his['iceberg'] ) ; OCE_mas_iceberg = OCE_flux_int ( OCE_iceberg ) OCE_friver = rprec (d_OCE_his['friver'] ) ; OCE_mas_friver = OCE_flux_int ( OCE_friver ) OCE_runoffs = rprec (d_OCE_his['runoffs'] ) ; OCE_mas_runoffs = OCE_flux_int ( OCE_runoffs ) if NEMO == 4.0 or NEMO == 4.2 : OCE_wfxice = rprec (d_OCE_his['vfxice']) ; OCE_mas_wfxice = OCE_flux_int ( OCE_wfxice ) OCE_wfxsnw = rprec (d_OCE_his['vfxsnw']) ; OCE_mas_wfxsnw = OCE_flux_int ( OCE_wfxsnw ) OCE_wfxsub = rprec (d_OCE_his['vfxsub']) ; OCE_mas_wfxsub = OCE_flux_int ( OCE_wfxsub ) if NEMO == 3.6 : OCE_wfxice = rprec (d_OCE_his['vfxice'])/86400.*ICE_rho_ice ; OCE_mas_wfxice = OCE_flux_int ( OCE_wfxice ) OCE_wfxsnw = rprec (d_OCE_his['vfxsnw'])/86400.*ICE_rho_sno ; OCE_mas_wfxsnw = OCE_flux_int ( OCE_wfxsnw ) OCE_wfxsub = rprec (d_OCE_his['vfxsub'])/86400.*ICE_rho_sno ; OCE_mas_wfxsub = OCE_flux_int ( OCE_wfxsub ) # Additional checks OCE_evap_oce = rprec (d_OCE_his['evap_ao_cea']) ; OCE_mas_evap_oce = OCE_flux_int ( OCE_evap_oce ) ICE_evap_ice = rprec (d_OCE_his['subl_ai_cea']) ; ICE_mas_evap_ice = OCE_flux_int ( ICE_evap_ice ) OCE_snow_oce = rprec (d_OCE_his['snow_ao_cea']) ; OCE_mas_snow_oce = OCE_flux_int ( OCE_snow_oce ) OCE_snow_ice = rprec (d_OCE_his['snow_ai_cea']) ; OCE_mas_snow_ice = OCE_flux_int ( OCE_snow_ice ) OCE_rain = rprec (d_OCE_his['rain'] ) ; OCE_mas_rain = OCE_flux_int ( OCE_rain ) ICE_wfxsub_err = rprec (d_ICE_his['vfxsub_err'] ) ; ICE_mas_wfxsub_err = OCE_flux_int ( ICE_wfxsub_err ) if NEMO == 4.0 or NEMO == 4.2 : ICE_wfxpnd = rprec (d_ICE_his['vfxpnd'] ) ; ICE_mas_wfxpnd = OCE_flux_int ( ICE_wfxpnd ) ICE_wfxsnw_sub = rprec (d_ICE_his['vfxsnw_sub']) ; ICE_mas_wfxsnw_sub = OCE_flux_int ( ICE_wfxsnw_sub ) ICE_wfxsnw_pre = rprec (d_ICE_his['vfxsnw_pre']) ; ICE_mas_wfxsnw_pre = OCE_flux_int ( ICE_wfxsnw_pre ) if NEMO == 3.6 : ICE_wfxpnd = 0.0 ; ICE_mas_wfxpnd = 0.0 ICE_wfxsnw_sub = rprec (d_ICE_his['vfxsub'])/86400.*ICE_rho_sno ; ICE_mas_wfxsnw_sub = OCE_flux_int ( ICE_wfxsnw_sub ) ICE_wfxsnw_pre = rprec (d_ICE_his['vfxspr'])/86400.*ICE_rho_sno ; ICE_mas_wfxsnw_pre = OCE_flux_int ( ICE_wfxsnw_pre ) OCE_wfcorr = rprec (d_OCE_his['wfcorr']) ; OCE_mas_wfcorr = OCE_flux_int ( OCE_wfcorr ) if OCE_relax : # ssr and fwb are included in emp=>empmr but not in emp_oce (outputed by sea-ice) OCE_vflx_fwb = rprec (d_OCE_his['vflx_fwb']) ; OCE_mas_vflx_fwb = OCE_flux_int ( OCE_vflx_fwb ) OCE_vflx_ssr = rprec (d_OCE_his['vflx_ssr']) ; OCE_mas_vflx_ssr = OCE_flux_int ( OCE_vflx_ssr ) else : OCE_fwb = 0.0 ; OCE_mas_fwb = 0.0 OCE_ssr = 0.0 ; OCE_mas_ssr = 0.0 if OCE_icb : OCE_berg_icb = rprec (d_OCE_his['berg_floating_melt']) ; OCE_mas_berg_icb = OCE_flux_int ( OCE_berg_icb ) OCE_calving_icb = rprec (d_OCE_his['calving_icb'] ) ; OCE_mas_calv_icb = OCE_flux_int ( OCE_calving_icb ) else : OCE_berg_icb = 0. ; OCE_mas_berg_icb = 0. OCE_calv_icb = 0. ; OCE_mas_calv_icb = 0. 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:' ) prtFlux ('OCE+ICE budget ', OCE_mas_all , 'e', True) prtFlux (' EMPMR ', OCE_mas_empmr , 'e', True) prtFlux (' WFOB ', OCE_mas_wfob , 'e', True) prtFlux (' EMP_OCE ', OCE_mas_emp_oce , 'e', True) prtFlux (' EMP_ICE ', OCE_mas_emp_ice , 'e', True) prtFlux (' EMP ', OCE_mas_emp , 'e', True) prtFlux (' ICEBERG ', OCE_mas_iceberg , 'e', True) prtFlux (' ICESHELF ', OCE_mas_iceshelf , 'e', True) prtFlux (' CALVING ', OCE_mas_calving , 'e', True) prtFlux (' FRIVER ', OCE_mas_friver , 'e', True) prtFlux (' RUNOFFS ', OCE_mas_runoffs , 'e', True) prtFlux (' WFXICE ', OCE_mas_wfxice , 'e', True) prtFlux (' WFXSNW ', OCE_mas_wfxsnw , 'e', True) prtFlux (' WFXSUB ', OCE_mas_wfxsub , 'e', True) prtFlux (' WFXPND ', ICE_mas_wfxpnd , 'e', True) prtFlux (' WFXSNW_SUB ', ICE_mas_wfxsnw_sub, 'e', True) prtFlux (' WFXSNW_PRE ', ICE_mas_wfxsnw_pre, 'e', True) prtFlux (' WFXSUB_ERR ', ICE_mas_wfxsub_err, 'e', True) prtFlux (' EVAP_OCE ', OCE_mas_evap_oce , 'e' ) prtFlux (' EVAP_ICE ', ICE_mas_evap_ice , 'e', True) prtFlux (' SNOW_OCE ', OCE_mas_snow_oce , 'e', True) prtFlux (' SNOW_ICE ', OCE_mas_snow_ice , 'e', True) prtFlux (' RAIN ', OCE_mas_rain , 'e' ) prtFlux (' FWB ', OCE_mas_fwb , 'e', True) prtFlux (' SSR ', OCE_mas_ssr , 'e', True) prtFlux (' WFCORR ', OCE_mas_wfcorr , 'e', True) prtFlux (' BERG_ICB ', OCE_mas_berg_icb , 'e', True) prtFlux (' CALV_ICB ', OCE_mas_calv_icb , 'e', True) echo (' ') if Coupled : prtFlux ( 'Bilan ocean :', -OCE_mas_emp_oce - OCE_mas_emp_ice + OCE_mas_runoffs + OCE_mas_iceshelf, 'e', True ) else : prtFlux ( 'Bilan ocean :', -OCE_mas_emp_oce - OCE_mas_emp_ice + OCE_mas_runoffs + OCE_mas_iceberg + OCE_mas_calving + OCE_mas_iceshelf, 'e', True ) echo ('\n===> Comparing ===>' ) echo (' WFX OCE = -empmr + iceshelf = {:12.5e} (kg) '.format ( -OCE_mas_empmr + OCE_mas_iceshelf) ) echo (' versus d OCE = {:12.5e} (kg) '.format ( dOCE_mas_wat) ) echo (' WFX ICE+SNW+PND 1 = emp_ice - wfxice - wfxsnw - wfxpnd - wfxsub_err = {:12.5e} (kg) '.format ( -OCE_mas_emp_ice - OCE_mas_wfxice - OCE_mas_wfxsnw - ICE_mas_wfxpnd - ICE_mas_wfxsub_err) ) echo (' WFX ICE+SNW+PND 2 = -emp_ice + empmr - emp_oce + runoffs = {:12.5e} (kg) '.format ( -OCE_mas_emp_ice + OCE_mas_empmr - OCE_mas_emp_oce + OCE_mas_runoffs )) echo (' versus d ICE+SNW+PND = {:12.5e} (kg) '.format ( dICE_mas_wat)) # Manque PND ? if Coupled : echo (' WFX OCE+ICE+SNW+PND = -emp_oce - emp_ice + runoffs + iceshelf = {:12.5e} (kg) '.format ( -OCE_mas_emp_oce - OCE_mas_emp_ice + OCE_mas_runoffs + OCE_mas_iceshelf)) else : echo (' WFX OCE+ICE+SNW+PND = -emp_oce - emp_ice + runoffs + iceberg + calving + iceshelf = {:12.5e} (kg) '.format ( -OCE_mas_emp_oce - OCE_mas_emp_ice + OCE_mas_runoffs + OCE_mas_iceberg + OCE_mas_calving + OCE_mas_iceshelf)) echo (' versus d OCE+ICE+SNW+PND = {:12.5e} (kg) '.format ( dSEA_mas_wat )) # Manque PND echo ('\n===> Leaks ===>') echo (' Leak OCE = dOCE_mas_wat + empmr - iceshelf = {:12.3e} (kg) '.format ( dOCE_mas_wat + OCE_mas_empmr - OCE_mas_iceshelf) ) echo (' = (dOCE_mas_wat + empmr - iceshelf)/abs(dOCE_mas_wat) = {:12.1e} (-) '.format ( (dOCE_mas_wat + OCE_mas_empmr - OCE_mas_iceshelf ) / abs (dOCE_mas_wat) ) ) echo (' Leak ICE+SNW+PND 1 = dICE_mas_wat + emp_ice + wfxice + wfxsnw + wfxpnd + wfxsub_err = {:12.3e} (kg) '.format ( dICE_mas_wat + OCE_mas_emp_ice + OCE_mas_wfxice + OCE_mas_wfxsnw + ICE_mas_wfxpnd + ICE_mas_wfxsub_err ) ) echo (' = (dICE_mas_wat + emp_ice + wfxice + wfxsnw + wfxpnd + wfxsub_err )/dICE_mas_wat = {:12.1e} (-) '.format ( (dICE_mas_wat + OCE_mas_emp_ice + OCE_mas_wfxice + OCE_mas_wfxsnw + ICE_mas_wfxpnd + ICE_mas_wfxsub_err)/abs(dICE_mas_wat)) ) echo (' Leak ICE+SNW+PND 2 = dICE_mas_wat + emp_ice - empmr + emp_oce - runoffs = {:12.3e} (kg) '.format ( dICE_mas_wat + OCE_mas_emp_ice - OCE_mas_empmr + OCE_mas_emp_oce - OCE_mas_runoffs )) echo (' = (dICE_mas_wat - empmr + emp_oce - runoffs)/abs(dICE_mas_wat) = {:12.1e} (-) '.format ( (dICE_mas_wat - OCE_mas_empmr + OCE_mas_emp_oce - OCE_mas_runoffs)/abs(dICE_mas_wat)) ) echo (' Leak OCE+ICE+SNW+PND = d(ICE+OCE)_mas_wat + emp_oce + emp_ice - runoffs - iceshelf = {:12.3e} (kg) '.format ( dSEA_mas_wat + OCE_mas_emp_oce +OCE_mas_emp_ice - OCE_mas_runoffs - OCE_mas_iceshelf )) echo (' = (dSEA_mas_wat + emp_oce + emp_ice - runoffs - iceshelf)*/abs(dSEA_mas_wat) = {:12.1e} (-) '.format ( (dSEA_mas_wat + OCE_mas_emp_oce + OCE_mas_emp_ice - OCE_mas_runoffs - OCE_mas_iceshelf)/abs(dSEA_mas_wat) ) ) prtFlux (' Leak OCE ', ( dOCE_mas_wat + OCE_mas_empmr - OCE_mas_iceshelf) , 'e', True ) prtFlux (' Leak ICE+SNW+PND ', ( dICE_mas_wat + OCE_mas_emp_ice + OCE_mas_wfxice + OCE_mas_wfxsnw + ICE_mas_wfxpnd + ICE_mas_wfxsub_err ) , 'e', True ) prtFlux (' Leak OCE+ICE+SNW+PND ', ( dSEA_mas_wat + OCE_mas_emp_oce +OCE_mas_emp_ice - OCE_mas_runoffs - OCE_mas_iceshelf ) , 'e', True ) # check if emp = emp_ice + emp_oce - calving # emp_ice = wfxsub + wfxsnw_sub + wfxsnw_pre - wfxsub_err echo ('\n===> Checks ===>' ) echo (' Check EMPMR = empmr - emp_oce + runoffs + wfxice + wfxsnw + wfxpnd + wfxsub_err = {:12.5e} (kg) '.format ( OCE_mas_empmr - OCE_mas_emp_oce + OCE_mas_runoffs + OCE_mas_wfxice + OCE_mas_wfxsnw + ICE_mas_wfxpnd + ICE_mas_wfxsub_err )) if Coupled : echo (' Check EMP_OCE = emp_oce + fwb + ssr - evap_oce + rain + snow_oce + calving = {:12.5e} (kg) '.format ( OCE_mas_emp_oce + OCE_mas_fwb + OCE_mas_ssr - OCE_mas_evap_oce + OCE_mas_rain + OCE_mas_snow_oce + OCE_mas_calving )) else : echo (' Check EMP_OCE = emp_oce + ssr + fwb - evap_oce + rain + snow_oce = {:12.5e} (kg) '.format ( OCE_mas_emp_oce + OCE_mas_ssr + OCE_mas_fwb - OCE_mas_evap_oce + OCE_mas_rain + OCE_mas_snow_oce )) echo (' Check EMP_ICE = emp_ice - evap_ice + snow_ice = {:12.5e} (kg) '.format ( OCE_mas_emp_ice - ICE_mas_evap_ice + OCE_mas_snow_ice )) echo (' Check EMP_ICE vs WFX = emp_ice - wfxsub - wfxsnw_sub - wfxsnw_pre + wfxsub_err = {:12.5e} (kg) '.format ( OCE_mas_emp_ice - OCE_mas_wfxsub - ICE_mas_wfxsnw_sub - ICE_mas_wfxsnw_pre + ICE_mas_wfxsub_err )) echo ( '\n------------------------------------------------------------------------------------' ) echo ( '-- Computations in the PDF note of Clément Rousset') echo ( 'Freshwater budget of the ice-ocean system = emp_oce + emp_ice - runoffs - iceberg - iceshelf = {:12.5e} (kg) '.format ( OCE_mas_emp_oce + OCE_mas_emp_ice - OCE_mas_runoffs - OCE_mas_iceberg - OCE_mas_iceshelf )) echo ( 'Freshwater budget of the ice-ocean system = emp_oce + emp_ice - friver - iceberg - iceshelf = {:12.5e} (kg) '.format ( OCE_mas_emp_oce + OCE_mas_emp_ice - OCE_mas_friver - OCE_mas_iceberg - OCE_mas_iceshelf )) echo ( 'Freshwater budget in the ocean = ocean mass change = emp_oce - vfxice - ( vfxsnw + vfxsub_err ) - runoffs - iceshelf = {:12.5e} (kg) '.format ( OCE_mas_emp_oce - OCE_mas_wfxice - ( OCE_mas_wfxsnw + ICE_mas_wfxsub_err ) - OCE_mas_runoffs - OCE_mas_iceshelf )) echo ( 'Freshwater budget in the ocean = ocean mass change = emp_oce - vfxice - ( vfxsnw + vfxsub_err ) - friver - iceshelf = {:12.5e} (kg) '.format ( OCE_mas_emp_oce - OCE_mas_wfxice - ( OCE_mas_wfxsnw + ICE_mas_wfxsub_err ) - OCE_mas_friver - OCE_mas_iceshelf )) echo ( 'Freshwater budget in the ice = ice mass change = vfxice + ( vfxsnw + vfxsub + vfxspr ) = {:12.5e} (kg) '.format ( OCE_mas_wfxice + OCE_mas_wfxsnw + OCE_mas_wfxsub + ICE_mas_wfxsnw_pre )) echo ( 'Freshwater flux at the interface [ice/snow]-ocean = vfxice + ( vfxsnw + vfxsub_err ) = {:12.5e} (kg) '.format ( OCE_mas_wfxsub + ICE_mas_wfxsnw_pre )) echo ( 'Freshwater flux at the interface [ice/snow]-atm = ( vfxsub + vfxspr ) = {:12.5e} (kg) '.format ( OCE_mas_emp_ice + ICE_mas_wfxsub_err )) echo ( 'Freshwater flux at the interface [ice/snow]-atm = emp_ice + vfxsub_err = {:12.5e} (kg) '.format ( OCE_mas_emp_ice + ICE_mas_wfxsub_err )) echo ( 'Freshwater flux at the interface ocean-atm = emp_oce + calving - vfxsub_err = {:12.5e} (kg) '.format ( OCE_mas_emp_oce + OCE_mas_calving - ICE_mas_wfxsub_err )) echo ( "scsshtot : global_average_sea_level_change = {:12.3e} (m) ".format ( np.sum (d_OCE_sca['scsshtot'] ).values ) ) echo ( "scsshtot : global_average_sea_level_change = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['scsshtot'] ).values * OCE_aire_tot*OCE_rho_liq ) ) echo ( "bgvolssh : drift in global mean ssh volume wrt timestep 1 = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['bgvolssh'] ).values * 1e9 * OCE_rho_liq ) ) echo ( "bgvole3t : drift in global mean volume variation (e3t) wrt timestep 1 = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['bgvole3t'] ).values * 1e9 * OCE_rho_liq ) ) echo ( "bgfrcvol : global mean volume from forcing = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['bgfrcvol'] ).values * 1e9 * OCE_rho_liq ) ) echo ( "ibgvol_tot : global mean ice volume = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['ibgvol_tot']).values * 1e9 * OCE_rho_liq ) ) echo ( "sbgvol_tot : global mean snow volume = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['sbgvol_tot']).values * 1e9 * OCE_rho_liq ) ) echo ( "ibgvolume : drift in ice/snow volume (equivalent ocean volume) = {:12.3e} (kg)".format ( np.sum (d_OCE_sca['ibgvolume'] ).values * 1e9 * OCE_rho_liq ) )