import os import urllib.request from datetime import datetime, timedelta # Base URL for the files base_url = "https://gws-access.jasmin.ac.uk/public/tamsat/rfe/data_degraded/v3.1/daily/0.25" # Define the start and end year start_year = 2003 end_year = 2025 # List of months to loop over (September to January) months = ["09", "10", "11", "12", "01"] # Define the output directory output_dir = "/nird/datalake/NS9853K/kolstad/tamsat" # Function to download the file def download_file(file_url, save_path): try: # Check if the file already exists if os.path.exists(save_path): print(f"File already exists: {save_path}") else: print(f"Downloading {file_url}...") urllib.request.urlretrieve(file_url, save_path) print(f"File saved as {save_path}") except Exception as e: print(f"Error downloading {file_url}: {e}") # Loop over the years from start_year to end_year for year in range(start_year, end_year + 1): for month in months: # Skip months in the years that fall outside of the range if (year == start_year and int(month) < 9) or (year == end_year and int(month) > 1): continue # Set the correct number of days for the month if month in ["09", "04", "06", "09", "11"]: # Months with 30 days days_in_month = 30 elif month == "02": # February (Leap year check) if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)): days_in_month = 29 # Leap year else: days_in_month = 28 # Non-leap year else: # Months with 31 days days_in_month = 31 # Loop through the days of the month for day in range(1, days_in_month + 1): day_str = f"{day:02d}" file_url = f"{base_url}/{year}/{month}/rfe{year}_{month}_{day_str}_0.25.v3.1.nc" file_name = f"rfe{year}_{month}_{day_str}_0.25.v3.1.nc" save_path = os.path.join(output_dir, file_name) # Download the file download_file(file_url, save_path) print("Download complete!")