{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import netCDF4 as nc\n", "import scipy.interpolate as spi\n", "import scipy.sparse as sp\n", "import numpy as np\n", "from salishsea_tools import nc_tools" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def use_weights(weights,ops):\n", " \"\"\"Given a weights and operation file, return a NEMO shaped array with wind data interpolated.\n", " \n", " :arg weights: Path to weights file to be used interpolation.\n", " :type weights: str\n", " \n", " :arg ops: Path to operational file to be used in interpolation.\n", " :type ops: str\n", " \n", " :returns: NEMO-sized Numpy array containing interpolations of wind data\"\"\"\n", " # Weights\n", " #weightsfile = '/home/mdunphy/MEOPAR/NEMO-forcing/grid/weights-gem2.5-ops.nc'\n", " with nc.Dataset(weights) as f:\n", " s1 = f.variables['src01'][:]-1 # minus one for fortran-to-python indexing\n", " s2 = f.variables['src02'][:]-1\n", " s3 = f.variables['src03'][:]-1\n", " s4 = f.variables['src04'][:]-1\n", " w1 = f.variables['wgt01'][:]\n", " w2 = f.variables['wgt02'][:]\n", " w3 = f.variables['wgt03'][:]\n", " w4 = f.variables['wgt04'][:]\n", "\n", " # Operational data\n", " #opsfile='/results/forcing/atmospheric/GEM2.5/operational/ops_y2017m04d29.nc'\n", " with nc.Dataset(ops) as f:\n", " odata = f.variables['tair'][0,...] # Load a 2D field\n", "\n", "\n", " NO = odata.size # number of operational grid points\n", " NN = s1.size # number of NEMO grid points\n", "\n", " # Build matrix\n", " n = np.array([x for x in range(0,NN)])\n", " M1 = sp.csr_matrix((w1.flatten(), (n, s1.flatten())), (NN,NO))\n", " M2 = sp.csr_matrix((w2.flatten(), (n, s2.flatten())), (NN,NO))\n", " M3 = sp.csr_matrix((w3.flatten(), (n, s3.flatten())), (NN,NO))\n", " M4 = sp.csr_matrix((w4.flatten(), (n, s4.flatten())), (NN,NO))\n", " M = M1+M2+M3+M4\n", "\n", " # Interpolate by matrix multiply - quite fast\n", " ndata = M*odata.flatten()\n", "\n", " # Reshape to NEMO shaped array\n", " ndata=ndata.reshape(s1.shape)\n", " \n", " return ndata" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 285.18725039 285.21280022 285.26716202 ..., 275.34619208\n", " 275.44375196 275.5399466 ]\n", " [ 285.18840503 285.19137598 285.24226713 ..., 275.66929409\n", " 275.73676056 275.84787996]\n", " [ 285.19230722 285.17914618 285.21921623 ..., 275.96556107\n", " 276.00473081 276.13578149]\n", " ..., \n", " [ 278.04653682 277.66699859 277.30327173 ..., 273.95910365\n", " 274.00692939 273.80333675]\n", " [ 277.9115867 277.60606786 277.34447418 ..., 273.69122196 273.6991445\n", " 273.58461212]\n", " [ 278.04473504 277.78919863 277.54421604 ..., 273.458561 273.42629574\n", " 273.3687499 ]]\n" ] }, { "data": { "text/plain": [ "(898, 398)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "opsfile='/results/forcing/atmospheric/GEM2.5/operational/ops_y2017m04d29.nc'\n", "weightsfile ='/home/vdo/MEOPAR/NEMO-forcing/grid/weights-gem2.5-ops.nc'\n", "ans = use_weights(weightsfile,opsfile)\n", "print(ans)\n", "ans.shape" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import netCDF4 as nc\n", "import scipy.interpolate as spi\n", "import scipy.sparse as sp\n", "import numpy as np\n", "from salishsea_tools import nc_tools\n", "\n", "def build_matrix(weights,ops):\n", " \"\"\"Given a weights and operation file and a variable of the ops file, return matrix built with inputs as well tuple containing shape of NEMO array \n", " \n", " :arg weights: Path to weights file to be used interpolation.\n", " :type weights: str\n", " \n", " :arg ops: Path to operational file to be used in interpolation.\n", " :type ops: str\n", " \n", " :returns: SciPy Compressed Sparse Row matrix\n", " :type :py:class 'scipy.sparse.csr_matrix'\n", " \n", " :returns: shape of NEMO array\n", " :type tuple\"\"\"\n", " # Weights\n", " with nc.Dataset(weights) as f:\n", " s1 = f.variables['src01'][:]-1 # minus one for fortran-to-python indexing\n", " s2 = f.variables['src02'][:]-1\n", " s3 = f.variables['src03'][:]-1\n", " s4 = f.variables['src04'][:]-1\n", " w1 = f.variables['wgt01'][:]\n", " w2 = f.variables['wgt02'][:]\n", " w3 = f.variables['wgt03'][:]\n", " w4 = f.variables['wgt04'][:]\n", " \n", " opsfile = nc.Dataset(ops)\n", "\n", " NO = len(opsfile.dimensions['x'])*len(opsfile.dimensions['y']) # number of operational grid points\n", " NN = s1.size # number of NEMO grid points\n", " nemosize = s1.shape\n", "\n", " # Build matrix\n", " n = np.array([x for x in range(0,NN)])\n", " M1 = sp.csr_matrix((w1.flatten(), (n, s1.flatten())), (NN,NO))\n", " M2 = sp.csr_matrix((w2.flatten(), (n, s2.flatten())), (NN,NO))\n", " M3 = sp.csr_matrix((w3.flatten(), (n, s3.flatten())), (NN,NO))\n", " M4 = sp.csr_matrix((w4.flatten(), (n, s4.flatten())), (NN,NO))\n", " M = M1+M2+M3+M4\n", " return M,nemosize\n", " \n", " #######################################\n", " #new function begins here ish\n", "\n", "def use_matrix(ops,matrix,variable,nemosize,time):\n", " \"\"\"Given a opsfile, a matrix, variable name, and time index, returns NEMO-shaped array interpolated with data \n", " \n", " :arg ops: Path to operational file to be used in interpolation.\n", " :type ops: str\n", " \n", " :arg matrix: SciPy Compressed Sparse Row matrix\n", " :type :py:class 'scipy.sparse.csr_matrix'\n", " \n", " :arg variable: Specified variable in ops file.\n", " :type variable: str\n", " \n", " :arg nemosize: Shape of NEMO array\n", " :type nemosize: tuple\n", " \n", " :arg time index: time index in ops file\n", " :type time index: integer\n", " \n", " :returns: NEMO-sized Numpy array containing interpolations\n", " :type :py:class 'numpy.ndarray'\"\"\"\n", " \n", " with nc.Dataset(ops) as f:\n", " odata = f.variables[variable][time,...] # Load a 2D field\n", " \n", " # Interpolate by matrix multiply - quite fast\n", " ndata = matrix*odata.flatten()\n", "\n", " # Reshape to NEMO shaped array\n", " ndata=ndata.reshape(nemosize)\n", " \n", " return ndata" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [], "source": [ "matrix,nemosize = build_matrix(weightsfile,opsfile)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ans = use_matrix(opsfile,matrix,'tair',nemosize,5)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(nemosize)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(ans)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }