|
6 | 6 | "source": [
|
7 | 7 | "# Introduction to `cf_xarray`\n",
|
8 | 8 | "\n",
|
9 |
| - "This notebook is a brief introduction to `cf_xarray`'s current capabilities." |
| 9 | + "This notebook is a brief introduction to `cf_xarray`'s current capabilities.\n" |
10 | 10 | ]
|
11 | 11 | },
|
12 | 12 | {
|
|
29 | 29 | "cell_type": "markdown",
|
30 | 30 | "metadata": {},
|
31 | 31 | "source": [
|
32 |
| - "Lets read two datasets." |
| 32 | + "Lets read two datasets.\n" |
33 | 33 | ]
|
34 | 34 | },
|
35 | 35 | {
|
|
52 | 52 | "cell_type": "markdown",
|
53 | 53 | "metadata": {},
|
54 | 54 | "source": [
|
55 |
| - "This one is inspired by POP model output and illustrates how the coordinates attribute is interpreted" |
| 55 | + "This one is inspired by POP model output and illustrates how the coordinates\n", |
| 56 | + "attribute is interpreted\n" |
56 | 57 | ]
|
57 | 58 | },
|
58 | 59 | {
|
|
67 | 68 | "outputs": [],
|
68 | 69 | "source": [
|
69 | 70 | "pop = xr.Dataset()\n",
|
70 |
| - "pop.coords[\"TLONG\"] = ((\"nlat\", \"nlon\"), np.ones((20, 30)), {\"units\": \"degrees_east\"})\n", |
71 |
| - "pop.coords[\"TLAT\"] = ((\"nlat\", \"nlon\"), 2 * np.ones((20, 30)), {\"units\": \"degrees_north\"})\n", |
72 |
| - "pop.coords[\"ULONG\"] = ((\"nlat\", \"nlon\"), 0.5 * np.ones((20, 30)), {\"units\": \"degrees_east\"})\n", |
73 |
| - "pop.coords[\"ULAT\"] = ((\"nlat\", \"nlon\"), 2.5 * np.ones((20, 30)), {\"units\": \"degrees_north\"})\n", |
| 71 | + "pop.coords[\"TLONG\"] = (\n", |
| 72 | + " (\"nlat\", \"nlon\"),\n", |
| 73 | + " np.ones((20, 30)),\n", |
| 74 | + " {\"units\": \"degrees_east\"},\n", |
| 75 | + ")\n", |
| 76 | + "pop.coords[\"TLAT\"] = (\n", |
| 77 | + " (\"nlat\", \"nlon\"),\n", |
| 78 | + " 2 * np.ones((20, 30)),\n", |
| 79 | + " {\"units\": \"degrees_north\"},\n", |
| 80 | + ")\n", |
| 81 | + "pop.coords[\"ULONG\"] = (\n", |
| 82 | + " (\"nlat\", \"nlon\"),\n", |
| 83 | + " 0.5 * np.ones((20, 30)),\n", |
| 84 | + " {\"units\": \"degrees_east\"},\n", |
| 85 | + ")\n", |
| 86 | + "pop.coords[\"ULAT\"] = (\n", |
| 87 | + " (\"nlat\", \"nlon\"),\n", |
| 88 | + " 2.5 * np.ones((20, 30)),\n", |
| 89 | + " {\"units\": \"degrees_north\"},\n", |
| 90 | + ")\n", |
74 | 91 | "\n",
|
75 | 92 | "pop[\"UVEL\"] = (\n",
|
76 | 93 | " (\"nlat\", \"nlon\"),\n",
|
|
80 | 97 | "pop[\"TEMP\"] = (\n",
|
81 | 98 | " (\"nlat\", \"nlon\"),\n",
|
82 | 99 | " np.ones((20, 30)) * 15,\n",
|
83 |
| - " {\"coordinates\": \"TLONG TLAT\", \"standard_name\": \"sea_water_potential_temperature\"},\n", |
| 100 | + " {\n", |
| 101 | + " \"coordinates\": \"TLONG TLAT\",\n", |
| 102 | + " \"standard_name\": \"sea_water_potential_temperature\",\n", |
| 103 | + " },\n", |
84 | 104 | ")\n",
|
85 | 105 | "pop"
|
86 | 106 | ]
|
|
89 | 109 | "cell_type": "markdown",
|
90 | 110 | "metadata": {},
|
91 | 111 | "source": [
|
92 |
| - "This synthetic dataset has multiple `X` and `Y` coords. An example would be model output on a staggered grid." |
| 112 | + "This synthetic dataset has multiple `X` and `Y` coords. An example would be\n", |
| 113 | + "model output on a staggered grid.\n" |
93 | 114 | ]
|
94 | 115 | },
|
95 | 116 | {
|
|
143 | 164 | " dict(standard_name=\"specific_humidity standard_error\", units=\"g/g\"),\n",
|
144 | 165 | ")\n",
|
145 | 166 | "anc[\"q_detection_limit\"] = xr.DataArray(\n",
|
146 |
| - " 1e-3, attrs=dict(standard_name=\"specific_humidity detection_minimum\", units=\"g/g\"),\n", |
| 167 | + " 1e-3,\n", |
| 168 | + " attrs=dict(\n", |
| 169 | + " standard_name=\"specific_humidity detection_minimum\", units=\"g/g\"\n", |
| 170 | + " ),\n", |
147 | 171 | ")\n",
|
148 | 172 | "anc"
|
149 | 173 | ]
|
|
152 | 176 | "cell_type": "markdown",
|
153 | 177 | "metadata": {},
|
154 | 178 | "source": [
|
155 |
| - "## What attributes have been discovered?" |
| 179 | + "## What attributes have been discovered?\n" |
156 | 180 | ]
|
157 | 181 | },
|
158 | 182 | {
|
|
173 | 197 | "cell_type": "markdown",
|
174 | 198 | "metadata": {},
|
175 | 199 | "source": [
|
176 |
| - "`ds.lon` has attributes `axis: X`. This means that `cf_xarray` can identify the `'X'` axis as being represented by the `lon` variable.\n", |
| 200 | + "`ds.lon` has attributes `axis: X`. This means that `cf_xarray` can identify the\n", |
| 201 | + "`'X'` axis as being represented by the `lon` variable.\n", |
177 | 202 | "\n",
|
178 |
| - "It can also use the `standard_name` and `units` attributes to infer that `lon` is \"Longitude\". To see variable names that `cf_xarray` can infer, use `.cf.describe()`" |
| 203 | + "It can also use the `standard_name` and `units` attributes to infer that `lon`\n", |
| 204 | + "is \"Longitude\". To see variable names that `cf_xarray` can infer, use\n", |
| 205 | + "`.cf.describe()`\n" |
179 | 206 | ]
|
180 | 207 | },
|
181 | 208 | {
|
|
196 | 223 | "cell_type": "markdown",
|
197 | 224 | "metadata": {},
|
198 | 225 | "source": [
|
199 |
| - "For `pop`, only `latitude` and `longitude` are detected, not `X` or `Y`. Please comment here: https://github.com/xarray-contrib/cf-xarray/issues/23 if you have opinions about this behaviour." |
| 226 | + "For `pop`, only `latitude` and `longitude` are detected, not `X` or `Y`. Please\n", |
| 227 | + "comment here: https://github.com/xarray-contrib/cf-xarray/issues/23 if you have\n", |
| 228 | + "opinions about this behaviour.\n" |
200 | 229 | ]
|
201 | 230 | },
|
202 | 231 | {
|
|
217 | 246 | "cell_type": "markdown",
|
218 | 247 | "metadata": {},
|
219 | 248 | "source": [
|
220 |
| - "For `multiple`, multiple `X` and `Y` coordinates are detected" |
| 249 | + "For `multiple`, multiple `X` and `Y` coordinates are detected\n" |
221 | 250 | ]
|
222 | 251 | },
|
223 | 252 | {
|
|
240 | 269 | "source": [
|
241 | 270 | "## Feature: Accessing coordinate variables\n",
|
242 | 271 | "\n",
|
243 |
| - "`.cf` implements `__getitem__` to allow easy access to coordinate and axis variables." |
| 272 | + "`.cf` implements `__getitem__` to allow easy access to coordinate and axis\n", |
| 273 | + "variables.\n" |
244 | 274 | ]
|
245 | 275 | },
|
246 | 276 | {
|
|
291 | 321 | "cell_type": "markdown",
|
292 | 322 | "metadata": {},
|
293 | 323 | "source": [
|
294 |
| - "`Dataset.cf[...]` returns a `Dataset`, possibly with multiple variables" |
| 324 | + "`Dataset.cf[...]` returns a `Dataset`, possibly with multiple variables\n" |
295 | 325 | ]
|
296 | 326 | },
|
297 | 327 | {
|
|
313 | 343 | "cell_type": "markdown",
|
314 | 344 | "metadata": {},
|
315 | 345 | "source": [
|
316 |
| - "`Dataset.cf[...]` returns a single `DataArray`, parsing the `coordinates` attribute if present, so we correctly get the `TLONG` variable and not the `ULONG` variable" |
| 346 | + "`Dataset.cf[...]` returns a single `DataArray`, parsing the `coordinates`\n", |
| 347 | + "attribute if present, so we correctly get the `TLONG` variable and not the\n", |
| 348 | + "`ULONG` variable\n" |
317 | 349 | ]
|
318 | 350 | },
|
319 | 351 | {
|
|
335 | 367 | "cell_type": "markdown",
|
336 | 368 | "metadata": {},
|
337 | 369 | "source": [
|
338 |
| - "`Dataset.cf[...]` also interprets the `ancillary_variables` attribute. The ancillary variables are returned as coordinates of a DataArray" |
| 370 | + "`Dataset.cf[...]` also interprets the `ancillary_variables` attribute. The\n", |
| 371 | + "ancillary variables are returned as coordinates of a DataArray\n" |
339 | 372 | ]
|
340 | 373 | },
|
341 | 374 | {
|
|
356 | 389 | "cell_type": "markdown",
|
357 | 390 | "metadata": {},
|
358 | 391 | "source": [
|
359 |
| - "## Feature: Accessing variables by standard names" |
| 392 | + "## Feature: Accessing variables by standard names\n" |
360 | 393 | ]
|
361 | 394 | },
|
362 | 395 | {
|
|
393 | 426 | "source": [
|
394 | 427 | "## Feature: Utility functions\n",
|
395 | 428 | "\n",
|
396 |
| - "\n", |
397 |
| - "There are some utility functions to allow use by downstream libraries" |
| 429 | + "There are some utility functions to allow use by downstream libraries\n" |
398 | 430 | ]
|
399 | 431 | },
|
400 | 432 | {
|
|
417 | 449 | "source": [
|
418 | 450 | "## Feature: Rewriting property dictionaries\n",
|
419 | 451 | "\n",
|
420 |
| - "`cf_xarray` will rewrite the `.sizes` and `.chunks` dictionaries so that one can index by a special CF axis or coordinate name" |
| 452 | + "`cf_xarray` will rewrite the `.sizes` and `.chunks` dictionaries so that one can\n", |
| 453 | + "index by a special CF axis or coordinate name\n" |
421 | 454 | ]
|
422 | 455 | },
|
423 | 456 | {
|
|
438 | 471 | "1. One for `X`, `Y`, `T`\n",
|
439 | 472 | "2. and one for `longitude`, `latitude` and `time`.\n",
|
440 | 473 | "\n",
|
441 |
| - "An error is raised if there are multiple `'X'` variables (for example)" |
| 474 | + "An error is raised if there are multiple `'X'` variables (for example)\n" |
442 | 475 | ]
|
443 | 476 | },
|
444 | 477 | {
|
|
469 | 502 | "source": [
|
470 | 503 | "## Feature: Rewriting arguments\n",
|
471 | 504 | "\n",
|
472 |
| - "`cf_xarray` can rewrite arguments for a large number of xarray functions. By this I mean that instead of specifing say `dim=\"lon\"`, you can pass `dim=\"X\"` or `dim=\"longitude\"` and `cf_xarray` will rewrite that to `dim=\"lon\"` based on the attributes present in the dataset. \n", |
| 505 | + "`cf_xarray` can rewrite arguments for a large number of xarray functions. By\n", |
| 506 | + "this I mean that instead of specifing say `dim=\"lon\"`, you can pass `dim=\"X\"` or\n", |
| 507 | + "`dim=\"longitude\"` and `cf_xarray` will rewrite that to `dim=\"lon\"` based on the\n", |
| 508 | + "attributes present in the dataset.\n", |
473 | 509 | "\n",
|
474 |
| - "Here are a few examples" |
| 510 | + "Here are a few examples\n" |
475 | 511 | ]
|
476 | 512 | },
|
477 | 513 | {
|
478 | 514 | "cell_type": "markdown",
|
479 | 515 | "metadata": {},
|
480 | 516 | "source": [
|
481 |
| - "### Slicing" |
| 517 | + "### Slicing\n" |
482 | 518 | ]
|
483 | 519 | },
|
484 | 520 | {
|
|
499 | 535 | "cell_type": "markdown",
|
500 | 536 | "metadata": {},
|
501 | 537 | "source": [
|
502 |
| - "Slicing works will expand a single key like `X` to multiple dimensions if those dimensions are tagged with `axis: X`" |
| 538 | + "Slicing works will expand a single key like `X` to multiple dimensions if those\n", |
| 539 | + "dimensions are tagged with `axis: X`\n" |
503 | 540 | ]
|
504 | 541 | },
|
505 | 542 | {
|
|
520 | 557 | "cell_type": "markdown",
|
521 | 558 | "metadata": {},
|
522 | 559 | "source": [
|
523 |
| - "### Reductions" |
| 560 | + "### Reductions\n" |
524 | 561 | ]
|
525 | 562 | },
|
526 | 563 | {
|
|
541 | 578 | "cell_type": "markdown",
|
542 | 579 | "metadata": {},
|
543 | 580 | "source": [
|
544 |
| - "Expanding to multiple dimensions is also supported" |
| 581 | + "Expanding to multiple dimensions is also supported\n" |
545 | 582 | ]
|
546 | 583 | },
|
547 | 584 | {
|
|
563 | 600 | "cell_type": "markdown",
|
564 | 601 | "metadata": {},
|
565 | 602 | "source": [
|
566 |
| - "### Plotting" |
| 603 | + "### Plotting\n" |
567 | 604 | ]
|
568 | 605 | },
|
569 | 606 | {
|
|
598 | 635 | "cell_type": "markdown",
|
599 | 636 | "metadata": {},
|
600 | 637 | "source": [
|
601 |
| - "`cf_xarray` can facet" |
| 638 | + "`cf_xarray` can facet\n" |
602 | 639 | ]
|
603 | 640 | },
|
604 | 641 | {
|
|
619 | 656 | "cell_type": "markdown",
|
620 | 657 | "metadata": {},
|
621 | 658 | "source": [
|
622 |
| - "### Resample & groupby" |
| 659 | + "### Resample & groupby\n" |
623 | 660 | ]
|
624 | 661 | },
|
625 | 662 | {
|
|
654 | 691 | "cell_type": "markdown",
|
655 | 692 | "metadata": {},
|
656 | 693 | "source": [
|
657 |
| - "### Rolling & coarsen " |
| 694 | + "### Rolling & coarsen\n" |
658 | 695 | ]
|
659 | 696 | },
|
660 | 697 | {
|
|
680 | 717 | }
|
681 | 718 | },
|
682 | 719 | "source": [
|
683 |
| - "``coarsen`` works but everything later will break because of xarray bug https://github.com/pydata/xarray/issues/4120\n", |
| 720 | + "`coarsen` works but everything later will break because of xarray bug\n", |
| 721 | + "https://github.com/pydata/xarray/issues/4120\n", |
684 | 722 | "\n",
|
685 |
| - "``ds.isel(lon=slice(50)).cf.coarsen(Y=5, X=10).mean()``" |
| 723 | + "`ds.isel(lon=slice(50)).cf.coarsen(Y=5, X=10).mean()`\n" |
686 | 724 | ]
|
687 | 725 | },
|
688 | 726 | {
|
689 | 727 | "cell_type": "markdown",
|
690 | 728 | "metadata": {},
|
691 | 729 | "source": [
|
692 |
| - "## Feature: mix \"special names\" and variable names" |
| 730 | + "## Feature: mix \"special names\" and variable names\n" |
693 | 731 | ]
|
694 | 732 | },
|
695 | 733 | {
|
|
712 | 750 | "source": [
|
713 | 751 | "## Feature: Weight by Cell Measures\n",
|
714 | 752 | "\n",
|
715 |
| - "`cf_xarray` can weight by cell measure variables `\"area\"` and `\"volume\"` if the appropriate attribute is set" |
| 753 | + "`cf_xarray` can weight by cell measure variables `\"area\"` and `\"volume\"` if the\n", |
| 754 | + "appropriate attribute is set\n" |
716 | 755 | ]
|
717 | 756 | },
|
718 | 757 | {
|
|
0 commit comments