@@ -393,7 +393,7 @@ def combine_nested(
393
393
--------
394
394
395
395
A common task is collecting data from a parallelized simulation in which
396
- each processor wrote out to a separate file. A domain which was decomposed
396
+ each process wrote out to a separate file. A domain which was decomposed
397
397
into 4 parts, 2 each along both the x and y axes, requires organising the
398
398
datasets into a doubly-nested list, e.g:
399
399
@@ -505,8 +505,7 @@ def combine_by_coords(
505
505
----------
506
506
datasets : sequence of xarray.Dataset
507
507
Dataset objects to combine.
508
- compat : {'identical', 'equals', 'broadcast_equals',
509
- 'no_conflicts', 'override'}, optional
508
+ compat : {'identical', 'equals', 'broadcast_equals', 'no_conflicts', 'override'}, optional
510
509
String indicating how to compare variables of the same name for
511
510
potential conflicts:
512
511
@@ -520,9 +519,21 @@ def combine_by_coords(
520
519
of all non-null values.
521
520
- 'override': skip comparing and pick variable from first dataset
522
521
data_vars : {'minimal', 'different', 'all' or list of str}, optional
523
- Details are in the documentation of concat
522
+ These data variables will be concatenated together:
523
+
524
+ * 'minimal': Only data variables in which the dimension already
525
+ appears are included.
526
+ * 'different': Data variables which are not equal (ignoring
527
+ attributes) across all datasets are also concatenated (as well as
528
+ all for which dimension already appears). Beware: this option may
529
+ load the data payload of data variables into memory if they are not
530
+ already loaded.
531
+ * 'all': All data variables will be concatenated.
532
+ * list of str: The listed data variables will be concatenated, in
533
+ addition to the 'minimal' data variables.
534
+ If objects are DataArrays, `data_vars` must be 'all'.
524
535
coords : {'minimal', 'different', 'all' or list of str}, optional
525
- Details are in the documentation of concat
536
+ As per the 'data_vars' kwarg, but for coordinate variables.
526
537
fill_value : scalar, optional
527
538
Value to use for newly missing values
528
539
join : {'outer', 'inner', 'left', 'right', 'exact'}, optional
@@ -556,29 +567,91 @@ def combine_by_coords(
556
567
they are concatenated based on the values in their dimension coordinates,
557
568
not on their position in the list passed to `combine_by_coords`.
558
569
570
+ >>> import numpy as np
571
+ >>> import xarray as xr
572
+
573
+ >>> x1 = xr.Dataset(
574
+ ... {
575
+ ... "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
576
+ ... "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
577
+ ... },
578
+ ... coords={"y": [0, 1], "x": [10, 20, 30]},
579
+ ... )
580
+ >>> x2 = xr.Dataset(
581
+ ... {
582
+ ... "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
583
+ ... "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
584
+ ... },
585
+ ... coords={"y": [2, 3], "x": [10, 20, 30]},
586
+ ... )
587
+ >>> x3 = xr.Dataset(
588
+ ... {
589
+ ... "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
590
+ ... "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
591
+ ... },
592
+ ... coords={"y": [2, 3], "x": [40, 50, 60]},
593
+ ... )
594
+
559
595
>>> x1
560
596
<xarray.Dataset>
561
- Dimensions: (x: 3)
562
- Coords:
563
- * position (x) int64 0 1 2
597
+ Dimensions: (x: 3, y: 2)
598
+ Coordinates:
599
+ * y (y) int64 0 1
600
+ * x (x) int64 10 20 30
564
601
Data variables:
565
- temperature (x) float64 11.04 23.57 20.77 ...
602
+ temperature (y, x) float64 1.654 10.63 7.015 2.543 13.93 9.436
603
+ precipitation (y, x) float64 0.2136 0.9974 0.7603 0.4679 0.3115 0.945
566
604
567
605
>>> x2
568
606
<xarray.Dataset>
569
- Dimensions: (x: 3)
570
- Coords:
571
- * position (x) int64 3 4 5
607
+ Dimensions: (x: 3, y: 2)
608
+ Coordinates:
609
+ * y (y) int64 2 3
610
+ * x (x) int64 10 20 30
611
+ Data variables:
612
+ temperature (y, x) float64 9.341 0.1251 6.269 7.709 8.82 2.316
613
+ precipitation (y, x) float64 0.1728 0.1178 0.03018 0.6509 0.06938 0.3792
614
+
615
+ >>> x3
616
+ <xarray.Dataset>
617
+ Dimensions: (x: 3, y: 2)
618
+ Coordinates:
619
+ * y (y) int64 2 3
620
+ * x (x) int64 40 50 60
572
621
Data variables:
573
- temperature (x) float64 6.97 8.13 7.42 ...
622
+ temperature (y, x) float64 2.789 2.446 6.551 12.46 2.22 15.96
623
+ precipitation (y, x) float64 0.4804 0.1902 0.2457 0.6125 0.4654 0.5953
574
624
575
- >>> combined = xr.combine_by_coords([x2, x1])
625
+ >>> xr.combine_by_coords([x2, x1])
576
626
<xarray.Dataset>
577
- Dimensions: (x: 6)
578
- Coords:
579
- * position (x) int64 0 1 2 3 4 5
627
+ Dimensions: (x: 3, y: 4)
628
+ Coordinates:
629
+ * x (x) int64 10 20 30
630
+ * y (y) int64 0 1 2 3
580
631
Data variables:
581
- temperature (x) float64 11.04 23.57 20.77 ...
632
+ temperature (y, x) float64 1.654 10.63 7.015 2.543 ... 7.709 8.82 2.316
633
+ precipitation (y, x) float64 0.2136 0.9974 0.7603 ... 0.6509 0.06938 0.3792
634
+
635
+ >>> xr.combine_by_coords([x3, x1])
636
+ <xarray.Dataset>
637
+ Dimensions: (x: 6, y: 4)
638
+ Coordinates:
639
+ * x (x) int64 10 20 30 40 50 60
640
+ * y (y) int64 0 1 2 3
641
+ Data variables:
642
+ temperature (y, x) float64 1.654 10.63 7.015 nan ... nan 12.46 2.22 15.96
643
+ precipitation (y, x) float64 0.2136 0.9974 0.7603 ... 0.6125 0.4654 0.5953
644
+
645
+ >>> xr.combine_by_coords([x3, x1], join='override')
646
+ <xarray.Dataset>
647
+ Dimensions: (x: 3, y: 4)
648
+ Coordinates:
649
+ * x (x) int64 10 20 30
650
+ * y (y) int64 0 1 2 3
651
+ Data variables:
652
+ temperature (y, x) float64 1.654 10.63 7.015 2.543 ... 12.46 2.22 15.96
653
+ precipitation (y, x) float64 0.2136 0.9974 0.7603 ... 0.6125 0.4654 0.5953
654
+
582
655
"""
583
656
584
657
# Group by data vars
0 commit comments