climate_ref.datasets
#
Dataset handling utilities
CMIP6DatasetAdapter
#
Bases: FinaliseableDatasetAdapterMixin, DatasetAdapter
Adapter for CMIP6 datasets
Source code in packages/climate-ref/src/climate_ref/datasets/cmip6.py
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 | |
find_local_datasets(file_or_directory)
#
Generate a data catalog from the specified file or directory
Each dataset may contain multiple files, which are represented as rows in the data catalog.
Each dataset has a unique identifier, which is in slug_column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_or_directory
|
Path
|
File or directory containing the datasets |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Data catalog containing the metadata for the dataset |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip6.py
get_complete_parser()
#
Return the complete parser that opens files to extract full CMIP6 metadata.
Returns:
| Type | Description |
|---|---|
DatasetParsingFunction
|
Complete CMIP6 parsing function |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip6.py
get_parsing_function()
#
Get the parsing function for CMIP6 datasets based on configuration
The parsing function used is determined by the cmip6_parser configuration value:
- "drs": Use the DRS parser (default)
- "complete": Use the complete parser that extracts all available metadata
Returns:
| Type | Description |
|---|---|
DatasetParsingFunction
|
The appropriate parsing function based on configuration |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip6.py
iter_local_datasets(file_or_directory, chunk_size=10000)
#
Stream the data catalog in chunks to bound peak memory.
Discovery walks the tree once, but parsing and DataFrame construction
happen chunk_size files at a time. Chunks flush at directory
boundaries so files belonging to the same dataset (which share a DRS
version directory) stay together in a single chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_or_directory
|
Path
|
Root of the CMIP6 archive (or a single file) to ingest. |
required |
chunk_size
|
int
|
Soft target for the number of files per chunk. Increasing this trades higher peak memory for fewer per-chunk overheads. |
10000
|
Yields:
| Type | Description |
|---|---|
DataFrame
|
Catalog DataFrames, each containing metadata for one chunk of files. Empty chunks are skipped. |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip6.py
CMIP7DatasetAdapter
#
Bases: FinaliseableDatasetAdapterMixin, DatasetAdapter
Adapter for CMIP7 datasets
Based on CMIP7 Global Attributes v1.0 (DOI: 10.5281/zenodo.17250297).
Source code in packages/climate-ref/src/climate_ref/datasets/cmip7.py
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 | |
find_local_datasets(file_or_directory)
#
Generate a data catalog from the specified file or directory.
Each dataset may contain multiple files, which are represented as rows in the data catalog.
Each dataset has a unique identifier, which is in slug_column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_or_directory
|
Path
|
File or directory containing the datasets |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Data catalog containing the metadata for the dataset |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip7.py
get_complete_parser()
#
Return the complete parser that opens files to extract full CMIP7 metadata.
Returns:
| Type | Description |
|---|---|
DatasetParsingFunction
|
Complete CMIP7 parsing function |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip7.py
get_parsing_function()
#
Get the parsing function for CMIP7 datasets based on configuration
The parsing function used is determined by the cmip7_parser configuration value:
- "drs": Use the DRS parser (default)
- "complete": Use the complete parser that extracts all available metadata
Returns:
| Type | Description |
|---|---|
DatasetParsingFunction
|
The appropriate parsing function based on configuration |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip7.py
iter_local_datasets(file_or_directory, chunk_size=10000)
#
Stream the data catalog in chunks to bound peak memory.
Discovery walks the tree once, but parsing and DataFrame construction
happen chunk_size files at a time. Chunks flush at directory
boundaries so files belonging to the same dataset (which share a DRS
version directory) stay together in a single chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_or_directory
|
Path
|
Root of the CMIP7 archive (or a single file) to ingest. |
required |
chunk_size
|
int
|
Soft target for the number of files per chunk. Increasing this trades higher peak memory for fewer per-chunk overheads. |
10000
|
Yields:
| Type | Description |
|---|---|
DataFrame
|
Catalog DataFrames, each containing metadata for one chunk of files. Empty chunks are skipped. |
Source code in packages/climate-ref/src/climate_ref/datasets/cmip7.py
DatasetAdapter
#
Bases: Protocol
An adapter to provide a common interface for different dataset types
This allows the same code to work with different dataset types.
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
columns_requiring_finalisation = frozenset()
class-attribute
instance-attribute
#
Columns that are not available until the dataset has been finalised.
For adapters that support two-phase parsing (e.g. DRS-only then complete),
these columns will contain pd.NA until finalisation opens the files.
Filtering or grouping on these columns before finalisation will silently
produce incorrect results.
dataset_id_metadata = ()
class-attribute
instance-attribute
#
The group of metadata columns that are specific to the dataset excluding the version information.
Each unique dataset should have the same values for these columns.
This is generally the columns that describe the slug of a dataset,
excluding the version information.
derived_metadata = ()
class-attribute
instance-attribute
#
Columns that are not stored in the database but are computed from stored metadata.
These are reconstructed by :meth:_add_derived_columns whenever a catalog is loaded,
so that a DB-loaded catalog has the same columns as a freshly parsed one
(e.g. CMIP7's branded_variable).
:meth:load_catalog enforces that every column listed here is present after loading.
slug_column
instance-attribute
#
The column in the data catalog that contains the dataset slug. The dataset slug is a unique identifier for the dataset that includes the version of the dataset. This can be used to group files together that belong to the same dataset.
version_metadata = 'version'
class-attribute
instance-attribute
#
The column in the data catalog that contains the version of the dataset.
filter_latest_versions(catalog)
#
Filter a data catalog to only include the latest version of each dataset
Groups by dataset_id_metadata and keeps only the rows where the version
matches the maximum version within each group (lexicographic comparison).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
DataFrame
|
Data catalog to filter |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Filtered data catalog with only the latest version of each dataset |
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
find_local_datasets(file_or_directory)
#
Generate a data catalog from the specified file or directory
This data catalog should contain all the metadata needed by the database. The index of the data catalog should be the dataset slug.
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
load_catalog(db, include_files=True, limit=None)
#
Load the data catalog containing the currently tracked datasets/files from the database
Iterating over different datasets within the data catalog can be done using a groupby
operation for the instance_id column.
Only the latest version of each dataset is returned.
The index of the data catalog is the primary key of the dataset. This should be maintained during any processing.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Data catalog containing the metadata for the currently ingested datasets |
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
pretty_subset(data_catalog)
#
Get a subset of the data_catalog to pretty print
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_catalog
|
DataFrame
|
Data catalog to subset |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Subset of the data catalog to pretty print |
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
register_dataset(db, data_catalog_dataset)
#
Register a dataset in the database using the data catalog
This assumes that the data catalog has already been validated with validate_data_catalog
to ensure that the dataset-specific metadata is consistent across all files in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Database
|
Database instance |
required |
data_catalog_dataset
|
DataFrame
|
A subset of the data catalog containing the metadata for a single dataset |
required |
Raises:
| Type | Description |
|---|---|
RefException
|
If the data catalog contains validation errors that should have been caught by
|
Returns:
| Type | Description |
|---|---|
DatasetRegistrationResult
|
Registration result with dataset and file change information |
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
validate_data_catalog(data_catalog, skip_invalid=False)
#
Validate a data catalog
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_catalog
|
DataFrame
|
Data catalog to validate |
required |
skip_invalid
|
bool
|
If True, ignore datasets with invalid metadata and remove them from the resulting data catalog. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Validated data catalog |
Source code in packages/climate-ref/src/climate_ref/datasets/base.py
IngestionStats
#
Statistics from ingesting datasets into the database
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
__iadd__(other)
#
Accumulate counts in place from another :class:IngestionStats.
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
log_summary(prefix='')
#
Log a summary of the ingestion statistics.
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
Obs4MIPsDatasetAdapter
#
Bases: DatasetAdapter
Adapter for obs4MIPs datasets
Source code in packages/climate-ref/src/climate_ref/datasets/obs4mips.py
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 | |
find_local_datasets(file_or_directory)
#
Generate a data catalog from the specified file or directory
Each dataset may contain multiple files, which are represented as rows in the data catalog.
Each dataset has a unique identifier, which is in slug_column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_or_directory
|
Path
|
File or directory containing the datasets |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Data catalog containing the metadata for the dataset |
Source code in packages/climate-ref/src/climate_ref/datasets/obs4mips.py
PMPClimatologyDatasetAdapter
#
Bases: Obs4MIPsDatasetAdapter
Adapter for climatology datasets post-processed from obs4MIPs datasets by PMP.
These data look like obs4MIPs datasets and are ingested in the same way, but are treated separately as they may have the same metadata as the obs4MIPs datasets.
Source code in packages/climate-ref/src/climate_ref/datasets/pmp_climatology.py
get_dataset_adapter(source_type, **kwargs)
#
Get the appropriate adapter for the specified source type
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_type
|
str
|
Type of source dataset |
required |
Returns:
| Type | Description |
|---|---|
DatasetAdapter
|
DatasetAdapter instance |
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
get_slug_column(source_type)
#
Get the slug column name for a source dataset type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_type
|
SourceDatasetType | str
|
Source dataset type (enum or string value) |
required |
Returns:
| Type | Description |
|---|---|
str
|
The slug column name for the given source type |
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
ingest_datasets(adapter, directory, db, *, data_catalog=None, skip_invalid=True, chunk_size=None)
#
Ingest datasets from a directory into the database.
This is the common ingestion logic shared between the CLI ingest command and provider setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
DatasetAdapter
|
The dataset adapter to use for parsing and registering datasets |
required |
directory
|
Path | None
|
Directory containing the datasets to ingest. Can be None if data_catalog is provided. |
required |
db
|
Database
|
Database instance |
required |
data_catalog
|
DataFrame | None
|
Optional pre-validated data catalog. If provided, directory is ignored and the catalog is used directly.
This avoids redundant find/validate operations.
When supplied, |
None
|
skip_invalid
|
bool
|
If True, skip datasets that fail validation (default True) |
True
|
chunk_size
|
int | None
|
When provided and |
None
|
Returns:
| Type | Description |
|---|---|
IngestionStats
|
Statistics about the ingestion (created/updated/unchanged counts) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no valid datasets are found in the directory |
Source code in packages/climate-ref/src/climate_ref/datasets/__init__.py
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 | |
sub-packages#
| Sub-package | Description |
|---|---|
| base | |
| catalog_builder | Catalog builder for discovering and parsing dataset files into a DataFrame |
| cmip6 | |
| cmip6_parsers | CMIP6 parser functions for extracting metadata from netCDF files |
| cmip7 | CMIP7 Dataset Adapter |
| cmip7_parsers | CMIP7 parser functions for extracting metadata from netCDF files |
| mixins | Mixins for dataset adapters that support lazy finalization. |
| netcdf_utils | Shared utilities for reading NetCDF metadata using netCDF4 directly. |
| obs4mips | |
| pmp_climatology | |
| utils | Shared utility functions for dataset adapters |