コード例 #1
0
def main():
    #-----------Setting up and unsing option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    

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

    parser.add_option("-p","--pattern",
                      action="store",dest="pattern",
                      default=r".+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec).+",
                      help="Regular expression to extract month from filename")

    parser.add_option("-o","--outfile",
                      action="store",dest="outfile",
                      help="Output file for sum of montly emission rasters")                      

    
    (options, args) = parser.parse_args()

    #--------------------Init logger-----------------------
    rootLogger = logger.RootLogger(level=options.loglevel)
    global log
    log = rootLogger.getStreamLogger(sys.argv[0])
        
    #-----------------Validating options-------------------
    if len(args)!=0:
        log.error("No arguments wanted, only options")
        
    pat=re.compile(options.pattern)
    files=glob.glob(options.infiles)
    yearRast=None
    
    for f in files:
        monthMatch=pat.match(f)
        if monthMatch is None:
            continue
        month= monthMatch.group(1)
        rast=Raster()
        try:
            rast.read(f)
        except:
            rast=res2rast(f)
            
        cellsPerKm2=1.0e6/pow(rast.cellsize,2)
        rast=rast/float(cellsPerKm2)
        if yearRast is None:
            yearRast= rast/12.0
        else:
            yearRast+= rast/12.0
            
        monthSum=rast.sum()/12.0
        print "%s, %s: %f" %(f,month,monthSum)

    if options.outfile is not None:
        yearRast.write(options.outfile)