def main():
    parser = OptionParser(usage=usage)
    parser.add_option('-n', '--dryrun', action="store_true", default=False, help="Print actions but do not execute.")
    parser.add_option('-v', '--verbose', action="store_true", default=False, help="")
    parser.add_option('-x', '--offsetx', type="int", help="")
    parser.add_option('-y', '--offsety', type="int", help="")

    options, args = parser.parse_args()
    
    # set log level
    if options.verbose:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
    else:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)

    # use offsets if A01 is not the first well imaged
    # e.g. it first well is C4, set offsetx = 3, offsety = 2
    offsetx = 0
    offsety = 0
    if options.offsetx:
        offsetx = options.offsetx
    if options.offsety:
        offsety = options.offsety

    
    # When the python script is started, it has own new shell, with a new irods environment
    # where the current working directory is ~. 
    # This command checks the current working directory of the parent shell from the .irodsEnv files.
    iutils.icd2ipwd()
    ipwd= iutils.ipwd()

    target = None
    # check if template was given
    if len(args) == 0:
        parser.error("No target folder given.")
    else:
        target = args[0]

    # check if template is an absolute path
    if iquest.collection_exists(ipwd + "/" + target):
        target = ipwd + "/" + target
    else:
        if not iquest.collection_exists(target):
            parser.error("Target " + target + " not found.")
    logging.info("Target folder " + target)

    dataset = createDatasetCode(target)

    files = iquest.list_dataobjects_recursive(target)
    for f in files:
        # for originals and converted hyperstacks
        result = reOMEfield.search(f)
        if result:
            logging.debug(f)
            # read Matrix Screener indexes
            u = result.group(3)
            v = result.group(4)
            x = result.group(8)
            y = result.group(9)
            
            # apply offsets
            u = int(u) + offsetx
            v = int(v) + offsety
            
            avu = imeta.AVU(ATTR_DATASET, dataset)
            logging.debug('dataset:{0}'.format(dataset))
            if not options.dryrun:
                imeta.delete(f,avu)
                imeta.add(f,avu)

            well = createWellCode(u, v)
            logging.debug('U:{0}, V:{1}, well:{2}'.format(u, v, well))
            avu = imeta.AVU(ATTR_WELL,  well)
            if not options.dryrun:
                imeta.delete(f, avu)
                imeta.add(f, avu)
            
            field = createFieldIndex(x, y)
            logging.debug("field: " + field)
            avu = imeta.AVU(ATTR_FIELD,  field)
            if not options.dryrun:
                imeta.delete(f, avu)
                imeta.add(f, avu)

        # for original MatrixScreener output
        result = reOME.search(f)
        if result:
            c = result.group(12)
            channel = str(int(c))
            logging.debug("channel: " + channel)
            avu = imeta.AVU(ATTR_CHANNEL, channel)
            if not options.dryrun:
                imeta.delete(f, avu)
                imeta.add(f, avu)
def main():
    irods_host = IRODS_DEFAULT_HOST
    bisque_host = BISQUE_DEFAULT_HOST

    parser = OptionParser(usage=usage)
    parser.add_option("-l", "--list", action="store_true", default=False, help="list contents of irods url and exit")
    parser.add_option(
        "-n", "--dryrun", action="store_true", default=False, help="print actions but do not execute.. sets verbose"
    )
    parser.add_option("-i", "--irods_host", help="e.g. irods://ida.csc.fi")
    parser.add_option("-b", "--bisque_host", help="e.g. https://bisquevm-1.it.helsinki.fi")
    parser.add_option("-v", "--verbose", action="store_true", default=False, help="be verbose")
    options, args = parser.parse_args()
    if len(args) > 0:
        irods_data = args.pop(0)
    else:
        parser.error("No irods data given.")

    # set log level
    if options.verbose:
        logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
    elif options.dryrun:
        logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
    else:
        logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.CRITICAL)

    # check server URLs
    if options.irods_host:
        irods_host = options.irods_host
    if not irods_host or not irods_host.startswith("irods://"):
        parser.error("must include a valid iRODS url i.e. %s" % IRODS_DEFAULT_HOST)
    if options.bisque_host:
        bisque_host = options.bisque_host
    if not bisque_host or not (bisque_host.startswith("http://") or bisque_host.startswith("https://")):
        parser.error(
            "Invalid Bisque host URL '%s'.\nPlease specify valid HTTP URL i.e. '%s'."
            % (bisque_host, BISQUE_DEFAULT_HOST)
        )
    if bisque_host.startswith("http://"):
        logging.warning("Using plain HTTP, passwords will be sent unencrypted.")

    DATA_IS_COLLECTION = False
    DATA_IS_SINGLE_OBJECT = False
    # check if user specified a collection or a single dataobject
    DATA_IS_COLLECTION = iquest.collection_exists(irods_data)
    if not DATA_IS_COLLECTION:
        DATA_IS_SINGLE_OBJECT = iquest.dataobject_exists(irods_data)
    # check if the collection or data exists
    if not DATA_IS_COLLECTION and not DATA_IS_SINGLE_OBJECT:
        # user probably didn't give full path starting from /ZONE/home/...
        # try in the current irods directory
        irods_data_orig = irods_data
        iutils.icd2ipwd()
        root = iutils.ipwd()
        irods_data = root + "/" + irods_data
        DATA_IS_COLLECTION = iquest.collection_exists(irods_data)
        if not DATA_IS_COLLECTION:
            DATA_IS_SINGLE_OBJECT = iquest.dataobject_exists(irods_data)
        if not DATA_IS_COLLECTION and not DATA_IS_SINGLE_OBJECT:
            parser.error("File or collection '" + irods_data_orig + "' not found in " + irods_host + ".")

    if DATA_IS_COLLECTION:
        entries = iquest.list_dataobjects_recursive(irods_data)
    else:
        entries = []
        entries.append(irods_data)

    if options.list:
        for e in entries:
            print e
        return

    # ask user for bisque username/password
    print
    print "Please login to Bisque with your University AD username and password."
    bisque_user = getpass.getuser()
    print "Username:"******"Username/password needs to be provided for uploads.")

    bisque = BisqueConnection(bisque_host, bisque_user, bisque_password)
    for irods_file in entries:
        bisque.register_irods_file(irods_host, irods_file, options.dryrun)