@@ -621,6 +621,116 @@ def test_select_non_dim_coord(self, cm_basic):
621621 out , _ = new_cm .select (new_dim = (1 , 20 ))
622622 assert new_cm == out
623623
624+ def test_select_non_dim_coord_shortens_coordinate (self , cm_basic ):
625+ """Test that selecting non-dimensional coords shortens only that coordinate."""
626+ # Add a non-dimensional coordinate with numeric values
627+ quality_scores = np .array ([0.1 , 0.5 , 0.9 , 0.2 , 0.8 , 0.3 , 0.7 ])
628+ new_cm = cm_basic .update (quality = (None , quality_scores ))
629+ # Select subset of quality scores using array indexing
630+ selected_indices = np .array ([1 , 3 , 5 ]) # Select indices 1, 3, 5
631+ out , _ = new_cm .select (quality = selected_indices , samples = True )
632+ # Quality coordinate should be shortened
633+ expected_quality = quality_scores [selected_indices ]
634+ assert np .array_equal (out .get_array ("quality" ), expected_quality )
635+ # Dimensional coordinates should be unchanged
636+ assert cm_basic .shape == out .shape
637+ assert np .array_equal (out .get_array ("time" ), new_cm .get_array ("time" ))
638+ assert np .array_equal (out .get_array ("distance" ), new_cm .get_array ("distance" ))
639+
640+ def test_select_non_dim_coord_with_boolean_mask (self , cm_basic ):
641+ """Test selecting non-dimensional coordinates using boolean arrays."""
642+ # Add a non-dimensional coordinate
643+ values = np .array ([10 , 20 , 30 , 40 , 50 , 60 , 70 ])
644+ new_cm = cm_basic .update (sensor_values = (None , values ))
645+ # Create boolean mask
646+ mask = values > 35 # Should select [40, 50, 60, 70]
647+ out , _ = new_cm .select (sensor_values = mask )
648+ # Only the non-dimensional coordinate should be affected
649+ expected_values = values [mask ]
650+ assert np .array_equal (out .get_array ("sensor_values" ), expected_values )
651+ # Dimensional coordinates should remain unchanged
652+ for coord in set (cm_basic .coord_map ) - {"sensor_values" }:
653+ assert cm_basic .get_coord (coord ) == new_cm .get_coord (coord )
654+
655+ def test_select_multi_dim_coord_raises (self , cm_multidim ):
656+ """
657+ Coords that are associated with more than one dim cannot be selected
658+ because it could ruin the squareness of the patch.
659+ """
660+ # Non-dim coord associated with one dimension should work.
661+ lat = cm_multidim .get_array ("latitude" )
662+ lat_mean = np .mean (lat )
663+ out , _ = cm_multidim .select (latitude = (..., lat_mean ))
664+ assert isinstance (out , dc .CoordManager )
665+ # Multi-dim coord should raise CoordError
666+ msg = "Only 1 dimensional coordinates"
667+ with pytest .raises (CoordError , match = msg ):
668+ cm_multidim .select (quality = (1 , 20 ))
669+
670+ def test_select_coord_tied_to_dimension_affects_others (self , cm_multidim ):
671+ """
672+ Test that selecting a coord tied to a dimension affects other coords
673+ on that dim.
674+ """
675+ # cm_multidim should have coordinates that share dimensions
676+ # Get a coordinate that's tied to a dimension and has other coords sharing
677+ # that dim
678+ lat = cm_multidim .get_array ("latitude" )
679+ lat_mean = np .mean (lat )
680+ out , _ = cm_multidim .select (latitude = (..., lat_mean ))
681+ # Check that the new lat is what we expect.
682+ new_lat = out .get_array ("latitude" )
683+ expected = lat [lat <= lat_mean ]
684+ assert np .array_equal (new_lat , expected )
685+ # And the other coord associated with that dimension have the same len.
686+ for name , coord in out .coord_map .items ():
687+ coord_dims = out .dim_map [name ]
688+ # Skip coords not tied to distance dimension
689+ if "distance" not in coord_dims :
690+ continue
691+ axis = coord_dims .index ("distance" )
692+ assert coord .shape [axis ] == len (new_lat )
693+
694+ def test_select_nonexistent_coordinate_ignores_gracefully (self , cm_basic ):
695+ """Test that selecting on a non-existent coordinate is ignored gracefully."""
696+ # This tests line 122 in _get_indexers_and_new_coords_dict
697+ original_shape = cm_basic .shape
698+ out , _ = cm_basic .select (nonexistent_coord = (1 , 10 ))
699+
700+ # Should return unchanged coordinate manager
701+ assert out == cm_basic
702+ assert out .shape == original_shape
703+
704+ # Should work with multiple nonexistent coordinates
705+ out2 , _ = cm_basic .select (
706+ fake_coord1 = (1 , 2 ), fake_coord2 = slice (0 , 5 ), another_fake = (10 , 20 )
707+ )
708+ assert out2 == cm_basic
709+ assert out2 .shape == original_shape
710+
711+ def test_select_mix_valid_invalid_coordinates (self , cm_basic ):
712+ """Test selecting with mix of valid and invalid coordinate names."""
713+ # This also exercises line 122 but with mixed scenarios
714+ time_vals = cm_basic .get_array ("time" )
715+ subset_time = (time_vals [1 ], time_vals [- 2 ])
716+
717+ out , _ = cm_basic .select (
718+ time = subset_time , # valid coordinate
719+ nonexistent = (1 , 10 ), # invalid coordinate - should be ignored
720+ fake_dim = slice (0 , 5 ), # another invalid coordinate
721+ )
722+
723+ # Only the valid coordinate selection should have been applied
724+ assert (
725+ out .shape [cm_basic .get_axis ("time" )]
726+ < cm_basic .shape [cm_basic .get_axis ("time" )]
727+ )
728+ # Distance should be unchanged since it wasn't selected
729+ assert (
730+ out .shape [cm_basic .get_axis ("distance" )]
731+ == cm_basic .shape [cm_basic .get_axis ("distance" )]
732+ )
733+
624734
625735class TestOrder :
626736 """Tests for ordering coordinate managers."""
0 commit comments