Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : for a added feature in weather-mv to extract specific date's data from zarr files. #400

Merged
merged 5 commits into from
Oct 5, 2023
Merged
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
14 changes: 12 additions & 2 deletions weather_mv/loader_pipeline/bq.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class ToBigQuery(ToDataSink):
skip_creating_polygon: bool = False
lat_grid_resolution: t.Optional[float] = None
lon_grid_resolution: t.Optional[float] = None
start_date: t.Optional[str] = None
end_date: t.Optional[str] = None

@classmethod
def add_parser_arguments(cls, subparser: argparse.ArgumentParser):
Expand Down Expand Up @@ -154,8 +156,10 @@ def validate_arguments(cls, known_args: argparse.Namespace, pipeline_args: t.Lis
_, uri_extension = os.path.splitext(known_args.uris)
if (uri_extension in ['.tif', '.tiff'] and not known_args.tif_metadata_for_start_time):
raise RuntimeError("'--tif_metadata_for_start_time' is required for tif files.")
elif (uri_extension not in ['.tif', '.tiff'] and (known_args.tif_metadata_for_start_time
or known_args.tif_metadata_for_end_time)):
elif uri_extension not in ['.tif', '.tiff'] and (
known_args.tif_metadata_for_start_time
or known_args.tif_metadata_for_end_time
):
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
raise RuntimeError("'--tif_metadata_for_start_time' and "
"'--tif_metadata_for_end_time' can be specified only for tif files.")

Expand All @@ -171,6 +175,8 @@ def __post_init__(self):
"""Initializes Sink by creating a BigQuery table based on user input."""
if self.zarr:
self.xarray_open_dataset_kwargs = self.zarr_kwargs
self.start_date = self.zarr_kwargs.get('start_date')
self.end_date = self.zarr_kwargs.get('end_date')
with open_dataset(self.first_uri, self.xarray_open_dataset_kwargs,
self.disable_grib_schema_normalization, self.tif_metadata_for_start_time,
self.tif_metadata_for_end_time, is_zarr=self.zarr) as open_ds:
Expand Down Expand Up @@ -311,6 +317,10 @@ def expand(self, paths):
xarray_open_dataset_kwargs = self.xarray_open_dataset_kwargs.copy()
xarray_open_dataset_kwargs.pop('chunks')
ds, chunks = xbeam.open_zarr(self.first_uri, **xarray_open_dataset_kwargs)

if self.start_date is not None and self.end_date is not None:
Copy link
Collaborator

@alxmrs alxmrs Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be useful for non-Zarr datasets? I'm on the fence.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR: #396 is useful for the non-zarr datasets and this PR will be working on zarr datasets.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alxmrs I found that for non-zarr datasets it isn't working so I added a issue & PR. I tested for zarr & non-zarr dataset and it's working with this new PR's code.

ds = ds.sel(time=slice(self.start_date, self.end_date))

ds.attrs[DATA_URI_COLUMN] = self.first_uri
extracted_rows = (
paths
Expand Down
6 changes: 6 additions & 0 deletions weather_mv/loader_pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import json
import logging
import typing as t
import warnings

import apache_beam as beam
from apache_beam.io.filesystems import FileSystems
Expand Down Expand Up @@ -145,6 +146,11 @@ def run(argv: t.List[str]) -> t.Tuple[argparse.Namespace, t.List[str]]:
if known_args.zarr_kwargs and not known_args.zarr:
raise ValueError('`--zarr_kwargs` argument is only allowed with valid Zarr input URI.')

if known_args.zarr_kwargs:
if not known_args.zarr_kwargs.get('start_date') or not known_args.zarr_kwargs.get('end_date'):
warnings.warn('`--zarr_kwargs` not contains both `start_date` and `end_date`'
'so whole zarr-dataset will ingested.')

if known_args.zarr:
known_args.zarr_kwargs['chunks'] = known_args.zarr_kwargs.get('chunks', None)
known_args.zarr_kwargs['consolidated'] = known_args.zarr_kwargs.get('consolidated', True)
Expand Down
Loading