def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-u",'--user',
                      action="store",dest="user",
                      help="Name of target edb user")

    parser.add_option("-e","--edb",
                      action="store",dest="edb",
                      help="Name of target edb")

    parser.add_option("--begin",
                      action="store",dest="begin",
                      help="YYMMDDHH to begin calculation")

    parser.add_option("--end",
                      action="store",dest="end",
                      help="YYMMDDHH to end calculation")
    
    parser.add_option("-l", "--loglevel",
                      action="store",dest="loglevel",default=2,
                      help="Sets the loglevel, 0-3 where 3=full logging")
    
    parser.add_option("-o","--outfile",
                      action="store",dest="outfile",
                      default=None,
                      help="Name of output file")

    parser.add_option("--MMSI",action="store",
                      dest="MMSIfile",default=None,
                      help="File with MMSI for ships process")

    parser.add_option("--macro",action="store",
                      dest="macro",default=None,
                      help="Macro of search")

    parser.add_option("-p","--parameterTable",action="store",
                      dest="parameterTable",
                      help="Tab-sep ascii table with one row for each run and one column for each search parameter to set")

    (options, args) = parser.parse_args()


    
    #--------------------Init logger-----------------------
    rootLogger = logger.RootLogger(level=options.loglevel)
    global log
    log = rootLogger.getStreamLogger(sys.argv[0])
    #-----------------Validating options-------------------

    if options.user is None:
        log.error("Need to specify -u <user>")
        return 1
    if options.edb is None:
        log.error("Need to specify -e <edb>")
        return 1
    if options.begin is None:
        log.error("Need to specify --begin <YYMMDDHH>")
        return 1
    if options.end is None:
        log.error("Need to specify --end <YYMMDDHH>")
        return 1
    if options.MMSIfile is None:
        log.error("Need to specify --MMSI <mmsi-file>")
        return 1        
    if options.macro is None:
        log.error("Need to specify --macro <macro-file>")
        return 1
        
    dmn=Domain()
    try:
        edb = Edb(dmn,options.user,options.edb)
    except:
        log.error("No such edb found")
        return 1

    parTable = DataTable()
    parTable.read(options.parameterTable)


    mmsiList=open(options.MMSIfile,"r").read().split("\n")
    
    #log.error("Could not read mmsi file")
    #return 1
    
    tableConds=[]
    tableVars= []
    for colId in parTable.listIds():
        if colId.startswith("var."):
            tableVars.append(colId)
        elif colId.startswith("cond."):
            tableConds.append(colId)
        
        
    emfacdb = Emfacdb(edb)
    emfacdb.read()

    sourcedb = Sourcedb(edb)
    
    macro = ControlFile(options.macro,removeComments=False)
    macro.setParam("FROM","      : "+options.begin)
    macro.setParam("TO","        : "+options.end)
    macro.setParam("edb.user:"******"edb.edb:",options.edb)
    #Write header to result file
    outfile = open(options.outfile,"w")
    outfile.write("MMSI"+"\t")
    for header in parTable.listIds():
        outfile.write("header"+"\t")
    outfile.write("Result\n")

    emisPattern = re.compile("#EMIS (\d*\.\d*)")
    command="gifmap -T -i "+options.macro+" -o /usr/airviro/tmp/res.tmp"
    
    for mmsi in mmsiList:
        if mmsi=="":
            continue
        sourcedb.read(X1=SHIPFLAG,Y1=int(mmsi))
        if len(sourcedb.sources) !=1:
            log.warning("No ship with MMSI %s found" %mmsi)
            continue
        source = sourcedb.sources[0]

        macro.setParam("SEARCHSTR","  : "+mmsi+",*")

        for rowInd,row in enumerate(parTable.data):
            for tableVar in tableVars:
                for actIndex, act  in  source.activity_emis.items():
                    ef = emfacdb[actIndex]
                    for varInd,var in ef.vars.items():
                        tableVarName=tableVar[4:]
                        if "var."+var.name==tableVarName:
                            break
                    colInd=parTable.colIndex("var."+var.name)
                    varVal=row[colInd]
                    act["VARLIST"][varInd-1]=(varInd,varVal)
            if len(tableVars)>0:
                sourcedb.write()

            for condPar in tableConds:
                colIndex = parTable.colIndex(condPar)
                condVal=row[colIndex]
                macroKey=condPar[5:]
                macro.setParam(macroKey,"  : "+condVal)

            macro.write()
            p=subprocess.Popen(command,stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
            retCode=p.wait()
            res=p.stdout.read()
            match=emisPattern.search(res)
            emis = float(match.groups()[0])
            outfile.write(mmsi+"\t")
            for val in row:
                outfile.write(val+"\t")
            outfile.write(str(emis)+"\n")

    log.info("Finished!")
    return 0