Skip to content

Commit

Permalink
Clsing hdf file at any situation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Nov 20, 2023
1 parent 4c7fc16 commit babbb1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
14 changes: 7 additions & 7 deletions pynxtools/dataconverter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,13 @@ def add_units_key(dataset, path):

def write(self):
"""Writes the NeXus file with previously validated data from the reader with NXDL attrs."""

if self.write_in_memory:
raise ValueError(f"To write in memory and get the file obhect please use "
f"the method get_in_memory_obj()")

self._process_data_into_hdf5()
self.output_nexus.close()
try:
if self.write_in_memory:
raise ValueError(f"To write in memory and get the file obhect please use "
f"the method get_in_memory_obj()")
self._process_data_into_hdf5()
finally:
self.output_nexus.close()

def get_in_memory_obj(self):
"""Write the nexus file as in-memory obj.
Expand Down
38 changes: 20 additions & 18 deletions pynxtools/nexus/nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,24 +642,26 @@ def full_visit(self, root, hdf_node, name, func):
def process_nexus_master_file(self, parser):
"""Process a nexus master file by processing all its nodes and their attributes"""
self.parser = parser
if not self.is_hdf5_file_obj:
self.in_file = h5py.File(
self.input_file_name[0]
if isinstance(self.input_file_name, list)
else self.input_file_name, 'r'
)
else:
self.in_file = self.input_file_name
self.full_visit(self.in_file, self.in_file, '', self.visit_node)
if self.d_inq_nd is None and self.c_inq_nd is None:
get_default_plotable(self.in_file, self.logger)
# To log the provided concept and concepts founded
if self.c_inq_nd is not None:
for hdf_path in self.hdf_path_list_for_c_inq_nd:
self.logger.info(hdf_path)
# To test if hdf_file is open print(self.in_file.id.valid)
self.in_file.close()
# To test if hdf_file is open print(self.in_file.id.valid)
try:
if not self.is_hdf5_file_obj:
self.in_file = h5py.File(
self.input_file_name[0]
if isinstance(self.input_file_name, list)
else self.input_file_name, 'r'
)
else:
self.in_file = self.input_file_name
self.full_visit(self.in_file, self.in_file, '', self.visit_node)
if self.d_inq_nd is None and self.c_inq_nd is None:
get_default_plotable(self.in_file, self.logger)
# To log the provided concept and concepts founded
if self.c_inq_nd is not None:
for hdf_path in self.hdf_path_list_for_c_inq_nd:
self.logger.info(hdf_path)
finally:
# To test if hdf_file is open print(self.in_file.id.valid)
self.in_file.close()
# To test if hdf_file is open print(self.in_file.id.valid)


@click.command()
Expand Down

0 comments on commit babbb1d

Please sign in to comment.