22from sqlite3 import NotSupportedError
33import traceback
44from typing import List , Optional
5+ from energyml .utils .data .datasets_io import get_handler_registry
56import numpy as np
67from energyml .utils .data .helper import _ARRAY_NAMES_ , read_array
78from energyml .utils .data .mesh import (
@@ -412,16 +413,65 @@ def read_wellbore_frame_repr_demo_jfr_02_26(
412413 return meshes
413414
414415
416+ def test_read_write_array (h5_path ):
417+
418+ handler_registry = get_handler_registry ()
419+
420+ h5_handler = handler_registry .get_handler_for_file (h5_path )
421+ if h5_handler is None :
422+ print (f"No handler found for file { h5_path } " )
423+ return
424+ h5_handler .write_array (
425+ array = np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]]),
426+ target = h5_path ,
427+ path_in_external_file = "/test_array" ,
428+ )
429+
430+ h5_handler .file_cache .close_all ()
431+
432+ print (
433+ h5_handler .read_array (
434+ source = h5_path ,
435+ path_in_external_file = "/test_array" ,
436+ )
437+ )
438+
439+ success = h5_handler .write_array (
440+ array = np .array ([[7 , 8 , 9 ], [10 , 11 , 12 ]]),
441+ target = h5_path ,
442+ path_in_external_file = "/test_array2" ,
443+ )
444+ print (f"Write success: { success } " )
445+
446+ cached = h5_handler .file_cache .get_or_open (h5_path , h5_handler , "a" )
447+ # print if file is still opened :
448+ print (f"File still opened after write: { cached } is open: { hasattr (cached , 'id' ) and cached .id .valid } " )
449+
450+ success = h5_handler .write_array (
451+ array = np .array ([[13 , 14 , 15 ], [16 , 17 , 18 ]]),
452+ target = h5_path ,
453+ path_in_external_file = "/test_array3" ,
454+ )
455+ print (f"Write success: { success } " )
456+
457+ print (
458+ h5_handler .read_array (
459+ source = h5_path ,
460+ path_in_external_file = "/test_array2" ,
461+ )
462+ )
463+
464+
415465if __name__ == "__main__" :
416466 logging .basicConfig (level = logging .DEBUG )
417-
467+ meshes = []
418468 # meshes = read_grid()
419469 # meshes = read_polyline()
420470 # meshes = read_wellbore_frame_repr()
421471 # meshes = read_representation_set_representation()
422472 # meshes = read_trset()
423473 # meshes = read_pointset()
424- meshes = read_wellbore_frame_repr_demo_jfr_02_26 ()
474+ # meshes = read_wellbore_frame_repr_demo_jfr_02_26()
425475
426476 print (f"Number of meshes read: { len (meshes )} " )
427477
@@ -444,3 +494,4 @@ def read_wellbore_frame_repr_demo_jfr_02_26(
444494 raise e
445495
446496 # read_props_and_cbt()
497+ test_read_write_array ("test_array_rw.h5" )
0 commit comments