{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Read monthly precipitation from the CMIP5 models Historical simulations and puts them all in the same grid and time period in a python-friendly structure (dictionary or xarray). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "import xesmf as xe\n", "\n", "from matplotlib import pyplot as plt\n", "%matplotlib inline\n", "plt.rcParams['figure.figsize'] = (8,5)\n", "\n", "import cartopy.crs as ccrs\n", "\n", "from glob import glob\n", "\n", "import os, sys\n", "sys.path.append(\"/home2/biasutti/py_packages\") # substitute your username for nhn2\n", "from ingrid import times\n", "from ingrid.times import to_pandas\n", "from ingrid.times import enso2date" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "models = !ls /net/strega.ldeo.columbia.edu/home/CMIP5/byScenario/historical/atmos/mon/pr\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "path = '/net/strega.ldeo.columbia.edu/home/CMIP5/byScenario/historical/atmos/mon/pr'\n", "dirs = glob(path+'/*')\n", "files = glob(path+'/*/r1i1p1/pr.nc')\n", "ds = list(map(lambda x: xr.open_dataset(x,decode_times=False), files))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reuse existing file: bilinear_48x96_89x179_peri.nc\n", "Reuse existing file: bilinear_145x192_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_128x256_89x179_peri.nc\n", "Reuse existing file: bilinear_96x192_89x179_peri.nc\n", "Reuse existing file: bilinear_160x320_89x179_peri.nc\n", "Reuse existing file: bilinear_320x640_89x179_peri.nc\n", "Reuse existing file: bilinear_96x144_89x179_peri.nc\n", "Reuse existing file: bilinear_96x144_89x179_peri.nc\n", "Reuse existing file: bilinear_160x320_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_145x192_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_96x96_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_240x480_89x179_peri.nc\n", "Reuse existing file: bilinear_160x320_89x179_peri.nc\n", "Reuse existing file: bilinear_145x192_89x179_peri.nc\n", "Reuse existing file: bilinear_96x144_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_192x288_89x179_peri.nc\n", "Reuse existing file: bilinear_192x288_89x179_peri.nc\n", "Reuse existing file: bilinear_160x320_89x179_peri.nc\n", "Reuse existing file: bilinear_96x192_89x179_peri.nc\n", "Reuse existing file: bilinear_60x128_89x179_peri.nc\n", "Reuse existing file: bilinear_145x192_89x179_peri.nc\n", "Reuse existing file: bilinear_108x128_89x179_peri.nc\n", "Reuse existing file: bilinear_143x144_89x179_peri.nc\n", "Reuse existing file: bilinear_96x192_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_120x180_89x179_peri.nc\n", "Reuse existing file: bilinear_96x96_89x179_peri.nc\n", "Reuse existing file: bilinear_128x256_89x179_peri.nc\n", "Reuse existing file: bilinear_96x144_89x179_peri.nc\n", "Reuse existing file: bilinear_145x192_89x179_peri.nc\n", "Reuse existing file: bilinear_73x96_89x179_peri.nc\n", "Reuse existing file: bilinear_90x144_89x179_peri.nc\n", "Reuse existing file: bilinear_96x192_89x179_peri.nc\n", "Reuse existing file: bilinear_192x288_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_128x256_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n", "Reuse existing file: bilinear_192x288_89x179_peri.nc\n", "Reuse existing file: bilinear_96x192_89x179_peri.nc\n", "Reuse existing file: bilinear_64x128_89x179_peri.nc\n" ] } ], "source": [ "ds_out = xr.Dataset({'lat': (['lat'], np.arange(-89,89, 2)),\n", " 'lon': (['lon'], np.arange(-179,179,2)),\n", " }\n", " )\n", "regridder = list(map(lambda x: \n", " xe.Regridder(x, ds_out, 'bilinear',\n", " periodic=True, reuse_weights=True ), ds))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# rewrite time in pandas format\n", "\n", "i = 0\n", "for k in ds:\n", " #print(k.time.units)\n", " #print(k.model_id)\n", " k['time'] = times.to_pandas(k['time'])\n", " i += 1\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# this does not work... is there a way to do the loop above this way?\n", "#ds_out = list(map(lambda x: to_pandas(x['time']), ds))\n", "#ds_out" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "#pr = list(map(lambda x: x.pr.sel(time=slice('1901-01-01', '2001-01-01')),ds))\n", "#pr_2x2 = list(map(lambda x,y: x(y),regridder,pr))\n", "pr_2x2 = list(map(lambda x,y: \n", " x(y.pr.sel(time=slice('1901-01-01', '2001-01-01'))),\n", " regridder,ds))\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# make a dictionary\n", "ensemble = {'model':models,'data':pr_2x2}\n", "# or make a xarray with an extra dimension\n", "pr_ens = xr.concat(pr_2x2[:],dim='model')" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# this might be needed for calculating gradients and stuff\n", "import xgcm\n", "grid = list(map(lambda x: xgcm.Grid(x, periodic=['X', 'Y']), ensemble['data']))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "gist_info": { "gist_id": null, "gist_url": null }, "kernelspec": { "display_name": "Py3 pangeo", "language": "python", "name": "pangeo" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }