### Installing documentation dependencies via pip Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs VirtualiZarr in editable mode along with the 'docs' dependency group, which is necessary for building the project documentation locally. ```shell pip install '-e .[docs]' ``` -------------------------------- ### Installing VirtualiZarr via pip Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs the core VirtualiZarr library with only the minimal required dependencies using the pip package manager. ```shell pip install virtualizarr ``` -------------------------------- ### Install Dependencies (Shell) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Installs the necessary Python packages, including virtualizarr with 'icechunk' and 'hdf' extras, and ipykernel, required to run the notebook. ```shell !pip install 'virtualizarr['icechunk','hdf']' ipykernel ``` -------------------------------- ### Verify Icechunk Installation (Shell) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Uses pip to display information about the installed icechunk package, confirming its presence and version. ```shell !pip show icechunk ``` -------------------------------- ### Installing specific optional dependencies via pip Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs the core VirtualiZarr library along with specific optional dependency groups, such as 'hdf' for reading HDF files and 'icechunk' for writing virtual references in icechunk format. ```shell pip install "virtualizarr[hdf, icechunk]" ``` -------------------------------- ### Installing compound optional dependency group via pip Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs the core VirtualiZarr library along with a compound optional dependency group, such as 'all_readers', which includes multiple individual reader dependency sets. ```shell pip install "virtualizarr[all_readers]" ``` -------------------------------- ### Install Virtualizarr, Coiled, and Dependencies (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Installs the necessary Python packages, including virtualizarr with 'icechunk' and 'hdf' extras, coiled, ipykernel, and bokeh, using pip. This prepares the environment for running the notebook and the coiled functions. ```Bash pip install 'virtualizarr['icechunk','hdf']' coiled ipykernel bokeh ``` -------------------------------- ### CLI Example: Checking Data Store Access Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Example command to execute the `check_data_store_access` function via the CLI to verify connectivity to the data store. ```bash python main.py check_data_store_access ``` -------------------------------- ### Installing VirtualiZarr via conda Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs the VirtualiZarr library from the conda-forge channel using the conda package manager. ```shell conda install -c conda-forge virtualizarr ``` -------------------------------- ### Opening Kerchunk References with VirtualiZarr Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Shows how to open Kerchunk-formatted references (JSON or Parquet) using `virtualizarr.open_virtual_dataset` and then convert the resulting virtual dataset to an Icechunk store. ```Python from virtualizarr import open_virtual_dataset vds = open_virtual_dataset('refs.json') # vds = open_virtual_dataset('refs.parq') # kerchunk parquet files are supported too vds.virtualize.to_icechunk(icechunkstore) ``` -------------------------------- ### CLI Example: Calculating Icechunk Store Mean Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Example command to execute the `calc_icechunk_store_mean` function via the CLI, specifying a date range for the calculation. ```bash python main.py calc_icechunk_store_mean --start_date 2022-01-01 --end_date 2022-01-31 ``` -------------------------------- ### Installing Pre-commit Tool (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Command to install the `pre-commit` tool using Python's package installer, `pip`. This tool is used to manage and run code quality hooks before commits. Requires Python and pip. ```Bash python -m pip install pre-commit ``` -------------------------------- ### Installing test dependencies for local development via pip Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/installation.md Installs VirtualiZarr in editable mode along with the 'test' dependency group, which is required for running the test suite during local development. ```shell pip install '-e .[test]' ``` -------------------------------- ### Serialize VirtualiZarr Dataset to Icechunk Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet shows how to serialize a VirtualiZarr dataset (`ds`) into the Icechunk store format. ```Python ds.virtualize.to_icechunk() ``` -------------------------------- ### Building VirtualiZarr Documentation Locally (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Commands to install documentation build dependencies using `pixi` and then build the documentation. The output will be generated in the `docs/_build/html` directory. Requires `pixi` and the docs environment dependencies. ```Bash pixi install --environment docs pixi run build-docs ``` -------------------------------- ### Installing Pre-commit Hooks Locally (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Command to install the configured `pre-commit` hooks into the local Git repository. These hooks will automatically run on subsequent commits to enforce code standards. Requires `pre-commit` installed. ```Bash pre-commit install ``` -------------------------------- ### CLI Example: Writing Data to Icechunk Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Example command to execute the `write_to_icechunk` function via the CLI, specifying a date range for processing. ```bash python main.py write_to_icechunk --start_date 2022-01-01 --end_date 2022-01-02 ``` -------------------------------- ### Setting up Python Environment with uv Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Commands to create a Python virtual environment using `uv`, activate it, and install dependencies from `requirements.txt`. ```sh uv venv virtualizarr-lithops --python 3.11 source virtualizarr-lithops/bin/activate uv pip install -r requirements.txt ``` -------------------------------- ### Serving VirtualiZarr Documentation Locally (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Command to start a local web server using `pixi` to serve the documentation, allowing for live preview during development. The documentation will be accessible at `http://0.0.0.0:8000/`. Requires `pixi` and the docs environment dependencies. ```Bash pixi run serve-docs ``` -------------------------------- ### Running All Pre-commit Checks (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Command to manually trigger all `pre-commit` checks on all files in the repository, regardless of whether they were modified in the current commit. Useful for initial setup or checking the entire codebase. Requires `pre-commit` installed and hooks installed locally. ```Bash pre-commit run --all-files ``` -------------------------------- ### Example Rewritten Manifest Entry (JSON) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Shows an example of a single chunk entry from the manifest of the 'air' variable after the file paths have been rewritten using the `rename_paths` method and the `local_to_s3_url` function. Note the updated 'path' value. ```json {'0.0.0': {'path': 'http://s3.amazonaws.com/my_bucket/air.nc', 'offset': 15419, 'length': 7738000}} ``` -------------------------------- ### Testing VirtualiZarr in Different Environments (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Shows how to run tests against different dependency configurations (minimal, upstream) using `pixi` environments. Also includes a command to list the packages installed in a specific environment. Requires `pixi`. ```Bash pixi run --environment min-deps run-tests # Test with the minimal set of dependencies installed pixi run --environment upstream run-tests # Test with unreleased versions of upstream libraries # List which versions are installed in the `min-deps` environment pixi list --environment min-deps ``` -------------------------------- ### Serialize VirtualiZarr Dataset to Zarr v3 Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet demonstrates serializing a VirtualiZarr dataset (`ds`) into a Zarr v3 store, which includes `manifest.json` files. ```Python ds.virtualize.to_zarr() ``` -------------------------------- ### Testing: Checking Data Store Access via CLI Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Example command used for testing the package by verifying access to the data store through the command-line interface. ```bash python main.py check_data_store_access ``` -------------------------------- ### Generate References from Zarr v2 using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is intended to open a virtual dataset from a Zarr v2 store, but is not yet implemented. ```Python open_virtual_dataset(..., filetype='zarr') ``` -------------------------------- ### Configure Local Icechunk Storage (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Sets up a local filesystem storage configuration for Icechunk, specifying the directory `./terraclimate` where the virtual dataset references will be stored. It then opens or creates an Icechunk repository at this location and starts a writable session named "main". ```Python local_storage_conifg = icechunk.local_filesystem_storage("./terraclimate") repo = icechunk.Repository.open_or_create(local_storage_conifg) session = repo.writable_session("main") ``` -------------------------------- ### Generate References from Zarr v3 using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is used to open a virtual dataset from an existing Zarr v3 store. ```Python open_virtual_dataset(..., filetype='zarr') ``` -------------------------------- ### Generate References from GRIB using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is intended to open a virtual datatree from a GRIB file, but is not yet implemented. ```Python open_virtual_datatree(..., filetype='grib') ``` -------------------------------- ### Serialize VirtualiZarr Dataset to Kerchunk JSON Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet demonstrates serializing a VirtualiZarr dataset (`ds`) into the Kerchunk JSON reference format using the `to_kerchunk` method. ```Python ds.virtualize.to_kerchunk('combined.json', format='JSON') ``` -------------------------------- ### Generate References from FITS using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk function is used to process a FITS file and generate Zarr references. ```Python kerchunk.fits.process_file ``` -------------------------------- ### Generate References from FITS using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is used to open a virtual dataset from a FITS file, leveraging Kerchunk's process_file internally. ```Python open_virtual_dataset(..., filetype='fits') ``` -------------------------------- ### Generate References from GRIB2 using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk function is used to scan a GRIB2 file and generate Zarr references. ```Python kerchunk.grib2.scan_grib ``` -------------------------------- ### Set up Python environment with Conda and Pip (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/virtualizarr-with-lithops/README.md Creates a new conda environment named 'virtualizarr-lithops' with Python 3.11, activates it, and installs dependencies from 'requirements.txt' using pip. ```Bash conda create --name virtualizarr-lithops -y python=3.11 conda activate virtualizarr-lithops pip install -r requirements.txt ``` -------------------------------- ### Serialize VirtualiZarr Dataset to Kerchunk Parquet Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet shows how to serialize a VirtualiZarr dataset (`ds`) into the Kerchunk Parquet reference format using the `to_kerchunk` method. ```Python ds.virtualize.to_kerchunk('combined.parq', format=parquet') ``` -------------------------------- ### Generate References from NetCDF3 using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk class is used to generate Zarr references from a single NetCDF3 file. ```Python kerchunk.netCDF3.NetCDF3ToZarr ``` -------------------------------- ### Generate References from HDF4 using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk class is used to generate Zarr references from a single HDF4 file. ```Python kerchunk.hdf4.HDF4ToZarr ``` -------------------------------- ### Generate References from Zarr v2 using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk class is used to generate Zarr references from an existing Zarr v2 store. ```Python kerchunk.zarr.ZarrToZarr ``` -------------------------------- ### Generate References from HDF5 using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is used to open a virtual dataset from an HDF5 file, leveraging Kerchunk's SingleHdf5ToZarr internally. ```Python open_virtual_dataset(..., filetype='hdf5') ``` -------------------------------- ### Serialize Kerchunk Refs to JSON (ujson) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet shows how to serialize Kerchunk references into a JSON string using the `ujson` library and the `h5chunks.translate()` function. ```Python ujson.dumps(h5chunks.translate()) ``` -------------------------------- ### Generate References from NetCDF3 using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is used to open a virtual dataset from a NetCDF3 file, leveraging Kerchunk's NetCDF3ToZarr internally. ```Python open_virtual_dataset(..., filetype='netcdf3') ``` -------------------------------- ### Generate References from HDF5 using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk class is used to generate Zarr references from a single HDF5 or NetCDF4 file. ```Python kerchunk.hdf.SingleHdf5ToZarr ``` -------------------------------- ### Example Chunk Manifest Structure (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/data_structures.md This Python dictionary illustrates the conceptual structure of a Chunk Manifest, showing how Zarr chunk keys are mapped to file paths, byte offsets, and lengths within those files. ```python { "0.0.0": {"path": "s3://bucket/foo.nc", "offset": 100, "length": 100}, "0.0.1": {"path": "s3://bucket/foo.nc", "offset": 200, "length": 100}, "0.1.0": {"path": "s3://bucket/foo.nc", "offset": 300, "length": 100}, "0.1.1": {"path": "s3://bucket/foo.nc", "offset": 400, "length": 100} } ``` -------------------------------- ### Opening Virtual Dataset from Kerchunk References (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Opens a virtual dataset from existing kerchunk JSON or parquet reference files using virtualizarr. The 'filetype' parameter is set to 'kerchunk' to indicate the source format. ```Python open_virtual_dataset(..., filetype='kerchunk') ``` -------------------------------- ### Generate References from TIFF using Kerchunk Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This Kerchunk function is used to generate Zarr references from a TIFF or COG file. ```Python kerchunk.tiff.tiff_to_zarr ``` -------------------------------- ### Getting Original Dataset Size (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates how to access the `nbytes` attribute of the original xarray Dataset (`ds`) to show its memory footprint. ```python ds.nbytes ``` -------------------------------- ### Dropping Variables from Xarray Dataset (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Removes specified variables from an existing xarray Dataset object in memory. ```Python xarray.Dataset.drop_vars ``` -------------------------------- ### Renaming Variables in Xarray Dataset (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Renames specified variables within an existing xarray Dataset object in memory. ```Python xarray.Dataset.rename_vars ``` -------------------------------- ### Opening Virtual Dataset Dropping Variables (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Opens a virtual dataset using virtualizarr while excluding specified variables during the loading process. This allows for selective loading of data. ```Python open_virtual_dataset(..., drop_variables=...) ``` -------------------------------- ### Serialize Kerchunk Refs to Parquet Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This snippet illustrates serializing Kerchunk references, likely stored in a dictionary (`out_dict`), into a Parquet file using a method like `df.refs_to_dataframe`. ```Python df.refs_to_dataframe(out_dict, "combined.parq") ``` -------------------------------- ### Generate References from HDF4 using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is intended to open a virtual dataset from an HDF4 file, leveraging Kerchunk's HDF4ToZarr internally, but is not yet implemented. ```Python open_virtual_dataset(..., filetype='hdf4') ``` -------------------------------- ### Opening Multiple NetCDF Files with Xarray Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Demonstrates how to open and combine multiple NetCDF files into a single lazy xarray dataset using `xr.open_mfdataset`. Notes that this operation can be time-consuming due to internal checks. ```Python ds = xr.open_mfdataset( '/my/files*.nc', engine='h5netcdf', combine='nested', ) ds # the complete lazy xarray dataset ``` -------------------------------- ### Generate References from TIFF using VirtualiZarr Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md This VirtualiZarr function is used to open a virtual dataset from a TIFF file, potentially using Kerchunk's tiff_to_zarr or tifffile. ```Python open_virtual_dataset(..., filetype='tiff') ``` -------------------------------- ### Opening Virtual Dataset from DMR++ Metadata (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Opens a virtual dataset from a DMR++ metadata file using the virtualizarr library. The 'filetype' parameter is explicitly set to 'dmrpp' to specify the input format. ```Python open_virtual_dataset(..., filetype='dmrpp') ``` -------------------------------- ### Merging Xarray Datasets/DataArrays (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Combines multiple xarray DataArrays or Datasets into a single Dataset. This is typically used when combining objects that represent different variables. ```Python xarray.merge ``` -------------------------------- ### Get Writable Session for Append (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Obtains a new writable session for the 'main' branch of the Icechunk repository, which is necessary before performing the append operation. ```python append_session = repo.writable_session("main") ``` -------------------------------- ### Example Output of Accessed Chunk Manifest (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/data_structures.md This Python dictionary shows the typical output when accessing a Chunk Manifest from a VirtualiZarr dataset, indicating the location (path, offset, length) of a single chunk within a specified file. ```python {'0.0.0': {'path': 'file:///work/data/air.nc', 'offset': 15419, 'length': 7738000}} ``` -------------------------------- ### Combining Xarray Objects by Coordinates (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Combines multiple xarray objects by attempting to align them based on their coordinate variables. This method requires the coordinate variables to be loaded into memory. ```Python xarray.combine_by_coords ``` -------------------------------- ### Get Read-Only Session (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Obtains a read-only session for the 'main' branch of the Icechunk repository, allowing access to the data including the newly appended records. ```python read_session = repo.readonly_session(branch="main") ``` -------------------------------- ### Generating VirtualiZarr Test Coverage Reports (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Provides commands to generate test coverage reports locally using `pytest-cov` via `pixi`. Examples include generating a terminal report and an HTML report. Requires `pixi` and the test environment with `pytest-cov`. ```Bash pixi run --environment test run-tests-cov # Terminal report showing missing coverage pixi run --environment test run-tests-html-cov # HTML report written to htmlcov/index.html ``` -------------------------------- ### Initialize Icechunk Repository (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Sets up a local Icechunk repository, configures it with a virtual chunk container pointing to an anonymous S3 bucket, defines credentials, and opens/creates the repository with a writable session. ```python storage = icechunk.local_filesystem_storage("./noaa-cdr-icechunk") config = icechunk.RepositoryConfig.default() config.set_virtual_chunk_container( icechunk.VirtualChunkContainer("s3", "s3://", icechunk.s3_store(region="us-east-1")) ) credentials = icechunk.containers_credentials( s3=icechunk.s3_credentials(anonymous=True) ) repo = icechunk.Repository.open_or_create(storage, config, credentials) session = repo.writable_session("main") ``` -------------------------------- ### Combining Xarray Objects Nested (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Combines multiple xarray objects based on a nested structure, useful when coordinate data is not available for alignment or when combining along multiple dimensions simultaneously. ```Python xarray.combine_nested ``` -------------------------------- ### Getting Virtual Dataset Size (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Shows how to access the `nbytes` attribute of the virtual dataset's `virtualize` accessor (`vds.virtualize`) to demonstrate its significantly smaller memory footprint compared to the original dataset. ```python vds.virtualize.nbytes ``` -------------------------------- ### Splitting and Saving Xarray Dataset to NetCDF - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates how to split an existing xarray dataset (`ds`) into two parts based on the 'time' dimension using `isel` and save each part to a separate netCDF file ('air1.nc' and 'air2.nc'). This prepares the files for a subsequent combination example. ```python ds1 = ds.isel(time=slice(None, 1460)) ds2 = ds.isel(time=slice(1460, None)) ds1.to_netcdf('air1.nc') ds2.to_netcdf('air2.nc') ``` -------------------------------- ### Running VirtualiZarr Tests with Pixi (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Demonstrates how to execute the VirtualiZarr test suite using `pixi`. Includes commands for running all tests and skipping network-dependent tests for faster execution or offline use. Requires `pixi` and the test environment dependencies. ```Bash # Run all tests pixi run --environment test run-tests # Skip tests that require a network connection pixi run --environment test run-tests-no-network ``` -------------------------------- ### Concatenating Xarray Datasets/DataArrays (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/faq.md Concatenates multiple xarray DataArrays or Datasets along a specified dimension. This is commonly used when combining objects that represent the same variable but cover different ranges along a dimension. ```Python xarray.concat ``` -------------------------------- ### Define Path Rewriting Function (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Defines a Python function `local_to_s3_url` that takes a local file path string and returns a corresponding S3 URL string. This function is used as an example for transforming file locations when using the `rename_paths` method. ```python def local_to_s3_url(old_local_path: str) -> str: from pathlib import Path new_s3_bucket_url = "http://s3.amazonaws.com/my_bucket/" filename = Path(old_local_path).name return str(new_s3_bucket_url / filename) ``` -------------------------------- ### Create Virtual Datasets (Initial) (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Defines storage options for anonymous S3 access and creates a list of VirtualiZarr virtual datasets from the first two file paths obtained from the S3 listing. ```python so = dict(anon=True, default_fill_cache=False, default_cache_type="none") virtual_datasets = [ open_virtual_dataset(url, indexes={}, reader_options={"storage_options": so}) for url in oisst_files[0:2] ] ``` -------------------------------- ### Display Initial Virtual Dataset (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Prints the structure and contents of the concatenated initial virtual dataset to the console, showing its dimensions, coordinates, and data variables. ```python virtual_ds ``` -------------------------------- ### Import Libraries (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Imports the required Python libraries: warnings for filtering messages, fsspec for filesystem access, icechunk for store management, xarray for data handling, and open_virtual_dataset from virtualizarr. ```python import warnings import fsspec import icechunk import xarray as xr from virtualizarr import open_virtual_dataset warnings.filterwarnings("ignore", category=UserWarning) ``` -------------------------------- ### Import Required Libraries (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Imports the Python libraries needed for the notebook, including coiled for serverless functions, icechunk for storage, numpy for array operations, xarray for dataset handling, and open_virtual_dataset from virtualizarr. ```Python import coiled import icechunk import numpy as np import xarray as xr from virtualizarr import open_virtual_dataset ``` -------------------------------- ### Run Virtualizarr Script with Lithops (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/virtualizarr-with-lithops/README.md Executes the main Python script 'virtualizarr-with-lithops.py' to perform the virtual dataset generation using the configured lithops environment. ```Bash python virtualizarr-with-lithops.py ``` -------------------------------- ### Building Lithops AWS Lambda Runtime Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Sets the Lithops configuration file path and builds the Lithops runtime image for AWS Lambda using Docker, naming it `vz-runtime`. ```bash export LITHOPS_CONFIG_FILE=$(pwd)/lithops.yaml lithops runtime build -b aws_lambda -f Dockerfile vz-runtime ``` -------------------------------- ### Using the Package Command-Line Interface Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md General syntax for running functions provided by the package via the command line, specifying the function name and optional arguments. ```bash python main.py [options] ``` -------------------------------- ### List Remote Files (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Initializes an anonymous S3 filesystem using fsspec and lists the paths of OISST NetCDF files for August 2024 from a public S3 bucket, then sorts the list. ```python fs = fsspec.filesystem("s3", anon=True) oisst_files = fs.glob( "s3://noaa-cdr-sea-surface-temp-optimum-interpolation-pds/data/v2.1/avhrr/202408/oisst-avhrr-v02r01.*.nc" ) oisst_files = sorted(["s3://" + f for f in oisst_files]) ``` -------------------------------- ### Writing Virtualizarr to Icechunk Store (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates initializing an Icechunk repository and session, then writing a Virtualizarr dataset to the Icechunk store. This uses the `to_icechunk` method to serialize the virtual dataset's references into the transactional Icechunk format. ```python # create an icechunk repository, session and write the virtual dataset to the session import icechunk storage = icechunk.local_filesystem_storage("./local/icechunk/store") # By default, local virtual references and public remote virtual references can be read without extra configuration. repo = icechunk.Repository.create(storage) session = repo.writable_session("main") # write the virtual dataset to the session with the IcechunkStore vds1.virtualize.to_icechunk(session.store) session.commit("Wrote first dataset") ``` -------------------------------- ### Open Icechunk Store with Xarray (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Opens the Icechunk store containing the virtual dataset references using `xr.open_zarr`. It points to the store managed by the Icechunk session and specifies `consolidated=False` and `zarr_format=3` for compatibility. This allows accessing the virtual dataset via the Xarray interface. ```Python combined_ds = xr.open_zarr(session.store, consolidated=False, zarr_format=3) ``` -------------------------------- ### Creating and Saving Virtual Zarr References with VirtualiZarr (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/index.md This snippet shows the core workflow of VirtualiZarr. It imports `open_virtual_dataset`, iterates through files to create virtual datasets, combines them into a single virtual dataset using `xarray.combine_nested`, and then saves the virtual references to a Kerchunk-compatible JSON file using the `.virtualize.to_kerchunk()` method. This process creates a lightweight reference file without copying the original data. ```Python from virtualizarr import open_virtual_dataset virtual_datasets = [ open_virtual_dataset(filepath) for filepath in glob.glob('/my/files*.nc') ] # this Dataset wraps a bunch of virtual ManifestArray objects directly virtual_ds = xr.combine_nested(virtual_datasets, concat_dim=['time']) # cache the combined dataset pattern to disk, in this case using the existing kerchunk specification for reference files virtual_ds.virtualize.to_kerchunk('combined.json', format='json') ``` -------------------------------- ### Running VirtualiZarr Tests Manually in Pixi Shell (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/contributing.md Explains how to enter a `pixi` environment shell to execute commands like `pytest` directly. Useful for debugging or running specific commands not aliased by `pixi` tasks. Requires `pixi`. ```Bash # Start a shell within the environment pixi shell --environment test # Run the tests pytest virtualizarr # Exit the shell exit ``` -------------------------------- ### Creating NetCDF File with xarray (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates how to use the xarray library to open a tutorial dataset and save it as a netCDF file named 'air.nc' on disk. This file serves as the input for subsequent virtual dataset operations. ```python import xarray as xr # create an example pre-existing netCDF4 file ds = xr.tutorial.open_dataset('air_temperature') ds.to_netcdf('air.nc') ``` -------------------------------- ### Build Lithops Runtime Image for AWS Lambda (Bash) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/virtualizarr-with-lithops/README.md Sets the LITHOPS_CONFIG_FILE environment variable to the local lithops.yaml path and builds a lithops runtime image named 'virtualizarr-runtime' for the AWS Lambda backend using the specified Dockerfile. ```Bash export LITHOPS_CONFIG_FILE=$(pwd)/lithops.yaml lithops runtime build -b aws_lambda -f Dockerfile_virtualizarr virtualizarr-runtime ``` -------------------------------- ### Verify Initial Data in Icechunk (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Opens the Icechunk store using xarray's `open_zarr` function to read the data that was just written and committed, allowing verification of the initial write operation. ```python ds = xr.open_zarr(session.store, consolidated=False, zarr_format=3) ds ``` -------------------------------- ### Display Opened Dataset Structure (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Displays the structure and contents of the virtual dataset loaded from the Icechunk store (`combined_ds`). This shows the variables, dimensions, coordinates, and attributes, confirming that the dataset was successfully opened. ```Python combined_ds ``` -------------------------------- ### Concatenate Virtual Datasets (Initial) (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Concatenates the list of initial virtual datasets created from the first two files into a single xarray Dataset along the 'time' dimension. ```python virtual_ds = xr.concat( virtual_datasets, dim="time", coords="minimal", compat="override", combine_attrs="override", ) ``` -------------------------------- ### Opening Virtual Zarr Store with xarray and Kerchunk Engine (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/index.md This snippet shows how to access the virtual Zarr store defined by the Kerchunk reference file created in the previous step. It uses `xarray.open_dataset` with the path to the reference file, specifying `engine='kerchunk'`. The `chunks={}` argument is often used with kerchunk to avoid immediate chunk loading. The resulting `ds` object behaves like a standard xarray Dataset, allowing performant access to the data within the original files. ```Python ds = xr.open_dataset('combined.json', engine='kerchunk', chunks={}) # normal xarray.Dataset object, wrapping dask/numpy arrays etc. ``` -------------------------------- ### Create Virtual Datasets (Append) (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Creates a new list of VirtualiZarr virtual datasets from the next two file paths (indices 2 to 4) from the S3 listing, specifically for the append operation. ```python virtual_datasets_a = [ open_virtual_dataset( url, indexes={}, reader_options={"storage_options": {"anon": True}} ) for url in oisst_files[2:4] ] ``` -------------------------------- ### Write Initial Data to Icechunk (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Writes the data from the initial concatenated virtual dataset (`virtual_ds`) into the Icechunk store using the writable session obtained during repository initialization. ```python virtual_ds.virtualize.to_icechunk(session.store) ``` -------------------------------- ### Opening Kerchunk References with Relative Paths (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Handles opening Kerchunk reference files that contain relative paths by specifying the root directory of the filesystem using the `fs_root` argument. This ensures relative paths are correctly resolved. ```python # file `relative_refs.json` contains a path like './file.nc' vds = open_virtual_dataset( 'relative_refs.json', filetype='kerchunk', virtual_backend_kwargs={'fs_root': 'file:///some_directory/'} ) ``` -------------------------------- ### Verify Appended Data in Icechunk (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Opens the Icechunk store using xarray's `open_zarr` via the read-only session to verify that the data was successfully appended and is now part of the dataset. ```python ds = xr.open_zarr(read_session.store, consolidated=False, zarr_format=3) ds ``` -------------------------------- ### Loading Specific Variables in Virtual Dataset - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates how to open a virtual dataset from a netCDF file ('air.nc') and specify which variables ('air', 'time') should be loaded into memory as regular variables rather than remaining as virtual variables. ```python vds = open_virtual_dataset('air.nc', loadable_variables=['air', 'time']) ``` -------------------------------- ### Opening NetCDF as Virtual Dataset (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Shows how to use the `virtualizarr.open_virtual_dataset` function to create a virtual representation of the previously saved 'air.nc' netCDF file. The function automatically infers the file format. ```python from virtualizarr import open_virtual_dataset vds = open_virtual_dataset('air.nc') ``` -------------------------------- ### Combine Virtual Datasets (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Collects the results from the coiled serverless functions into a list. It then uses `xr.combine_by_coords` to merge the individual virtual datasets generated for each file into a single, combined virtual Xarray dataset (`mds`), handling coordinate alignment and attribute dropping. ```Python # extract generator values into a list vds_list = [result for result in results] # combine individual refs into a virtual Xarray dataset mds = xr.combine_by_coords( vds_list, coords="minimal", compat="override", combine_attrs="drop" ) mds ``` -------------------------------- ### Define and Map Coiled Serverless Function (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Defines a Python function `process` decorated with `@coiled.function` to run on Coiled serverless workers. This function opens a single NetCDF file as a virtual dataset using `open_virtual_dataset`. The code then uses `process.map` to distribute the processing of a subset of file URLs (`combinations[0:2]`) across the coiled workers with retry logic. ```Python @coiled.function( region="us-west-2", vm_type=["t4g.small"], spot_policy="spot_with_fallback", arm=True, idle_timeout="10 minutes", n_workers=[10, 100], name="parallel_reference_generation", ) def process(filename): vds = open_virtual_dataset( filename, decode_times=True, loadable_variables=["time", "lat", "lon", "crs"], ) return vds # process.map distributes out the input file urls to coiled functions # retires=10 allows for individual task retires, which can be useful for inconsistent server behavior results = process.map(combinations[0:2], retries=10) ``` -------------------------------- ### Print Estimated Dataset Size (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Calculates the estimated size of the combined virtual dataset (`mds`) in terabytes by accessing its `nbytes` property and dividing by 1e12. It then prints the formatted size to two decimal places. ```Python print(str("{0:.2f}".format(mds.nbytes / 1e12)), " TB") ``` -------------------------------- ### Commit Initial Changes (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Commits the changes made to the Icechunk store during the initial data write operation, saving them permanently to the repository's 'main' branch with a descriptive message. ```python session.commit("first 2 days of 202408 data") ``` -------------------------------- ### Generate Terraclimate File URLs (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/coiled/terraclimate.ipynb Defines the list of Terraclimate variables and the year range. It then generates a list of URLs for each NetCDF file by combining the base URL, variable names, and years. This list serves as the input for the parallel processing step. ```Python tvars = [ "aet", "def", "pet", "ppt", "q", "soil", "srad", "swe", "tmax", "tmin", "vap", "ws", "vpd", "PDSI", ] min_year = 1958 max_year = 2023 time_list = np.arange(min_year, max_year + 1, 1) combinations = [ f"https://climate.northwestknowledge.net/TERRACLIMATE-DATA/TerraClimate_{var}_{year}.nc" for year in time_list for var in tvars ] ``` -------------------------------- ### Opening Virtual Datasets (Manual Concatenation) - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Opens two NetCDF files as virtual datasets using `open_virtual_dataset` from VirtualiZarr for subsequent manual concatenation. ```python vds1 = open_virtual_dataset('air1.nc') vds2 = open_virtual_dataset('air2.nc') ``` -------------------------------- ### Programmatic Usage: Writing Data to Icechunk Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/mursst-icechunk-with-lithops/README.md Python code demonstrating how to import and call the `write_to_icechunk` function directly within a script, passing date arguments. ```python from lithops_functions import write_to_icechunk # Write data to Icechunk write_to_icechunk(start_date="2022-01-01", end_date="2022-01-31") ``` -------------------------------- ### Cleanup Icechunk Store (Shell) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/examples/append/noaa-cdr-sst.ipynb Provides a commented-out shell command to remove the local directory used for the icechunk store, useful for cleaning up between runs. ```shell #!rm -rf ./noaa-cdr-icechunk/ ``` -------------------------------- ### Opening Virtual Datasets (Coordinate Ordering) - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Opens two NetCDF files as virtual datasets using `open_virtual_dataset` from VirtualiZarr in preparation for concatenation ordered automatically by coordinate values. ```python vds1 = open_virtual_dataset('air1.nc') vds2 = open_virtual_dataset('air2.nc') ``` -------------------------------- ### Loading Variables with CF-Convention Time Decoding - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Illustrates how to open a virtual dataset, load specific variables ('air', 'time'), and additionally enable CF-convention decoding for time variables by setting `decode_times=True`. This ensures time coordinates are parsed as datetime objects. ```python vds = open_virtual_dataset( 'air.nc', loadable_variables=['air', 'time'], decode_times=True, ) ``` -------------------------------- ### Reading Kerchunk Parquet with Xarray (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Opens a Kerchunk Parquet reference file as an Xarray dataset using fsspec's kerchunk engine. This provides access to the data referenced in the Parquet file. ```python combined_ds = xr.open_dataset('combined.parquet', engine="kerchunk") ``` -------------------------------- ### Writing Virtualizarr to Kerchunk Parquet (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Serializes a Virtualizarr dataset into a Kerchunk-compliant Parquet reference file. This format is more suitable for large datasets with many chunks compared to JSON. ```python combined_vds.virtualize.to_kerchunk('combined.parquet', format='parquet') ``` -------------------------------- ### Apply Path Rewriting Function (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Demonstrates how to apply a custom path rewriting function (`local_to_s3_url`) to a virtual dataset (`vds`) using the `vds.virtualize.rename_paths` method. It then accesses and displays the resulting manifest dictionary for the 'air' variable to show the effect of the renaming. ```python renamed_vds = vds.virtualize.rename_paths(local_to_s3_url) renamed_vds['air'].data.manifest.dict() ``` -------------------------------- ### Writing Virtualizarr to Kerchunk JSON (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Serializes a Virtualizarr dataset into a Kerchunk-compliant JSON reference file. This method generates byte range references for the virtual dataset's data, saving them to the specified file path. ```python combined_vds.virtualize.to_kerchunk('combined.json', format='json') ``` -------------------------------- ### Opening Kerchunk References as Virtualizarr VDS (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Opens existing Kerchunk JSON or Parquet reference files directly as a Virtualizarr virtual dataset object. This allows converting or further processing Kerchunk references within the Virtualizarr framework. ```python vds = open_virtual_dataset('combined.json', filetype='kerchunk') # or vds = open_virtual_dataset('combined.parquet', filetype='kerchunk') ``` -------------------------------- ### Inspecting Virtual Dataset Manifest (concat) - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Accesses and prints the manifest dictionary for the 'air' data variable within the combined virtual dataset created by `xarray.concat`, showing the underlying chunk references and their order. ```python combined_vds['air'].data.manifest.dict() ``` -------------------------------- ### Inspecting Virtual Dataset Manifest (combine_by_coords) - Python Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Accesses and prints the manifest dictionary for the 'air' data variable within the combined virtual dataset created by `xarray.combine_by_coords`, showing the underlying chunk references ordered by coordinate values. ```python combined_vds['air'].data.manifest.dict() ``` -------------------------------- ### Opening Remote File as Virtual Dataset (Python) Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Illustrates how to open a remote netCDF file stored on Amazon S3 as a virtual dataset by passing connection details (like AWS credentials) via the `reader_options` parameter to `open_virtual_dataset`. ```python aws_credentials = {"key": ..., "secret": ...} vds = open_virtual_dataset("s3://some-bucket/file.nc", reader_options={'storage_options': aws_credentials}) ``` -------------------------------- ### Output of Virtual Dataset with Loaded Variables - Text Source: https://github.com/zarr-developers/virtualizarr/blob/develop/docs/usage.md Shows the xarray Dataset representation after opening with specific variables ('air', 'time') loaded. Note that 'air' and 'time' are backed by standard arrays, while 'lat' and 'lon' are backed by `ManifestArray` indicating they are virtual. ```text Size: 31MB Dimensions: (time: 2920, lat: 25, lon: 53) Coordinates: * time (time) datetime64[ns] 23kB 2013-01-01 ... 2014-12-31T18:00:00 lat (lat) float32 100B ManifestArray Size: 31MB Dimensions: (time: 2920, lat: 25, lon: 53) Coordinates: * time (time) datetime64[ns] 23kB 2013-01-01 ... 2014-12-31T18:00:00 lat (lat) float32 100B ManifestArray