Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created March 1, 2024 09:16
Show Gist options
  • Save epifanio/efa84fd14c353869e57de82fd395102c to your computer and use it in GitHub Desktop.
Save epifanio/efa84fd14c353869e57de82fd395102c to your computer and use it in GitHub Desktop.
dataset_test.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "9b098cd6-c31b-4e09-bdc4-4d18e60f39e6",
"metadata": {},
"source": [
"* ValueError: index must be monotonic for resampling"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4eaff137-8978-4ff7-86a9-d97290b9fcbc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 'time' coordinate is not monotonic.\n"
]
}
],
"source": [
"import xarray as xr\n",
"import numpy as np\n",
"nc_url = \"https://thredds.met.no/thredds/dodsC/met.no/observations/surface/39100/211/oksoy_fyr_air_temperature.nc\"\n",
"ds = xr.open_dataset(nc_url)\n",
"coords_values = ds.coords['time'].values[::-1]\n",
"\n",
"is_monotonic = all(coords_values[i] <= coords_values[i + 1] for i in range(len(coords_values) - 1)) or all(coords_values[i] >= coords_values[i + 1] for i in range(len(coords_values) - 1))\n",
"\n",
"if is_monotonic:\n",
" print(\"The 'time' coordinate is monotonic.\")\n",
"else:\n",
" print(\"The 'time' coordinate is not monotonic.\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7814784f-dbc6-4cc2-9b60-e67f83674e89",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"347971\n",
"348587\n"
]
}
],
"source": [
"for i in range(len(coords_values) - 1):\n",
" if not ds.coords['time'].values[i] <= ds.coords['time'].values[i+1]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "129ed207-7cfd-4f92-bff4-bc6fe1ae60fd",
"metadata": {},
"outputs": [],
"source": [
"i_a = 347971\n",
"i_b = 348587"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2a9aa468-be5b-4f15-9fe4-8e2fd59f1dfc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[numpy.datetime64('2024-02-24T07:00:00.000000000'),\n",
" numpy.datetime64('2024-02-24T08:00:00.000000000'),\n",
" numpy.datetime64('2024-01-22T17:00:00.000000000')]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[ds.coords['time'].values[i_a-1], ds.coords['time'].values[i_a], ds.coords['time'].values[i_a+1]]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "35d3f362-a3fd-4f70-a5de-be9b8cf2775c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[numpy.datetime64('2024-02-17T07:00:00.000000000'),\n",
" numpy.datetime64('2024-02-17T08:00:00.000000000'),\n",
" numpy.datetime64('2023-10-02T17:00:00.000000000')]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[ds.coords['time'].values[i_b-1], ds.coords['time'].values[i_b], ds.coords['time'].values[i_b+1]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6377fdef-824f-4b67-af50-58b7c6d0b74a",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment