-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script to fetch OpenMeteo Data(NWP Forecast and Historical data) #93
Open
praj-tarun
wants to merge
5
commits into
openclimatefix:main
Choose a base branch
from
praj-tarun:OpenMeteo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d87d589
Script to fetch OpenMEteo Data(NWP Forecast and Historical data)
praj-tarun be4a232
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3bb5cf0
OpenMeteo to xarray dataset
praj-tarun ddaa1b2
Merge branch 'OpenMeteo' of https://github.com/praj-tarun/graph_weath…
praj-tarun 2494c84
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import openmeteo_requests | ||
import requests_cache | ||
import pandas as pd | ||
from retry_requests import retry | ||
import numpy as np | ||
import xarray as xr | ||
from typing import Tuple, List | ||
|
||
class WeatherDataFetcher: | ||
def __init__(self): | ||
# Setup the Open-Meteo API client with cache and retry on error | ||
cache_session = requests_cache.CachedSession('.cache', expire_after=3600) | ||
retry_session = retry(cache_session, retries=5, backoff_factor=0.2) | ||
self.openmeteo = openmeteo_requests.Client(session=retry_session) | ||
|
||
def generate_lat_lon_grid(self, lat_range: Tuple[float, float] = (-90, 90), lon_range: Tuple[float, float] = (-180, 180), lat_step: float = 0.25, lon_step: float = 0.25) -> Tuple[np.ndarray, np.ndarray]: | ||
latitudes = np.arange(lat_range[0], lat_range[1] + lat_step, lat_step) | ||
longitudes = np.arange(lon_range[0], lon_range[1] + lon_step, lon_step) | ||
return latitudes, longitudes | ||
|
||
def fetch_world_grid_data(self, start_date: str, end_date: str, weather_variables: List[str]) -> xr.Dataset: | ||
# Generate latitude and longitude grid | ||
latitudes, longitudes = self.generate_lat_lon_grid() | ||
|
||
# Split the grid into smaller chunks (adjust as needed) | ||
chunk_size = 200 | ||
latitude_chunks = [latitudes[i:i+chunk_size] for i in range(0, len(latitudes), chunk_size)] | ||
longitude_chunks = [longitudes[i:i+chunk_size] for i in range(0, len(longitudes), chunk_size)] | ||
|
||
all_data = [] | ||
lat = [] | ||
lon = [] | ||
# Make API requests for each chunk of latitude and longitude values | ||
for lat_chunk, lon_chunk in zip(latitude_chunks, longitude_chunks): | ||
params = { | ||
"latitude": lat_chunk.tolist(), | ||
"longitude": lon_chunk.tolist(), | ||
"hourly": weather_variables, | ||
"start_date": start_date, | ||
"end_date": end_date | ||
} | ||
try: | ||
responses = self.openmeteo.weather_api(url, params=params) | ||
|
||
except: | ||
break | ||
res = [lat for lat in lat_chunk.tolist()] | ||
lat+=res | ||
res = [lon for lon in lat_chunk.tolist()] | ||
lon+=res | ||
# Process responses as needed | ||
for response in responses: | ||
data = { | ||
"latitude": response.Latitude(), | ||
"longitude": response.Longitude(), | ||
"date": pd.date_range( | ||
start=pd.to_datetime(response.Hourly().Time(), unit="s", utc=True), | ||
end=pd.to_datetime(response.Hourly().TimeEnd(), unit="s", utc=True), | ||
freq=pd.Timedelta(seconds=response.Hourly().Interval()), | ||
inclusive="left" | ||
) | ||
} | ||
for var in weather_variables: | ||
data[var] = response.Hourly().Variables(weather_variables.index(var)).ValuesAsNumpy() | ||
|
||
all_data.append(data) | ||
print(len(lat)) | ||
print(all_data[0]["visibility"]) | ||
# Create an xarray dataset from the collected data | ||
dataset = xr.Dataset( | ||
{var: (["latitude", "longitude", "date"], np.array(all_data[i][var])) for i,var in zip(range(len(all_data)),weather_variables)}, | ||
coords={"latitude": lat, "longitude": lon, "date": np.array(all_data[i]["date"] for i in range((len(all_data))))} | ||
) | ||
return dataset | ||
|
||
# Example usage: | ||
fetcher = WeatherDataFetcher() | ||
start_date = "2024-01-01" | ||
end_date = "2024-01-10" | ||
weather_variables = ["temperature_2m", "precipitation", "visibility", "cloud_cover"] | ||
world_grid_data = fetcher.fetch_world_grid_data(start_date, end_date, weather_variables) | ||
print(world_grid_data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jacobbieker,
I'm encountering an issue while creating an xarray dataset with the OpenMeteo data due to dimension problems. Although I'm able to successfully fetch datasets for multiple coordinates, I'm facing challenges with dimension handling. although the len of dims are same, still!
I'm planning to add an argument for NWP (Numerical Weather Prediction) if we need to specify a particular NWP in the function. What do you think about this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is a bit hard to debug from this, but if you add to each data point the
coord
latitude and longitude, that might then work to reshape into a grid?For adding an argument to specify the NWP, that is perfect! We want to be able to access all the NWPs from OpenMeteo from this, so that would be ideal.