#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed May 26 20:32:51 EDT 2021 if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports # if __name__ == '__main__': tt.check('end import') # #start from here if len(sys.argv) == 1: ifiles = [ 'ts.mdrtrop.past1000.0850-1849.maxmissing150.ens10.nc', 'ts.mdrtrop.historical.1850-2005.maxmissing11.ens47.nc', 'ts.mdrtrop.rcp85.2006-2100.ens39.nc', ] ofile = f'ts.mdrtrop.past1000histrcp85.0850-2100.ens{n:02d}.nc' else: ifiles = sys.argv[1:] ofile = __file__.replace('.py', '.nc') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() models_in_common = xr.open_dataset(ifiles[0]).model.values for ifile in ifiles[1:]: models_in_common = list(set(models_in_common)&set(xr.open_dataset(ifile).model.values)) models_in_common.sort(key=lambda x: x.upper()) """ models_in_common = [ m for m in xr.open_dataset(ifiles[0]).model.values if m in xr.open_dataset(ifiles[1]).model.values and m in xr.open_dataset(ifiles[2]).model.values ] """ n = len(models_in_common) print(f'{n} models in common:', models_in_common) print('concat...') dss = [] for ifile in ifiles: dss.append(xr.open_dataset(ifile).sel(model=models_in_common)) ds = xr.concat(dss, dim='year') print('saving...') ds.to_netcdf(ofile) print('[saved]:', ofile) if __name__ == '__main__': #from wyconfig import * #my plot settings #savefig if 'savefig' in sys.argv: figname = __file__.replace('.py', f'.png') wysavefig(figname) tt.check(f'**Done**') plt.show()