<reponame>7l2icj/kamo_clone
#!/usr/bin/env yamtbx.python
"""
(c) RIKEN 2015. All rights reserved. 
Author: <NAME>

This software is released under the new BSD License; see LICENSE.
"""

from libtbx import easy_mp

from yamtbx.dataproc import cbf

import h5py
import numpy
import os
import sys

nproc = 11

def save_cbf(wavelen, data, cbfout):
    header = """\
# Detector: NOT PILATUS but MPCCD
# Wavelength %f A
""" % wavelen

    height, width = data.shape
    #print "Saving", cbfout, height, width, data
    cbf.save_numpy_data_as_cbf(data.reshape(width*height), width, height, "hdf5_converted", str(cbfout),
                               pilatus_header=header)
# save_cbf()

def convert_single(h5in, root, cbfout):
    f = h5py.File(h5in, "r")

    wavelen = f["%s/photon_wavelength_A"%root].value
    data = numpy.array(f["%s/data"%root])
    save_cbf(wavelen, data, cbfout)
    f.close()
    print "Processed: %s %s" % (os.path.basename(h5in), root)
# convert_single()

def convert(h5in, par=False):
    f = h5py.File(h5in, "r")
    if "/LCLS/data" in f:
        convert_single(h5in, root="/LCLS", cbfout=os.path.basename(h5in)+".cbf")
    else:
        for tag in f:
            convert_single(h5in, root="/%s"%tag,
                           cbfout="%s_%s.cbf" % (os.path.basename(h5in), tag))
# convert()

def run(h5_files):
    if len(h5_files) == 0: return

    if len(h5_files) > 1:
        easy_mp.pool_map(fixed_func=convert,
                         args=h5_files,
                         processes=nproc)
    else:
        h5in = h5_files[0]
        tags = h5py.File(h5in, "r").keys()
        fun = lambda x: convert_single(h5in, root="/%s"%x,
                                       cbfout="%s_%s.cbf" % (os.path.basename(h5in), x))
        for tag in tags: fun(tag)
        return # parallel reading of single file seems buggy..
        easy_mp.pool_map(fixed_func=fun,
                         args=tags,
                         processes=nproc)
# run()

if __name__ == "__main__":
    import sys

    h5_files = filter(lambda x: x.endswith(".h5"), sys.argv[1:])

    if len(h5_files) == 0:
        print "Usage: %s h5_files" % sys.argv[0]
        quit()

    run(h5_files)

#!/usr/bin/env yamtbx.python
"""
(c) RIKEN 2015. All rights reserved. 
Author: <NAME>

This software is released under the new BSD License; see LICENSE.
"""

from libtbx import easy_mp

from yamtbx.dataproc import cbf

import h5py
import numpy
import os
import sys

nproc = 11

def save_cbf(wavelen, data, cbfout):
    header = """\
# Detector: NOT PILATUS but MPCCD
# Wavelength %f A
""" % wavelen

    height, width = data.shape
    #print "Saving", cbfout, height, width, data
    cbf.save_numpy_data_as_cbf(data.reshape(width*height), width, height, "hdf5_converted", str(cbfout),
                               pilatus_header=header)
# save_cbf()

def convert_single(h5in, root, cbfout):
    f = h5py.File(h5in, "r")

    wavelen = f["%s/photon_wavelength_A"%root].value
    data = numpy.array(f["%s/data"%root])
    save_cbf(wavelen, data, cbfout)
    f.close()
    print "Processed: %s %s" % (os.path.basename(h5in), root)
# convert_single()

def convert(h5in, par=False):
    f = h5py.File(h5in, "r")
    if "/LCLS/data" in f:
        convert_single(h5in, root="/LCLS", cbfout=os.path.basename(h5in)+".cbf")
    else:
        for tag in f:
            convert_single(h5in, root="/%s"%tag,
                           cbfout="%s_%s.cbf" % (os.path.basename(h5in), tag))
# convert()

def run(h5_files):
    if len(h5_files) == 0: return

    if len(h5_files) > 1:
        easy_mp.pool_map(fixed_func=convert,
                         args=h5_files,
                         processes=nproc)
    else:
        h5in = h5_files[0]
        tags = h5py.File(h5in, "r").keys()
        fun = lambda x: convert_single(h5in, root="/%s"%x,
                                       cbfout="%s_%s.cbf" % (os.path.basename(h5in), x))
        for tag in tags: fun(tag)
        return # parallel reading of single file seems buggy..
        easy_mp.pool_map(fixed_func=fun,
                         args=tags,
                         processes=nproc)
# run()

if __name__ == "__main__":
    import sys

    h5_files = filter(lambda x: x.endswith(".h5"), sys.argv[1:])

    if len(h5_files) == 0:
        print "Usage: %s h5_files" % sys.argv[0]
        quit()

    run(h5_files)
#!/usr/bin/env yamtbx.python (c) RIKEN 2015. All rights reserved.  Author: <NAME>  This software is released under the new BSD License; see LICENSE. \ # Detector: NOT PILATUS but MPCCD # Wavelength %f A #print "Saving", cbfout, height, width, data # save_cbf() # convert_single() # convert() # parallel reading of single file seems buggy.. # run()