{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mask a variable on pressure levels where data are below surface \n", "\n", "This tutorial shows how to mask data\n", "\n", "In some case earth system model output are interpolated to som standard pressure levels, this can lead to data being generated bellow the ground\n", "\n", "This tutorial shows how to mask these erroneous data. Data courtesy of Jerry Potter\n", "\n", "The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.\n", "\n", "[Download the Jupyter Notebook](Mask_variable_on_pressure_where_data_are_bellow_the_surface.ipynb)\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare Notebook\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "import cdms2\n", "import numpy\n", "import vcs\n", "import requests\n", "import os\n", "\n", "for filename in ['ps.nc','ta.nc']:\n", " if not os.path.exists(filename):\n", " r = requests.get(\"https://cdat.llnl.gov/cdat/sample_data/notebooks/{}\".format(filename), stream=True)\n", " with open(filename,\"wb\") as f:\n", " for chunk in r.iter_content(chunk_size=1024):\n", " if chunk: # filter local_filename keep-alive new chunks\n", " f.write(chunk)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "PS_file = cdms2.open(\"ps.nc\")\n", "TA_file = cdms2.open(\"ta.nc\")\n", "\n", "ps = PS_file(\"ps\")\n", "ta = TA_file(\"ta\")\n", "\n", "print(\"Shaped: ps {} and ta {}\".format(ps.shape,ta.shape))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Regrid surface pressure data to target grid\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ps = ps.regrid(ta.getGrid())\n", "print(\"ps new shape:\",ps.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mask Data Below Surface\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Loop through levels and mask where pressure is less than ps\n", "\n", "levels = ta.getLevel()\n", "print(\"Levels are:\", levels[:])\n", "for i,level in enumerate(levels):\n", " low = numpy.less(ps,level)\n", " ta[:,i] = numpy.ma.masked_where(low,ta[:,i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot Data\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = vcs.init(bg=True,geometry=(600,400))\n", "x.plot(ta[:,0])" ] } ], "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.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }