Skip to content

Lazy NetCDF cube#

Backs NetCDF.chunk(). A LazyNetCDF is the lazy twin of an eager cube: one dask.array.Array per gridded variable, alongside the eager cube it was built from.

Backend method build_lazy_cube
Module pyramids_lazy._lazy_cube
Returns LazyNetCDF
Public as pyramids.netcdf.LazyNetCDF
from pyramids.netcdf import NetCDF

cube = NetCDF.read_file("era5.nc")
lazy = cube.chunk({"rows": 256, "cols": 256})
lazy.chunksizes  # an alias of .chunks: per dimension, merged across the variables
eager = lazy.compute()

chunk takes the same spec as read_array: "auto", a single int, a sequence per axis, or a dict keyed by rows / cols / columns / bands (case-insensitive) or by integer axis index. It is not keyed by the file's dimension names — {"time": 24} raises ValueError.

Whether reduce(dim, how=…) stays lazy depends on what the view holds:

  • a single variable (a cube pinned with get_variable(…), or any view a composed op produced) composes lazily — it returns another LazyNetCDF, with no warning;
  • a multi-variable container cannot compose per block, so it materialises and runs eagerly, returning the eager cube and warning that it has done so.

Call compute() first when you want the eager path to be explicit rather than implied.

A wrapper, not a subclass#

LazyFeatureCollection can subclass dask_geopandas.GeoDataFrame because a feature collection is one frame. A cube is many arrays, so there is no dask type to subclass — this is a wrapper instead, holding the lazy arrays plus the eager cube that is both the source of its geo and band metadata and the twin its lifecycle materialises back to.

The dask lifecycle members live here: chunks and chunksizes read the chunking off the arrays, compute() and load() return the eager cube, persist() realises the graph in worker memory while staying lazy, and unify_chunks() re-chunks every variable to a common grid.

API#

pyramids_lazy._lazy_cube.LazyNetCDF #

A lazy, dask-backed view of a :class:~pyramids.netcdf.NetCDF cube.

Built by :meth:NetCDF.chunk. Holds a :class:dask.array.Array per gridded variable and the eager cube they were read from; the eager cube supplies geo / band metadata and is the twin returned by :meth:compute / :meth:load.

Attributes:

Name Type Description
variable_names list[str]

The gridded variables carried as dask arrays.

Examples:

  • A lazy view reports its chunking and computes back to its eager twin:
>>> from pyramids.netcdf import NetCDF  # doctest: +SKIP
>>> lazy = NetCDF.read_file("cube.nc").chunk("auto")  # doctest: +SKIP
>>> lazy.chunks  # doctest: +SKIP
{'time': (4,), 'y': (256, 256), 'x': (256, 256)}
>>> eager = lazy.compute()  # doctest: +SKIP
Source code in src/pyramids_lazy/_lazy_cube.py
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  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
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
class LazyNetCDF:
    """A lazy, dask-backed view of a :class:`~pyramids.netcdf.NetCDF` cube.

    Built by :meth:`NetCDF.chunk`. Holds a :class:`dask.array.Array` per gridded variable and the
    eager cube they were read from; the eager cube supplies geo / band metadata and is the twin
    returned by :meth:`compute` / :meth:`load`.

    Attributes:
        variable_names (list[str]): The gridded variables carried as dask arrays.

    Examples:
        - A lazy view reports its chunking and computes back to its eager twin:

          ```python
          >>> from pyramids.netcdf import NetCDF  # doctest: +SKIP
          >>> lazy = NetCDF.read_file("cube.nc").chunk("auto")  # doctest: +SKIP
          >>> lazy.chunks  # doctest: +SKIP
          {'time': (4,), 'y': (256, 256), 'x': (256, 256)}
          >>> eager = lazy.compute()  # doctest: +SKIP

          ```
    """

    def __init__(
        self,
        source: NetCDF,
        arrays: dict[str, Any],
        dim_names: dict[str, tuple[str, ...]],
        records: dict[str, _LazyVar] | None = None,
    ) -> None:
        """Wrap per-variable dask arrays alongside the eager cube they came from.

        Args:
            source: The eager cube this view was built from — the source of its metadata and the
                twin :meth:`compute` returns when no op has transformed the arrays.
            arrays: One dask array per gridded variable, laid out `(*band_sizes, rows, cols)`.
            dim_names: The dimension names per variable, outermost first, i.e.
                `(*band_dim_names, row_name, col_name)` — the keys :attr:`chunks` reports.
            records: Per-variable deferred state, set once an array-native op has composed over the
                cube (#1237). When present the cube is **transformed**: :meth:`compute` rebuilds the
                eager cube from these arrays rather than returning the source. `None` (the default,
                from :meth:`NetCDF.chunk`) is an untransformed view whose arrays are chunked reads of
                the source.
        """
        self._source = source
        self._arrays = dict(arrays)
        self._dim_names = dict(dim_names)
        # Copied, not aliased, to match `arrays` / `dim_names` above: views share a source cube but
        # never a mutable mapping.
        self._records = None if records is None else dict(records)
        self._materialize_warned = False

    @property
    def variable_names(self) -> list[str]:
        """The gridded variables this view carries, in build order."""
        return list(self._arrays)

    @property
    def chunks(self) -> dict[str, tuple[int, ...]]:
        """Per-dimension chunk sizes, keyed by dimension name.

        Merged across variables — two variables that disagree on a shared dimension leave the last
        one's chunking here until :meth:`unify_chunks` reconciles them.
        """
        merged: dict[str, tuple[int, ...]] = {}
        for name, array in self._arrays.items():
            for dim, chunk in zip(self._dim_names[name], array.chunks):
                merged[dim] = tuple(int(size) for size in chunk)
        return merged

    @property
    def chunksizes(self) -> dict[str, tuple[int, ...]]:
        """Alias of :attr:`chunks` (xarray keeps both)."""
        return self.chunks

    def compute(self, **kwargs: Any) -> NetCDF:
        """Materialise the lazy view and return an **independent** eager NetCDF cube.

        The lazy arrays are chunked reads of the source cube, so the result holds the same data; it is
        returned as an independent copy (`compute() is not` the source), matching xarray's `compute()`,
        so mutating the result never touches the original or any other `compute()` result. The
        `**kwargs` are accepted for API symmetry with xarray / dask and ignored — in this v1 the data is
        a view of the already-eager source, so nothing has to be scheduled.

        Returns:
            NetCDF: A fresh eager cube, independent of the source.
        """
        if self._records is not None:
            # A cube transformed by a composed op (#1237): materialise the deferred dask arrays
            # through the eager rebuild rather than returning the (untransformed) source.
            return self._rebuild()
        return self._source.copy()

    def load(self, **kwargs: Any) -> NetCDF:
        """Materialise in place and return the eager cube — the in-place form of :meth:`compute`.

        Matches xarray's `load`, which realises in place and returns the same object: this returns the
        **source cube itself** (not a copy), so it aliases the cube `.chunk()` was called on. Use
        :meth:`compute` when you need an independent result. The `**kwargs` are accepted for symmetry
        and ignored.

        Returns:
            NetCDF: The source cube (the eager twin), realised in place. A transformed cube
            (a composed op) has no untransformed source to alias, so it returns the rebuilt eager
            result, as :meth:`compute` does.
        """
        if self._records is not None:
            return self._rebuild()
        return self._source

    def persist(self, **kwargs: Any) -> LazyNetCDF:
        """Realise the dask graph in worker memory, staying lazy.

        Returns a new :class:`LazyNetCDF` whose arrays are `dask.persist`-ed — the graph is kept but
        its results are held in memory / across the cluster, so later reads do not recompute it.

        A composed array-native op survives the round trip: the deferred per-variable records are
        carried onto the new view, exactly as `unify_chunks()` carries them, so `persist().compute()`
        rebuilds the *transformed* cube instead of falling back to the untransformed source. A view
        straight from `NetCDF.chunk()` carries no records, and `persist()` invents none.

        Args:
            **kwargs: Forwarded verbatim to `dask.persist` — for example `scheduler="threads"` or
                `optimize_graph=False`.

        Returns:
            LazyNetCDF: A lazy view over the persisted arrays, keeping this view's chunking and its
            composed op.

        Examples:
            - Persisting stays lazy and keeps the chunking:
                ```python
                >>> from pyramids.netcdf import NetCDF
                >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
                >>> lazy = cube.get_variable("temperature").chunk("auto")
                >>> lazy.chunks
                {'time': (4,), 'pressure_level': (3,), 'lat': (5,), 'lon': (6,)}
                >>> lazy.persist().chunks == lazy.chunks
                True
                >>> cube.close()

                ```
            - A composed reduction survives persisting: the view computes to the time-collapsed
              result, not back to the four-step source it was read from:
                ```python
                >>> from pyramids.netcdf import NetCDF
                >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
                >>> lazy = cube.get_variable("temperature").chunk("auto")
                >>> lazy.compute().read_array().shape
                (4, 3, 5, 6)
                >>> lazy.reduce("time", "mean").persist().compute().read_array().shape
                (3, 5, 6)
                >>> cube.close()

                ```

        See Also:
            unify_chunks: Reconciles shared-dimension chunking across two or more variables; a
                composed (single-variable) view is already unified, so it is returned unchanged.
            compute: Materialises the view — the rebuilt transformed cube once an op has composed.
        """
        import_dask(_DASK_MISSING)
        import dask

        names = list(self._arrays)
        record_items = list(self._records.items()) if self._records is not None else []
        # One dask.persist call for both mappings: the record's array is the graph `_rebuild` and
        # every `_compose_*` actually read, so persisting only `_arrays` would leave them
        # recomputing from the store while this view claimed to be in memory. Persisting together
        # also keeps shared parts of the graph shared.
        realised = dask.persist(
            *(self._arrays[name] for name in names),
            *(record.array for _, record in record_items),
            **kwargs,
        )
        arrays = dict(zip(names, realised[: len(names)]))
        records = None
        if self._records is not None:
            # Carry _records through, as unify_chunks does: without them compute() takes its
            # "untransformed" branch and returns self._source.copy(), discarding a composed op.
            records = {
                name: record._replace(array=array)
                for (name, record), array in zip(record_items, realised[len(names) :])
            }
        return LazyNetCDF(self._source, arrays, self._dim_names, records)

    def unify_chunks(self) -> LazyNetCDF:
        """Re-chunk every variable to a common chunking across their shared dimensions.

        A no-op when the view holds fewer than two variables — that view is returned unchanged. Two
        variables chunked differently on a shared dimension come out sharing one chunking afterward.
        A composed array-native op is carried onto the result, as it is through `persist()`.

        Returns:
            LazyNetCDF: A lazy view whose variables agree on their shared-dimension chunking.
        """
        import_dask(_DASK_MISSING)
        import dask.array as da

        if len(self._arrays) < 2:
            return self
        names = list(self._arrays)
        pairs: list[Any] = []
        for name in names:
            pairs.extend((self._arrays[name], self._dim_names[name]))
        _, unified = da.core.unify_chunks(*pairs)
        arrays = dict(zip(names, unified))
        return LazyNetCDF(self._source, arrays, self._dim_names, self._records)

    def _current_records(self) -> dict[str, _LazyVar]:
        """The per-variable deferred records, derived from the source for an untransformed view.

        An untransformed cube (`_records is None`, straight from :meth:`NetCDF.chunk`) carries only
        chunked reads; its records are read off the source variables so a first op has a uniform
        record to transform. A transformed cube returns its carried records.

        Returns:
            dict[str, _LazyVar]: One record per gridded variable.
        """
        if self._records is not None:
            return self._records
        from pyramids.lazy_support import _read_no_data, crs_spec

        source = self._source
        pinned = source._source_var_name
        records: dict[str, _LazyVar] = {}
        for name in self._arrays:
            var = (
                source if pinned is not None else source._require_raster_variable(name)
            )
            records[name] = _LazyVar(
                array=self._arrays[name],
                band_names=list(var._band_dim_names),
                values_map=dict(var._band_dim_values_map),
                no_data=_read_no_data(var),
                geotransform=var.geotransform,
                epsg=crs_spec(var.epsg, var.crs),
                dim_names=self._dim_names[name],
                time_attrs=var._resolved_band_dim_time_attrs(),
                source_var=var,
            )
        return records

    def _rebuild(self) -> NetCDF:
        """Materialise the transformed records into an eager cube through the eager rebuild path.

        One record (a pinned variable, the composition path this version supports) rebuilds through
        :func:`_variable_from_applied`, the same helper the eager ops use, so the result's band
        coordinates, unlabelled-dimension gaps and CF time units match the eager reduce exactly.

        Returns:
            NetCDF: The eager variable the deferred ops computed to.
        """
        from pyramids.lazy_support import _Applied, _variable_from_applied, as_numpy

        records = self._current_records()
        (rec,) = records.values()
        applied = _Applied(
            as_numpy(rec.array), rec.band_names, rec.values_map, rec.no_data
        )
        return _variable_from_applied(rec.source_var, applied)

    def reduce(
        self,
        dim: str,
        how: str = "mean",
        *,
        skipna: bool = True,
        q: float | None = None,
    ) -> Any:
        """Reduce a band dimension, composing lazily when the cube is a single variable (#1237).

        Collapses `dim` with `how`, keeping the result a lazy cube: the reduction is a `dask.array`
        step deferred until :meth:`compute`, so a chain of reductions stays lazy and reads the store
        only once per block. Mirrors :meth:`NetCDF.reduce`.

        A single pinned variable composes lazily and returns a :class:`LazyNetCDF`. A multi-variable
        container cannot yet compose at the cube level, so it auto-materialises to the eager cube and
        reduces there, warning once — the v1 lazy/eager boundary (the same shape as the GDAL-op
        boundary). Pin a variable with `get_variable(...)` first to keep a container reduce lazy.

        Args:
            dim: The band dimension to collapse.
            how: The reduction (`"mean"`, `"sum"`, `"max"`, a quantile, …), as :meth:`NetCDF.reduce`.
            skipna: Whether gaps are skipped.
            q: The quantile, for `how="quantile"`.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager reduced cube for a
            multi-variable container (materialised at the boundary).
        """
        if len(self._current_records()) != 1:
            # Container composition is not built yet: materialise at the boundary and reduce
            # eagerly, warning once, rather than silently reading the whole cube without notice.
            self._warn_materialize("reduce")
            return self.compute().reduce(dim, how, skipna=skipna, q=q)
        return self._compose_reduction(
            dim,
            how,
            skipna=skipna,
            q=q,
            group_positions=None,
            resize=None,
            window_mean_coords=False,
        )

    def coarsen(
        self,
        dim: str,
        window: int,
        *,
        how: str = "mean",
        boundary: str = "exact",
        skipna: bool = True,
        q: float | None = None,
    ) -> Any:
        """Block-aggregate a band dimension into windows, composing lazily for a single variable.

        The lazy twin of :meth:`NetCDF.coarsen` — the same grouped reduction, kept as a deferred
        `dask.array` step so it composes with other lazy ops. A multi-variable container
        materialises at the boundary and coarsens eagerly, warning once (as :meth:`reduce` does).

        Args:
            dim: The band dimension to block-aggregate.
            window: The number of steps per window.
            how: The reduction, as :meth:`NetCDF.coarsen`.
            boundary: How a trailing partial window is handled (`"exact"`, `"trim"`, `"pad"`).
            skipna: Whether gaps are skipped.
            q: The quantile, for `how="quantile"`.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager coarsened cube for a
            multi-variable container.
        """
        from pyramids.lazy_support import (
            _BOUNDARIES,
            COUNTING_REDUCERS,
            REDUCERS,
            _check_how,
            _check_quantile,
            _check_window,
            _coarsen_windows,
        )

        _check_how(how, {*REDUCERS, *COUNTING_REDUCERS})
        q = _check_quantile(how, q)
        length = _check_window(window, caller="coarsen")
        if boundary not in _BOUNDARIES:
            raise ValueError(
                f"boundary must be one of {list(_BOUNDARIES)}, got {boundary!r}."
            )
        records = self._current_records()
        if len(records) != 1:
            self._warn_materialize("coarsen")
            return self.compute().coarsen(
                dim, window, how=how, boundary=boundary, skipna=skipna, q=q
            )
        (rec,) = records.values()
        size = rec.array.shape[rec.band_names.index(dim)]
        resized, positions = _coarsen_windows(dim, size, length, boundary)
        return self._compose_reduction(
            dim,
            how,
            skipna=skipna,
            q=q,
            group_positions=positions,
            resize=resized,
            window_mean_coords=True,
        )

    def rolling(
        self,
        dim: str,
        window: int,
        *,
        how: str = "mean",
        center: bool = False,
        min_periods: int | None = None,
        q: float | None = None,
    ) -> Any:
        """Moving-window reduction along a band dimension, composing lazily for a single variable.

        The lazy twin of :meth:`NetCDF.rolling` — the window reduction stays a deferred
        `dask.array` step. A multi-variable container materialises at the boundary, warning once.

        Args:
            dim: The band dimension to roll along.
            window: The window length in steps.
            how: The reduction, as :meth:`NetCDF.rolling`.
            center: Whether the window is centred rather than trailing.
            min_periods: The minimum valid steps for a non-gap result; defaults to `window`.
            q: The quantile, for `how="quantile"`.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager rolled cube otherwise.
        """
        from pyramids.lazy_support import (
            COUNTING_REDUCERS,
            REDUCERS,
            _check_how,
            _check_min_periods,
            _check_quantile,
            _check_window,
            _rolled_array,
        )

        _check_how(how, {*REDUCERS, *COUNTING_REDUCERS})
        q = _check_quantile(how, q)
        length = _check_window(window, caller="rolling")
        needed = _check_min_periods(min_periods, length)
        if len(self._current_records()) != 1:
            self._warn_materialize("rolling")
            return self.compute().rolling(
                dim, window, how=how, center=center, min_periods=min_periods, q=q
            )
        return self._compose_direct(
            _rolled_array,
            dim,
            window=length,
            center=bool(center),
            min_periods=needed,
            how=how,
            q=q,
        )

    def diff(self, dim: str, n: int = 1, *, label: str = "upper") -> Any:
        """Difference neighbouring steps along a band dimension, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.diff`; the difference stays a deferred `dask.array` step.
        A multi-variable container differences eagerly at the boundary, warning once.

        Args:
            dim: The band dimension to difference along.
            n: The order (how many times the difference is taken).
            label: `"upper"` or `"lower"` — which step each difference is labelled with.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager diffed cube otherwise.
        """
        from pyramids.lazy_support import _DIFF_LABELS, _check_order, _diffed_array

        order = _check_order(n)
        if label not in _DIFF_LABELS:
            raise ValueError(
                f"label must be one of {list(_DIFF_LABELS)}, got {label!r}."
            )
        if len(self._current_records()) != 1:
            self._warn_materialize("diff")
            return self.compute().diff(dim, n, label=label)
        return self._compose_direct(_diffed_array, dim, n=order, label=label)

    def cumsum(self, dim: str, *, skipna: bool = True) -> Any:
        """Running total along a band dimension, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.cumsum`; the cumulative sum stays a deferred `dask.array`
        step, keeping the dimension's length. A multi-variable container totals eagerly at the
        boundary, warning once.

        Args:
            dim: The band dimension to total along.
            skipna: Whether gaps are skipped.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager totalled cube otherwise.
        """
        from pyramids.lazy_support import _CumSum

        if len(self._current_records()) != 1:
            self._warn_materialize("cumsum")
            return self.compute().cumsum(dim, skipna=skipna)
        return self._compose_op(_CumSum(skipna=bool(skipna)), dim)

    def shift(self, dim: str, periods: int = 1, *, fill_value: Any = None) -> Any:
        """Shift values along a band dimension, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.shift`; the shift stays a deferred `dask.array` step,
        keeping the dimension's length. A multi-variable container shifts eagerly at the boundary,
        warning once.

        Args:
            dim: The band dimension to shift along.
            periods: How many steps to move (positive towards the end, negative the other way).
            fill_value: The value for the vacated steps; the variable's no-data (or NaN) by default.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager shifted cube otherwise.
        """
        from pyramids.lazy_support import _check_fill_value, _check_periods, _Shift

        steps = _check_periods(periods)
        _check_fill_value(fill_value)
        if len(self._current_records()) != 1:
            self._warn_materialize("shift")
            return self.compute().shift(dim, periods, fill_value=fill_value)
        return self._compose_op(_Shift(periods=steps, fill_value=fill_value), dim)

    def rank(self, dim: str, *, pct: bool = False) -> Any:
        """Rank each pixel's values along a band dimension, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.rank`. `rank` uses `scipy.stats.rankdata`, which needs a
        numpy array, so it cannot stay a pure dask graph; instead the eager kernel runs per spatial
        block (the band axis whole) through `dask.array.map_blocks`, which is lazy and bit-for-bit
        with the eager rank. A multi-variable container ranks eagerly at the boundary, warning once.

        Args:
            dim: The band dimension to rank along.
            pct: Whether to return ranks as a fraction of the valid count.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager ranked cube otherwise.
        """
        from pyramids.lazy_support import _Rank

        if len(self._current_records()) != 1:
            self._warn_materialize("rank")
            return self.compute().rank(dim, pct=pct)
        return self._compose_mapblocks(_Rank(pct=pct), dim)

    def argmin(self, dim: str, *, skipna: bool = True) -> Any:
        """Position of the minimum along a band dimension, composing lazily (#1237)."""
        return self._extremum(dim, "min", False, "argmin", skipna)

    def argmax(self, dim: str, *, skipna: bool = True) -> Any:
        """Position of the maximum along a band dimension, composing lazily (#1237)."""
        return self._extremum(dim, "max", False, "argmax", skipna)

    def idxmin(self, dim: str, *, skipna: bool = True) -> Any:
        """Coordinate of the minimum along a band dimension, composing lazily (#1237)."""
        return self._extremum(dim, "min", True, "idxmin", skipna)

    def idxmax(self, dim: str, *, skipna: bool = True) -> Any:
        """Coordinate of the maximum along a band dimension, composing lazily (#1237)."""
        return self._extremum(dim, "max", True, "idxmax", skipna)

    def interpolate_na(
        self,
        dim: str,
        method: str = "linear",
        *,
        limit: int | None = None,
        use_coordinate: bool = True,
    ) -> Any:
        """Fill interior gaps along a band dimension, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.interpolate_na`; the interpolation (a scipy kernel) runs per
        spatial block via map_blocks, keeping the dimension's length. A multi-variable container
        interpolates eagerly at the boundary, warning once.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.
        """
        from pyramids.lazy_support import _check_limit, _Interpolate

        if len(self._current_records()) != 1:
            self._warn_materialize("interpolate_na")
            return self.compute().interpolate_na(
                dim, method, limit=limit, use_coordinate=use_coordinate
            )
        op = _Interpolate(
            method=method,
            limit=_check_limit(limit, caller="interpolate_na"),
            use_coordinate=bool(use_coordinate),
        )
        return self._compose_mapblocks(op, dim)

    def interp(self, method: str = "linear", **coords: Any) -> Any:
        """Interpolate band dimensions onto new coordinate values, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.interp`; each `dim=targets` interpolation (a scipy kernel)
        runs per spatial block via map_blocks, resizing the dimension to the target length. Several
        `dim=targets` pairs compose in sequence. A multi-variable container interpolates eagerly at
        the boundary, warning once.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.
        """
        from pyramids.lazy_support import (
            _INTERP_MIN_POINTS,
            _interp_targets,
            _InterpTo,
            _refuse_spatial_band_op,
            _resolve_interp_kind,
        )

        kind = _resolve_interp_kind(method)
        if not coords or len(self._current_records()) != 1:
            self._warn_materialize("interp")
            return self.compute().interp(method, **coords)
        result: Any = self
        for dim, target in coords.items():
            _refuse_spatial_band_op(self._source, dim, caller="interp")
            rec = next(iter(result._current_records().values()))
            if dim not in rec.band_names:
                raise ValueError(
                    f"interp(): {dim!r} is not a band dimension of this variable."
                )
            size = rec.array.shape[rec.band_names.index(dim)]
            minimum = _INTERP_MIN_POINTS[kind]
            if size < minimum:
                raise ValueError(
                    f"interp() method {kind!r} needs at least {minimum} source steps along "
                    f"{dim!r}, got {size}."
                )
            targets = _interp_targets(target, dim, caller="interp")
            result = result._compose_mapblocks(
                _InterpTo(target=targets, kind=kind, caller="interp"), dim
            )
        return result

    def pad(
        self, *, mode: str = "constant", constant_values: Any = None, **pad_width: Any
    ) -> Any:
        """Pad band dimensions with a constant fill, composing lazily (#1237).

        The lazy twin of :meth:`NetCDF.pad` for **band** dimensions (extended via map_blocks). A
        spatial-axis pad moves the geotransform (a GDAL op) and a multi-variable container cannot
        compose, so either materialises at the boundary, warning once; several band dimensions in
        one call compose in sequence.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube when every padded dimension is a band dimension of a
            single variable; an eager padded cube otherwise.
        """
        from pyramids.lazy_support import _Pad, _pad_before_after

        records = self._current_records()
        band_names = next(iter(records.values())).band_names if records else []
        if (
            mode != "constant"
            or not pad_width
            or len(records) != 1
            or any(dim not in band_names for dim in pad_width)
        ):
            self._warn_materialize("pad")
            return self.compute().pad(
                mode=mode, constant_values=constant_values, **pad_width
            )
        result: Any = self
        for dim, width in pad_width.items():
            before, after = _pad_before_after(width, dim)
            result = result._compose_mapblocks(
                _Pad(before=before, after=after, fill_value=constant_values), dim
            )
        return result

    def clip(self, min: Any = None, max: Any = None) -> Any:
        """Bound the values to `[min, max]`, composing lazily (#1237); a gap stays a gap.

        Cell-wise and shape-preserving, so it runs the eager :meth:`NetCDF.clip` per spatial block
        via map_blocks (bit-for-bit; the dtype widening is bound-based, so it is the same for every
        block). A multi-variable container clips eagerly at the boundary, warning once.
        """
        if len(self._current_records()) != 1:
            self._warn_materialize("clip")
            return self.compute().clip(min=min, max=max)
        return self._compose_cellwise("clip", min, max)

    def fillna(self, value: float | int) -> Any:
        """Fill the gaps with `value`, composing lazily (#1237).

        Cell-wise and shape-preserving; runs the eager :meth:`NetCDF.fillna` per block via
        map_blocks. A multi-variable container fills eagerly at the boundary, warning once.
        """
        if len(self._current_records()) != 1:
            self._warn_materialize("fillna")
            return self.compute().fillna(value)
        return self._compose_cellwise("fillna", value)

    def round(self, decimals: int = 0) -> Any:
        """Round the values, composing lazily (#1237); a gap stays a gap.

        Cell-wise and shape-preserving; runs the eager :meth:`NetCDF.round` per block via
        map_blocks. An integer band rounded to tens (`decimals < 0`) may widen based on the data,
        which is not consistent across blocks, so that case materialises at the boundary, as does a
        multi-variable container — both warn once.
        """
        if isinstance(decimals, bool) or not isinstance(decimals, (int, np.integer)):
            raise TypeError(
                f"round() needs an integer number of decimals, got {decimals!r}."
            )
        records = self._current_records()
        integer_widen = (
            len(records) == 1
            and next(iter(records.values())).array.dtype.kind in "iu"
            and int(decimals) < 0
        )
        if len(records) != 1 or integer_widen:
            self._warn_materialize("round")
            return self.compute().round(decimals)
        return self._compose_cellwise("round", decimals)

    def _binary_op(self, other: Any, op_name: str) -> Any:
        """A cell-wise operator: compose lazily for a real scalar, else materialise (#1237).

        `lazy * 2`, `lazy - 273.15`, `lazy >= threshold` and the like run the eager operator per
        block (the result dtype/sentinel is value-based, so it is consistent across blocks). A
        raster/array operand needs grid + band alignment through the combine engine, and a
        multi-variable container cannot compose, so either materialises at the boundary, warning
        once.
        """
        from numbers import Real

        scalar = isinstance(other, Real) and not isinstance(other, bool)
        if not scalar or len(self._current_records()) != 1:
            self._warn_materialize(op_name.strip("_"))
            return getattr(self.compute(), op_name)(other)
        return self._compose_cellwise(op_name, other)

    def _unary_op(self, op_name: str) -> Any:
        """A cell-wise unary operator (`-cube`, `abs(cube)`): compose per block, else materialise."""
        if len(self._current_records()) != 1:
            self._warn_materialize(op_name.strip("_"))
            return getattr(self.compute(), op_name)()
        return self._compose_cellwise(op_name)

    def __add__(self, other: Any) -> Any:
        """Add a real scalar cell by cell, lazily (a raster operand materialises)."""
        return self._binary_op(other, "__add__")

    def __radd__(self, other: Any) -> Any:
        """Right-hand add of a real scalar, lazily."""
        return self._binary_op(other, "__radd__")

    def __sub__(self, other: Any) -> Any:
        """Subtract a real scalar cell by cell, lazily."""
        return self._binary_op(other, "__sub__")

    def __rsub__(self, other: Any) -> Any:
        """Right-hand subtract of a real scalar, lazily."""
        return self._binary_op(other, "__rsub__")

    def __mul__(self, other: Any) -> Any:
        """Multiply by a real scalar cell by cell, lazily."""
        return self._binary_op(other, "__mul__")

    def __rmul__(self, other: Any) -> Any:
        """Right-hand multiply by a real scalar, lazily."""
        return self._binary_op(other, "__rmul__")

    def __truediv__(self, other: Any) -> Any:
        """Divide by a real scalar cell by cell, lazily."""
        return self._binary_op(other, "__truediv__")

    def __pow__(self, other: Any) -> Any:
        """Raise to a real-scalar power cell by cell, lazily."""
        return self._binary_op(other, "__pow__")

    def __ge__(self, other: Any) -> Any:
        """Cell-by-cell `>=` a real scalar, as a Byte mask, lazily."""
        return self._binary_op(other, "__ge__")

    def __gt__(self, other: Any) -> Any:
        """Cell-by-cell `>` a real scalar, lazily."""
        return self._binary_op(other, "__gt__")

    def __le__(self, other: Any) -> Any:
        """Cell-by-cell `<=` a real scalar, lazily."""
        return self._binary_op(other, "__le__")

    def __lt__(self, other: Any) -> Any:
        """Cell-by-cell `<` a real scalar, lazily."""
        return self._binary_op(other, "__lt__")

    def __neg__(self) -> Any:
        """Negate the values cell by cell, lazily."""
        return self._unary_op("__neg__")

    def __abs__(self) -> Any:
        """Absolute value cell by cell, lazily."""
        return self._unary_op("__abs__")

    def squeeze(self, dim: str | None = None) -> Any:
        """Drop length-one band dimensions, composing lazily (#1237).

        Pure band-axis reshape (no data touched), so it stays lazy: the dask array's size-1 band
        axes are squeezed and the band metadata narrowed to match :meth:`NetCDF.squeeze`. A
        multi-variable container squeezes eagerly at the boundary, warning once.
        """
        from pyramids.lazy_support import _assert_band_dimension

        if len(self._current_records()) != 1:
            self._warn_materialize("squeeze")
            return self.compute().squeeze(dim)
        name, rec = next(iter(self._current_records().items()))
        names = list(rec.band_names)
        sizes = list(rec.array.shape[: len(names)])
        if dim is not None:
            _assert_band_dimension(self._source, dim, caller="squeeze")
            length = sizes[names.index(dim)]
            if length != 1:
                raise ValueError(
                    f"squeeze() drops a dimension of length one, and {dim!r} has "
                    f"length {length}. Select one step first with isel({dim}=[0])."
                )
            gone = {dim}
        else:
            gone = {n for n, size in zip(names, sizes) if size == 1}
        if not gone:
            return self
        axes = tuple(i for i, n in enumerate(names) if n in gone)
        new_arr = rec.array.squeeze(axis=axes)
        new_names = [n for n in names if n not in gone]
        new_vmap = {n: rec.values_map.get(n) for n in new_names}
        new = rec._replace(
            array=new_arr,
            band_names=new_names,
            values_map=new_vmap,
            dim_names=(*new_names, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(
            self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
        )

    def transpose(self, *dims: Any) -> Any:
        """Reorder the band dimensions (spatial plane stays trailing), composing lazily (#1237).

        Pure band-axis permutation, so it stays lazy: `_transpose_order` resolves and validates the
        order (as :meth:`NetCDF.transpose`), then the dask array's band axes are permuted. A
        multi-variable container transposes eagerly at the boundary, warning once.
        """
        from pyramids.lazy_support import _transpose_order

        if len(self._current_records()) != 1:
            self._warn_materialize("transpose")
            return self.compute().transpose(*dims)
        name, rec = next(iter(self._current_records().items()))
        order = _transpose_order(list(rec.band_names), dims)
        perm = [rec.band_names.index(n) for n in order]
        ndim = rec.array.ndim
        new_arr = rec.array.transpose([*perm, ndim - 2, ndim - 1])
        new_vmap = {n: rec.values_map.get(n) for n in order}
        new = rec._replace(
            array=new_arr,
            band_names=list(order),
            values_map=new_vmap,
            dim_names=(*order, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(
            self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
        )

    def isel(self, *, drop: bool = False, **indexers: Any) -> Any:
        """Select bands by position along band dimensions, composing lazily (#1237).

        Pure band-axis indexing, so it stays lazy: each selector is resolved to positions (the same
        `_resolve_positional_indices` :meth:`NetCDF.isel` uses) and the dask array is indexed along
        that axis, the coordinates narrowed to match; `drop=True` squeezes the axes a scalar
        selector collapsed. A multi-variable container selects eagerly at the boundary, warning once.
        """
        from pyramids.lazy_support import (
            _assert_band_dimension,
            _resolve_positional_indices,
        )

        if not indexers:
            raise ValueError(
                "isel() requires at least one keyword argument, e.g. isel(time=0)."
            )
        if len(self._current_records()) != 1:
            self._warn_materialize("isel")
            return self.compute().isel(drop=drop, **indexers)
        rec = next(iter(self._current_records().values()))
        resolved: list[tuple[str, list[int]]] = []
        scalar_dims: list[str] = []
        for dim_name, selector in indexers.items():
            _assert_band_dimension(self._source, dim_name, caller="isel")
            axis = rec.band_names.index(dim_name)
            size = int(rec.array.shape[axis])
            resolved.append(
                (dim_name, _resolve_positional_indices(selector, size, dim_name))
            )
            if not isinstance(selector, (slice, list, tuple)):
                scalar_dims.append(dim_name)
        result: Any = self
        for dim_name, dim_indices in resolved:
            result = result._subset_band(dim_name, dim_indices)
        if drop:
            for dim_name in scalar_dims:
                # The False arm is unreachable through the public API and is left as a guard:
                # `indexers` is a dict, so a dimension cannot repeat, `_subset_band` preserves
                # dimensions, and `squeeze(dim)` drops only the dimension it is given — so a
                # scalar selector's dimension is always still present when the loop reaches it.
                if (
                    dim_name
                    in next(iter(result._current_records().values())).band_names
                ):
                    result = result.squeeze(dim_name)
        return result

    def sel(
        self,
        *,
        method: str | None = None,
        tolerance: float | None = None,
        **kwargs: Any,
    ) -> Any:
        """Select bands by coordinate value along band dimensions, composing lazily (#1237).

        The value twin of :meth:`isel`: each selector is resolved to positions against the source
        coordinates (the same `_resolve_one_dim` :meth:`NetCDF.sel` uses, honouring
        `method="nearest"` / `tolerance`), then the dask array is indexed — staying lazy. A
        multi-variable container selects eagerly at the boundary, warning once.
        """
        from pyramids.lazy_support import _resolve_one_dim

        if not kwargs:
            raise ValueError(
                "sel() requires at least one keyword argument, e.g. sel(time=6)."
            )
        if method not in (None, "nearest"):
            raise ValueError(
                f"sel() method must be None (exact) or 'nearest', got {method!r}."
            )
        if tolerance is not None and method != "nearest":
            raise ValueError(
                "sel() tolerance= is only meaningful with method='nearest' — without it "
                "a label either matches exactly or does not match at all, and there is no "
                "distance for a tolerance to bound."
            )
        if len(self._current_records()) != 1:
            self._warn_materialize("sel")
            return self.compute().sel(method=method, tolerance=tolerance, **kwargs)
        resolved = [
            (
                dim_name,
                _resolve_one_dim(self._source, dim_name, selector, method, tolerance),
            )
            for dim_name, selector in kwargs.items()
        ]
        result: Any = self
        for dim_name, dim_indices in resolved:
            result = result._subset_band(dim_name, dim_indices)
        return result

    def _subset_band(self, dim_name: str, dim_indices: list[int]) -> LazyNetCDF:
        """Index the single variable's dask array along one band axis and narrow its coordinates.

        The lazy, dimension-preserving twin of `_subset_along_dim`: a plain dask index along the
        band axis (no flatten, no compute), keeping the dimension and relabelling its coordinates.
        The caller has checked there is exactly one variable.

        Returns:
            LazyNetCDF: The cube with that band axis narrowed to `dim_indices`, still lazy.
        """
        name, rec = next(iter(self._current_records().items()))
        axis = rec.band_names.index(dim_name)
        new_arr = rec.array[(slice(None),) * axis + (list(dim_indices),)]
        coords = rec.values_map.get(dim_name)
        new_vmap = dict(rec.values_map)
        new_vmap[dim_name] = (
            [coords[i] for i in dim_indices] if coords is not None else None
        )
        new = rec._replace(array=new_arr, values_map=new_vmap)
        return LazyNetCDF(
            self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
        )

    def _compose_cellwise(self, method: str, *args: Any, **kwargs: Any) -> LazyNetCDF:
        """Run a cell-wise eager op (`clip`/`fillna`/`round`) per spatial block via map_blocks.

        These ops are element-wise and band-agnostic, and their shared gap/dtype machinery is not
        dask-safe (it mutates in place and calls `.all()`), so each numpy block is wrapped in a
        throwaway eager `Dataset` and run through the real method — bit-for-bit, shape-preserving.
        The output dtype and no-data value (data-independent for these ops) come from a 1x1 spatial
        probe. The caller has checked there is exactly one variable.

        Returns:
            LazyNetCDF: The cube with the cell-wise op applied, still lazy.
        """
        import dask.array as da
        from pyramids.dataset import Dataset
        from pyramids.lazy_support import GeoReference

        name, rec = next(iter(self._current_records().items()))
        ndv = rec.no_data

        def _block(block: Any) -> tuple[Any, Any]:
            flat = np.asarray(block)
            shape = flat.shape
            ds = Dataset.from_array(
                flat.reshape(-1, shape[-2], shape[-1]),
                geo_ref=GeoReference(
                    top_left_corner=(0.0, float(shape[-2])), cell_size=1.0, epsg=4326
                ),
                no_data_value=ndv,
            )
            out = getattr(ds, method)(*args, **kwargs)
            values = np.asarray(out.read_array(squeeze=True)).reshape(shape)
            return values, out.no_data_value[0]

        probe, out_ndv = _block(np.asarray(rec.array[..., :1, :1].compute()))
        new_arr = da.map_blocks(
            lambda block: _block(block)[0],
            rec.array,
            dtype=probe.dtype,
            meta=np.empty((0,) * rec.array.ndim, dtype=probe.dtype),
        )
        new = rec._replace(array=new_arr, no_data=out_ndv)
        return LazyNetCDF(
            self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
        )

    def _extremum(
        self, dim: str, extreme: str, coordinate: bool, caller: str, skipna: bool
    ) -> Any:
        """Shared `argmin`/`argmax`/`idxmin`/`idxmax`: collapse `dim` to its extremum, lazily.

        The extremum locators use `np.argmin`/`argmax` (numpy), so they compose via map_blocks like
        `rank`; collapsing `dim` is handled by `_compose_mapblocks`'s `drop_axis`. A multi-variable
        container locates eagerly at the boundary, warning once.

        Returns:
            LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.
        """
        from pyramids.lazy_support import _Extremum

        if len(self._current_records()) != 1:
            self._warn_materialize(caller)
            return getattr(self.compute(), caller)(dim, skipna=skipna)
        op = _Extremum(
            extreme=extreme,
            coordinate=coordinate,
            skipna=bool(skipna),
            caller=caller,
        )
        return self._compose_mapblocks(op, dim)

    def _compose_mapblocks(self, op: Any, dim: str) -> LazyNetCDF:
        """Run an `_AlongDim` op's eager kernel per spatial block via `dask.array.map_blocks`.

        For ops whose kernel is numpy/scipy and does not dispatch on dask (`rank`, `interp`, `pad`,
        the extremum locators), the band axis is rechunked whole so each block holds the full band
        axis, and the op's `apply` runs on each `(whole bands, y-chunk, x-chunk)` numpy block
        through `override`. The result is lazy and bit-for-bit with the eager op. The output band
        layout (names, coords, no-data, and any dimension the op drops or resizes) is read from a
        1x1 spatial probe, which is data-independent for these ops. The caller has checked there is
        exactly one variable.

        Returns:
            LazyNetCDF: The cube with the op applied, still lazy.
        """
        import dask.array as da

        op.start()
        name, rec = next(iter(self._current_records().items()))
        nbd = len(rec.band_names)
        arr = rec.array.rechunk({i: -1 for i in range(nbd)})
        meta_in = (list(rec.band_names), dict(rec.values_map), rec.no_data)

        def _run(block: Any) -> Any:
            return op.apply(self._source, None, dim, override=(block, *meta_in)).values

        probe = op.apply(
            self._source,
            None,
            dim,
            override=(np.asarray(arr[..., :1, :1].compute()), *meta_in),
        )
        out_names = list(probe.band_names)
        dropped = [i for i, band in enumerate(rec.band_names) if band not in out_names]
        band_chunks = tuple((int(size),) for size in probe.values.shape[:-2])
        new_arr = da.map_blocks(
            _run,
            arr,
            dtype=probe.values.dtype,
            drop_axis=dropped or None,
            chunks=(*band_chunks, arr.chunks[-2], arr.chunks[-1]),
            meta=np.empty((0,) * (len(band_chunks) + 2), dtype=probe.values.dtype),
        )
        new = rec._replace(
            array=new_arr,
            band_names=out_names,
            values_map=dict(probe.values_map),
            no_data=probe.no_data,
            dim_names=(*out_names, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(
            self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
        )

    def _compose_op(self, op: Any, dim: str) -> LazyNetCDF:
        """Run an `_AlongDim` op (one supporting `override`) on the single variable's dask array.

        Sets `op.materialize = False` so the op's `apply` returns a deferred dask array, and runs
        it on the cube's current array via `override`, for ops whose kernel keeps instance state
        (so a factored free function would be awkward). The caller has checked one variable.

        Returns:
            LazyNetCDF: The cube with the op applied, still lazy.
        """
        op.materialize = False
        name, rec = next(iter(self._current_records().items()))
        applied = op.apply(
            self._source,
            None,
            dim,
            override=(rec.array, rec.band_names, rec.values_map, rec.no_data),
        )
        new = rec._replace(
            array=applied.values,
            band_names=applied.band_names,
            values_map=applied.values_map,
            no_data=applied.no_data,
            dim_names=(*applied.band_names, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(
            self._source, {name: applied.values}, {name: new.dim_names}, {name: new}
        )

    def _compose_direct(self, kernel: Any, dim: str, **params: Any) -> LazyNetCDF:
        """Run a factored along-dim `kernel` on the single variable's dask array, deferring it.

        The kernel has the uniform signature
        `(nc, arr, band_names, values_map, no_data, dim, *, materialize, **params)` and returns
        `(array, band_names, values_map, no_data)`; called with `materialize=False` it keeps the
        result a `dask.array`, so the op composes with later lazy ops until :meth:`compute` (#1237).
        The caller has checked there is exactly one variable.

        Returns:
            LazyNetCDF: The cube with the op applied, still lazy.
        """
        name, rec = next(iter(self._current_records().items()))
        arr, band_names, values_map, no_data = kernel(
            self._source,
            rec.array,
            rec.band_names,
            rec.values_map,
            rec.no_data,
            dim,
            materialize=False,
            **params,
        )
        new = rec._replace(
            array=arr,
            band_names=band_names,
            values_map=values_map,
            no_data=no_data,
            dim_names=(*band_names, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(self._source, {name: arr}, {name: new.dim_names}, {name: new})

    def _compose_reduction(
        self,
        dim: str,
        how: str,
        *,
        skipna: bool,
        q: float | None,
        group_positions: list | None,
        resize: int | None,
        window_mean_coords: bool,
    ) -> LazyNetCDF:
        """Run a `_Reduction` kernel on the single variable's dask array, deferring it (#1237).

        Shared by :meth:`reduce` and :meth:`coarsen`; the caller has checked there is exactly one
        variable. The reduced array stays a `dask.array` (`materialize=False`), so the step composes
        with later lazy ops until :meth:`compute`.

        Returns:
            LazyNetCDF: The cube with the dimension reduced, still lazy.
        """
        from pyramids.lazy_support import _reduced_array

        name, rec = next(iter(self._current_records().items()))
        arr, band_names, values_map, no_data = _reduced_array(
            self._source,
            None,
            dim,
            how,
            group_positions=group_positions,
            skipna=skipna,
            q=q,
            resize=resize,
            window_mean_coords=window_mean_coords,
            materialize=False,
            override=(rec.array, rec.band_names, rec.values_map, rec.no_data),
        )
        new = rec._replace(
            array=arr,
            band_names=band_names,
            values_map=values_map,
            no_data=no_data,
            dim_names=(*band_names, *rec.dim_names[-2:]),
        )
        return LazyNetCDF(self._source, {name: arr}, {name: new.dim_names}, {name: new})

    def _warn_materialize(self, name: str) -> None:
        """Emit the one-time boundary warning when an op falls back to the eager cube."""
        if not self._materialize_warned:
            warnings.warn(
                f"{name!r} on a lazy NetCDF cube is not composed lazily yet; it materialises the "
                f"cube and runs eagerly. Call .compute() for an explicit eager cube.",
                UserWarning,
                stacklevel=3,
            )
            self._materialize_warned = True

    def __getattr__(self, name: str) -> Any:
        """Delegate any eager operation to the **source** cube, warning once.

        Reached only for names this wrapper does not define. A leading-underscore name is refused
        outright (it is internal, not an operation). A public name absent from the eager cube also
        raises :class:`AttributeError` **without** warning, so an existence probe (`hasattr`, feature
        detection) neither warns spuriously nor burns the once-flag. The attribute is fetched exactly
        once (a sentinel default on a single `getattr`, not a `hasattr`-then-`getattr`), so a property
        getter on the source is not run twice.

        A public name that exists forwards to :data:`self._source` **directly**, aliasing the source
        (the `load` contract) rather than returning an independent :meth:`compute` copy: a functional
        op returns a new cube and leaves the source intact, but an in-place mutator reached this way
        would touch the source, so call :meth:`compute` first when you need an isolated eager cube.
        This is the v1 lazy/eager boundary (array-native ops do not yet compose lazily); it emits a
        one-time :class:`UserWarning`, and the once-flag is set only after `warn` returns, so a first
        access under warnings-as-error does not silence later boundary warnings.
        """
        if name.startswith("_"):
            raise AttributeError(name)
        # A transformed cube materialises its deferred arrays (the rebuilt eager result); an
        # untransformed view aliases its source, as before.
        target = self._rebuild() if self._records is not None else self._source
        attr = getattr(target, name, _MISSING)
        if attr is _MISSING:
            raise AttributeError(name)
        if not self._materialize_warned:
            warnings.warn(
                f"Accessing {name!r} on a lazy NetCDF cube materialises it onto the eager source "
                f"cube (aliasing it, like .load()); call .compute() for an independent eager cube.",
                UserWarning,
                stacklevel=2,
            )
            self._materialize_warned = True
        return attr

    def __repr__(self) -> str:
        """A short, dask-aware summary naming the variables and their chunking."""
        return f"LazyNetCDF(variables={self.variable_names}, chunks={self.chunks})"

chunks property #

Per-dimension chunk sizes, keyed by dimension name.

Merged across variables — two variables that disagree on a shared dimension leave the last one's chunking here until :meth:unify_chunks reconciles them.

chunksizes property #

Alias of :attr:chunks (xarray keeps both).

variable_names property #

The gridded variables this view carries, in build order.

__abs__() #

Absolute value cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __abs__(self) -> Any:
    """Absolute value cell by cell, lazily."""
    return self._unary_op("__abs__")

__add__(other) #

Add a real scalar cell by cell, lazily (a raster operand materialises).

Source code in src/pyramids_lazy/_lazy_cube.py
def __add__(self, other: Any) -> Any:
    """Add a real scalar cell by cell, lazily (a raster operand materialises)."""
    return self._binary_op(other, "__add__")

__ge__(other) #

Cell-by-cell >= a real scalar, as a Byte mask, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __ge__(self, other: Any) -> Any:
    """Cell-by-cell `>=` a real scalar, as a Byte mask, lazily."""
    return self._binary_op(other, "__ge__")

__getattr__(name) #

Delegate any eager operation to the source cube, warning once.

Reached only for names this wrapper does not define. A leading-underscore name is refused outright (it is internal, not an operation). A public name absent from the eager cube also raises :class:AttributeError without warning, so an existence probe (hasattr, feature detection) neither warns spuriously nor burns the once-flag. The attribute is fetched exactly once (a sentinel default on a single getattr, not a hasattr-then-getattr), so a property getter on the source is not run twice.

A public name that exists forwards to :data:self._source directly, aliasing the source (the load contract) rather than returning an independent :meth:compute copy: a functional op returns a new cube and leaves the source intact, but an in-place mutator reached this way would touch the source, so call :meth:compute first when you need an isolated eager cube. This is the v1 lazy/eager boundary (array-native ops do not yet compose lazily); it emits a one-time :class:UserWarning, and the once-flag is set only after warn returns, so a first access under warnings-as-error does not silence later boundary warnings.

Source code in src/pyramids_lazy/_lazy_cube.py
def __getattr__(self, name: str) -> Any:
    """Delegate any eager operation to the **source** cube, warning once.

    Reached only for names this wrapper does not define. A leading-underscore name is refused
    outright (it is internal, not an operation). A public name absent from the eager cube also
    raises :class:`AttributeError` **without** warning, so an existence probe (`hasattr`, feature
    detection) neither warns spuriously nor burns the once-flag. The attribute is fetched exactly
    once (a sentinel default on a single `getattr`, not a `hasattr`-then-`getattr`), so a property
    getter on the source is not run twice.

    A public name that exists forwards to :data:`self._source` **directly**, aliasing the source
    (the `load` contract) rather than returning an independent :meth:`compute` copy: a functional
    op returns a new cube and leaves the source intact, but an in-place mutator reached this way
    would touch the source, so call :meth:`compute` first when you need an isolated eager cube.
    This is the v1 lazy/eager boundary (array-native ops do not yet compose lazily); it emits a
    one-time :class:`UserWarning`, and the once-flag is set only after `warn` returns, so a first
    access under warnings-as-error does not silence later boundary warnings.
    """
    if name.startswith("_"):
        raise AttributeError(name)
    # A transformed cube materialises its deferred arrays (the rebuilt eager result); an
    # untransformed view aliases its source, as before.
    target = self._rebuild() if self._records is not None else self._source
    attr = getattr(target, name, _MISSING)
    if attr is _MISSING:
        raise AttributeError(name)
    if not self._materialize_warned:
        warnings.warn(
            f"Accessing {name!r} on a lazy NetCDF cube materialises it onto the eager source "
            f"cube (aliasing it, like .load()); call .compute() for an independent eager cube.",
            UserWarning,
            stacklevel=2,
        )
        self._materialize_warned = True
    return attr

__gt__(other) #

Cell-by-cell > a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __gt__(self, other: Any) -> Any:
    """Cell-by-cell `>` a real scalar, lazily."""
    return self._binary_op(other, "__gt__")

__init__(source, arrays, dim_names, records=None) #

Wrap per-variable dask arrays alongside the eager cube they came from.

Parameters:

Name Type Description Default
source NetCDF

The eager cube this view was built from — the source of its metadata and the twin :meth:compute returns when no op has transformed the arrays.

required
arrays dict[str, Any]

One dask array per gridded variable, laid out (*band_sizes, rows, cols).

required
dim_names dict[str, tuple[str, ...]]

The dimension names per variable, outermost first, i.e. (*band_dim_names, row_name, col_name) — the keys :attr:chunks reports.

required
records dict[str, _LazyVar] | None

Per-variable deferred state, set once an array-native op has composed over the cube (#1237). When present the cube is transformed: :meth:compute rebuilds the eager cube from these arrays rather than returning the source. None (the default, from :meth:NetCDF.chunk) is an untransformed view whose arrays are chunked reads of the source.

None
Source code in src/pyramids_lazy/_lazy_cube.py
def __init__(
    self,
    source: NetCDF,
    arrays: dict[str, Any],
    dim_names: dict[str, tuple[str, ...]],
    records: dict[str, _LazyVar] | None = None,
) -> None:
    """Wrap per-variable dask arrays alongside the eager cube they came from.

    Args:
        source: The eager cube this view was built from — the source of its metadata and the
            twin :meth:`compute` returns when no op has transformed the arrays.
        arrays: One dask array per gridded variable, laid out `(*band_sizes, rows, cols)`.
        dim_names: The dimension names per variable, outermost first, i.e.
            `(*band_dim_names, row_name, col_name)` — the keys :attr:`chunks` reports.
        records: Per-variable deferred state, set once an array-native op has composed over the
            cube (#1237). When present the cube is **transformed**: :meth:`compute` rebuilds the
            eager cube from these arrays rather than returning the source. `None` (the default,
            from :meth:`NetCDF.chunk`) is an untransformed view whose arrays are chunked reads of
            the source.
    """
    self._source = source
    self._arrays = dict(arrays)
    self._dim_names = dict(dim_names)
    # Copied, not aliased, to match `arrays` / `dim_names` above: views share a source cube but
    # never a mutable mapping.
    self._records = None if records is None else dict(records)
    self._materialize_warned = False

__le__(other) #

Cell-by-cell <= a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __le__(self, other: Any) -> Any:
    """Cell-by-cell `<=` a real scalar, lazily."""
    return self._binary_op(other, "__le__")

__lt__(other) #

Cell-by-cell < a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __lt__(self, other: Any) -> Any:
    """Cell-by-cell `<` a real scalar, lazily."""
    return self._binary_op(other, "__lt__")

__mul__(other) #

Multiply by a real scalar cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __mul__(self, other: Any) -> Any:
    """Multiply by a real scalar cell by cell, lazily."""
    return self._binary_op(other, "__mul__")

__neg__() #

Negate the values cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __neg__(self) -> Any:
    """Negate the values cell by cell, lazily."""
    return self._unary_op("__neg__")

__pow__(other) #

Raise to a real-scalar power cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __pow__(self, other: Any) -> Any:
    """Raise to a real-scalar power cell by cell, lazily."""
    return self._binary_op(other, "__pow__")

__radd__(other) #

Right-hand add of a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __radd__(self, other: Any) -> Any:
    """Right-hand add of a real scalar, lazily."""
    return self._binary_op(other, "__radd__")

__repr__() #

A short, dask-aware summary naming the variables and their chunking.

Source code in src/pyramids_lazy/_lazy_cube.py
def __repr__(self) -> str:
    """A short, dask-aware summary naming the variables and their chunking."""
    return f"LazyNetCDF(variables={self.variable_names}, chunks={self.chunks})"

__rmul__(other) #

Right-hand multiply by a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __rmul__(self, other: Any) -> Any:
    """Right-hand multiply by a real scalar, lazily."""
    return self._binary_op(other, "__rmul__")

__rsub__(other) #

Right-hand subtract of a real scalar, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __rsub__(self, other: Any) -> Any:
    """Right-hand subtract of a real scalar, lazily."""
    return self._binary_op(other, "__rsub__")

__sub__(other) #

Subtract a real scalar cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __sub__(self, other: Any) -> Any:
    """Subtract a real scalar cell by cell, lazily."""
    return self._binary_op(other, "__sub__")

__truediv__(other) #

Divide by a real scalar cell by cell, lazily.

Source code in src/pyramids_lazy/_lazy_cube.py
def __truediv__(self, other: Any) -> Any:
    """Divide by a real scalar cell by cell, lazily."""
    return self._binary_op(other, "__truediv__")

argmax(dim, *, skipna=True) #

Position of the maximum along a band dimension, composing lazily (#1237).

Source code in src/pyramids_lazy/_lazy_cube.py
def argmax(self, dim: str, *, skipna: bool = True) -> Any:
    """Position of the maximum along a band dimension, composing lazily (#1237)."""
    return self._extremum(dim, "max", False, "argmax", skipna)

argmin(dim, *, skipna=True) #

Position of the minimum along a band dimension, composing lazily (#1237).

Source code in src/pyramids_lazy/_lazy_cube.py
def argmin(self, dim: str, *, skipna: bool = True) -> Any:
    """Position of the minimum along a band dimension, composing lazily (#1237)."""
    return self._extremum(dim, "min", False, "argmin", skipna)

clip(min=None, max=None) #

Bound the values to [min, max], composing lazily (#1237); a gap stays a gap.

Cell-wise and shape-preserving, so it runs the eager :meth:NetCDF.clip per spatial block via map_blocks (bit-for-bit; the dtype widening is bound-based, so it is the same for every block). A multi-variable container clips eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def clip(self, min: Any = None, max: Any = None) -> Any:
    """Bound the values to `[min, max]`, composing lazily (#1237); a gap stays a gap.

    Cell-wise and shape-preserving, so it runs the eager :meth:`NetCDF.clip` per spatial block
    via map_blocks (bit-for-bit; the dtype widening is bound-based, so it is the same for every
    block). A multi-variable container clips eagerly at the boundary, warning once.
    """
    if len(self._current_records()) != 1:
        self._warn_materialize("clip")
        return self.compute().clip(min=min, max=max)
    return self._compose_cellwise("clip", min, max)

coarsen(dim, window, *, how='mean', boundary='exact', skipna=True, q=None) #

Block-aggregate a band dimension into windows, composing lazily for a single variable.

The lazy twin of :meth:NetCDF.coarsen — the same grouped reduction, kept as a deferred dask.array step so it composes with other lazy ops. A multi-variable container materialises at the boundary and coarsens eagerly, warning once (as :meth:reduce does).

Parameters:

Name Type Description Default
dim str

The band dimension to block-aggregate.

required
window int

The number of steps per window.

required
how str

The reduction, as :meth:NetCDF.coarsen.

'mean'
boundary str

How a trailing partial window is handled ("exact", "trim", "pad").

'exact'
skipna bool

Whether gaps are skipped.

True
q float | None

The quantile, for how="quantile".

None

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager coarsened cube for a

Any

multi-variable container.

Source code in src/pyramids_lazy/_lazy_cube.py
def coarsen(
    self,
    dim: str,
    window: int,
    *,
    how: str = "mean",
    boundary: str = "exact",
    skipna: bool = True,
    q: float | None = None,
) -> Any:
    """Block-aggregate a band dimension into windows, composing lazily for a single variable.

    The lazy twin of :meth:`NetCDF.coarsen` — the same grouped reduction, kept as a deferred
    `dask.array` step so it composes with other lazy ops. A multi-variable container
    materialises at the boundary and coarsens eagerly, warning once (as :meth:`reduce` does).

    Args:
        dim: The band dimension to block-aggregate.
        window: The number of steps per window.
        how: The reduction, as :meth:`NetCDF.coarsen`.
        boundary: How a trailing partial window is handled (`"exact"`, `"trim"`, `"pad"`).
        skipna: Whether gaps are skipped.
        q: The quantile, for `how="quantile"`.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager coarsened cube for a
        multi-variable container.
    """
    from pyramids.lazy_support import (
        _BOUNDARIES,
        COUNTING_REDUCERS,
        REDUCERS,
        _check_how,
        _check_quantile,
        _check_window,
        _coarsen_windows,
    )

    _check_how(how, {*REDUCERS, *COUNTING_REDUCERS})
    q = _check_quantile(how, q)
    length = _check_window(window, caller="coarsen")
    if boundary not in _BOUNDARIES:
        raise ValueError(
            f"boundary must be one of {list(_BOUNDARIES)}, got {boundary!r}."
        )
    records = self._current_records()
    if len(records) != 1:
        self._warn_materialize("coarsen")
        return self.compute().coarsen(
            dim, window, how=how, boundary=boundary, skipna=skipna, q=q
        )
    (rec,) = records.values()
    size = rec.array.shape[rec.band_names.index(dim)]
    resized, positions = _coarsen_windows(dim, size, length, boundary)
    return self._compose_reduction(
        dim,
        how,
        skipna=skipna,
        q=q,
        group_positions=positions,
        resize=resized,
        window_mean_coords=True,
    )

compute(**kwargs) #

Materialise the lazy view and return an independent eager NetCDF cube.

The lazy arrays are chunked reads of the source cube, so the result holds the same data; it is returned as an independent copy (compute() is not the source), matching xarray's compute(), so mutating the result never touches the original or any other compute() result. The **kwargs are accepted for API symmetry with xarray / dask and ignored — in this v1 the data is a view of the already-eager source, so nothing has to be scheduled.

Returns:

Name Type Description
NetCDF NetCDF

A fresh eager cube, independent of the source.

Source code in src/pyramids_lazy/_lazy_cube.py
def compute(self, **kwargs: Any) -> NetCDF:
    """Materialise the lazy view and return an **independent** eager NetCDF cube.

    The lazy arrays are chunked reads of the source cube, so the result holds the same data; it is
    returned as an independent copy (`compute() is not` the source), matching xarray's `compute()`,
    so mutating the result never touches the original or any other `compute()` result. The
    `**kwargs` are accepted for API symmetry with xarray / dask and ignored — in this v1 the data is
    a view of the already-eager source, so nothing has to be scheduled.

    Returns:
        NetCDF: A fresh eager cube, independent of the source.
    """
    if self._records is not None:
        # A cube transformed by a composed op (#1237): materialise the deferred dask arrays
        # through the eager rebuild rather than returning the (untransformed) source.
        return self._rebuild()
    return self._source.copy()

cumsum(dim, *, skipna=True) #

Running total along a band dimension, composing lazily (#1237).

The lazy twin of :meth:NetCDF.cumsum; the cumulative sum stays a deferred dask.array step, keeping the dimension's length. A multi-variable container totals eagerly at the boundary, warning once.

Parameters:

Name Type Description Default
dim str

The band dimension to total along.

required
skipna bool

Whether gaps are skipped.

True

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager totalled cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def cumsum(self, dim: str, *, skipna: bool = True) -> Any:
    """Running total along a band dimension, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.cumsum`; the cumulative sum stays a deferred `dask.array`
    step, keeping the dimension's length. A multi-variable container totals eagerly at the
    boundary, warning once.

    Args:
        dim: The band dimension to total along.
        skipna: Whether gaps are skipped.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager totalled cube otherwise.
    """
    from pyramids.lazy_support import _CumSum

    if len(self._current_records()) != 1:
        self._warn_materialize("cumsum")
        return self.compute().cumsum(dim, skipna=skipna)
    return self._compose_op(_CumSum(skipna=bool(skipna)), dim)

diff(dim, n=1, *, label='upper') #

Difference neighbouring steps along a band dimension, composing lazily (#1237).

The lazy twin of :meth:NetCDF.diff; the difference stays a deferred dask.array step. A multi-variable container differences eagerly at the boundary, warning once.

Parameters:

Name Type Description Default
dim str

The band dimension to difference along.

required
n int

The order (how many times the difference is taken).

1
label str

"upper" or "lower" — which step each difference is labelled with.

'upper'

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager diffed cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def diff(self, dim: str, n: int = 1, *, label: str = "upper") -> Any:
    """Difference neighbouring steps along a band dimension, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.diff`; the difference stays a deferred `dask.array` step.
    A multi-variable container differences eagerly at the boundary, warning once.

    Args:
        dim: The band dimension to difference along.
        n: The order (how many times the difference is taken).
        label: `"upper"` or `"lower"` — which step each difference is labelled with.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager diffed cube otherwise.
    """
    from pyramids.lazy_support import _DIFF_LABELS, _check_order, _diffed_array

    order = _check_order(n)
    if label not in _DIFF_LABELS:
        raise ValueError(
            f"label must be one of {list(_DIFF_LABELS)}, got {label!r}."
        )
    if len(self._current_records()) != 1:
        self._warn_materialize("diff")
        return self.compute().diff(dim, n, label=label)
    return self._compose_direct(_diffed_array, dim, n=order, label=label)

fillna(value) #

Fill the gaps with value, composing lazily (#1237).

Cell-wise and shape-preserving; runs the eager :meth:NetCDF.fillna per block via map_blocks. A multi-variable container fills eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def fillna(self, value: float | int) -> Any:
    """Fill the gaps with `value`, composing lazily (#1237).

    Cell-wise and shape-preserving; runs the eager :meth:`NetCDF.fillna` per block via
    map_blocks. A multi-variable container fills eagerly at the boundary, warning once.
    """
    if len(self._current_records()) != 1:
        self._warn_materialize("fillna")
        return self.compute().fillna(value)
    return self._compose_cellwise("fillna", value)

idxmax(dim, *, skipna=True) #

Coordinate of the maximum along a band dimension, composing lazily (#1237).

Source code in src/pyramids_lazy/_lazy_cube.py
def idxmax(self, dim: str, *, skipna: bool = True) -> Any:
    """Coordinate of the maximum along a band dimension, composing lazily (#1237)."""
    return self._extremum(dim, "max", True, "idxmax", skipna)

idxmin(dim, *, skipna=True) #

Coordinate of the minimum along a band dimension, composing lazily (#1237).

Source code in src/pyramids_lazy/_lazy_cube.py
def idxmin(self, dim: str, *, skipna: bool = True) -> Any:
    """Coordinate of the minimum along a band dimension, composing lazily (#1237)."""
    return self._extremum(dim, "min", True, "idxmin", skipna)

interp(method='linear', **coords) #

Interpolate band dimensions onto new coordinate values, composing lazily (#1237).

The lazy twin of :meth:NetCDF.interp; each dim=targets interpolation (a scipy kernel) runs per spatial block via map_blocks, resizing the dimension to the target length. Several dim=targets pairs compose in sequence. A multi-variable container interpolates eagerly at the boundary, warning once.

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def interp(self, method: str = "linear", **coords: Any) -> Any:
    """Interpolate band dimensions onto new coordinate values, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.interp`; each `dim=targets` interpolation (a scipy kernel)
    runs per spatial block via map_blocks, resizing the dimension to the target length. Several
    `dim=targets` pairs compose in sequence. A multi-variable container interpolates eagerly at
    the boundary, warning once.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.
    """
    from pyramids.lazy_support import (
        _INTERP_MIN_POINTS,
        _interp_targets,
        _InterpTo,
        _refuse_spatial_band_op,
        _resolve_interp_kind,
    )

    kind = _resolve_interp_kind(method)
    if not coords or len(self._current_records()) != 1:
        self._warn_materialize("interp")
        return self.compute().interp(method, **coords)
    result: Any = self
    for dim, target in coords.items():
        _refuse_spatial_band_op(self._source, dim, caller="interp")
        rec = next(iter(result._current_records().values()))
        if dim not in rec.band_names:
            raise ValueError(
                f"interp(): {dim!r} is not a band dimension of this variable."
            )
        size = rec.array.shape[rec.band_names.index(dim)]
        minimum = _INTERP_MIN_POINTS[kind]
        if size < minimum:
            raise ValueError(
                f"interp() method {kind!r} needs at least {minimum} source steps along "
                f"{dim!r}, got {size}."
            )
        targets = _interp_targets(target, dim, caller="interp")
        result = result._compose_mapblocks(
            _InterpTo(target=targets, kind=kind, caller="interp"), dim
        )
    return result

interpolate_na(dim, method='linear', *, limit=None, use_coordinate=True) #

Fill interior gaps along a band dimension, composing lazily (#1237).

The lazy twin of :meth:NetCDF.interpolate_na; the interpolation (a scipy kernel) runs per spatial block via map_blocks, keeping the dimension's length. A multi-variable container interpolates eagerly at the boundary, warning once.

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def interpolate_na(
    self,
    dim: str,
    method: str = "linear",
    *,
    limit: int | None = None,
    use_coordinate: bool = True,
) -> Any:
    """Fill interior gaps along a band dimension, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.interpolate_na`; the interpolation (a scipy kernel) runs per
    spatial block via map_blocks, keeping the dimension's length. A multi-variable container
    interpolates eagerly at the boundary, warning once.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager result otherwise.
    """
    from pyramids.lazy_support import _check_limit, _Interpolate

    if len(self._current_records()) != 1:
        self._warn_materialize("interpolate_na")
        return self.compute().interpolate_na(
            dim, method, limit=limit, use_coordinate=use_coordinate
        )
    op = _Interpolate(
        method=method,
        limit=_check_limit(limit, caller="interpolate_na"),
        use_coordinate=bool(use_coordinate),
    )
    return self._compose_mapblocks(op, dim)

isel(*, drop=False, **indexers) #

Select bands by position along band dimensions, composing lazily (#1237).

Pure band-axis indexing, so it stays lazy: each selector is resolved to positions (the same _resolve_positional_indices :meth:NetCDF.isel uses) and the dask array is indexed along that axis, the coordinates narrowed to match; drop=True squeezes the axes a scalar selector collapsed. A multi-variable container selects eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def isel(self, *, drop: bool = False, **indexers: Any) -> Any:
    """Select bands by position along band dimensions, composing lazily (#1237).

    Pure band-axis indexing, so it stays lazy: each selector is resolved to positions (the same
    `_resolve_positional_indices` :meth:`NetCDF.isel` uses) and the dask array is indexed along
    that axis, the coordinates narrowed to match; `drop=True` squeezes the axes a scalar
    selector collapsed. A multi-variable container selects eagerly at the boundary, warning once.
    """
    from pyramids.lazy_support import (
        _assert_band_dimension,
        _resolve_positional_indices,
    )

    if not indexers:
        raise ValueError(
            "isel() requires at least one keyword argument, e.g. isel(time=0)."
        )
    if len(self._current_records()) != 1:
        self._warn_materialize("isel")
        return self.compute().isel(drop=drop, **indexers)
    rec = next(iter(self._current_records().values()))
    resolved: list[tuple[str, list[int]]] = []
    scalar_dims: list[str] = []
    for dim_name, selector in indexers.items():
        _assert_band_dimension(self._source, dim_name, caller="isel")
        axis = rec.band_names.index(dim_name)
        size = int(rec.array.shape[axis])
        resolved.append(
            (dim_name, _resolve_positional_indices(selector, size, dim_name))
        )
        if not isinstance(selector, (slice, list, tuple)):
            scalar_dims.append(dim_name)
    result: Any = self
    for dim_name, dim_indices in resolved:
        result = result._subset_band(dim_name, dim_indices)
    if drop:
        for dim_name in scalar_dims:
            # The False arm is unreachable through the public API and is left as a guard:
            # `indexers` is a dict, so a dimension cannot repeat, `_subset_band` preserves
            # dimensions, and `squeeze(dim)` drops only the dimension it is given — so a
            # scalar selector's dimension is always still present when the loop reaches it.
            if (
                dim_name
                in next(iter(result._current_records().values())).band_names
            ):
                result = result.squeeze(dim_name)
    return result

load(**kwargs) #

Materialise in place and return the eager cube — the in-place form of :meth:compute.

Matches xarray's load, which realises in place and returns the same object: this returns the source cube itself (not a copy), so it aliases the cube .chunk() was called on. Use :meth:compute when you need an independent result. The **kwargs are accepted for symmetry and ignored.

Returns:

Name Type Description
NetCDF NetCDF

The source cube (the eager twin), realised in place. A transformed cube

NetCDF

(a composed op) has no untransformed source to alias, so it returns the rebuilt eager

NetCDF

result, as :meth:compute does.

Source code in src/pyramids_lazy/_lazy_cube.py
def load(self, **kwargs: Any) -> NetCDF:
    """Materialise in place and return the eager cube — the in-place form of :meth:`compute`.

    Matches xarray's `load`, which realises in place and returns the same object: this returns the
    **source cube itself** (not a copy), so it aliases the cube `.chunk()` was called on. Use
    :meth:`compute` when you need an independent result. The `**kwargs` are accepted for symmetry
    and ignored.

    Returns:
        NetCDF: The source cube (the eager twin), realised in place. A transformed cube
        (a composed op) has no untransformed source to alias, so it returns the rebuilt eager
        result, as :meth:`compute` does.
    """
    if self._records is not None:
        return self._rebuild()
    return self._source

pad(*, mode='constant', constant_values=None, **pad_width) #

Pad band dimensions with a constant fill, composing lazily (#1237).

The lazy twin of :meth:NetCDF.pad for band dimensions (extended via map_blocks). A spatial-axis pad moves the geotransform (a GDAL op) and a multi-variable container cannot compose, so either materialises at the boundary, warning once; several band dimensions in one call compose in sequence.

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube when every padded dimension is a band dimension of a

Any

single variable; an eager padded cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def pad(
    self, *, mode: str = "constant", constant_values: Any = None, **pad_width: Any
) -> Any:
    """Pad band dimensions with a constant fill, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.pad` for **band** dimensions (extended via map_blocks). A
    spatial-axis pad moves the geotransform (a GDAL op) and a multi-variable container cannot
    compose, so either materialises at the boundary, warning once; several band dimensions in
    one call compose in sequence.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube when every padded dimension is a band dimension of a
        single variable; an eager padded cube otherwise.
    """
    from pyramids.lazy_support import _Pad, _pad_before_after

    records = self._current_records()
    band_names = next(iter(records.values())).band_names if records else []
    if (
        mode != "constant"
        or not pad_width
        or len(records) != 1
        or any(dim not in band_names for dim in pad_width)
    ):
        self._warn_materialize("pad")
        return self.compute().pad(
            mode=mode, constant_values=constant_values, **pad_width
        )
    result: Any = self
    for dim, width in pad_width.items():
        before, after = _pad_before_after(width, dim)
        result = result._compose_mapblocks(
            _Pad(before=before, after=after, fill_value=constant_values), dim
        )
    return result

persist(**kwargs) #

Realise the dask graph in worker memory, staying lazy.

Returns a new :class:LazyNetCDF whose arrays are dask.persist-ed — the graph is kept but its results are held in memory / across the cluster, so later reads do not recompute it.

A composed array-native op survives the round trip: the deferred per-variable records are carried onto the new view, exactly as unify_chunks() carries them, so persist().compute() rebuilds the transformed cube instead of falling back to the untransformed source. A view straight from NetCDF.chunk() carries no records, and persist() invents none.

Parameters:

Name Type Description Default
**kwargs Any

Forwarded verbatim to dask.persist — for example scheduler="threads" or optimize_graph=False.

{}

Returns:

Name Type Description
LazyNetCDF LazyNetCDF

A lazy view over the persisted arrays, keeping this view's chunking and its

LazyNetCDF

composed op.

Examples:

  • Persisting stays lazy and keeps the chunking:
    >>> from pyramids.netcdf import NetCDF
    >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
    >>> lazy = cube.get_variable("temperature").chunk("auto")
    >>> lazy.chunks
    {'time': (4,), 'pressure_level': (3,), 'lat': (5,), 'lon': (6,)}
    >>> lazy.persist().chunks == lazy.chunks
    True
    >>> cube.close()
    
  • A composed reduction survives persisting: the view computes to the time-collapsed result, not back to the four-step source it was read from:
    >>> from pyramids.netcdf import NetCDF
    >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
    >>> lazy = cube.get_variable("temperature").chunk("auto")
    >>> lazy.compute().read_array().shape
    (4, 3, 5, 6)
    >>> lazy.reduce("time", "mean").persist().compute().read_array().shape
    (3, 5, 6)
    >>> cube.close()
    
See Also

unify_chunks: Reconciles shared-dimension chunking across two or more variables; a composed (single-variable) view is already unified, so it is returned unchanged. compute: Materialises the view — the rebuilt transformed cube once an op has composed.

Source code in src/pyramids_lazy/_lazy_cube.py
def persist(self, **kwargs: Any) -> LazyNetCDF:
    """Realise the dask graph in worker memory, staying lazy.

    Returns a new :class:`LazyNetCDF` whose arrays are `dask.persist`-ed — the graph is kept but
    its results are held in memory / across the cluster, so later reads do not recompute it.

    A composed array-native op survives the round trip: the deferred per-variable records are
    carried onto the new view, exactly as `unify_chunks()` carries them, so `persist().compute()`
    rebuilds the *transformed* cube instead of falling back to the untransformed source. A view
    straight from `NetCDF.chunk()` carries no records, and `persist()` invents none.

    Args:
        **kwargs: Forwarded verbatim to `dask.persist` — for example `scheduler="threads"` or
            `optimize_graph=False`.

    Returns:
        LazyNetCDF: A lazy view over the persisted arrays, keeping this view's chunking and its
        composed op.

    Examples:
        - Persisting stays lazy and keeps the chunking:
            ```python
            >>> from pyramids.netcdf import NetCDF
            >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
            >>> lazy = cube.get_variable("temperature").chunk("auto")
            >>> lazy.chunks
            {'time': (4,), 'pressure_level': (3,), 'lat': (5,), 'lon': (6,)}
            >>> lazy.persist().chunks == lazy.chunks
            True
            >>> cube.close()

            ```
        - A composed reduction survives persisting: the view computes to the time-collapsed
          result, not back to the four-step source it was read from:
            ```python
            >>> from pyramids.netcdf import NetCDF
            >>> cube = NetCDF.read_file("tests/data/netcdf/cf__5v__1d4-4d1__y-asc.nc")
            >>> lazy = cube.get_variable("temperature").chunk("auto")
            >>> lazy.compute().read_array().shape
            (4, 3, 5, 6)
            >>> lazy.reduce("time", "mean").persist().compute().read_array().shape
            (3, 5, 6)
            >>> cube.close()

            ```

    See Also:
        unify_chunks: Reconciles shared-dimension chunking across two or more variables; a
            composed (single-variable) view is already unified, so it is returned unchanged.
        compute: Materialises the view — the rebuilt transformed cube once an op has composed.
    """
    import_dask(_DASK_MISSING)
    import dask

    names = list(self._arrays)
    record_items = list(self._records.items()) if self._records is not None else []
    # One dask.persist call for both mappings: the record's array is the graph `_rebuild` and
    # every `_compose_*` actually read, so persisting only `_arrays` would leave them
    # recomputing from the store while this view claimed to be in memory. Persisting together
    # also keeps shared parts of the graph shared.
    realised = dask.persist(
        *(self._arrays[name] for name in names),
        *(record.array for _, record in record_items),
        **kwargs,
    )
    arrays = dict(zip(names, realised[: len(names)]))
    records = None
    if self._records is not None:
        # Carry _records through, as unify_chunks does: without them compute() takes its
        # "untransformed" branch and returns self._source.copy(), discarding a composed op.
        records = {
            name: record._replace(array=array)
            for (name, record), array in zip(record_items, realised[len(names) :])
        }
    return LazyNetCDF(self._source, arrays, self._dim_names, records)

rank(dim, *, pct=False) #

Rank each pixel's values along a band dimension, composing lazily (#1237).

The lazy twin of :meth:NetCDF.rank. rank uses scipy.stats.rankdata, which needs a numpy array, so it cannot stay a pure dask graph; instead the eager kernel runs per spatial block (the band axis whole) through dask.array.map_blocks, which is lazy and bit-for-bit with the eager rank. A multi-variable container ranks eagerly at the boundary, warning once.

Parameters:

Name Type Description Default
dim str

The band dimension to rank along.

required
pct bool

Whether to return ranks as a fraction of the valid count.

False

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager ranked cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def rank(self, dim: str, *, pct: bool = False) -> Any:
    """Rank each pixel's values along a band dimension, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.rank`. `rank` uses `scipy.stats.rankdata`, which needs a
    numpy array, so it cannot stay a pure dask graph; instead the eager kernel runs per spatial
    block (the band axis whole) through `dask.array.map_blocks`, which is lazy and bit-for-bit
    with the eager rank. A multi-variable container ranks eagerly at the boundary, warning once.

    Args:
        dim: The band dimension to rank along.
        pct: Whether to return ranks as a fraction of the valid count.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager ranked cube otherwise.
    """
    from pyramids.lazy_support import _Rank

    if len(self._current_records()) != 1:
        self._warn_materialize("rank")
        return self.compute().rank(dim, pct=pct)
    return self._compose_mapblocks(_Rank(pct=pct), dim)

reduce(dim, how='mean', *, skipna=True, q=None) #

Reduce a band dimension, composing lazily when the cube is a single variable (#1237).

Collapses dim with how, keeping the result a lazy cube: the reduction is a dask.array step deferred until :meth:compute, so a chain of reductions stays lazy and reads the store only once per block. Mirrors :meth:NetCDF.reduce.

A single pinned variable composes lazily and returns a :class:LazyNetCDF. A multi-variable container cannot yet compose at the cube level, so it auto-materialises to the eager cube and reduces there, warning once — the v1 lazy/eager boundary (the same shape as the GDAL-op boundary). Pin a variable with get_variable(...) first to keep a container reduce lazy.

Parameters:

Name Type Description Default
dim str

The band dimension to collapse.

required
how str

The reduction ("mean", "sum", "max", a quantile, …), as :meth:NetCDF.reduce.

'mean'
skipna bool

Whether gaps are skipped.

True
q float | None

The quantile, for how="quantile".

None

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager reduced cube for a

Any

multi-variable container (materialised at the boundary).

Source code in src/pyramids_lazy/_lazy_cube.py
def reduce(
    self,
    dim: str,
    how: str = "mean",
    *,
    skipna: bool = True,
    q: float | None = None,
) -> Any:
    """Reduce a band dimension, composing lazily when the cube is a single variable (#1237).

    Collapses `dim` with `how`, keeping the result a lazy cube: the reduction is a `dask.array`
    step deferred until :meth:`compute`, so a chain of reductions stays lazy and reads the store
    only once per block. Mirrors :meth:`NetCDF.reduce`.

    A single pinned variable composes lazily and returns a :class:`LazyNetCDF`. A multi-variable
    container cannot yet compose at the cube level, so it auto-materialises to the eager cube and
    reduces there, warning once — the v1 lazy/eager boundary (the same shape as the GDAL-op
    boundary). Pin a variable with `get_variable(...)` first to keep a container reduce lazy.

    Args:
        dim: The band dimension to collapse.
        how: The reduction (`"mean"`, `"sum"`, `"max"`, a quantile, …), as :meth:`NetCDF.reduce`.
        skipna: Whether gaps are skipped.
        q: The quantile, for `how="quantile"`.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager reduced cube for a
        multi-variable container (materialised at the boundary).
    """
    if len(self._current_records()) != 1:
        # Container composition is not built yet: materialise at the boundary and reduce
        # eagerly, warning once, rather than silently reading the whole cube without notice.
        self._warn_materialize("reduce")
        return self.compute().reduce(dim, how, skipna=skipna, q=q)
    return self._compose_reduction(
        dim,
        how,
        skipna=skipna,
        q=q,
        group_positions=None,
        resize=None,
        window_mean_coords=False,
    )

rolling(dim, window, *, how='mean', center=False, min_periods=None, q=None) #

Moving-window reduction along a band dimension, composing lazily for a single variable.

The lazy twin of :meth:NetCDF.rolling — the window reduction stays a deferred dask.array step. A multi-variable container materialises at the boundary, warning once.

Parameters:

Name Type Description Default
dim str

The band dimension to roll along.

required
window int

The window length in steps.

required
how str

The reduction, as :meth:NetCDF.rolling.

'mean'
center bool

Whether the window is centred rather than trailing.

False
min_periods int | None

The minimum valid steps for a non-gap result; defaults to window.

None
q float | None

The quantile, for how="quantile".

None

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager rolled cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def rolling(
    self,
    dim: str,
    window: int,
    *,
    how: str = "mean",
    center: bool = False,
    min_periods: int | None = None,
    q: float | None = None,
) -> Any:
    """Moving-window reduction along a band dimension, composing lazily for a single variable.

    The lazy twin of :meth:`NetCDF.rolling` — the window reduction stays a deferred
    `dask.array` step. A multi-variable container materialises at the boundary, warning once.

    Args:
        dim: The band dimension to roll along.
        window: The window length in steps.
        how: The reduction, as :meth:`NetCDF.rolling`.
        center: Whether the window is centred rather than trailing.
        min_periods: The minimum valid steps for a non-gap result; defaults to `window`.
        q: The quantile, for `how="quantile"`.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager rolled cube otherwise.
    """
    from pyramids.lazy_support import (
        COUNTING_REDUCERS,
        REDUCERS,
        _check_how,
        _check_min_periods,
        _check_quantile,
        _check_window,
        _rolled_array,
    )

    _check_how(how, {*REDUCERS, *COUNTING_REDUCERS})
    q = _check_quantile(how, q)
    length = _check_window(window, caller="rolling")
    needed = _check_min_periods(min_periods, length)
    if len(self._current_records()) != 1:
        self._warn_materialize("rolling")
        return self.compute().rolling(
            dim, window, how=how, center=center, min_periods=min_periods, q=q
        )
    return self._compose_direct(
        _rolled_array,
        dim,
        window=length,
        center=bool(center),
        min_periods=needed,
        how=how,
        q=q,
    )

round(decimals=0) #

Round the values, composing lazily (#1237); a gap stays a gap.

Cell-wise and shape-preserving; runs the eager :meth:NetCDF.round per block via map_blocks. An integer band rounded to tens (decimals < 0) may widen based on the data, which is not consistent across blocks, so that case materialises at the boundary, as does a multi-variable container — both warn once.

Source code in src/pyramids_lazy/_lazy_cube.py
def round(self, decimals: int = 0) -> Any:
    """Round the values, composing lazily (#1237); a gap stays a gap.

    Cell-wise and shape-preserving; runs the eager :meth:`NetCDF.round` per block via
    map_blocks. An integer band rounded to tens (`decimals < 0`) may widen based on the data,
    which is not consistent across blocks, so that case materialises at the boundary, as does a
    multi-variable container — both warn once.
    """
    if isinstance(decimals, bool) or not isinstance(decimals, (int, np.integer)):
        raise TypeError(
            f"round() needs an integer number of decimals, got {decimals!r}."
        )
    records = self._current_records()
    integer_widen = (
        len(records) == 1
        and next(iter(records.values())).array.dtype.kind in "iu"
        and int(decimals) < 0
    )
    if len(records) != 1 or integer_widen:
        self._warn_materialize("round")
        return self.compute().round(decimals)
    return self._compose_cellwise("round", decimals)

sel(*, method=None, tolerance=None, **kwargs) #

Select bands by coordinate value along band dimensions, composing lazily (#1237).

The value twin of :meth:isel: each selector is resolved to positions against the source coordinates (the same _resolve_one_dim :meth:NetCDF.sel uses, honouring method="nearest" / tolerance), then the dask array is indexed — staying lazy. A multi-variable container selects eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def sel(
    self,
    *,
    method: str | None = None,
    tolerance: float | None = None,
    **kwargs: Any,
) -> Any:
    """Select bands by coordinate value along band dimensions, composing lazily (#1237).

    The value twin of :meth:`isel`: each selector is resolved to positions against the source
    coordinates (the same `_resolve_one_dim` :meth:`NetCDF.sel` uses, honouring
    `method="nearest"` / `tolerance`), then the dask array is indexed — staying lazy. A
    multi-variable container selects eagerly at the boundary, warning once.
    """
    from pyramids.lazy_support import _resolve_one_dim

    if not kwargs:
        raise ValueError(
            "sel() requires at least one keyword argument, e.g. sel(time=6)."
        )
    if method not in (None, "nearest"):
        raise ValueError(
            f"sel() method must be None (exact) or 'nearest', got {method!r}."
        )
    if tolerance is not None and method != "nearest":
        raise ValueError(
            "sel() tolerance= is only meaningful with method='nearest' — without it "
            "a label either matches exactly or does not match at all, and there is no "
            "distance for a tolerance to bound."
        )
    if len(self._current_records()) != 1:
        self._warn_materialize("sel")
        return self.compute().sel(method=method, tolerance=tolerance, **kwargs)
    resolved = [
        (
            dim_name,
            _resolve_one_dim(self._source, dim_name, selector, method, tolerance),
        )
        for dim_name, selector in kwargs.items()
    ]
    result: Any = self
    for dim_name, dim_indices in resolved:
        result = result._subset_band(dim_name, dim_indices)
    return result

shift(dim, periods=1, *, fill_value=None) #

Shift values along a band dimension, composing lazily (#1237).

The lazy twin of :meth:NetCDF.shift; the shift stays a deferred dask.array step, keeping the dimension's length. A multi-variable container shifts eagerly at the boundary, warning once.

Parameters:

Name Type Description Default
dim str

The band dimension to shift along.

required
periods int

How many steps to move (positive towards the end, negative the other way).

1
fill_value Any

The value for the vacated steps; the variable's no-data (or NaN) by default.

None

Returns:

Type Description
Any

LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager shifted cube otherwise.

Source code in src/pyramids_lazy/_lazy_cube.py
def shift(self, dim: str, periods: int = 1, *, fill_value: Any = None) -> Any:
    """Shift values along a band dimension, composing lazily (#1237).

    The lazy twin of :meth:`NetCDF.shift`; the shift stays a deferred `dask.array` step,
    keeping the dimension's length. A multi-variable container shifts eagerly at the boundary,
    warning once.

    Args:
        dim: The band dimension to shift along.
        periods: How many steps to move (positive towards the end, negative the other way).
        fill_value: The value for the vacated steps; the variable's no-data (or NaN) by default.

    Returns:
        LazyNetCDF | NetCDF: A lazy cube for a pinned variable; an eager shifted cube otherwise.
    """
    from pyramids.lazy_support import _check_fill_value, _check_periods, _Shift

    steps = _check_periods(periods)
    _check_fill_value(fill_value)
    if len(self._current_records()) != 1:
        self._warn_materialize("shift")
        return self.compute().shift(dim, periods, fill_value=fill_value)
    return self._compose_op(_Shift(periods=steps, fill_value=fill_value), dim)

squeeze(dim=None) #

Drop length-one band dimensions, composing lazily (#1237).

Pure band-axis reshape (no data touched), so it stays lazy: the dask array's size-1 band axes are squeezed and the band metadata narrowed to match :meth:NetCDF.squeeze. A multi-variable container squeezes eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def squeeze(self, dim: str | None = None) -> Any:
    """Drop length-one band dimensions, composing lazily (#1237).

    Pure band-axis reshape (no data touched), so it stays lazy: the dask array's size-1 band
    axes are squeezed and the band metadata narrowed to match :meth:`NetCDF.squeeze`. A
    multi-variable container squeezes eagerly at the boundary, warning once.
    """
    from pyramids.lazy_support import _assert_band_dimension

    if len(self._current_records()) != 1:
        self._warn_materialize("squeeze")
        return self.compute().squeeze(dim)
    name, rec = next(iter(self._current_records().items()))
    names = list(rec.band_names)
    sizes = list(rec.array.shape[: len(names)])
    if dim is not None:
        _assert_band_dimension(self._source, dim, caller="squeeze")
        length = sizes[names.index(dim)]
        if length != 1:
            raise ValueError(
                f"squeeze() drops a dimension of length one, and {dim!r} has "
                f"length {length}. Select one step first with isel({dim}=[0])."
            )
        gone = {dim}
    else:
        gone = {n for n, size in zip(names, sizes) if size == 1}
    if not gone:
        return self
    axes = tuple(i for i, n in enumerate(names) if n in gone)
    new_arr = rec.array.squeeze(axis=axes)
    new_names = [n for n in names if n not in gone]
    new_vmap = {n: rec.values_map.get(n) for n in new_names}
    new = rec._replace(
        array=new_arr,
        band_names=new_names,
        values_map=new_vmap,
        dim_names=(*new_names, *rec.dim_names[-2:]),
    )
    return LazyNetCDF(
        self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
    )

transpose(*dims) #

Reorder the band dimensions (spatial plane stays trailing), composing lazily (#1237).

Pure band-axis permutation, so it stays lazy: _transpose_order resolves and validates the order (as :meth:NetCDF.transpose), then the dask array's band axes are permuted. A multi-variable container transposes eagerly at the boundary, warning once.

Source code in src/pyramids_lazy/_lazy_cube.py
def transpose(self, *dims: Any) -> Any:
    """Reorder the band dimensions (spatial plane stays trailing), composing lazily (#1237).

    Pure band-axis permutation, so it stays lazy: `_transpose_order` resolves and validates the
    order (as :meth:`NetCDF.transpose`), then the dask array's band axes are permuted. A
    multi-variable container transposes eagerly at the boundary, warning once.
    """
    from pyramids.lazy_support import _transpose_order

    if len(self._current_records()) != 1:
        self._warn_materialize("transpose")
        return self.compute().transpose(*dims)
    name, rec = next(iter(self._current_records().items()))
    order = _transpose_order(list(rec.band_names), dims)
    perm = [rec.band_names.index(n) for n in order]
    ndim = rec.array.ndim
    new_arr = rec.array.transpose([*perm, ndim - 2, ndim - 1])
    new_vmap = {n: rec.values_map.get(n) for n in order}
    new = rec._replace(
        array=new_arr,
        band_names=list(order),
        values_map=new_vmap,
        dim_names=(*order, *rec.dim_names[-2:]),
    )
    return LazyNetCDF(
        self._source, {name: new_arr}, {name: new.dim_names}, {name: new}
    )

unify_chunks() #

Re-chunk every variable to a common chunking across their shared dimensions.

A no-op when the view holds fewer than two variables — that view is returned unchanged. Two variables chunked differently on a shared dimension come out sharing one chunking afterward. A composed array-native op is carried onto the result, as it is through persist().

Returns:

Name Type Description
LazyNetCDF LazyNetCDF

A lazy view whose variables agree on their shared-dimension chunking.

Source code in src/pyramids_lazy/_lazy_cube.py
def unify_chunks(self) -> LazyNetCDF:
    """Re-chunk every variable to a common chunking across their shared dimensions.

    A no-op when the view holds fewer than two variables — that view is returned unchanged. Two
    variables chunked differently on a shared dimension come out sharing one chunking afterward.
    A composed array-native op is carried onto the result, as it is through `persist()`.

    Returns:
        LazyNetCDF: A lazy view whose variables agree on their shared-dimension chunking.
    """
    import_dask(_DASK_MISSING)
    import dask.array as da

    if len(self._arrays) < 2:
        return self
    names = list(self._arrays)
    pairs: list[Any] = []
    for name in names:
        pairs.extend((self._arrays[name], self._dim_names[name]))
    _, unified = da.core.unify_chunks(*pairs)
    arrays = dict(zip(names, unified))
    return LazyNetCDF(self._source, arrays, self._dim_names, self._records)

pyramids_lazy._lazy_cube.build_lazy_cube(nc, chunks='auto') #

Build a lazy, dask-backed :class:LazyNetCDF view of nc (backs NetCDF.chunk).

Every gridded variable is read lazily through nc.read_array with chunks and wrapped in a :class:LazyNetCDF. Only a file-backed cube can be chunked -- the lazy read reopens the store per block -- so an in-memory / in-place-rebuilt cube is refused.

Parameters:

Name Type Description Default
nc NetCDF

The file-backed NetCDF container (or pinned single variable) to view lazily.

required
chunks Any

The dask chunk spec forwarded to nc.read_array for every variable; "auto" lets dask choose block sizes from the stored chunking.

'auto'

Returns:

Name Type Description
LazyNetCDF LazyNetCDF

A lazy view whose variables are dask arrays over nc.

Raises:

Type Description
ValueError

nc is in-memory (no on-disk store to reopen per block), so it cannot be chunked; write it with to_file() first or operate eagerly.

Source code in src/pyramids_lazy/_lazy_cube.py
def build_lazy_cube(nc: NetCDF, chunks: Any = "auto") -> LazyNetCDF:
    """Build a lazy, dask-backed :class:`LazyNetCDF` view of ``nc`` (backs ``NetCDF.chunk``).

    Every gridded variable is read lazily through ``nc.read_array`` with ``chunks`` and wrapped in a
    :class:`LazyNetCDF`. Only a file-backed cube can be chunked -- the lazy read reopens the store per
    block -- so an in-memory / in-place-rebuilt cube is refused.

    Args:
        nc: The file-backed NetCDF container (or pinned single variable) to view lazily.
        chunks: The dask chunk spec forwarded to ``nc.read_array`` for every variable; ``"auto"``
            lets dask choose block sizes from the stored chunking.

    Returns:
        LazyNetCDF: A lazy view whose variables are dask arrays over ``nc``.

    Raises:
        ValueError: ``nc`` is in-memory (no on-disk store to reopen per block), so it cannot be
            chunked; write it with ``to_file()`` first or operate eagerly.
    """
    if _is_in_memory(nc):
        raise ValueError(
            "chunk() needs a file-backed cube: the lazy read reopens the store per block, and "
            "this cube has no on-disk path. Write it with to_file() first, or operate eagerly."
        )
    pinned = nc._source_var_name
    names = [pinned] if pinned is not None else list(nc._spatial_variable_names())
    arrays: dict[str, Any] = {}
    dim_names: dict[str, tuple[str, ...]] = {}
    for name in names:
        var = nc if pinned is not None else nc._require_raster_variable(name)
        array = (
            nc.read_array(chunks=chunks, squeeze=False)
            if pinned is not None
            else nc.read_array(name, chunks=chunks, squeeze=False)
        )
        arrays[name] = array
        dim_names[name] = _lazy_cube_dim_names(var, array.ndim)
    return LazyNetCDF(nc, arrays, dim_names)