Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Wattsight API python library usage example

An introductory example of using Wattsight API python library for searching, fetching, and plotting Wattsight data series. A number of use cases is presented in order to introduce different data structures.

Dependencies

Wattsight API python library
Jupyter (iPython) Notebooks
Pandas Data Analysis library
Plotly Charting library
World Timezone Definitions for Python

Installation of Wattsight API python library

  1. Using pip

    pip install -U wapi-python

Implementation

Complete working code can be found in the attached Jupyter Notebook file.

  1. Establish a session using client credentials.

    This requires having an OAuth client, which can be created at https://auth.volueinsight.com/account/oauth-clients.

    >>> session = wapi.Session(client_id='client id', client_secret='client secret')
  2. Search curves by meta data.

    For this use case define meta criteria for DK1 area wind power production.

    >>> curves = session.search(commodity='POW',
                                category='WND',
                                unit='MWh/h',
                                area='DK1',
                                data_type=['A', 'F']
                               )
    >>> [c.name for c in curves]
    ['pro dk1 wnd intraday ec00da mwh/h cet min15 f',
     'pro dk1 wnd intraday lastec mwh/h cet min15 f',
     'pro dk1 wnd intraday tso mwh/h cet min15 f',
     'pro dk1 wnd ec00 mwh/h cet min15 f',
     'pro dk1 wnd ec12 mwh/h cet min15 f',
     'pro dk1 wnd gfs00 mwh/h cet min15 f',
     'pro dk1 wnd gfs06 mwh/h cet min15 f',
     'pro dk1 wnd gfs12 mwh/h cet min15 f',
     'pro dk1 wnd gfs18 mwh/h cet min15 f',
     'pro dk1 wnd ec00ens mwh/h cet min15 f',
     'pro dk1 wnd ec12ens mwh/h cet min15 f',
     'pro dk1 wnd mwh/h cet min15 a']
  3. For the first example, filter on intraday curves and retrieve data for the selected curves.

    >>> intraday_curves = [c for c in curves if 'INTRADAY' in c.categories]
    >>> data = {}
    >>> label = {}
    >>> issue_date = pytz.timezone('CET').localize(datetime(2018, 4, 12))
    >>> date_from = issue_date + timedelta(minutes=15)
    >>> date_to = date_from + timedelta(hours=24)
    >>> for c in intraday_curves:
    >>>     if c.curve_type == 'INSTANCES':
    >>>         ts = c.get_instance(issue_date)
    >>>         if ts is not None:
    >>>             label[c.name] = ';'.join(c.sources)
    >>>             data[c.name] = ts.to_pandas()
    >>>     elif c.curve_type == 'TIME_SERIES':
    >>>         data[c.name] = c.get_data(data_from=date_from, 
    >>>                                   data_to=date_to).to_pandas()
    >>>         label[c.name] = 'Actual'
  4. Plot selected data

    >>> fig = tls.make_subplots(len(data), cols=1, shared_xaxes=True)
    >>> for key in data.keys():
    >>>     ts = data[key]
    >>>     fig.append_trace({'x': ts.index, 'y': ts.values, 'type': 'scatter', 
    >>>                       'name': label[key]}, 1, 1)
    >>> title = ' '.join([c.commodity,
    >>>                   ' '.join(c.categories),
    >>>                   c.area,
    >>>                   issue_date.strftime("%Y-%m-%d %H:%M"),
    >>>                   c.time_zone])
    >>> fig['layout'].update(title=title)
    >>> fig['layout'].update(yaxis=dict(title=c.unit))    
    >>> offline.iplot(fig)

    Chart

  5. Retrive data for more instances and build contract development series.

    >>> curve = intraday_curves[0]
    >>> latest_instance = curve.get_latest(with_data=False)
    >>> issue_date = parser.parse(latest_instance.issue_date).astimezone(tz=latest_instance.tz)
    >>> value_date = issue_date + timedelta(minutes=15)
    >>> index = []
    >>> values = []
    >>> forecast_exists = True
    >>> while forecast_exists:
    >>>     ts = curve.get_instance(issue_date=issue_date)
    >>>     try:
    >>>         ts = curve.get_instance(issue_date=issue_date).to_pandas()
    >>>         values.append(ts[value_date])
    >>>         index.append(issue_date)
    >>>         issue_date = issue_date - timedelta(minutes=15)
    >>>     except:
    >>>         forecast_exists = False

    Chart

  6. For an example of working with tagged data, filter out weather data forecast curves.

    >>> curves_at00 = [c for c in curves 
                       if 'INTRADAY' not in c.categories 
                       and (c.data_type == 'A' or c.sources [0] in ["EC00", "EC00Ens", 'GFS00'] )
                      ]
    >>> [c.name for c in curves_at00]
    ['pro dk1 wnd ec00 mwh/h cet min15 f',
     'pro dk1 wnd gfs00 mwh/h cet min15 f',
     'pro dk1 wnd ec00ens mwh/h cet min15 f',
     'pro dk1 wnd mwh/h cet min15 a']
  7. Retrive data for the selcted curves.

    >>> data = {}
    >>> label = {}
    >>> issue_date = pytz.timezone('CET').localize(datetime(2018, 4, 12))
    >>> date_to = date_from + timedelta(days=14)
    >>> for c in curves_at00:
    >>>     ts = None
    >>>     if c.curve_type == 'TAGGED_INSTANCES':
    >>>         ts = c.get_instance(issue_date=issue_date, tag='Avg')
    >>>         label[c.name] = ' '.join([';'.join(c.sources), 'Avg'])
    >>>     elif c.curve_type == 'INSTANCES':
    >>>         ts = c.get_instance(issue_date=issue_date)
    >>>         label[c.name] = ';'.join(c.sources)
    >>>     elif c.curve_type == 'TIME_SERIES':
    >>>         data[c.name] = c.get_data(data_from=date_from, data_to=date_to).to_pandas()
    >>>         label[c.name] = 'Actual'
    >>>     if ts is not None:
    >>>         data[c.name] = ts.to_pandas()

    Chart