@@ -360,8 +360,6 @@ def get_time_sliced_dataset(self, var: str, time_slice: TimeSlice) -> xr.Dataset
360360
361361 These variables can either be from the test data or reference data.
362362
363- TODO: Add unit test for this method.
364-
365363 Parameters
366364 ----------
367365 var : str
@@ -381,34 +379,31 @@ def get_time_sliced_dataset(self, var: str, time_slice: TimeSlice) -> xr.Dataset
381379 if not isinstance (self .var , str ) or self .var == "" :
382380 raise ValueError ("The `var` argument is not a valid string." )
383381
384- ds = self ._get_full_dataset (var )
385- ds = self ._apply_time_slice_to_dataset (ds , time_slice )
382+ filepath = self ._get_filepath_with_params ()
386383
387- try :
388- filepath = self ._get_filepath_with_params ()
384+ if filepath is None :
385+ raise RuntimeError (
386+ f"Unable to get file path for { self .data_type } dataset. "
387+ f"For time slicing, please ensure that "
388+ f"{ 'ref_file' if self .data_type == 'ref' else 'test_file' } parameter is set."
389+ )
389390
390- if filepath :
391- self . parameter . _add_filepath_attr ( self . data_type , filepath )
391+ if not os . path . exists ( filepath ) :
392+ raise RuntimeError ( f"File not found: { filepath } " )
392393
393- except Exception as e :
394- logger .warning (f"Failed to store absolute file path: { e } " )
394+ self .parameter ._add_filepath_attr (self .data_type , filepath )
395+
396+ ds = self ._get_full_dataset ()
397+ ds = self ._apply_time_slice_to_dataset (ds , time_slice )
395398
396399 return ds
397400
398- def _get_full_dataset (self , var : str ) -> xr .Dataset :
401+ def _get_full_dataset (self ) -> xr .Dataset :
399402 """Get the full dataset without any time averaging for time slicing.
400403
401404 This function uses the dataset's file path parameters to directly open
402405 the raw data file for time slicing operations.
403406
404- TODO: Add unit test for this method.
405- FIXME: The `var` parameter is currently unused.
406-
407- Parameters
408- ----------
409- var : str
410- The key of the variable.
411-
412407 Returns
413408 -------
414409 xr.Dataset
@@ -419,31 +414,20 @@ def _get_full_dataset(self, var: str) -> xr.Dataset:
419414 RuntimeError
420415 If unable to get the full dataset or file not found.
421416 """
422- filepath = self ._get_filepath_with_params ()
423-
424- if filepath is None :
425- raise RuntimeError (
426- f"Unable to get file path for { self .data_type } dataset. "
427- f"For time slicing, please ensure that "
428- f"{ 'ref_file' if self .data_type == 'ref' else 'test_file' } parameter is set."
429- )
430-
431- if not os .path .exists (filepath ):
432- raise RuntimeError (f"File not found: { filepath } " )
417+ filepath = getattr (self .parameter , f"{ self .data_type } _data_file_path" )
433418
434419 logger .info (f"Opening full dataset from: { filepath } " )
435420
436421 try :
437- ds = xr .open_dataset (filepath , decode_times = True )
422+ ds = xc .open_dataset (filepath , add_bounds = [ "X" , "Y" , "T" ] )
438423
439424 logger .info (
440425 f"Successfully opened dataset with time dimension size: { ds .sizes .get ('time' , 'N/A' )} "
441426 )
442-
443- return ds
444- # FIXME: This except block is too broad; it should catch specific exceptions.
445- except Exception as e :
427+ except (FileNotFoundError , OSError , ValueError ) as e :
446428 raise RuntimeError (f"Failed to open dataset { filepath } : { e } " ) from e
429+ else :
430+ return ds
447431
448432 def _get_filepath_with_params (self ) -> str | None :
449433 """Get the filepath using parameters.
@@ -483,14 +467,10 @@ def _apply_time_slice_to_dataset(
483467 xr.Dataset
484468 The dataset with time slice applied.
485469 """
486- # FIXME: Use xcdat.get_dim_keys to find time dimension.
487- time_dim = None
488-
489- for dim in ds .dims :
490- if str (dim ).lower () in ["time" , "t" ]:
491- time_dim = dim
492-
493- break
470+ try :
471+ time_dim = xc .get_dim_keys (ds , axis = "T" )
472+ except (ValueError , KeyError ):
473+ time_dim = None
494474
495475 if time_dim is None :
496476 logger .warning (
@@ -499,9 +479,15 @@ def _apply_time_slice_to_dataset(
499479
500480 return ds
501481
502- # Single index selection
503482 index = int (time_slice )
504- ds_sliced = ds .isel ({time_dim : index })
483+
484+ try :
485+ ds_sliced = ds .isel ({time_dim : index })
486+ except IndexError as e :
487+ raise IndexError (
488+ f"Time slice index { index } is out of bounds for time dimension "
489+ f"of size { ds .sizes [time_dim ]} ."
490+ ) from e
505491
506492 logger .info (
507493 f"Applied time slice '{ time_slice } ' to dataset. "
@@ -562,7 +548,7 @@ def get_climo_dataset(self, var: str, season: ClimoFreq) -> xr.Dataset:
562548 ds_climo = climo (ds , self .var , season ).to_dataset ()
563549 ds_climo = ds_climo .bounds .add_missing_bounds (axes = ["X" , "Y" ])
564550
565- self .parameter ._add_time_series_file_path_attr (self .data_type , ds )
551+ self .parameter ._add_time_series_filepath_attr (self .data_type , ds )
566552
567553 return ds_climo
568554
0 commit comments