Skip to content

Commit e2f1f31

Browse files
authored
Merge pull request #87 from r-ford/main
Fix error in ECS notebook
2 parents 39d2b33 + dfcde9c commit e2f1f31

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

notebooks/example-workflows/ecs-cmip6.ipynb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
" which can lead to issues when merging.\"\"\"\n",
312312
" drop_vars = [vname for vname in ds.coords\n",
313313
" if (('_bounds') in vname ) or ('_bnds') in vname]\n",
314-
" return ds.drop(drop_vars)\n",
314+
" return ds.drop_vars(drop_vars)\n",
315315
"\n",
316316
"def open_dsets(df):\n",
317317
" \"\"\"Open datasets from cloud storage and return xarray dataset.\"\"\"\n",
@@ -471,7 +471,7 @@
471471
" # https://github.com/pydata/xarray/issues/2237#issuecomment-620961663\n",
472472
" dsets_ann_mean = [v[expt].pipe(global_mean)\n",
473473
" .swap_dims({'time': 'year'})\n",
474-
" .drop('time')\n",
474+
" .drop_vars('time')\n",
475475
" .coarsen(year=12).mean()\n",
476476
" for expt in expts]\n",
477477
"\n",
@@ -645,14 +645,12 @@
645645
"source": [
646646
"ds_abrupt = ds_anom.sel(year=first_150_years, experiment_id=co2_option).reset_coords(drop=True)\n",
647647
"\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",
652650
" ecs = -1.0 * (b/a) # Change -1.0 to -0.5 if using 4xCO2\n",
653651
" return xr.DataArray(ecs)\n",
654652
"\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",
656654
"ds_abrupt.compute()"
657655
]
658656
},
@@ -776,7 +774,7 @@
776774
"name": "python",
777775
"nbconvert_exporter": "python",
778776
"pygments_lexer": "ipython3",
779-
"version": "3.10.9"
777+
"version": "3.10.15"
780778
},
781779
"nbdime-conflicts": {
782780
"local_diff": [

0 commit comments

Comments
 (0)