#ncks -d south_north,77 -d west_east,65 -v PREC

import xarray as xr
from glob import glob
from dask.distributed import Client
import time

bdir = '/home/disk/rocinante/DATA/temp/WRF/raw/gfdl-cm3/wrf2d/'
odir = '/home/disk/rocinante/DATA/temp/WRF/raw/gfdl-cm3/wrf2d/seatac/makina/'
#c = Client(n_workers=5, threads_per_worker=2, memory_limit='1GB')
#client = Client('127.0.0.1:8770', n_workers=5, threads_per_worker=2)
#client


for yr in range(1970,2100):
    #for mn in ['{:02d}'.format(x) for x in range(1,13)]:
        
    dec = int(yr / 10) * 10
    reg_in = '{}/{}s/{}/*d02_{}-*.nc'.format(bdir, dec, yr, yr)
    reg_out = '{}/seatac_{}.nc'.format(odir, yr)
    print(reg_in)
    
    #print('Processing {}-{}: '.format(yr, mn))
    print('Processing {}: '.format(yr))
    print('--to: {}'.format(reg_out))


    def m1():
        
        data = glob(reg_in)
        arr = [xr.open_dataset(f).sel(south_north=77, west_east=65)[['Times', 'PREC']] for f in data]
        print('--read complete')    
        mg = xr.concat(arr, dim='Time')
        print('--merge complete')
        mg.to_netcdf(reg_out)

    def m3():
        
        data = glob(reg_in)
        arr = [xr.open_dataset(f).sel(south_north=77, west_east=65)[['Times', 'PREC']] for f in data]
        print('--read complete')    
        mg = xr.auto_combine(arr, concat_dim='Time')
        print('--merge complete')
        mg.to_netcdf(reg_out)
        

    def m2():
        
        mg = xr.open_mfdataset(reg_in, concat_dim='Time')
        print('--read complete')
        mg = mg.sel(south_north=77, west_east=65)[['Times', 'PREC']]
        print('--select complete')
        mg.to_netcdf(reg_out)
    

    st=time.time()
    #m1()
    #m2()
    m3()
    ed=time.time()
    print('--write complete: {}\n\n'.format(ed-st))

    
    
#client.close()
