create DAMIP files.ipynb 23.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c73bb8b",
   "metadata": {},
   "source": [
    "## Utilities"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
13
   "execution_count": 8,
14
15
16
   "id": "cfe495d7",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
17
18
     "end_time": "2022-01-05T13:29:39.635445Z",
     "start_time": "2022-01-05T13:29:13.046460Z"
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
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from jupyterthemes import jtplot\n",
    "import matplotlib as mpl\n",
    "from scipy.integrate import odeint\n",
    "from numpy import linalg as LA\n",
    "from scipy.optimize import fsolve\n",
    "from scipy.optimize import curve_fit\n",
    "mpl.rcdefaults() \n",
    "from scipy.stats import uniform\n",
    "import scipy.stats as st\n",
    "from matplotlib import colors\n",
    "from matplotlib import cm\n",
    "import pandas as pd\n",
    "import datetime\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "import cartopy.crs as ccrs\n",
    "import cartopy\n",
    "import json\n",
    "import xarray as xr\n",
    "import pickle\n",
    "import cftime\n",
    "from scipy.stats import linregress\n",
    "from EWS_functions import *\n",
    "from get_subpolar_gyre_functions import *\n",
    "from scipy.optimize import curve_fit\n",
    "from scipy.ndimage import gaussian_filter1d\n",
    "\n",
    "jtplot.style(context='paper', fscale=1.4, spines=True, grid=False, ticks=True,gridlines='--')\n",
    "\n",
    "fontsize=16\n",
    "mpl.rcParams['xtick.direction'] = 'in'\n",
    "mpl.rcParams['ytick.direction'] = 'in'\n",
    "mpl.rcParams['xtick.top'] = True\n",
    "mpl.rcParams['ytick.right'] = True\n",
    "\n",
    "mpl.rcParams['font.size'] = 16\n",
    "mpl.rcParams['legend.fontsize'] = 'large'\n",
    "mpl.rcParams['figure.titlesize'] = 'medium'\n",
    "mpl.rcParams['axes.labelsize']= 'x-large'\n",
    "mpl.rcParams['figure.facecolor']='white'\n",
    "\n",
    "mpl.rcParams['font.family'] = 'sans-serif'\n",
    "mpl.rcParams['font.sans-serif'] = ['Arial']\n",
    "hfont = {'fontname':'Arial'}\n",
    "\n",
    "mpl.rcParams['text.latex.preamble']= r'\\usepackage{amsmath}'\n",
    "mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=['#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e','#e6ab02','#a6761d','#666666']) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71826856",
   "metadata": {},
   "source": [
    "### EWS plot functions"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
82
   "execution_count": 9,
83
84
85
   "id": "0e524815",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
86
87
     "end_time": "2022-01-05T13:29:39.721126Z",
     "start_time": "2022-01-05T13:29:39.713467Z"
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
    },
    "init_cell": true
   },
   "outputs": [],
   "source": [
    "def get_EWS(time,data,trend,ws):\n",
    "    linfits = []\n",
    "    ps = []\n",
    "    bound = ws // 2\n",
    "    \n",
    "    std = runstd(data - trend, ws)[bound:-bound]\n",
    "    p0, p1 = np.polyfit(time[bound : -bound][:-2], std[:-2], 1)\n",
    "    linfits.append([p0,p1])\n",
    "    ps.append(kendall_tau_test(std[:-2], 1000, p0))\n",
    "    \n",
    "    ar1 = runac(data - trend, ws)[bound : -bound]\n",
    "    p0, p1 = np.polyfit(time[bound : -bound][:-3], ar1[:-3], 1)\n",
    "    linfits.append([p0,p1])\n",
    "    ps.append(kendall_tau_test(ar1[:-2], 1000, p0))\n",
    "    \n",
    "    lam = run_fit_a_ar1(data-trend,ws)[bound:-bound]\n",
    "    p0, p1 = np.polyfit(time[bound : -bound][:-2], lam[:-2], 1)\n",
    "    linfits.append([p0,p1])\n",
    "    ps.append(kendall_tau_test(lam[:-2], 1000, p0))\n",
    "    \n",
    "    return std, ar1, lam, linfits, ps"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
118
   "execution_count": 10,
119
120
121
   "id": "9115bb07",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
122
123
     "end_time": "2022-01-05T13:29:39.739767Z",
     "start_time": "2022-01-05T13:29:39.722633Z"
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
    },
    "init_cell": true
   },
   "outputs": [],
   "source": [
    "def plot_EWS(data, timess, ws=70, col='k',lbl='',alph=1,lw=1):\n",
    "    bound = ws // 2\n",
    "    popt, cov = curve_fit(funcfit3, timess, data, p0 = [-8.33097773e-01,  1.05507897e-02,  2.02518923e+03], maxfev = 1000000000)\n",
    "    trend = funcfit3(timess, *popt)\n",
    "    std, ar1, lam, linfits, ps = get_EWS(timess,data,trend,ws)\n",
    "\n",
    "    ax1.plot(timess[bound : -bound],std,color=col,label=lbl,alpha=alph,lw=lw)\n",
    "    pv = kendall_tau_test(std[:-2],1000,linfits[1][0])\n",
    "    ax1.plot(timess[bound : -bound][:-2],linfits[0][0] * timess[bound : -bound][:-2] + linfits[0][1],linestyle='--',color=col,alpha=alph,lw=lw,label=\"p = {:.3f}\".format(pv))\n",
    "\n",
    "    ax2.plot(timess[bound : -bound],ar1,color=col,label=lbl,alpha=alph,lw=lw)\n",
    "    pv = kendall_tau_test(ar1[:-2],1000,linfits[1][0])\n",
    "    ax2.plot(timess[bound : -bound][:-2],linfits[1][0] * timess[bound : -bound][:-2] + linfits[1][1],linestyle='--',color=col,alpha=alph,lw=lw,label=\"p = {:.3f}\".format(pv))\n",
    "\n",
    "    ax3.plot(timess[bound : -bound],lam,color=col,label=lbl,alpha=alph,lw=lw)\n",
    "    p0, p1, p2 = np.polyfit(timess[bound : -bound][:-2], lam[:-2], 2)\n",
    "    pl0, pl1  = np.polyfit(timess[bound : -bound][:-2], lam[:-2], 1)\n",
    "    pv = kendall_tau_test(lam[:-2], 1000, pl0) # precentile of 1000 fourier surrogates have a larger linear slope\n",
    "#     ax3.plot(timess[bound : -bound][:-2], p0 * timess[bound : -bound][:-2]**2+p1 * timess[bound : -bound][:-2] + p2, color=col,linestyle='--',alpha=alph,lw=lw,label=\"p = {:.3f}\".format(pv))\n",
    "    ax3.plot(timess[bound : -bound][:-2], pl0 * timess[bound : -bound][:-2] + pl1, color=col,linestyle='--',alpha=alph,lw=lw,label=\"p = {:.3f}\".format(pv))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "42e1b0d1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T12:59:28.224539Z",
     "start_time": "2021-12-31T12:59:28.212342Z"
    }
   },
   "outputs": [],
   "source": [
    "nparray = np.empty((8, 10, 1980))\n",
    "nparray.fill(np.nan)\n",
    "dims = ('models', 'ensemble_members', 'time')\n",
    "models = ['CESM2','HadGEM3-GC31-LL',\n",
    " 'CNRM-CM6-1',\n",
    " 'GISS-E2-1-G',\n",
    " 'MIROC6',\n",
    " 'CanESM5',\n",
    " 'BCC-CSM2-MR',\n",
    " 'IPSL-CM6A-LR']\n",
    "ensemble_members = ['r{}i1p1f1'.format(i) for i in range(1,11)]\n",
    "time = np.arange(np.datetime64('1850-01'), np.datetime64('2015-01'))\n",
    "\n",
    "\n",
    "# aer_amoc_index = xr.DataArray(\n",
    "#     nparray,\n",
    "#     dims = dims,\n",
    "#     coords = dict(\n",
    "#         models      = xr.DataArray(models, dims=\"models\", coords=dict(models=(\"models\", models))),\n",
    "#         time        = xr.DataArray(time, dims=\"time\", coords=dict(time=(\"time\", time))),\n",
    "#         ensemble_members = xr.DataArray(ensemble_members, dims=\"ensemble_members\", coords=dict(ensemble_members=(\"ensemble_members\", ensemble_members))),\n",
    "#         )\n",
    "#     )\n",
    "ghg_amoc_index = xr.DataArray(\n",
    "    nparray,\n",
    "    dims = dims,\n",
    "    coords = dict(\n",
    "        models      = xr.DataArray(models, dims=\"models\", coords=dict(models=(\"models\", models))),\n",
    "        time        = xr.DataArray(time, dims=\"time\", coords=dict(time=(\"time\", time))),\n",
    "        ensemble_members = xr.DataArray(ensemble_members, dims=\"ensemble_members\", coords=dict(ensemble_members=(\"ensemble_members\", ensemble_members))),\n",
    "        )\n",
    "    )\n",
    "nat_amoc_index = xr.DataArray(\n",
    "    nparray,\n",
    "    dims = dims,\n",
    "    coords = dict(\n",
    "        models      = xr.DataArray(models, dims=\"models\", coords=dict(models=(\"models\", models))),\n",
    "        time        = xr.DataArray(time, dims=\"time\", coords=dict(time=(\"time\", time))),\n",
    "        ensemble_members = xr.DataArray(ensemble_members, dims=\"ensemble_members\", coords=dict(ensemble_members=(\"ensemble_members\", ensemble_members))),\n",
    "        )\n",
    "    )\n",
    "# aer_amoc_index.to_netcdf('hist_aer_amoc_index.nc')\n",
    "# nat_amoc_index.to_netcdf('hist_nat_amoc_index.nc')\n",
    "# ghg_amoc_index.to_netcdf('hist_ghg_amoc_index.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "3fcc05ea",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T12:57:35.874219Z",
     "start_time": "2021-12-31T12:57:35.869223Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lon_min: 305\n",
      "lon_max: 340\n",
      "lat_min: 46\n",
      "lat_max: 61\n",
      "lon_keys: ['lon', 'longitude', 'nav_lon']\n",
      "lat_keys: ['lat', 'latitude', 'nav_lat']\n"
     ]
    }
   ],
   "source": [
    "# Region\n",
    "latitude_maximum = 61\n",
    "latitude_minimum = 46\n",
    "longitude_maximum = 360-20\n",
    "longitude_minimum = 360-55\n",
    "\n",
    "set_boundaries(\n",
    "    longitude_maximum = longitude_maximum,\n",
    "    longitude_minimum = longitude_minimum,\n",
    "    latitude_maximum = latitude_maximum,\n",
    "    latitude_minimum = latitude_minimum\n",
    ")\n",
    "\n",
    "# these seem to be the three latitude/longitude keys\n",
    "lat_keys = ['lat', 'latitude', 'nav_lat']\n",
    "lon_keys = ['lon', 'longitude', 'nav_lon']\n",
    "\n",
    "set_lon_lat_keys(\n",
    "    longitudes=lon_keys,\n",
    "    latitudes =lat_keys\n",
    ")\n",
    "\n",
    "\n",
    "get_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cca52407",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "9a42a710",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T12:57:38.452632Z",
     "start_time": "2021-12-31T12:57:38.383232Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No hist-nat for  GISS-E2-1-G\n"
     ]
    }
   ],
   "source": [
    "path_base = '/p/tmp/mayayami/SYNDA/data/CMIP6/DAMIP/'\n",
    "institutes = os.listdir(path_base)\n",
    "aerpaths_all_models = dict()\n",
    "\n",
    "for i in institutes:\n",
    "    pi = path_base + i\n",
    "    models = os.listdir(pi)\n",
    "    for m in models:\n",
    "        pim = pi + '/' + m + '/hist-aer/'\n",
    "        emembers = os.listdir(pim)\n",
    "        tmp = dict()\n",
    "        for v in emembers:\n",
    "            paths = [x[0] for x in os.walk(pim+v)]\n",
    "            data_paths = []\n",
    "            for path in paths:\n",
    "                if glob.glob(\"{}/tos_Omon_*.nc\".format(path)):\n",
    "                    data_paths.append(glob.glob(\"{}/tos_Omon_*.nc\".format(path)))\n",
    "            if (len(data_paths)) != 1:\n",
    "                print(\"Several paths provided for model '\"+m+\"' and ensemble member '\"+v+\"'!\")\n",
    "                print(data_paths)\n",
    "            tmp[v] = data_paths\n",
    "        aerpaths_all_models[m] = tmp\n",
    "\n",
    "ghgpaths_all_models = dict()\n",
    "\n",
    "for i in institutes:\n",
    "    pi = path_base + i\n",
    "    models = os.listdir(pi)\n",
    "    for m in models:\n",
    "        pim = pi + '/' + m + '/hist-GHG/'\n",
    "        emembers = os.listdir(pim)\n",
    "        tmp = dict()\n",
    "        for v in emembers:\n",
    "            paths = [x[0] for x in os.walk(pim+v)]\n",
    "            data_paths = []\n",
    "            for path in paths:\n",
    "                if glob.glob(\"{}/tos_Omon_*.nc\".format(path)):\n",
    "                    data_paths.append(glob.glob(\"{}/tos_Omon_*.nc\".format(path)))\n",
    "            if (len(data_paths)) != 1:\n",
    "                print(\"Several paths provided for model '\"+m+\"' and ensemble member '\"+v+\"'!\")\n",
    "                print(data_paths)\n",
    "            tmp[v] = data_paths\n",
    "        ghgpaths_all_models[m] = tmp\n",
    "        \n",
    "natpaths_all_models = dict()\n",
    "\n",
    "for i in institutes:\n",
    "    pi = path_base + i\n",
    "    models = os.listdir(pi)\n",
    "    for m in models:\n",
    "        pim = pi + '/' + m + '/hist-nat/'\n",
    "        try:\n",
    "            emembers = os.listdir(pim)\n",
    "            tmp = dict()\n",
    "            for v in emembers:\n",
    "                paths = [x[0] for x in os.walk(pim+v)]\n",
    "                data_paths = []\n",
    "                for path in paths:\n",
    "                    if glob.glob(\"{}/tos_Omon_*.nc\".format(path)):\n",
    "                        data_paths.append(glob.glob(\"{}/tos_Omon_*.nc\".format(path)))\n",
    "                if (len(data_paths)) != 1:\n",
    "                    print(\"Several paths provided for model '\"+m+\"' and ensemble member '\"+v+\"'!\")\n",
    "                    print(data_paths)\n",
    "                tmp[v] = data_paths\n",
    "            natpaths_all_models[m] = tmp\n",
    "        except:\n",
    "            print('No hist-nat for ',m)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f36bc291",
   "metadata": {},
   "source": [
    "## Do it for DAMIP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "4c4ddf62",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T13:31:25.858068Z",
     "start_time": "2021-12-31T13:31:25.842822Z"
    }
   },
   "outputs": [],
   "source": [
    "aer_amoc_index = xr.open_dataarray(\"hist_aer_amoc_index.nc\")\n",
    "# aer_amoc_index = time_to_year_month(aer_amoc_index)\n",
    "# # Test with one ensemble member per model\n",
    "# for model, emembers in aerpaths_all_models.items():\n",
    "#     print('------------------------------------------')\n",
    "#     print(model)\n",
    "#     print_info = True\n",
    "#     for emember in list(emembers.keys()):\n",
    "#         emember_in_amoc_index = emember[:-1]+\"1\" \n",
    "\n",
    "#         spg = get_subpolar_gyre(\n",
    "#             model   = model,\n",
    "#             emember =  emember, \n",
    "#             print_info = print_info, \n",
    "#             paths_all_models = aerpaths_all_models)\n",
    "        \n",
    "#         mask_time = (spg.time <= np.datetime64('2014-12-01'))\n",
    "#         spg = spg.where(mask_time, drop=True)\n",
    "\n",
    "#         if not spg is None:\n",
    "#             print_info = False\n",
    "#             aer_amoc_index.loc[dict(models=model, ensemble_members=emember_in_amoc_index)] = spg\n",
    "# aer_amoc_index.to_netcdf('hist_aer_amoc_index.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "73f1acf4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T13:05:25.527550Z",
     "start_time": "2021-12-31T13:05:25.524809Z"
    }
   },
   "outputs": [],
   "source": [
    "# # nat_amoc_index = xr.open_dataarray(\"hist_nat_amoc_index.nc\")\n",
    "# nat_amoc_index = time_to_year_month(nat_amoc_index)\n",
    "# # Test with one ensemble member per model\n",
    "# for model, emembers in natpaths_all_models.items():\n",
    "#     print('------------------------------------------')\n",
    "#     print(model)\n",
    "#     print_info = True\n",
    "#     for emember in list(emembers.keys()):\n",
    "#         emember_in_amoc_index = emember[:-1]+\"1\" \n",
    "\n",
    "#         spg = get_subpolar_gyre(\n",
    "#             model   = model,\n",
    "#             emember =  emember, \n",
    "#             print_info = print_info, \n",
    "#             paths_all_models = natpaths_all_models)\n",
    "        \n",
    "#         mask_time = (spg.time <= np.datetime64('2014-12-01'))\n",
    "#         spg = spg.where(mask_time, drop=True)\n",
    "        \n",
    "#         if not spg is None:\n",
    "#             print_info = False\n",
    "#             nat_amoc_index.loc[dict(models=model, ensemble_members=emember_in_amoc_index)] = spg\n",
    "# nat_amoc_index.to_netcdf('hist_nat_amoc_index.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "c526da97",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-31T13:14:29.797853Z",
     "start_time": "2021-12-31T13:14:29.793114Z"
    }
   },
   "outputs": [],
   "source": [
    "# # ghg_amoc_index = xr.open_dataarray(\"hist_ghg_amoc_index.nc\")\n",
    "# ghg_amoc_index = time_to_year_month(ghg_amoc_index)\n",
    "\n",
    "# for model, emembers in ghgpaths_all_models.items():\n",
    "#     print('------------------------------------------')\n",
    "#     print(model)\n",
    "#     print_info = True\n",
    "#     for emember in list(emembers.keys()):\n",
    "#         emember_in_amoc_index = emember[:-1]+\"1\" \n",
    "\n",
    "#         spg = get_subpolar_gyre(\n",
    "#             model   = model,\n",
    "#             emember =  emember, \n",
    "#             print_info = print_info, \n",
    "#             paths_all_models = ghgpaths_all_models)\n",
    "        \n",
    "#         mask_time = (spg.time <= np.datetime64('2014-12-01'))\n",
    "#         spg = spg.where(mask_time, drop=True)\n",
    "\n",
    "\n",
    "#         if not spg is None:\n",
    "#             print_info = False\n",
    "#             ghg_amoc_index.loc[dict(models=model, ensemble_members=emember_in_amoc_index)] = spg\n",
    "        \n",
    "# ghg_amoc_index.to_netcdf('hist_ghg_amoc_index.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a475de5c",
   "metadata": {},
   "source": [
    "# Full file"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
488
   "execution_count": 5,
489
490
491
   "id": "a52b47b7",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
492
493
     "end_time": "2022-01-05T13:28:57.507947Z",
     "start_time": "2022-01-05T13:28:57.319145Z"
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    }
   },
   "outputs": [],
   "source": [
    "with open('matthew/JSON_data/Figure_AR6_DAMIP_AMOC_26N_1000m.json', 'r') as handle:\n",
    "    json_load = json.load(handle)\n",
    "\n",
    "amoc_damip6_ts = np.ma.asarray(json_load[\"amoc_damip6_ts\"])  # Note the use of numpy masked arrays (np.ma)\n",
    "damip6_models = json_load[\"damip6_models\"]\n",
    "year = np.asarray(json_load[\"year\"])"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
508
   "execution_count": 6,
509
510
511
   "id": "3f8dbd9b",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
512
513
     "end_time": "2022-01-05T13:28:58.169995Z",
     "start_time": "2022-01-05T13:28:58.166671Z"
514
515
516
517
518
519
520
521
522
    }
   },
   "outputs": [],
   "source": [
    "experiments_damip6 = ['historical', 'hist-aer', 'hist-GHG', 'hist-nat', 'hist-stratO3']"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
523
   "execution_count": 11,
524
525
526
   "id": "62734989",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
527
528
     "end_time": "2022-01-05T13:29:52.207296Z",
     "start_time": "2022-01-05T13:29:52.202138Z"
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
    }
   },
   "outputs": [],
   "source": [
    "amoc_damip6 = xr.DataArray(\n",
    "    amoc_damip6_ts,\n",
    "    dims = ('models', 'experiments', 'ensemble_members', 'latitudes', 'time'),\n",
    "    coords = dict(\n",
    "        models      = damip6_models,\n",
    "        experiments = ['historical', 'hist-aer', 'hist-GHG', 'hist-nat', 'hist-stratO3'],\n",
    "        latitudes   = ['26.5N', '35N'],\n",
    "        time        = year\n",
    "        )\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
547
   "execution_count": 12,
548
549
550
   "id": "48fe8589",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
551
552
     "end_time": "2022-01-05T13:29:53.921065Z",
     "start_time": "2022-01-05T13:29:53.918444Z"
553
554
555
556
557
558
559
560
561
    }
   },
   "outputs": [],
   "source": [
    "ens_names = ['r{}i1p1f1'.format(i) for i in range(1,11)]"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
562
   "execution_count": 13,
563
564
565
   "id": "4302aa75",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
566
567
     "end_time": "2022-01-05T13:29:55.208336Z",
     "start_time": "2022-01-05T13:29:55.154808Z"
568
569
570
571
572
573
574
575
576
577
578
579
580
    }
   },
   "outputs": [],
   "source": [
    "ds = xr.open_dataset('CMIP6_amoc.nc')\n",
    "strn26 = ds.strength_265N\n",
    "strn35 = ds.strength_35N\n",
    "index = ds.index\n",
    "historical = index.sel(models=damip6_models)"
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
581
   "execution_count": 15,
582
583
584
   "id": "6b0fea79",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
585
586
     "end_time": "2022-01-05T13:30:54.330694Z",
     "start_time": "2022-01-05T13:30:53.764183Z"
587
588
589
590
    }
   },
   "outputs": [],
   "source": [
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
591
592
593
594
595
596
597
    "# aer = aer_amoc_index.sel(models=damip6_models).groupby(\"time.year\").mean(dim=\"time\")\n",
    "# nat = nat_amoc_index.sel(models=damip6_models).groupby(\"time.year\").mean(dim=\"time\")\n",
    "# ghg = ghg_amoc_index.sel(models=damip6_models).groupby(\"time.year\").mean(dim=\"time\")\n",
    "\n",
    "aer = xr.open_dataarray('hist_aer_amoc_index.nc').groupby('time.year').mean('time').sel(models=damip6_models)\n",
    "nat = xr.open_dataarray('hist_nat_amoc_index.nc').groupby('time.year').mean('time').sel(models=damip6_models)\n",
    "ghg = xr.open_dataarray('hist_ghg_amoc_index.nc').groupby('time.year').mean('time').sel(models=damip6_models)"
598
599
600
601
   ]
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
602
   "execution_count": 16,
603
604
605
   "id": "44e25559",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
606
607
     "end_time": "2022-01-05T13:30:58.358209Z",
     "start_time": "2022-01-05T13:30:58.355254Z"
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    }
   },
   "outputs": [],
   "source": [
    "index_damip = np.stack([historical.values,aer.values,ghg.values,nat.values],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "574e4e20",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
625
   "execution_count": 17,
626
627
628
   "id": "ece37096",
   "metadata": {
    "ExecuteTime": {
Maya Ben-Yami's avatar
update    
Maya Ben-Yami committed
629
630
     "end_time": "2022-01-05T13:31:01.415493Z",
     "start_time": "2022-01-05T13:31:01.405757Z"
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
    }
   },
   "outputs": [],
   "source": [
    "damip_amoc = xr.Dataset(\n",
    "    data_vars = dict(amoc_damip=(['models', 'experiments', 'ensemble_members', 'latitudes', 'year'],amoc_damip6[:,:-1][:,:,:,:,:-86].values),\n",
    "                     index_damip=(['models', 'experiments', 'ensemble_members', 'year'],index_damip)),\n",
    "    coords = dict(\n",
    "            models      = xr.DataArray(damip6_models, dims=\"models\", coords=dict(models=(\"models\", damip6_models))),\n",
    "            year      = historical.year,\n",
    "            latitudes   = ['26.5N', '35N'],\n",
    "            experiments = ['historical', 'hist-aer', 'hist-GHG', 'hist-nat'],\n",
    "            ensemble_members = xr.DataArray(ens_names, dims=\"ensemble_members\", coords=dict(ensemble_members=(\"ensemble_members\", ens_names)))),\n",
    "    attrs = dict(\n",
    "    description='AMOC strength and index in the DAMIP experiments')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36ef1fb6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}