Changeset 6508 for TOOLS/WATER_BUDGET/OCE_waterbudget.py
- Timestamp:
- 06/13/23 12:58:38 (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TOOLS/WATER_BUDGET/OCE_waterbudget.py
r6277 r6508 24 24 import numpy as np 25 25 import configparser, re 26 27 ## Creates parser 26 from pathlib import Path 27 28 ## Import local module 29 import WaterUtils as wu 30 31 # Check python version 32 if sys.version_info < (3, 8, 0) : 33 print ( f'Python version : {platform.python_version()}' ) 34 raise Exception ( "Minimum Python version is 3.8" ) 35 36 ## Creates parser for reading .ini input file 28 37 config = configparser.ConfigParser() 29 38 config.optionxform = str # To keep capitals … … 33 42 34 43 ## 35 ARCHIVE=None ; STORAGE = None ; SCRATCHDIR=None ; R_IN=None ; rebuild= None36 TmpDir=None ; FileOut=None ; dir_ATM_his=None ; dir_SRF_his=None ; dir_OCE_his=None ; dir_ICE_his=None ; FileCommon=None44 ARCHIVE=None ; STORAGE = None ; SCRATCHDIR=None ; R_IN=None ; rebuild='rebuild_nemo' 45 TmpDir=None ; RunDir=None ; FileOut=None ; dir_ATM_his=None ; dir_SRF_his=None ; dir_OCE_his=None ; dir_ICE_his=None ; FileCommon=None 37 46 file_ATM_his=None ; file_SRF_his=None ; file_RUN_his=None ; file_OCE_his=None ; file_ICE_his=None ; file_OCE_sca=None 38 file_restart_beg=None ; file_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=None47 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 39 48 file_RUN_beg=None ; file_RUN_end=None ; file_OCE_beg=None ; file_ICE_beg=None ; file_OCE_end=None ; file_ICE_end=None 40 41 # Arguments passed 49 ContinueOnError=False ; ErrorCount=0 50 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 51 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 52 53 # Read command line arguments 42 54 print ( "Name of Python script:", sys.argv[0] ) 43 55 IniFile = sys.argv[1] 56 # Text existence of IniFile 57 print ("Input file : ", IniFile ) 58 59 if 'full' in IniFile : FullIniFile = IniFile 60 else : FullIniFile = 'full_' + IniFile 61 44 62 print ("Input file : ", IniFile ) 45 63 config.read (IniFile) 46 if 'full' in IniFile : FullIniFile = IniFile 47 else : FullIniFile = 'full_' + IniFile 48 49 def setBool (chars) : 50 '''Convert specific char string in boolean if possible''' 51 setBool = chars 52 for key in configparser.ConfigParser.BOOLEAN_STATES.keys () : 53 if chars.lower() == key : setBool = configparser.ConfigParser.BOOLEAN_STATES[key] 54 return setBool 55 56 def setNum (chars) : 57 '''Convert specific char string in integer or real if possible''' 58 if type (chars) == str : 59 realnum = re.compile ("^[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?$") 60 isReal = realnum.match(chars.strip()) != None 61 isInt = chars.strip().isdigit() 62 if isReal : 63 if isInt : setNum = int (chars) 64 else : setNum = float (chars) 65 else : setNum = chars 66 else : setNum = chars 67 return setNum 68 69 def setNone (chars) : 70 '''Convert specific char string to None if possible''' 71 if type (chars) == str : 72 if chars in ['None', 'NONE', 'none'] : setNone = None 73 else : setNone = chars 74 else : setNone = chars 75 return setNone 76 77 ## Reading config 64 FullIniFile = 'full_' + IniFile 65 66 ## Reading config file 78 67 for Section in ['Experiment', 'libIGCM', 'Files', 'Physics' ] : 79 if Section in config.keys() :80 print ( f' [{Section}]' )68 if Section in config.keys () : 69 print ( f'Reading [{Section}]' ) 81 70 for VarName in config[Section].keys() : 82 71 locals()[VarName] = config[Section][VarName] 83 locals()[VarName] = setBool (locals()[VarName]) 84 locals()[VarName] = setNum (locals()[VarName]) 72 exec ( f'{VarName} = wu.setBool ({VarName})' ) 73 exec ( f'{VarName} = wu.setNum ({VarName})' ) 74 exec ( f'{VarName} = wu.setNone ({VarName})' ) 75 exec ( f'wu.{VarName} = {VarName}' ) 85 76 print ( '{:25} set to : {:}'.format (VarName, locals()[VarName]) ) 86 77 #exec ( f'del {VarName}' ) 78 79 #-- Some physical constants 80 if wu.unDefined ( 'Ra' ) : Ra = 6366197.7236758135 #-- Earth Radius (m) 81 if wu.unDefined ( 'Grav' ) : Grav = 9.81 #-- Gravity (m^2/s 82 if wu.unDefined ( 'ICE_rho_ice' ) : ICE_rho_ice = 917.0 #-- Ice volumic mass (kg/m3) in LIM3 83 if wu.unDefined ( 'ICE_rho_sno') : ICE_rho_sno = 330.0 #-- Snow volumic mass (kg/m3) in LIM3 84 if wu.unDefined ( 'OCE_rho_liq' ) : OCE_rho_liq = 1026.0 #-- Ocean water volumic mass (kg/m3) in NEMO 85 if wu.unDefined ( 'ATM_rho' ) : ATM_rho = 1000.0 #-- Water volumic mass in atmosphere (kg/m^3) 86 if wu.unDefined ( 'SRF_rho' ) : SRF_rho = 1000.0 #-- Water volumic mass in surface reservoir (kg/m^3) 87 if wu.unDefined ( 'RUN_rho' ) : RUN_rho = 1000.0 #-- Water volumic mass of rivers (kg/m^3) 88 if wu.unDefined ( 'ICE_rho_pnd' ) : ICE_rho_pnd = 1000. #-- Water volumic mass in ice ponds (kg/m^3) 89 if wu.unDefined ( 'YearLength' ) : YearLength = 365.25 * 24. * 60. * 60. #-- Year length (s) 90 91 # Set libIGCM and machine dependant values 87 92 if not 'Files' in config.keys() : config['Files'] = {} 88 89 def unDefined (char) : 90 if char in globals () : 91 if char == None : return True 92 else : return False 93 else : return True 94 95 ##-- Some physical constants 96 #-- Earth Radius 97 if not 'Ra' in locals () : Ra = 6366197.7236758135 98 #-- Gravity 99 if not 'Grav' in locals () : Grav = 9.81 100 #-- Ice volumic mass (kg/m3) in LIM3 101 if not 'ICE_rho_ice' in locals () : ICE_rho_ice = 917.0 102 #-- Snow volumic mass (kg/m3) in LIM3 103 if not 'ICE_rho_sno' in locals () : ICE_rho_sno = 330.0 104 #-- Water density in ice pounds in SI3 105 if not 'ICE_rho_pnd' in locals () : ICE_rho_pnd = 1000. 106 #-- Ocean water volumic mass (kg/m3) in NEMO 107 if not 'OCE_rho_liq' in locals () : OCE_rho_liq = 1026. 108 #-- Water volumic mass in atmosphere 109 if not 'ATM_rho' in locals () : ATM_rho = 1000. 110 #-- Water volumic mass in surface reservoirs 111 if not 'SRF_rho' in locals () : SRF_rho = 1000. 112 #-- Water volumic mass of rivers 113 if not 'RUN_rho' in locals () : RUN_rho = 1000. 114 #-- Year length 115 if not 'YearLength' in locals () : YearLength = 365.25 * 24. * 60. * 60. 116 117 config['Physics'] = { 'Ra':Ra, 'Grav':Grav, 'ICE_rho_ice':ICE_rho_ice, 'ICE_rho_sno':ICE_rho_sno, 'OCE_rho_liq':OCE_rho_liq, 'ICE_rho_pnd':ICE_rho_pnd, 118 'ATM_rho':ATM_rho, 'SRF_rho':SRF_rho, 'RUN_rho':RUN_rho} 93 94 config['Physics'] = { 'Ra':Ra, 'Grav':Grav, 'ICE_rho_ice':ICE_rho_ice, 'ICE_rho_sno':ICE_rho_sno, 'OCE_rho_liq':OCE_rho_liq, 'ATM_rho':ATM_rho, 'SRF_rho':SRF_rho, 'RUN_rho':RUN_rho} 119 95 120 121 122 # Where do we run ? 123 SysName, NodeName, Release, Version, Machine = os.uname() 124 TGCC = ( 'irene' in NodeName ) 125 IDRIS = ( 'jeanzay' in NodeName ) 126 127 ## Set site specific libIGCM directories, and other specific stuff 128 if TGCC : 129 CPU = subprocess.getoutput ( 'lscpu | grep "Model name"' ) 130 if "Intel(R) Xeon(R) Platinum" in CPU : Machine = 'irene' 131 if "AMD" in CPU : Machine = 'irene-amd' 132 133 ARCHIVE = subprocess.getoutput ( f'ccc_home --cccstore -d {Project} -u {User}' ) 134 STORAGE = subprocess.getoutput ( f'ccc_home --cccwork -d {Project} -u {User}' ) 135 SCRATCHDIR = subprocess.getoutput ( f'ccc_home --cccscratch -d {Project} -u {User}' ) 136 R_IN = os.path.join ( subprocess.getoutput ( f'ccc_home --cccwork -d igcmg -u igcmg'), 'IGCM') 137 rebuild = os.path.join ( subprocess.getoutput ( f'ccc_home --ccchome -d igcmg -u igcmg'), 'Tools', Machine, 'rebuild_nemo', 'bin', 'rebuild_nemo' ) 138 139 ## Specific to run at TGCC. 140 # Needed before importing a NetCDF library (netCDF4, xarray, cmds, etc ...) 141 import mpi4py 142 mpi4py.rc.initialize = False 143 144 ## Creates output directory 145 TmpDir = os.path.join ( subprocess.getoutput ( 'ccc_home --cccscratch' ), f'WATER_{JobName}_{YearBegin}_{YearEnd}' ) 146 147 if IDRIS : 148 raise Exception ("Pour IDRIS : repertoires et chemins a definir") 149 150 config['System'] = {'SysName':SysName, 'NodeName':NodeName, 'Release':Release, 'Version':Version,'Machine':Machine, 'TGCC':TGCC,'IDRIS':IDRIS, 'CPU':CPU, 151 'Program' : "Generated by : " + str(sys.argv), 152 'HOSTNAME' : platform.node (), 'LOGNAME' : os.getlogin (), 153 'Python' : f'{platform.python_version ()}', 154 'OS' : f'{platform.system ()} release : {platform.release ()} hardware : {platform.machine ()}', 155 'SVN_Author' : "$Author$", 156 'SVN_Date' : "$Date$", 157 'SVN_Revision' : "$Revision$", 158 'SVN_Id' : "$Id$", 159 'SVN_HeadURL' : "$HeadURL$"} 96 with open ('SetLibIGCM.py') as f: exec ( f.read() ) 97 config['Files']['TmpDir'] = TmpDir 160 98 161 99 if libIGCM : … … 167 105 import xarray as xr 168 106 169 # Output file 170 if FileOut == None : 171 FileOut = f'OCE_waterbudget_{JobName}_{YearBegin}_{YearEnd}.out' 172 config['Files']['FileOut'] = FileOut 107 # Output file with water budget diagnostics 108 if wu.unDefined ( 'FileOut' ) : FileOut = f'OCE_waterbudget_{JobName}_{YearBegin}_{YearEnd}.out' 109 config['Files']['FileOut'] = FileOut 173 110 174 111 f_out = open ( FileOut, mode = 'w' ) 175 176 # Function to print to stdout *and* output file 112 113 # Useful functions 114 def kg2Sv (val, rho=ATM_rho) : 115 '''From kg to Sverdrup''' 116 return val/dtime_sec*1.0e-6/rho 117 118 def kg2myear (val, rho=ATM_rho) : 119 '''From kg to m/year''' 120 return val/ATM_aire_sea_tot/rho/NbYear 121 122 def var2prt (var, small=False, rho=ATM_rho) : 123 if small : return var , kg2Sv(var, rho=rho)*1000., kg2myear(var, rho=rho)*1000. 124 else : return var , kg2Sv(var, rho=rho) , kg2myear(var, rho=rho) 125 126 def prtFlux (Desc, var, Form='F', small=False, rho=ATM_rho, width=15) : 127 if small : 128 if Form in ['f', 'F'] : ff=" {:12.4e} kg | {:12.4f} mSv | {:12.4f} mm/year " 129 if Form in ['e', 'E'] : ff=" {:12.4e} kg | {:12.4e} mSv | {:12.4e} mm/year " 130 else : 131 if Form in ['f', 'F'] : ff=" {:12.4e} kg | {:12.4f} Sv | {:12.4f} m/year " 132 if Form in ['e', 'E'] : ff=" {:12.4e} kg | {:12.4e} Sv | {:12.4e} m/year " 133 echo ( (' {:>{width}} = ' +ff).format (Desc, *var2prt(var, small, rho=rho), width=width ) ) 134 return None 135 177 136 def echo (string, end='\n') : 137 '''Function to print to stdout *and* output file''' 178 138 print ( str(string), end=end ) 179 139 sys.stdout.flush () … … 182 142 return None 183 143 184 ## Set libIGCM directories 185 R_OUT = os.path.join ( ARCHIVE , 'IGCM_OUT') 186 R_BUF = os.path.join ( SCRATCHDIR, 'IGCM_OUT') 187 188 L_EXP = os.path.join (TagName, SpaceName, ExperimentName, JobName) 189 R_SAVE = os.path.join ( R_OUT, L_EXP ) 190 R_BUFR = os.path.join ( R_BUF, L_EXP ) 191 POST_DIR = os.path.join ( R_BUF, L_EXP, 'Out' ) 192 REBUILD_DIR = os.path.join ( R_BUF, L_EXP, 'REBUILD' ) 193 R_BUF_KSH = os.path.join ( R_BUFR, 'Out' ) 194 R_FIGR = os.path.join ( STORAGE, 'IGCM_OUT', L_EXP ) 195 196 #if os.path.isdir (TmpDir) : shutil.rmtree ( TmpDir ) 197 if not os.path.isdir (TmpDir) : os.mkdir (TmpDir) 198 TmpDirOCE = os.path.join (TmpDir, 'OCE') 199 TmpDirICE = os.path.join (TmpDir, 'ICE') 200 if not os.path.exists (TmpDirOCE) : os.mkdir (TmpDirOCE ) 201 if not os.path.exists (TmpDirICE) : os.mkdir (TmpDirICE ) 144 ### Set libIGCM directories 145 if wu.unDefined ('R_OUT' ) : R_OUT = os.path.join ( ARCHIVE , 'IGCM_OUT' ) 146 if wu.unDefined ('R_BUF' ) : R_BUF = os.path.join ( SCRATCHDIR, 'IGCM_OUT' ) 147 if wu.unDefined ('L_EXP' ) : L_EXP = os.path.join (TagName, SpaceName, ExperimentName, JobName) 148 if wu.unDefined ('R_SAVE' ) : R_SAVE = os.path.join ( R_OUT, L_EXP ) 149 if wu.unDefined ('R_BUFR' ) : R_BUFR = os.path.join ( R_BUF, L_EXP ) 150 if wu.unDefined ('POST_DIR' ) : POST_DIR = os.path.join ( R_BUFR, 'Out' ) 151 if wu.unDefined ('REBUILD_DIR') : REBUILD_DIR = os.path.join ( R_BUFR, 'REBUILD' ) 152 if wu.unDefined ('R_BUF_KSH' ) : R_BUF_KSH = os.path.join ( R_BUFR, 'Out' ) 153 if wu.unDefined ('R_FIGR' ) : R_FIGR = os.path.join ( STORAGE, 'IGCM_OUT', L_EXP ) 154 155 config['libIGCM'] = { '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 } 156 157 # Set directory to extract files 158 if wu.unDefined ( 'RunDir' ) : RunDir = os.path.join ( TmpDir, f'WATER_{JobName}_{YearBegin}_{YearEnd}' ) 159 config['Files']['RunDir'] = RunDir 160 161 if not os.path.isdir (RunDir) : os.makedirs (RunDir) 162 163 # Set directories to rebuild ocean and ice restart files 164 RunDirOCE = os.path.join (RunDir, 'OCE') 165 RunDirICE = os.path.join (RunDir, 'ICE') 166 if not os.path.exists (RunDirOCE) : os.mkdir (RunDirOCE ) 167 if not os.path.exists (RunDirICE) : os.mkdir (RunDirICE ) 202 168 203 169 echo (' ') 204 echo ( f'JobName : {JobName}' ) 205 echo (Comment) 206 echo ( f'Working in TMPDIR : {TmpDir}' ) 207 208 echo ( f'\nDealing with {L_EXP}' ) 209 210 #-- Model output directories 211 if Freq == "MO" : FreqDir = os.path.join ('Output' , 'MO' ) 212 if Freq == "SE" : FreqDir = os.path.join ('Analyse', 'SE' ) 213 if dir_OCE_his == None : 170 echo ( f'JobName : {JobName}' ) 171 echo ( f'Comment : {Comment}' ) 172 echo ( f'TmpDir : {TmpDir}' ) 173 echo ( f'RunDirOCE : {RunDirOCE}' ) 174 echo ( f'RunDirICE : {RunDirICE}' ) 175 176 echo ( f'\nDealing with {L_EXP}' ) 177 178 #-- Creates model output directory names 179 if Freq == "MO" : FreqDir = os.path.join ( 'Output' , 'MO' ) 180 if Freq == "SE" : FreqDir = os.path.join ( 'Analyse', 'SE' ) 181 if wu.unDefined ( 'dir_OCE_his' ) : 214 182 dir_OCE_his = os.path.join ( R_SAVE, "OCE", FreqDir ) 215 183 config['Files']['dir_OCE_his'] = dir_OCE_his 216 if dir_ICE_his == None:184 if wu.unDefined ( 'dir_ICE_his' ) : 217 185 dir_ICE_his = os.path.join ( R_SAVE, "ICE", FreqDir ) 218 186 config['Files']['dir_OCE_his'] = dir_OCE_his … … 222 190 echo ( f'{dir_ICE_his}' ) 223 191 224 #-- Files Names225 if Period == None:192 #-- Creates files names 193 if wu.unDefined ( 'Period ' ) : 226 194 if Freq == 'MO' : Period = f'{YearBegin}0101_{YearEnd}1231_1M' 227 195 if Freq == 'SE' : Period = f'SE_{YearBegin}0101_{YearEnd}1231_1M' 228 196 config['Files']['Period'] = Period 229 if FileCommon == None:197 if wu.unDefined ( 'FileCommon' ) : 230 198 FileCommon = f'{JobName}_{Period}' 231 199 config['Files']['FileCommon'] = FileCommon 232 200 233 if Title == None:201 if wu.unDefined ( 'Title' ) : 234 202 Title = f'{JobName} : {Freq} : {YearBegin}-01-01 - {YearEnd}-12-31' 235 203 config['Files']['Title'] = Title … … 237 205 238 206 echo ('\nOpen history files' ) 239 if file_OCE_his == None:207 if wu.unDefined ('file_OCE_his' ) : 240 208 file_OCE_his = os.path.join ( dir_OCE_his, f'{FileCommon}_grid_T.nc' ) 241 209 file_OCE_his = file_OCE_his 242 if file_OCE_sca == None:210 if wu.unDefined ('file_OCE_sca' ) : 243 211 file_OCE_sca = os.path.join ( dir_OCE_his, f'{FileCommon}_scalar.nc' ) 244 212 config['Files']['file_OCE_sca'] = file_OCE_sca 245 if file_ICE_his == None:213 if wu.unDefined ( 'file_ICE_hi' ) : 246 214 file_ICE_his = os.path.join ( dir_ICE_his, f'{FileCommon}_icemod.nc' ) 247 215 config['Files']['file_ICE_his'] = file_ICE_his … … 258 226 ## Compute run length 259 227 dtime = ( d_OCE_his.time_counter_bounds.max() - d_OCE_his.time_counter_bounds.min() ) 260 echo ( '\nRun length : {:8.2f} days'.format ( (dtime/np.timedelta64(1, "D")).values ))228 echo ( f'\nRun length : {(dtime/np.timedelta64(1, "D")).values:8.2f} days' ) 261 229 dtime_sec = (dtime/np.timedelta64(1, "s")).values.item() # Convert in seconds 262 230 263 231 ## Compute length of each period 264 232 dtime_per = ( d_OCE_his.time_counter_bounds[:,-1] - d_OCE_his.time_counter_bounds[:,0] ) 265 echo ( '\nPeriods lengths (days) : ')266 echo ( ' {:}'.format ( (dtime_per/np.timedelta64(1, "D")).values ))233 echo ( f'\nPeriods lengths (days) : ') 234 echo ( f' {(dtime_per/np.timedelta64(1, "D")).values}' ) 267 235 dtime_per_sec = (dtime_per/np.timedelta64(1, "s")).values # In seconds 268 236 dtime_per_sec = xr.DataArray (dtime_per_sec, dims=["time_counter", ], coords=[d_OCE_his.time_counter,] ) … … 271 239 ## Number of years 272 240 NbYear = dtime_sec / YearLength 241 273 242 #-- Open restart files 274 243 YearRes = YearBegin - 1 # Year of the restart of beginning of simulation … … 277 246 echo (f'Restart dates - Start : {YearRes}-12-31 / End : {YearEnd}-12-31 ') 278 247 279 if TarRestartPeriod_beg == None:248 if wu.unDefined ( 'TarRestartPeriod_beg' ) : 280 249 echo (f'Restart dates - Start : {YearRes}-12-31 / End : {YearEnd}-12-31 ') 281 250 TarRestartPeriod_beg = f'{YearPre}0101_{YearRes}1231' 282 251 config['Files']['TarRestartPeriod_beg'] = TarRestartPeriod_beg 283 252 284 if TarRestartPeriod_end == None:253 if wu.unDefined ( 'TarRestartPeriod_end' ) : 285 254 YearPre = YearBegin - PackFrequency # Year to find the tarfile of the restart of beginning of simulation 286 255 echo (f'Restart dates - Start : {YearRes}-12-31 / End : {YearEnd}-12-31 ') … … 288 257 config['Files']['TarRestartPeriod_end'] = TarRestartPeriod_end 289 258 290 if file_restart_beg == None : 291 file_restart_beg = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_beg}_restart.tar' ) 292 config['Files']['file_restart_beg'] = file_restart_beg 293 if file_restart_end == None : 294 file_restart_end = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_end}_restart.tar' ) 295 config['Files']['file_restart_end'] = file_restart_end 296 297 echo ( f'{file_restart_beg}' ) 298 echo ( f'{file_restart_end}' ) 299 300 if file_OCE_beg == None : 301 file_OCE_beg = f'{TmpDir}/OCE_{JobName}_{YearRes}1231_restart.nc' 259 if wu.unDefined ( 'tar_restart_beg' ) : 260 tar_restart_beg = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_beg}_restart.tar' ) 261 config['Files']['tar_restart_beg'] = tar_restart_beg 262 if wu.unDefined ( 'tar_restart_end' ) : 263 tar_restart_end = os.path.join ( R_SAVE, 'RESTART', f'{JobName}_{TarRestartPeriod_end}_restart.tar' ) 264 config['Files']['tar_restart_end'] = tar_restart_end 265 266 echo ( f'{tar_restart_beg}' ) 267 echo ( f'{tar_restart_end}' ) 268 269 # 270 if wu.unDefined ('tar_restart_beg_ATM' ) : tar_restart_beg_ATM = tar_restart_beg 271 if wu.unDefined ('tar_restart_beg_DYN' ) : tar_restart_beg_DYN = tar_restart_beg 272 if wu.unDefined ('tar_restart_beg_SRF' ) : tar_restart_beg_SRF = tar_restart_beg 273 if wu.unDefined ('tar_restart_beg_RUN' ) : tar_restart_beg_RUN = tar_restart_beg 274 if wu.unDefined ('tar_restart_beg_OCE' ) : tar_restart_beg_OCE = tar_restart_beg 275 if wu.unDefined ('tar_restart_beg_ICE' ) : tar_restart_beg_ICE = tar_restart_beg 276 277 if wu.unDefined ('tar_restart_end_ATM' ) : tar_restart_end_ATM = tar_restart_end 278 if wu.unDefined ('tar_restart_end_DYN' ) : tar_restart_end_DYN = tar_restart_end 279 if wu.unDefined ('tar_restart_end_SRF' ) : tar_restart_end_SRF = tar_restart_end 280 if wu.unDefined ('tar_restart_end_RUN' ) : tar_restart_end_RUN = tar_restart_end 281 if wu.unDefined ('tar_restart_end_OCE' ) : tar_restart_end_OCE = tar_restart_end 282 if wu.unDefined ('tar_restart_end_ICE' ) : tar_restart_end_ICE = tar_restart_end 283 284 if wu.unDefined ( 'file_OCE_beg' ) : 285 file_OCE_beg = f'{RunDir}/OCE_{JobName}_{YearRes}1231_restart.nc' 302 286 config['Files']['file_OCE_beg'] = file_OCE_beg 303 if file_OCE_end == None:304 file_OCE_end = f'{ TmpDir}/OCE_{JobName}_{YearEnd}1231_restart.nc'287 if wu.unDefined ( 'file_OCE_end' ) : 288 file_OCE_end = f'{RunDir}/OCE_{JobName}_{YearEnd}1231_restart.nc' 305 289 config['Files']['file_OCE_end'] = file_OCE_end 306 if file_ICE_beg == None:307 file_ICE_beg = f'{ TmpDir}/ICE_{JobName}_{YearRes}1231_restart_icemod.nc'290 if wu.unDefined ( 'file_ICE_beg' ) : 291 file_ICE_beg = f'{RunDir}/ICE_{JobName}_{YearRes}1231_restart_icemod.nc' 308 292 config['Files']['file_ICE_beg'] = file_ICE_beg 309 if file_ICE_end == None:310 file_ICE_end = f'{ TmpDir}/ICE_{JobName}_{YearEnd}1231_restart_icemod.nc'293 if wu.unDefined ( 'file_ICE_end' ) : 294 file_ICE_end = f'{RunDir}/ICE_{JobName}_{YearEnd}1231_restart_icemod.nc' 311 295 config['Files']['file_ICE_end'] = file_ICE_end 312 296 … … 321 305 #ndomain_opa = d_zfile.attrs['DOMAIN_number_total'] 322 306 #d_zfile.close () 323 ndomain_opa = subprocess.getoutput ( f'ls {zfile}_*.nc | wc -l' ) .format()307 ndomain_opa = subprocess.getoutput ( f'ls {zfile}_*.nc | wc -l' ) #.format() 324 308 return int (ndomain_opa) 325 309 326 if not os.path.exists ( file_OCE_beg) : 327 echo ( f'Extracting {file_OCE_beg}' ) 328 base_file_OCE_beg = os.path.basename (file_OCE_beg) 329 if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart_0000.nc') ) : 330 command = f'cd {TmpDirOCE} ; tar xf {file_restart_beg} OCE_{JobName}_{YearRes}1231_restart_*.nc' 331 echo ( command ) 332 os.system ( command ) 333 echo ('extract ndomain' ) 334 ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearRes}1231_restart') ) 335 command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearRes}1231_restart {ndomain_opa} ; mv {base_file_OCE_beg} {TmpDir}' 336 echo ( command ) 337 os.system ( command ) 338 echo ( f'Rebuild done : {file_OCE_beg}') 339 340 if not os.path.exists ( file_OCE_end) : 341 echo ( f'Extracting {file_OCE_end}' ) 342 base_file_OCE_end = os.path.basename (file_OCE_end) 343 if not os.path.exists ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart_0000.nc') ): 344 command = f'cd {TmpDirOCE} ; tar xf {file_restart_end} OCE_{JobName}_{YearEnd}1231_restart_*.nc' 345 echo ( command ) 346 os.system ( command ) 347 echo ('extract ndomain' ) 348 ndomain_opa = get_ndomain ( os.path.join (TmpDirOCE, f'OCE_{JobName}_{YearEnd}1231_restart') ) 349 command = f'cd {TmpDirOCE} ; {rebuild} OCE_{JobName}_{YearEnd}1231_restart {ndomain_opa} ; mv {base_file_OCE_end} {TmpDir}' 350 echo ( command ) 351 os.system ( command ) 352 echo ( f'Rebuild done : {file_OCE_end}') 353 354 if not os.path.exists ( file_ICE_beg) : 355 echo ( f'Extracting {file_ICE_beg}' ) 356 base_file_ICE_beg = os.path.basename (file_ICE_beg) 357 if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod_0000.nc') ): 358 command = f'cd {TmpDirICE} ; tar xf {file_restart_beg} ICE_{JobName}_{YearRes}1231_restart_icemod_*.nc' 359 echo ( command ) 360 os.system ( command ) 361 echo ('extract ndomain' ) 362 ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') ) 363 command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearRes}1231_restart_icemod {ndomain_opa} ; mv {base_file_ICE_beg} {TmpDir} ' 364 echo ( command ) 365 os.system ( command ) 366 echo ( f'Rebuild done : {file_OCE_beg}') 367 368 if not os.path.exists ( file_ICE_end ) : 369 echo ( f'Extracting {file_ICE_end}' ) 370 base_file_ICE_end = os.path.basename (file_ICE_end) 371 if not os.path.exists ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearEnd}1231_restart_icemod_0000.nc') ): 372 command = f'cd {TmpDirICE} ; tar xf {file_restart_end} ICE_{JobName}_{YearEnd}1231_restart_icemod_*.nc' 373 echo ( command ) 374 os.system ( command ) 375 echo ('extract ndomain' ) 376 ndomain_opa = get_ndomain ( os.path.join (TmpDirICE, f'ICE_{JobName}_{YearRes}1231_restart_icemod') ) 377 command = f'cd {TmpDirICE} ; {rebuild} ICE_{JobName}_{YearEnd}1231_restart_icemod {ndomain_opa} ; mv {base_file_ICE_end} {TmpDir}' 378 echo ( command ) 379 os.system ( command ) 380 echo ( f'Rebuild done : {file_ICE_end}') 310 def extract_and_rebuild ( file_name=file_OCE_beg, tar_restart=tar_restart_end, RunDirComp=RunDirOCE, ErrorCount=ErrorCount ) : 311 '''Extract restart file from tar. Rebuild ocean files if needed''' 312 echo ( f'----------') 313 if os.path.exists ( file_name ) : 314 echo ( f'-- File ready : {file_name = }' ) 315 else : 316 echo ( f'-- Extracting {file_name = }' ) 317 base_resFile = Path (file_name).stem # basename, and remove suffix 318 # Try to extract the rebuilded file 319 if os.path.exists ( tar_restart ) : 320 command = f'cd {RunDirComp} ; tar xf {tar_restart} {base_resFile}.nc' 321 echo ( command ) 322 try : 323 os.system ( command ) 324 except : 325 if not os.path.exists ( os.path.join (RunDir, f'{base_resFile}_0000.nc') ): 326 command = f'cd {RunDirComp} ; tar xf {tar_restart_end} {base_file}_*.nc' 327 echo ( command ) 328 ierr = os.system ( command ) 329 if ierr == 0 : echo ( f'tar done : {base_resFile}.nc') 330 else : raise Exception ( f'command failed : {command}' ) 331 echo ( f'extract ndomain' ) 332 ndomain_opa = get_ndomain ( os.path.join (RunDir, f'{base_file}') ) 333 command = f'cd {RunDirComp} ; {rebuild} {base_resFile} {ndomain_opa} ; mv {base_resFile}.nc {RunDir}' 334 echo ( command ) 335 ierr = os.system ( command ) 336 if ierr == 0 : echo ( f'Rebuild done : {base_resFile}.nc') 337 else : raise Exception ( f'command failed : {command}' ) 338 else : 339 echo ( f'tar done : {base_resFile}') 340 command = f'cd {RunDirComp} ; mv {base_resFile}.nc {RunDir}' 341 ierr = os.system ( command ) 342 if ierr == 0 : echo ( f'command done : {command}' ) 343 else : raise Exception ( f'command failed : {command = }' ) 344 else : 345 echo ( f'****** Tar restart file {tar_restart = } not found ' ) 346 if ContinueOnError : 347 ErrorCount += 1 348 else : 349 raise Exception ( f'****** tar file not found {tar_restart = } - Stopping' ) 350 return ErrorCount 351 352 353 ErrorCount += extract_and_rebuild ( file_name=file_OCE_beg, tar_restart=tar_restart_beg, RunDirComp=RunDirOCE ) 354 ErrorCount += extract_and_rebuild ( file_name=file_OCE_end, tar_restart=tar_restart_end, RunDirComp=RunDirOCE ) 355 ErrorCount += extract_and_rebuild ( file_name=file_ICE_beg, tar_restart=tar_restart_beg, RunDirComp=RunDirICE ) 356 ErrorCount += extract_and_rebuild ( file_name=file_ICE_end, tar_restart=tar_restart_end, RunDirComp=RunDirICE ) 381 357 382 358 echo ('Opening OCE and ICE restart files') 383 359 if NEMO == 3.6 : 384 d_OCE_beg = xr.open_dataset ( os.path.join ( TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()385 d_OCE_end = xr.open_dataset ( os.path.join ( TmpDir, file_OCE_end), decode_times=False, decode_cf=True).squeeze()386 d_ICE_beg = xr.open_dataset ( os.path.join ( TmpDir, file_ICE_beg), decode_times=False, decode_cf=True).squeeze()387 d_ICE_end = xr.open_dataset ( os.path.join ( TmpDir, file_ICE_end), decode_times=False, decode_cf=True).squeeze()360 d_OCE_beg = xr.open_dataset ( os.path.join (RunDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() 361 d_OCE_end = xr.open_dataset ( os.path.join (RunDir, file_OCE_end), decode_times=False, decode_cf=True).squeeze() 362 d_ICE_beg = xr.open_dataset ( os.path.join (RunDir, file_ICE_beg), decode_times=False, decode_cf=True).squeeze() 363 d_ICE_end = xr.open_dataset ( os.path.join (RunDir, file_ICE_end), decode_times=False, decode_cf=True).squeeze() 388 364 if NEMO == 4.0 or NEMO == 4.2 : 389 d_OCE_beg = xr.open_dataset ( os.path.join ( TmpDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()390 d_OCE_end = xr.open_dataset ( os.path.join ( TmpDir, file_OCE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()391 d_ICE_beg = xr.open_dataset ( os.path.join ( TmpDir, file_ICE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()392 d_ICE_end = xr.open_dataset ( os.path.join ( TmpDir, file_ICE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze()393 394 ## 365 d_OCE_beg = xr.open_dataset ( os.path.join (RunDir, file_OCE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() 366 d_OCE_end = xr.open_dataset ( os.path.join (RunDir, file_OCE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() 367 d_ICE_beg = xr.open_dataset ( os.path.join (RunDir, file_ICE_beg), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() 368 d_ICE_end = xr.open_dataset ( os.path.join (RunDir, file_ICE_end), decode_times=False, decode_cf=True, drop_variables=['y', 'x']).squeeze() 369 370 ## Write the full configuration 395 371 config_out = open (FullIniFile, 'w') 396 372 config.write (config_out ) … … 473 449 OCE_sum_e3tn_end = OCE_stock_int ( OCE_e3tn_end * OCE_msk3) 474 450 475 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))451 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' ) 476 452 dOCE_ssh_vol = ( OCE_sum_ssh_end - OCE_sum_ssh_beg ) 477 453 dOCE_ssh_mas = dOCE_ssh_vol * OCE_rho_liq 478 454 479 455 if NEMO == 3.6 : 480 echo ( 'OCE_sum_e3tn_beg = {:12.6e} m^3 - OCE_sum_e3tn_end = {:12.6e} m^3'.format (OCE_sum_e3tn_beg, OCE_sum_e3tn_end))456 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' ) 481 457 dOCE_e3tn_vol = ( OCE_sum_e3tn_end - OCE_sum_e3tn_beg ) 482 458 dOCE_e3tn_mas = dOCE_e3tn_vol * OCE_rho_liq … … 484 460 dOCE_vol_wat = dOCE_ssh_vol ; dOCE_mas_wat = dOCE_ssh_mas 485 461 486 echo ( 'dOCE vol = {:12.3e} m^3'.format ( dOCE_vol_wat))487 echo ( 'dOCE ssh = {:12.3e} m '.format ( dOCE_vol_wat/OCE_aire_tot))462 echo ( f'dOCE vol = {dOCE_vol_wat :12.3e} m^3' ) 463 echo ( f'dOCE ssh = {dOCE_vol_wat/OCE_aire_tot:12.3e} m ' ) 488 464 prtFlux ( 'dOCE mass ', dOCE_mas_wat, 'e' ) 489 465 490 466 if NEMO == 3.6 : 491 echo ( 'dOCE e3tn vol = {:12.3e} m^3'.format ( dOCE_e3tn_vol))467 echo ( f'dOCE e3tn vol = {dOCE_e3tn_vol:12.3e} m^3' ) 492 468 prtFlux ( 'dOCE e3tn mass', dOCE_e3tn_mas, 'e' ) 493 469 … … 547 523 dSEA_mas_wat = dOCE_mas_wat + dICE_mas_wat 548 524 549 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))550 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))551 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))552 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))553 554 echo ( 'dICE_vol_ice = {:12.3e} m^3'.format (dICE_vol_ice))555 echo ( 'dICE_vol_sno = {:12.3e} m^3'.format (dICE_vol_sno))556 echo ( 'dICE_vol_pnd = {:12.3e} m^3'.format (dICE_vol_pnd))557 echo ( 'dICE_mas_ice = {:12.3e} m^3'.format (dICE_mas_ice))558 echo ( 'dICE_mas_sno = {:12.3e} m^3'.format (dICE_mas_sno))559 echo ( 'dICE_mas_pnd = {:12.3e} m^3'.format (dICE_mas_pnd))560 echo ( 'dICE_mas_fzl = {:12.3e} m^3'.format (dICE_mas_fzl))525 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' ) 526 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' ) 527 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' ) 528 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' ) 529 530 echo ( f'dICE_vol_ice = {dICE_vol_ice:12.3e} m^3' ) 531 echo ( f'dICE_vol_sno = {dICE_vol_sno:12.3e} m^3' ) 532 echo ( f'dICE_vol_pnd = {dICE_vol_pnd:12.3e} m^3' ) 533 echo ( f'dICE_mas_ice = {dICE_mas_ice:12.3e} m^3' ) 534 echo ( f'dICE_mas_sno = {dICE_mas_sno:12.3e} m^3' ) 535 echo ( f'dICE_mas_pnd = {dICE_mas_pnd:12.3e} m^3' ) 536 echo ( f'dICE_mas_fzl = {dICE_mas_fzl:12.3e} m^3' ) 561 537 562 538 echo ( '\n------------------------------------------------------------') 563 echo ( ' Variation du contenu en eau ocean + glace' )539 echo ( 'Change in water content (ocean + ice) ' ) 564 540 prtFlux ( 'dMass (ocean)', dSEA_mas_wat, 'e', True ) 565 541 … … 584 560 585 561 echo ( '\n------------------------------------------------------------------------------------' ) 586 echo ( '-- Checks in NEMO - from budget_modipsl.sh (Cl ement Rousset)' )562 echo ( '-- Checks in NEMO - from budget_modipsl.sh (Clément Rousset)' ) 587 563 588 564 # Read variable and computes integral over space and time … … 658 634 prtFlux (' WFXSNW_PRE ', ICE_mas_wfxsnw_pre, 'e', True) 659 635 prtFlux (' WFXSUB_ERR ', ICE_mas_wfxsub_err, 'e', True) 660 prtFlux (' EVAP_OCE ', OCE_mas_evap_oce , 'e' )636 prtFlux (' EVAP_OCE ', OCE_mas_evap_oce , 'e' ) 661 637 prtFlux (' EVAP_ICE ', ICE_mas_evap_ice , 'e', True) 662 638 prtFlux (' SNOW_OCE ', OCE_mas_snow_oce , 'e', True) 663 639 prtFlux (' SNOW_ICE ', OCE_mas_snow_ice , 'e', True) 664 prtFlux (' RAIN ', OCE_mas_rain , 'e' )640 prtFlux (' RAIN ', OCE_mas_rain , 'e' ) 665 641 prtFlux (' FWB ', OCE_mas_fwb , 'e', True) 666 642 prtFlux (' SSR ', OCE_mas_ssr , 'e', True) … … 705 681 706 682 echo ( '\n------------------------------------------------------------------------------------' ) 707 echo ( '-- C alculs dans la note PDF de Clement')683 echo ( '-- Computations in the PDF note of Clément Rousset') 708 684 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 )) 709 685 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 ))
Note: See TracChangeset
for help on using the changeset viewer.