|
3 | 3 | import logging
|
4 | 4 | # The arrow library is used to handle datetimes
|
5 | 5 | import arrow
|
| 6 | +import datetime as dt |
6 | 7 | import pandas as pd
|
7 | 8 | from . import occtonet
|
8 | 9 |
|
|
16 | 17 | # JP-SK : Shikoku
|
17 | 18 | # JP-KY : Kyushu
|
18 | 19 | # JP-ON : Okinawa
|
| 20 | +# JP-CG : Chūgoku |
| 21 | + |
19 | 22 |
|
20 | 23 | def fetch_production(zone_key='JP-TK', session=None, target_datetime=None,
|
21 | 24 | logger=logging.getLogger(__name__)):
|
@@ -118,15 +121,59 @@ def fetch_consumption_df(zone_key='JP-TK', target_datetime=None,
|
118 | 121 | df = df[['datetime', 'cons']]
|
119 | 122 | return df
|
120 | 123 |
|
| 124 | + |
| 125 | +def fetch_price(zone_key='JP-TK', session=None, target_datetime=None, |
| 126 | + logger=logging.getLogger(__name__)): |
| 127 | + if target_datetime is None: |
| 128 | + target_datetime = dt.datetime.now() + dt.timedelta(days=1) |
| 129 | + |
| 130 | + # price files contain data for fiscal year and not calendar year. |
| 131 | + if target_datetime.month <= 3: |
| 132 | + fiscal_year = target_datetime.year - 1 |
| 133 | + else: |
| 134 | + fiscal_year = target_datetime.year |
| 135 | + url = 'http://www.jepx.org/market/excel/spot_{}.csv'.format(fiscal_year) |
| 136 | + df = pd.read_csv(url) |
| 137 | + |
| 138 | + df = df.iloc[:, [0, 1, 6, 7, 8, 9, 10, 11, 12, 13, 14]] |
| 139 | + df.columns = ['Date', 'Period', 'JP-HKD', 'JP-TH', 'JP-TK', 'JP-CB', |
| 140 | + 'JP-HR', 'JP-KN', 'JP-CG', 'JP-SK', 'JP-KY'] |
| 141 | + |
| 142 | + if zone_key not in df.columns[2:]: |
| 143 | + return [] |
| 144 | + |
| 145 | + start = target_datetime - dt.timedelta(days=1) |
| 146 | + df['Date'] = df['Date'].apply(lambda x: dt.datetime.strptime(x, '%Y/%m/%d')) |
| 147 | + df = df[(df['Date'] >= start.date()) & (df['Date'] <= target_datetime.date())] |
| 148 | + |
| 149 | + df['datetime'] = df.apply(lambda row: arrow.get(row['Date']).shift( |
| 150 | + minutes=30 * (row['Period'] - 1)).replace(tzinfo='Asia/Tokyo'), axis=1) |
| 151 | + |
| 152 | + data = list() |
| 153 | + for row in df.iterrows(): |
| 154 | + data.append({ |
| 155 | + 'zoneKey': zone_key, |
| 156 | + 'currency': 'JPY', |
| 157 | + 'datetime': row[1]['datetime'].datetime, |
| 158 | + 'price': row[1][zone_key], |
| 159 | + 'source': 'jepx.org' |
| 160 | + }) |
| 161 | + |
| 162 | + return data |
| 163 | + |
| 164 | + |
121 | 165 | def parse_dt(row):
|
122 | 166 | """
|
123 | 167 | Parses timestamps from date and time
|
124 | 168 | """
|
125 | 169 | return arrow.get(' '.join([row['Date'], row['Time']]).replace('/', '-'),
|
126 | 170 | 'YYYY-M-D H:mm').replace(tzinfo='Asia/Tokyo').datetime
|
127 | 171 |
|
| 172 | + |
128 | 173 | if __name__ == '__main__':
|
129 | 174 | """Main method, never used by the Electricity Map backend, but handy for testing."""
|
130 | 175 |
|
131 | 176 | print('fetch_production() ->')
|
132 | 177 | print(fetch_production())
|
| 178 | + print('fetch_price() ->') |
| 179 | + print(fetch_price()) |
0 commit comments