def fetch_dataset (bq, uri, destdir):
    """ Fetch the binaries associated with a bisque dataset
    @param bq: A BQSession object
    @param uri: a dataset uri 
    @param destdir: directory to put the binaries in
    @return :  A dict mapping a blob uri -> binary filename 
    """
    # Get the elements of the dataset as xml entries
    dataset = bq.fetchxml("%s/value" %uri)
    results = {}
    # loop through each element i.e each image/video to fetch the binary
    for fxml  in dataset:
        # get the unique uri of the blob
        furi = fxml.get ('uri')  
        logging.debug( "FETCHING %s", furi)
        # sanity check that we are fetching something that has a binary component
        if  fxml.tag == 'file' or fxml.tag == 'image':
            x = fetch_blob(bq, furi, dest=destdir)
        #elif fxml.tag == 'image':
        #    x = fetch_image_pixels(bq, furi, dest=destdir)
        else:
            logging.warn ('skipping %s', BQFactory.to_string(fxml))
            continue
        results.update (x)
    return results
    def main(self, image_url,  mex_url = None, bisque_token=None, bq = None, args = None):
        #  Allow for testing by passing an alreay initialized session
        if bq is None:
            bq = BQSession().init_mex(mex_url, bisque_token)
        # Fetch the blob  links
        if not os.path.exists ('videos'):
            os.makedirs ('videos')
        video = fetch_blob(bq, image_url, 'videos')
        print "VIDEO file ", video
	 #pass arguments to MotionMeerkat scripts, located in MotionMeerkat/main.py	
	#Structure arguments
	#Format call string

        callargs = ["python MotionMeerkat/main.py", 
                    "--i", video.values()[0], 
                    "--threshT", args[0],
                    "--sub", args[1],
                    "--mogh", args[2],
                    "--mogv", args[3],
                    "--accA", args[4],
                    "--burn", args[5],
                    "--frameSET", 
                    "--frame_rate", "1",
                    "--makeV", "none",
                    "--fileD", "Output"]
	print "Calling ", " ".join(callargs)

	#run MotionMeerkat
	r =  call(" ".join(callargs), shell=True)
        if r != 0:
		bq.fail_mex ("Meerkat returned non-zero")
		#Post Results
	
	#get filename to get the destination of the csv to be posted
	fn=video.values()[0]
	fbs= os.path.basename(fn)
	head, tail = os.path.splitext(fbs)
	
	#post file
	frames_blob = bq.postblob(str("Output/" + head + "/Frames.csv"))
	
	#get file location from regex
	uri=re.search("uri=\"(.*?)\"", frames_blob).group(1)
	tags = [{ 'name': 'outputs','tag' : [{'name': 'frames_csv', 'type':'file', 'value':uri}]}]
	bq.finish_mex(tags = tags)
	sys.exit(0)