def import_generators(self, session, debug=False):
        """
        Imports renewable (res) and conventional (conv) generators

        Args:
            session : sqlalchemy.orm.session.Session
                Database session
            debug: If True, information is printed during process
        Notes:
            Connection of generators is done later on in NetworkDing0's method connect_generators()
        """

        def import_res_generators():
            """Imports renewable (res) generators"""

            # build query
            generators_sqla = session.query(
                self.orm['orm_re_generators'].columns.id,
                self.orm['orm_re_generators'].columns.subst_id,
                self.orm['orm_re_generators'].columns.la_id,
                self.orm['orm_re_generators'].columns.mvlv_subst_id,
                self.orm['orm_re_generators'].columns.electrical_capacity,
                self.orm['orm_re_generators'].columns.generation_type,
                self.orm['orm_re_generators'].columns.generation_subtype,
                self.orm['orm_re_generators'].columns.voltage_level,
                self.orm['orm_re_generators'].columns.w_id,
                func.ST_AsText(func.ST_Transform(
                    self.orm['orm_re_generators'].columns.rea_geom_new, srid)).label('geom_new'),
                func.ST_AsText(func.ST_Transform(
                    self.orm['orm_re_generators'].columns.geom, srid)).label('geom')
            ). \
                filter(
                self.orm['orm_re_generators'].columns.subst_id.in_(list(mv_grid_districts_dict))). \
                filter(self.orm['orm_re_generators'].columns.voltage_level.in_([4, 5, 6, 7])). \
                filter(self.orm['version_condition_re'])

            # read data from db
            generators = pd.read_sql_query(generators_sqla.statement,
                                           session.bind,
                                           index_col='id')
            # define generators with unknown subtype as 'unknown'
            generators.loc[generators[
                               'generation_subtype'].isnull(),
                           'generation_subtype'] = 'unknown'

            for id_db, row in generators.iterrows():

                # treat generators' geom:
                # use geom_new (relocated genos from data processing)
                # otherwise use original geom from EnergyMap
                if row['geom_new']:
                    geo_data = wkt_loads(row['geom_new'])
                elif not row['geom_new']:
                    geo_data = wkt_loads(row['geom'])
                    logger.warning(
                        'Generator {} has no geom_new entry,'
                        'EnergyMap\'s geom entry will be used.'.format(
                        id_db))
                # if no geom is available at all, skip generator
                elif not row['geom']:
                    #geo_data =
                    logger.error('Generator {} has no geom entry either'
                                 'and will be skipped.'.format(id_db))
                    continue

                # look up MV grid
                mv_grid_district_id = row['subst_id']
                mv_grid = mv_grid_districts_dict[mv_grid_district_id].mv_grid

                # create generator object
                if row['generation_type'] in ['solar', 'wind']:
                    generator = GeneratorFluctuatingDing0(
                        id_db=id_db,
                        mv_grid=mv_grid,
                        capacity=row['electrical_capacity'],
                        type=row['generation_type'],
                        subtype=row['generation_subtype'],
                        v_level=int(row['voltage_level']),
                        weather_cell_id=row['w_id'])
                else:
                    generator = GeneratorDing0(
                        id_db=id_db,
                        mv_grid=mv_grid,
                        capacity=row['electrical_capacity'],
                        type=row['generation_type'],
                        subtype=row['generation_subtype'],
                        v_level=int(row['voltage_level']))

                # MV generators
                if generator.v_level in [4, 5]:
                    generator.geo_data = geo_data
                    mv_grid.add_generator(generator)

                # LV generators
                elif generator.v_level in [6, 7]:

                    # look up MV-LV substation id
                    mvlv_subst_id = row['mvlv_subst_id']

                    # if there's a LVGD id
                    if mvlv_subst_id and not isnan(mvlv_subst_id):
                        # assume that given LA exists
                        try:
                            # get LVGD
                            lv_station = lv_stations_dict[mvlv_subst_id]
                            lv_grid_district = lv_station.grid.grid_district
                            generator.lv_grid = lv_station.grid

                            # set geom (use original from db)
                            generator.geo_data = geo_data

                        # if LA/LVGD does not exist, choose random LVGD and move generator to station of LVGD
                        # this occurs due to exclusion of LA with peak load < 1kW
                        except:
                            lv_grid_district = random.choice(list(lv_grid_districts_dict.values()))

                            generator.lv_grid = lv_grid_district.lv_grid
                            generator.geo_data = lv_grid_district.lv_grid.station().geo_data

                            logger.warning('Generator {} cannot be assigned to '
                                           'non-existent LV Grid District and was '
                                           'allocated to a random LV Grid District ({}).'.format(
                                            repr(generator), repr(lv_grid_district)))
                            pass

                    else:
                        lv_grid_district = random.choice(list(lv_grid_districts_dict.values()))

                        generator.lv_grid = lv_grid_district.lv_grid
                        generator.geo_data = lv_grid_district.lv_grid.station().geo_data

                        logger.warning('Generator {} has no la_id and was '
                                       'assigned to a random LV Grid District ({}).'.format(
                                        repr(generator), repr(lv_grid_district)))

                    generator.lv_load_area = lv_grid_district.lv_load_area
                    lv_grid_district.lv_grid.add_generator(generator)

        def import_conv_generators():
            """Imports conventional (conv) generators"""

            # build query
            generators_sqla = session.query(
                self.orm['orm_conv_generators'].columns.id,
                self.orm['orm_conv_generators'].columns.subst_id,
                self.orm['orm_conv_generators'].columns.name,
                self.orm['orm_conv_generators'].columns.capacity,
                self.orm['orm_conv_generators'].columns.fuel,
                self.orm['orm_conv_generators'].columns.voltage_level,
                func.ST_AsText(func.ST_Transform(
                    self.orm['orm_conv_generators'].columns.geom, srid)).label('geom')). \
                filter(
                self.orm['orm_conv_generators'].columns.subst_id.in_(list(mv_grid_districts_dict))). \
                filter(self.orm['orm_conv_generators'].columns.voltage_level.in_([4, 5, 6])). \
                filter(self.orm['version_condition_conv'])

            # read data from db
            generators = pd.read_sql_query(generators_sqla.statement,
                                           session.bind,
                                           index_col='id')

            for id_db, row in generators.iterrows():

                # look up MV grid
                mv_grid_district_id = row['subst_id']
                mv_grid = mv_grid_districts_dict[mv_grid_district_id].mv_grid

                # create generator object
                generator = GeneratorDing0(id_db=id_db,
                                           name=row['name'],
                                           geo_data=wkt_loads(row['geom']),
                                           mv_grid=mv_grid,
                                           capacity=row['capacity'],
                                           type=row['fuel'],
                                           subtype='unknown',
                                           v_level=int(row['voltage_level']))

                # add generators to graph
                if generator.v_level in [4, 5]:
                    mv_grid.add_generator(generator)
                # there's only one conv. geno with v_level=6 -> connect to MV grid
                elif generator.v_level in [6]:
                    generator.v_level = 5
                    mv_grid.add_generator(generator)

        # get ding0s' standard CRS (SRID)
        srid = str(int(cfg_ding0.get('geo', 'srid')))

        # get predefined random seed and initialize random generator
        seed = int(cfg_ding0.get('random', 'seed'))
        random.seed(a=seed)

        # build dicts to map MV grid district and Load Area ids to related objects
        mv_grid_districts_dict,\
        lv_load_areas_dict,\
        lv_grid_districts_dict,\
        lv_stations_dict = self.get_mvgd_lvla_lvgd_obj_from_id()

        # import renewable generators
        import_res_generators()

        # import conventional generators
        import_conv_generators()

        logger.info('=====> Generators imported')
def import_generators ( self , session , debug = False ) : def import_res_generators ( ) : generators_sqla = session . query ( self . orm [ 'orm_re_generators' ] . columns . id , self . orm [ 'orm_re_generators' ] . columns . subst_id , self . orm [ 'orm_re_generators' ] . columns . la_id , self . orm [ 'orm_re_generators' ] . columns . mvlv_subst_id , self . orm [ 'orm_re_generators' ] . columns . electrical_capacity , self . orm [ 'orm_re_generators' ] . columns . generation_type , self . orm [ 'orm_re_generators' ] . columns . generation_subtype , self . orm [ 'orm_re_generators' ] . columns . voltage_level , self . orm [ 'orm_re_generators' ] . columns . w_id , func . ST_AsText ( func . ST_Transform ( self . orm [ 'orm_re_generators' ] . columns . rea_geom_new , srid ) ) . label ( 'geom_new' ) , func . ST_AsText ( func . ST_Transform ( self . orm [ 'orm_re_generators' ] . columns . geom , srid ) ) . label ( 'geom' ) ) . filter ( self . orm [ 'orm_re_generators' ] . columns . subst_id . in_ ( list ( mv_grid_districts_dict ) ) ) . filter ( self . orm [ 'orm_re_generators' ] . columns . voltage_level . in_ ( [ 4 , 5 , 6 , 7 ] ) ) . filter ( self . orm [ 'version_condition_re' ] ) generators = pd . read_sql_query ( generators_sqla . statement , session . bind , index_col = 'id' ) generators . loc [ generators [ 'generation_subtype' ] . isnull ( ) , 'generation_subtype' ] = 'unknown' for id_db , row in generators . iterrows ( ) : if row [ 'geom_new' ] : geo_data = wkt_loads ( row [ 'geom_new' ] ) elif not row [ 'geom_new' ] : geo_data = wkt_loads ( row [ 'geom' ] ) logger . warning ( 'Generator {} has no geom_new entry,' 'EnergyMap\'s geom entry will be used.' . format ( id_db ) ) elif not row [ 'geom' ] : logger . error ( 'Generator {} has no geom entry either' 'and will be skipped.' . format ( id_db ) ) continue mv_grid_district_id = row [ 'subst_id' ] mv_grid = mv_grid_districts_dict [ mv_grid_district_id ] . mv_grid if row [ 'generation_type' ] in [ 'solar' , 'wind' ] : generator = GeneratorFluctuatingDing0 ( id_db = id_db , mv_grid = mv_grid , capacity = row [ 'electrical_capacity' ] , type = row [ 'generation_type' ] , subtype = row [ 'generation_subtype' ] , v_level = int ( row [ 'voltage_level' ] ) , weather_cell_id = row [ 'w_id' ] ) else : generator = GeneratorDing0 ( id_db = id_db , mv_grid = mv_grid , capacity = row [ 'electrical_capacity' ] , type = row [ 'generation_type' ] , subtype = row [ 'generation_subtype' ] , v_level = int ( row [ 'voltage_level' ] ) ) if generator . v_level in [ 4 , 5 ] : generator . geo_data = geo_data mv_grid . add_generator ( generator ) elif generator . v_level in [ 6 , 7 ] : mvlv_subst_id = row [ 'mvlv_subst_id' ] if mvlv_subst_id and not isnan ( mvlv_subst_id ) : try : lv_station = lv_stations_dict [ mvlv_subst_id ] lv_grid_district = lv_station . grid . grid_district generator . lv_grid = lv_station . grid generator . geo_data = geo_data except : lv_grid_district = random . choice ( list ( lv_grid_districts_dict . values ( ) ) ) generator . lv_grid = lv_grid_district . lv_grid generator . geo_data = lv_grid_district . lv_grid . station ( ) . geo_data logger . warning ( 'Generator {} cannot be assigned to ' 'non-existent LV Grid District and was ' 'allocated to a random LV Grid District ({}).' . format ( repr ( generator ) , repr ( lv_grid_district ) ) ) pass else : lv_grid_district = random . choice ( list ( lv_grid_districts_dict . values ( ) ) ) generator . lv_grid = lv_grid_district . lv_grid generator . geo_data = lv_grid_district . lv_grid . station ( ) . geo_data logger . warning ( 'Generator {} has no la_id and was ' 'assigned to a random LV Grid District ({}).' . format ( repr ( generator ) , repr ( lv_grid_district ) ) ) generator . lv_load_area = lv_grid_district . lv_load_area lv_grid_district . lv_grid . add_generator ( generator ) def import_conv_generators ( ) : generators_sqla = session . query ( self . orm [ 'orm_conv_generators' ] . columns . id , self . orm [ 'orm_conv_generators' ] . columns . subst_id , self . orm [ 'orm_conv_generators' ] . columns . name , self . orm [ 'orm_conv_generators' ] . columns . capacity , self . orm [ 'orm_conv_generators' ] . columns . fuel , self . orm [ 'orm_conv_generators' ] . columns . voltage_level , func . ST_AsText ( func . ST_Transform ( self . orm [ 'orm_conv_generators' ] . columns . geom , srid ) ) . label ( 'geom' ) ) . filter ( self . orm [ 'orm_conv_generators' ] . columns . subst_id . in_ ( list ( mv_grid_districts_dict ) ) ) . filter ( self . orm [ 'orm_conv_generators' ] . columns . voltage_level . in_ ( [ 4 , 5 , 6 ] ) ) . filter ( self . orm [ 'version_condition_conv' ] ) generators = pd . read_sql_query ( generators_sqla . statement , session . bind , index_col = 'id' ) for id_db , row in generators . iterrows ( ) : mv_grid_district_id = row [ 'subst_id' ] mv_grid = mv_grid_districts_dict [ mv_grid_district_id ] . mv_grid generator = GeneratorDing0 ( id_db = id_db , name = row [ 'name' ] , geo_data = wkt_loads ( row [ 'geom' ] ) , mv_grid = mv_grid , capacity = row [ 'capacity' ] , type = row [ 'fuel' ] , subtype = 'unknown' , v_level = int ( row [ 'voltage_level' ] ) ) if generator . v_level in [ 4 , 5 ] : mv_grid . add_generator ( generator ) elif generator . v_level in [ 6 ] : generator . v_level = 5 mv_grid . add_generator ( generator ) srid = str ( int ( cfg_ding0 . get ( 'geo' , 'srid' ) ) ) seed = int ( cfg_ding0 . get ( 'random' , 'seed' ) ) random . seed ( a = seed ) mv_grid_districts_dict , lv_load_areas_dict , lv_grid_districts_dict , lv_stations_dict = self . get_mvgd_lvla_lvgd_obj_from_id ( ) import_res_generators ( ) import_conv_generators ( ) logger . info ( '=====> Generators imported' )
Imports renewable (res) and conventional (conv) generators

        Args:
            session : sqlalchemy.orm.session.Session
                Database session
            debug: If True, information is printed during process
        Notes:
            Connection of generators is done later on in NetworkDing0's method connect_generators()