#!/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 expname = 'CTL1860_noleap_tigercpu_intelmpi_18_576PE' data_name = 't_ref' year_start_ctl = 11 n_years = 5 ens = range(1, 31) n_ens = len(ens) time_new = xr.cftime_range(start='0001-01', periods=n_years*12, freq='MS', calendar='noleap') ofile = f'{expname}.{data_name}.ens.nc' def get_data(expname, years=None): '''get data from model output''' # ifiles if years is None: ifiles = glob.glob(f'/tigress/wenchang/MODEL_OUT/{expname}/POSTP/????0101.atmos_month.nc') ifiles.sort() years = [int( s.split('/')[-1][0:4] ) for s in ifiles] else: ifiles = [f'/tigress/wenchang/MODEL_OUT/{expname}/POSTP/{year:04d}0101.atmos_month.nc' for year in years] # open dataset with xr.open_mfdataset(ifiles) as ds: da = ds[data_name] # new ds ds = da.to_dataset(name=data_name) return ds if __name__ == '__main__': if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() # record the start time tformat = '%Y-%m-%d %H:%M:%S' t0 = datetime.now() print('[time start]:', t0.strftime(tformat)) dss = [] for en in ens: print(f'en = {en:02d}') years = range(year_start_ctl+en-1, year_start_ctl+en-1+5) ds = get_data(expname=expname, years=years) ds = ds.assign_coords(time=time_new) dss.append(ds) # concatenated dataset ds = xr.concat(dss, dim=pd.Index(ens, name='en')) ds['en'].attrs['long_name'] = 'ensemble member' # save the new dataset print('saving to netcdf ...') encoding = {data_name: {'dtype': 'float32', 'zlib': True, 'complevel': 1}} ds.load().to_netcdf(ofile, encoding=encoding) print('[saved]:', ofile) # the end t1 = datetime.now() dt = t1 - t0 print('[time end]:', t1.strftime(tformat)) print('[time used]:', f'{dt.seconds:,} seconds') print()