Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mmartini-usgs/5b87a6bf1c28f7ea7ad3c992137b1cd0 to your computer and use it in GitHub Desktop.
Save mmartini-usgs/5b87a6bf1c28f7ea7ad3c992137b1cd0 to your computer and use it in GitHub Desktop.
xarray loses hours of the day
{
"cells": [
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "import datetime as dt\nimport xarray as xr\n",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "infile = \"E:\\\\data\\\\Matanzas\\\\WellTest2017\\\\Signature\\\\python2\\\\1108sigall.nc\"\n",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# first lets see what kind of time we have for the main axis\n# if xarray - MUST set decode_time=False to see the coordinate time variable units\nds = xr.open_dataset(infile, decode_times=False)\ntimeunits = ds['time'].attrs['units']\nprint(ds['time'][0:5])\nds.close()\nvars2omit = {'EPIC_time','EPIC_time2'}\nds = xr.open_dataset(infile, drop_variables=vars2omit)\nprint(ds['time'][0:5])\nds.close()",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "<xarray.DataArray 'time' (time: 5)>\narray([ 0. , 0.2503, 0.5003, 0.7503, 1.0001])\nCoordinates:\n * time (time) float64 0.0 0.2503 0.5003 0.7503 1.0\nAttributes:\n units: seconds since 2017-9-25 15:0:0.063300 0:00\n<xarray.DataArray 'time' (time: 5)>\narray(['2017-09-25T00:00:00.063300000', '2017-09-25T00:00:00.313600000',\n '2017-09-25T00:00:00.563599999', '2017-09-25T00:00:00.813600000',\n '2017-09-25T00:00:01.063400000'], dtype='datetime64[ns]')\nCoordinates:\n * time (time) datetime64[ns] 2017-09-25T00:00:00.063300 ...\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "vars2omit = {'EPIC_time','EPIC_time2'}\nds = xr.open_dataset(infile,decode_times=True,drop_variables=vars2omit,mask_and_scale=False)\n\nds_1h = ds.resample('H','time',how='mean',keep_attrs=True)\n",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(ds_1h['time'])",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "<xarray.DataArray 'time' (time: 75)>\narray(['2017-09-25T00:00:00.000000000', '2017-09-25T01:00:00.000000000',\n '2017-09-25T02:00:00.000000000', '2017-09-25T03:00:00.000000000',\n '2017-09-25T04:00:00.000000000', '2017-09-25T05:00:00.000000000',\n '2017-09-25T06:00:00.000000000', '2017-09-25T07:00:00.000000000',\n '2017-09-25T08:00:00.000000000', '2017-09-25T09:00:00.000000000',\n '2017-09-25T10:00:00.000000000', '2017-09-25T11:00:00.000000000',\n '2017-09-25T12:00:00.000000000', '2017-09-25T13:00:00.000000000',\n '2017-09-25T14:00:00.000000000', '2017-09-25T15:00:00.000000000',\n '2017-09-25T16:00:00.000000000', '2017-09-25T17:00:00.000000000',\n '2017-09-25T18:00:00.000000000', '2017-09-25T19:00:00.000000000',\n '2017-09-25T20:00:00.000000000', '2017-09-25T21:00:00.000000000',\n '2017-09-25T22:00:00.000000000', '2017-09-25T23:00:00.000000000',\n '2017-09-26T00:00:00.000000000', '2017-09-26T01:00:00.000000000',\n '2017-09-26T02:00:00.000000000', '2017-09-26T03:00:00.000000000',\n '2017-09-26T04:00:00.000000000', '2017-09-26T05:00:00.000000000',\n '2017-09-26T06:00:00.000000000', '2017-09-26T07:00:00.000000000',\n '2017-09-26T08:00:00.000000000', '2017-09-26T09:00:00.000000000',\n '2017-09-26T10:00:00.000000000', '2017-09-26T11:00:00.000000000',\n '2017-09-26T12:00:00.000000000', '2017-09-26T13:00:00.000000000',\n '2017-09-26T14:00:00.000000000', '2017-09-26T15:00:00.000000000',\n '2017-09-26T16:00:00.000000000', '2017-09-26T17:00:00.000000000',\n '2017-09-26T18:00:00.000000000', '2017-09-26T19:00:00.000000000',\n '2017-09-26T20:00:00.000000000', '2017-09-26T21:00:00.000000000',\n '2017-09-26T22:00:00.000000000', '2017-09-26T23:00:00.000000000',\n '2017-09-27T00:00:00.000000000', '2017-09-27T01:00:00.000000000',\n '2017-09-27T02:00:00.000000000', '2017-09-27T03:00:00.000000000',\n '2017-09-27T04:00:00.000000000', '2017-09-27T05:00:00.000000000',\n '2017-09-27T06:00:00.000000000', '2017-09-27T07:00:00.000000000',\n '2017-09-27T08:00:00.000000000', '2017-09-27T09:00:00.000000000',\n '2017-09-27T10:00:00.000000000', '2017-09-27T11:00:00.000000000',\n '2017-09-27T12:00:00.000000000', '2017-09-27T13:00:00.000000000',\n '2017-09-27T14:00:00.000000000', '2017-09-27T15:00:00.000000000',\n '2017-09-27T16:00:00.000000000', '2017-09-27T17:00:00.000000000',\n '2017-09-27T18:00:00.000000000', '2017-09-27T19:00:00.000000000',\n '2017-09-27T20:00:00.000000000', '2017-09-27T21:00:00.000000000',\n '2017-09-27T22:00:00.000000000', '2017-09-27T23:00:00.000000000',\n '2017-09-28T00:00:00.000000000', '2017-09-28T01:00:00.000000000',\n '2017-09-28T02:00:00.000000000'], dtype='datetime64[ns]')\nCoordinates:\n * time (time) datetime64[ns] 2017-09-25 2017-09-25T01:00:00 ...\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "ds.close()",
"execution_count": 6,
"outputs": []
},
{
"metadata": {},
"cell_type": "raw",
"source": "Gist: https://gist.github.com/mmartini-usgs/5b87a6bf1c28f7ea7ad3c992137b1cd0"
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "5b87a6bf1c28f7ea7ad3c992137b1cd0",
"data": {
"description": "xarray loses hours of the day",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/5b87a6bf1c28f7ea7ad3c992137b1cd0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment