def uploadImageToDataset(services, pixelsType, imageArray, imageName, dataset=None):
    
    """
    Uploads a local Spider image to an OMERO dataset. Same function exists in spider2omero.py.
    
    @param services     Map of OMERO services
    @param pixelsType   The OMERO PixelsType object for new image.
    @param imageArray   Numpy array of pixel data - 2D
    @param imageName    The local file, for getting image header info
    @param dataset      Dataset to put images in, if specified. omero.model.Dataset
    """

    session = services["session"]
    queryService = services["queryService"]
    updateService = services["updateService"]
    rawFileStore = services["rawFileStore"]
    
    namespace = omero.constants.namespaces.NSCOMPANIONFILE 
    fileName = omero.constants.annotation.file.ORIGINALMETADATA
    
    print "Importing image: %s" % imageName
    description = ""
    if len(imageArray.shape) > 2:
        plane2Dlist = imageArray    # 3D array already. TODO: Need to check that volume is not mirrored (Z in correct order)
    else:
        plane2Dlist = [imageArray]  # single plane image
    
    name = os.path.basename(imageName)
    image = scriptUtil.createNewImage(session, plane2Dlist, name, description, dataset)
    
    # header is a list of values corresponding to attributes 
    header = getSpiderHeader(imageName)
    
    # if we know the pixel size, set it in the new image
    if len(header) >= 38:
        physicalSizeX = header[38]
        physicalSizeY = header[38]
        pixels = image.getPrimaryPixels()
        pixels.setPhysicalSizeX(rdouble(physicalSizeX))
        pixels.setPhysicalSizeY(rdouble(physicalSizeY))
        updateService.saveObject(pixels)
    
    # make a temp text file. 
    f = open(fileName, 'w')
    f.write("[GlobalMetadata]\n")

    # now add image attributes as "Original Metadata", sorted by key. 
    for i, h in enumerate(header):
        if i in spiderHeaderMap:
            f.write("%s=%s\n" % (spiderHeaderMap[i], h))
            
    f.close()

    scriptUtil.uploadAndAttachFile(queryService, updateService, rawFileStore, image, fileName, "text/plain", None, namespace)
    # delete temp file
    os.remove(fileName)
def uploadImageToDataset(session, services, pixelsType, localImage, dataset=None, description="", imageName=None):
    
    """
    Uploads a local Spider image to an OMERO dataset. Same function exists in spider2omero.py.
    
    @param services     Map of OMERO services
    @param pixelsType   The OMERO PixelsType object for new image.
    @param imageName    The local image path/name. Also used for new image name. 
    @param dataset      Dataset to put images in, if specified. omero.model.Dataset
    """
    
    renderingEngine = services["renderingEngine"]
    queryService = services["queryService"]
    pixelsService = services["pixelsService"]
    rawPixelStore = services["rawPixelStore"]
    updateService = services["updateService"]
    rawFileStore = services["rawFileStore"]

    
    namespace = omero.constants.namespaces.NSCOMPANIONFILE 
    fileName = omero.constants.annotation.file.ORIGINALMETADATA
    
    if imageName == None:  imageName = localImage
    print "Importing image: %s" % imageName
    plane2D = spider2array(localImage)
    plane2Dlist = [plane2D]        # single plane image
    
    image = scriptUtil.createNewImage(session, plane2Dlist, imageName, description, dataset)
    
    # header is a list of values corresponding to attributes 
    header = getSpiderHeader(localImage)
    
    # if we know the pixel size, set it in the new image
    if len(header) >= 38:
        physicalSizeX = header[38]
        physicalSizeY = header[38]
        pixels = image.getPrimaryPixels()
        pixels.setPhysicalSizeX(rdouble(physicalSizeX))
        pixels.setPhysicalSizeY(rdouble(physicalSizeY))
        updateService.saveObject(pixels)
    
    # make a temp text file. 
    f = open(fileName, 'w')
    f.write("[GlobalMetadata]\n")

    # now add image attributes as "Original Metadata", sorted by key. 
    for i, h in enumerate(header):
        if i in spiderHeaderMap:
            f.write("%s=%s\n" % (spiderHeaderMap[i], h))
            
    f.close()

    scriptUtil.uploadAndAttachFile(queryService, updateService, rawFileStore, image, fileName, "text/plain", None, namespace)
    # delete temp file
    os.remove(fileName)
    return image