@@ -2916,7 +2916,7 @@ def test_open_dataset_returns_dataset(
29162916 time_buffer = pd .Timedelta (0 ),
29172917 )
29182918
2919- ds = p .open_dataset (p [ 0 ], open_dataset_kwargs = {"engine" : "netcdf4" })
2919+ ds = p .open_dataset (0 , open_method = {"open_kwargs" : { " engine" : "netcdf4" }}, silent = True )
29202920 assert isinstance (ds , xr .Dataset )
29212921 assert "sst" in ds
29222922 ds .close ()
@@ -2954,7 +2954,7 @@ def test_open_mfdataset_returns_dataset(
29542954 fake_ds = xr .Dataset ({"sst" : (["lat" , "lon" ], [[1.0 ]])}, coords = {"lat" : [0.0 ], "lon" : [0.0 ]})
29552955 with patch ("xarray.open_mfdataset" , return_value = fake_ds ) as mock_mfdataset :
29562956 # Pass a list of results directly (backward-compatible path)
2957- ds = p .open_mfdataset (fake_results , open_dataset_kwargs = {"engine" : "netcdf4" })
2957+ ds = p .open_mfdataset (fake_results , open_method = {"open_kwargs" : { " engine" : "netcdf4" }}, silent = True )
29582958
29592959 assert ds is fake_ds
29602960 mock_ea .open .assert_called_once_with (fake_results , pqdm_kwargs = {"disable" : True })
@@ -3016,7 +3016,7 @@ def test_open_mfdataset_accepts_subset_plan(
30163016
30173017 fake_ds = xr .Dataset ({"sst" : (["lat" , "lon" ], [[1.0 ]])}, coords = {"lat" : [0.0 ], "lon" : [0.0 ]})
30183018 with patch ("xarray.open_mfdataset" , return_value = fake_ds ) as mock_mfdataset :
3019- ds = p .open_mfdataset (subset , open_dataset_kwargs = {"engine" : "netcdf4" })
3019+ ds = p .open_mfdataset (subset , open_method = {"open_kwargs" : { " engine" : "netcdf4" }}, silent = True )
30203020
30213021 assert ds is fake_ds
30223022 mock_ea .open .assert_called_once_with (fake_results , pqdm_kwargs = {"disable" : True })
@@ -3047,7 +3047,7 @@ def test_open_dataset_default_uses_auto(
30473047 time_buffer = pd .Timedelta (0 ),
30483048 )
30493049
3050- ds = p .open_dataset (p [ 0 ] , open_method = " dataset" , open_dataset_kwargs = {"engine" : "netcdf4" })
3050+ ds = p .open_dataset (0 , open_method = { "xarray_open" : " dataset" , "open_kwargs" : {"engine" : "netcdf4" }}, silent = True )
30513051 assert isinstance (ds , xr .Dataset )
30523052 assert "sst" in ds
30533053 ds .close ()
@@ -3073,7 +3073,7 @@ def test_open_dataset_invalid_open_method_raises(
30733073 )
30743074
30753075 with pytest .raises (ValueError , match = "open_method" ):
3076- p .open_dataset (p [ 0 ] , open_method = "bad" )
3076+ p .open_dataset (0 , open_method = "bad" , silent = True )
30773077
30783078 def test_open_mfdataset_with_open_method_dataset_uses_open_mfdataset (
30793079 self , tmp_path : pathlib .Path , monkeypatch : pytest .MonkeyPatch
@@ -3106,7 +3106,7 @@ def test_open_mfdataset_with_open_method_dataset_uses_open_mfdataset(
31063106
31073107 fake_ds = xr .Dataset ({"sst" : (["lat" , "lon" ], [[1.0 ]])}, coords = {"lat" : [0.0 ], "lon" : [0.0 ]})
31083108 with patch ("xarray.open_mfdataset" , return_value = fake_ds ) as mock_mfd :
3109- ds = p .open_mfdataset (fake_results , open_method = " dataset" , open_dataset_kwargs = {"engine" : "netcdf4" })
3109+ ds = p .open_mfdataset (fake_results , open_method = { "xarray_open" : " dataset" , "open_kwargs" : {"engine" : "netcdf4" }}, silent = True )
31103110
31113111 assert ds is fake_ds
31123112 mock_mfd .assert_called_once_with ([nc_a , nc_b ], chunks = {}, engine = "netcdf4" , decode_timedelta = False )
@@ -3139,14 +3139,123 @@ def test_open_mfdataset_datatree_merge_concatenates(
31393139
31403140 ds = p .open_mfdataset (
31413141 fake_results ,
3142- open_method = " datatree- merge" ,
3143- open_dataset_kwargs = { "engine" : "netcdf4" } ,
3142+ open_method = { "xarray_open" : " datatree" , " merge": "all" , "open_kwargs" : { "engine" : "netcdf4" }} ,
3143+ silent = True ,
31443144 )
31453145 # Result is a Dataset with a "granule" dimension from concatenation.
31463146 assert isinstance (ds , xr .Dataset )
31473147 assert ds .sizes ["granule" ] == 2
31483148 assert "sst" in ds
31493149
3150+ def test_open_dataset_prints_effective_spec_when_not_silent (
3151+ self , tmp_path : pathlib .Path , monkeypatch : pytest .MonkeyPatch , capsys : pytest .CaptureFixture
3152+ ) -> None :
3153+ """open_dataset prints the effective open_method spec when silent=False."""
3154+ nc_path = str (tmp_path / "test.nc" )
3155+ _make_l3_dataset ([- 90.0 , 0.0 , 90.0 ], [- 180.0 , 0.0 , 180.0 ]).to_netcdf (
3156+ nc_path , engine = "netcdf4"
3157+ )
3158+ mock_ea = MagicMock ()
3159+ mock_ea .open .return_value = [nc_path ]
3160+ monkeypatch .setitem (__import__ ("sys" ).modules , "earthaccess" , mock_ea )
3161+
3162+ pts = pd .DataFrame (
3163+ {"lat" : [0.0 ], "lon" : [0.0 ], "time" : pd .to_datetime (["2023-06-01" ])}
3164+ )
3165+ p = Plan (
3166+ points = pts ,
3167+ results = [object ()],
3168+ granules = [],
3169+ point_granule_map = {0 : []},
3170+ source_kwargs = {"short_name" : "TEST" },
3171+ time_buffer = pd .Timedelta (0 ),
3172+ )
3173+
3174+ ds = p .open_dataset (0 , open_method = {"open_kwargs" : {"engine" : "netcdf4" }}, silent = False )
3175+ ds .close ()
3176+ captured = capsys .readouterr ()
3177+ assert "open_method:" in captured .out
3178+ # The effective spec includes the resolved defaults (chunks, decode_timedelta)
3179+ assert "engine" in captured .out
3180+
3181+ def test_open_dataset_silent_suppresses_output (
3182+ self , tmp_path : pathlib .Path , monkeypatch : pytest .MonkeyPatch , capsys : pytest .CaptureFixture
3183+ ) -> None :
3184+ """open_dataset prints nothing when silent=True."""
3185+ nc_path = str (tmp_path / "test.nc" )
3186+ _make_l3_dataset ([- 90.0 , 0.0 , 90.0 ], [- 180.0 , 0.0 , 180.0 ]).to_netcdf (
3187+ nc_path , engine = "netcdf4"
3188+ )
3189+ mock_ea = MagicMock ()
3190+ mock_ea .open .return_value = [nc_path ]
3191+ monkeypatch .setitem (__import__ ("sys" ).modules , "earthaccess" , mock_ea )
3192+
3193+ pts = pd .DataFrame (
3194+ {"lat" : [0.0 ], "lon" : [0.0 ], "time" : pd .to_datetime (["2023-06-01" ])}
3195+ )
3196+ p = Plan (
3197+ points = pts ,
3198+ results = [object ()],
3199+ granules = [],
3200+ point_granule_map = {0 : []},
3201+ source_kwargs = {"short_name" : "TEST" },
3202+ time_buffer = pd .Timedelta (0 ),
3203+ )
3204+
3205+ ds = p .open_dataset (0 , open_method = {"open_kwargs" : {"engine" : "netcdf4" }}, silent = True )
3206+ ds .close ()
3207+ captured = capsys .readouterr ()
3208+ assert captured .out == ""
3209+
3210+ def test_open_dataset_integer_index (
3211+ self , tmp_path : pathlib .Path , monkeypatch : pytest .MonkeyPatch
3212+ ) -> None :
3213+ """open_dataset(0) resolves the integer to plan.results[0]."""
3214+ nc_path = str (tmp_path / "test.nc" )
3215+ _make_l3_dataset ([- 90.0 , 0.0 , 90.0 ], [- 180.0 , 0.0 , 180.0 ]).to_netcdf (
3216+ nc_path , engine = "netcdf4"
3217+ )
3218+ mock_ea = MagicMock ()
3219+ mock_ea .open .return_value = [nc_path ]
3220+ monkeypatch .setitem (__import__ ("sys" ).modules , "earthaccess" , mock_ea )
3221+
3222+ fake_result = object ()
3223+ pts = pd .DataFrame (
3224+ {"lat" : [0.0 ], "lon" : [0.0 ], "time" : pd .to_datetime (["2023-06-01" ])}
3225+ )
3226+ p = Plan (
3227+ points = pts ,
3228+ results = [fake_result ],
3229+ granules = [],
3230+ point_granule_map = {0 : []},
3231+ source_kwargs = {"short_name" : "TEST" },
3232+ time_buffer = pd .Timedelta (0 ),
3233+ )
3234+
3235+ # Using integer index 0 should behave identically to plan[0]
3236+ ds = p .open_dataset (0 , open_method = {"open_kwargs" : {"engine" : "netcdf4" }}, silent = True )
3237+ assert isinstance (ds , xr .Dataset )
3238+ assert "sst" in ds
3239+ ds .close ()
3240+ mock_ea .open .assert_called_once_with ([fake_result ], pqdm_kwargs = {"disable" : True })
3241+
3242+ def test_open_dataset_integer_index_out_of_range_raises (self ) -> None :
3243+ """open_dataset raises IndexError for an out-of-range integer index."""
3244+ pts = pd .DataFrame (
3245+ {"lat" : [0.0 ], "lon" : [0.0 ], "time" : pd .to_datetime (["2023-06-01" ])}
3246+ )
3247+ p = Plan (
3248+ points = pts ,
3249+ results = [object ()],
3250+ granules = [],
3251+ point_granule_map = {0 : []},
3252+ source_kwargs = {"short_name" : "TEST" },
3253+ time_buffer = pd .Timedelta (0 ),
3254+ )
3255+
3256+ with pytest .raises (IndexError , match = "out of range" ):
3257+ p .open_dataset (5 , silent = True )
3258+
31503259
31513260# ---------------------------------------------------------------------------
31523261# Helper: create a grouped NetCDF4 file (HDF5 with subgroups)
@@ -3426,7 +3535,7 @@ def test_show_variables_prints_dims_and_vars(
34263535 time_buffer = pd .Timedelta (0 ),
34273536 )
34283537
3429- p .show_variables (open_dataset_kwargs = {"engine" : "netcdf4" })
3538+ p .show_variables (open_method = {"open_kwargs" : { " engine" : "netcdf4" } })
34303539 captured = capsys .readouterr ()
34313540 assert "Dimensions" in captured .out
34323541 assert "Variables" in captured .out
@@ -4575,7 +4684,7 @@ def test_show_variables_dataset_layout_prints_dims_and_vars(
45754684 time_buffer = pd .Timedelta (0 ),
45764685 )
45774686
4578- p .show_variables (open_dataset_kwargs = {"engine" : "netcdf4" })
4687+ p .show_variables (open_method = {"open_kwargs" : { " engine" : "netcdf4" } })
45794688 captured = capsys .readouterr ()
45804689 assert "Dimensions" in captured .out
45814690 assert "Variables" in captured .out
@@ -4613,7 +4722,7 @@ def test_show_variables_geo_detection_none_warns(
46134722 time_buffer = pd .Timedelta (0 ),
46144723 )
46154724
4616- p .show_variables (open_dataset_kwargs = {"engine" : "netcdf4" })
4725+ p .show_variables (open_method = {"open_kwargs" : { " engine" : "netcdf4" } })
46174726 captured = capsys .readouterr ()
46184727 assert "NONE" in captured .out or "no geolocation" in captured .out .lower ()
46194728
0 commit comments