Skip to content
Snippets Groups Projects
renku_datasets_biweekly.ipynb 48.8 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create biweekly renku datasets from `climetlab-s2s-ai-challenge`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Goal:\n",
    "\n",
    "- Create biweekly renku datasets from [`climatelab-s2s-ai-challenge`](https://github.com/ecmwf-lab/climetlab-s2s-ai-challenge).\n",
    "- These renku datasets are then used in notebooks:\n",
    "    - `ML_train_and_predict.ipynb` to train the ML model and do ML-based predictions\n",
    "    - `RPSS_verification.ipynb` to calculate RPSS of the ML model\n",
    "\n",
    "Requirements:\n",
    "- [`climetlab`](https://github.com/ecmwf/climetlab)\n",
    "- [`climatelab-s2s-ai-challenge`](https://github.com/ecmwf-lab/climetlab-s2s-ai-challenge)\n",
    "- S2S and CPC observations uploaded on [European Weather Cloud (EWC)](https://storage.ecmwf.europeanweather.cloud/s2s-ai-challenge/data/training-input/0.3.0/netcdf/index.html)\n",
    "\n",
    "Output: [renku dataset](https://renku-python.readthedocs.io/en/latest/commands.html#module-renku.cli.dataset) `s2s-ai-challenge`\n",
    "- observations\n",
    "    - deterministic:\n",
    "        - `hindcast-like-observations_2000-2019_biweekly_deterministic.zarr`\n",
    "        - `forecast-like-observations_2020_biweekly_deterministic.zarr`\n",
    "    - edges:\n",
    "        - `hindcast-like-observations_2000-2019_biweekly_tercile-edges.nc`\n",
    "    - probabilistic:\n",
    "        - `hindcast-like-observations_2000-2019_biweekly_terciled.zarr`\n",
    "        - `forecast-like-observations_2020_biweekly_terciled.nc`\n",
    "- forecasts/hindcasts\n",
    "    - deterministic:\n",
    "        - `ecmwf_hindcast-input_2000-2019_biweekly_deterministic.zarr`\n",
    "        - `ecmwf_forecast-input_2020_biweekly_deterministic.zarr`\n",
    "    - more models could be added\n",
    "- benchmark:\n",
    "    - probabilistic:\n",
    "        - `ecmwf_recalibrated_benchmark_2020_biweekly_terciled.nc`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!renku dataset create s2s-ai-challenge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: ecmwflibs universal: cannot find a library called MagPlus\n",
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/climetlab/plotting/drivers/magics/actions.py:36: UserWarning: Magics library could not be found\n",
      "  warnings.warn(str(e))\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<xarray.core.options.set_options at 0x2b2b3825c090>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import climetlab as cml\n",
    "import matplotlib.pyplot as plt\n",
    "import xarray as xr\n",
    "import xskillscore as xs\n",
    "import pandas as pd\n",
    "\n",
    "xr.set_options(keep_attrs=True)\n",
    "xr.set_options(display_style='text')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# caching path for climetlab\n",
    "cache_path = \"/work/mh0727/m300524/S2S_AI/cache\" # set your own path\n",
    "cml.settings.set(\"cache-directory\", cache_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "cache_path = \"../data\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Climetlab version : 0.7.0\n",
      "Climetlab-s2s-ai-challenge plugin version : 0.6.0\n"
     ]
    }
   ],
   "source": [
    "import climetlab_s2s_ai_challenge\n",
    "print(f'Climetlab version : {cml.__version__}')\n",
    "print(f'Climetlab-s2s-ai-challenge plugin version : {climetlab_s2s_ai_challenge.__version__}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download and cache\n",
    "\n",
    "Download all files for the observations, forecast and hindcast."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['20200102', '20200109', '20200116', '20200123', '20200130']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = xr.cftime_range(start='20200102',freq='7D', periods=53).strftime('%Y%m%d').to_list()\n",
    "dates[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## observations `output-reference`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "obs_dataset_labels = ['training-output-reference','test-output-reference'] # ML community\n",
    "# equiv to\n",
    "obs_dataset_labels = ['hindcast-like-observations','forecast-like-observations'] # NWP community"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "varlist_obs = ['tp','t2m']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "# takes 5h for one model\n",
    "for v in varlist_obs:\n",
    "    for d in dates:\n",
    "        for ds in obs_dataset_labels:\n",
    "            print(ds,v,d)\n",
    "            # only netcdf, no choice\n",
    "            cml.load_dataset(f\"s2s-ai-challenge-{ds}\", date=d, parameter=v).to_xarray()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## hindcast and forecast `input`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "forecast_dataset_labels = ['training-input','test-input'] # ML community\n",
    "# equiv to\n",
    "forecast_dataset_labels = ['hindcast-input','forecast-input'] # NWP community"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "varlist_forecast = ['tp','t2m']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_list = ['ecmwf']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "# takes 4-5h for one model\n",
    "for model in model_list:\n",
    "    for v in varlist_forecast:\n",
    "        for d in dates:\n",
    "            for ds in forecast_dataset_labels:\n",
    "                print(ds,v,d)\n",
    "                # take netcdf format\n",
    "                cml.load_dataset(f\"s2s-ai-challenge-{ds}\", origin=model, date=d, parameter=v, format='netcdf').to_xarray()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# create bi-weekly aggregates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scripts import add_valid_time_from_forecast_reference_time_and_lead_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.DataArray (lead_time: 14)&gt;\n",
       "array([1209600000000000, 1296000000000000, 1382400000000000,\n",
       "       1468800000000000, 1555200000000000, 1641600000000000,\n",
       "       1728000000000000, 1814400000000000, 1900800000000000,\n",
       "       1987200000000000, 2073600000000000, 2160000000000000,\n",
       "       2246400000000000, 2332800000000000], dtype=&#x27;timedelta64[ns]&#x27;)\n",
       "Coordinates:\n",
       "  * lead_time  (lead_time) timedelta64[ns] 14 days 15 days ... 26 days 27 days</pre>"
      ],
      "text/plain": [
       "<xarray.DataArray (lead_time: 14)>\n",
       "array([1209600000000000, 1296000000000000, 1382400000000000,\n",
       "       1468800000000000, 1555200000000000, 1641600000000000,\n",
       "       1728000000000000, 1814400000000000, 1900800000000000,\n",
       "       1987200000000000, 2073600000000000, 2160000000000000,\n",
       "       2246400000000000, 2332800000000000], dtype='timedelta64[ns]')\n",
       "Coordinates:\n",
       "  * lead_time  (lead_time) timedelta64[ns] 14 days 15 days ... 26 days 27 days"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w34 = [pd.Timedelta(f'{i} d') for i in range(14,28)]\n",
    "w34 = xr.DataArray(w34,dims='lead_time', coords={'lead_time':w34})\n",
    "w34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.DataArray (lead_time: 14)&gt;\n",
       "array([2419200000000000, 2505600000000000, 2592000000000000,\n",
       "       2678400000000000, 2764800000000000, 2851200000000000,\n",
       "       2937600000000000, 3024000000000000, 3110400000000000,\n",
       "       3196800000000000, 3283200000000000, 3369600000000000,\n",
       "       3456000000000000, 3542400000000000], dtype=&#x27;timedelta64[ns]&#x27;)\n",
       "Coordinates:\n",
       "  * lead_time  (lead_time) timedelta64[ns] 28 days 29 days ... 40 days 41 days</pre>"
      ],
      "text/plain": [
       "<xarray.DataArray (lead_time: 14)>\n",
       "array([2419200000000000, 2505600000000000, 2592000000000000,\n",
       "       2678400000000000, 2764800000000000, 2851200000000000,\n",
       "       2937600000000000, 3024000000000000, 3110400000000000,\n",
       "       3196800000000000, 3283200000000000, 3369600000000000,\n",
       "       3456000000000000, 3542400000000000], dtype='timedelta64[ns]')\n",
       "Coordinates:\n",
       "  * lead_time  (lead_time) timedelta64[ns] 28 days 29 days ... 40 days 41 days"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w56 = [pd.Timedelta(f'{i} d') for i in range(28,42)]\n",
    "w56 = xr.DataArray(w56,dims='lead_time', coords={'lead_time':w56})\n",
    "w56"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# starting dates forecast_time in 2020\n",
    "dates = xr.cftime_range(start='20200102',freq='7D', periods=53).strftime('%Y%m%d').to_list()\n",
    "\n",
    "biweekly_lead = [pd.Timedelta(f\"{i} d\") for i in [14, 28]] # take first day of biweekly average \n",
    "# alternative: new lead_time coordinate: use 21 35 as mid points of week 3-4 and week 5-6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hindcast-input tp\n",
      "By downloading data from this dataset, you agree to the terms and conditions defined at https://apps.ecmwf.int/datasets/data/s2s/licence/. If you do not agree with such terms, do not download the data. \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: ecmwflibs universal: found eccodes at /work/mh0727/m300524/conda-envs/s2s-ai/lib/libeccodes.so\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warning: ecCodes 2.21.0 or higher is recommended. You are running version 2.12.3\n",
      "hindcast-input t2m\n",
      "save to: ../data/ecmwf_hindcast-input_2000-2019_biweekly_deterministic.zarr\n",
      "forecast-input tp\n",
      "By downloading data from this dataset, you agree to the terms and conditions defined at https://apps.ecmwf.int/datasets/data/s2s/licence/. If you do not agree with such terms, do not download the data. \n",
      "forecast-input t2m\n",
      "save to: ../data/ecmwf_forecast-input_2020_biweekly_deterministic.zarr\n",
      "hindcast-like-observations tp\n",
      "By downloading data from this dataset, you agree to the terms and conditions defined at https://apps.ecmwf.int/datasets/data/s2s/licence/. If you do not agree with such terms, do not download the data.  This dataset has been dowloaded from IRIDL. By downloading this data you also agree to the terms and conditions defined at https://iridl.ldeo.columbia.edu.\n",
      "hindcast-like-observations t2m\n",
      "save to: ../data/hindcast-like-observations_2000-2019_biweekly_deterministic.zarr\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/dask/array/numpy_compat.py:40: RuntimeWarning: invalid value encountered in true_divide\n",
      "  x = np.divide(x1, x2, out)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "forecast-like-observations tp\n",
      "By downloading data from this dataset, you agree to the terms and conditions defined at https://apps.ecmwf.int/datasets/data/s2s/licence/. If you do not agree with such terms, do not download the data.  This dataset has been dowloaded from IRIDL. By downloading this data you also agree to the terms and conditions defined at https://iridl.ldeo.columbia.edu.\n",
      "forecast-like-observations t2m\n",
      "save to: ../data/forecast-like-observations_2020_biweekly_deterministic.zarr\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/dask/array/numpy_compat.py:40: RuntimeWarning: invalid value encountered in true_divide\n",
      "  x = np.divide(x1, x2, out)\n"
     ]
    }
   ],
   "source": [
    "# refactor after closed: https://github.com/ecmwf/climetlab/issues/16 https://github.com/ecmwf-lab/climetlab-s2s-ai-challenge/issues/9\n",
    "# then, no need to loop over dates\n",
    "model = 'ecmwf'\n",
    "for dsl in forecast_dataset_labels + obs_dataset_labels: # dataset labels\n",
    "    for v in varlist_forecast:\n",
    "        print(dsl,v)\n",
    "        ds = []\n",
    "        for d in dates:\n",
    "            if 'input' in dsl:\n",
    "                # just ecmwf model for now\n",
    "                ds2 = cml.load_dataset(f\"s2s-ai-challenge-{dsl}\", origin=model, date=d, parameter=v, format='netcdf').to_xarray()\n",
    "            elif 'observation' in dsl: # obs only netcdf, no choice\n",
    "                ds2 = cml.load_dataset(f\"s2s-ai-challenge-{dsl}\", date=d, parameter=v).to_xarray().chunk()\n",
    "            if v == 't2m': # biweekly mean\n",
    "                d34 = ds2.sel(lead_time=w34).mean('lead_time')\n",
    "                d56 = ds2.sel(lead_time=w56).mean('lead_time')\n",
    "                ds2 = xr.concat([d34,d56],'lead_time').assign_coords(lead_time=biweekly_lead)\n",
    "            elif v=='tp': # biweekly sum\n",
    "                d34 = ds2.sel(lead_time=pd.Timedelta(\"27 d\")) - ds2.sel(lead_time=pd.Timedelta(\"13 d\")) # tp from day 14 to day 27\n",
    "                d56 = ds2.sel(lead_time=pd.Timedelta(\"41 d\")) - ds2.sel(lead_time=pd.Timedelta(\"27 d\")) # tp from day 28 to day 42\n",
    "                ds2 = xr.concat([d34,d56],'lead_time').assign_coords(lead_time=biweekly_lead)\n",
    "            ds.append(ds2.chunk({'forecast_time':1}))\n",
    "        \n",
    "        # sort forecast_time\n",
    "        ds = xr.concat(ds, 'forecast_time').sortby('forecast_time')\n",
    "        ds = add_valid_time_from_forecast_reference_time_and_lead_time(ds)\n",
    "        ds.lead_time.attrs['comment'] = 'lead_time describes bi-weekly aggregates. The pd.Timedelta corresponds to the first day of the '\n",
    "\n",
    "        # first run on tp and add t2m to that dataset\n",
    "        if v == 'tp':\n",
    "            ds_ = ds.copy()\n",
    "        elif v == 't2m':\n",
    "            ds_[v] = ds[v]\n",
    "            ds = ds_\n",
    "\n",
    "    if 'test' in dsl:\n",
    "        ds = ds.chunk('auto')\n",
    "    else:\n",
    "        ds = ds.chunk({'forecast_time':12,'lead_time':-1,'longitude':'auto','latitude':'auto'}) # 3 month init chunk\n",
    "    \n",
    "    if 'hindcast' in dsl:\n",
    "        time = '2000-2019'\n",
    "        if 'input' in dsl:\n",
    "            name = f'{model}_{dsl}'\n",
    "        elif 'observations':\n",
    "            name = dsl\n",
    "        \n",
    "    elif 'forecast' in dsl:\n",
    "        time = '2020'\n",
    "        if 'input' in dsl:\n",
    "            name = f'{model}_{dsl}'\n",
    "        elif 'observations':\n",
    "            name = dsl\n",
    "    else:\n",
    "        assert False\n",
    "    \n",
    "    # pattern: {model if applies _}{observations/forecast/hindcast}_{time}_biweekly_deterministic.zarr\n",
    "    zp = f'{cache_path}/{name}_{time}_biweekly_deterministic.zarr'\n",
    "    print(f'save to: {zp}')\n",
    "    ds.to_zarr(zp, consolidated=True, mode='w')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## add to `renku` dataset `s2s-ai-challenge`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# observations as hindcast\n",
    "# run renku commands from projects root directory only\n",
    "# !renku dataset add s2s-ai-challenge data/hindcast-like-observations_2000-2019_biweekly_deterministic.zarr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for further use retrieve from git lfs\n",
    "# !renku storage pull ../data/hindcast-like-observations_2000-2019_biweekly_deterministic.zarr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frozen(SortedKeysDict({'forecast_time': 1060, 'latitude': 121, 'lead_time': 2, 'longitude': 240})) \n",
      " Coordinates:\n",
      "  * forecast_time  (forecast_time) datetime64[ns] 2000-01-02 ... 2019-12-31\n",
      "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
      "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
      "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
      "    valid_time     (lead_time, forecast_time) datetime64[ns] dask.array<chunksize=(2, 1060), meta=np.ndarray> \n",
      " 985.065144 MB\n"
     ]
    }
   ],
   "source": [
    "obs_2000_2019 = xr.open_zarr(f\"{cache_path}/hindcast-like-observations_2000-2019_biweekly_deterministic.zarr\", consolidated=True)\n",
    "print(obs_2000_2019.sizes,'\\n',obs_2000_2019.coords,'\\n', obs_2000_2019.nbytes/1e6,'MB')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# observations as forecast\n",
    "# run renku commands from projects root directory only\n",
    "# !renku dataset add s2s-ai-challenge data/forecast-like-observations_2020_biweekly_deterministic.zarr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frozen(SortedKeysDict({'forecast_time': 53, 'latitude': 121, 'lead_time': 2, 'longitude': 240})) \n",
      " Coordinates:\n",
      "  * forecast_time  (forecast_time) datetime64[ns] 2020-01-02 ... 2020-12-31\n",
      "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
      "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
      "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
      "    valid_time     (lead_time, forecast_time) datetime64[ns] dask.array<chunksize=(2, 53), meta=np.ndarray> \n",
      " 49.256016 MB\n"
     ]
    }
   ],
   "source": [
    "obs_2020 = xr.open_zarr(f\"{cache_path}/forecast-like-observations_2020_biweekly_deterministic.zarr\", consolidated=True)\n",
    "print(obs_2020.sizes,'\\n',obs_2020.coords,'\\n', obs_2020.nbytes/1e6,'MB')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf hindcast-input\n",
    "# run renku commands from projects root directory only\n",
    "# !renku dataset add s2s-ai-challenge data/ecmwf_hindcast-input_2000-2019_biweekly_deterministic.zarr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frozen(SortedKeysDict({'forecast_time': 1060, 'latitude': 121, 'lead_time': 2, 'longitude': 240, 'realization': 11})) \n",
      " Coordinates:\n",
      "  * forecast_time  (forecast_time) datetime64[ns] 2000-01-02 ... 2019-12-31\n",
      "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
      "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
      "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
      "  * realization    (realization) int64 0 1 2 3 4 5 6 7 8 9 10\n",
      "    valid_time     (lead_time, forecast_time) datetime64[ns] dask.array<chunksize=(2, 1060), meta=np.ndarray> \n",
      " 5417.730832 MB\n"
     ]
    }
   ],
   "source": [
    "hind_2000_2019 = xr.open_zarr(f\"{cache_path}/ecmwf_hindcast-input_2000-2019_biweekly_deterministic.zarr\", consolidated=True)\n",
    "print(hind_2000_2019.sizes,'\\n',hind_2000_2019.coords,'\\n', hind_2000_2019.nbytes/1e6,'MB')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf forecast-input\n",
    "# run renku commands from projects root directory only\n",
    "# !renku dataset add s2s-ai-challenge data/ecmwf_forecast-input_2020_biweekly_deterministic.zarr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frozen(SortedKeysDict({'forecast_time': 53, 'latitude': 121, 'lead_time': 2, 'longitude': 240, 'realization': 51})) \n",
      " Coordinates:\n",
      "  * forecast_time  (forecast_time) datetime64[ns] 2020-01-02 ... 2020-12-31\n",
      "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
      "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
      "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
      "  * realization    (realization) int64 0 1 2 3 4 5 6 7 ... 44 45 46 47 48 49 50\n",
      "    valid_time     (lead_time, forecast_time) datetime64[ns] dask.array<chunksize=(2, 53), meta=np.ndarray> \n",
      " 1255.926504 MB\n"
     ]
    }
   ],
   "source": [
    "fct_2020 = xr.open_zarr(f\"{cache_path}/ecmwf_forecast-input_2020_biweekly_deterministic.zarr\", consolidated=True)\n",
    "print(fct_2020.sizes,'\\n',fct_2020.coords,'\\n', fct_2020.nbytes/1e6,'MB')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tercile edges\n",
    "\n",
    "Create 2 tercile edges at 1/3 and 2/3 quantiles of the 2000-2019 biweekly distrbution for each week of the year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "tercile_file = f'{cache_path}/hindcast-like-observations_2000-2019_biweekly_tercile-edges.nc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/xarray/core/accessor_dt.py:381: FutureWarning: dt.weekofyear and dt.week have been deprecated. Please use dt.isocalendar().week instead.\n",
      "  FutureWarning,\n",
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/numpy/lib/nanfunctions.py:1390: RuntimeWarning: All-NaN slice encountered\n",
      "  overwrite_input, interpolation)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 15min 49s, sys: 6min 16s, total: 22min 6s\n",
      "Wall time: 13min 14s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "xr.open_zarr(f'{cache_path}/hindcast-like-observations_2000-2019_biweekly_deterministic.zarr',\n",
    "             consolidated=True).chunk({'forecast_time':-1,'longitude':'auto'}).groupby('forecast_time.weekofyear').quantile(q=[1./3.,2./3.], dim=['forecast_time']).rename({'quantile':'category_edge'}).to_netcdf(tercile_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.Dataset&gt;\n",
       "Dimensions:        (category_edge: 2, latitude: 121, lead_time: 2, longitude: 240, weekofyear: 53)\n",
       "Coordinates:\n",
       "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
       "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
       "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
       "  * category_edge  (category_edge) float64 0.3333 0.6667\n",
       "  * weekofyear     (weekofyear) int64 1 2 3 4 5 6 7 8 ... 47 48 49 50 51 52 53\n",
       "Data variables:\n",
       "    t2m            (weekofyear, category_edge, lead_time, latitude, longitude) float64 ...\n",
       "    tp             (weekofyear, category_edge, lead_time, latitude, longitude) float64 ...</pre>"
      ],
      "text/plain": [
       "<xarray.Dataset>\n",
       "Dimensions:        (category_edge: 2, latitude: 121, lead_time: 2, longitude: 240, weekofyear: 53)\n",
       "Coordinates:\n",
       "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
       "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
       "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
       "  * category_edge  (category_edge) float64 0.3333 0.6667\n",
       "  * weekofyear     (weekofyear) int64 1 2 3 4 5 6 7 8 ... 47 48 49 50 51 52 53\n",
       "Data variables:\n",
       "    t2m            (weekofyear, category_edge, lead_time, latitude, longitude) float64 ...\n",
       "    tp             (weekofyear, category_edge, lead_time, latitude, longitude) float64 ..."
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tercile_edges = xr.open_dataset(tercile_file)\n",
    "\n",
    "tercile_edges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(98.507024, 'MB')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tercile_edges.nbytes*1e-6,'MB'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run renku commands from projects root directory only\n",
    "# tercile edges\n",
    "#!renku dataset add s2s-ai-challenge data/hindcast-like-observations_2000-2019_biweekly_tercile-edges.nc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to use retrieve from git lfs\n",
    "#!renku storage pull ../data/hindcast-like-observations_2000-2019_biweekly_tercile-edges.nc\n",
    "#xr.open_dataset(\"../data/hindcast-like-observations_2000-2019_biweekly_tercile-edges.nc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# observations in categories\n",
    "\n",
    "- counting how many deterministic forecasts realizations fall into each category, like counting rps\n",
    "- categorize forecast-like-observations 2020 into categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Frozen(SortedKeysDict({'forecast_time': 53, 'latitude': 121, 'lead_time': 2, 'longitude': 240}))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obs_2020 = xr.open_zarr(f'{cache_path}/forecast-like-observations_2020_biweekly_deterministic.zarr', consolidated=True)\n",
    "obs_2020.sizes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a mask for land grid\n",
    "mask = obs_2020.std(['lead_time','forecast_time']).notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mask.to_array().plot(col='variable')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tercile_edges = xr.open_dataset(tercile_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.DataArray &#x27;tp&#x27; (weekofyear: 53, category_edge: 2, lead_time: 2, latitude: 121, longitude: 240)&gt;\n",
       "[6156480 values with dtype=float64]\n",
       "Coordinates:\n",
       "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
       "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
       "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
       "  * category_edge  (category_edge) float64 0.3333 0.6667\n",
       "  * weekofyear     (weekofyear) int64 1 2 3 4 5 6 7 8 ... 47 48 49 50 51 52 53\n",
       "Attributes:\n",
       "    comment:        precipitation accumulated since lead_time including 0 days\n",
       "    long_name:      total precipitation\n",
       "    pointwidth:     0\n",
       "    standard_name:  precipitation_amount\n",
       "    units:          kg m-2</pre>"
      ],
      "text/plain": [
       "<xarray.DataArray 'tp' (weekofyear: 53, category_edge: 2, lead_time: 2, latitude: 121, longitude: 240)>\n",
       "[6156480 values with dtype=float64]\n",
       "Coordinates:\n",
       "  * latitude       (latitude) float64 90.0 88.5 87.0 85.5 ... -87.0 -88.5 -90.0\n",
       "  * lead_time      (lead_time) timedelta64[ns] 14 days 28 days\n",
       "  * longitude      (longitude) float64 0.0 1.5 3.0 4.5 ... 355.5 357.0 358.5\n",
       "  * category_edge  (category_edge) float64 0.3333 0.6667\n",
       "  * weekofyear     (weekofyear) int64 1 2 3 4 5 6 7 8 ... 47 48 49 50 51 52 53\n",
       "Attributes:\n",
       "    comment:        precipitation accumulated since lead_time including 0 days\n",
       "    long_name:      total precipitation\n",
       "    pointwidth:     0\n",
       "    standard_name:  precipitation_amount\n",
       "    units:          kg m-2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tercile_edges.tp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# total precipitation in arid regions are masked\n",
    "# Frederic Vitart suggested by email: \"Based on your map we could mask all the areas where the lower tercile boundary is lower than 0.1 mm\"\n",
    "# we are using a dry mask as in https://doi.org/10.1175/MWR-D-17-0092.1\n",
    "th = 0.01\n",
    "tp_arid_mask = tercile_edges.tp.isel(category_edge=0, lead_time=0, drop=True) > th\n",
    "#tp_arid_mask.where(mask.tp).plot(col='forecast_time', col_wrap=4)\n",
    "#plt.suptitle(f'dry mask: week 3-4 tp 1/3 category_edge > {th} kg m-2',y=1., x=.4)\n",
    "#plt.savefig('dry_mask.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# look into tercile edges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tercile_edges.isel(forecast_time=0)['tp'].plot(col='lead_time',row='category_edge', robust=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tercile_edges.isel(forecast_time=[0,20],category_edge=1)['tp'].plot(col='lead_time', row='forecast_time', robust=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tercile_edges.tp.mean(['forecast_time']).plot(col='lead_time',row='category_edge',vmax=.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## categorize observations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scripts import make_probabilistic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/work/mh0727/m300524/conda-envs/s2s-ai/lib/python3.7/site-packages/xarray/core/accessor_dt.py:381: FutureWarning: dt.weekofyear and dt.week have been deprecated. Please use dt.isocalendar().week instead.\n",
      "  FutureWarning,\n"
     ]
    }
   ],
   "source": [
    "obs_2020_p = make_probabilistic(obs_2020, tercile_edges, mask=mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(147.760264, 'MB')"
      ]
     },