|
#!/usr/bin/env python
|
|
##############################################################################################
|
|
#climatological interpolation of missing years from monthly boundary condition datasets
|
|
#(e.g., January data interpolates Januaries, February data interpolates Februaries, etc.)
|
|
#input arguments: file1 year1 year2
|
|
# ex: python interp_clim.py T42_dms_surf_emiss_2015_SSP126.nc 2015 2020
|
|
#output file pattern: 's/year1/newyear/' file1
|
|
#python 3.6 / libraries: xarray 0.16.0, cftime 1.2.1, numpy 1.17.3, netCDF4 1.4.2, dask 2.6.0
|
|
#
|
|
#note that NaN values are filled in this procedure. Modify line 26 to change behavior
|
|
#
|
|
#$Id: interp_clim.py,v 1.4 2020/09/11 18:45:18 klobas Exp $
|
|
#########################J. Eric Klobas - Harvard University - klobas@huarp.harvard.edu#######
|
|
|
|
import numpy as np
|
|
import xarray as xr
|
|
import sys
|
|
import re
|
|
|
|
fileone=sys.argv[1]
|
|
yearone=sys.argv[2]
|
|
yeartwo=sys.argv[3]
|
|
|
|
yearrange=np.arange(int(yearone),int(yeartwo),1)[1:]
|
|
print('interpolating years: '+str(yearrange)+' from '+fileone+' and '+re.sub(str(yearone),str(yeartwo),fileone))
|
|
workcat = xr.merge([xr.open_dataset(fileone),xr.open_dataset(re.sub(str(yearone),str(yeartwo),fileone))]).fillna(0)
|
|
clim=workcat.groupby('time.month')
|
|
interpdict={}
|
|
|
|
for label in list(clim.groups):
|
|
if label != 2:
|
|
timelist=[str(year)+'-'+'%02d' % label +'-16' for year in yearrange]
|
|
else:
|
|
timelist=[str(year)+'-'+'%02d' % label +'-15' for year in yearrange]
|
|
interpval=workcat.isel(time=clim.groups[label]).interp(time=timelist)
|
|
interpdict[str(label)] = interpval
|
|
|
|
interpdset=xr.merge([interpdict[key] for key in interpdict])
|
|
|
|
for year in yearrange:
|
|
interpdset.isel(time=interpdset.groupby('time.year').groups[year]).to_netcdf(re.sub(str(yearone),str(year),fileone))
|