Skip to content

Commit

Permalink
Add kerchunk and fastparquet to requirements; enhance xarray function…
Browse files Browse the repository at this point in the history
… with dynamic kwargs
  • Loading branch information
andersy005 committed Oct 24, 2024
1 parent 094a799 commit f9f4f33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildpacks = ["heroku/buildpack-python:0.17.0"]

[[vm]]
size = "shared-cpu-1x"
memory = "2048mb"
memory = "4096mb"


[env]
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ xarray>=2022.9
zarr
fastapi-cache2 @ git+https://github.com/andersy005/fastapi-cache@pydantic-v2-compat # for pydantic v2 compatilibity
universal_pathlib
kerchunk >=0.2.6
fastparquet
18 changes: 16 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import traceback
import typing
from contextlib import asynccontextmanager

import pydantic
Expand All @@ -15,6 +16,12 @@
from .helpers import sanitize_url
from .log import get_logger


class XarrayOpenKwargs(pydantic.BaseModel):
engine: typing.Literal['zarr', 'kerchunk'] = 'zarr'
chunks: dict = {}


origins = ['*']

logger = get_logger()
Expand Down Expand Up @@ -75,8 +82,15 @@ async def xarray(
description='URL to a zarr store',
example='https://ncsa.osn.xsede.org/Pangeo/pangeo-forge/HadISST-feedstock/hadisst.zarr',
),
xarray_open_kwargs: str = Query(
default='{"engine": "zarr", "chunks": {}}',
description='Keyword arguments for xr.open_dataset',
),
):
logger.info(f'🚀 Starting to process request for URL: {url}')
kwargs_model = XarrayOpenKwargs.model_validate_json(xarray_open_kwargs)
logger.info(
f'🚀 Starting to process request for URL: {url} with kwargs: {kwargs_model.model_dump()}'
)

import time

Expand All @@ -93,7 +107,7 @@ async def xarray(

try:
logger.info(f'📂 Attempting to open dataset from URL: {url}')
ds = xr.open_dataset(sanitized_url, engine='zarr', chunks={})
ds = xr.open_dataset(sanitized_url, **kwargs_model.model_dump())
logger.info('✅ Successfully opened dataset. Generating HTML representation.')
html = ds._repr_html_().strip()

Expand Down

0 comments on commit f9f4f33

Please sign in to comment.