5
5
from collections import OrderedDict , defaultdict
6
6
from distutils .version import LooseVersion
7
7
from numbers import Number
8
+ from pathlib import Path
8
9
from typing import (Any , Dict , Hashable , Iterable , Iterator , List ,
9
- Mapping , Optional , Sequence , Set , Tuple , Union , cast )
10
+ Mapping , MutableMapping , Optional , Sequence , Set , Tuple ,
11
+ Union , cast )
10
12
11
13
import numpy as np
12
14
import pandas as pd
45
47
pass
46
48
47
49
if TYPE_CHECKING :
48
- from ..backends import AbstractDataStore
50
+ from ..backends import AbstractDataStore , ZarrStore
49
51
from .dataarray import DataArray
52
+ try :
53
+ from dask .delayed import Delayed
54
+ except ImportError :
55
+ Delayed = None
50
56
51
57
52
58
# list of attributes of pd.DatetimeIndex that are ndarrays of time info
@@ -1309,9 +1315,17 @@ def dump_to_store(self, store: 'AbstractDataStore', **kwargs) -> None:
1309
1315
# with to_netcdf()
1310
1316
dump_to_store (self , store , ** kwargs )
1311
1317
1312
- def to_netcdf (self , path = None , mode = 'w' , format = None , group = None ,
1313
- engine = None , encoding = None , unlimited_dims = None ,
1314
- compute = True ):
1318
+ def to_netcdf (
1319
+ self ,
1320
+ path = None ,
1321
+ mode : str = 'w' ,
1322
+ format : str = None ,
1323
+ group : str = None ,
1324
+ engine : str = None ,
1325
+ encoding : Mapping = None ,
1326
+ unlimited_dims : Iterable [Hashable ] = None ,
1327
+ compute : bool = True ,
1328
+ ) -> Union [bytes , 'Delayed' , None ]:
1315
1329
"""Write dataset contents to a netCDF file.
1316
1330
1317
1331
Parameters
@@ -1366,7 +1380,7 @@ def to_netcdf(self, path=None, mode='w', format=None, group=None,
1366
1380
This allows using any compression plugin installed in the HDF5
1367
1381
library, e.g. LZF.
1368
1382
1369
- unlimited_dims : sequence of str , optional
1383
+ unlimited_dims : iterable of hashable , optional
1370
1384
Dimension(s) that should be serialized as unlimited dimensions.
1371
1385
By default, no dimensions are treated as unlimited dimensions.
1372
1386
Note that unlimited_dims may also be set via
@@ -1383,9 +1397,17 @@ def to_netcdf(self, path=None, mode='w', format=None, group=None,
1383
1397
unlimited_dims = unlimited_dims ,
1384
1398
compute = compute )
1385
1399
1386
- def to_zarr (self , store = None , mode = 'w-' , synchronizer = None , group = None ,
1387
- encoding = None , compute = True , consolidated = False ,
1388
- append_dim = None ):
1400
+ def to_zarr (
1401
+ self ,
1402
+ store : Union [MutableMapping , str , Path ] = None ,
1403
+ mode : str = 'w-' ,
1404
+ synchronizer = None ,
1405
+ group : str = None ,
1406
+ encoding : Mapping = None ,
1407
+ compute : bool = True ,
1408
+ consolidated : bool = False ,
1409
+ append_dim : Hashable = None
1410
+ ) -> 'ZarrStore' :
1389
1411
"""Write dataset contents to a zarr group.
1390
1412
1391
1413
.. note:: Experimental
@@ -1394,15 +1416,15 @@ def to_zarr(self, store=None, mode='w-', synchronizer=None, group=None,
1394
1416
1395
1417
Parameters
1396
1418
----------
1397
- store : MutableMapping or str , optional
1419
+ store : MutableMapping, str or Path , optional
1398
1420
Store or path to directory in file system.
1399
1421
mode : {'w', 'w-', 'a'}
1400
1422
Persistence mode: 'w' means create (overwrite if exists);
1401
1423
'w-' means create (fail if exists);
1402
1424
'a' means append (create if does not exist).
1403
1425
synchronizer : object, optional
1404
1426
Array synchronizer
1405
- group : str, obtional
1427
+ group : str, optional
1406
1428
Group path. (a.k.a. `path` in zarr terminology.)
1407
1429
encoding : dict, optional
1408
1430
Nested dictionary with variable names as keys and dictionaries of
@@ -1414,7 +1436,7 @@ def to_zarr(self, store=None, mode='w-', synchronizer=None, group=None,
1414
1436
consolidated: bool, optional
1415
1437
If True, apply zarr's `consolidate_metadata` function to the store
1416
1438
after writing.
1417
- append_dim: str , optional
1439
+ append_dim: hashable , optional
1418
1440
If mode='a', the dimension on which the data will be appended.
1419
1441
1420
1442
References
@@ -1432,10 +1454,10 @@ def to_zarr(self, store=None, mode='w-', synchronizer=None, group=None,
1432
1454
group = group , encoding = encoding , compute = compute ,
1433
1455
consolidated = consolidated , append_dim = append_dim )
1434
1456
1435
- def __repr__ (self ):
1457
+ def __repr__ (self ) -> str :
1436
1458
return formatting .dataset_repr (self )
1437
1459
1438
- def info (self , buf = None ):
1460
+ def info (self , buf = None ) -> None :
1439
1461
"""
1440
1462
Concise summary of a Dataset variables and attributes.
1441
1463
@@ -1448,7 +1470,6 @@ def info(self, buf=None):
1448
1470
pandas.DataFrame.assign
1449
1471
ncdump: netCDF's ncdump
1450
1472
"""
1451
-
1452
1473
if buf is None : # pragma: no cover
1453
1474
buf = sys .stdout
1454
1475
@@ -1473,11 +1494,11 @@ def info(self, buf=None):
1473
1494
buf .write ('\n ' .join (lines ))
1474
1495
1475
1496
@property
1476
- def chunks (self ):
1497
+ def chunks (self ) -> Mapping [ Hashable , Tuple [ int , ...]] :
1477
1498
"""Block dimensions for this dataset's data or None if it's not a dask
1478
1499
array.
1479
1500
"""
1480
- chunks = {}
1501
+ chunks = {} # type: Dict[Hashable, Tuple[int, ...]]
1481
1502
for v in self .variables .values ():
1482
1503
if v .chunks is not None :
1483
1504
for dim , c in zip (v .dims , v .chunks ):
@@ -1486,8 +1507,17 @@ def chunks(self):
1486
1507
chunks [dim ] = c
1487
1508
return Frozen (SortedKeysDict (chunks ))
1488
1509
1489
- def chunk (self , chunks = None , name_prefix = 'xarray-' , token = None ,
1490
- lock = False ):
1510
+ def chunk (
1511
+ self ,
1512
+ chunks : Union [
1513
+ None ,
1514
+ Number ,
1515
+ Mapping [Hashable , Union [None , Number , Tuple [Number , ...]]]
1516
+ ] = None ,
1517
+ name_prefix : str = 'xarray-' ,
1518
+ token : str = None ,
1519
+ lock : bool = False
1520
+ ) -> 'Dataset' :
1491
1521
"""Coerce all arrays in this dataset into dask arrays with the given
1492
1522
chunks.
1493
1523
@@ -1500,7 +1530,7 @@ def chunk(self, chunks=None, name_prefix='xarray-', token=None,
1500
1530
1501
1531
Parameters
1502
1532
----------
1503
- chunks : int or dict , optional
1533
+ chunks : int or mapping , optional
1504
1534
Chunk sizes along each dimension, e.g., ``5`` or
1505
1535
``{'x': 5, 'y': 5}``.
1506
1536
name_prefix : str, optional
@@ -1526,7 +1556,7 @@ def chunk(self, chunks=None, name_prefix='xarray-', token=None,
1526
1556
chunks = dict .fromkeys (self .dims , chunks )
1527
1557
1528
1558
if chunks is not None :
1529
- bad_dims = [ d for d in chunks if d not in self .dims ]
1559
+ bad_dims = chunks . keys () - self .dims . keys ()
1530
1560
if bad_dims :
1531
1561
raise ValueError ('some chunks keys are not dimensions on this '
1532
1562
'object: %s' % bad_dims )
0 commit comments