#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Nov 6 00:06:34 EST 2019 import sys, os.path, os, datetime import xarray as xr, numpy as np, pandas as pd #import matplotlib.pyplot as plt volcs = ['Pinatubo', 'Chichon', 'Agung', 'StMaria'] data_name = 'SIE' n_years = 5 time_new = xr.cftime_range(start='0001-01', periods=n_years*12, freq='MS', calendar='noleap') ofile = f'Volc.{data_name}.ens.nc' if __name__ == '__main__': if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() tformat = '%Y-%m-%d %H:%M:%S' t0 = datetime.datetime.now() print('[start]:', t0.strftime(tformat)) dss = [] for volc in volcs: print(volc) ifile = f'{volc}_PI_ens_noleap.{data_name}.nc' ds = xr.open_dataset(ifile).assign_coords(time=time_new) dss.append(ds) ds = xr.concat(dss, dim=pd.Index(volcs, name='volc')) # save the dataset ds.to_netcdf(ofile) print('[saved]:', ofile) t1 = datetime.datetime.now() print('[total time used]:', f'{(t1-t0).seconds:,} seconds') print('[end]:', t1.strftime(tformat)) print()