Skip to content

Instantly share code, notes, and snippets.

@gmaze
Last active October 17, 2024 06:25
Show Gist options
  • Save gmaze/9b98726da291f15ae18c8c2c7f1b079b to your computer and use it in GitHub Desktop.
Save gmaze/9b98726da291f15ae18c8c2c7f1b079b to your computer and use it in GitHub Desktop.
Convert an Argo multi-prof netcdf into zarr
#!/usr/bin/env python
# coding: utf-8
"""
Convert an Argo multi-prof netcdf into zarr
"""
import xarray as xr
import zarr
from numcodecs import blosc
import argopy
from argopy.stores import httpstore
# Define WMO to process:
wmo = 6902914
# Get the url toward the multi profiles netcdf file:
uri = argopy.DataFetcher(src='gdac').float(wmo).uri[0]
# Then download data in memory as a xarray dataset:
ds = httpstore().open_dataset(uri, xr_opts={'engine': 'argo'})
# Define zarr compression:
compressor = zarr.Blosc(cname="zstd", clevel=3, shuffle=2)
encoding = {}
for v in ds:
encoding.update({v: {"compressor": compressor}})
# And convert netcdf data to a zarr file using compression:
ds.to_zarr('6902914_prof.zarr', encoding=encoding)
# Open new zarr file to check content:
xr.open_dataset('6902914_prof.zarr', engine='zarr')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment