moczarr.moc_index
MortonMocIndex: the MOC-backed lazy xarray index (core, xarray-only).
The §6 "domain = MOC, dense views fabricated lazily" centerpiece (phase 5,
espg/moczarr#1): an :class:xarray.Index whose state is a
:class:~moczarr.ranges.MortonRanges interval set instead of a materialized
cell array — the HealpixMocIndex analog (xarray-contrib/xdggs#143) on
the numpy interval substrate. Selection, subsetting, and alignment are rank
arithmetic; the coordinate itself is fabricated on demand
(:meth:MortonMocIndex.create_variables), so an open_hive result can
carry a fully functional cell index without ever reading a coordinate chunk.
Core placement is deliberate: this module imports xarray only (no xdggs).
The accessor/registry wiring lives in moczarr.dggs (the [xdggs]
extra), which wraps this index behind index_kind="moc" the way upstream
HealpixIndex wraps HealpixMocIndex.
Mixing with the pandas-backed index raises pointedly: an interval set and a
hash table have no shared alignment currency, so both sides of an
align/join must be opened with the same index_kind.
MortonMocIndex
Bases: Index
Lazy cell index over a :class:~moczarr.ranges.MortonRanges domain.
sel accepts packed uint64 words, decimal strings, or lists/arrays
of either; isel and alignment (join/reindex_like) run in
interval algebra. Selections an interval set cannot represent (scalar
collapse, non-monotonic positional picks) degrade by dropping the lazy
index — xarray then works on the already-fabricated coordinate — never
by materializing a second truth.
Source code in src/moczarr/moc_index.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
level
property
HEALPix order of the domain's cells (manifest cell_order).
ranges
property
The interval set backing this index.
size
property
Number of cells in the indexed domain.
concat(indexes, dim, positions=None)
classmethod
Concatenate disjoint, ascending MOC domains (the batch-sweep case).
xr.concat of moc-indexed datasets is supported when the domains
are already disjoint and in ascending word order end to end — the
AOI-tile / batch-sweep pattern, where each open covers a distinct
spatial block. The data variables concatenate in the given block
order, and the fabricated morton coordinate has to match row for
row; that holds exactly when the concatenated interval blocks are
ascending and non-overlapping, so no coalescing sort can reorder the
domain out from under the data (adjacent blocks, next.lo ==
prev.hi + 1, merge without disturbing the ascending word sequence).
An empty domain contributes no intervals, so concat([empty, full])
returns full — the issue #4 "empty composes through concat"
contract, now honored on the default index.
Overlapping, interleaved, or reversed domains — and any request that
carries explicit positions (xarray's interleave path) — raise a
pointed NotImplementedError naming the index_kind="pandas"
escape, whose materialized coordinate concatenates arbitrarily.
Source code in src/moczarr/moc_index.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
create_variables(variables=None)
Fabricate the coordinate variable — arithmetic, zero store reads.
Plain numpy by default. When chunks were requested at
construction, each chunk fabricates in its own dask task (the
HealpixMocIndex chunked shape); dask is required only then.
Source code in src/moczarr/moc_index.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
equals(other, **kwargs)
Domain equality; pairing with a non-MOC index raises pointedly.
Source code in src/moczarr/moc_index.py
393 394 395 396 | |
from_moc(source, *, cell_order, aoi=None, dim='cells', name='morton', chunks=None)
classmethod
The first-class constructor: a domain from coverage arithmetic.
source is a root coverage envelope (the "ranges" dict a hive
store's coverage.moc holds), an array of shard words (the walk
fallback), or an existing :class:MortonRanges. aoi (a morton
cover, mixed orders allowed) intersects the domain in interval
space. chunks requests dask-backed coordinate fabrication (an
int or explicit chunk sizes); the default fabricates plain numpy.
Source code in src/moczarr/moc_index.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
from_variables(variables, *, options)
classmethod
Build from a materialized morton coordinate (ds.set_xindex).
The materialized→lazy direction: run-detects the words into
intervals. level comes from options or the variable's
level/cell_order attrs, else the words' own order.
Source code in src/moczarr/moc_index.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
isel(indexers)
Positional subset in interval algebra; None when unrepresentable.
A non-monotonic or duplicated indexer cannot round-trip an interval
set, so the lazy index drops (values stay correct) — this is also
where a sel drop surfaces, since xarray lowers label selection to
a positional isel. A duplicate-free non-monotonic pick warns on
the drop; any duplicate-containing pick drops silently — an
interval set cannot hold duplicates, and warning on every truncation-
join lookup (phase 6b) would be noise, so a user-initiated duplicate
sel(morton=[w, w]) is included in that silence too (see
:func:_warn_index_dropped). A scalar collapse returns None
silently — the dimension is gone, so there is no index to keep.
Source code in src/moczarr/moc_index.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
join(other, how='inner')
Alignment in interval algebra — no materialization.
Source code in src/moczarr/moc_index.py
398 399 400 401 402 403 404 405 406 407 408 409 | |
reindex_like(other)
Positions of other's cells in this domain (-1 where absent).
Source code in src/moczarr/moc_index.py
411 412 413 414 | |
sel(labels, method=None, tolerance=None)
Label selection by cell id — rank arithmetic, KeyError on misses.
Labels may be packed uint64 words, decimal strings, or
lists/arrays of either. method/tolerance have no meaning on
an exact cell domain and raise; so do slices (cell ids are not an
ordinal axis the user should be slicing by half-open label ranges —
pass an AOI cover or use isel).
Repeated labels return repeated positions in label order — the same
rows PandasIndex selects (the rank lookup is elementwise, so
repeats plumb straight through the IndexSelResult positions).
The result of a duplicated selection carries no lazy index (an
interval set cannot hold duplicate labels) and drops it silently:
this is the truncation-join lookup pattern
(coarse.sel(morton=parent_cells(fine, level)), phase 6b), not an
accident. A duplicate-free non-monotonic pick still warns on drop.
Source code in src/moczarr/moc_index.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |