def start_of_year(year, ps=_ps): dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, '^' + str(year)) syntar_f = '{}\\PopSynTargets.csv'.format(year) if not (os.path.exists(syntar_f)): cmd = 'copy /Y AllYears\\Working\\PopulationSynthesis\\PopSynTargets.csv {}\\PopSynTargets.csv'.format( year) print(cmd) os.system(cmd) skimyear = pr.get_skim_year(year, ps.skimyears) if skimyear < year: skimfilename = ps.skim_fname.format(yr=skimyear) if skimyear < ps.earliest_squeeze_year: dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, str(skimyear) + '_' + skimfilename[0:-4]) if year == ps.baseyear: prev_year = year - 1 dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, '^' + str(prev_year)) if year == 2013: y13_smain_input = '..\\..\\Supply\\data\\supply_input_{}.csv'.format( year) dc.dump_a_table(lambda: pr.connect_to_aa(ps), 'srf', '_supply_input', y13_smain_input)
def start_of_year(year,ps=_ps): dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, str(year)) skimyear = pr.get_skim_year(year, ps.skimyears) skimfilename = ps.skim_fname.format(yr=skimyear) dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, str(skimyear)+'_'+skimfilename[0:-4]) if year==ps.baseyear: prev_year=year-1 dc.dump_pg_tbls(lambda: pr.connect_to_aa(ps), ps.scendir, ps.aa_schema, str(prev_year))
def get_args_and_run(): pr.set_up_logging() args = sys.argv + ["", ""] print("Args are" + str(args)) _year = int(args[1]) if args[2] and args[2] != "--rerun": main_ps_name = args[2] main_ps = importlib.import_module(main_ps_name) else: import aa_settings as main_ps have_org = False if args[2] == "--rerun" or args[3] == "--rerun": logging.info("Rerunning AA in " + str(_year) + " with solved prices from ExchangeResults.csv") have_org = True try: shutil.move( main_ps.scendir + "/" + str(_year) + "/ExchangeResultsI.csv", main_ps.scendir + "/" + str(_year) + "/ExchangeResultsI-orig.csv") except FileNotFoundError: have_org = False shutil.copyfile( main_ps.scendir + "/" + str(_year) + "/ExchangeResults.csv", main_ps.scendir + "/" + str(_year) + "/ExchangeResultsI.csv") _skimyear = pr.get_skim_year(_year, main_ps.skimyears) skimfilename = main_ps.skim_fname.format(yr=_skimyear) aa = StandardAARunner() aa.popsyn.start() try: logging.info("Running aa in year " + str(_year)) # noinspection PyTypeChecker aa.run_aa(main_ps, _year, skimfilename, _skimyear, dbyear=None, load=False) if have_org: shutil.move( main_ps.scendir + "/" + str(_year) + "/ExchangeResultsI-orig.csv", main_ps.scendir + "/" + str(_year) + "/ExchangeResultsI.csv") finally: aa.popsyn.stop()
def main_impl(ps, aa_runner): logging.info( "Recreate the inputs and yearly folder structure by downloading data from pg" ) import os #cmd = 'python dump2csv.py' #os.system(cmd) logging.info("**********************") logging.info("Starting PECAS Run in " + ps.scendir + ". scenario " + ps.scenario + ", AA schema " + ps.aa_schema) if ps.travel_model_input_years: aa_runner.popsyn.start() if ps.resume_run: try: year = read_resume() except (IOError, ValueError): logging.warning( "No valid resume file; exiting; you can set resume_run to false if you want to start over" ) raise else: year = ps.baseyear converted_skims = set() project_code.before_run(ps=ps) # ------------------------------------------------------------------------------------------------------------------ # Main loop # ------------------------------------------------------------------------------------------------------------------ while year <= ps.stopyear: write_resume(ps, year) skimyear = pr.get_skim_year(year, ps.skimyears) skimfilename = ps.skim_fname.format(yr=skimyear) project_code.start_of_year(year, ps=ps) pre_check(ps, aa_runner, year) if year > 2012 and ps.run_supply: project_code.before_aa(year, ps=ps) # -------------------------------------------------------------------------------------------------------------- # AA module # -------------------------------------------------------------------------------------------------------------- if year >= ps.aa_startyear: if ps.squeeze_skims and skimyear >= ps.earliest_squeeze_year and skimyear not in converted_skims: #skims.main(skimyear, ps) cmd = 'python skims_to_sem.py ' + str(skimyear) os.system(cmd) converted_skims.add(skimyear) if year in ps.aayears: aa_runner.run_aa(ps, year, str(skimfilename), skimyear) else: if year == ps.baseyear: logging.error( "You need to run AA in the base year, check aayears!") raise ValueError else: # need to copy all of the stuff from last year for file in aa.output_file_list: try: shutil.copyfile( ps.scendir + "/" + str(year - 1) + "/" + file, ps.scendir + "/" + str(year) + "/" + file) except IOError: logging.warning("Couldn't copy " + str(file) + " from year " + str(year - 1) + " to year " + str(year)) if ps.travel_model_input_years: aa_runner.popsyn.push(None) logging.info( "Waiting for the employment/population synthesizer thread") aa_runner.popsyn.join() logging.info("Employment/population synthesizer thread finished") if ps.run_demand: project_code.after_aa(year, ps=ps) year = year + 1