0% found this document useful (0 votes)
7 views2 pages

ERA5RAIN

The document outlines a Python script that retrieves monthly and annual precipitation data from the Copernicus Climate Data Store (CDS) for a specified geographical region and time period (1960-2024). It uses the CDS API to download total precipitation data, processes it using the xarray library, and saves the results in NetCDF format. The script creates directories for storing the data and prints the list of saved files after processing each year.

Uploaded by

Alban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

ERA5RAIN

The document outlines a Python script that retrieves monthly and annual precipitation data from the Copernicus Climate Data Store (CDS) for a specified geographical region and time period (1960-2024). It uses the CDS API to download total precipitation data, processes it using the xarray library, and saves the results in NetCDF format. The script creates directories for storing the data and prints the list of saved files after processing each year.

Uploaded by

Alban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import os

import cdsapi
import xarray as xr

# Coordinates for the region


upper_left_lat = 43.0
upper_left_lon = 17.0
lower_right_lat = 39.0
lower_right_lon = 22.0

# Initialize the CDS API client


c = cdsapi.Client('https://cds.climate.copernicus.eu/api/v2','129357:d98e89a5-90a0-
4c52-9b2f-fcbb956473d3')

# Specify the range of years


start_year = 1960
end_year = 2024

# Create directories for monthly and annual precipitation sums


monthly_folder = r'E:\Reshjet_era5\Monthly'
annual_folder = r'E:\Reshjet_era5\Annual'

# Ensure the directories exist


os.makedirs(monthly_folder, exist_ok=True)
os.makedirs(annual_folder, exist_ok=True)

# Loop over each year and retrieve the data


for year in range(start_year, end_year + 1):
year_str = str(year)

# Create an empty list to store the monthly precipitation sum datasets


monthly_precipitation_datasets = []

for month in range(1, 13):


month_str = str(month).zfill(2) # Zero-padding month number

# Request data from CDS API


output_filename = os.path.join(monthly_folder,
f'{year_str}monthly_precipitation_sum{month_str}.nc')
c.retrieve(
'reanalysis-era5-land',
{
'variable': 'total_precipitation',
'year': year_str,
'month': month_str,
'time': '23:00',
'day': [
'01', '02', '03', '04', '05', '06',
'07', '08', '09', '10', '11', '12',
'13', '14', '15', '16', '17', '18',
'19', '20', '21', '22', '23', '24',
'25', '26', '27', '28', '29', '30', '31',
],
'format': 'netcdf',
'area': [upper_left_lat, upper_left_lon, lower_right_lat,
lower_right_lon],
},
output_filename)
# Open the downloaded NetCDF file using xarray
dataset = xr.open_dataset(output_filename)

# Extract the precipitation data


precipitation_data = dataset['tp']

# Convert precipitation from meters to millimeters


precipitation_data *= 1000

# Calculate the monthly sum


monthly_precipitation_sum = precipitation_data.sum(dim='time')

# Save the monthly sum to a new dataset


monthly_sum_filename = os.path.join(monthly_folder,
f'{year_str}monthly_precipitation_sum{month_str}.nc')
monthly_sum_dataset = xr.Dataset(
{'monthly_precipitation_sum': monthly_precipitation_sum},
coords={'latitude': precipitation_data.latitude, 'longitude':
precipitation_data.longitude}
)
monthly_sum_dataset.to_netcdf(monthly_sum_filename)

# Save each month's precipitation sum dataset


monthly_precipitation_datasets.append(monthly_sum_filename)

# Close the dataset


dataset.close()

# Calculate the annual precipitation by summing up monthly values


annual_precipitation = xr.open_mfdataset(monthly_precipitation_datasets,
combine='nested', concat_dim='month')
annual_precipitation_sum =
annual_precipitation['monthly_precipitation_sum'].sum(dim='month')

# Save the annual sum to a new dataset


annual_sum_filename = os.path.join(annual_folder,
f'{year_str}_annual_precipitation_sum.nc')
annual_sum_dataset = xr.Dataset(
{'annual_precipitation_sum': annual_precipitation_sum},
coords={'latitude': precipitation_data.latitude, 'longitude':
precipitation_data.longitude}
)
annual_sum_dataset.to_netcdf(annual_sum_filename)

# Close the annual precipitation dataset


annual_precipitation.close()

# Print the list of saved monthly sum files for the current year
print(f"Year: {year_str}")
print(f"Monthly sum files: {monthly_precipitation_datasets}")
print(f"Annual sum file: {annual_sum_filename}")

print("Data download and processing completed.")

You might also like