|
| 1 | +# qsub -I -P xv83 -l ncpus=48 -l mem=190GB -l jobfs=4GB -l walltime=1:00:00 -l storage=gdata/xv83+gdata/oi10+gdata/dk92+gdata/hh5+gdata/rr3+gdata/al33+gdata/fs38+gdata/xp65+gdata/p73 |
| 2 | +# qsub -I -q express -P xv83 -l ncpus=48 -l mem=190GB -l jobfs=4GB -l walltime=1:00:00 -l storage=gdata/xv83+gdata/dk92+gdata/hh5+gdata/xp65+gdata/p73 |
| 3 | + |
| 4 | +# import sys to access script arguments (experiment, ensemble, first_year, last_year) |
| 5 | +import sys |
| 6 | + |
| 7 | +# These are fixed (correspond to my AA simulation) |
| 8 | +model='ACCESS-ESM1-5' |
| 9 | +experiment='historical' |
| 10 | +year_start=1850 |
| 11 | +num_years=10 |
| 12 | + |
| 13 | +# 1. Load packages |
| 14 | + |
| 15 | +# Import os for makedirs/isfile/environ |
| 16 | +import os |
| 17 | +os.environ["PYTHONWARNINGS"] = "ignore" |
| 18 | + |
| 19 | +# Load dask |
| 20 | +from dask.distributed import Client |
| 21 | + |
| 22 | +# Load xarray for N-dimensional arrays |
| 23 | +import xarray as xr |
| 24 | + |
| 25 | +# Load traceback to print exceptions |
| 26 | +import traceback |
| 27 | + |
| 28 | +# # Load xmip for preprocessing (trying to get consistent metadata for making matrices down the road) |
| 29 | +# from xmip.preprocessing import combined_preprocessing |
| 30 | + |
| 31 | + |
| 32 | +# 2. Define some functions |
| 33 | +# (to avoid too much boilerplate code) |
| 34 | +print("Defining functions") |
| 35 | + |
| 36 | +def time_window_strings(year_start, num_years): |
| 37 | + """ |
| 38 | + return strings for start_time and end_time |
| 39 | + """ |
| 40 | + # start_time is first second of year_start |
| 41 | + start_time = f'{year_start}' |
| 42 | + # end_time is last second of last_year |
| 43 | + end_time = f'{year_start + num_years - 1}' |
| 44 | + # Return the weighted average |
| 45 | + return start_time, end_time |
| 46 | + |
| 47 | +def open_my_dataset(paths): |
| 48 | + ds = xr.open_mfdataset( |
| 49 | + paths, |
| 50 | + chunks={'time':-1, 'st_ocean':-1}, |
| 51 | + concat_dim="time", |
| 52 | + compat='override', |
| 53 | + preprocess=None, |
| 54 | + engine='netcdf4', |
| 55 | + data_vars='minimal', |
| 56 | + coords='minimal', |
| 57 | + combine='nested', |
| 58 | + parallel=True, |
| 59 | + join='outer', |
| 60 | + attrs_file=None, |
| 61 | + combine_attrs='override', |
| 62 | + ) |
| 63 | + return ds |
| 64 | + |
| 65 | +# Create directory on scratch to save the data |
| 66 | +scratchdatadir = '/scratch/xv83/TMIP/data' |
| 67 | +AAdatadir = '/scratch/xv83/bp3051/access-esm/archive/andersonacceleration_test-n10-5415f621/' |
| 68 | +gdatadatadir = '/g/data/xv83/TMIP/data' |
| 69 | + |
| 70 | +# Depends on time window |
| 71 | +start_time, end_time = time_window_strings(year_start, num_years) |
| 72 | +start_time_str = f'Jan{start_time}' |
| 73 | +end_time_str = f'Dec{end_time}' |
| 74 | + |
| 75 | +# Simulation years from AA output |
| 76 | +simyears = range(10) # AA cycles of 10 years |
| 77 | + |
| 78 | +print("Starting client") |
| 79 | + |
| 80 | +# This `if` statement is required in scripts (not required in Jupyter) |
| 81 | +if __name__ == '__main__': |
| 82 | + client = Client(n_workers=44, threads_per_worker=1) #, memory_limit='16GB') # Note: with 1thread/worker cannot plot thetao. Maybe I need to understand why? |
| 83 | + # print ensemble/member |
| 84 | + print(f"\nProcessing AA output") |
| 85 | + # directory to save the data to (as NetCDF) |
| 86 | + outputdir = f'{scratchdatadir}/{model}/{experiment}/AA/{start_time_str}-{end_time_str}' |
| 87 | + print("Creating directory: ", outputdir) |
| 88 | + os.makedirs(outputdir, exist_ok=True) |
| 89 | + print(" averaging data from: ", AAdatadir) |
| 90 | + print(" to be saved in: ", outputdir) |
| 91 | + # # tx_trans |
| 92 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-tx_trans-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 93 | + # try: |
| 94 | + # print(" Loading tx_trans") |
| 95 | + # tx_trans_ds = open_my_dataset(paths) |
| 96 | + # print(" selecting time window") |
| 97 | + # tx_trans_sel = tx_trans_ds.sel(time=slice(start_time, end_time)) |
| 98 | + # print(" averaging") |
| 99 | + # tx_trans = tx_trans_sel["tx_trans"].weighted(tx_trans_sel.time.dt.days_in_month).mean(dim="time") |
| 100 | + # print("\ntx_trans: ", tx_trans) |
| 101 | + # print(" saving to: ", f'{outputdir}/tx_trans.nc') |
| 102 | + # tx_trans.to_netcdf(f'{outputdir}/tx_trans.nc', compute=True) |
| 103 | + # except Exception: |
| 104 | + # print(f'Error processing tx_trans') |
| 105 | + # print(traceback.format_exc()) |
| 106 | + # # ty_trans |
| 107 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-ty_trans-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 108 | + # try: |
| 109 | + # print(" Loading ty_trans") |
| 110 | + # ty_trans_ds = open_my_dataset(paths) |
| 111 | + # print(" selecting time window") |
| 112 | + # ty_trans_sel = ty_trans_ds.sel(time=slice(start_time, end_time)) |
| 113 | + # print(" averaging") |
| 114 | + # ty_trans = ty_trans_sel["ty_trans"].weighted(ty_trans_sel.time.dt.days_in_month).mean(dim="time") |
| 115 | + # print("\nty_trans: ", ty_trans) |
| 116 | + # print(" saving to: ", f'{outputdir}/ty_trans.nc') |
| 117 | + # ty_trans.to_netcdf(f'{outputdir}/ty_trans.nc', compute=True) |
| 118 | + # except Exception: |
| 119 | + # print(f'Error processing ty_trans') |
| 120 | + # print(traceback.format_exc()) |
| 121 | + # # tx_trans_gm |
| 122 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-tx_trans_gm-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 123 | + # try: |
| 124 | + # print(" Loading tx_trans_gm") |
| 125 | + # tx_trans_gm_ds = open_my_dataset(paths) |
| 126 | + # print(" selecting time window") |
| 127 | + # tx_trans_gm_sel = tx_trans_gm_ds.sel(time=slice(start_time, end_time)) |
| 128 | + # print(" averaging") |
| 129 | + # tx_trans_gm = tx_trans_gm_sel["tx_trans_gm"].weighted(tx_trans_gm_sel.time.dt.days_in_month).mean(dim="time") |
| 130 | + # print("\ntx_trans_gm: ", tx_trans_gm) |
| 131 | + # print(" saving to: ", f'{outputdir}/tx_trans_gm.nc') |
| 132 | + # tx_trans_gm.to_netcdf(f'{outputdir}/tx_trans_gm.nc', compute=True) |
| 133 | + # except Exception: |
| 134 | + # print(f'Error processing tx_trans_gm') |
| 135 | + # print(traceback.format_exc()) |
| 136 | + # # ty_trans_gm |
| 137 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-ty_trans_gm-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 138 | + # try: |
| 139 | + # print(" Loading ty_trans_gm") |
| 140 | + # ty_trans_gm_ds = open_my_dataset(paths) |
| 141 | + # print(" selecting time window") |
| 142 | + # ty_trans_gm_sel = ty_trans_gm_ds.sel(time=slice(start_time, end_time)) |
| 143 | + # print(" averaging") |
| 144 | + # ty_trans_gm = ty_trans_gm_sel["ty_trans_gm"].weighted(ty_trans_gm_sel.time.dt.days_in_month).mean(dim="time") |
| 145 | + # print("\nty_trans_gm: ", ty_trans_gm) |
| 146 | + # print(" saving to: ", f'{outputdir}/ty_trans_gm.nc') |
| 147 | + # ty_trans_gm.to_netcdf(f'{outputdir}/ty_trans_gm.nc', compute=True) |
| 148 | + # except Exception: |
| 149 | + # print(f'Error processing ty_trans_gm') |
| 150 | + # print(traceback.format_exc()) |
| 151 | + # # tx_trans_submeso |
| 152 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-tx_trans_submeso-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 153 | + # try: |
| 154 | + # print(" Loading tx_trans_submeso") |
| 155 | + # tx_trans_submeso_ds = open_my_dataset(paths) |
| 156 | + # print(" selecting time window") |
| 157 | + # tx_trans_submeso_sel = tx_trans_submeso_ds.sel(time=slice(start_time, end_time)) |
| 158 | + # print(" averaging") |
| 159 | + # tx_trans_submeso = tx_trans_submeso_sel["tx_trans_submeso"].weighted(tx_trans_submeso_sel.time.dt.days_in_month).mean(dim="time") |
| 160 | + # print("\ntx_trans_submeso: ", tx_trans_submeso) |
| 161 | + # print(" saving to: ", f'{outputdir}/tx_trans_submeso.nc') |
| 162 | + # tx_trans_submeso.to_netcdf(f'{outputdir}/tx_trans_submeso.nc', compute=True) |
| 163 | + # except Exception: |
| 164 | + # print(f'Error processing tx_trans_submeso') |
| 165 | + # print(traceback.format_exc()) |
| 166 | + # # ty_trans_submeso |
| 167 | + # paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-3d-ty_trans_submeso-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 168 | + # try: |
| 169 | + # print(" Loading ty_trans_submeso") |
| 170 | + # ty_trans_submeso_ds = open_my_dataset(paths) |
| 171 | + # print(" selecting time window") |
| 172 | + # ty_trans_submeso_sel = ty_trans_submeso_ds.sel(time=slice(start_time, end_time)) |
| 173 | + # print(" averaging") |
| 174 | + # ty_trans_submeso = ty_trans_submeso_sel["ty_trans_submeso"].weighted(ty_trans_submeso_sel.time.dt.days_in_month).mean(dim="time") |
| 175 | + # print("\nty_trans_submeso: ", ty_trans_submeso) |
| 176 | + # print(" saving to: ", f'{outputdir}/ty_trans_submeso.nc') |
| 177 | + # ty_trans_submeso.to_netcdf(f'{outputdir}/ty_trans_submeso.nc', compute=True) |
| 178 | + # except Exception: |
| 179 | + # print(f'Error processing ty_trans_submeso') |
| 180 | + # print(traceback.format_exc()) |
| 181 | + # MLD |
| 182 | + paths = [f'{AAdatadir}/output00{simyear}/ocean/ocean-2d-mld-1monthly-mean-ym_185{simyear}_01.nc' for simyear in simyears] |
| 183 | + try: |
| 184 | + print("Loading mlotst data") |
| 185 | + mlotst_ds = open_my_dataset(paths) |
| 186 | + print("\nmlotst_ds: ", mlotst_ds) |
| 187 | + print("Slicing mlotst for the time period") |
| 188 | + mlotst_ds_sel = mlotst_ds.sel(time=slice(start_time, end_time)) |
| 189 | + print("Averaging mlotst (mean of the yearly maximum of monthly data)") |
| 190 | + mlotst_yearlymax = mlotst_ds_sel.groupby("time.year").max(dim="time") |
| 191 | + print("\nmlotst_yearlymax: ", mlotst_yearlymax) |
| 192 | + mlotst = mlotst_yearlymax.mean(dim="year") |
| 193 | + print("\nmlotst: ", mlotst) |
| 194 | + print("Saving mlotst to: ", f'{outputdir}/mlotst.nc') |
| 195 | + mlotst.to_netcdf(f'{outputdir}/mlotst.nc', compute=True) |
| 196 | + mlotst_max = mlotst_ds_sel.max(dim="time") |
| 197 | + print("\nmlotst_max: ", mlotst_max) |
| 198 | + print("Saving mlotst_max to: ", f'{outputdir}/mlotst_max.nc') |
| 199 | + mlotst_max.to_netcdf(f'{outputdir}/mlotst_max.nc', compute=True) |
| 200 | + except Exception: |
| 201 | + print(f'Error processing {model} {member} mlotst') |
| 202 | + print(traceback.format_exc()) |
| 203 | + client.close() |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
0 commit comments