Friday, 17 October 2025

How to download the CMIP6 projected climate change weather data? Using legacy CDS

I found a good article on how to download and process the CMIP6 for projecting the climate change data. The original article can be seen here (https://www.linkedin.com/pulse/how-download-cmip6-projected-climate-change-weather-data-callejo-pyqlc/). The article is as follows:

Downloading the CMIP6 data

  1. Create an account in the Climate Change Service of European Union's Copernicus. Proceed to step #2 if you already have an active account.
  2. Login to the Climate Change Service.
  3. Proceed to the download form for the CMIP6 climate projections catalogue.
  4. Select your preferred Temporal resolution. Might as well select Daily. There will be an example below on processing of this daily data.
  5. Select the Experiment. This refers to your preferred climate scenario or SSP.
  6. Select the Variable. This refers to the data you want to download. This could be temperature, precipitation, relative humidity, etc. Might as well select Precipitation. There will be an example below on processing of this precipitation data.
  7. Select your preferred Model.
  8. Select your preferred Year(s), Month(s), and Day(s).
  9. Choose Sub-region extraction on the Geographical area
  10. Input the North field with the northernmost latitude of your target area. The value should be in decimal degrees with WGS84 EPSG:4326 projection which ranges from -90 to 90.
  11. Input the West field with the westernmost longitude of your target area. The value should be in decimal degrees with WGS84 EPSG:4326 projection which ranges from -180 to 180.
  12. Input the East field with the easternmost longitude of your target area. The value should be in decimal degrees with WGS84 EPSG:4326 projection which ranges from -180 to 180.
  13. Input the South field with the southernmost latitude of your target area. The value should be in decimal degrees with WGS84 EPSG:4326 projection which ranges from -90 to 90.
  14. Click the Submit Form button. Your product/data request will then be with an "In progress" status.
  15. Wait for your product/data request to become ready for download.
  16. Click the Download button of your product/data request.

Although you are done with downloading the CMIP6 data, these data are usually not immediately usable. It will need further processing to actually make use of this data.

Converting the CMIP6 ZIP data into the classic CSV data

  1. Unzip or decompress the downloaded ZIP data. This process could vary depending on whether you are using a Windows or Mac computer.
  2. Look for the NetCDF file that has an extension of .nc like so: pr_example.nc. That file contains the actual climate data. Don't do anything with the file yet. Just be familiarize of where you can find it.
  3. Install Python in preparation for processing NetCDF files.
  4. Install the Python package called "netCDF4". You may do so via the command-line with `pip install netCDF4`
  5. Create the file nc2csv.py Python script for converting NetCDF file into CSV. Please see the contents of the nc2csv.py file in the next section.
  6. Run the nc2csv.py script. You may do so via the command-line with `python /path/to/nc2csv.py`.

nc2csv.py

import netCDF4 as nc
import calendar
import datetime
import csv

# Open the .nc files
dataset_precipitation = nc.Dataset('C:\path\to\pr_example.nc')

time = dataset_precipitation.variables['time'][:]
lon = dataset_precipitation.variables['lon'][:]
lat = dataset_precipitation.variables['lat'][:]
pr = dataset_precipitation.variables['pr'][:]

print("Processing data...")

headers = ['Date', 'Longitude', 'Latitude', 'Precipitation']

with open('C:\path\to\data.csv', 'w', encoding='UTF8', newline='') as f:
	writer = csv.writer(f)
	writer.writerow(headers)
	for i in range(0, len(time)-1):

		# Convert NetCDF's numeric time into ISO8601 time format
		epoch = datetime.datetime(1850, 1, 1)
		timestamp = calendar.timegm( epoch.timetuple() ) + ( time[i] * 86400 )
		iso_datetime = datetime.datetime.fromtimestamp( timestamp ).strftime('%Y-%m-%d')

		for j in range(0, len(lat)-1):
			for k in range(0, len(lon)-1):
				# Prepare the row data
				row = [ iso_datetime, lon[k], lat[j], '{:f}'.format(pr[i][j][k]) ]

				# Store the row data into the CSV
				writer.writerow(row)

		print(iso_datetime)

# Close the dataset
dataset_precipitation.close()

print("Done processing.")

Before running the script, make sure to replace the following:

  • `C:\path\to\pr_example.nc` with the actual path to your NetCDF file.
  • `C:\path\to\data.csv` with the actual path to the target outcome CSV file.

This should do the trick! However for me, I usually store the data into my PostgreSQL server instead of having it as CSV. This is because with PostgreSQL, it would be much easier to retrieve the data you wanted and at the format you wanted. However, that is something no longer part of this tutorial.

No comments:

Post a Comment