Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ __pycache__
.coverage
htmlcov
dist
build
.direnv
.mypy_cache
.pytest_cache
.ruff_cache
*.egg-info/
.venv
*.tar
*.zarr
252 changes: 252 additions & 0 deletions tests/createZarrAndTar_Stores.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"id": "d3a17686-0c0f-42a9-81f5-e09a115f0ed0",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import tarfile\n",
"\n",
"import numpy as np\n",
"import xarray as xr\n",
"import zarr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "86fbe34a-1870-4f0a-83df-baab853048a1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.1.5.dev2734+g596fd7f91\n",
"2025.12.0\n",
"2.4.0\n"
]
}
],
"source": [
"print(zarr.__version__)\n",
"print(xr.__version__)\n",
"print(np.__version__)"
]
},
{
"cell_type": "markdown",
"id": "de4e8067-95a7-47d5-98e1-17ffa9fe642f",
"metadata": {},
"source": [
"# Testing zarr stores with sample data"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "41b5467f-5c9c-4a91-90ff-b5d80334553b",
"metadata": {},
"outputs": [],
"source": [
"times = np.arange('2023-01-01', '2023-01-03', dtype='datetime64[D]')\n",
"lats = [34, 35, 36]\n",
"lons = [-118, -117, -116]\n",
"variableDict= {\n",
" 'temperature' : {\n",
" 'values' : np.random.rand(2, 3, 3) * 30 + 273.15, # (time, lat, lon)\n",
" 'units' : \"K\"\n",
" },\n",
" 'precipitation' : {\n",
" 'values' : np.random.rand(2, 3, 3), # (time, lat, lon)\n",
" 'units' : \"mm/day\"\n",
" }\n",
"}\n",
"zarrStorePrefix='testStore'\n",
"zarrStoreList = []"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ed4f5dbd-5bb1-4b91-a450-ef5999fe464b",
"metadata": {},
"outputs": [],
"source": [
"def createZarrStore( varName, index ):\n",
" # Create the Dataset\n",
" ds = xr.Dataset(\n",
" data_vars={\n",
" f\"{varName}\": (\n",
" (\"time\", \"lat\", \"lon\"),\n",
" variableDict[varName]['values'],\n",
" {\"units\": variableDict[varName]['units']}),\n",
" },\n",
" coords={\n",
" \"time\": times,\n",
" \"lat\": lats,\n",
" \"lon\": lons\n",
" },\n",
" attrs={\"description\": \"Sample weather data\"}\n",
" )\n",
" #Convert and save to zarr store with 'index' in the name.\n",
" zarrStoreName = f'{zarrStorePrefix}{index}Dev.zarr'\n",
" ds.to_zarr( f'{zarrStoreName}', mode='w', zarr_format=3, consolidated = True )\n",
" zarrStoreList.append(zarrStoreName)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3eac65ce-8880-4e40-a777-dca8bd3bdfb3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/work/bk1414/k204247/staczarrDev/tools/myRepo/zarr-python/src/zarr/api/asynchronous.py:247: ZarrUserWarning: Consolidated metadata is currently not part in the Zarr format 3 specification. It may not be supported by other zarr implementations and may change in the future.\n",
" warnings.warn(\n",
"/work/bk1414/k204247/staczarrDev/tools/myRepo/zarr-python/src/zarr/api/asynchronous.py:247: ZarrUserWarning: Consolidated metadata is currently not part in the Zarr format 3 specification. It may not be supported by other zarr implementations and may change in the future.\n",
" warnings.warn(\n"
]
}
],
"source": [
"count = 1\n",
"for variable in variableDict.keys():\n",
" createZarrStore( variable, count )\n",
" count += 1"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8885aaa4-ed8a-4863-a9fc-3671ceef2426",
"metadata": {},
"outputs": [],
"source": [
"def scan_dir( dirName ):\n",
" with os.scandir( dirName ) as it:\n",
" for entry in it:\n",
" if entry.is_file():\n",
" list_of_vars.append( entry.path )\n",
" elif entry.is_dir():\n",
" scan_dir( entry.path )"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "13992e7b-c04d-4665-979f-536c383700b8",
"metadata": {},
"outputs": [],
"source": [
"def createTarStoreFromZarrStore(zarrStoreName):\n",
" zarrBaseName=zarrStoreName.split('.')[0]\n",
" tarFileName=f'{zarrBaseName}.tar'\n",
"\n",
" scan_dir( zarrStoreName )\n",
" print(list_of_vars)\n",
"\n",
" try:\n",
" with tarfile.open(tarFileName, \"w\") as tar:\n",
" for name in list_of_vars:\n",
" print(f\"Adding file {name} to {tarFileName}\\n\")\n",
" tar.add( name, arcname=name.replace( zarrStoreName + os.path.sep, '' ) )\n",
" tar.close()\n",
" except Exception as e:\n",
" print(f\"Exception occured: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "cbbb89a6-3e32-4139-bc6b-72efcc1851e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['testStore1Dev.zarr/time/zarr.json', 'testStore1Dev.zarr/time/c/0', 'testStore1Dev.zarr/temperature/zarr.json', 'testStore1Dev.zarr/temperature/c/0/0/0', 'testStore1Dev.zarr/zarr.json', 'testStore1Dev.zarr/lon/zarr.json', 'testStore1Dev.zarr/lon/c/0', 'testStore1Dev.zarr/lat/zarr.json', 'testStore1Dev.zarr/lat/c/0']\n",
"Adding file testStore1Dev.zarr/time/zarr.json to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/time/c/0 to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/temperature/zarr.json to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/temperature/c/0/0/0 to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/zarr.json to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/lon/zarr.json to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/lon/c/0 to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/lat/zarr.json to testStore1Dev.tar\n",
"\n",
"Adding file testStore1Dev.zarr/lat/c/0 to testStore1Dev.tar\n",
"\n",
"['testStore2Dev.zarr/precipitation/zarr.json', 'testStore2Dev.zarr/precipitation/c/0/0/0', 'testStore2Dev.zarr/time/zarr.json', 'testStore2Dev.zarr/time/c/0', 'testStore2Dev.zarr/zarr.json', 'testStore2Dev.zarr/lon/zarr.json', 'testStore2Dev.zarr/lon/c/0', 'testStore2Dev.zarr/lat/zarr.json', 'testStore2Dev.zarr/lat/c/0']\n",
"Adding file testStore2Dev.zarr/precipitation/zarr.json to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/precipitation/c/0/0/0 to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/time/zarr.json to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/time/c/0 to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/zarr.json to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/lon/zarr.json to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/lon/c/0 to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/lat/zarr.json to testStore2Dev.tar\n",
"\n",
"Adding file testStore2Dev.zarr/lat/c/0 to testStore2Dev.tar\n",
"\n"
]
}
],
"source": [
"for zarrStore in zarrStoreList:\n",
" list_of_vars=[]\n",
" createTarStoreFromZarrStore( zarrStore )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e65d3a09-65f1-4cb5-a068-ceb3f8a2730c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pystacdevenv",
"language": "python",
"name": "pystacdevenv"
},
"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.14.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading