Skip to content

Commit 3397b14

Browse files
committed
Start of HDF5 Loader
1 parent 63d2ff2 commit 3397b14

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/modacor/io/hdf/hdf_loader.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 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"
28+
29+
30+
from ..io_source import IoSource
31+
from ..io_sources import IoSources
32+
from modacor.dataclasses.messagehandler import *
33+
from logging import WARNING
34+
import numpy as np
35+
import h5py
36+
37+
38+
class HDFLoader(IoSources):
39+
def __init__(self, source_reference: str, source: IoSource, logging_level = WARNING):
40+
super().__init__(source_reference, source)
41+
self.hdf_logger = MessageHandler('hdf5logger', logging_level)
42+
43+
44+
def _open_file(self, file_path = None):
45+
if file_path is None:
46+
error = 'No filepath given'
47+
self.hdf_logger.log.error(error)
48+
raise OSError(error)
49+
50+
try:
51+
self.file_reference = h5py.File(file_path, 'r')
52+
except OSError as error:
53+
self.hdf_logger.log.error(error)
54+
raise OSError(error)
55+
56+
57+
def _close_file(self):
58+
try:
59+
self.file_reference.close()
60+
except OSError as error:
61+
self.hdf_logger.log.error(error)
62+
raise OSError(error)

0 commit comments

Comments
 (0)