Skip to content

Commit

Permalink
FIX: Some parts of hdf/netcdf datasets require the file-like object t…
Browse files Browse the repository at this point in the history
…o still be open.
  • Loading branch information
jmilloy committed Jul 9, 2020
1 parent 2bba5cd commit 69a640d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions podpac/core/data/dataset_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def open_dataset(self, fp):
return xr.open_dataset(fp)

def close_dataset(self):
super(Dataset, self).close_dataset()
self.dataset.close()

@cached_property
Expand Down
29 changes: 16 additions & 13 deletions podpac/core/data/file_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,32 @@ def _dataset_caching_node(self):

@cached_property
def dataset(self):
# get from the cache
# use the _dataset_caching_node "stub" here because the only node attr we care about is the source
if self.cache_dataset and self._dataset_caching_node.has_cache(key="dataset"):
data = self._dataset_caching_node.get_cache(key="dataset")
with BytesIO(data) as f:
return self._open(BytesIO(data), cache=False)
elif self.source.startswith("s3://"):
self._file = BytesIO(data)
return self._open(self._file, cache=False)

# otherwise, open the file
if self.source.startswith("s3://"):
_logger.info("Loading AWS resource: %s" % self.source)
with self.s3.open(self.source, "rb") as f:
return self._open(f)
self._file = self.s3.open(self.source, "rb")
elif self.source.startswith("http://") or self.source.startswith("https://"):
_logger.info("Downloading: %s" % self.source)
response = requests.get(self.source)
with BytesIO(response.content) as f:
return self._open(f)
self._file = BytesIO(response.content)
elif self.source.startswith("ftp://"):
_logger.info("Downloading: %s" % self.source)
addinfourl = urlopen(self.source)
with BytesIO(addinfourl.read()) as f:
return self._open(f)
self._file = BytesIO(addinfourl.read())
elif self.source.startswith("file://"):
addinfourl = urlopen(self.source)
with BytesIO(addinfourl.read()) as f:
return self._open(f)
self._file = BytesIO(addinfourl.read())
else:
with open(self.source, "rb") as f:
return self._open(f)
self._file = open(self.source, "rb")

return self._open(self._file)

def _open(self, f, cache=True):
if self.cache_dataset and cache:
Expand All @@ -124,6 +124,9 @@ def open_dataset(self, f):
""" TODO """
raise NotImplementedError()

def close_dataset(self):
self._file.close()


@common_doc(COMMON_DATA_DOC)
class FileKeysMixin(tl.HasTraits):
Expand Down
1 change: 1 addition & 0 deletions podpac/core/data/h5py_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def dataset(self):

def close_dataset(self):
"""Closes the file. """
super(Dataset, self).close_dataset()
self.dataset.close()

# -------------------------------------------------------------------------
Expand Down

2 comments on commit 69a640d

@jmilloy
Copy link
Collaborator Author

@jmilloy jmilloy commented on 69a640d Jul 9, 2020

Choose a reason for hiding this comment

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

@mpu-creare This is the fix for the closed file object. You might want to pull this into your #410 work.

@mpu-creare
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Please sign in to comment.