#!/usr/bin/python

# Create a project and stack associated HDF5 file with additional
# data such as labels, meshes etc. 

import os.path as op
import h5py
from contextlib import closing
import numpy as np

project_id = 3
stack_id = 4
filepath = '/home/stephan/dev/CATMAID/django/hdf5'

with closing(h5py.File(op.join(filepath, '%s_%s.hdf' % (project_id, stack_id)), 'w')) as hfile:
    mesh=hfile.create_group('meshes')
    midline=mesh.create_group('midline')
    midline.create_dataset("vertices", data=np.array( [61200,  15200, 26750,63920,  26400, 76800, 66800,57600,76800, 69200,53120,26750] , dtype = np.float32 ) )
    # faces are coded according to the three.js JSONLoader standard.
    # See https://github.com/mrdoob/three.js/blob/master/src/extras/loaders/JSONLoader.js
    midline.create_dataset("faces", data=np.array( [0, 0, 1, 2,  0,2,3,0] , dtype = np.uint32 ) )

    image=hfile.create_group('myimage')
    scale=image.create_group('scale')
    scale0=scale.create_group('0')
    scale0.create_dataset('data', data=np.random.random_integers(0,255,
            (1024,1024,10)), dtype = np.uint8)

    scale1=scale.create_group('1')
    scale1.create_dataset('data', data=np.random.random_integers(0,255,
            (512,512,10)), dtype = np.uint8)

    scale2=scale.create_group('2')
    scale2.create_dataset('data', data=np.random.random_integers(0,255,
            (256,256,10)), dtype = np.uint8)
    
    image=hfile.create_group('labels')
    scale=image.create_group('scale')
    scale0=scale.create_group('0')
    scale0.create_dataset('data', data=np.zeros( (1024,1024,10)),
                          dtype = np.uint8)

    scale1=scale.create_group('1')
    scale1.create_dataset('data', data=np.zeros( (512,512,10)),
                          dtype = np.uint8)

    scale2=scale.create_group('2')
    scale2.create_dataset('data', data=np.zeros( (256,256,10)),
                          dtype = np.uint8)
    
    # multi-scale image data in hdf5
    # myimage/ (type:multi_scale_image)
    #   scale/ (int:number_of_scales)
    #       0/
    #           irregular_dataset:mydataset
    #       1/
    #           irregular_dataset:mydataset

@@ -917,119 +917,664 @@
 e')%0A
+%0A
     
-scale=image.create_group('scale')%0A    scale0=scale.create_group('0')%0A    scale0.create_dataset('data',
+image.attrs%5B'axis0__label'%5D = 'x'%0A    image.attrs%5B'axis0__unit_label'%5D = 'micrometer'%0A    image.attrs%5B'axis0__unit_xref'%5D = 'UO:0000017'%0A    # the interval defines for a spatial axis the resolution / dimension%0A    # for scale 0%0A    image.attrs%5B'axis0__interval'%5D = 1.0%0A    %0A    image.attrs%5B'axis1__label'%5D = 'y'%0A    image.attrs%5B'axis1__unit_label'%5D = 'micrometer'%0A    image.attrs%5B'axis1__unit_xref'%5D = 'UO:0000017'%0A    image.attrs%5B'axis1__interval'%5D = 1.0%0A%0A    image.attrs%5B'axis2__label'%5D = 'z'%0A    image.attrs%5B'axis2__unit_label'%5D = 'micrometer'%0A    image.attrs%5B'axis2__unit_xref'%5D = 'UO:0000017'%0A    image.attrs%5B'axis2__interval'%5D = 1.0%0A%0A   
  data
+set
 =np.
@@ -1606,366 +1606,611 @@
 255,
-%0A            (1024,1024,10)), dtype = np.uint8)%0A%0A    scale1=scale.create_group('1')%0A    scale1.create_dataset('data', data=np.random.random_integers(0,255,%0A            (512,512,10)), dtype = np.uint8)%0A%0A    scale2=scale.create_group('2')%0A    scale2.create_dataset('data', data=np.random.random_integers(0,255,%0A            (256,256,10)), dtype = np.uint8)%0A    
+ (1024,1024,10))%0A    scale=image.create_group('scale')%0A    scale.attrs%5B'number_of_scales'%5D = 3%0A    %0A    scale0=scale.create_group('0')%0A    scale0.attrs%5B'axes_scaling_factor'%5D = (1.0, 1.0, 1.0)%0A    scale0.create_dataset('data', data=dataset, dtype = np.uint8)%0A%0A    scale1=scale.create_group('1')%0A    scale1.create_dataset('data', data=dataset%5B::2,::2,:%5D, dtype = np.uint8)%0A    scale1.attrs%5B'axes_scaling_factor'%5D = (0.5, 0.5, 1.0)%0A%0A    scale2=scale.create_group('2')%0A    scale2.create_dataset('data', data=dataset%5B::4,::4,:%5D, dtype = np.uint8)%0A    scale2.attrs%5B'axes_scaling_factor'%5D = (0.25, 0.25, 1.0)%0A
 %0A   

Update multi-scale pyramid HDF5 layout