@@ -2693,3 +2693,29 @@ def test_gridded_file_pfb():
26932693 hf .get_gridded_file (file_path , options )
26942694 data = hf .gridded .read_fast_pfb (file_path )
26952695 assert data .shape == (1 , 144 , 19 , 48 )
2696+
2697+
2698+ def test_create_da_indexer_hourly_multiday_time_index ():
2699+ """
2700+ Unit test that _create_da_indexer uses total_seconds() (not .seconds) when
2701+ computing the hourly time index from a netcdf file's time dimension.
2702+
2703+ timedelta.seconds wraps at 86400 (one day), so a start_time that is more
2704+ than 24 hours past the file's first timestep would produce a wrong index.
2705+ For example, 54 hours past the start: .seconds gives 6*3600 → index 6,
2706+ total_seconds() gives 54*3600 → index 54 (correct).
2707+ """
2708+ dimension_start = datetime .datetime (2005 , 10 , 1 , 0 , 0 , 0 )
2709+ times = [dimension_start + datetime .timedelta (hours = h ) for h in range (100 )]
2710+ data = np .zeros ((100 ,))
2711+ data_da = xr .DataArray (data , dims = ["time" ], coords = {"time" : times }, name = "var" )
2712+ data_ds = data_da .to_dataset ()
2713+
2714+ entry = {"temporal_resolution" : "hourly" , "grid" : None }
2715+ # start_time is 54 hours (2 days + 6 hours) after the dimension start.
2716+ # .seconds would return 6*3600, giving index 6. total_seconds() gives 54.
2717+ options = {"start_time" : "2005-10-03 06:00:00" }
2718+
2719+ da_indexers = gr ._create_da_indexer (options , entry , data_ds , data_da , "synthetic.nc" )
2720+
2721+ assert da_indexers ["time" ] == 54
0 commit comments