Skip to content

Commit

Permalink
Meteostat 1.6.2 (#99)
Browse files Browse the repository at this point in the history
* Black formatting

* Black formatting II & Refactoring

* Black formatting III

* Rename manual test files

* Replace depreciated `DataFrame.append` by `concat` (#94)

Co-authored-by: Christian Lamprecht <[email protected]>

* Linting

* Final review

Co-authored-by: Jonas Maison <[email protected]>
  • Loading branch information
clampr and Jonas1312 authored Jun 1, 2022
1 parent 5454203 commit 1f33283
Show file tree
Hide file tree
Showing 59 changed files with 721 additions and 741 deletions.
11 changes: 11 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cff-version: 1.2.0
title: Meteostat Python
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Christian Sebastian
family-names: Lamprecht
email: [email protected]
orcid: 'https://orcid.org/0000-0003-3301-2852'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The Meteostat Python package is available through [PyPI](https://pypi.org/projec
pip install meteostat
```

Meteostat **requires Python 3.5** or higher. If you want to visualize data, please install Matplotlib, too.
Meteostat **requires Python 3.6** or higher. If you want to visualize data, please install Matplotlib, too.

## Documentation

Expand Down
6 changes: 3 additions & 3 deletions examples/daily/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
end = datetime(2018, 12, 31)

# Get daily data
data = Daily('10637', start, end)
data = Daily("10637", start, end)

# Group & aggregate weekly
data = data.normalize().aggregate(freq='1W').fetch()
data = data.normalize().aggregate(freq="1W").fetch()

# Plot chart
data.plot(y=['tavg', 'tmin', 'tmax'])
data.plot(y=["tavg", "tmin", "tmax"])
plt.show()
12 changes: 6 additions & 6 deletions examples/daily/aggregate_regional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
The code is licensed under the MIT license.
"""

if __name__ == '__main__':
if __name__ == "__main__":

from datetime import datetime
import matplotlib.pyplot as plt
Expand All @@ -23,19 +23,19 @@

# Get random weather stations in the US
stations = Stations()
stations = stations.region('US')
stations = stations.inventory('daily', (start, end))
stations = stations.region("US")
stations = stations.inventory("daily", (start, end))
stations = stations.fetch(limit=150, sample=True)

# Get daily data
data = Daily(stations, start, end)

# Normalize & aggregate
data = data.normalize().aggregate('1Y', spatial=True).fetch()
data = data.normalize().aggregate("1Y", spatial=True).fetch()

# Chart title
TITLE = 'Average US Annual Temperature from 1980 to 2019'
TITLE = "Average US Annual Temperature from 1980 to 2019"

# Plot chart
data.plot(y=['tavg'], title=TITLE)
data.plot(y=["tavg"], title=TITLE)
plt.show()
4 changes: 2 additions & 2 deletions examples/daily/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
# Get closest weather station
stations = Stations()
stations = stations.nearby(49.2497, -123.1193)
stations = stations.inventory('daily', (start, end))
stations = stations.inventory("daily", (start, end))
station = stations.fetch(1)

# Get daily data
data = Daily(station, start, end)
data = data.fetch()

# Plot chart
data.plot(y=['tavg', 'tmin', 'tmax'])
data.plot(y=["tavg", "tmin", "tmax"])
plt.show()
9 changes: 5 additions & 4 deletions examples/daily/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
end = datetime(2019, 12, 31)

# Get daily data
data = Daily(['71624', '72295', '68816', '94767'], start, end)
data = Daily(["71624", "72295", "68816", "94767"], start, end)
data = data.fetch()

# Plot chart
data.unstack('station')['tavg'].plot(
data.unstack("station")["tavg"].plot(
legend=True,
ylabel='Avg. Daily Temperature °C',
title='Average Temperature Report for 2019')
ylabel="Avg. Daily Temperature °C",
title="Average Temperature Report for 2019",
)
plt.show()
15 changes: 8 additions & 7 deletions examples/daily/compare_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# Get weather stations by Meteostat ID
stations = Stations()
stations = stations.fetch()
stations = stations[stations.index.isin(('D1424', '10729', '10803', '10513'))]
stations = stations[stations.index.isin(("D1424", "10729", "10803", "10513"))]

# Get names of weather stations
names = stations['name'].to_list()
names = stations["name"].to_list()

# Time period
start = datetime(2000, 1, 1)
Expand All @@ -28,15 +28,16 @@
data = Daily(stations, start, end)

# Aggregate annually
data = data.aggregate(freq='1Y').fetch()
data = data.aggregate(freq="1Y").fetch()

# Plot chart
fig, ax = plt.subplots(figsize=(8, 6))
data.unstack('station')['tmax'].plot(
data.unstack("station")["tmax"].plot(
legend=True,
ax=ax,
style='.-',
ylabel='Max. Annual Temperature (°C)',
title='Max. Temperature Report')
style=".-",
ylabel="Max. Annual Temperature (°C)",
title="Max. Temperature Report",
)
plt.legend(names)
plt.show()
2 changes: 1 addition & 1 deletion examples/daily/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
data = data.fetch()

# Plot line chart including average, minimum and maximum temperature
data.plot(y=['tavg', 'tmin', 'tmax'])
data.plot(y=["tavg", "tmin", "tmax"])
plt.show()
2 changes: 1 addition & 1 deletion examples/daily/source_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
start = datetime(2018, 1, 1)
end = datetime(2018, 12, 31)

data = Daily('10637', start, end, flags=True)
data = Daily("10637", start, end, flags=True)
df = data.fetch()

# Print DataFrame
Expand Down
4 changes: 2 additions & 2 deletions examples/hourly/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
end = datetime(2018, 1, 1, 23, 59)

# Get hourly data & aggregate daily
data = Hourly('10637', start, end)
data = data.aggregate('1D')
data = Hourly("10637", start, end)
data = data.aggregate("1D")
data = data.fetch()

# Print
Expand Down
2 changes: 1 addition & 1 deletion examples/hourly/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
data = data.fetch()

# Plot chart
data.plot(y='temp')
data.plot(y="temp")
plt.show()
4 changes: 2 additions & 2 deletions examples/hourly/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
station = stations.fetch(1)

# Get hourly data
data = Hourly(station, start, end, timezone='Europe/Berlin')
data = Hourly(station, start, end, timezone="Europe/Berlin")

# Convert data units
data = data.convert({'temp': fahrenheit, 'wdir': direction, 'coco': condition})
data = data.convert({"temp": fahrenheit, "wdir": direction, "coco": condition})

# Print to console
data = data.fetch()
Expand Down
4 changes: 2 additions & 2 deletions examples/hourly/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end = datetime(2018, 8, 4, 23, 59)

# Get hourly data
data = Hourly('10730', start, end)
data = Hourly("10730", start, end)

# Normalize data & interpolate up to 6 missing consecutive records
data = data.normalize()
Expand All @@ -27,5 +27,5 @@
data = data.fetch()

# Plot chart
data.plot(y='temp')
data.plot(y="temp")
plt.show()
4 changes: 2 additions & 2 deletions examples/hourly/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
The code is licensed under the MIT license.
"""

if __name__ == '__main__':
if __name__ == "__main__":

from timeit import default_timer as timer

Expand All @@ -24,7 +24,7 @@
start = datetime(1960, 1, 1)
end = datetime(2021, 1, 1, 23, 59)

data = Hourly('10637', start, end, timezone='Europe/Berlin')
data = Hourly("10637", start, end, timezone="Europe/Berlin")
data = data.fetch()

# Get end time
Expand Down
2 changes: 1 addition & 1 deletion examples/hourly/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
point = Point(50.3167, 8.5, 320)

# Get hourly data
data = Hourly(point, start, end, timezone='Europe/Berlin')
data = Hourly(point, start, end, timezone="Europe/Berlin")

# Print to console
data = data.fetch()
Expand Down
6 changes: 3 additions & 3 deletions examples/monthly/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

# Get monthly data
# Then, aggregate annually
data = Monthly('72202', start, end)
data = data.normalize().aggregate(freq='1Y').fetch()
data = Monthly("72202", start, end)
data = data.normalize().aggregate(freq="1Y").fetch()

# Plot chart
data.plot(y='tavg')
data.plot(y="tavg")
plt.show()
2 changes: 1 addition & 1 deletion examples/normals/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
data = data.normalize().fetch()

# Plot chart
data.plot(y=['tavg', 'tmin', 'tmax'])
data.plot(y=["tavg", "tmin", "tmax"])
plt.show()
4 changes: 2 additions & 2 deletions examples/normals/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from meteostat import Normals

# Get normals
data = Normals('72407')
data = Normals("10637")
data = data.normalize().fetch()

# Plot chart
data.plot(y=['tavg', 'tmin', 'tmax'])
data.plot(y=["tavg", "tmin", "tmax"])
plt.show()
4 changes: 2 additions & 2 deletions examples/stations/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

# Get number of stations in northern hemisphere
northern = stations.bounds((90, -180), (0, 180))
print('Stations in northern hemisphere:', northern.count())
print("Stations in northern hemisphere:", northern.count())

# Get number of stations in southern hemisphere
southern = stations.bounds((0, -180), (-90, 180))
print('Stations in southern hemisphere:', southern.count())
print("Stations in southern hemisphere:", southern.count())
6 changes: 3 additions & 3 deletions examples/stations/nearby.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# Get weather station
stations = Stations()
stations = stations.nearby(50, 8)
stations = stations.inventory('hourly')
station = stations.fetch(1).to_dict('records')[0]
stations = stations.inventory("hourly")
station = stations.fetch(1).to_dict("records")[0]

# Print name
print('Closest weather station at coordinates 50, 8:', station["name"])
print("Closest weather station at coordinates 50, 8:", station["name"])
4 changes: 2 additions & 2 deletions examples/stations/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Get stations in Ontario
stations = Stations()
stations = stations.region('CA', 'ON')
stations = stations.region("CA", "ON")

# Print count to console
print('Stations in Ontario:', stations.count())
print("Stations in Ontario:", stations.count())
4 changes: 2 additions & 2 deletions meteostat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
The code is licensed under the MIT license.
"""

__appname__ = 'meteostat'
__version__ = '1.6.1'
__appname__ = "meteostat"
__version__ = "1.6.2"

from .interface.base import Base
from .interface.timeseries import TimeSeries
Expand Down
30 changes: 8 additions & 22 deletions meteostat/core/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@
import hashlib


def get_local_file_path(
cache_dir: str,
cache_subdir: str,
path: str
) -> str:
def get_local_file_path(cache_dir: str, cache_subdir: str, path: str) -> str:
"""
Get the local file path
"""

# Get file ID
file = hashlib.md5(path.encode('utf-8')).hexdigest()
file = hashlib.md5(path.encode("utf-8")).hexdigest()

return f"{cache_dir}/{cache_subdir}/{file}"


def file_in_cache(
path: str,
max_age: int = 0
) -> bool:
def file_in_cache(path: str, max_age: int = 0) -> bool:
"""
Check if a file exists in the local cache
"""
Expand All @@ -47,18 +40,14 @@ def file_in_cache(
pass

# Return the file path if it exists
if os.path.isfile(path) and time.time() - \
os.path.getmtime(path) <= max_age:
if os.path.isfile(path) and time.time() - os.path.getmtime(path) <= max_age:
return True

return False


@classmethod
def clear_cache(
cls,
max_age: int = None
) -> None:
def clear_cache(cls, max_age: int = None) -> None:
"""
Clear the cache
"""
Expand All @@ -73,15 +62,12 @@ def clear_cache(
now = time.time()

# Go through all files
for file in os.listdir(
cls.cache_dir + os.sep + cls.cache_subdir):
for file in os.listdir(cls.cache_dir + os.sep + cls.cache_subdir):

# Get full path
path = os.path.join(
cls.cache_dir + os.sep + cls.cache_subdir, file)
path = os.path.join(cls.cache_dir + os.sep + cls.cache_subdir, file)

# Check if file is older than max_age
if now - \
os.path.getmtime(path) > max_age and os.path.isfile(path):
if now - os.path.getmtime(path) > max_age and os.path.isfile(path):
# Delete file
os.remove(path)
Loading

0 comments on commit 1f33283

Please sign in to comment.