Skip to content
Merged
32 changes: 31 additions & 1 deletion docs/historic.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ res.to_pandas().head()
| 2022-06-01 07:00:00+00:00 | 13 | 62 | 12 |
| 2022-06-01 07:30:00+00:00 | 13 | 0 | 0 |
| 2022-06-01 08:00:00+00:00 | 12 | 0 | 0 |
| 2022-06-01 08:30:00+00:00 | 12 | 0 | 0 |
| 2022-06-01 08:30:00+00:00 | 12 | 0 | 0 |


## Example of multi period request for the year of 2023 from Jan 01
### The below code is using an unmetered location. If using a metered location, it will consume 12 request.

```python
from solcast import historic
import pandas as pd
from solcast.unmetered_locations import UNMETERED_LOCATIONS
from solcast import historic

site = UNMETERED_LOCATIONS["Stonehenge"]
latitude, longitude = site["latitude"], site["longitude"]

data = []
start_date = '2023-01-01'
start_dates = pd.date_range(start=start_date, periods=12, freq='MS')

for start in start_dates:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I liked about your previous implementation with while loop, is that it did not require the user to think up front about how the queries should be chunked.

The following version of the former while code will only get the exact number of days required (including getting all of the final day).

step = 31
while days < days_required:
    chunk_start = start + pd.DateOffset(days=days)
    duration_days = min(step, days_required - days + 1)
    res = historic.radiation_and_weather(latitude=-33.856784, longitude=151.215297, start=chunk_start, duration=f'P{duration_days}D')
    responses.append(res)
    days += step

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was updated as a solution to @Zempire's comment:
#37 (review)

I think it's a cleaner way to pull the data, if the final objective is to retrieve only one years worth of data.

start_str = start.strftime('%Y-%m-%dT00:00:00.000Z')
end_date = (start + pd.offsets.MonthEnd(1)).strftime('%Y-%m-%dT23:59:59.000Z')

res = historic.radiation_and_weather(latitude=latitude, longitude=longitude, start=start_str, end=end_date)
if res.success:
data.append(res.to_pandas())
else:
print(res.exception)

output = pd.concat(data)
```