climate_data
Climate Data
This package contains modules for extracting, processing, harmonizing, and downscaling climate data. It sources historical climate data from the European Centre for Medium-Range Weather Forecasts (ECMWF) ERA5 dataset and future climate data from the Coupled Model Intercomparison Project Phase 6 (CMIP6).
cli
cdrun() -> None
cli_options
Climate Data CLI Options
This module provides a set of CLI options for extracting climate data from the ERA5 and CMIP6 datasets. These options are used to specify the data to extract, such as the year, month, variable, and dataset. It also provides global variables representing the full space of valid values for these options.
with_year(years: Collection[str], *, allow_all: bool = False) -> Callable[[Callable[P, T]], Callable[P, T]]
Create a CLI option for selecting a year.
Source code in src/climate_data/cli_options.py
data
Climate Data Management
This module provides a class for managing the climate data used in the project. It includes methods for loading and saving data, as well as for accessing the various directories where data is stored. This abstraction allows for easy access to the data and ensures that all data is stored in a consistent and organized manner. It also provides a central location for managing the data, which makes it easier to update and maintain the path structure of the data as needed.
This module generally does not load or process data itself, though some exceptions are made for metadata which is generally loaded and cached on disk.
ClimateData
Class for managing the climate data used in the project.
Source code in src/climate_data/data.py
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 | |
save_parquet(df: pd.DataFrame, output_path: str | Path) -> None
Save a pandas DataFrame to a file with standard parameters.
Parameters
df The DataFrame to save. output_path The path to save the DataFrame to.
Source code in src/climate_data/data.py
save_raster(raster: rt.RasterArray, output_path: str | Path, num_cores: int = 1, **kwargs: Any) -> None
Save a raster to a file with standard parameters.
Parameters
raster The raster to save. output_path The path to save the raster to. num_cores The number of cores to use for compression.
Source code in src/climate_data/data.py
save_raster_to_cog(raster: rt.RasterArray, output_path: str | Path, num_cores: int = 1, resampling: str = 'nearest') -> None
Save a raster to a COG file.
A COG file is a cloud-optimized GeoTIFF that is optimized for use in cloud storage systems. This function saves the raster to a COG file with the specified resampling method.
Parameters
raster The raster to save. output_path The path to save the raster to. num_cores The number of cores to use for compression. resampling The resampling method to use when building the overviews.
Source code in src/climate_data/data.py
save_xarray(ds: xr.Dataset, output_path: str | Path, encoding_kwargs: dict[str, Any]) -> None
Save an xarray dataset to a file with standard parameters.
Parameters
ds The dataset to save. output_path The path to save the dataset to. encoding_kwargs The encoding parameters to use when saving the dataset.
Source code in src/climate_data/data.py
extract
Climate Data Extraction
This module contains pipelines for extracting climate data from various sources.
cmip6
CMIP6 Data Extraction
extract_cmip6(cmip6_source: list[str], cmip6_experiment: list[str], cmip6_variable: list[str], output_dir: str, queue: str, overwrite: bool) -> None
Extract CMIP6 data.
Extracts CMIP6 data for the given source, experiment, and variable. We use the
the table at https://www.nature.com/articles/s41597-023-02549-6/tables/3 to determine
which CMIP6 source_ids to include. See ClimateData.load_koppen_geiger_model_inclusion
to load and examine this table. The extraction criteria does not completely
capture model inclusion criteria as it does not account for the year range avaialable
in the data. This determiniation is made when we proccess the data in later steps.
Source code in src/climate_data/extract/cmip6.py
load_cmip_data(zarr_path: str) -> xr.Dataset
Loads a CMIP6 dataset from a zarr path.
Source code in src/climate_data/extract/cmip6.py
elevation
extract_elevation(model_name: str, output_dir: str, queue: str) -> None
Download elevation data from Open Topography.
Source code in src/climate_data/extract/elevation.py
extract_elevation_task(model_name: str, lat_start: int, lon_start: int, output_dir: str) -> None
Download elevation data from Open Topography.
Source code in src/climate_data/extract/elevation.py
era5
ERA5 Data Extraction
generate
utils
buck_vapor_pressure(temperature_c: xr.Dataset) -> xr.Dataset
Approximate vapor pressure of water.
https://en.wikipedia.org/wiki/Arden_Buck_equation https://journals.ametsoc.org/view/journals/apme/20/12/1520-0450_1981_020_1527_nefcvp_2_0_co_2.xml
Parameters
temperature_c Temperature in Celsius
Returns
xr.Dataset Vapor pressure in hPa
Source code in src/climate_data/generate/utils.py
identity(ds: xr.Dataset) -> xr.Dataset
interpolate_to_target_latlon(ds: xr.Dataset, method: str = 'nearest', target_lon: xr.DataArray = cdc.TARGET_LONGITUDE, target_lat: xr.DataArray = cdc.TARGET_LATITUDE) -> xr.Dataset
Interpolate a dataset to a target latitude and longitude grid.
Parameters
ds Dataset to interpolate method Interpolation method target_lon Target longitude grid target_lat Target latitude grid
Returns
xr.Dataset Interpolated dataset
Source code in src/climate_data/generate/utils.py
kelvin_to_celsius(temperature_k: xr.Dataset) -> xr.Dataset
Convert temperature from Kelvin to Celsius
Parameters
temperature_k Temperature in Kelvin
Returns
xr.Dataset Temperature in Celsius
Source code in src/climate_data/generate/utils.py
meter_to_millimeter(rainfall_m: xr.Dataset) -> xr.Dataset
Convert rainfall from meters to millimeters
Parameters
rainfall_m Rainfall in meters
Returns
xr.Dataset Rainfall in millimeters
Source code in src/climate_data/generate/utils.py
precipitation_flux_to_rainfall(precipitation_flux: xr.Dataset) -> xr.Dataset
Convert precipitation flux to rainfall
Parameters
precipitation_flux Precipitation flux in kg m-2 s-1
Returns
xr.Dataset Rainfall in mm/day
Source code in src/climate_data/generate/utils.py
rh_percent(temperature_c: xr.Dataset, dewpoint_temperature_c: xr.Dataset) -> xr.Dataset
Calculate relative humidity from temperature and dewpoint temperature.
Parameters
temperature_c Temperature in Celsius dewpoint_temperature_c Dewpoint temperature in Celsius
Returns
xr.Dataset Relative humidity as a percentage
Source code in src/climate_data/generate/utils.py
scale_wind_speed_height(wind_speed_10m: xr.Dataset) -> xr.Dataset
Scaling wind speed from a height of 10 meters to a height of 2 meters
Reference: Bröde et al. (2012) https://doi.org/10.1007/s00484-011-0454-1
Parameters
wind_speed_10m The 10m wind speed [m/s]. May be signed (ie a velocity component)
Returns
xr.DataSet The 2m wind speed [m/s]. May be signed (ie a velocity component)
Source code in src/climate_data/generate/utils.py
vector_magnitude(x: xr.Dataset, y: xr.Dataset) -> xr.Dataset
utils
Climate Data Utilities
Utility functions for working with climate data.
make_raster_template(x_min: int | float, y_min: int | float, stride: int | float, resolution: int | float, crs: str = 'EPSG:4326') -> rt.RasterArray
Create a raster template with the specified dimensions and resolution.
A raster template is a RasterArray with a specified extent, resolution, and CRS. The data values are initialized to zero. This function is useful for creating a template to use when resampling another raster to a common grid.
Parameters
x_min The minimum x-coordinate of the raster. y_min The minimum y-coordinate of the raster. stride The length of one side of the raster in the x and y directions measured in the units of the provided coordinate reference system. resolution The resolution of the raster in the units of the provided coordinate reference system. crs The coordinate reference system of the generated raster.
Returns
rt.RasterArray A raster template with the specified dimensions and resolution.
Source code in src/climate_data/utils.py
to_raster(ds: xr.DataArray, no_data_value: float | int, lat_col: str = 'lat', lon_col: str = 'lon', crs: str = 'EPSG:4326') -> rt.RasterArray
Convert an xarray DataArray to a RasterArray.
Parameters
ds The xarray DataArray to convert. no_data_value The value to use for missing data. This should be consistent with the dtype of the data. lat_col The name of the latitude coordinate in the dataset. lon_col The name of the longitude coordinate in the dataset. crs The coordinate reference system of the data.
Returns
rt.RasterArray The RasterArray representation of the input data.