Skip to content
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

Download data with python instead of bash #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
plot/
.env
.env
data/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DGMR_PLOT_PATH="<path_to_save_the_plots>" # If empty, forecast GIFs will be sav

### Downloading Data with Meteo-France's API

Simply run the script `./dgmr/download_data.sh` to download the latest radar images.
Simply run the script `python download_data.py` to download the latest radar image.

### Setting up a Cron Job for Automated Data Download

Expand All @@ -81,9 +81,9 @@ To automate the data downloading process using a cron job, follow these steps:

1. Open your terminal and type `crontab -e` to edit the cron table.

2. Add a new line at the end of the file to define the cron job. The general syntax for a cron task is:
2. Add a new line at the end of the file to define the cron job. The syntax for the cron task is:
```bash
*/5 * * * * <ABSOLUTE_PATH_TO_REPO>/dgmr/download_data.sh
*/5 * * * * micromamba activate dgmr && cd <ABSOLUTE_PATH_TO_REPO> && python download_data.py >> cron_output.txt 2>&1
```

### Making Forecasts
Expand Down
2 changes: 1 addition & 1 deletion dgmr/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def get_list_files(date: dt.datetime) -> List[Path]:
delta = dt.timedelta(minutes=TIMESTEP)
dates = [date + i * delta for i in range(-INPUT_STEPS + 1, 1)]
filenames = [d.strftime("%Y_%m_%d_%H_%M.h5") for d in dates]
filenames = [d.strftime("%Y-%m-%d_%Hh%M.h5") for d in dates]
return [DATA_PATH / f for f in filenames]


Expand Down
58 changes: 58 additions & 0 deletions download_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import datetime as dt
import os
import time
from pathlib import Path

import requests
from dotenv import load_dotenv

# Load environment variables from .env file if it exists
dotenv_path = Path(".env")
if dotenv_path.is_file():
load_dotenv(dotenv_path)

METEO_FRANCE_DATA_PATH = Path(os.getenv("METEO_FRANCE_DATA_PATH", "data"))
METEO_FRANCE_DATA_PATH.mkdir(parents=True, exist_ok=True)

METEO_FRANCE_API_URL = (
"https://public-api.meteofrance.fr/public/DPRadar/v1/mosaiques/"
"METROPOLE/observations/LAME_D_EAU/produit?maille=500"
)
METEO_FRANCE_API_KEY = os.getenv("METEO_FRANCE_API_KEY")


max_retries = 20
retry_delay = 3
retry_count = 0
success = False

while retry_count < max_retries:
response = requests.get(
METEO_FRANCE_API_URL,
headers={
"accept": "application/octet-stream+gzip",
"apikey": METEO_FRANCE_API_KEY,
},
)

if response.status_code == 200:
now = dt.datetime.now(dt.timezone.utc)
now = now - dt.timedelta( # round date to 5 minutes
minutes=now.minute % 5,
seconds=now.second,
microseconds=now.microsecond,
)
output_file = METEO_FRANCE_DATA_PATH / now.strftime("%Y-%m-%d_%Hh%M.h5")
with open(output_file, "wb") as file:
file.write(response.content)
success = True
break
else:
print(f"Attempt {retry_count + 1} failed. Retry in {retry_delay} seconds...")
retry_count += 1
time.sleep(retry_delay)

if success:
print(f"Downloaded successfully {output_file.name}.")
else:
print(f"Download failed after {max_retries} attempts.")
7 changes: 7 additions & 0 deletions run_download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Activate the micromamba environment
source /usr/local/bin/micromamba
micromamba activate dgmr

python download_data.py