#!/usr/bin/env python

## Just run code in recipes, use it carefully
## example: use netcdf4 to make cache file. Since python is faster than ncl.
#;DIAG_NORCPM; RUNTHESECODES: print('    no codes run here.')

import xarray as xr
from os.path import exists
from sys import exit
infile_u = "TBI_CoEx_tro10Atl5m_ng2l15dA_U200_ensmean_1980-2000.nc"
infile_v = "TBI_CoEx_tro10Atl5m_ng2l15dA_V200_ensmean_1980-2000.nc"
outfile = "TBI_CoEx_tro10Atl5m_ng2l15dA_UV_DJF_2000-2020.nc"
years = list(range(2000,2020+1))
months = [12,1,2]
if exists(outfile): exit()
ds = xr.open_mfdataset([infile_u,infile_v])
ds = ds.sel(time=ds.time.dt.month.isin(months))
clids = ds.mean(dim='time')
ds = ds.sel(time=ds.time.dt.year.isin(years))
ds = ds.mean(dim='time')
anods = ds - clids

ds.to_netcdf(outfile)
anods.to_netcdf('ano_'+outfile)


