Skip to content

Commit

Permalink
encode time intervals more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Aug 31, 2020
1 parent 298e746 commit 23f77b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 62 deletions.
39 changes: 10 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ client = meteoblue_dataset_sdk.Client(apikey="xxxxxx")
result = client.querySync(query)
# result is a structured object containing timestamps and data

timestamps = result.geometries[0].timeIntervals[0].timestamps
timeInterval = result.geometries[0].timeIntervals[0]
data = result.geometries[0].codes[0].timeIntervals[0].data

print(timestamps)
# [1546300800, 1546304400, 1546308000, 1546311600, 1546315200, ...
print(data)
# [2.89, 2.69, 2.549999, 2.3800001,
print(timeInterval)
# start: 1546300800
# end: 1546387200
# stride: 3600
```

NOTE: `timeInterval.end` is the first timestamp that is not included anymore in the time interval.

If your code is using `async/await`, you should use `await client.query()` instead of `client.querySync()`. Asynchronous IO is essential for modern webserver frameworks like Flask or FastAPI.

```python
Expand All @@ -92,30 +94,9 @@ geometries {
ny: 1
timeResolution: "hourly"
timeIntervals {
timestamps: 1546300800
timestamps: 1546304400
timestamps: 1546308000
timestamps: 1546311600
timestamps: 1546315200
timestamps: 1546318800
timestamps: 1546322400
timestamps: 1546326000
timestamps: 1546329600
timestamps: 1546333200
timestamps: 1546336800
timestamps: 1546340400
timestamps: 1546344000
timestamps: 1546347600
timestamps: 1546351200
timestamps: 1546354800
timestamps: 1546358400
timestamps: 1546362000
timestamps: 1546365600
timestamps: 1546369200
timestamps: 1546372800
timestamps: 1546376400
timestamps: 1546380000
timestamps: 1546383600
start: 1546300800
end: 1546387200
stride: 3600
}
codes {
code: 11
Expand Down
9 changes: 6 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ async def myFunction():
result = await client.query(query)
# result is a structured object containing timestamps and data

timestamps = result.geometries[0].timeIntervals[0].timestamps
timeInterval = result.geometries[0].timeIntervals[0]
data = result.geometries[0].codes[0].timeIntervals[0].data

print(timestamps)
# [1546300800, 1546304400, 1546308000, 1546311600, 1546315200, ...
print(timeInterval)
# start: 1546300800
# end: 1546387200
# stride: 3600

print(data)
# [2.89, 2.69, 2.549999, 2.3800001,

Expand Down
35 changes: 5 additions & 30 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_simple_query(self):
client = meteoblue_dataset_sdk.Client(os.environ["APIKEY"])
result = asyncio.run(client.query(query))
geo = result.geometries[0]
timestamps = geo.timeIntervals[0].timestamps
timeInterval = result.geometries[0].timeIntervals[0]
variable = geo.codes[0]
data = variable.timeIntervals[0].data
# print(result)
Expand All @@ -63,35 +63,10 @@ def test_simple_query(self):
self.assertEqual(variable.unit, "°C")
self.assertEqual(variable.aggregation, "none")

self.assertEqual(
timestamps,
[
1546300800,
1546304400,
1546308000,
1546311600,
1546315200,
1546318800,
1546322400,
1546326000,
1546329600,
1546333200,
1546336800,
1546340400,
1546344000,
1546347600,
1546351200,
1546354800,
1546358400,
1546362000,
1546365600,
1546369200,
1546372800,
1546376400,
1546380000,
1546383600,
],
)
self.assertEqual(timeInterval.start, 1546300800)
self.assertEqual(timeInterval.end, 1546383600 + 3600)
self.assertEqual(timeInterval.stride, 3600)

self.assertEqual(
data,
[
Expand Down

0 comments on commit 23f77b5

Please sign in to comment.