Giter Club home page Giter Club logo

Comments (3)

ivanstan avatar ivanstan commented on August 19, 2024

I got this:

Traceback (most recent call last):
  File "HDF5toJSON.py", line 26, in <module>
    h5file = open_file(sys.argv[1])
  File "/usr/local/lib/python2.7/site-packages/tables/file.py", line 320, in open_file
    return File(filename, mode, title, root_uep, filters, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/tables/file.py", line 784, in __init__
    self._g_new(filename, mode, **params)
  File "tables/hdf5extension.pyx", line 489, in tables.hdf5extension.File._g_new
tables.exceptions.HDF5ExtError: HDF5 error back trace

  File "H5F.c", line 604, in H5Fopen
    unable to open file
  File "H5Fint.c", line 1087, in H5F_open
    unable to read superblock
  File "H5Fsuper.c", line 277, in H5F_super_read
    file signature not found

End of HDF5 error back trace

Unable to open/create file '/Users/ivanstan/projects/MODIS/MOD14.A2018290.1035.006.NRT.hdf'
  • File exists

from hdf5-to-json-converter.

Tointoin avatar Tointoin commented on August 19, 2024

doesn't work for me either. Got this:

python HDF5toJSON.py /path/to/my/existing/file.hdf5
Traceback (most recent call last):
  File "HDF5toJSON.py", line 125, in <module>
    json_data = converter(h5file)
  File "HDF5toJSON.py", line 61, in __init__
    gp_contents = {gp_name : self.groupContentsDict[gp_name]}
KeyError: 'groupe_name'
Closing remaining open files : /path/to/my/existing/file.hdf5

from hdf5-to-json-converter.

Ayesha742 avatar Ayesha742 commented on August 19, 2024

The following code works:

`# -- coding: utf-8 --

HDF5 to JSON converter

Author - Janu Verma

[email protected]

##############################################################################################
import json
import sys
from collections import defaultdict
import re
import io

try:
import numpy
except:
print ("Error : Requires numpy")
sys.exit()

try:
from tables import *
except:
print ("Error : Requires PyTables")

h5file = open_file("labels.h5")

##########################################################################################

class converter:
"""
Converts the contents of an HDF5 file into JSON.
Also has methods to access the contents of a group directly
without following the hierarchy.
"""
def init(self, input_file):
self.file_name = re.sub(r'.h5$', '','labels.h5')
self.groupParentDict = defaultdict(list)
self.groupContentsDict = {}
self.file = input_file
self.allGroups = []
for group in input_file.walk_groups():
name = group._v_name
parent = group._v_parent
parent = parent._v_name
self.allGroups.append(name)
self.groupParentDict[parent].append(name)
self.groupContentsDict[name] = {}

		for array in h5file.list_nodes(group, classname="Array"):
			array_name = array._v_name
			array_contents = array.read()
			array_info = {array_name : array_contents}
			self.groupContentsDict[name].update(array_info)

		for gp in h5file.list_nodes(group, classname="Group"):
			gp_name = gp._v_name
			gp_contents = {gp_name : self.groupContentsDict[gp_name]}
			self.groupContentsDict[name].update(gp_contents)

		for table in h5file.list_nodes(group, classname="Table"):
			table_name = table._v_name
			table_contents = table.read()
			table_info = {table_name : table_contents}
			self.groupContentsDict[name].update(table_info)	

def jsonOutput(self):
	"""
	Returns a JSON document containing all the information stored in the HDF5 file.
	Creates a JSON file of the same name as the input HDF5 file with json extension.
	When decoded the file contains a nested dictionary. 
	The primary key is the root group '\'. 
	"""
	alpha = self.groupContentsDict

	json_file_name = self.file_name + '.json' 
	with io.open(json_file_name, 'w', encoding='utf-8') as f:
		#record = json.dumps(alpha,cls=NumpyAwareJSONEncoder)
		f.write(json.dumps(alpha, cls=NumpyAwareJSONEncoder, ensure_ascii=False))
	f.close()
	return 

def Groups(self):
	"""
	Returns all the groups in the HDF5 file. 
	Helpful in exploring the file and getting an idea of the contents. 
	"""	
	return json.dumps(self.allGroups, cls=NumpyAwareJSONEncoder)

def subgroups(self, group):
	"""
	Returns the subgroups of the group.  
	"""	
	return json.dumps(self.groupParentDict[group], cls=NumpyAwareJSONEncoder)

def groupContents(self, group):
	"""
	Returns the contents of a groups. 
	You can access the contents of the group directly
	without following the hierarchy. 
	""" 	
	info = self.groupContentsDict[group]
	return json.dumps(info, cls=NumpyAwareJSONEncoder)

########################################################################################################

class NumpyAwareJSONEncoder(json.JSONEncoder):
"""
This class facilitates the JSON encoding of Numpy onjects.
e.g. numpy arrays are not supported by the standard json encoder - dumps.
"""
def default(self, obj):
if isinstance(obj, numpy.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)

#######################################################################################################

json_data = converter(h5file)
contents = json_data.jsonOutput()

`

from hdf5-to-json-converter.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.