def date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs): '''Creates a fixed frequency Datetime range. Alias for ``ak.Datetime(pd.date_range(args))``. Subject to size limit imposed by client.maxTransferBytes. Parameters ---------- start : str or datetime-like, optional Left bound for generating dates. end : str or datetime-like, optional Right bound for generating dates. periods : int, optional Number of periods to generate. freq : str or DateOffset, default 'D' Frequency strings can have multiples, e.g. '5H'. See timeseries.offset_aliases for a list of frequency aliases. tz : str or tzinfo, optional Time zone name for returning localized DatetimeIndex, for example 'Asia/Hong_Kong'. By default, the resulting DatetimeIndex is timezone-naive. normalize : bool, default False Normalize start/end dates to midnight before generating date range. name : str, default None Name of the resulting DatetimeIndex. closed : {None, 'left', 'right'}, optional Make the interval closed with respect to the given frequency to the 'left', 'right', or both sides (None, the default). **kwargs For compatibility. Has no effect on the result. Returns ------- rng : DatetimeIndex Notes ----- Of the four parameters ``start``, ``end``, ``periods``, and ``freq``, exactly three must be specified. If ``freq`` is omitted, the resulting ``DatetimeIndex`` will have ``periods`` linearly spaced elements between ``start`` and ``end`` (closed on both sides). To learn more about the frequency strings, please see `this link <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__. ''' return Datetime( pd_date_range(start, end, periods, freq, tz, normalize, name, closed, **kwargs))
def make_fileList(reanalysis, variable, date_range, grid=None): ''' Generates a list of filepaths for a given reanalysis and variable for a date range. The code deals with CFSR* spanning two products. reanalysis - name of reanalysis variable - my standard variable name date_range - tuple of dates in format (yyyymmdd, yyyymmdd) returns - filepath string ''' from pandas import date_range as pd_date_range import datetime as dt filelist = [] for date in pd_date_range(date_range[0], date_range[1], freq='M'): if (reanalysis == 'CFSR') & (date >= dt.datetime(2011,1,1)): filelist.append(make_filepath('CFSR2', variable, date, grid=grid)) else: filelist.append(make_filepath(reanalysis, variable, date, grid=grid)) return filelist
import xarray as xr import numpy as np from pandas import date_range as pd_date_range from cdo import * cdo = Cdo() # PET function exec(open("./src/ai_fao.py").read()) # hydrological time hydro_time = pd_date_range("1982-01-01", "2016-12-31", freq="D") hydro_time = hydro_time[~((hydro_time.day == 29) & (hydro_time.month == 2))] hydro_time2 = pd_date_range("1982-01-01", "2016-12-31", freq="M") # pet piscopet = xr.open_dataset(cdo.cat(input="./data/raw/PET/*.nc")) #piscopet = piscopet.rename({"__xarray_dataarray_variable__":"pet"}) piscopet = piscopet.sel(time=slice("1981-09-01", "2016-08-31")) piscopet = piscopet.isel( time=~piscopet.time.dt.strftime('%m-%d').isin("02-29")) piscopet["time"] = hydro_time piscopet_anual = piscopet.resample(time="1Y").sum() piscopet_anual = piscopet_anual.reindex( longitude=np.arange(piscopet.longitude.values[1], piscopet.longitude.values[-1], 0.008), latitude=np.arange(piscopet.latitude.values[1], piscopet.latitude.values[-1], -0.008), method="nearest") # precp piscop = xr.open_dataset("./data/raw/PISCO/PISCOpd.nc")
piscotx = xr.open_dataset("./data/raw/PISCO/PISCOdtx_v1.1.nc") piscotn = xr.open_dataset("./data/raw/PISCO/PISCOdtn_v1.1.nc") # building lat grid pisco_lat = xr.DataArray( np.tile(piscotx["latitude"].values, (145, 1)).transpose(), coords=[piscotx["latitude"].values, piscotx["longitude"].values], dims=["latitude", "longitude"]) # getting time values as Julian day """ # as gridded, not efficient [np.full((145, 202), i) for i in dates] np.stack([np.full((145, 202), 1), np.full((145, 202), 2), np.full((145, 202), 3)], axis=2).shape """ dates = pd_date_range('1981-01-01', "2016-12-31", freq='D') dates = np.array([int(i.strftime("%j")) for i in dates]) # computing PET for year in range(1981, 2017): dates = pd_date_range(str(year) + '-01-01', str(year) + "-12-31", freq='D') dates = np.array([int(i.strftime("%j")) for i in dates]) xr.apply_ufunc(hargreaves_samani, piscotx.tx.loc[str(year) + '-01-01':str(year) + "-12-31"], piscotn.tn.loc[str(year) + '-01-01':str(year) + "-12-31"], dates, pisco_lat, vectorize=True,