|
43 | 43 | ], |
44 | 44 | "source": [ |
45 | 45 | "from dask_gateway import Gateway\n", |
| 46 | + "\n", |
46 | 47 | "g = Gateway()\n", |
47 | 48 | "running_clusters = g.list_clusters()\n", |
48 | 49 | "print(running_clusters)\n", |
|
77 | 78 | "source": [ |
78 | 79 | "from distributed import Client\n", |
79 | 80 | "from dask_gateway import GatewayCluster\n", |
| 81 | + "\n", |
80 | 82 | "cluster = GatewayCluster()\n", |
81 | 83 | "cluster.scale(30)\n", |
82 | 84 | "cluster" |
|
255 | 257 | } |
256 | 258 | ], |
257 | 259 | "source": [ |
258 | | - "zkwargs = {'consolidated':True, 'use_cftime':True}\n", |
259 | | - "kwargs = {'zarr_kwargs':zkwargs, 'preprocess':combined_preprocessing, 'aggregate':False}\n", |
| 260 | + "zkwargs = {\"consolidated\": True, \"use_cftime\": True}\n", |
| 261 | + "kwargs = {\n", |
| 262 | + " \"zarr_kwargs\": zkwargs,\n", |
| 263 | + " \"preprocess\": combined_preprocessing,\n", |
| 264 | + " \"aggregate\": False,\n", |
| 265 | + "}\n", |
260 | 266 | "\n", |
261 | 267 | "col = google_cmip_col()\n", |
262 | 268 | "\n", |
263 | 269 | "\n", |
264 | | - "cat = col.search(source_id='CanESM5-CanOE', variable_id='thetao')\n", |
| 270 | + "cat = col.search(source_id=\"CanESM5-CanOE\", variable_id=\"thetao\")\n", |
265 | 271 | "\n", |
266 | 272 | "\n", |
267 | | - "ddict_historical = cat.search(experiment_id='historical').to_dataset_dict(**kwargs)\n", |
268 | | - "ddict_ssp585 = cat.search(experiment_id='ssp585').to_dataset_dict(**kwargs)\n", |
269 | | - "ddict_picontrol = cat.search(experiment_id='piControl').to_dataset_dict(**kwargs)" |
| 273 | + "ddict_historical = cat.search(experiment_id=\"historical\").to_dataset_dict(**kwargs)\n", |
| 274 | + "ddict_ssp585 = cat.search(experiment_id=\"ssp585\").to_dataset_dict(**kwargs)\n", |
| 275 | + "ddict_picontrol = cat.search(experiment_id=\"piControl\").to_dataset_dict(**kwargs)" |
270 | 276 | ] |
271 | 277 | }, |
272 | 278 | { |
|
286 | 292 | "metadata": {}, |
287 | 293 | "outputs": [], |
288 | 294 | "source": [ |
289 | | - "ds_control = ddict_picontrol['CMIP.CCCma.CanESM5-CanOE.piControl.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/CMIP/CCCma/CanESM5-CanOE/piControl/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429']\n", |
290 | | - "ds_historical = ddict_historical['CMIP.CCCma.CanESM5-CanOE.historical.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/CMIP/CCCma/CanESM5-CanOE/historical/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429']\n", |
291 | | - "ds_ssp585 = ddict_ssp585['ScenarioMIP.CCCma.CanESM5-CanOE.ssp585.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/ScenarioMIP/CCCma/CanESM5-CanOE/ssp585/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429']\n", |
| 295 | + "ds_control = ddict_picontrol[\n", |
| 296 | + " \"CMIP.CCCma.CanESM5-CanOE.piControl.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/CMIP/CCCma/CanESM5-CanOE/piControl/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429\"\n", |
| 297 | + "]\n", |
| 298 | + "ds_historical = ddict_historical[\n", |
| 299 | + " \"CMIP.CCCma.CanESM5-CanOE.historical.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/CMIP/CCCma/CanESM5-CanOE/historical/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429\"\n", |
| 300 | + "]\n", |
| 301 | + "ds_ssp585 = ddict_ssp585[\n", |
| 302 | + " \"ScenarioMIP.CCCma.CanESM5-CanOE.ssp585.r1i1p2f1.Omon.thetao.gn.gs://cmip6/CMIP6/ScenarioMIP/CCCma/CanESM5-CanOE/ssp585/r1i1p2f1/Omon/thetao/gn/v20190429/.nan.20190429\"\n", |
| 303 | + "]\n", |
292 | 304 | "\n", |
293 | 305 | "# Pick a random location in x/y/z space to use as an exmple\n", |
294 | | - "roi = {'x':100,'y':220, 'lev':30}" |
| 306 | + "roi = {\"x\": 100, \"y\": 220, \"lev\": 30}" |
295 | 307 | ] |
296 | 308 | }, |
297 | 309 | { |
|
335 | 347 | ], |
336 | 348 | "source": [ |
337 | 349 | "# ok lets just plot them together\n", |
338 | | - "ds_control.isel(**roi).thetao.plot(color='0.5')\n", |
339 | | - "ds_historical.isel(**roi).thetao.plot(color='C1')" |
| 350 | + "ds_control.isel(**roi).thetao.plot(color=\"0.5\")\n", |
| 351 | + "ds_historical.isel(**roi).thetao.plot(color=\"C1\")" |
340 | 352 | ] |
341 | 353 | }, |
342 | 354 | { |
|
373 | 385 | } |
374 | 386 | ], |
375 | 387 | "source": [ |
376 | | - "{k:v for k,v in ds_historical.attrs.items() if 'parent' in k}" |
| 388 | + "{k: v for k, v in ds_historical.attrs.items() if \"parent\" in k}" |
377 | 389 | ] |
378 | 390 | }, |
379 | 391 | { |
|
428 | 440 | ], |
429 | 441 | "source": [ |
430 | 442 | "# ok lets just plot them together\n", |
431 | | - "ds_control_adj.isel(**roi).thetao.plot(color='0.5')\n", |
432 | | - "ds_historical_adj.isel(**roi).thetao.plot(color='C1')" |
| 443 | + "ds_control_adj.isel(**roi).thetao.plot(color=\"0.5\")\n", |
| 444 | + "ds_historical_adj.isel(**roi).thetao.plot(color=\"C1\")" |
433 | 445 | ] |
434 | 446 | }, |
435 | 447 | { |
|
471 | 483 | ], |
472 | 484 | "source": [ |
473 | 485 | "# ok lets just plot them together\n", |
474 | | - "ds_control_adj.isel(**roi, time=slice(0,24)).thetao.plot()\n", |
475 | | - "ds_historical_adj.isel(**roi, time=slice(0,24)).thetao.plot()" |
| 486 | + "ds_control_adj.isel(**roi, time=slice(0, 24)).thetao.plot()\n", |
| 487 | + "ds_historical_adj.isel(**roi, time=slice(0, 24)).thetao.plot()" |
476 | 488 | ] |
477 | 489 | }, |
478 | 490 | { |
|
491 | 503 | "outputs": [], |
492 | 504 | "source": [ |
493 | 505 | "from xmip.drift_removal import replace_time\n", |
| 506 | + "\n", |
494 | 507 | "# with the defaults it will just replace the dates with new ones which have time stamps at the beginning of the month.\n", |
495 | | - "ds_historical_adj = replace_time(ds_historical_adj) " |
| 508 | + "ds_historical_adj = replace_time(ds_historical_adj)" |
496 | 509 | ] |
497 | 510 | }, |
498 | 511 | { |
|
526 | 539 | ], |
527 | 540 | "source": [ |
528 | 541 | "# ok lets just plot them together again\n", |
529 | | - "ds_control_adj.isel(**roi, time=slice(0,24)).thetao.plot()\n", |
530 | | - "ds_historical_adj.isel(**roi, time=slice(0,24)).thetao.plot()" |
| 542 | + "ds_control_adj.isel(**roi, time=slice(0, 24)).thetao.plot()\n", |
| 543 | + "ds_historical_adj.isel(**roi, time=slice(0, 24)).thetao.plot()" |
531 | 544 | ] |
532 | 545 | }, |
533 | 546 | { |
|
564 | 577 | ], |
565 | 578 | "source": [ |
566 | 579 | "for name, ds in ddict_historical.items():\n", |
567 | | - " print(name, ds.attrs['branch_time_in_parent'])" |
| 580 | + " print(name, ds.attrs[\"branch_time_in_parent\"])" |
568 | 581 | ] |
569 | 582 | }, |
570 | 583 | { |
|
598 | 611 | "# replace the timestamp with the first of the month for the control run and plot\n", |
599 | 612 | "# we will also average the data yearly to remove some of the visual noise\n", |
600 | 613 | "\n", |
601 | | - "plt.figure(figsize=[12,4])\n", |
602 | | - "replace_time(ds_control).isel(**roi).thetao.coarsen(time=3).mean().isel(time=slice(0,150*4)).plot(color='0.5')\n", |
| 614 | + "plt.figure(figsize=[12, 4])\n", |
| 615 | + "replace_time(ds_control).isel(**roi).thetao.coarsen(time=3).mean().isel(\n", |
| 616 | + " time=slice(0, 150 * 4)\n", |
| 617 | + ").plot(color=\"0.5\")\n", |
603 | 618 | "\n", |
604 | | - "# now we loop through all the historical members, adjust the time and plot them in the same way, \n", |
| 619 | + "# now we loop through all the historical members, adjust the time and plot them in the same way,\n", |
605 | 620 | "# but only for the first 20 years\n", |
606 | 621 | "for name, ds in ddict_historical.items():\n", |
607 | | - " _, ds_adj = unify_time(ds_control, ds, adjust_to='parent')\n", |
608 | | - " ds_adj.isel(**roi).thetao.coarsen(time=3).mean().isel(time=slice(0,30*4)).plot(color='C1')" |
| 622 | + " _, ds_adj = unify_time(ds_control, ds, adjust_to=\"parent\")\n", |
| 623 | + " ds_adj.isel(**roi).thetao.coarsen(time=3).mean().isel(time=slice(0, 30 * 4)).plot(\n", |
| 624 | + " color=\"C1\"\n", |
| 625 | + " )" |
609 | 626 | ] |
610 | 627 | }, |
611 | 628 | { |
|
654 | 671 | "# setting up the scratch bucket\n", |
655 | 672 | "import os\n", |
656 | 673 | "import fsspec\n", |
657 | | - "PANGEO_SCRATCH = os.environ['PANGEO_SCRATCH']+'cmip6_pp_demo'\n", |
658 | | - "path = f'{PANGEO_SCRATCH}/test_rechunked.zarr'\n", |
659 | | - "temp_path = f'{PANGEO_SCRATCH}/test_rechunked_temp.zarr'\n", |
| 674 | + "\n", |
| 675 | + "PANGEO_SCRATCH = os.environ[\"PANGEO_SCRATCH\"] + \"cmip6_pp_demo\"\n", |
| 676 | + "path = f\"{PANGEO_SCRATCH}/test_rechunked.zarr\"\n", |
| 677 | + "temp_path = f\"{PANGEO_SCRATCH}/test_rechunked_temp.zarr\"\n", |
660 | 678 | "mapper = fsspec.get_mapper(path)\n", |
661 | 679 | "mapper_temp = fsspec.get_mapper(temp_path)" |
662 | 680 | ] |
|
1506 | 1524 | "source": [ |
1507 | 1525 | "if not mapper.fs.exists(path):\n", |
1508 | 1526 | " # recompute the rechunked data into the scratch bucket (is only triggered when the temporary store was erased)\n", |
1509 | | - " \n", |
| 1527 | + "\n", |
1510 | 1528 | " # Remove the temp store if for some reason that still exists\n", |
1511 | 1529 | " if mapper.fs.exists(temp_path):\n", |
1512 | 1530 | " mapper.fs.rm(temp_path, recursive=True)\n", |
1513 | 1531 | " from rechunker import rechunk\n", |
| 1532 | + "\n", |
1514 | 1533 | " target_chunks = {\n", |
1515 | | - " 'thetao': {'time':6012, 'lev':1, 'x':3, 'y':291},\n", |
1516 | | - " 'x': {'x':3},\n", |
1517 | | - " 'y': {'y':291},\n", |
1518 | | - " 'lat': {'x':3, 'y':291},\n", |
1519 | | - " 'lev': {'lev':1},\n", |
1520 | | - " 'lon': {'x':3, 'y':291},\n", |
1521 | | - " 'time': {'time':6012}, \n", |
| 1534 | + " \"thetao\": {\"time\": 6012, \"lev\": 1, \"x\": 3, \"y\": 291},\n", |
| 1535 | + " \"x\": {\"x\": 3},\n", |
| 1536 | + " \"y\": {\"y\": 291},\n", |
| 1537 | + " \"lat\": {\"x\": 3, \"y\": 291},\n", |
| 1538 | + " \"lev\": {\"lev\": 1},\n", |
| 1539 | + " \"lon\": {\"x\": 3, \"y\": 291},\n", |
| 1540 | + " \"time\": {\"time\": 6012},\n", |
1522 | 1541 | " }\n", |
1523 | | - " max_mem = '1GB'\n", |
| 1542 | + " max_mem = \"1GB\"\n", |
1524 | 1543 | "\n", |
1525 | | - " array_plan = rechunk(ds_control[['thetao']], target_chunks, max_mem, mapper, temp_store=mapper_temp)\n", |
| 1544 | + " array_plan = rechunk(\n", |
| 1545 | + " ds_control[[\"thetao\"]], target_chunks, max_mem, mapper, temp_store=mapper_temp\n", |
| 1546 | + " )\n", |
1526 | 1547 | " array_plan.execute(retries=10)\n", |
1527 | | - " \n", |
| 1548 | + "\n", |
1528 | 1549 | "ds_control_rechunked = xr.open_zarr(mapper, use_cftime=True)\n", |
1529 | | - "ds_control_rechunked " |
| 1550 | + "ds_control_rechunked" |
1530 | 1551 | ] |
1531 | 1552 | }, |
1532 | 1553 | { |
|
2061 | 2082 | } |
2062 | 2083 | ], |
2063 | 2084 | "source": [ |
2064 | | - "drift = calculate_drift(ds_control_rechunked, ds_historical, 'thetao') \n", |
2065 | | - "drift = drift.load() # This takes a bit, but it is worth loading this small output to avoid repeated computation\n", |
| 2085 | + "drift = calculate_drift(ds_control_rechunked, ds_historical, \"thetao\")\n", |
| 2086 | + "drift = drift.load() # This takes a bit, but it is worth loading this small output to avoid repeated computation\n", |
2066 | 2087 | "drift" |
2067 | 2088 | ] |
2068 | 2089 | }, |
|
2116 | 2137 | "source": [ |
2117 | 2138 | "start = drift.trend_time_range.isel(bnds=0).data.tolist()\n", |
2118 | 2139 | "stop = drift.trend_time_range.isel(bnds=1).data.tolist()\n", |
2119 | | - "time = xr.cftime_range(start, stop, freq='1MS')\n", |
| 2140 | + "time = xr.cftime_range(start, stop, freq=\"1MS\")\n", |
2120 | 2141 | "\n", |
2121 | 2142 | "# cut the control it to the time over which the trend was calculated\n", |
2122 | 2143 | "ds_control_cut = ds_control_rechunked.sel(time=slice(start, stop))\n", |
2123 | 2144 | "\n", |
2124 | 2145 | "# use the linear slope from the same point to construct a trendline\n", |
2125 | | - "trendline = xr.DataArray((np.arange(len(time)) * drift.thetao.isel(**roi).data) + ds_control_cut.thetao.isel(**roi, time=0).data, dims=['time'], coords={'time':time})" |
| 2146 | + "trendline = xr.DataArray(\n", |
| 2147 | + " (np.arange(len(time)) * drift.thetao.isel(**roi).data)\n", |
| 2148 | + " + ds_control_cut.thetao.isel(**roi, time=0).data,\n", |
| 2149 | + " dims=[\"time\"],\n", |
| 2150 | + " coords={\"time\": time},\n", |
| 2151 | + ")" |
2126 | 2152 | ] |
2127 | 2153 | }, |
2128 | 2154 | { |
|
2205 | 2231 | } |
2206 | 2232 | ], |
2207 | 2233 | "source": [ |
2208 | | - "control_detrended = remove_trend(ds_control, drift, 'thetao', ref_date=str(ds_control.time.data[0]))\n", |
| 2234 | + "control_detrended = remove_trend(\n", |
| 2235 | + " ds_control, drift, \"thetao\", ref_date=str(ds_control.time.data[0])\n", |
| 2236 | + ")\n", |
2209 | 2237 | "control_detrended.isel(**roi).plot()" |
2210 | 2238 | ] |
2211 | 2239 | }, |
|
2247 | 2275 | } |
2248 | 2276 | ], |
2249 | 2277 | "source": [ |
2250 | | - "ds_historical_dedrifted = remove_trend(ds_historical, drift, 'thetao', ref_date=str(ds_historical.time.data[0]))\n", |
| 2278 | + "ds_historical_dedrifted = remove_trend(\n", |
| 2279 | + " ds_historical, drift, \"thetao\", ref_date=str(ds_historical.time.data[0])\n", |
| 2280 | + ")\n", |
2251 | 2281 | "ds_historical_dedrifted.isel(**roi).plot()" |
2252 | 2282 | ] |
2253 | 2283 | }, |
|
2866 | 2896 | } |
2867 | 2897 | ], |
2868 | 2898 | "source": [ |
2869 | | - "ds_historical_dedrifted.attrs['drift_removed']" |
| 2899 | + "ds_historical_dedrifted.attrs[\"drift_removed\"]" |
2870 | 2900 | ] |
2871 | 2901 | }, |
2872 | 2902 | { |
|
2930 | 2960 | "ds_ssp585_dedrifted = remove_trend(\n", |
2931 | 2961 | " ds_ssp585,\n", |
2932 | 2962 | " drift,\n", |
2933 | | - " 'thetao',\n", |
2934 | | - " ref_date=str(ds_historical.time.data[0]) \n", |
2935 | | - " # Note that the ref_date is still the first time point of the *historical*run. \n", |
2936 | | - " # This ensures that the scenario is treated as an extension of the historical \n", |
| 2963 | + " \"thetao\",\n", |
| 2964 | + " ref_date=str(ds_historical.time.data[0]),\n", |
| 2965 | + " # Note that the ref_date is still the first time point of the *historical*run.\n", |
| 2966 | + " # This ensures that the scenario is treated as an extension of the historical\n", |
2937 | 2967 | " # run and the offset is calculated appropriately\n", |
2938 | 2968 | ")" |
2939 | 2969 | ] |
|
2968 | 2998 | } |
2969 | 2999 | ], |
2970 | 3000 | "source": [ |
2971 | | - "ds_historical.isel(**roi).thetao.coarsen(time=36, boundary='trim').mean().plot(color='C0', label='raw data')\n", |
2972 | | - "ds_ssp585.isel(**roi).thetao.coarsen(time=36, boundary='trim').mean().plot(color='C0')\n", |
2973 | | - "ds_historical_dedrifted.isel(**roi).coarsen(time=36, boundary='trim').mean().plot(color='C1', label='control drift removed')\n", |
2974 | | - "ds_ssp585_dedrifted.isel(**roi).coarsen(time=36, boundary='trim').mean().plot(color='C1')" |
| 3001 | + "ds_historical.isel(**roi).thetao.coarsen(time=36, boundary=\"trim\").mean().plot(\n", |
| 3002 | + " color=\"C0\", label=\"raw data\"\n", |
| 3003 | + ")\n", |
| 3004 | + "ds_ssp585.isel(**roi).thetao.coarsen(time=36, boundary=\"trim\").mean().plot(color=\"C0\")\n", |
| 3005 | + "ds_historical_dedrifted.isel(**roi).coarsen(time=36, boundary=\"trim\").mean().plot(\n", |
| 3006 | + " color=\"C1\", label=\"control drift removed\"\n", |
| 3007 | + ")\n", |
| 3008 | + "ds_ssp585_dedrifted.isel(**roi).coarsen(time=36, boundary=\"trim\").mean().plot(\n", |
| 3009 | + " color=\"C1\"\n", |
| 3010 | + ")" |
2975 | 3011 | ] |
2976 | 3012 | }, |
2977 | 3013 | { |
|
0 commit comments