@@ -72,6 +72,7 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
72
72
------
73
73
SchemaError
74
74
'''
75
+ assert isinstance (da , xr .core .dataarray .DataArray ),'Input is not an xarray DataArray and schema for chunks are not yet implemented'
75
76
76
77
if self .dtype is not None and not np .issubdtype (da .dtype , self .dtype ):
77
78
raise SchemaError (f'dtype { da .dtype } != { self .dtype } ' )
@@ -103,14 +104,21 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
103
104
raise NotImplementedError ('coords schema not implemented yet' )
104
105
105
106
if self .chunks :
106
- # ensure that the chunks are what you want them to be
107
- for dim , expected in self .chunks .items ():
108
- # for special case of chunksize=-1, make the expected equal to the full length of that dimension
109
- if expected == - 1 :
110
- expected = len (da [dim ])
111
- actual = da .chunks [dim ][0 ]
112
- if actual != expected :
113
- raise SchemaError (f'chunk mismatch for dimension { dim } : { actual } != { expected } ' )
107
+ dim_chunks = dict (zip (da .dims , da .chunks ))
108
+ for key , ec in self .chunks .items ():
109
+ if isinstance (ec , int ):
110
+ # handles case of expected chunksize is shorthand of -1 which translates to the full length of dimension
111
+ if ec == - 1 :
112
+ ec = len (da [key ])
113
+ # grab the first entry in da's tuple of chunks to be representative (as it should be assuming they're regular)
114
+ ac = dim_chunks [key ][0 ]
115
+ if ac != ec :
116
+ raise SchemaError (f'{ key } chunks did not match: { ac } != { ec } ' )
117
+
118
+ else : # assumes ec is an iterable
119
+ ac = dim_chunks [key ]
120
+ if tuple (ac ) != tuple (ec ):
121
+ raise SchemaError (f'{ key } chunks did not match: { ac } != { ec } ' )
114
122
115
123
if self .attrs :
116
124
raise NotImplementedError ('attrs schema not implemented yet' )
@@ -122,8 +130,6 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
122
130
for check in self .checks :
123
131
da = check (da )
124
132
125
- return da
126
-
127
133
128
134
class DatasetSchema :
129
135
'''A light-weight xarray.Dataset validator
0 commit comments