docs: def results_to_oedb(session, network, args, grid='hv', safe_results=False):
      """Return results obtained from PyPSA to oedb
  
      Parameters
      ----------
      session: 
      network : PyPSA network container
          Holds topology of grid including results from powerflow analysis
      args: dict
          Settings from appl.py
      grid: str
          Choose voltage-level, currently only 'hv' implemented
      safe_results: boolean
          If it is set to 'True' the result set will be saved
          to the versioned grid schema eventually apart from
          being saved to the model_draft by a SQL-script.
          ONLY set to True if you know what you are doing.
  
      """
      # Update generator_ids when k_means clustering to get integer ids
      if args['network_clustering_kmeans'] != False:
          new_index=pd.DataFrame(index = network.generators.index)
          new_index['new']=range(len(network.generators))
  
          for col in (network.generators_t):
              if not network.generators_t[col].empty:
                  network.generators_t[col].columns =\
                      new_index.new[network.generators_t[col].columns]
  
          network.generators.index = range(len(network.generators))
      
      # moved this here to prevent error when not using the mv-schema
      import datetime
      if grid.lower() == 'mv':
          print('MV currently not implemented')
      elif grid.lower() == 'hv':
          from egoio.db_tables.model_draft import\
              EgoGridPfHvResultBus as BusResult,\
              EgoGridPfHvResultBusT as BusTResult,\
              EgoGridPfHvResultStorage as StorageResult,\
              EgoGridPfHvResultStorageT as StorageTResult,\
              EgoGridPfHvResultGenerator as GeneratorResult,\
              EgoGridPfHvResultGeneratorT as GeneratorTResult,\
              EgoGridPfHvResultLine as LineResult,\
              EgoGridPfHvResultLineT as LineTResult,\
              EgoGridPfHvResultLoad as LoadResult,\
              EgoGridPfHvResultLoadT as LoadTResult,\
              EgoGridPfHvResultTransformer as TransformerResult,\
              EgoGridPfHvResultTransformerT as TransformerTResult,\
              EgoGridPfHvResultMeta as ResultMeta,\
              EgoGridPfHvSource as Source
      else:
          print('Please enter mv or hv!')
  
      print('Uploading results to db...')
      # get last result id and get new one
      last_res_id = session.query(func.max(ResultMeta.result_id)).scalar()
      if last_res_id == None:
          new_res_id = 1
      else:
          new_res_id = last_res_id + 1
  
      # result meta data
      res_meta = ResultMeta()
      meta_misc = []
      for arg, value in args.items():
          if arg not in dir(res_meta) and arg not in ['db', 'lpfile',
                                                       'results', 'export']:
              meta_misc.append([arg, str(value)])
  
      res_meta.result_id = new_res_id
      res_meta.scn_name = args['scn_name']
      res_meta.calc_date = datetime.datetime.now()
      res_meta.user_name = args['user_name']
      res_meta.method = args['method']
      res_meta.start_snapshot = args['start_snapshot']
      res_meta.end_snapshot = args['end_snapshot']
      res_meta.safe_results = safe_results
      res_meta.snapshots = network.snapshots.tolist()
      res_meta.solver = args['solver']
      res_meta.settings = meta_misc
  
      session.add(res_meta)
      session.commit()
  
      # get source_id
      sources = pd.read_sql(session.query(Source).statement, session.bind)
      for gen in network.generators.index:
          if network.generators.carrier[gen] not in sources.name.values:
              new_source = Source()
              new_source.source_id = session.query(
                  func.max(Source.source_id)).scalar()+1
              new_source.name = network.generators.carrier[gen]
              session.add(new_source)
              session.commit()
              sources = pd.read_sql(
                      session.query(Source).statement, session.bind)
          try:
              old_source_id = int(
                  sources.source_id[
                          sources.name == network.generators.carrier[gen]])
              network.generators.set_value(gen, 'source', int(old_source_id))
          except:
              print(
                  'Source ' + network.generators.carrier[gen] + 
                  ' is not in the source table!')
      for stor in network.storage_units.index:
          if network.storage_units.carrier[stor] not in sources.name.values:
              new_source = Source()
              new_source.source_id = session.query(
                  func.max(Source.source_id)).scalar()+1
              new_source.name = network.storage_units.carrier[stor]
              session.add(new_source)
              session.commit()
              sources = pd.read_sql(
                      session.query(Source).statement, session.bind)
          try:
              old_source_id = int(
                  sources.source_id[
                          sources.name == network.storage_units.carrier[stor]])
              network.storage_units.set_value(stor, 'source', int(old_source_id))
          except:
              print(
                  'Source ' + network.storage_units.carrier[stor] +
                  ' is not in the source table!')
  
      whereismyindex = {BusResult: network.buses.index,
                        LoadResult: network.loads.index,
                        LineResult: network.lines.index,
                        TransformerResult: network.transformers.index,
                        StorageResult: network.storage_units.index,
                        GeneratorResult: network.generators.index,
                        BusTResult: network.buses.index,
                        LoadTResult: network.loads.index,
                        LineTResult: network.lines.index,
                        TransformerTResult: network.transformers.index,
                        StorageTResult: network.storage_units.index,
                        GeneratorTResult: network.generators.index}
  
      whereismydata = {BusResult: network.buses,
                       LoadResult: network.loads,
                       LineResult: network.lines,
                       TransformerResult: network.transformers,
                       StorageResult: network.storage_units,
                       GeneratorResult: network.generators,
                       BusTResult: network.buses_t,
                       LoadTResult: network.loads_t,
                       LineTResult: network.lines_t,
                       TransformerTResult: network.transformers_t,
                       StorageTResult: network.storage_units_t,
                       GeneratorTResult: network.generators_t}
  
      new_to_old_name = {'p_min_pu_fixed': 'p_min_pu',
                         'p_max_pu_fixed': 'p_max_pu',
                         'dispatch': 'former_dispatch',
                         'current_type': 'carrier',
                         'soc_cyclic': 'cyclic_state_of_charge',
                         'soc_initial': 'state_of_charge_initial'}
  
      ormclasses = [BusResult, LoadResult, LineResult, TransformerResult, 
                    GeneratorResult, StorageResult, BusTResult, LoadTResult, 
                    LineTResult, TransformerTResult, GeneratorTResult, 
                    StorageTResult]
  
      for ormclass in ormclasses:
          for index in whereismyindex[ormclass]:
              myinstance = ormclass()
              columns = ormclass.__table__.columns.keys()
              columns.remove('result_id')
              myinstance.result_id = new_res_id
              for col in columns:
                  if '_id' in col:
                      class_id_name = col
                  else:
                      continue
              setattr(myinstance, class_id_name, index)
              columns.remove(class_id_name)
  
              if str(ormclass)[:-2].endswith('T'):
                  for col in columns:
                      if col == 'soc_set':
                          try:
                              setattr(myinstance, col, getattr(
                                  whereismydata[ormclass], 
                                  'state_of_charge_set')[index].tolist())
                          except:
                              pass
                      else:
                          try:
                              setattr(myinstance, col, getattr(
                                  whereismydata[ormclass], col)[index].tolist())
                          except:
                              pass
                  session.add(myinstance)
  
              else:
                  for col in columns:
                      if col in new_to_old_name:
                          if col == 'soc_cyclic':
                              try:
                                  setattr(myinstance, col, bool(
                                      whereismydata[ormclass].loc[index,
                                                   new_to_old_name[col]]))
                              except:
                                  pass
                          elif 'Storage' in str(ormclass) and col == 'dispatch':
                              try:
                                  setattr(myinstance, col,
                                          whereismydata[ormclass].loc[index, col])
                              except:
                                  pass
                          else:
                              try:
                                  setattr(
                                      myinstance, col, whereismydata[ormclass].\
                                      loc[index, new_to_old_name[col]])
                              except:
                                  pass
                      elif col in ['s_nom_extendable', 'p_nom_extendable']:
                          try:
                              setattr(myinstance, col, bool(
                                  whereismydata[ormclass].loc[index, col]))
                          except:
                              pass
                      else:
                          try:
                              setattr(myinstance, col,
                                      whereismydata[ormclass].loc[index, col])
                          except:
                              pass
                  session.add(myinstance)
  
          session.commit()
      print('Upload finished!')
  
      return