コード例 #1
0
def main(option_group=None, args=None):

    if option_group is None:
        option_group = restart_run.RestartRunOptionGroup()
    parser = option_group.parser
    if args is None:
        options, args = option_group.parse()
    else: 
        options, _args = option_group.parse()
    run_manager = RunManager(option_group.get_services_database_configuration(options))
    run_as_multiprocess = not options.run_as_single_process

    if len(args) < 1:
        parser.print_help()
        sys.exit(1)
    run_id = int(args[0])
    year = None
    if len(args) > 1:
        year = int(args[1])

    # Get configuration from DB and ensure the run directory is mounted.  Note
    # that we pass a dummy value for restart_year because we might not know it
    # yet.  But when we actually restart the run, we will pass the correct
    # year.
    run_resources = run_manager.create_run_resources_from_history(run_id=run_id,
                                                                  restart_year=2010)
    hudson_common.mount_cache_dir(run_resources)

    cache_dir = run_resources['cache_directory']
    if not year:
        # guess the year based on how the cache dir is populated
        years = map(lambda y : int(os.path.basename(y)),
                    glob.glob(os.path.join(cache_dir, "2*")))
        year = max(years)

    end_year = int(options.end_year) if options.end_year is not None else None
    run_manager.restart_run(run_id, 
                            year,
                            options.project_name,
                            end_year=end_year,
                            skip_urbansim=options.skip_urbansim,
                            create_baseyear_cache_if_not_exists=options.create_baseyear_cache_if_not_exists,
                            skip_cache_cleanup=options.skip_cache_cleanup,
                            run_as_multiprocess=run_as_multiprocess
                            )
コード例 #2
0
def main():
    option_group = ReportOptionGroup()
    parser = option_group.parser
    (options, args) = parser.parse_args()

    if not options.xml_configuration or \
           not options.years or \
           not options.output:
        print "ERROR: -x, -y, and -o are required options"
        sys.exit(1)

    # determine the run_id from the command line args
    if options.run_id:
        run_id = options.run_id
    elif options.cache_directory:
        run_id = hudson_common.run_id_from_cache_dir(options.cache_directory)
        if not run_id:
            print "Failed to parse run ID from cache directory name"
            sys.exit(1)
    else:
        # get most recent succesful run from DB
        conn = MySQLdb.connect('paris.urbansim.org', 'hudson', os.getenv('HUDSON_DBPASS'), 'services');
        c = conn.cursor()
        c.execute("SELECT max(date_time), run_id FROM run_activity where status='done'")
        results = c.fetchone()
        run_id = str(results[1])

    # Note the dummy restart_year.  Because we're not actually going to
    # restart the run, this will be ignored.
    run_manager = RunManager(option_group.get_services_database_configuration(options))
    run_resources = run_manager.create_run_resources_from_history(run_id=run_id,
                                                                  restart_year=2010)
    cache_directory = run_resources['cache_directory']
    scenario = run_resources['scenario_name']

    # The cache directory may not exist if this script is being run on a hudson
    # slave that did not perform the original run.  Ensure that we mount it if
    # nec.
    hudson_common.mount_cache_dir(run_resources)

    # Hudson appends "_hudson" to the scenario names.  Peel this off
    # if it's present.
    scenario = scenario.replace("_hudson", "")

    # prepare the report staging directory
    output_dir = os.path.join(options.output, scenario, "run_" + run_id)
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.makedirs(output_dir)
    f = open(os.path.join(output_dir, "README.txt"), 'w')

    years = eval(options.years)
    text = ""
    if os.getenv("HUDSON_DRAFT_RESULTS") == "true":
        text += "\n".join(textwrap.wrap(
"""NOTICE: This report contains unofficial draft model results that have not
been reviewed or approved by any agency.
""")) + "\n\n"
    text += "\n".join(textwrap.wrap(
"""This report has been auto-generated by urbansim for the following model run:
"""
)) + """
project: bay_area_parcel
scenario: %s
years: %d-%d
cached data: %s
""" % (scenario, years[0], years[-1],
       os.sep.join(cache_directory.split(os.sep)[-3:]))
    comment = os.getenv("HUDSON_COMMENT")
    if comment:
        text += "\n".join(textwrap.wrap("comment: " + comment)) + "\n"
    f.write(text.replace("\n", "\r\n"))
    f.close()

    # prepare the indicators
    shutil.rmtree(os.path.join(output_dir, "indicators"), True)
    for indicator_batch in ["county_indicators", "zone_data", "superdistrict_indicators", "regional_indicators_short","pda_indicators","abag_eir_area_permutation"]:
        urbansim.tools.make_indicators.run(options.xml_configuration,
                                           indicator_batch,
                                           None,
                                           cache_directory,
                                           options.years)

    my_location = os.path.split(__file__)[0]
    shp_path = os.path.join(os.getenv("OPUS_HOME"), "data", "bay_area_parcel", "shapefiles")
    
    ##county-level summary report
    pdf_county_script = os.path.join(my_location, "summarize_county_indicators_map.R")
    cmd = "Rscript %s %s %d %d %s %s %s %s" % (pdf_county_script,
                                   os.path.join(cache_directory, "indicators"),
                                   years[0], years[-1],shp_path,"bayarea_counties.shp", scenario, "county")
    print "Summarizing county indicators: " + cmd
    if os.system(cmd) != 0:
        print "WARNING: Failed to generate county indicators"
       
    ##superdistrict-level summary report --never mind the name of the script; is en route to being generalized   
    pdf_supdist_script = os.path.join(my_location, "summarize_superdistrict_indicators_map.R")
    cmd = "Rscript %s %s %d %d %s %s %s %s" % (pdf_supdist_script,
                                   os.path.join(cache_directory, "indicators"),
                                   years[0], years[-1],shp_path,"superdistricts.shp", scenario, "superdistrict")
    print "Summarizing superdistrict indicators: " + cmd
    if os.system(cmd) != 0:
        print "WARNING: Failed to generate superdistrict indicators"

    
    shutil.copytree(os.path.join(cache_directory, "indicators"),
                    os.path.join(output_dir, "indicators"),
                    ignore=shutil.ignore_patterns("*_stored_data*", "*.log"))

    # add the travel model EMFAC output to the web report
    config = XMLConfiguration(options.xml_configuration).get_run_configuration(scenario)
    travel_model = os.getenv("HUDSON_TRAVEL_MODEL")
    travel_model_for_R ="FALSE"
    if travel_model and travel_model.lower() == "full":
        print "Copying over travel model output"
        travel_model_for_R = "TRUE"
        tm_base_dir = mtc_common.tm_get_base_dir(config)
        tm_dir = os.path.join(tm_base_dir, "runs", cache_directory.split(os.sep)[-1])
        for f in glob.glob(os.path.join(tm_dir, '*', 'emfac', 'output', 'EMFAC2011-SG Summary*Group 1.*')):
            shutil.copy(f, output_dir)
		
    p = os.path.join(cache_directory, "mtc_data")
    if os.path.exists(p):
        shutil.copytree(p, os.path.join(output_dir, "mtc_data"))
    
    #Generate PDA comparison chart
    pda_script = os.path.join(my_location, "pda_compare.R")
    cmd = "Rscript %s %s %d %d %s %s" % (pda_script,
                                   os.path.join(cache_directory, "indicators"),
                                   years[0], years[-1], run_id, scenario)
    print "Creating pda comparison chart: " + cmd
    
    
    #Generate county x PDA facet chart
    pda_county_script = os.path.join(my_location, "growth_by_county_by_pda.R")
    cmd = "Rscript %s %s %d %d %s %s" % (pda_county_script,
                                   os.path.join(cache_directory, "indicators"),
                                   years[0], years[-1], run_id, scenario)
    print "Creating county by pda chart: " + cmd
    
    
    # topsheet--need the EMFAC outputs to be generated first, so this goes last
    #travel_model_for_R = "TRUE" if travel_model else "FALSE" 
    topsheet_script = os.path.join(my_location, "regional_indicators.R")
    cmd = "Rscript %s %s %d %d %s %s %s" % (topsheet_script,
                                   os.path.join(cache_directory, "indicators"),
                                   years[0], years[-1], run_id, travel_model_for_R, scenario)
    print "Creating topsheet: " + cmd
    if os.system(cmd) != 0:
        print "WARNING: Failed to generate indicators"