FileIO
VAE.utils.fileio
Collection of file readers.
VAE.utils.fileio.read_netcdf
read_netcdf(filename, level_range=None, time_interval=None, time_range=None, num2date=False, datetime_unit='D', scale=1.0, dtype=None)
Read netCDF file.
The function follows the CF-1 convention and assumes data of the form
2D: (time, level)
3D: (time, lat, lon)
4D: (time, level, lat, lon)
Parameters:
-
filename
(str
) –Name of the netCDF file.
-
level_range
(tuple[int, int]
, default:None
) –Start and stop indices of levels to be read (in slice notation). Defaults to
None
. -
time_interval
(tuple[str, str]
, default:None
) –Start and stop time to be read (in datetime notation). Default is 'None'.
-
time_range
(tuple[int, int]
, default:None
) –Start and stop indices along time dimensions to be read (in slice notation). Default is 'None'.
-
num2date
(bool
, default:False
) –Convert time to Numpy datetime. Defaults to 'True' if 'time_interval' is given otherwise default is 'False'.
-
datetime_unit
(str
, default:'D'
) –Unit of datetime if
num2date
is set toTrue
. Defaults toD
, meaning day. For a list of units see https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-units. -
scale
(float
, default:1.0
) –Scale all variables by this factor. Defaults to 1.
-
dtype
(str
, default:None
) –Data type of the variables that will be returned. Default is None, meaning dtype of netCDF file.
Returns:
-
tuple[dict, dict, dict]
–tuple of three dicts containing the variables, dimensions, and attributes.
Source code in VAE/utils/fileio.py
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 |
|
VAE.utils.fileio.write_netcdf
write_netcdf(filename, variables, dimensions, attributes, scale=None, dtype='f4', compression='zlib', verbose=True)
Write netCDF file.
The function follows the CF-1 convention and assumes data of the form
2D: (time, level)
3D: (time, lat, lon)
4D: (time, level, lat, lon)
The structure of the dictionaries variables
, dimensions
, and attributes
follows that of :func:read_netcdf
.
Parameters:
-
filename
(str
) –Name of the netCDF file.
-
variables
(dict[str, ndarray]
) –Dictionary of variables with the items defining the variable name and values.
-
dimensions
(dict[str, ndarray]
) –Dictionary of dimensions with the items defining the dimension name and values. Dimension names must be either
time
,level
,lat
, orlon
. -
attributes
(dict[str, dict]
) –Dictionary of attributes for the variables and the dimensions with the items defining the variable/dimension name and a dict of attributes. Global attributes will be taken from
attributes['.']
. -
scale
(float
, default:None
) –Scale all variables. If scale is
None
, scale is obtained fromattributes[variable_name]['scale']
. -
dtype
(str
, default:'f4'
) –Data type of the variables that will be written. Default is 'float'.
-
compression
(str
, default:'zlib'
) –Data will be compressed in the netCDF file using the specified compression algorithm.
Source code in VAE/utils/fileio.py
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 |
|
VAE.utils.fileio.read_netcdf_multi
read_netcdf_multi(filename, level_range=None, time_interval=None, time_range=None, scale=1.0, recursive=False, num2date=False, datetime_unit='D', dtype=None, verbose=True)
Read multiple files of netCDF data.
Parameters:
-
filename
(list[str]
) –Name of file(s). Glob patterns can be used.
-
level_range
(tuple[int, int]
, default:None
) –Start and stop indices of levels to be read (in slice notation). If a list of tuples, length of list must match the length of
filename
. Defaults toNone
. -
time_interval
(tuple[str, str]
, default:None
) –Time intervaL to be read. If a list of tuples, length of list must match the length of
filename
. Defaults toNone
. -
time_range
(tuple[int, int]
, default:None
) –Start and stop indices along time dimensions to be read (in slice notation). If a list of tuples, length of list must match the length of
filename
. Defaults toNone
. -
recursive
(bool
, default:False
) –If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories.
-
num2date
(bool
, default:False
) –Convert time to Numpy datetime. Defaults to 'True' if 'time_interval' is given otherwise default is 'False'.
-
datetime_unit
(str
, default:'D'
) –Unit of datetime if
num2date
is set toTrue
. Defaults toD
, meaning day. For a list of units see https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-units. -
dtype
(str
, default:None
) –Data type of the variables that will be returned. Default is None, meaning dtype of netCDF file.
Returns:
-
tuple[dict, dict, dict]
–tuple of three dicts containing the variables, dimensions, and attributes of each file.
Source code in VAE/utils/fileio.py
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 |
|
VAE.utils.fileio.read_climexp_raw_data
read_climexp_raw_data(filename, ensemble_members=None, time_interval=None, dtype='float')
Read single file of raw data from climexp.
Read a single file of raw data downloaded from https://climexp.knmi.nl.
Parameters:
-
filename
(str
) –File name.
-
ensemble_members
(list[int]
, default:None
) –Ensemble members that will be returned. Defaults to
None
, meaning all members are returned. -
time_interval
(tuple[str, str]
, default:None
) –Time interval to be read.
-
dtype
(str
, default:'float'
) –Data type of the returned DataFrame. Default is 'float'.
Returns:
-
tuple[DataFrame, dict]
–tuple of DataFrame and dict, where the dataframe contains the data and the dict contains the metadata.
Source code in VAE/utils/fileio.py
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 |
|
VAE.utils.fileio.read_climexp_raw_data_multi
read_climexp_raw_data_multi(filename, ensemble_members=None, time_interval=None, recursive=False, join='inner', dtype='float')
Read multiple files of raw data from climexp.
Read multiples files of raw data downloaded from https://climexp.knmi.nl.
Parameters:
-
filename
(list[str]
) –Name of file(s). Glob patterns can be used.
-
ensemble_members
(list[int]
, default:None
) –Ensemble members that will be returned. Defaults to
None
, meaning all members are returned. -
time_interval
(tuple[str, str]
, default:None
) –Time interval to be read. If a list, it must match the length of
filename
. -
recursive
(bool
, default:False
) –If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories.
-
join
(str
, default:'inner'
) –How to join the different files. 'inner' means that only the common rows will be kept. 'outer' means that all rows will be kept and missing values will be filled with NaN. Default is 'inner'.
-
dtype
(str
, default:'float'
) –Data type of the returned DataFrame. Default is 'float'.
Returns:
-
DataFrame
–tuple of DataFrame and dict of dict. The dataframe contains the data and the dict contains the metadata. In the
-
dict[str, dict]
–DataFrame, the filename is used as level-zero index in a multi-index. In the dict, the filename is used as key.
Source code in VAE/utils/fileio.py
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 |
|
VAE.utils.fileio.example_read_climexp_raw_data_multi
example_read_climexp_raw_data_multi()
Example of how to use the function read_climexp_raw_data_multi
.
The function reads multiple files of raw data from the example_data/
folder.
example_data/icmip5_tos_Omon_one_rcp45_pc01.txt
example_data/icmip5_tos_Omon_one_rcp45_pc02.txt
Source code in VAE/utils/fileio.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
VAE.utils.fileio.example1_read_netcdf
example1_read_netcdf()
Example of how to use the function read_netcdf
.
This function reads EOFs in a netCDF file from the Climate explorer from the example_data/
folder:
example_data/eofs_icmip5_tos_Omon_one_rcp45.nc
Source code in VAE/utils/fileio.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
|
VAE.utils.fileio.example2_read_netcdf
example2_read_netcdf()
Example of how to use the function read_netcdf
.
This function reads EOFs in a netCDF file from the output of the climate data operators (CDO). The file is from the
example_data/
folder:
example_data/eofs_anom_gpcc_v2020_1dgr.nc
:material-github: For the calculation of the EOFs and PCs with CDO see the CDO scripts.
Source code in VAE/utils/fileio.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
VAE.utils.fileio.example3_read_netcdf
example3_read_netcdf()
Example of how to use the function read_netcdf
.
This function reads PCs in a netCDF file from the output of the climate data operators (CDO). The file is from the
example_data/
folder:
example_data/pcs_anom_gpcc_v2020_1dgr.nc
:material-github: For the calculation of the EOFs and PCs with CDO see the CDO scripts.
Source code in VAE/utils/fileio.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
|
VAE.utils.fileio.example_read_netcdf_multi
example_read_netcdf_multi(filename)
Example of how to use the function read_netcdf_multi
.
This function reads PCs in multiple netCDF files from the output of the climate data operators (CDO). The files are
from the example_data/
folder:
example_data/pcs_anom_pr_*.nc
:material-github: For the calculation of the EOFs and PCs with CDO see the CDO scripts.
Source code in VAE/utils/fileio.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
|