Skip to content

Instantly share code, notes, and snippets.

@j08lue
Created January 25, 2018 12:48
Show Gist options
  • Save j08lue/34498cf17b176d15933e778278ba2921 to your computer and use it in GitHub Desktop.
Save j08lue/34498cf17b176d15933e778278ba2921 to your computer and use it in GitHub Desktop.
xarray 0.10.0 time decoding round-off error
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from xarray.conventions import decode_cf_datetime"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0.10.0'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xr.__version__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#!curl -O ftp://ftp.chg.ucsb.edu/pub/org/chg/products/CHIRPS-2.0/global_daily/netcdf/p05/by_month/chirps-v2.0.1983.01.days_p05.nc"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"infile = 'chirps-v2.0.1983.01.days_p05.nc'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.DataArray 'time' (time: 31)>\n",
"array(['1982-12-31T23:59:59.560122368', '1983-01-02T00:00:05.712183296',\n",
" '1983-01-03T00:00:03.274309632', '1983-01-04T00:00:00.836435968',\n",
" '1983-01-05T00:00:06.988496896', '1983-01-06T00:00:04.550623232',\n",
" '1983-01-07T00:00:02.112749568', '1983-01-07T23:59:59.674875904',\n",
" '1983-01-09T00:00:05.826936832', '1983-01-10T00:00:03.389063168',\n",
" '1983-01-11T00:00:00.951189504', '1983-01-12T00:00:07.103250432',\n",
" '1983-01-13T00:00:04.665376768', '1983-01-14T00:00:02.227503104',\n",
" '1983-01-14T23:59:59.789629440', '1983-01-16T00:00:05.941690368',\n",
" '1983-01-17T00:00:03.503816704', '1983-01-18T00:00:01.065943040',\n",
" '1983-01-19T00:00:07.218003968', '1983-01-20T00:00:04.780130304',\n",
" '1983-01-21T00:00:02.342256640', '1983-01-21T23:59:59.904382976',\n",
" '1983-01-23T00:00:06.056443904', '1983-01-24T00:00:03.618570240',\n",
" '1983-01-25T00:00:01.180696576', '1983-01-26T00:00:07.332757504',\n",
" '1983-01-27T00:00:04.894883840', '1983-01-28T00:00:02.457010176',\n",
" '1983-01-29T00:00:00.019136512', '1983-01-30T00:00:06.171197440',\n",
" '1983-01-31T00:00:03.733323776'], dtype='datetime64[ns]')\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 1982-12-31T23:59:59.560122368 ...\n",
"Attributes:\n",
" standard_name: time\n",
" axis: T"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = xr.open_dataset(infile, decode_times=True)\n",
"ds.time"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['1982-12-31T23:59:59.560122368', '1983-01-02T00:00:05.712183296',\n",
" '1983-01-03T00:00:03.274309632', '1983-01-04T00:00:00.836435968',\n",
" '1983-01-05T00:00:06.988496896', '1983-01-06T00:00:04.550623232',\n",
" '1983-01-07T00:00:02.112749568', '1983-01-07T23:59:59.674875904',\n",
" '1983-01-09T00:00:05.826936832', '1983-01-10T00:00:03.389063168',\n",
" '1983-01-11T00:00:00.951189504', '1983-01-12T00:00:07.103250432',\n",
" '1983-01-13T00:00:04.665376768', '1983-01-14T00:00:02.227503104',\n",
" '1983-01-14T23:59:59.789629440', '1983-01-16T00:00:05.941690368',\n",
" '1983-01-17T00:00:03.503816704', '1983-01-18T00:00:01.065943040',\n",
" '1983-01-19T00:00:07.218003968', '1983-01-20T00:00:04.780130304',\n",
" '1983-01-21T00:00:02.342256640', '1983-01-21T23:59:59.904382976',\n",
" '1983-01-23T00:00:06.056443904', '1983-01-24T00:00:03.618570240',\n",
" '1983-01-25T00:00:01.180696576', '1983-01-26T00:00:07.332757504',\n",
" '1983-01-27T00:00:04.894883840', '1983-01-28T00:00:02.457010176',\n",
" '1983-01-29T00:00:00.019136512', '1983-01-30T00:00:06.171197440',\n",
" '1983-01-31T00:00:03.733323776'], dtype='datetime64[ns]')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = xr.open_dataset(infile, decode_times=False)\n",
"decode_cf_datetime(ds.time.values, units=ds.time.units, calendar=ds.time.calendar)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Variable'>\n",
"float32 time(time)\n",
" units: days since 1980-1-1 0:0:0\n",
" standard_name: time\n",
" calendar: gregorian\n",
" axis: T\n",
"unlimited dimensions: \n",
"current shape = (31,)\n",
"filling on, default _FillValue of 9.969209968386869e+36 used"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import netCDF4 as nc4\n",
"dd = nc4.Dataset(infile)\n",
"tvar = dd.variables['time']\n",
"tvar"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([datetime.datetime(1983, 1, 1, 0, 0),\n",
" datetime.datetime(1983, 1, 2, 0, 0),\n",
" datetime.datetime(1983, 1, 3, 0, 0),\n",
" datetime.datetime(1983, 1, 4, 0, 0),\n",
" datetime.datetime(1983, 1, 5, 0, 0),\n",
" datetime.datetime(1983, 1, 6, 0, 0),\n",
" datetime.datetime(1983, 1, 7, 0, 0),\n",
" datetime.datetime(1983, 1, 8, 0, 0),\n",
" datetime.datetime(1983, 1, 9, 0, 0),\n",
" datetime.datetime(1983, 1, 10, 0, 0),\n",
" datetime.datetime(1983, 1, 11, 0, 0),\n",
" datetime.datetime(1983, 1, 12, 0, 0),\n",
" datetime.datetime(1983, 1, 13, 0, 0),\n",
" datetime.datetime(1983, 1, 14, 0, 0),\n",
" datetime.datetime(1983, 1, 15, 0, 0),\n",
" datetime.datetime(1983, 1, 16, 0, 0),\n",
" datetime.datetime(1983, 1, 17, 0, 0),\n",
" datetime.datetime(1983, 1, 18, 0, 0),\n",
" datetime.datetime(1983, 1, 19, 0, 0),\n",
" datetime.datetime(1983, 1, 20, 0, 0),\n",
" datetime.datetime(1983, 1, 21, 0, 0),\n",
" datetime.datetime(1983, 1, 22, 0, 0),\n",
" datetime.datetime(1983, 1, 23, 0, 0),\n",
" datetime.datetime(1983, 1, 24, 0, 0),\n",
" datetime.datetime(1983, 1, 25, 0, 0),\n",
" datetime.datetime(1983, 1, 26, 0, 0),\n",
" datetime.datetime(1983, 1, 27, 0, 0),\n",
" datetime.datetime(1983, 1, 28, 0, 0),\n",
" datetime.datetime(1983, 1, 29, 0, 0),\n",
" datetime.datetime(1983, 1, 30, 0, 0),\n",
" datetime.datetime(1983, 1, 31, 0, 0)], dtype=object)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nc4.num2date(tvar[:], units=tvar.units, calendar=tvar.calendar)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:fd]",
"language": "python",
"name": "conda-env-fd-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment