NumPy for Climate Scientists & Meteorologists¶
This notebook teaches NumPy skills mapped to climate workflows with fully synthetic arrays.
Requirements¶
- Python 3.8+
- numpy (and matplotlib for optional plots)
0) Setup¶
# Set working directory
import os
os.chdir("c:\\Users\\yonas\\Documents\\ICPAC\\python-climate")
processed_data_dir = os.path.join("data", "processed")
raw_data_dir = os.path.join("data", "raw")
1. Creating Arrays¶
Manual construction of arrays¶
2-Dimensional Arrays¶
3-Dimensional Arrays¶
2. Functions for creating arrays¶
Evenly spaced arrays:¶
Common Arrays¶
Ones¶
Zeros¶
Diagonal Matrix¶
Random numbers¶
Uniform¶
Gaussian¶
3. Array Examples¶
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
360 361 362 363 364 365]
# Random Number Generation
rng = np.random.default_rng(0)
# Draw random samples from a normal (Gaussian) distribution.
noise = rng.normal(0.0,0.8,size=(time.size, lat.size, lon.size))
# plot seasonal cycle
import matplotlib.pyplot as plt
plt.plot(doy, seasonal[:,0,0])
plt.xlabel('Day of Year')
plt.ylabel('Seasonal Cycle (°C)')
plt.title('Seasonal Cycle of Temperature')
# plot the latitudinal gradient
# plt.plot(lat, lat_grad[0,:,0])
# plt.xlabel('Latitude (°)')
# plt.ylabel('Latitudinal Gradient (°C)')
# plt.title('Latitudinal Gradient of Temperature')
# Combine all components to create synthetic 2m temperature data
t2m = 24.0 + seasonal + lat_grad + noise
t2m.dtype
# plot t2m for a specific lat/lon point
plt.plot(doy, t2m[:,5,7])
plt.xlabel('Day of Year')
plt.ylabel('2m Temperature (°C)')
plt.title('Synthetic 2m Temperature Time Series at Lat 8°, Lon 40°')
# plot 2m temperature for all latitudes at a specific day
plt.plot(lat, t2m[200,:,7])
plt.xlabel('Latitude (°)')
plt.ylabel('2m Temperature (°C)')
plt.title('Synthetic 2m Temperature on Day 200 at Lon 40°')
# plot 2m temperature for all longitudes at a specific day
plt.plot(lon, t2m[200,5,:])
plt.xlabel('Longitude (°)')
plt.ylabel('2m Temperature (°C)')
plt.title('Synthetic 2m Temperature on Day 200 at Lat 8°')
# plot 2m temperature for all latitudes and all longitudes at a specific day
plt.pcolormesh(t2m[200,:,:])
plt.colorbar(label='2m Temperature (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Synthetic 2m Temperature on Day 200')
plt.show()
# plot 2m temperature for all latitudes and all longitudes at a specific day
plt.contourf(t2m[200,:,:], # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower',
aspect='auto')
plt.colorbar(label='2m Temperature (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Synthetic 2m Temperature on Day 200')
plt.show()
C:\Users\yonas\AppData\Local\Temp\ipykernel_141320\973129106.py:2: UserWarning: The following kwargs were not used by contour: 'aspect'
plt.contourf(t2m[200,:,:], # imshow, contourf
4. Indexing, Slicing & Masking¶
array([False, False, True, True, True, True, True, True, True,
True, True, True, True, False, False])
array([[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True],
[ True, True, True, True, True, True, True, True, True,
True, True]])
plt.contourf(hot_mask[:,:], # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower',
aspect='auto')
plt.colorbar(label='2m Temperature (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Synthetic 2m Temperature on Day 200')
plt.show()
C:\Users\yonas\AppData\Local\Temp\ipykernel_141320\415729875.py:1: UserWarning: The following kwargs were not used by contour: 'aspect'
plt.contourf(hot_mask[:,:], # imshow, contourf
# plot the 2tm that satisfy the hot mask
plt.contourf(t2m_sub[200,:,:]*hot_mask, # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower')
plt.colorbar(label='2m Temperature (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Synthetic 2m Temperature on Day 200')
plt.show()
# plot climatology
plt.contourf(clim_mean, # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower')
plt.colorbar(label='2m Temperature (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Climatology of Synthetic 2m Temperature')
plt.show()
### plot the anomaly for day 200
plt.contourf(t2m_anom[200,:,:], # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower')
plt.colorbar(label='2m Temperature Anomaly (°C)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Anomaly of Synthetic 2m Temperature on Day 200')
plt.show()
5) Broadcasting & Vectorization¶
# create u and v wind components
u = rng.normal(0.0,4.0,size=t2m.shape)
v = rng.normal(0.0,4.0,size=t2m.shape)
# plot the wind speed
plt.contourf(wind_speed[200,:,:], # imshow, contourf
extent=(lon.min(), lon.max(), lat.min(), lat.max()),
origin='lower')
plt.colorbar(label='Wind Speed (m/s)')
plt.xlabel('Longitude (°)')
plt.ylabel('Latitude (°)')
plt.title('Wind Speed on Day 200')
plt.show()
6. Reductions & Area Weights¶
# Calculate weighted mean
weighted_mean = np.sum(t2m*w, axis=(1,2)) / np.sum(w, axis=(1,2))
weighted_mean.shape
7. Linear Algebra (EOF/PCA prep)¶
# Compute the second principal component
pc2 = U[:,1]*s[1]
# Compute the third principal component
pc3 = U[:,2]*s[2]
# Compute the second EOF
eof2 = VT[1].reshape(NY,NX)
# Compute the third EOF
eof3 = VT[2].reshape(NY,NX)
8. I/O with NumPy¶
# Save t2m_anom to a .npy file
np.save("data/processed/t2m_anom.npy", t2m_anom)
np.save("data/processed/pc1.npy", pc1)
9. Working with Time¶
array(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
'2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
'2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12',
'2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',
'2020-01-17', '2020-01-18', '2020-01-19', '2020-01-20',
'2020-01-21', '2020-01-22', '2020-01-23', '2020-01-24',
'2020-01-25', '2020-01-26', '2020-01-27', '2020-01-28',
'2020-01-29', '2020-01-30', '2020-01-31', '2020-02-01',
'2020-02-02', '2020-02-03', '2020-02-04', '2020-02-05',
'2020-02-06', '2020-02-07', '2020-02-08', '2020-02-09',
'2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13',
'2020-02-14', '2020-02-15', '2020-02-16', '2020-02-17',
'2020-02-18', '2020-02-19', '2020-02-20', '2020-02-21',
'2020-02-22', '2020-02-23', '2020-02-24', '2020-02-25',
'2020-02-26', '2020-02-27', '2020-02-28', '2020-02-29',
'2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04',
'2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08',
'2020-03-09', '2020-03-10', '2020-03-11', '2020-03-12',
'2020-03-13', '2020-03-14', '2020-03-15', '2020-03-16',
'2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20',
'2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24',
'2020-03-25', '2020-03-26', '2020-03-27', '2020-03-28',
'2020-03-29', '2020-03-30', '2020-03-31', '2020-04-01',
'2020-04-02', '2020-04-03', '2020-04-04', '2020-04-05',
'2020-04-06', '2020-04-07', '2020-04-08', '2020-04-09',
'2020-04-10', '2020-04-11', '2020-04-12', '2020-04-13',
'2020-04-14', '2020-04-15', '2020-04-16', '2020-04-17',
'2020-04-18', '2020-04-19', '2020-04-20', '2020-04-21',
'2020-04-22', '2020-04-23', '2020-04-24', '2020-04-25',
'2020-04-26', '2020-04-27', '2020-04-28', '2020-04-29',
'2020-04-30', '2020-05-01', '2020-05-02', '2020-05-03',
'2020-05-04', '2020-05-05', '2020-05-06', '2020-05-07',
'2020-05-08', '2020-05-09', '2020-05-10', '2020-05-11',
'2020-05-12', '2020-05-13', '2020-05-14', '2020-05-15',
'2020-05-16', '2020-05-17', '2020-05-18', '2020-05-19',
'2020-05-20', '2020-05-21', '2020-05-22', '2020-05-23',
'2020-05-24', '2020-05-25', '2020-05-26', '2020-05-27',
'2020-05-28', '2020-05-29', '2020-05-30', '2020-05-31',
'2020-06-01', '2020-06-02', '2020-06-03', '2020-06-04',
'2020-06-05', '2020-06-06', '2020-06-07', '2020-06-08',
'2020-06-09', '2020-06-10', '2020-06-11', '2020-06-12',
'2020-06-13', '2020-06-14', '2020-06-15', '2020-06-16',
'2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20',
'2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24',
'2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28',
'2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02',
'2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06',
'2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10',
'2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14',
'2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18',
'2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22',
'2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26',
'2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30',
'2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03',
'2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07',
'2020-08-08', '2020-08-09', '2020-08-10', '2020-08-11',
'2020-08-12', '2020-08-13', '2020-08-14', '2020-08-15',
'2020-08-16', '2020-08-17', '2020-08-18', '2020-08-19',
'2020-08-20', '2020-08-21', '2020-08-22', '2020-08-23',
'2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27',
'2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31',
'2020-09-01', '2020-09-02', '2020-09-03', '2020-09-04',
'2020-09-05', '2020-09-06', '2020-09-07', '2020-09-08',
'2020-09-09', '2020-09-10', '2020-09-11', '2020-09-12',
'2020-09-13', '2020-09-14', '2020-09-15', '2020-09-16',
'2020-09-17', '2020-09-18', '2020-09-19', '2020-09-20',
'2020-09-21', '2020-09-22', '2020-09-23', '2020-09-24',
'2020-09-25', '2020-09-26', '2020-09-27', '2020-09-28',
'2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02',
'2020-10-03', '2020-10-04', '2020-10-05', '2020-10-06',
'2020-10-07', '2020-10-08', '2020-10-09', '2020-10-10',
'2020-10-11', '2020-10-12', '2020-10-13', '2020-10-14',
'2020-10-15', '2020-10-16', '2020-10-17', '2020-10-18',
'2020-10-19', '2020-10-20', '2020-10-21', '2020-10-22',
'2020-10-23', '2020-10-24', '2020-10-25', '2020-10-26',
'2020-10-27', '2020-10-28', '2020-10-29', '2020-10-30',
'2020-10-31', '2020-11-01', '2020-11-02', '2020-11-03',
'2020-11-04', '2020-11-05', '2020-11-06', '2020-11-07',
'2020-11-08', '2020-11-09', '2020-11-10', '2020-11-11',
'2020-11-12', '2020-11-13', '2020-11-14', '2020-11-15',
'2020-11-16', '2020-11-17', '2020-11-18', '2020-11-19',
'2020-11-20', '2020-11-21', '2020-11-22', '2020-11-23',
'2020-11-24', '2020-11-25', '2020-11-26', '2020-11-27',
'2020-11-28', '2020-11-29', '2020-11-30', '2020-12-01',
'2020-12-02', '2020-12-03', '2020-12-04', '2020-12-05',
'2020-12-06', '2020-12-07', '2020-12-08', '2020-12-09',
'2020-12-10', '2020-12-11', '2020-12-12', '2020-12-13',
'2020-12-14', '2020-12-15', '2020-12-16', '2020-12-17',
'2020-12-18', '2020-12-19', '2020-12-20', '2020-12-21',
'2020-12-22', '2020-12-23', '2020-12-24', '2020-12-25',
'2020-12-26', '2020-12-27', '2020-12-28', '2020-12-29',
'2020-12-30'], dtype='datetime64[D]')
array([2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020])
10. Numpy Help¶
On the web: https://numpy.org/doc/
Asking For Help¶
📝 Test Your Knowledge¶
Ready to test your NumPy skills for climate data analysis? Take the interactive quiz to assess your understanding of array operations, broadcasting, reductions, and more.