{ "cells": [ { "cell_type": "markdown", "id": "e32f671b-8fb2-40e8-aa4d-fbd17e2eae90", "metadata": {}, "source": [ "# Subsetting Data\n", "\n", "## Using existing xarray functions\n", "Tracks are loaded as an `xarray.Dataset` which have lots of built in methods for subsetting data.\n", "e.g. for indexing see [xarray indexing](https://docs.xarray.dev/en/stable/user-guide/indexing.html).\n", "\n", "For more specific selection of data, the best method is to use\n", "[xarray.Dataset.where](https://docs.xarray.dev/en/stable/generated/xarray.Dataset.where.html)\n", "with the argument `drop=True`. e.g." ] }, { "cell_type": "code", "execution_count": 1, "id": "5e345c67-31c2-443b-af6f-36024ff3f5eb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "array([120.5 , 119. , 119. , 119.25, 119.5 , 118.75, 118.5 , 118.25,\n", " 118.25, 118.25, 118.75, 119.25, 119.25, 119.75, 120. , 120. ,\n", " 119.5 , 119.25, 118.25, 117.5 , 117. , 117. , 116.75, 116.75,\n", " 117.5 , 119.25, 121. , 123.5 , 127.5 , 130.25, 131.25, 149.5 ,\n", " 151.5 , 154. , 156.25, 158.5 , 159.5 , 160. , 160. , 160. ,\n", " 158.25, 156. , 154.25, 153.25, 152.75, 152.5 , 152.5 , 153. ,\n", " 153.75, 154.75, 156. , 55.25, 54.25, 52.75, 54. , 55. ,\n", " 52. , 51. , 52. , 51.5 , 50.75, 50.25, 50.5 , 50.75,\n", " 49.75, 50. , 50.5 , 51. , 51.75, 52.25, 53. , 53.25,\n", " 52.75, 54.5 , 53. , 53.75, 53.5 , 53.25, 53.25, 52.75,\n", " 52.5 , 52.25, 52.5 , 53.25, 54.25, 55.5 , 56.75, 58.25,\n", " 59.5 , 59.25, 58.75, 58.25, 57.75, 57.5 , 57.25, 57.5 ,\n", " 58.5 , 60.25, 62.25])\n", "Coordinates:\n", " * record (record) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 \n", "\n", "\n", "array([120.5 , 119. , 119. , 119.25, 119.5 , 118.75, 118.5 , 118.25,\n", " 118.25, 118.25, 118.75, 119.25, 119.25, 119.75, 120. , 120. ,\n", " 119.5 , 119.25, 118.25, 117.5 , 117. , 117. , 116.75, 116.75,\n", " 117.5 , 119.25, 121. , 123.5 , 127.5 , 130.25, 131.25, 149.5 ,\n", " 151.5 , 154. , 156.25, 158.5 , 159.5 , 160. , 160. , 160. ,\n", " 158.25, 156. , 154.25, 153.25, 152.75, 152.5 , 152.5 , 153. ,\n", " 153.75, 154.75, 156. , 60.25, 62.25])\n", "Coordinates:\n", " * record (record) int64 0 1 2 3 4 5 6 7 8 9 ... 44 45 46 47 48 49 50 97 98\n" ] } ], "source": [ "import huracanpy\n", "\n", "tracks = huracanpy.load(huracanpy.example_csv_file)\n", "\n", "# Select all points with longitude > 60\n", "print(tracks.lon, \"\\n\")\n", "tracks_subset = tracks.where(tracks.lon > 60, drop=True)\n", "print(tracks_subset.lon)" ] }, { "cell_type": "markdown", "id": "25225038-3c80-4fdd-a9fd-0e23e5f8fd93", "metadata": {}, "source": [ "## Selecting times\n", "Generally the `time` array will be loaded in as an\n", "[np.datetime64](https://numpy.org/doc/stable/reference/arrays.datetime.html)\n", "array. This means it doesn't work to compare it with the standard `datetime`" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c593b94-c1dc-4dde-bc1e-4271ec06b1ba", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "array(['1980-01-06T06:00:00.000000000', '1980-01-06T12:00:00.000000000',\n", " '1980-01-06T18:00:00.000000000', '1980-01-07T00:00:00.000000000',\n", " '1980-01-07T06:00:00.000000000', '1980-01-07T12:00:00.000000000',\n", " '1980-01-07T18:00:00.000000000', '1980-01-08T00:00:00.000000000',\n", " '1980-01-08T06:00:00.000000000', '1980-01-08T12:00:00.000000000',\n", " '1980-01-08T18:00:00.000000000', '1980-01-09T00:00:00.000000000',\n", " '1980-01-09T06:00:00.000000000', '1980-01-09T12:00:00.000000000',\n", " '1980-01-09T18:00:00.000000000', '1980-01-10T00:00:00.000000000',\n", " '1980-01-10T06:00:00.000000000', '1980-01-10T12:00:00.000000000',\n", " '1980-01-10T18:00:00.000000000', '1980-01-11T00:00:00.000000000',\n", " '1980-01-11T06:00:00.000000000', '1980-01-11T12:00:00.000000000',\n", " '1980-01-11T18:00:00.000000000', '1980-01-12T00:00:00.000000000',\n", " '1980-01-12T06:00:00.000000000', '1980-01-12T12:00:00.000000000',\n", " '1980-01-12T18:00:00.000000000', '1980-01-13T00:00:00.000000000',\n", " '1980-01-13T06:00:00.000000000', '1980-01-13T12:00:00.000000000',\n", " '1980-01-13T18:00:00.000000000', '1980-01-07T00:00:00.000000000',\n", " '1980-01-07T06:00:00.000000000', '1980-01-07T12:00:00.000000000',\n", " '1980-01-07T18:00:00.000000000', '1980-01-08T00:00:00.000000000',\n", " '1980-01-08T06:00:00.000000000', '1980-01-08T12:00:00.000000000',\n", " '1980-01-08T18:00:00.000000000', '1980-01-09T06:00:00.000000000',\n", "...\n", " '1980-01-21T06:00:00.000000000', '1980-01-21T12:00:00.000000000',\n", " '1980-01-21T18:00:00.000000000', '1980-01-22T00:00:00.000000000',\n", " '1980-01-22T06:00:00.000000000', '1980-01-22T12:00:00.000000000',\n", " '1980-01-22T18:00:00.000000000', '1980-01-23T00:00:00.000000000',\n", " '1980-01-23T06:00:00.000000000', '1980-01-23T12:00:00.000000000',\n", " '1980-01-23T18:00:00.000000000', '1980-01-24T00:00:00.000000000',\n", " '1980-01-24T06:00:00.000000000', '1980-01-24T12:00:00.000000000',\n", " '1980-01-24T18:00:00.000000000', '1980-01-25T00:00:00.000000000',\n", " '1980-01-25T06:00:00.000000000', '1980-01-25T12:00:00.000000000',\n", " '1980-01-25T18:00:00.000000000', '1980-01-26T00:00:00.000000000',\n", " '1980-01-26T06:00:00.000000000', '1980-01-26T12:00:00.000000000',\n", " '1980-01-26T18:00:00.000000000', '1980-01-27T00:00:00.000000000',\n", " '1980-01-27T06:00:00.000000000', '1980-01-27T12:00:00.000000000',\n", " '1980-01-27T18:00:00.000000000', '1980-01-28T00:00:00.000000000',\n", " '1980-01-28T06:00:00.000000000', '1980-01-28T12:00:00.000000000',\n", " '1980-01-28T18:00:00.000000000', '1980-01-29T00:00:00.000000000',\n", " '1980-01-29T06:00:00.000000000', '1980-01-29T12:00:00.000000000',\n", " '1980-01-29T18:00:00.000000000', '1980-01-30T00:00:00.000000000',\n", " '1980-01-30T06:00:00.000000000', '1980-01-30T12:00:00.000000000',\n", " '1980-01-30T18:00:00.000000000'], dtype='datetime64[ns]')\n", "Coordinates:\n", " * record (record) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98\n" ] }, { "ename": "TypeError", "evalue": "'>' not supported between instances of 'int' and 'datetime.datetime'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Try to select a subset of times based on datetime\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(tracks\u001b[38;5;241m.\u001b[39mtime)\n\u001b[0;32m----> 5\u001b[0m tracks_subset \u001b[38;5;241m=\u001b[39m tracks\u001b[38;5;241m.\u001b[39mwhere(\u001b[43mtracks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatetime\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1980\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m, drop\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:287\u001b[0m, in \u001b[0;36mDataArrayOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 286\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: DaCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self:\n\u001b[0;32m--> 287\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/dataarray.py:4691\u001b[0m, in \u001b[0;36mDataArray._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 4687\u001b[0m other_variable_or_arraylike: DaCompatible \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvariable\u001b[39m\u001b[38;5;124m\"\u001b[39m, other)\n\u001b[1;32m 4688\u001b[0m other_coords \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoords\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 4690\u001b[0m variable \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 4691\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvariable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_variable_or_arraylike\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4692\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive\n\u001b[1;32m 4693\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m f(other_variable_or_arraylike, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvariable)\n\u001b[1;32m 4694\u001b[0m )\n\u001b[1;32m 4695\u001b[0m coords, indexes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoords\u001b[38;5;241m.\u001b[39m_merge_raw(other_coords, reflexive)\n\u001b[1;32m 4696\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result_name(other)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:619\u001b[0m, in \u001b[0;36mVariableOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 618\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: VarCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self \u001b[38;5;241m|\u001b[39m T_DataArray:\n\u001b[0;32m--> 619\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/variable.py:2411\u001b[0m, in \u001b[0;36mVariable._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 2408\u001b[0m attrs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_attrs \u001b[38;5;28;01mif\u001b[39;00m keep_attrs \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 2409\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39merrstate(\u001b[38;5;28mall\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mignore\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 2410\u001b[0m new_data \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 2411\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mself_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_data\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive \u001b[38;5;28;01melse\u001b[39;00m f(other_data, self_data)\n\u001b[1;32m 2412\u001b[0m )\n\u001b[1;32m 2413\u001b[0m result \u001b[38;5;241m=\u001b[39m Variable(dims, new_data, attrs\u001b[38;5;241m=\u001b[39mattrs)\n\u001b[1;32m 2414\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'int' and 'datetime.datetime'" ] } ], "source": [ "import datetime\n", "\n", "# Try to select a subset of times based on datetime\n", "print(tracks.time)\n", "tracks_subset = tracks.where(tracks.time > datetime.datetime(1980, 1, 10), drop=True)" ] }, { "cell_type": "markdown", "id": "2401c2a8-7e7e-4c60-95d3-d8b56451f3f2", "metadata": {}, "source": [ "However, the same comparison can be done using `datetime64`, the syntax is just a bit different" ] }, { "cell_type": "code", "execution_count": 3, "id": "df00739b-a637-44cc-8eac-5855d0f87d21", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "array(['1980-01-10T06:00:00.000000000', '1980-01-10T12:00:00.000000000',\n", " '1980-01-10T18:00:00.000000000', '1980-01-11T00:00:00.000000000',\n", " '1980-01-11T06:00:00.000000000', '1980-01-11T12:00:00.000000000',\n", " '1980-01-11T18:00:00.000000000', '1980-01-12T00:00:00.000000000',\n", " '1980-01-12T06:00:00.000000000', '1980-01-12T12:00:00.000000000',\n", " '1980-01-12T18:00:00.000000000', '1980-01-13T00:00:00.000000000',\n", " '1980-01-13T06:00:00.000000000', '1980-01-13T12:00:00.000000000',\n", " '1980-01-13T18:00:00.000000000', '1980-01-10T06:00:00.000000000',\n", " '1980-01-10T12:00:00.000000000', '1980-01-10T18:00:00.000000000',\n", " '1980-01-11T00:00:00.000000000', '1980-01-11T06:00:00.000000000',\n", " '1980-01-11T12:00:00.000000000', '1980-01-11T18:00:00.000000000',\n", " '1980-01-12T00:00:00.000000000', '1980-01-12T06:00:00.000000000',\n", " '1980-01-17T06:00:00.000000000', '1980-01-17T18:00:00.000000000',\n", " '1980-01-18T06:00:00.000000000', '1980-01-19T00:00:00.000000000',\n", " '1980-01-19T06:00:00.000000000', '1980-01-20T06:00:00.000000000',\n", " '1980-01-20T12:00:00.000000000', '1980-01-20T18:00:00.000000000',\n", " '1980-01-21T00:00:00.000000000', '1980-01-21T06:00:00.000000000',\n", " '1980-01-21T12:00:00.000000000', '1980-01-21T18:00:00.000000000',\n", " '1980-01-22T00:00:00.000000000', '1980-01-22T06:00:00.000000000',\n", " '1980-01-22T12:00:00.000000000', '1980-01-22T18:00:00.000000000',\n", " '1980-01-23T00:00:00.000000000', '1980-01-23T06:00:00.000000000',\n", " '1980-01-23T12:00:00.000000000', '1980-01-23T18:00:00.000000000',\n", " '1980-01-24T00:00:00.000000000', '1980-01-24T06:00:00.000000000',\n", " '1980-01-24T12:00:00.000000000', '1980-01-24T18:00:00.000000000',\n", " '1980-01-25T00:00:00.000000000', '1980-01-25T06:00:00.000000000',\n", " '1980-01-25T12:00:00.000000000', '1980-01-25T18:00:00.000000000',\n", " '1980-01-26T00:00:00.000000000', '1980-01-26T06:00:00.000000000',\n", " '1980-01-26T12:00:00.000000000', '1980-01-26T18:00:00.000000000',\n", " '1980-01-27T00:00:00.000000000', '1980-01-27T06:00:00.000000000',\n", " '1980-01-27T12:00:00.000000000', '1980-01-27T18:00:00.000000000',\n", " '1980-01-28T00:00:00.000000000', '1980-01-28T06:00:00.000000000',\n", " '1980-01-28T12:00:00.000000000', '1980-01-28T18:00:00.000000000',\n", " '1980-01-29T00:00:00.000000000', '1980-01-29T06:00:00.000000000',\n", " '1980-01-29T12:00:00.000000000', '1980-01-29T18:00:00.000000000',\n", " '1980-01-30T00:00:00.000000000', '1980-01-30T06:00:00.000000000',\n", " '1980-01-30T12:00:00.000000000', '1980-01-30T18:00:00.000000000'],\n", " dtype='datetime64[ns]')\n", "Coordinates:\n", " * record (record) int64 16 17 18 19 20 21 22 23 ... 91 92 93 94 95 96 97 98\n" ] } ], "source": [ "import numpy as np\n", "\n", "tracks_subset = tracks.where(tracks.time > np.datetime64(\"1980-01-10\"), drop=True)\n", "print(tracks_subset.time)" ] }, { "cell_type": "markdown", "id": "6b275199-ae33-474d-95b9-1fdf6c7dfb1d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Note, that this isn't always the case. If the tracks are loaded in with a different\n", "calendar, then the times will use [cftime](https://unidata.github.io/cftime/)\n", "which is not converted to `datetime64` by xarray." ] }, { "cell_type": "code", "execution_count": 7, "id": "a8ac434d-a9b8-48d4-925e-c6d510d68ea1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "array([cftime.datetime(2022, 1, 13, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", "...\n", " cftime.datetime(2022, 1, 24, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 29, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 29, 6, 0, 0, 0, calendar='360_day', has_year_zero=True)],\n", " dtype=object)\n", "Dimensions without coordinates: record\n" ] } ], "source": [ "# The tracks don't actually use a 360_day calendar.\n", "# I'm just passing this as an argument to show an example of it loading this way\n", "tracks = huracanpy.load(\n", " huracanpy.example_TRACK_file, tracker=\"track\", calendar=\"360_day\"\n", ")\n", "print(tracks.time)" ] }, { "cell_type": "markdown", "id": "574bcf5b-496b-492a-a39a-e69aff8aa08c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "In this case, neither the `datetime` or the `datetime64` comparison will work and you\n", "have to compare to a `cftime.datetime` object with the same calendar" ] }, { "cell_type": "code", "execution_count": 9, "id": "39842f6a-f071-4f00-9dad-8afc45224120", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "cannot compare cftime.datetime(2022, 1, 13, 18, 0, 0, 0, calendar='360_day', has_year_zero=True) and datetime.datetime(1980, 1, 10, 0, 0) (different calendars)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m tracks_subset \u001b[38;5;241m=\u001b[39m tracks\u001b[38;5;241m.\u001b[39mwhere(\u001b[43mtracks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdatetime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatetime\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1980\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m, drop\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:287\u001b[0m, in \u001b[0;36mDataArrayOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 286\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: DaCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self:\n\u001b[0;32m--> 287\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/dataarray.py:4691\u001b[0m, in \u001b[0;36mDataArray._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 4687\u001b[0m other_variable_or_arraylike: DaCompatible \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvariable\u001b[39m\u001b[38;5;124m\"\u001b[39m, other)\n\u001b[1;32m 4688\u001b[0m other_coords \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoords\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 4690\u001b[0m variable \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 4691\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvariable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_variable_or_arraylike\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4692\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive\n\u001b[1;32m 4693\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m f(other_variable_or_arraylike, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvariable)\n\u001b[1;32m 4694\u001b[0m )\n\u001b[1;32m 4695\u001b[0m coords, indexes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoords\u001b[38;5;241m.\u001b[39m_merge_raw(other_coords, reflexive)\n\u001b[1;32m 4696\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result_name(other)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:619\u001b[0m, in \u001b[0;36mVariableOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 618\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: VarCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self \u001b[38;5;241m|\u001b[39m T_DataArray:\n\u001b[0;32m--> 619\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/variable.py:2411\u001b[0m, in \u001b[0;36mVariable._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 2408\u001b[0m attrs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_attrs \u001b[38;5;28;01mif\u001b[39;00m keep_attrs \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 2409\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39merrstate(\u001b[38;5;28mall\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mignore\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 2410\u001b[0m new_data \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 2411\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mself_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_data\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive \u001b[38;5;28;01melse\u001b[39;00m f(other_data, self_data)\n\u001b[1;32m 2412\u001b[0m )\n\u001b[1;32m 2413\u001b[0m result \u001b[38;5;241m=\u001b[39m Variable(dims, new_data, attrs\u001b[38;5;241m=\u001b[39mattrs)\n\u001b[1;32m 2414\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[0;32msrc/cftime/_cftime.pyx:1432\u001b[0m, in \u001b[0;36mcftime._cftime.datetime.__richcmp__\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: cannot compare cftime.datetime(2022, 1, 13, 18, 0, 0, 0, calendar='360_day', has_year_zero=True) and datetime.datetime(1980, 1, 10, 0, 0) (different calendars)" ] } ], "source": [ "tracks_subset = tracks.where(tracks.time > datetime.datetime(1980, 1, 10), drop=True)" ] }, { "cell_type": "code", "execution_count": 8, "id": "87bf2027-8b9c-42ab-8498-d64f5023182c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "'>' not supported between instances of 'cftime._cftime.datetime' and 'datetime.date'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m tracks_subset \u001b[38;5;241m=\u001b[39m tracks\u001b[38;5;241m.\u001b[39mwhere(\u001b[43mtracks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtime\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdatetime64\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m1980-01-10\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m, drop\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:287\u001b[0m, in \u001b[0;36mDataArrayOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 286\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: DaCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self:\n\u001b[0;32m--> 287\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/dataarray.py:4691\u001b[0m, in \u001b[0;36mDataArray._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 4687\u001b[0m other_variable_or_arraylike: DaCompatible \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvariable\u001b[39m\u001b[38;5;124m\"\u001b[39m, other)\n\u001b[1;32m 4688\u001b[0m other_coords \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(other, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoords\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 4690\u001b[0m variable \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 4691\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvariable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_variable_or_arraylike\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4692\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive\n\u001b[1;32m 4693\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m f(other_variable_or_arraylike, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvariable)\n\u001b[1;32m 4694\u001b[0m )\n\u001b[1;32m 4695\u001b[0m coords, indexes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcoords\u001b[38;5;241m.\u001b[39m_merge_raw(other_coords, reflexive)\n\u001b[1;32m 4696\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result_name(other)\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/_typed_ops.py:619\u001b[0m, in \u001b[0;36mVariableOpsMixin.__gt__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 618\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__gt__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: VarCompatible) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Self \u001b[38;5;241m|\u001b[39m T_DataArray:\n\u001b[0;32m--> 619\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_binary_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgt\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniforge3/envs/core/lib/python3.11/site-packages/xarray/core/variable.py:2411\u001b[0m, in \u001b[0;36mVariable._binary_op\u001b[0;34m(self, other, f, reflexive)\u001b[0m\n\u001b[1;32m 2408\u001b[0m attrs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_attrs \u001b[38;5;28;01mif\u001b[39;00m keep_attrs \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 2409\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39merrstate(\u001b[38;5;28mall\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mignore\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 2410\u001b[0m new_data \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 2411\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mself_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother_data\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m reflexive \u001b[38;5;28;01melse\u001b[39;00m f(other_data, self_data)\n\u001b[1;32m 2412\u001b[0m )\n\u001b[1;32m 2413\u001b[0m result \u001b[38;5;241m=\u001b[39m Variable(dims, new_data, attrs\u001b[38;5;241m=\u001b[39mattrs)\n\u001b[1;32m 2414\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'cftime._cftime.datetime' and 'datetime.date'" ] } ], "source": [ "tracks_subset = tracks.where(tracks.time > np.datetime64(\"1980-01-10\"), drop=True)" ] }, { "cell_type": "code", "execution_count": 12, "id": "1e1eab5c-5dac-4ab2-80c5-673e8a0940fd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "array([cftime.datetime(2022, 1, 13, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 14, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 15, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 16, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 17, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 22, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", "...\n", " cftime.datetime(2022, 1, 24, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 25, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 26, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 27, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 6, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 12, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 28, 18, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 29, 0, 0, 0, 0, calendar='360_day', has_year_zero=True),\n", " cftime.datetime(2022, 1, 29, 6, 0, 0, 0, calendar='360_day', has_year_zero=True)],\n", " dtype=object)\n", "Dimensions without coordinates: record\n" ] } ], "source": [ "import cftime\n", "\n", "tracks_subset = tracks.where(\n", " tracks.time > cftime.datetime(1980, 1, 10, calendar=\"360_day\"), drop=True\n", ")\n", "print(tracks_subset.time)" ] }, { "cell_type": "markdown", "id": "84113888-e2b6-4314-9355-f43437f6b5a4", "metadata": {}, "source": [ "## Subsetting by track\n", "To apply a criteria to each track in the dataset, use\n", "[huracanpy.subset.trackswhere](../api/subset.rst#huracanpy.subset.trackswhere)" ] }, { "cell_type": "code", "execution_count": 9, "id": "22bfae72-04e4-4327-8489-14160f4df557", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "track 0 category 2\n", "track 1 category 1\n", "track 2 category 2\n", "\n", " tracks remaining - {0, 2}\n" ] } ], "source": [ "# Add storm category by pressure to each track and filter those that don't reach\n", "# category 2\n", "tracks = huracanpy.load(huracanpy.example_csv_file)\n", "\n", "tracks[\"category\"] = huracanpy.utils.category.get_pressure_cat(\n", " tracks.slp, slp_units=\"Pa\"\n", ")\n", "\n", "# Show the categories for each storm\n", "# Storms 0 and 2 reach category 2, and storm 1 only reaches category 1\n", "for track_id, track in tracks.groupby(\"track_id\"):\n", " print(\"track\", track_id, \"category\", int(track.category.max()))\n", "\n", "# Subset the tracks by category threshold which will remove track 1\n", "track_subset = huracanpy.subset.trackswhere(\n", " tracks, lambda track: track.category.max() >= 2\n", ")\n", "\n", "# Confirm that track 1 has been filtered out\n", "print(\"\\n\", \"tracks remaining -\", set(track_subset.track_id.data))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }