Skip to content

Instantly share code, notes, and snippets.

@sharkinsspatial
Created January 30, 2025 16:55
Show Gist options
  • Select an option

  • Save sharkinsspatial/a00afe480186bbd953d5265e560fa24e to your computer and use it in GitHub Desktop.

Select an option

Save sharkinsspatial/a00afe480186bbd953d5265e560fa24e to your computer and use it in GitHub Desktop.
non_coordinate_dimensions.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1188c1b6-fff3-4fa9-acbf-53fb9b4ccca0",
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import xarray as xr\n",
"from xarray.tests.test_dataset import create_test_data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5c8dc6ff-e78c-4af1-812f-8acb5058873c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset> Size: 28kB\n",
"Dimensions: (dim2: 80, dim3: 10, time: 20, dim1: 20)\n",
"Coordinates:\n",
" * dim2 (dim2) float64 640B 0.0 0.5 1.0 1.5 2.0 ... 38.0 38.5 39.0 39.5\n",
" * dim3 (dim3) <U1 40B 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'\n",
" * time (time) datetime64[ns] 160B 2000-01-01 2000-01-02 ... 2000-01-20\n",
" numbers (dim3) int64 80B 1 1 1 2 2 0 1 2 0 0\n",
"Dimensions without coordinates: dim1\n",
"Data variables:\n",
" var1 (dim1, dim2) float64 13kB 0.845 -1.061 -0.4623 ... 0.2738 -0.5301\n",
" var2 (dim1, dim2) float64 13kB -0.8353 0.5596 0.3257 ... 2.753 1.234\n",
" var3 (dim3, dim1) float64 2kB -1.641 -0.4739 -0.7058 ... 1.417 1.049\n"
]
}
],
"source": [
"ds = create_test_data(dim_sizes=(20, 80, 10))\n",
"filepath = \"non_coord_dim.nc\"\n",
"ds.to_netcdf(filepath, engine=\"h5netcdf\")\n",
"print(ds)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b2fc2980-11ab-48b2-8b4e-57b5f13864df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = h5py.File(filepath)\n",
"f[\"dim1\"].id.get_storage_size()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "15103286-0094-4a74-a476-06297294d9e2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset> Size: 4B\n",
"Dimensions: ()\n",
"Data variables:\n",
" empty float32 4B ...\n"
]
}
],
"source": [
"empty_filepath = \"empty_variable.nc\"\n",
"f_empty = h5py.File(empty_filepath, \"w\")\n",
"f_empty.create_dataset(\"empty\", shape=(), dtype=\"float32\")\n",
"empty_ds = xr.open_dataset(empty_filepath)\n",
"print(empty_ds)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "21c1c5d8-ab19-47f2-a027-0837b90d4933",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_empty[\"empty\"].id.get_storage_size()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "16a150c8-9fdc-4fbe-95fd-a96bb8f8112d",
"metadata": {},
"outputs": [],
"source": [
"def determine_non_coordinate_dims(h5py_handle):\n",
" dimension_names = []\n",
" for name, obj in h5py_handle.items():\n",
" if \"_Netcdf4Dimid\" in obj.attrs:\n",
" dimension_names.append(name)\n",
" for name, obj in h5py_handle.items():\n",
" if obj.id.get_storage_size() == 0 and name in dimension_names:\n",
" print(f\"{name} is a dim without coordinates\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "9868576f-c5a2-4de6-88a0-fe30a47e2b5a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dim1 is a dim without coordinates\n"
]
}
],
"source": [
"determine_non_coordinate_dims(f)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "0b23028e-9b66-41a9-bde5-86010dce3d80",
"metadata": {},
"outputs": [],
"source": [
"determine_non_coordinate_dims(f_empty)"
]
}
],
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment