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

Add fsspec functionality to viirs_sdr reader #2534

Merged
merged 9 commits into from
Jan 10, 2024
20 changes: 16 additions & 4 deletions satpy/readers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/readers/__init__.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.87 to 4.94, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2018 Satpy developers
#
Expand All @@ -20,6 +20,7 @@

import logging
import os
import pathlib
import pickle # nosec B403
import warnings
from datetime import datetime, timedelta
Expand Down Expand Up @@ -778,9 +779,20 @@


def open_file_or_filename(unknown_file_thing):
"""Try to open the *unknown_file_thing*, otherwise return the filename."""
try:
f_obj = unknown_file_thing.open()
except AttributeError:
"""Try to open the provided file "thing" if needed, otherwise return the filename or Path.

This wraps the logic of getting something like an fsspec OpenFile object
that is not directly supported by most reading libraries and making it
usable. If a :class:`pathlib.Path` object or something that is not
open-able is provided then that object is passed along. In the case of
fsspec OpenFiles their ``.open()`` method is called and the result returned.

"""
if isinstance(unknown_file_thing, pathlib.Path):
f_obj = unknown_file_thing
else:
try:
f_obj = unknown_file_thing.open()
except AttributeError:
f_obj = unknown_file_thing
return f_obj
10 changes: 7 additions & 3 deletions satpy/readers/hdf5_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
import xarray as xr

from satpy.readers import open_file_or_filename
from satpy.readers.file_handlers import BaseFileHandler
from satpy.readers.utils import np2str
from satpy.utils import get_legacy_chunk_size
Expand All @@ -43,7 +44,8 @@ def __init__(self, filename, filename_info, filetype_info):
self._attrs_cache = {}

try:
file_handle = h5py.File(self.filename, "r")
f_obj = open_file_or_filename(self.filename)
file_handle = h5py.File(f_obj, "r")
except IOError:
LOG.exception(
"Failed reading file %s. Possibly corrupted file", self.filename)
Expand Down Expand Up @@ -73,7 +75,8 @@ def _collect_attrs(self, name, attrs):

def get_reference(self, name, key):
"""Get reference."""
with h5py.File(self.filename, "r") as hf:
f_obj = open_file_or_filename(self.filename)
with h5py.File(f_obj, "r") as hf:
return self._get_reference(hf, hf[name].attrs[key])

def _get_reference(self, hf, ref):
Expand All @@ -97,7 +100,8 @@ def __getitem__(self, key):
val = self.file_content[key]
if isinstance(val, h5py.Dataset):
# these datasets are closed and inaccessible when the file is closed, need to reopen
dset = h5py.File(self.filename, "r")[key]
f_obj = open_file_or_filename(self.filename)
dset = h5py.File(f_obj, "r")[key]
dset_data = da.from_array(dset, chunks=CHUNK_SIZE)
attrs = self._attrs_cache.get(key, dset.attrs)
if dset.ndim == 2:
Expand Down
Loading
Loading