#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Sep 18 21:10:30 EDT 2019 from datetime import datetime import os.path, sys, os, glob #import matplotlib.pyplot as plt import xarray as xr, numpy as np, pandas as pd #import geoxarray print() expnames = [ 'Pinatubo_PI_ens_noleap', 'Agung_PI_ens_noleap', 'StMaria_PI_ens_noleap', 'Chichon_PI_ens_noleap', ] data_name_o = 'HI' # data name of output def get_hi(expname, en, years=None): '''get sea ice thickness from model output''' # ifiles if years is None: ifiles = glob.glob(f'/tigress/wenchang/MODEL_OUT/{expname}/en{en:02d}/POSTP/????0101.ice_month.nc') ifiles.sort() years = [int( s.split('/')[-1][0:4] ) for s in ifiles] else: ifiles = [f'/tigress/wenchang/MODEL_OUT/{expname}/en{en:02d}/POSTP/{year:04d}0101.ice_month.nc' for year in years] # open dataset with xr.open_mfdataset(ifiles) as ds: da = ds['HI'] # new dataset ds = da.to_dataset(name=data_name_o) return ds def get_hi_ens(expname): '''get sea ice thickness ensembles from a given experiment name''' ens = range(1, 31) # ofile ofile = f'{expname}.{data_name_o}.nc' if os.path.exists(ofile): print('[exists]:', ofile) return dss = [] for en in ens: print(f'en = {en:02d}') ds = get_hi(expname=expname, en=en, years=None) dss.append(ds) # concatenate all ensemble members ds = xr.concat(dss, dim=pd.Index(ens, name='en')) ds['en'].attrs['long_name'] = 'ensemble member' # save to netcdf ds.to_netcdf(ofile) print('[saved]:', ofile) if __name__ == '__main__': # record the start time tformat = '%Y-%m-%d %H:%M:%S' t0 = datetime.now() print('[time start]:', t0.strftime(tformat)) for expname in expnames: print(expname, data_name_o) get_hi_ens(expname) # the end t1 = datetime.now() dt = t1 - t0 print('[time end]:', t1.strftime(tformat)) print('[time used]:', f'{dt.seconds:,} seconds') print()