Beispiel #1
0
def load_registry() -> List[StationSchema]:
    """
        Loads the facility registry into a list of Station schema's
    """
    stations = load_data("facility_registry.json")

    records = []

    for station_id, station_record in stations.items():
        facilities = []

        for duid, facility_record in station_record["duid_data"].items():
            status = map_compat_facility_state(
                station_record.get("status", {}).get("state", "")
            )
            fuel_tech = map_compat_fueltech(
                facility_record.get("fuel_tech", "")
            )
            registered_capacity = clean_capacity(
                facility_record.get("registered_capacity")
            )

            facility = FacilitySchema(
                name=normalize_whitespace(station_record.get("display_name")),
                network_region=map_compat_network_region(
                    station_record["region_id"]
                ),
                status=status,
                duid=duid,
                fueltech=fuel_tech,
                capacity=registered_capacity,
            )

            facilities.append(facility)

        record = StationSchema(
            name=normalize_whitespace(station_record.get("display_name")),
            code=station_id,
            state=station_record.get("location", {}).get("state", None),
            facilities=_sort_facilities(facilities),
        )

        records.append(record)

    records = _sort_stations(records)

    return records
def update_existing_geos() -> None:
    """
    Old method to update geos from existing facilities file on OpenNEM
    """

    station_fixture = load_data("facility_registry.json", from_fixture=True)

    stations = [{"station_code": k, **v} for k, v in station_fixture.items()]

    s = SessionLocal()

    for station_data in stations:
        station = None

        station_name = station_name_cleaner(station_data["display_name"])
        station_code = normalize_duid(station_data["station_code"])
        station_state = map_compat_facility_state(
            station_data["status"]["state"])

        station = s.query(Station).filter(
            Station.network_code == station_code).one_or_none()

        if not station:
            logger.info("Could not find station {}".format(station_code))
            continue

        if ("location" in station_data
                and "latitude" in station_data["location"]
                and station_data["location"]["latitude"]):
            station.geom = ("SRID=4326;POINT({} {})".format(
                station_data["location"]["latitude"],
                station_data["location"]["longitude"],
            ), )
            station.geocode_processed_at = datetime.now()
            station.geocode_by = "opennem"
            station.geocode_approved = True

            station.updated_by = "fixture.registry"

        s.add(station)

        logger.info("Updated station geo location {} ({})".format(
            station.code,
            station.name,
        ))

        facilities = [{
            "code": k,
            **v
        } for k, v in stations[0]["duid_data"].items()]

        # update fueltechs
        for facility_data in facilities:
            facility_duid = facility_data["code"]
            facility_fueltech = lookup_fueltech(facility_data["fuel_tech"])

            facility = s.query(Facility).filter(
                Facility.network_code == facility_duid).first()

            if not facility:
                logger.error(
                    "Could not find existing facility {} for station {}".
                    format(facility_duid, station_code))
                continue

            if not facility.fueltech_id:
                facility.fueltech_id = facility_fueltech

            if facility.fueltech_id != facility_fueltech:
                logger.error(
                    "Fueltech mismatch for {}. Old is {} and new is {}".format(
                        station_code, facility_fueltech, station.fueltech_id))

            s.add(facility)

        s.commit()
Beispiel #3
0
def load_opennem_facilities():
    station_fixture = load_data("facility_registry.json", from_fixture=True)

    stations = [{"station_code": k, **v} for k, v in station_fixture.items()]

    engine = db_connect()
    session = sessionmaker(bind=engine)
    s = session()

    for station_data in stations:
        station = None

        facilities = [{
            "code": k,
            **v
        } for k, v in station_data["duid_data"].items()]

        # update facilities
        for facility_data in facilities:
            facility_duid = facility_data["code"]
            station_name = station_name_cleaner(station_data["display_name"])
            # station_code = normalize_duid(station_data["station_code"])
            station_code = facility_map_station(
                facility_duid, normalize_duid(station_data["station_code"]))
            station_state = map_compat_facility_state(
                station_data["status"]["state"])
            station_network = "WEM" if station_data["location"][
                "state"] == "WA" else "NEM"

            station = s.query(Station).filter(
                Station.network_code == station_code).one_or_none()

            if not station:
                station = Station(
                    network_id=station_network,
                    code=station_code,
                    network_code=station_code,
                    name=station_name,
                    network_name=station_data["display_name"],
                    created_by="opennem.load_facilities",
                )
                logger.info("Created station: {} {} ".format(
                    station_name, station_code))

                s.add(station)
                s.commit()

            facility_status = station_state

            # Network region
            facility_network_region = map_compat_network_region(
                station_data["region_id"])

            # Fueltech
            facility_fueltech = None

            if "fuel_tech" in facility_data and facility_data["fuel_tech"]:
                facility_fueltech = map_compat_fueltech(
                    facility_data["fuel_tech"])

            # Capacity
            facility_capacity = None

            if "registered_capacity" in facility_data and facility_data[
                    "registered_capacity"]:
                facility_capacity = clean_capacity(
                    facility_data["registered_capacity"])

            facility = None

            try:
                facility = (s.query(Facility).filter(
                    Facility.network_code == facility_duid).one_or_none())
            except MultipleResultsFound:
                logger.error("Multiple facilities found for duid {}".format(
                    facility_duid))

                # facility = (
                #     s.query(Facility)
                #     .filter(Facility.network_code == facility_duid)
                #     .first()
                # )
                continue

            if not facility:
                facility = Facility(
                    code=facility_duid,
                    network_code=facility_duid,
                    network_region=facility_network_region,
                    created_by="opennem.load_facilities",
                )

                logger.info("Created facility: {} {} to station {} ".format(
                    facility_duid, facility_fueltech, station_code))

            if not facility.unit_id:
                facility.unit_id = 1
                facility.unit_number = 1

            if facility_capacity and not facility.unit_capacity:
                facility.unit_capacity = facility_capacity

            if facility_capacity and not facility.capacity_registered:
                facility.capacity_registered = facility_capacity

            if facility_fueltech and not facility.fueltech_id:
                facility.fueltech_id = facility_fueltech

            if not facility.status_id:
                facility.status_id = facility_status

            if not facility.station:
                facility.station = station

            s.add(facility)
            s.commit()