!mamba install kerchunk -y --quiet
!mamba install h5py=3.2 -y --quiet
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import s3fs
import datetime as dt
import logging
import fsspec
import ujson
import requests
from tqdm import tqdm
from glob import glob
import kerchunk.hdf
s3_cred_endpoint = {
    'podaac':'https://archive.podaac.earthdata.nasa.gov/s3credentials',
    'lpdaac':'https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials',
    'ornldaac':'https://data.ornldaac.earthdata.nasa.gov/s3credentials'
}
def get_temp_creds():
    temp_creds_url = s3_cred_endpoint['ornldaac']
    return requests.get(temp_creds_url).json()
temp_creds_req = get_temp_creds()
#fs = fsspec.filesystem('s3', anon=True)
fs = s3fs.S3FileSystem(anon=False, key=temp_creds_req['accessKeyId'], secret=temp_creds_req['secretAccessKey'], token=temp_creds_req['sessionToken'])
urls = ['s3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2015.nc',
 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2016.nc',
 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2017.nc',
 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2018.nc',
 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2019.nc',
 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2020.nc']
def gen_json(u):
    so = dict(
        mode="rb", anon=False, default_fill_cache=False,
        default_cache_type="none"
    )
    with fs.open(u, **so) as infile:
        print(u)
        h5chunks = kerchunk.hdf.SingleHdf5ToZarr(infile, u, inline_threshold=300)
        with open(f"jsons/{u.split('/')[-1]}.json", 'wb') as outfile:
           outfile.write(ujson.dumps(h5chunks.translate()).encode())
for u in urls:
    gen_json(u)
u = 's3://ornl-cumulus-prod-protected/daymet/Daymet_Daily_V4/data/daymet_v4_daily_na_tmax_2015.nc'
so = dict(
    mode="rb", anon=False, default_fill_cache=False, default_cache_type="none"
    )
inf = fs.open(u, **so)
h5chunks = kerchunk.hdf.SingleHdf5ToZarr(inf, u, inline_threshold=300)
h5chunks.translate()
---------------------------------------------------------------------------
ClientError                               Traceback (most recent call last)
File /srv/conda/envs/notebook/lib/python3.9/site-packages/s3fs/core.py:261, in S3FileSystem._call_s3(self, method, *akwarglist, **kwargs)
    260 try:
--> 261     out = await method(**additional_kwargs)
    262     return out

File /srv/conda/envs/notebook/lib/python3.9/site-packages/aiobotocore/client.py:180, in AioBaseClient._make_api_call(self, operation_name, api_params)
    179     error_class = self.exceptions.from_code(error_code)
--> 180     raise error_class(parsed_response, operation_name)
    181 else:

ClientError: An error occurred (ExpiredToken) when calling the GetObject operation: The provided token has expired.

The above exception was the direct cause of the following exception:

PermissionError                           Traceback (most recent call last)
Input In [22], in <cell line: 1>()
----> 1 h5chunks.translate()

File /srv/conda/envs/notebook/lib/python3.9/site-packages/kerchunk/hdf.py:73, in SingleHdf5ToZarr.translate(self)
     71 lggr.debug('Translation begins')
     72 self._transfer_attrs(self._h5f, self._zroot)
---> 73 self._h5f.visititems(self._translator)
     74 if self.inline > 0:
     75     self._do_inline(self.inline)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/h5py/_hl/group.py:592, in Group.visititems(self, func)
    590     name = self._d(name)
    591     return func(name, self[name])
--> 592 return h5o.visit(self.id, proxy)

File h5py/_objects.pyx:54, in h5py._objects.with_phil.wrapper()

File h5py/_objects.pyx:55, in h5py._objects.with_phil.wrapper()

File h5py/h5o.pyx:355, in h5py.h5o.visit()

File h5py/h5o.pyx:302, in h5py.h5o.cb_obj_simple()

File /srv/conda/envs/notebook/lib/python3.9/site-packages/h5py/_hl/group.py:591, in Group.visititems.<locals>.proxy(name)
    589 """ Use the text name of the object, not bytes """
    590 name = self._d(name)
--> 591 return func(name, self[name])

File /srv/conda/envs/notebook/lib/python3.9/site-packages/kerchunk/hdf.py:185, in SingleHdf5ToZarr._translator(self, name, h5obj)
    182     filters.append(numcodecs.Shuffle(elementsize=h5obj.dtype.itemsize))
    184 # Get storage info of this HDF5 dataset...
--> 185 cinfo = self._storage_info(h5obj)
    186 if h5py.h5ds.is_scale(h5obj.id) and not cinfo:
    187     return

File /srv/conda/envs/notebook/lib/python3.9/site-packages/kerchunk/hdf.py:300, in SingleHdf5ToZarr._storage_info(self, dset)
    298 chunk_size = dset.chunks
    299 for index in range(num_chunks):
--> 300     blob = dsid.get_chunk_info(index)
    301     key = tuple(
    302         [a // b for a, b in zip(blob.chunk_offset, chunk_size)])
    303     stinfo[key] = {'offset': blob.byte_offset, 'size': blob.size}

File h5py/_objects.pyx:54, in h5py._objects.with_phil.wrapper()

File h5py/_objects.pyx:55, in h5py._objects.with_phil.wrapper()

File h5py/h5d.pyx:576, in h5py.h5d.DatasetID.get_chunk_info()

File h5py/h5fd.pyx:162, in h5py.h5fd.H5FD_fileobj_read()

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/spec.py:1575, in AbstractBufferedFile.readinto(self, b)
   1570 """mirrors builtin file's readinto method
   1571 
   1572 https://docs.python.org/3/library/io.html#io.RawIOBase.readinto
   1573 """
   1574 out = memoryview(b).cast("B")
-> 1575 data = self.read(out.nbytes)
   1576 out[: len(data)] = data
   1577 return len(data)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/spec.py:1565, in AbstractBufferedFile.read(self, length)
   1562 if length == 0:
   1563     # don't even bother calling fetch
   1564     return b""
-> 1565 out = self.cache._fetch(self.loc, self.loc + length)
   1566 self.loc += len(out)
   1567 return out

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/caching.py:381, in BytesCache._fetch(self, start, end)
    379 elif start < self.start:
    380     if self.end - end > self.blocksize:
--> 381         self.cache = self.fetcher(start, bend)
    382         self.start = start
    383     else:

File /srv/conda/envs/notebook/lib/python3.9/site-packages/s3fs/core.py:1896, in S3File._fetch_range(self, start, end)
   1894 def _fetch_range(self, start, end):
   1895     try:
-> 1896         return _fetch_range(
   1897             self.fs,
   1898             self.bucket,
   1899             self.key,
   1900             self.version_id,
   1901             start,
   1902             end,
   1903             req_kw=self.req_kw,
   1904         )
   1906     except OSError as ex:
   1907         if ex.args[0] == errno.EINVAL and "pre-conditions" in ex.args[1]:

File /srv/conda/envs/notebook/lib/python3.9/site-packages/s3fs/core.py:2038, in _fetch_range(fs, bucket, key, version_id, start, end, req_kw)
   2036     return b""
   2037 logger.debug("Fetch: %s/%s, %s-%s", bucket, key, start, end)
-> 2038 resp = fs.call_s3(
   2039     "get_object",
   2040     Bucket=bucket,
   2041     Key=key,
   2042     Range="bytes=%i-%i" % (start, end - 1),
   2043     **version_id_kw(version_id),
   2044     **req_kw,
   2045 )
   2046 return sync(fs.loop, resp["Body"].read)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/asyn.py:91, in sync_wrapper.<locals>.wrapper(*args, **kwargs)
     88 @functools.wraps(func)
     89 def wrapper(*args, **kwargs):
     90     self = obj or args[0]
---> 91     return sync(self.loop, func, *args, **kwargs)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/asyn.py:71, in sync(loop, func, timeout, *args, **kwargs)
     69     raise FSTimeoutError from return_result
     70 elif isinstance(return_result, BaseException):
---> 71     raise return_result
     72 else:
     73     return return_result

File /srv/conda/envs/notebook/lib/python3.9/site-packages/fsspec/asyn.py:25, in _runner(event, coro, result, timeout)
     23     coro = asyncio.wait_for(coro, timeout=timeout)
     24 try:
---> 25     result[0] = await coro
     26 except Exception as ex:
     27     result[0] = ex

File /srv/conda/envs/notebook/lib/python3.9/site-packages/s3fs/core.py:281, in S3FileSystem._call_s3(self, method, *akwarglist, **kwargs)
    279         err = e
    280 err = translate_boto_error(err)
--> 281 raise err

PermissionError: The provided token has expired.