-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9340cb6
commit 4b421a7
Showing
5 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import s3fs | ||
import xarray as xr | ||
|
||
from dagster import AssetIn, AssetOut, asset, ConfigSchema | ||
from dagster_aws.s3 import S3Resource | ||
|
||
@asset( | ||
name="AsZarr", | ||
ins={ | ||
"RawLOCA2": AssetIn() | ||
}) | ||
def as_zarr(context, | ||
RawLOCA2, | ||
s3: S3Resource): | ||
context.log.info(f"Converting {RawLOCA2['s3_key']} to zarr") | ||
# Get S3 client from resource | ||
s3_client = context.resources.s3.get_client() | ||
|
||
# Initialize s3fs with the same credentials as the S3Resource | ||
fs = s3fs.S3FileSystem(client_kwargs={"session": s3_client._session}) | ||
|
||
# Construct S3 paths | ||
input_path = f"s3://{RawLOCA2['bucket']}/{RawLOCA2['s3_key']}" | ||
zarr_key = RawLOCA2['s3_key'].replace('.nc', '.zarr') | ||
output_path = f"s3://{RawLOCA2['bucket']}/{zarr_key}" | ||
|
||
# Read NetCDF file from S3 | ||
with fs.open(input_path, 'rb') as f: | ||
ds = xr.open_dataset(f) | ||
|
||
# Write to Zarr format | ||
ds.to_zarr( | ||
store=s3fs.S3Map(root=output_path, s3=fs), | ||
mode='w' # Overwrite if exists | ||
) | ||
|
||
# Close the dataset to free memory | ||
ds.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters