11# SPDX-License-Identifier: BSD-3-Clause
2- # Copyright 2025 MoDaCor Authors
3- #
4- # Redistribution and use in source and binary forms, with or without modification,
5- # are permitted provided that the following conditions are met:
6- # 1. Redistributions of source code must retain the above copyright notice, this
7- # list of conditions and the following disclaimer.
8- # 2. Redistributions in binary form must reproduce the above copyright notice,
9- # this list of conditions and the following disclaimer in the documentation
10- # and/or other materials provided with the distribution.
11- # 3. Neither the name of the copyright holder nor the names of its contributors
12- # may be used to endorse or promote products derived from this software without
13- # specific prior written permission.
14- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
15- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16- # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18- # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19- # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20- # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21- # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24-
25- __license__ = "BSD-3-Clause"
26- __copyright__ = "Copyright 2025 MoDaCor Authors"
27- __status__ = "Alpha"
2+ # /usr/bin/env python3
3+ # -*- coding: utf-8 -*-
4+
5+ from __future__ import annotations
6+
7+ __coding__ = "utf-8"
8+ __author__ = "Brian R. Pauw"
9+ __copyright__ = "Copyright 2025, The MoDaCor team"
10+ __date__ = "06/06/2025"
11+ __status__ = "Development" # "Development", "Production"
12+ # end of header and standard imports
13+
2814__all__ = ["IoSources" ]
2915
3016
31- from typing import Any
17+ from typing import Any , Optional
3218
3319import numpy as np
3420from attrs import define , field
3521
36- from modacor .io .io_source import IoSource
22+ from modacor .io .io_source import ArraySlice , IoSource
3723
3824
3925@define
@@ -47,21 +33,24 @@ class IoSources:
4733
4834 defined_sources : dict [str , IoSource ] = field (factory = dict )
4935
50- def register_source (self , source_reference : str , source : IoSource ):
36+ def register_source (self , source : IoSource , source_reference : str | None = None ):
5137 """
52- Register a new source class with the given name.
38+ Register a new source class with the given name. If no source_reference is provided, the
39+ source's own source_reference attribute will be used.
5340
5441 Parameters
5542 ----------
56- source_reference : str
57- The reference name of the source to register.
5843 source : IoSource
5944 The class of the source to register.
45+ source_reference : str
46+ The reference name of the source to register.
6047 """
61- if not isinstance (source_reference , str ):
62- raise TypeError ("source_name must be a string" )
6348 if not isinstance (source , IoSource ):
6449 raise TypeError ("source_class must be a subclass of IoSource" )
50+ if source_reference is None :
51+ source_reference = source .source_reference
52+ if not isinstance (source_reference , str ):
53+ raise TypeError ("source_name must be a string" )
6554 if source_reference in self .defined_sources :
6655 raise ValueError (f"Source { source_reference } already registered." )
6756 self .defined_sources [source_reference ] = source
@@ -109,7 +98,7 @@ def split_data_reference(self, data_reference: str) -> tuple[str, str]:
10998 )
11099 return _split [0 ], _split [1 ]
111100
112- def get_data (self , data_reference : str , index : int ) -> np .ndarray :
101+ def get_data (self , data_reference : str , load_slice : Optional [ ArraySlice ] = ... ) -> np .ndarray :
113102 """
114103 Get data from the specified source using the provided data key.
115104
@@ -120,8 +109,8 @@ def get_data(self, data_reference: str, index: int) -> np.ndarray:
120109 ----------
121110 data_reference : str
122111 The reference name of the source to access.
123- index : int
124- The index to access the data.
112+ load_slice : Optional[ArraySlice]
113+ A slice or tuple of slices to apply to the data. If None or ellipsis, the entire data is returned .
125114
126115 Returns
127116 -------
@@ -130,9 +119,9 @@ def get_data(self, data_reference: str, index: int) -> np.ndarray:
130119 """
131120 _source_ref , _data_key = self .split_data_reference (data_reference )
132121 _source = self .get_source (_source_ref )
133- return _source .get_data (index , _data_key )
122+ return _source .get_data (_data_key , load_slice = load_slice )
134123
135- def get_data_shape (self , data_reference : str , index : int ) -> np .ndarray :
124+ def get_data_shape (self , data_reference : str ) -> np .ndarray :
136125 """
137126 Get data from the specified source using the provided data key.
138127
@@ -143,8 +132,6 @@ def get_data_shape(self, data_reference: str, index: int) -> np.ndarray:
143132 ----------
144133 data_reference : str
145134 The reference name of the source to access.
146- index : int
147- The index to access the data.
148135
149136 Returns
150137 -------
@@ -153,9 +140,9 @@ def get_data_shape(self, data_reference: str, index: int) -> np.ndarray:
153140 """
154141 _source_ref , _data_key = self .split_data_reference (data_reference )
155142 _source = self .get_source (_source_ref )
156- return _source .get_data_shape (index , _data_key )
143+ return _source .get_data_shape (_data_key )
157144
158- def get_data_dtype (self , data_reference : str , index : int ) -> np .ndarray :
145+ def get_data_dtype (self , data_reference : str ) -> np .ndarray :
159146 """
160147 Get data from the specified source using the provided data key.
161148
@@ -166,8 +153,6 @@ def get_data_dtype(self, data_reference: str, index: int) -> np.ndarray:
166153 ----------
167154 data_reference : str
168155 The reference name of the source to access.
169- index : int
170- The index to access the data.
171156
172157 Returns
173158 -------
@@ -176,9 +161,9 @@ def get_data_dtype(self, data_reference: str, index: int) -> np.ndarray:
176161 """
177162 _source_ref , _data_key = self .split_data_reference (data_reference )
178163 _source = self .get_source (_source_ref )
179- return _source .get_data_dtype (index , _data_key )
164+ return _source .get_data_dtype (_data_key )
180165
181- def get_data_attributes (self , data_reference : str , index : int ) -> np .ndarray :
166+ def get_data_attributes (self , data_reference : str ) -> np .ndarray :
182167 """
183168 Get data from the specified source using the provided data key.
184169
@@ -199,7 +184,7 @@ def get_data_attributes(self, data_reference: str, index: int) -> np.ndarray:
199184 """
200185 _source_ref , _data_key = self .split_data_reference (data_reference )
201186 _source = self .get_source (_source_ref )
202- return _source .get_data_attributes (index , _data_key )
187+ return _source .get_data_attributes (_data_key )
203188
204189 def get_static_metadata (self , data_reference : str ) -> Any :
205190 """
0 commit comments