|
58 | 58 | from pint import UnitRegistry |
59 | 59 | from pint.errors import UndefinedUnitError |
60 | 60 | from tokenize import TokenError |
| 61 | +from tarfile import ExFileObject |
61 | 62 | from matplotlib.path import Path as BasePath |
62 | 63 | from .. import VAR_DESCR, VAR_DESCR_inv |
63 | 64 | from ..generic import ( |
@@ -431,8 +432,11 @@ def zarr_dimension(filename): |
431 | 432 |
|
432 | 433 | @classmethod |
433 | 434 | def load_file(cls, filename, **kwargs): |
434 | | - end = b".zarr" if isinstance(filename, bytes) else ".zarr" |
435 | | - if filename.endswith(end): |
| 435 | + filename_ = ( |
| 436 | + filename.filename if isinstance(filename, ExFileObject) else filename |
| 437 | + ) |
| 438 | + end = b".zarr" if isinstance(filename_, bytes) else ".zarr" |
| 439 | + if filename_.endswith(end): |
436 | 440 | return cls.load_from_zarr(filename, **kwargs) |
437 | 441 | else: |
438 | 442 | return cls.load_from_netcdf(filename, **kwargs) |
@@ -539,9 +543,14 @@ def load_from_netcdf( |
539 | 543 | cls, filename, raw_data=False, remove_vars=None, include_vars=None |
540 | 544 | ): |
541 | 545 | array_dim = "NbSample" |
542 | | - if not isinstance(filename, str): |
| 546 | + if isinstance(filename, bytes): |
543 | 547 | filename = filename.astype(str) |
544 | | - with Dataset(filename) as h_nc: |
| 548 | + if isinstance(filename, ExFileObject): |
| 549 | + filename.seek(0) |
| 550 | + args, kwargs = ("in-mem-file",), dict(memory=filename.read()) |
| 551 | + else: |
| 552 | + args, kwargs = (filename,), dict() |
| 553 | + with Dataset(*args, **kwargs) as h_nc: |
545 | 554 | var_list = list(h_nc.variables.keys()) |
546 | 555 | if include_vars is not None: |
547 | 556 | var_list = [i for i in var_list if i in include_vars] |
|
0 commit comments