def main():
    # setting up parser
    parser = OptionParser(usage=usage, version=version)

    parser.add_option("-o", "--outputFile", action="store", dest="outputFile", default=None, help="Path to output file")
    parser.add_option("-i", "--inputDir", action="store", dest="inputDir", help="Path to directory with input files")
    parser.add_option(
        "-f", "--flip", action="store_true", dest="flip", help="Flip rasters to correct north-south orientation"
    )
    parser.add_option("-s", "--substances", dest="substances", action="store", help="List of substances")

    (options, args) = parser.parse_args()

    wd = path.abspath(options.inputDir)
    utDir = path.dirname(path.abspath(options.outputFile))
    utRastName = path.basename(path.abspath(options.outputFile))
    substances = options.substances.split()

    # -----------------------------------------------------
    for substance in substances:
        pathStr = os.path.join(wd, "*" + substance + "*")
        fileList = glob.glob(pathStr)
        utRast = substance + "_" + utRastName
        utRastPath = os.path.join(utDir, utRast)

        num = 0
        for filePath in fileList:
            num += 1
            print "reading ", os.path.basename(filePath)
            rast = raster.raster()

            rast.read(filePath, format="airviro", index=2)
            print "Sum of raster is: ", rast.sum()

            # airviro rasters are upside-down, this parameter marks that the raster will be turned
            if options.flip:
                rast.flip()

            if num == 1:
                totRast = rast
            else:
                totRast = totRast + rast

        print "Sum for total rast: ", totRast.sum()
        totRast.write(utRastPath)
        print
        print "finished merging regions for substance: ", substance
        print
def main():
    
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
        
    parser.add_option("-d","--directory",
                      action="store",dest="rootDir",
                      help="Root directory to start search for rasters")
    
    parser.add_option("-f","--rasterFilter",
                      action="store",dest="rasterFilter",
                      help="Raster filter", default="*.asc")
    
    parser.add_option("-o","--outputPath",
                      action="store",dest="outputPath",
                      help="Output table path")

    parser.add_option("-m","--maskFilter",
                      action="store",dest="maskFilter",
                      help="Mask raster filter")    

    parser.add_option("--infoFilter",
                      action="store",dest="infoFilter",
                      help="Regular expression to extract metadata, named groups are needed for 'sector', 'substance', 'units', 'year' and 'version', default is: %default",
                      default=".*/(?P<sector>[a-z]*)(?P<substance>.*?)(?P<units>gpers)(?P<year>[0-9]*)(?P<version>.*?).asc")


    (options, args) = parser.parse_args()
        
    if len(args)>0:
        parser.error("Wrong number of arguments")

    if options.rootDir is None:
        parser.error("Needs to specify root directory")

    if options.maskFilter is None:
        parser.error("Needs to specify mask filter")


   
    rootDir=path.abspath(options.rootDir)
    rasterFilter=options.rasterFilter
    maskFilter=options.maskFilter
    outputTablePath=path.abspath(options.outputPath)

#     infoPattern=re.compile("?P<(.*?)>")
#     infoLabels =infoPattern.findall()
#     try:
#         infoLabels.remove("units")
#     except ValueError:
#         units=None
        
#     desc=[]
#     keys=[]
#     for l in infoLabels:
#         desc.append({"id":l,"type":str})
#         keys.append(l)
        
        
    outputTable= dataTable.DataTable(desc=[{"id":"Zone_id","type":int},
                                           {"id":"Sector","type":str},
                                           {"id":"Year","type":int},
                                           {"id":"Version","type":str}],
                                     keys=["Zone_id","Sector","Year","Version"])

    infoPattern=re.compile(options.infoFilter)     

    maskList=glob.glob(path.abspath(options.maskFilter))
        
    masks=[]
    for maski,maskPath in enumerate(maskList):
        sys.stdout.write("Reading mask raster %i out of %i..." %(maski,len(maskList)))
        mp = path.abspath(maskPath)
        mask=raster.raster()
        try:
            mask.read(mp)
        except:
            parser.error("Could not read mask raster %s" %mp)
            sys.exit(1)
        masks.append(mask)
    sys.stdout.write("finished\n")


    for root, dirs, files in os.walk(rootDir):
        print "Scanning directory: %s" %root
        rasterPaths=glob.glob(path.join(root,rasterFilter))

        for rp in rasterPaths:
            match=infoPattern.match(rp)

            try:
                substance=match.group('substance')
            except AttributeError:
                substance=path.basename(rp)
            
            try:
                sector=match.group('sector')
            except AttributeError:
                sector="unknown"
            if sector=="":
                sector="unknown"

            try:
                units=match.group('units')
            except:
                units="unknown"
            if units=="":
                units="unknown"

            try:
                year=int(match.group('year'))
            except:
                year=-9999
            if year =="":
                year=-9999

            try:
                ver=match.group('version')
            except:
                ver="default"
            if ver=="":
                ver=="default"
            

            rast=raster.raster()
            rast.read(rp)

            for mask in masks:
                statistics=zonalStatistics(rast,mask)


            if substance not in outputTable.listIds():
                outputTable.addCol({"id":substance,"type":float,"unit":units})

            for data in statistics:
                zone_id=data["zone_id"]
                zone_sum=data["sum"]
                row=outputTable.ncols()*[None]
                row[outputTable.colIndex("Zone_id")]=zone_id
                row[outputTable.colIndex("Year")]=year
                row[outputTable.colIndex("Sector")]=sector
                row[outputTable.colIndex("Version")]=ver
                row[outputTable.colIndex(substance)]=zone_sum
                outputTable.addRow(row,append=True)
    output=open(outputTablePath,"w")
    outputTable.write(output)    
def main():
    # -----------Setting up and unsing option parser-----------------------
    parser = OptionParser(usage=usage, version=version)
    logger = logging.getLogger("exportEmisGrids.py")

    parser.add_option(
        "-l",
        "--loglevel",
        action="store",
        dest="loglevel",
        default=2,
        help="Sets the loglevel (0-3 where 3=full logging)",
    )

    parser.add_option(
        "-d", "--distKey", action="store", dest="distKey", default=None, help="Distribution grid to be weighted"
    )

    parser.add_option("-r", "--regRast", action="store", dest="regRast", help="Region definition raster")

    parser.add_option("-s", "--regStat", action="store", dest="regStat", help="Regional statistics")

    parser.add_option(
        "-e",
        "--emis",
        action="store_true",
        dest="emis",
        default=False,
        help="Generate emission grid from key and regional emissions",
    )

    parser.add_option(
        "-c", "--colName", action="store", dest="colName", default=None, help="Title of column in regional statistics"
    )

    parser.add_option("-o", "--outRaster", action="store", dest="resRast", default=None, help="Result raster filename")

    (options, args) = parser.parse_args()

    # ------------Setting up logging capabilities -----------
    rootLogger = logger2.RootLogger(int(options.loglevel))
    logger = rootLogger.getLogger(sys.argv[0])
    # -------------------------------------------------------

    regRastPath = path.abspath(options.regRast)
    keyRastPath = path.abspath(options.distKey)
    regStatPath = path.abspath(options.regStat)
    resRastPath = path.abspath(options.resRast)

    key = raster.raster()
    regdef = raster.raster()

    logger.debug("Reading region raster")

    try:
        regdef.read(regRastPath)
    except:
        logger.error("Could not read regionRaster")
        sys.exit(1)

    if options.distKey is not None:
        logger.debug("Reading key raster")
        try:
            key.read(keyRastPath)
        except:
            logger.error("keyRaster does not exist")
            sys.exit(1)
    else:
        keydef.assign(regdef)
        keydef.data = numpy.ones(keyRast.data.shape)
        logger.debug("No distribution grid specified, using uniform distribution")
    key.nodataToZero()

    regstat = dataTable.DataTable()
    try:
        logger.debug("Reading regional statistics")
        regstat.read(regStatPath, units=True)
    except:
        logger.error("Could not read regional totals")
        sys.exit(1)

    logger.debug("Finished reading data!")

    if regdef.xll != key.xll or regdef.yll != key.yll:
        logger.error("The raster domain size is differs between key raster and region raster")
        sys.exit(1)

    # Makes sure that the input rasters have the same cellsize, if not - fix it
    if key.cellsize < regdef.cellsize:
        regdef = regdef.resample(key.cellsize, keepTotal=False)
    else:
        if regdef.cellsize < key.cellsize:
            key = key.resample(regdef.cellsize, keepTotal=True)

    # create a list of IDs from the regional totals (exclude the National total)
    try:
        gcInd = regstat.colIndex("Geocode")
    except ValueError:
        logger.error("No column with the title Geocode in regional statistics")
        sys.exit(1)

    try:
        colInd = regstat.colIndex(options.colName)
    except ValueError:
        logger.error("No column with the title %s in regional statistics" % options.colName)
        sys.exit(1)

    # create a list of IDs in region raster, remove nodata-marker from the list
    defIds = regdef.unique()
    if regdef.nodata in defIds:
        defIds.remove(regdef.nodata)

    codesMissingInRegdef = False
    codesMissingInRegstat = False
    totFractionList = []

    # Calculate the sum of emissions in all regions, for comparison with national total...
    regsum = 0
    for row in regstat.data:
        regTot = float(row[colInd])
        if regTot != None:
            regsum += regTot

    logger.debug("Sum of all gc column %s: %f" % (options.colName, regsum))

    statIds = []
    defErrMsg = "Geocodes missing in regdef: "
    statErrMsg = "Geocodes missing in regstat: "
    for row in regstat.data:
        gc = int(row[gcInd])
        statIds.append(gc)
        if gc not in defIds:
            codesMissingInRegdef = True
            defErrMsg += ", " + str(gc)
        emis = row[colInd]

        if emis is None:
            emis = 0
        else:
            emis = float(emis)

        if regsum == 0:
            logger.error("No regional statistics for substance: " + subst)
            sys.exit(1)
        emisFrac = emis / regsum
        logger.debug("Weighting gc: %i, value: %f, weight: %f" % (gc, emis, emisFrac))
        totFractionList.append((gc, emisFrac))

    for gc in defIds:
        if gc not in statIds:
            statErrMsg += ", " + str(gc)
            codesMissingInRegstat = True

    if codesMissingInRegstat or codesMissingInRegdef:
        if codesMissingInRegstat:
            logger.error(statErrMsg)
        if codesMissingInRegdef:
            logger.error(defErrMsg)
        sys.exit(1)

    # create a new raster with the region ID:s replaced by the regions fraction
    # of the national total emission
    totFractionRast = regdef.replace(totFractionList)
    totFractionRast.nodataToZero()
    # For all regions, calculate the regional sum, set the key values to key/regSum
    resKeyArray = numpy.zeros(key.data.shape)

    for gc in defIds:
        # regionalizing key
        mask = numpy.array(regdef.data == gc)
        regKey = mask * key.data
        regSum = numpy.sum(numpy.sum(regKey))
        if regSum > 0:
            resKeyArray = resKeyArray + regKey / regSum
        else:
            logger.warning("Distribution key is zero for geocode %i in regdef, using uniform distribution" % gc)
            resKeyArray = resKeyArray + mask / numpy.sum(numpy.sum(mask))

    key.data = resKeyArray
    key = totFractionRast * key
    key = key / key.sum()

    logger.info("Regionalized key")
    if options.emis:
        totEmis = regstat.sumCol(options.colName)
        emisRast = key * totEmis
        emisRast.write(resRastPath)
        logger.info("Wrote weighted and distributed emission grid")
    else:
        logger.info("Wrote weighted distribution grid")
        key.write(resRastPath)