#!/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 = "N2_FF_detrend_U200_ensmean_1980-2020.nc"
infile_v = "N2_FF_detrend_V200_ensmean_1980-2020.nc"
outfile = "N2_FF_detrend_UV_JJA_1980-2000.nc"
years = list(range(1980,2000+1))
months = [6,7,8]
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)


