Last active
June 30, 2026 00:51
-
-
Save sugiana/96d60c803be4fc6dd7b4e32397cde2fe to your computer and use it in GitHub Desktop.
Riwayat Cuaca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # pip install meteostat | |
| import sys | |
| from datetime import ( | |
| datetime, | |
| date, | |
| timedelta, | |
| ) | |
| from argparse import ArgumentParser | |
| from datetime import date | |
| import meteostat as ms | |
| date_pattern = "%Y-%m-%d" | |
| # Kabupaten Bogor, ID WMO 96751 (stasiun cuaca) | |
| lat = -6.7 | |
| lon = 106.85 | |
| today = date.today() | |
| # setahun yang lalu | |
| start_date = end_date = today - timedelta(365) | |
| start_date = start_date.strftime(date_pattern) | |
| end_date = end_date.strftime(date_pattern) | |
| help_lat = f"default {lat}" | |
| help_lon = f"default {lon}" | |
| help_start_date = f"default {start_date}" | |
| help_end_date = f"default {end_date}" | |
| pars = ArgumentParser() | |
| pars.add_argument("--lat", type=float, default=lat, help=help_lat) | |
| pars.add_argument("--lon", type=float, default=lon, help=help_lon) | |
| pars.add_argument("--start-date", default=start_date, help=help_start_date) | |
| pars.add_argument("--end-date", default=end_date, help=help_end_date) | |
| option = pars.parse_args(sys.argv[1:]) | |
| point = ms.Point(option.lat, option.lon) | |
| start_date = datetime.strptime(option.start_date, date_pattern) | |
| end_date = datetime.strptime(option.end_date, date_pattern) | |
| start_date = start_date.date() | |
| end_date = end_date.date() | |
| # Identitas stasiun cuaca | |
| stations = ms.stations.nearby(point, limit=4) | |
| st_dict = dict() | |
| for index, row in stations.iterrows(): | |
| st_id = row.name | |
| st_dict[st_id] = dict(row) | |
| # Data mentah | |
| ts = ms.daily(stations, start_date, end_date) # TimeSeries | |
| df = ts.fetch() | |
| if df.empty: | |
| print("Tidak ada data.") | |
| sys.exit() | |
| print("=== Sumber Data ===") | |
| for index, row in df.iterrows(): | |
| st_id, dt = row.name | |
| dt = dt.date() | |
| st = st_dict[st_id] | |
| st_point = f"{st['latitude']}, {st['longitude']}" | |
| print(f"Tanggal: {dt}") | |
| print(f"ID Stasiun: {st_id}") | |
| print(f"Koordinat: {st_point}") | |
| print(f"Nama lokasi: {st['name']}") | |
| print(f"Ketinggian:: {st['elevation']} mdpl") | |
| print(f"Suhu rata-rata {row.temp} derajat Celcius") | |
| print(f"Suhu minimum {row.tmin} derajat Celcius") | |
| print(f"Suhu maksimum {row.tmax} derajat Celcius") | |
| print(f"Kelembaban {row.rhum}%") | |
| print(f"Curah hujan {row.prcp} mm") | |
| print(f"Kecepatan angin {row.wspd} km/jam") | |
| print(f"Tekanan udara {row.pres} hPa") | |
| print(f"Tutupan awan {row.cldc}%") | |
| print("---") | |
| print("=== Data Gabungan ===") | |
| df = ms.interpolate(ts, point).fetch() | |
| for index, row in df.iterrows(): | |
| print("Tanggal", row.name.date()) | |
| print(f"Suhu rata-rata {row.temp} derajat Celcius") | |
| print(f"Suhu minimum {row.tmin} derajat Celcius") | |
| print(f"Suhu maksimum {row.tmax} derajat Celcius") | |
| print(f"Kelembaban {row.rhum}%") | |
| print(f"Curah hujan {row.prcp} mm") | |
| print(f"Kecepatan angin {row.wspd} km/jam") | |
| print(f"Tekanan udara {row.pres} hPa") | |
| print(f"Tutupan awan {row.cldc}%") | |
| print("---") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment