예제 #1
0
def _fetch_series(zone_key,
                  series_id,
                  session=None,
                  target_datetime=None,
                  logger=None):
    """Fetches and converts a data series."""
    key = os.environ['EIA_KEY']
    assert key and key != 'eia_key', key

    s = session or requests.Session()
    series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = series.last(24)

    # UTC timestamp with no offset returned.
    if not raw_data.get('series'):
        # Series doesn't exist. Probably requesting a fuel from a region that
        # doesn't have any capacity for that fuel type.
        return []

    return [{
        'zoneKey': zone_key,
        'datetime': parser.parse(datapoint[0]),
        'value': datapoint[1],
        'source': 'eia.gov',
    } for datapoint in raw_data['series'][0]['data']]
예제 #2
0
def fetch_exchange(zone_key1,
                   zone_key2,
                   session=None,
                   target_datetime=None,
                   logger=None):
    """Requests the last known power exchange (in MW) between two zones
    Arguments:
    zone_key1           -- the first country code
    zone_key2           -- the second country code; order of the two codes in params doesn't matter
    session (optional)      -- request session passed in order to re-use an existing session
    target_datetime (optional)      -- string in form YYYYMMDDTHHZ
    Return:
    A list of dictionaries in the form:
    {
      'sortedZoneKeys': 'DK->NO',
      'datetime': '2017-01-01T00:00:00Z',
      'netFlow': 0.0,
      'source': 'mysource.com'
    }
    where net flow is from DK into NO
    """

    sortedcodes = '->'.join(sorted([zone_key1, zone_key2]))

    series_id = EXCHANGES[sortedcodes]
    s = session or requests.Session()
    exchange_series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = exchange_series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = exchange_series.last(24)['series'][0]['data']

    data = []
    for datapoint in raw_data:
        if sortedcodes == 'MX->US-CA':
            datapoint[1] = -1 * datapoint[1]

        exchange = {
            'sortedZoneKeys': sortedcodes,
            'datetime': parser.parse(datapoint[0]),
            'netFlow': datapoint[1],
            'source': 'mysource.com'
        }

        data.append(exchange)

    return data
예제 #3
0
def fetch_exchange(zone_key1, zone_key2, session=None, target_datetime=None, logger=None):
    """Requests the last known power exchange (in MW) between two zones
    Arguments:
    zone_key1           -- the first country code
    zone_key2           -- the second country code; order of the two codes in params doesn't matter
    session (optional)      -- request session passed in order to re-use an existing session
    target_datetime (optional)      -- string in form YYYYMMDDTHHZ
    Return:
    A list of dictionaries in the form:
    {
      'sortedZoneKeys': 'DK->NO',
      'datetime': '2017-01-01T00:00:00Z',
      'netFlow': 0.0,
      'source': 'mysource.com'
    }
    where net flow is from DK into NO
    """

    sortedcodes = '->'.join(sorted([zone_key1, zone_key2]))

    series_id = EXCHANGES[sortedcodes]
    s = session or requests.Session()
    exchange_series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = exchange_series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = exchange_series.last(24)['series'][0]['data']

    data = []
    for datapoint in raw_data:
        if sortedcodes == 'MX-BC->US-CA':
            datapoint[1] = -1*datapoint[1]

        exchange = {'sortedZoneKeys': sortedcodes,
                    'datetime': parser.parse(datapoint[0]),
                    'netFlow': datapoint[1],
                    'source': 'mysource.com'}

        data.append(exchange)

    return data
예제 #4
0
def _fetch_production_or_consumption(zone_key, series_id, session=None,
                                     target_datetime=None, logger=None):
    """Fetches production or consumption forecast, determined by series_id."""
    s = session or requests.Session()
    series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = series.last(24)

    # UTC timestamp with no offset returned.

    return [{
        'zoneKey': zone_key,
        'datetime': parser.parse(datapoint[0]),
        'value': datapoint[1],
        'source': 'eia.org',
    } for datapoint in raw_data['series'][0]['data']]
예제 #5
0
def fetch_consumption_forecast(zone_key, session=None, target_datetime=None,
                               logger=None):

    series_id = DAY_AHEAD[zone_key]
    s = session or requests.Session()
    forecast_series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = forecast_series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = forecast_series.last(24)['series'][0]['data']

    # UTC timestamp with no offset returned.

    return [{
        'zoneKey': zone_key,
        'datetime': parser.parse(datapoint[0]),
        'value': datapoint[1],
        'source': 'eia.org',
    } for datapoint in raw_data]
예제 #6
0
def fetch_consumption_forecast(zone_key, session=None, target_datetime=None,
                               logger=None):

    series_id = DAY_AHEAD[zone_key]
    s = session or requests.Session()
    forecast_series = Series(series_id=series_id, session=s)

    if target_datetime:
        raw_data = forecast_series.last_from(24, end=target_datetime)
    else:
        # Get the last 24 hours available.
        raw_data = forecast_series.last(24)['series'][0]['data']

    # UTC timestamp with no offset returned.

    return [{
        'zoneKey': zone_key,
        'datetime': parser.parse(datapoint[0]),
        'value': datapoint[1],
        'source': 'eia.org',
    } for datapoint in raw_data]