從 HDF5 資料集建立 Dask 陣列
你要分析過去 40 年的歐洲降雨量。題目已提供歐洲各地網格位置的逐月平均降雨量,格式為 HDF5。由於檔案相當大,你決定使用 Dask 來載入並處理。
已為你匯入 h5py,且 dask.array 以 da 匯入。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 使用
h5py開啟'data/era_eu.hdf5'檔案。 - 使用
from_array()將'/precip'變數載入為 Dask 陣列,並設定分塊為(12 個月、15 個緯度、15 個經度)。 - 以陣列切片在第一個軸上每隔 12 個取一個索引——這會選出所有年份的 1 月資料。
- 對
january_rainfalls沿時間軸(軸0)取平均,計算全歐洲 1 月的平均降雨量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Open the HDF5 dataset using h5py
hdf5_file = ____.____(____)
# Load the file into a Dask array with a reasonable chunk size
precip = da.____(____, chunks=____)
# Select only the months of January
january_rainfalls = ____[____]
# Calculate the mean rainfall in January for each location
january_mean_rainfall = ____.____(axis=____)
plt.imshow(january_mean_rainfall.compute())
plt.show()