def H5toAnnotation ( key, idgrp, annodb ):
  """Return an annotation constructed from the contents of this HDF5 file"""

  # get the annotation type
#  if idgrp.get('ANNOTATION_TYPE'):
  if 'ANNOTATION_TYPE' in idgrp:
    annotype = idgrp['ANNOTATION_TYPE'][0]
  else:
    annotype = annotation.ANNO_ANNOTATION

  # And get the metadata group
  mdgrp = idgrp.get('METADATA')

  if annotype == annotation.ANNO_SEED:

    # Create the appropriate annotation type
    anno = annotation.AnnSeed(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the seed specific metadata
      if 'PARENT' in mdgrp:
        anno.parent = mdgrp['PARENT'][0]
      if 'POSITION' in mdgrp:
        anno.position = mdgrp['POSITION'][:]
      if 'CUBE_LOCATION' in mdgrp:
        anno.cubelocation = mdgrp['CUBE_LOCATION'][0]
      if 'SOURCE' in mdgrp:
        anno.source = mdgrp['SOURCE'][0] 

  elif annotype == annotation.ANNO_SYNAPSE:
    
    # Create the appropriate annotation type
    anno = annotation.AnnSynapse(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if 'SYNAPSE_TYPE' in mdgrp:
        anno.synapse_type = mdgrp['SYNAPSE_TYPE'][0]
      if 'WEIGHT' in mdgrp:
        anno.weight = mdgrp['WEIGHT'][0]
      if 'SEEDS' in mdgrp:
        anno.seeds = mdgrp['SEEDS'][:]
      if 'SEGMENTS' in mdgrp:
        anno.segments = mdgrp['SEGMENTS'] [:]

  elif annotype == annotation.ANNO_SEGMENT:
    
    # Create the appropriate annotation type
    anno = annotation.AnnSegment(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if mdgrp.get('PARENTSEED'):
        anno.parentseed = mdgrp['PARENTSEED'][0]
      if mdgrp.get('SEGMENTCLASS'):
        anno.segmentclass = mdgrp['SEGMENTCLASS'][0]
      if mdgrp.get('NEURON'):
        anno.neuron = mdgrp['NEURON'][0]
      if mdgrp.get('SYNAPSES') and len(mdgrp['SYNAPSES'])!=0:
        anno.synapses = mdgrp['SYNAPSES'][:]
      if mdgrp.get('ORGANELLES') and len(mdgrp['ORGANELLES'])!=0:
        anno.organelles = mdgrp['ORGANELLES'][:]

  elif annotype == annotation.ANNO_NEURON:

    # Create the appropriate annotation type
    anno = annotation.AnnNeuron(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if mdgrp.get('SEGMENTS') and len(mdgrp['SEGMENTS'])!=0:
        anno.segments = mdgrp['SEGMENTS'][:]

  elif annotype == annotation.ANNO_ORGANELLE:
    
    # Create the appropriate annotation type
    anno = annotation.AnnOrganelle(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if mdgrp.get('PARENTSEED'):
        anno.parentseed = mdgrp['PARENTSEED'][0]
      if mdgrp.get('ORGANELLECLASS'):
        anno.organelleclass = mdgrp['ORGANELLECLASS'][0]
      if mdgrp.get('SEEDS') and len(mdgrp['SEEDS'])!=0:
        anno.seeds = mdgrp['SEEDS'][:]
      if mdgrp.get('CENTROID'):
        anno.centroid = mdgrp['CENTROID'][:]

  elif annotype == annotation.ANNO_NODE:
    
    # Create the appropriate annotation type
    anno = annotation.AnnNode(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if 'NODETYPE' in mdgrp:
        anno.nodetype = mdgrp['NODETYPE'][0]
      if 'PARENTID' in mdgrp:
        anno.parentid = mdgrp['PARENTID'][0]
      if 'SKELETONID' in mdgrp:
        anno.skeletonid = mdgrp['SKELETONID'][0]
      if 'RADIUS' in mdgrp:
        anno.radius = mdgrp['RADIUS'][0]
      if mdgrp.get('CHILDREN') and len(mdgrp['CHILDREN'])!=0:
        anno.children = mdgrp['CHILDREN'][:]
      if mdgrp.get('LOCATION'):
        anno.location = mdgrp['LOCATION'][:]

  elif annotype == annotation.ANNO_SKELETON:
    
    # Create the appropriate annotation type
    anno = annotation.AnnSkeleton(annodb)

    # Load metadata if it exists
    if mdgrp:
      # load the synapse specific metadata
      if 'SKELETONTYPE' in mdgrp:
        anno.skeletontype = mdgrp['SKELETONTYPE'][0]
      if 'ROOTNODE' in mdgrp:
        anno.rootnode = mdgrp['ROOTNODE'][0]

  # No special action if it's a no type
  elif annotype == annotation.ANNO_ANNOTATION:
    # Just create a generic annotation object
    anno = annotation.Annotation(annodb)

  else:
    logger.warning ("Dont support this annotation type yet. Type = %s" % annotype)
    raise OCPCAError ("Dont support this annotation type yet. Type = %s" % annotype)

  # now load the annotation common fields
  if re.match("^\d+$", key):
    anno.annid = int(key)
  else:
    anno.annid = 0

  if mdgrp:
    # now load the metadata common fields
    if mdgrp.get('STATUS'):
      anno.status = mdgrp['STATUS'][0]
    if mdgrp.get('CONFIDENCE'):
      anno.confidence = mdgrp['CONFIDENCE'][0]
    if mdgrp.get('AUTHOR'):
      anno.author = mdgrp['AUTHOR'][0]

    # and the key/value pairs
    if mdgrp.get('KVPAIRS'):
      fstring = cStringIO.StringIO( mdgrp['KVPAIRS'][0] )
      csvr = csv.reader(fstring, delimiter=',')
      for r in csvr:
        anno.kvpairs[r[0]] = r[1] 

  return anno

# Need to convert the rest of this to multiple key
dataset/ETHPy150Open neurodata/ndstore/ocpca/h5ann.py/H5toAnnotation/original