import numpy as np
import xarray as xr
import dask
import netCDF4
from dask.diagnostics import ProgressBar
#import matplotlib.pyplot as plt

print('reading data...')
ens1 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0012.zonmean.nc',decode_times=False)
time1 = (np.arange(ens1.time.shape[0])/12.)-1
ens1.time.values=time1
ens1=ens1.chunk()
ens2 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0016.zonmean.nc',decode_times=False)
time2 = (np.arange(ens2.time.shape[0])/12.)-1
ens2.time.values=time2
ens2=ens2.chunk()
ens3 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0014.zonmean.nc',decode_times=False)
time3 = (np.arange(ens3.time.shape[0])/12.)-1
ens3.time.values=time3
ens3=ens3.chunk()
ens4 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0029.zonmean.nc',decode_times=False)
time4 = (np.arange(ens4.time.shape[0])/12.)-1
ens4.time.values=time4
ens4=ens4.chunk()
ens5 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0017.zonmean.nc',decode_times=False)
time5 = (np.arange(ens5.time.shape[0])/12.)-1
ens5.time.values=time5
ens5=ens5.chunk()
ens6 = xr.open_dataset('bwma1850.f09.test1.cam.h0.0022.zonmean.nc',decode_times=False)
time6 = (np.arange(ens6.time.shape[0])/12.)-1
ens6.time.values=time6
ens6=ens6.chunk()
print('done')

print('Concatenating along record dimension')
data = xr.concat([ens1,ens2,ens3,ens4,ens5,ens6],dim='record')
print('done')

print('writing combined data to new netcdf file')
with ProgressBar():
    data.to_netcdf('bwma1850.f09.test1.cam.h0.year_pre_ensembre.zonmean.nc',unlimited_dims=('time'))

print('done')
