• API
• FAQ
• Tools
• Archive
daily pastebin goal
25%
SHARE
TWEET

# Untitled

a guest Jul 11th, 2018 57 Never
ENDING IN00days00hours00mins00secs
1. {
2.  "cells": [
3.   {
4.    "cell_type": "code",
5.    "execution_count": 1,
7.     "collapsed": true
8.    },
9.    "outputs": [],
10.    "source": [
11.     "import xarray as xr\n",
12.     "import numpy as np\n",
13.     "import matplotlib.pyplot as plt"
14.    ]
15.   },
16.   {
17.    "cell_type": "code",
18.    "execution_count": 2,
20.     "collapsed": true
21.    },
22.    "outputs": [],
23.    "source": [
24.     "# Load model output of sea surface temperature (SST) from netCDF files\n",
25.     "datapath = '/mnt/disk2/data/CM4_historical/'\n",
26.     "data  = xr.open_dataset(datapath + 'ocean_monthly_1x1deg.201001-201412.tos.nc')\n",
27.     "grid = xr.open_dataset(datapath + 'ocean_monthly_1x1deg.static.nc')\n",
29.    ]
30.   },
31.   {
32.    "cell_type": "code",
33.    "execution_count": 4,
35.     "collapsed": true
36.    },
37.    "outputs": [],
38.    "source": [
39.     "# Calculate monthly averages\n",
40.     "monthly_average = np.zeros((12,nlat,nlon))\n",
41.     "nyears = ntime/12.\n",
42.     "for jtime in range(0,ntime):\n",
43.     "    current_month = np.mod(jtime,12) # Keep track of which month we're in\n",
44.     "    monthly_average[current_month,:,:] = tos[jtime,:,:] + monthly_average[current_month,:,:]\n",
45.     "monthly_average[:,:,:] = monthly_average[:,:,:]/nyears"
46.    ]
47.   },
48.   {
49.    "cell_type": "code",
50.    "execution_count": 5,
52.     "collapsed": true
53.    },
54.    "outputs": [],
55.    "source": [
56.     "# Calculate monthly anomalies by subtracting the monthly climatology\n",
57.     "tos_anomaly = np.zeros((ntime,nlat,nlon))\n",
58.     "for jtime in range(0,ntime):\n",
59.     "    current_month = np.mod(jtime,12) # Keep track of which month we're in\n",
60.     "    tos_anomaly[jtime,:,:] = tos[jtime,:,:] - monthly_average[current_month,:,:]"
61.    ]
62.   },
63.   {
64.    "cell_type": "code",
65.    "execution_count": 28,
67.    "outputs": [
68.     {
69.      "name": "stdout",
70.      "output_type": "stream",
71.      "text": [
72.       "0\n",
73.       "10\n",
74.       "20\n",
75.       "30\n",
76.       "40\n",
77.       "50\n",
78.       "60\n",
79.       "70\n",
80.       "80\n",
81.       "90\n",
82.       "100\n",
83.       "110\n",
84.       "120\n",
85.       "130\n",
86.       "140\n",
87.       "150\n",
88.       "160\n",
89.       "170\n"
90.      ]
91.     }
92.    ],
93.    "source": [
94.     "# Calculate the El-Nino Southern Oscillation (ENSO) Index from the Nino 3.4 region\n",
95.     "nino_minlat = -5  ; nino_maxlat = 5\n",
96.     "nino_minlon = -170 ; nino_maxlon = -120\n",
97.     "nino_index = np.zeros((ntime,1))\n",
98.     "pt_counter = 0\n",
99.     "for jlat in range(0,nlat):\n",
100.     "    if (np.mod(jlat,10)==0):\n",
101.     "        print(jlat)\n",
102.     "    for jlon in range(0,nlon):\n",
103.     "        inregion = ( (grid.geolat[jlat,jlon] >= nino_minlat and grid.geolat[jlat,jlon] <= nino_maxlat) and \n",
104.     "                     (grid.geolon[jlat,jlon] >= nino_minlon and grid.geolon[jlat,jlon] <= nino_maxlon) )\n",
105.     "        if (inregion):  \n",
106.     "            pt_counter = pt_counter + 1\n",
107.     "            for jtime in range(0,ntime):\n",
108.     "                nino_index[jtime] = nino_index[jtime] + tos_anomaly[jtime,jlat,jlon]\n",
109.     "nino_index[:] = nino_index[:] / pt_counter"
110.    ]
111.   },
112.   {
113.    "cell_type": "code",
114.    "execution_count": 24,
116.    "outputs": [],
117.    "source": [
118.     "# Calculate the covariance between the ENSO index and sea surface temperatures\n",
119.     "nino_sst_covariance = np.zeros((nlat,nlon))\n",
120.     "for jtime in range(0,ntime):\n",
121.     "    for jlat in range(0,nlat):\n",
122.     "        for jlon in range(0,nlon):\n",
123.     "            nino_sst_covariance[jlat,jlon] = (nino_sst_covariance[jlat,jlon] + \n",
124.     "                                              tos_anomaly[jtime,jlat,jlon]*nino_index[jtime])\n",
125.     "nino_sst_covariance = nino_sst_covariance/(ntime - 1)\n",
126.     "            "
127.    ]
128.   },
129.   {
130.    "cell_type": "code",
131.    "execution_count": 38,
133.    "outputs": [
134.     {
135.      "data": {
137.       "text/plain": [
138.        "<matplotlib.figure.Figure at 0x7f8f1d65fcf8>"
139.       ]
140.      },
142.      "output_type": "display_data"
143.     }
144.    ],
145.    "source": [
146.     "plt.pcolormesh(nino_sst_covariance); plt.colorbar(); plt.show()"
147.    ]
148.   },
149.   {
150.    "cell_type": "code",
151.    "execution_count": null,
153.     "collapsed": true
154.    },
155.    "outputs": [],
156.    "source": []
157.   }
158.  ],
160.   "kernelspec": {
161.    "display_name": "Python 3",
162.    "language": "python",
163.    "name": "python3"
164.   },
165.   "language_info": {
166.    "codemirror_mode": {
167.     "name": "ipython",
168.     "version": 3
169.    },
170.    "file_extension": ".py",
171.    "mimetype": "text/x-python",
172.    "name": "python",
173.    "nbconvert_exporter": "python",
174.    "pygments_lexer": "ipython3",
175.    "version": "3.6.0"
176.   }
177.  },
178.  "nbformat": 4,
179.  "nbformat_minor": 2
180. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top