Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created December 16, 2011 18:57
Show Gist options
  • Save swarbhanu/1487401 to your computer and use it in GitHub Desktop.
Save swarbhanu/1487401 to your computer and use it in GitHub Desktop.
Example code, pyon/examples/service/hdf_hello.py (to replace hello_service.py) that defines a method which the server uses to handle a message. In our case, the server makes an hdf file out of the binary string provided in the message, modifies it using h
#!/usr/bin/env python
from pyon.util.log import log
from interface.services.examples.hello.ihello_service import BaseHelloService
from examples.service.hello_service import HelloService
import h5py
import numpy
class HDFHelloService(HelloService):
def hello(self, text=''):
log.debug("In hdf_hello_service.hello. Text=%s" % text)
### Do HDF Stuff here!
# write the binary string to a new file
f = open("myfile_check_out.hdf5", mode='wb')
f.write(text)
f.close()
## Do something with the file using h5py
# use the core driver of h5Py to write an hdf file
file = h5py.File('myfile_check_out.hdf5', mode = 'a', driver='core')
# file = h5py.File('myfile_check2.hdf5', mode = 'w', driver='log')
grp = file.create_group('anotherGroup')
dataset2 = grp.create_dataset('dataset2', (10,10), '=f8', maxshape=(None,None))
file['dataset2'] = numpy.ones((10,10))
file.flush()
file.close()
# write the hdf file back into a string
f = open("myfile_check_out.hdf5", mode='rb')
# read the binary string representation of the file
dataString = f.read()
f.close()
# return the string to the client
return "%s" % dataString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment