|
311 | 311 | " which can lead to issues when merging.\"\"\"\n",
|
312 | 312 | " drop_vars = [vname for vname in ds.coords\n",
|
313 | 313 | " if (('_bounds') in vname ) or ('_bnds') in vname]\n",
|
314 |
| - " return ds.drop(drop_vars)\n", |
| 314 | + " return ds.drop_vars(drop_vars)\n", |
315 | 315 | "\n",
|
316 | 316 | "def open_dsets(df):\n",
|
317 | 317 | " \"\"\"Open datasets from cloud storage and return xarray dataset.\"\"\"\n",
|
|
471 | 471 | " # https://github.com/pydata/xarray/issues/2237#issuecomment-620961663\n",
|
472 | 472 | " dsets_ann_mean = [v[expt].pipe(global_mean)\n",
|
473 | 473 | " .swap_dims({'time': 'year'})\n",
|
474 |
| - " .drop('time')\n", |
| 474 | + " .drop_vars('time')\n", |
475 | 475 | " .coarsen(year=12).mean()\n",
|
476 | 476 | " for expt in expts]\n",
|
477 | 477 | "\n",
|
|
645 | 645 | "source": [
|
646 | 646 | "ds_abrupt = ds_anom.sel(year=first_150_years, experiment_id=co2_option).reset_coords(drop=True)\n",
|
647 | 647 | "\n",
|
648 |
| - "def calc_ecs(ds):\n", |
649 |
| - " # Some sources don't have all 150 years, drop those missing values.\n", |
650 |
| - " a, b = np.polyfit(ds.tas.dropna(\"year\"),\n", |
651 |
| - " ds.imbalance.dropna(\"year\"), 1)\n", |
| 648 | + "def calc_ecs(tas, imb):\n", |
| 649 | + " a, b = np.polyfit(tas, imb, 1)\n", |
652 | 650 | " ecs = -1.0 * (b/a) # Change -1.0 to -0.5 if using 4xCO2\n",
|
653 | 651 | " return xr.DataArray(ecs)\n",
|
654 | 652 | "\n",
|
655 |
| - "ds_abrupt['ecs'] = ds_abrupt.groupby('source_id').apply(calc_ecs)\n", |
| 653 | + "ds_abrupt['ecs'] = xr.apply_ufunc(calc_ecs, ds_abrupt.tas, ds_abrupt.imbalance, vectorize=True, input_core_dims=[['year'], ['year']])\n", |
656 | 654 | "ds_abrupt.compute()"
|
657 | 655 | ]
|
658 | 656 | },
|
|
776 | 774 | "name": "python",
|
777 | 775 | "nbconvert_exporter": "python",
|
778 | 776 | "pygments_lexer": "ipython3",
|
779 |
| - "version": "3.10.9" |
| 777 | + "version": "3.10.15" |
780 | 778 | },
|
781 | 779 | "nbdime-conflicts": {
|
782 | 780 | "local_diff": [
|
|
0 commit comments