#!/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_PACE_P_ANOM_U850_ensmean_1980-2021.nc"
infile_v = "TBI_CoEx_PACE_P_ANOM_V850_ensmean_1980-2021.nc"
outfile = "TBI_CoEx_PACE_P_ANOM_UV_MAM_2000-2020.nc"
years = list(range(2000,2020+1))
months = [3,4,5]
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)


