Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/exports.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file exports.py15# @author Joerg Schweizer16# @date 2012171819import os20import sys21import types22import numpy as np23import agilepy.lib_base.classman as cm24import agilepy.lib_base.arrayman as am252627from agilepy.lib_base.processes import Process2829#global IS_EXCEL3031try:32from openpyxl import Workbook3334IS_EXCEL = True353637except:38print 'WARNING: No Exel export possible. Install openpyxl python package.'39IS_EXCEL = False4041# if 'Workbook' in dir():42# print 'detected Exel'43# IS_EXCEL = True44# else:45# IS_EXCEL = False4647print 'IS_EXCEL', IS_EXCEL484950def export_excel(filepath, obj, ids=None, attrconfigs=None, groupnames=None,51is_header=True, is_ident=False, is_timestamp=True,52show_parentesis=True, name_id='ID', is_export_not_save=True):5354print 'export_excel' # ,attrconfigs,'groupnames',groupnames,5556wb = Workbook()57if (wb.worksheets) >= 1:58ws = wb.worksheets[0]59ws.title = "Scalar attributes"60else:61ws = wb.create_sheet(title="Scalar attributes")6263attrsman = obj.get_attrsman()64if attrconfigs is None:65attrconfigs = attrsman.get_configs(is_all=False, filtergroupnames=groupnames)66#attrconfigs_cols = attrsman.get_configs(is_all = False, structs = cm.STRUCTS_COL, filtergroupnames = groupnames)67ind_row = 16869for attrconf in attrconfigs:70# print ' attrconfig', attrconf.attrname,attrconf.struct71if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in cm.STRUCTS_SCALAR):72# print ' attrconf.attrname',attrconf.attrname,'val',attrconf.get_value()73cell = ws.cell(row=ind_row, column=1)74cell.value = attrconf.attrname+attrconf.format_unit(show_parentesis)75cell = ws.cell(row=ind_row, column=2)76value = attrconf.get_value()77tv = type(value)78if tv in cm.NODATATYPES:79value = str(value)80elif tv in (types.ListType, types.TupleType, np.ndarray):81value = str(value)8283cell.value = value84#d.value = 3.1485# print " cell",ind_row,'value',cell.value86ind_row += 18788# check if attributes are all column attribute and indicted for save8990ws = wb.create_sheet(title="Table attributes")91if hasattr(obj, 'get_ids'):92if ids is None:93ids = obj.get_ids()9495attrconfigs_checked = []96for attrconf in attrconfigs:97if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in cm.STRUCTS_COL):98attrconfigs_checked.append(attrconf)99100# first table row101ind_row = 1102ind_col = 1103cell = ws.cell(row=ind_row, column=ind_col)104cell.value = name_id105106for attrconf in attrconfigs_checked:107108cell = ws.cell(row=ind_row, column=ind_col)109cell.value = attrconf.format_symbol(show_parentesis=show_parentesis)110ind_col += 1111112# rest113for _id in ids:114ind_row += 1115ind_col = 1116cell = ws.cell(row=ind_row, column=ind_col)117cell.value = _id118119for attrconf in attrconfigs_checked:120121cell = ws.cell(row=ind_row, column=ind_col)122value = attrconf[_id]123# print ' attrconfig', attrconf.attrname,type(value)124tv = type(value)125mt = attrconf.metatype126if tv in cm.NODATATYPES:127value = str(value)128129elif mt == 'id':130value = attrconf.get_linktab().format_ids([value])131elif tv in (types.ListType, types.TupleType, np.ndarray):132value = str(value)133cell.value = value134ind_col += 1135136wb.save(filepath)137138139class CsvExporter(Process):140def __init__(self, obj, ident='csvexporter', name='CSV exporter',141info='Export data from a CSV file into object',142logger=None, **kwargs):143print 'CsvExporter.__init__'144self._init_common(ident,145parent=obj,146name=name,147logger=logger,148info=info,149)150151attrsman = self.set_attrsman(cm.Attrsman(self))152self.csvfilepath = attrsman.add(cm.AttrConf('csvfilepath', kwargs.get('csvfilepath', ''),153groupnames=['options'],154perm='rw',155name='CSV file',156wildcards='CSV file (*.csv)|*.csv|*.CSV',157metatype='filepath',158info="CSV plain text file path.",159))160161self.sep = attrsman.add(cm.AttrConf('sep', kwargs.get('sep', ","),162groupnames=['options', ],163perm='rw',164name='Separator',165info="""Seperator used in SCV file. Exampe: ; , <space key>""",166))167168self.is_header = attrsman.add(cm.AttrConf('is_header', kwargs.get('is_header', True),169groupnames=['options', ],170perm='rw',171name='Make header',172info="""Make header with date and time.""",173))174175self.show_parentesis = attrsman.add(cm.AttrConf('show_parentesis', kwargs.get('show_parentesis', True),176groupnames=['options', ],177perm='rw',178name='Show units in parenthesis',179info="""Show units (if any) in parenthesis.""",180))181# self.name_id = attrsman.add(cm.AttrConf('name_id', kwargs.get('name_id',"ID"),182# groupnames = ['options',],183# perm='rw',184# name = 'ID string',185# info = """String to be used to indicate IDs""",186# ))187188def do(self):189print 'CsvExporter.do',190obj = self.parent191attrsman = self.get_attrsman()192sep = self.sep193#logger = self._logger194195obj.export_csv(self.csvfilepath, sep=self.sep,196#ids = None,197attrconfigs=None,198groupnames=None,199show_parentesis=self.show_parentesis,200is_export_not_save=True, # export attr, also non save201# name_id='ID',202is_header=self.is_header)203204205