Skip to content

Instantly share code, notes, and snippets.

@epifanio
Last active April 9, 2024 10:27
Show Gist options
  • Save epifanio/822b6a4ee481c66c5987903f03424161 to your computer and use it in GitHub Desktop.
Save epifanio/822b6a4ee481c66c5987903f03424161 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b4fbad44-178b-453c-aa89-84f25b5d66db",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import xarray as xr\n",
"import holoviews as hv\n",
"import hvplot.xarray\n",
"import panel as pn\n",
"import numpy as np\n",
"\n",
"hv.renderer('bokeh').webgl = False "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10032e25-265e-4567-a596-550a7669ebe7",
"metadata": {},
"outputs": [],
"source": [
"def plot(var, title=None):\n",
" if not title:\n",
" try:\n",
" title = f\"{ds[var].attrs['long_name']}\"\n",
" except KeyError:\n",
" title = f\"{var}\"\n",
" else:\n",
" title=title\n",
" if 'featureType' in ds.attrs:\n",
" featureType = ds.attrs['featureType']\n",
" else:\n",
" featureType = None\n",
" if featureType == 'timeSeries':\n",
" var_coord = [i for i in ds.coords if ds.coords.dtypes[i] == np.dtype('<M8[ns]')]\n",
" axis_arguments = {'y': ds[var], 'grid':True, 'title': title, 'widget_location': 'bottom', 'responsive': True}\n",
" if featureType and featureType != \"timeSeries\":\n",
" axis_arguments = {'x': ds[var], 'grid':True, 'title': title, 'widget_location': 'bottom', 'responsive': True}\n",
" try:\n",
" plot_widget = ds[var].hvplot.line(**axis_arguments)\n",
" except TypeError:\n",
" plot_widget = ds[var].hvplot.line()\n",
" except ValueError:\n",
" axis_arguments = {'x': var, 'grid':True, 'title': title, 'widget_location': 'bottom', 'responsive': True}\n",
" plot_widget = ds[var].hvplot.line(**axis_arguments)\n",
" return plot_widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a961158c-fc4e-4f0f-890d-1f31756c9bea",
"metadata": {},
"outputs": [],
"source": [
"url = \"qlwXxbz0QQiE6qLALjaFlQ.nc\"\n",
"ds = xr.open_dataset(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03e5cd69-61a8-4666-9acc-e511764c1ba3",
"metadata": {},
"outputs": [],
"source": [
"ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb309b35-4d3f-4306-9c96-7892a4a32ff3",
"metadata": {},
"outputs": [],
"source": [
"plot('dw')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5776723-4aa1-4903-870b-f5e6e39d11a2",
"metadata": {},
"outputs": [],
"source": [
"plot('ws')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40ac5369-53c3-4921-a55e-b128ae9d0253",
"metadata": {},
"outputs": [],
"source": [
"\n",
"plottable_vars = [i for i in ds if (list(ds[i].dims) == list(ds.coords) or list(ds[i].dims) == list(ds.coords)[::-1])] \n",
"ds.coords.values(), ds.sizes, plottable_vars, ds.attrs['featureType']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f58447be-e99b-4cdc-b882-4b23a2feb841",
"metadata": {},
"outputs": [],
"source": [
"time_coord = [i for i in ds.coords if ds.coords.dtypes[i] == np.dtype('<M8[ns]')]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5d10909-d2cc-44a9-bcb0-d8ad1b22fa89",
"metadata": {},
"outputs": [],
"source": [
"time_coord"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4fcef8b6-1da1-423b-9b8e-346a00cd8506",
"metadata": {},
"outputs": [],
"source": [
"ds.indexes[time_coord[0]].min(), ds.indexes[time_coord[0]].max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abe7d18d-23e4-41bc-b2b7-a4856206fc86",
"metadata": {},
"outputs": [],
"source": [
"mapping_var_names = {}\n",
"for i in plottable_vars:\n",
" if int(len(list(ds[i].coords)) != 0):\n",
" try:\n",
" title = f\"{ds[i].attrs['long_name']} [{i}]\"\n",
" except KeyError:\n",
" title = f\"{i}\"\n",
" mapping_var_names[i] = title"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d14268c-b7e8-4320-ab78-e27174e80487",
"metadata": {},
"outputs": [],
"source": [
"mapping_var_names"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88dcfe11-42dc-44e8-8277-6658dc905722",
"metadata": {},
"outputs": [],
"source": [
"common_values = {j:[value for value in list(ds[j].coords) if value in list(ds.dims)] for j in ds}\n",
"common_values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f23ce79-a5c3-4d57-bff5-c3a716a771c5",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"var_coord = [i for i in ds.coords if ds.coords.dtypes[i] == np.dtype('<M8[ns]')]\n",
"var_coord"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "105f2a75-0765-48bb-a6fa-b85512e4477a",
"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