Skip to content

Commit

Permalink
add detrend
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Feb 7, 2024
1 parent d787613 commit b5f5896
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions notebooks/wp3/WIP-hit_rate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
"year_start_hindcast = 1993\n",
"year_stop_hindcast = 2016\n",
"\n",
"# Detrend\n",
"detrend = True\n",
"assert isinstance(detrend, bool)\n",
"\n",
"# Variable\n",
"variables = [\n",
" \"2m_temperature\",\n",
Expand Down Expand Up @@ -213,7 +217,7 @@
" return ds\n",
"\n",
"\n",
"def compute_tercile_occupation(ds, region):\n",
"def compute_tercile_occupation(ds, region, detrend):\n",
" # Mask region\n",
" mask = regionmask.defined_regions.srex.mask(ds)\n",
" index = regionmask.defined_regions.srex.map_keys(region)\n",
Expand All @@ -224,6 +228,13 @@
" else:\n",
" ds = ds.rename(forecast_reference_time=\"valid_time\")\n",
"\n",
" if detrend:\n",
" (da,) = ds.data_vars.values()\n",
" time = da[\"valid_time\"]\n",
" fit = ds.polyfit(time.name, deg=1, w=time.dt.days_in_month)\n",
" da -= xr.polyval(time, fit.polyfit_coefficients)\n",
" ds = da.to_dataset()\n",
"\n",
" # Compute anomaly\n",
" climatology = diagnostics.time_weighted_mean(ds, time_name=\"valid_time\")\n",
" climatology = climatology.mean(set(climatology.dims) & {\"realization\"})\n",
Expand Down Expand Up @@ -284,7 +295,7 @@
" chunks=chunks,\n",
" transform_chunks=False,\n",
" transform_func=compute_tercile_occupation,\n",
" transform_func_kwargs={\"region\": region},\n",
" transform_func_kwargs={\"region\": region, \"detrend\": detrend},\n",
" )\n",
" (da,) = ds.data_vars.values()\n",
" dataarrays.append(da.rename(variable))\n",
Expand Down Expand Up @@ -329,7 +340,7 @@
" chunks=chunks,\n",
" transform_chunks=False,\n",
" transform_func=compute_tercile_occupation,\n",
" transform_func_kwargs={\"region\": region},\n",
" transform_func_kwargs={\"region\": region, \"detrend\": detrend},\n",
" backend_kwargs={\n",
" \"time_dims\": (\n",
" \"forecastMonth\",\n",
Expand Down

0 comments on commit b5f5896

Please sign in to comment.