基于Python下载网格化CHIRPS数据集
Hey there! Since you're totally new to satellite/image data handling, let's break this down step by step—no jargon overload, promise. We'll cover downloading the data first, then organizing it, and finally basic processing tools to get you started.
The CHIRPS Africa daily dataset has thousands of files (one per day from 1981-2020), so manual downloading via browser isn't practical. Here are two reliable methods:
Option 1: Command-line bulk download (recommended)
Use wget (a free command-line tool for downloading files) to grab all the .tif files at once:
- Install wget:
- On Windows: Download from the official site or use Chocolatey (
choco install wget) - On macOS: Use Homebrew (
brew install wget) - On Linux: It's usually pre-installed, but if not, run
sudo apt install wget(Debian/Ubuntu) orsudo dnf install wget(Fedora)
- On Windows: Download from the official site or use Chocolatey (
- Run the download command:
Open your terminal/command prompt, navigate to the folder where you want to save the data, then run:
Let's break down the flags so you know what's happening:wget -r -np -nH --cut-dirs=5 -A "*.tif" https://data.chc.ucsb.edu/products/CHIRPS-2.0/africa_daily/tifs/p25/-r: Recursively download files from the directory-np: Don't go up to parent directories (avoids downloading unrelated files)-nH: Don't create a folder named after the host URL--cut-dirs=5: Skip the first 5 levels of the URL path so files save directly to your target folder-A "*.tif": Only download .tif files
- Resume interrupted downloads: If the download stops halfway, add
-cto the command to continue where you left off:wget -c -r -np -nH --cut-dirs=5 -A "*.tif" https://data.chc.ucsb.edu/products/CHIRPS-2.0/africa_daily/tifs/p25/
Option 2: Manual browser download (for testing with a small subset)
If you want to start with just a few files first:
- Go to the Africa daily dataset URL
- Navigate to a specific year/month folder (e.g.,
1981/01/for January 1981) - Click on individual .tif files to download them one by one
With 40 years of daily files, your folder will get messy fast. Use a simple script to sort files into year-based folders:
Bash/Linux/macOS script
Save this as organize_chirps.sh, make it executable (chmod +x organize_chirps.sh), and run it in your data folder:
for file in *.tif; do # Extract the year from the filename (e.g., chirps-v2.0.1981.01.01.tif → 1981) year=$(echo $file | cut -d'_' -f3 | cut -c1-4) # Create the year folder if it doesn't exist mkdir -p $year # Move the file to the year folder mv $file $year/ done
Windows PowerShell script
If you're on Windows, use this PowerShell command:
Get-ChildItem -Filter *.tif | ForEach-Object { $year = $_.Name.Split('_')[2].Substring(0,4) $folder = Join-Path -Path $PWD.Path -ChildPath $year if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder } Move-Item -Path $_.FullName -Destination $folder }
Now that you have the data, here are tools to work with it—starting with beginner-friendly options:
GUI Tool: QGIS (free & open-source)
Perfect for visualizing and doing basic tasks without coding:
- Download and install QGIS from the official site
- Open QGIS, go to
Layer > Add Layer > Add Raster Layer - Select a CHIRPS .tif file—you'll see the precipitation data mapped over Africa
- Use the "Identify Features" tool to click on any location and see the daily precipitation value
Programming Tool: Python (for bulk processing)
If you want to automate tasks (like calculating annual averages or extracting time series for a region), use Python with these libraries:
- Install required libraries via pip:
pip install xarray rioxarray matplotlib - Example: Load a year of data and calculate monthly averages
import xarray as xr import rioxarray as rxr import glob # Load all .tif files from 1981 file_list = sorted(glob.glob("1981/*.tif")) chirps_year = xr.open_mfdataset(file_list, engine="rasterio") # Calculate monthly average precipitation monthly_avg = chirps_year.resample(time="M").mean() # Save the monthly average to a new .tif file monthly_avg.rio.to_raster("1981_monthly_precip_avg.tif") # Plot the first month's average monthly_avg.isel(time=0).plot(cmap="Blues")
- Start small: Test workflows with one year of data first before tackling the full 40-year dataset—this saves time and avoids overwhelming your computer
- Check storage: Each .tif file is ~1MB, so 40 years of data is roughly 14,600 files (~14GB total)—make sure you have enough disk space
- Ask targeted questions: If you hit specific errors (e.g., wget failing, QGIS not loading files, Python code throwing exceptions), share the exact error message and what you tried—this makes troubleshooting easier
内容的提问来源于stack exchange,提问作者Sillians

