def run(self, config, year, *args, **kwargs): """Runs the travel model, using appropriate info from config. """ tm_config = config["travel_model_configuration"] self.prepare_for_run(tm_config, year) project_year_dir = get_project_year_dir(tm_config, year) # year_dir = tm_config[year] #'CoreEA0511202006\\urbansim\\2001' # dir_part1,dir_part2 = os.path.split(year_dir) # while dir_part1: # dir_part1, dir_part2 = os.path.split(dir_part1) # project_year_dir = os.path.join(tm_data_dir, dir_part2) #C:/SEMCOG_baseline/CoreEA0511202006 logger.log_status('Start travel model from directory %s for year %d' % (project_year_dir, year)) #for macroname, ui_db_file in tm_config['macro']['run_semcog_travel_model'].iteritems(): #pass macroname, ui_db_file = tm_config['macro']['run_semcog_travel_model'], tm_config['ui_file'] loops = 1 logger.log_status('Running travel model ...') tcwcmd = win32api.GetShortPathName(tm_config['transcad_binary']) os.system('start /B "start TransCAD" %s' % tcwcmd) #start TransCAD in background time.sleep(1) #os.system("%s -a %s -ai '%s'" % (tcwcmd, ui_db_file, macroname)) run_transcad_macro(macroname, ui_db_file, loops) try: pass ##win32process.TerminateProcess(self.hProcess, 0) except: logger.log_warning("The code has problem to terminate the TransCAD it started.")
def get_travel_data_from_travel_model(self, config, year, zone_set, tm_output_file="tm_output.txt", ): """ Extracts a new travel data set from a given set of transcad matrices by calling a pre-specified transcad macro. The columns in the travel data set are those given in matrix_variable_map in travel_model_configuration. """ tm_config = config['travel_model_configuration'] tm_data_dir = os.path.join(tm_config['travel_model_base_directory'], tm_config[year]["data_exchange_dir"]) tm_output_full_name = os.path.join(tm_data_dir, tm_output_file) matrix_attribute_name_map = tm_config['tm_to_urbansim_variable_mapping'] transcad_file_location = run_get_file_location_macro(tm_config) matrices = [] row_index_name, col_index_name = "ZoneID", "ZoneID" #default values if matrix_attribute_name_map.has_key('row_index_name'): row_index_name = matrix_attribute_name_map['row_index_name'] if matrix_attribute_name_map.has_key('col_index_name'): col_index_name = matrix_attribute_name_map['col_index_name'] for key, val in matrix_attribute_name_map.iteritems(): if (key != 'row_index_name') and (key != 'col_index_name'): if val.has_key('row_index_name'): row_index_name = val['row_index_name'] if val.has_key('col_index_name'): col_index_name = val['col_index_name'] matrix_file_name = transcad_file_location[key] #replace internal matrix name with absolute file name matrices.append([matrix_file_name, row_index_name, col_index_name, val.items()]) macro_args =[ ("ExportTo", tm_output_full_name) ] macro_args.append(("Matrix", matrices)) #for macroname, ui_db_file in tm_config['macro']['get_transcad_data_into_cache'].iteritems(): #ui_db_file = os.path.join(tm_config['directory'], ui_db_file) macroname, ui_db_file = tm_config['macro']['get_transcad_data_into_cache'], tm_config['ui_file'] run_transcad_macro(macroname, ui_db_file, macro_args) table_name = "travel_data" data_dict = self._read_macro_output_file(tm_output_full_name) #data_dict = self._seq_taz_to_zone_conversion(zone_set, data_dict) storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name=table_name, table_data=data_dict ) travel_data_set = TravelDataDataset(in_storage=storage, in_table_name=table_name) travel_data_set.size() return travel_data_set
def _update_travel_model_data_file(self, tm_config, data, header, input_file, datatable, joinfield, delimiter): self._delete_dcc_file( os.path.splitext(input_file)[0] + '.dcc' ) self._write_to_txt_file(data, header, input_file, delimiter) transcad_file_location = run_get_file_location_macro(tm_config) datatable = transcad_file_location[datatable] #replace internal matrix name with absolute file name macro_args = [["InputFile", input_file], ["DataTable", datatable], ["JoinField", joinfield] ] #for macroname in tm_config['macro']['get_cache_data_into_transcad'].keys(): #ui_db_file = tm_config['macro']['get_cache_data_into_transcad'][macroname] #ui_db_file = os.path.join(tm_config['directory'], ui_db_file) macroname, ui_db_file = tm_config['macro']['get_cache_data_into_transcad'] run_transcad_macro(macroname, ui_db_file, macro_args)
def get_travel_data_from_travel_model( self, config, year, zone_set, tm_output_file="tm_output.txt", ): """ Returns a new travel data set from a given set of transcad matrices populated by a specified transcad macro. The columns in the travel data set are those given in matrix_variable_map in travel_model_configuration. """ tm_config = config['travel_model_configuration'] tm_data_dir = os.path.join(tm_config['directory'], tm_config[year]) tm_output_full_name = os.path.join(tm_data_dir, tm_output_file) matrix_attribute_name_map = tm_config[ 'tm_to_urbansim_variable_mapping'] transcad_file_location = run_get_file_location_macro(tm_config) for matrix in matrix_attribute_name_map: matrix[0] = transcad_file_location[matrix[ 0]] #replace internal matrix name with absolute file name macro_args = [("ExportTo", tm_output_full_name)] macro_args.append(("Matrix", matrix_attribute_name_map)) #for macroname, ui_db_file in tm_config['macro']['get_transcad_data_into_cache'].iteritems(): #ui_db_file = os.path.join(tm_config['directory'], ui_db_file) macroname, ui_db_file = tm_config['macro'][ 'get_transcad_data_into_cache'] run_transcad_macro(macroname, ui_db_file, macro_args) table_name = "travel_data" data_dict = self._read_macro_output_file(tm_output_full_name) data_dict = self._seq_taz_to_zone_conversion(zone_set, data_dict) storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name=table_name, table_data=data_dict) travel_data_set = TravelDataDataset(in_storage=storage, in_table_name=table_name) travel_data_set.size() return travel_data_set
def _update_travel_model_data_file(self, config, data, header, filepath, datatable, #name of travel model TAZ Data Table joinfield, delimiter='\t', *args, **kwargs): self._delete_dcc_file( os.path.splitext(filepath)[0] + '.dcc' ) self._write_to_txt_file(data, header, filepath, delimiter) transcad_file_location = run_get_file_location_macro(config) datatable = transcad_file_location[datatable] #replace internal matrix name with absolute file name macro_args = [["InputFile", filepath], ["DataTable", datatable], ["JoinField", joinfield] ] macroname, ui_db_file = config['macro']['get_cache_data_into_transcad'], config['ui_file'] run_transcad_macro(macroname, ui_db_file, macro_args)
def get_travel_data_from_travel_model(self, config, year, zone_set, tm_output_file="tm_output.txt", ): """ Returns a new travel data set from a given set of transcad matrices populated by a specified transcad macro. The columns in the travel data set are those given in matrix_variable_map in travel_model_configuration. """ tm_config = config['travel_model_configuration'] tm_data_dir = os.path.join(tm_config['directory'], tm_config[year]) tm_output_full_name = os.path.join(tm_data_dir, tm_output_file) matrix_attribute_name_map = tm_config['tm_to_urbansim_variable_mapping'] transcad_file_location = run_get_file_location_macro(tm_config) for matrix in matrix_attribute_name_map: matrix[0] = transcad_file_location[matrix[0]] #replace internal matrix name with absolute file name macro_args =[ ("ExportTo", tm_output_full_name) ] macro_args.append(("Matrix", matrix_attribute_name_map)) #for macroname, ui_db_file in tm_config['macro']['get_transcad_data_into_cache'].iteritems(): #ui_db_file = os.path.join(tm_config['directory'], ui_db_file) macroname, ui_db_file = tm_config['macro']['get_transcad_data_into_cache'] run_transcad_macro(macroname, ui_db_file, macro_args) table_name = "travel_data" data_dict = self._read_macro_output_file(tm_output_full_name) data_dict = self._seq_taz_to_zone_conversion(zone_set, data_dict) storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name=table_name, table_data=data_dict ) travel_data_set = TravelDataDataset(in_storage=storage, in_table_name=table_name) travel_data_set.size() return travel_data_set