forked from 3rdparty/wrf-python
35 changed files with 1617 additions and 184 deletions
@ -0,0 +1,410 @@
@@ -0,0 +1,410 @@
|
||||
WRF Users' Workshop 2018 |
||||
========================= |
||||
|
||||
Welcome WRF-Python tutorial attendees! |
||||
|
||||
The instructions below should be completed prior to arriving at the tutorial. |
||||
There will not be enough time to do this during the tutorial. |
||||
|
||||
Prerequisites |
||||
--------------- |
||||
|
||||
This tutorial assumes that you have basic knowledge of how to type commands |
||||
in to a command terminal using your preferred operating system. You |
||||
should know some basic directory commands like *cd*, *mkdir*, *cp*, *mv*. |
||||
|
||||
Regarding Python, to understand the examples in this tutorial, you |
||||
should have some experience with Python basics. Below is a list of some |
||||
Python concepts that you will see in the examples, but don't worry if you aren't |
||||
familiar with everything. |
||||
|
||||
- Opening a Python interpreter and entering commands. |
||||
- Importing packages via the import statement. |
||||
- Familiarity with some of the basic Python types: str, list, tuple, dict, bool, float, int, None. |
||||
- Creating a list, tuple, or dict with "[ ]", "( )", "{ }" syntax (e.g. my_list = [1,2,3,4,5]). |
||||
- Accessing dict/list/tuple items with the "x[ ]" syntax (e.g. my_list_item = my_list[0]). |
||||
- Slicing str/list/tuple with the ":" syntax (e.g. my_slice = my_list[1:3]). |
||||
- Using object methods and attributes with the "x.y" syntax (e.g. my_list.append(6)). |
||||
- Calling functions (e.g. result = some_function(x, y)) |
||||
- Familiarity with numpy would be helpful, as only a very brief introduction |
||||
is provided. |
||||
- Familiarity with matplotlib would be helpful, as only a very brief |
||||
introduction is provided. |
||||
|
||||
If you are completely new to Python, that shouldn't be a problem, since |
||||
most of the examples consist of basic container types and function calls. It |
||||
would be helpful to look at some introductory material before arriving at the |
||||
tutorial. If you've programmed before, picking up Python isn't too difficult. |
||||
|
||||
Here are some links: |
||||
|
||||
https://www.learnpython.org/ |
||||
|
||||
https://developers.google.com/edu/python/ |
||||
|
||||
|
||||
Step 1: Open a Command Terminal |
||||
-------------------------------- |
||||
|
||||
To begin, you will first need to know how to open a command line terminal for |
||||
your operating system. |
||||
|
||||
For Windows: |
||||
|
||||
.. code-block:: none |
||||
|
||||
WINDOWS + r |
||||
type cmd in the run window |
||||
|
||||
For Mac: |
||||
|
||||
.. code-block:: none |
||||
|
||||
Finder -> Applications -> Utilities -> Terminal |
||||
|
||||
For Linux: |
||||
|
||||
.. code-block:: none |
||||
|
||||
Try one of the following: |
||||
|
||||
CTRL + ALT + T |
||||
CTRL + ALT + F2 |
||||
|
||||
|
||||
Step 2: Download Miniconda |
||||
---------------------------- |
||||
|
||||
For this tutorial, you will need to download and install Miniconda. We are |
||||
going to use Python 2.7, but it will also work with Python 3.5+. |
||||
|
||||
Please use the appropriate link below to download Miniconda for your operating |
||||
system. |
||||
|
||||
.. note:: |
||||
|
||||
64-bit OS recommended |
||||
|
||||
`Win64 <https://repo.continuum.io/miniconda/Miniconda2-latest-Windows-x86_64.exe>`_ |
||||
|
||||
`Mac <https://repo.continuum.io/miniconda/Miniconda2-latest-MacOSX-x86_64.sh>`_ |
||||
|
||||
`Linux <https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh>`_ |
||||
|
||||
For more information, see: https://conda.io/miniconda.html |
||||
|
||||
.. note:: |
||||
|
||||
**What is Miniconda?** |
||||
|
||||
If you have used the Anaconda distribution for Python before, then you will be |
||||
familiar with Miniconda. The Anaconda Python distribution includes numerous |
||||
scientific packages out of the box, which can be difficult for users to build and |
||||
install. More importantly, Anaconda includes the conda package manager. |
||||
|
||||
The conda package manager is a utility (similar to yum or apt-get) that installs |
||||
packages from a repository of pre-compiled Python packages. These repositories |
||||
are called channels. Conda makes it easy for Python users to install and |
||||
uninstall packages, and also can be used to create isolated Python environments |
||||
(more on that later). |
||||
|
||||
Miniconda is a bare bones implementation of Anaconda and only includes the |
||||
conda package manager. Since we are going to use the conda-forge channel to |
||||
install our scientific packages, Miniconda avoids any complications between |
||||
packages provided by Anaconda and conda-forge. |
||||
|
||||
|
||||
Step 3: Install Miniconda |
||||
---------------------------- |
||||
|
||||
Windows: |
||||
|
||||
1. Browse to the directory where you downloaded Miniconda2-latest-Windows-x86_64.exe. |
||||
|
||||
2. Double click on Miniconda2-latest-Windows-x86_64.exe. |
||||
|
||||
3. Follow the instructions. |
||||
|
||||
4. Restart your command terminal. |
||||
|
||||
Mac and Linux: |
||||
|
||||
For Mac and Linux, the installer is a bash script. |
||||
|
||||
1. Using a terminal, you need to execute the bash shell script that you downloaded by |
||||
doing:: |
||||
|
||||
bash /path/to/Miniconda2-latest-MacOSX-x86_64.sh [Mac] |
||||
|
||||
bash /path/to/Miniconda2-latest-Linux-x86_64.sh [Linux] |
||||
|
||||
2. Follow the instructions. |
||||
|
||||
3. At the end of the installation, it will ask if you want to add the |
||||
miniconda2 path to your bash environment. If you are unsure what to do, |
||||
you should say "yes". If you say "no", we're going to assume you know |
||||
what you are doing. |
||||
|
||||
If you said "yes", then once you restart your shell, the miniconda2 Python |
||||
will be found instead of the system Python when you type the "python" |
||||
command. If you want to undo this later, then you can edit |
||||
either ~/.bash_profile or ~/.bashrc (depending on OS used) and |
||||
comment out the line that looks similar to:: |
||||
|
||||
# added by Miniconda2 4.1.11 installer |
||||
export PATH="/path/to/miniconda2/bin:$PATH" |
||||
|
||||
4. Restart your command terminal. |
||||
|
||||
5. [Linux and Mac Users Only] Miniconda only works with bash. If bash is |
||||
not your default shell, then you need to activate the bash shell by typing |
||||
the following in to your command terminal:: |
||||
|
||||
bash |
||||
|
||||
6. Verify that your system is using the correct Python interpreter by typing |
||||
the following in to your command terminal:: |
||||
|
||||
which python |
||||
|
||||
You should see the path to your miniconda installation. If not, see the |
||||
note below. |
||||
|
||||
.. note:: |
||||
|
||||
If you have already installed another Python distribution, like Enthought |
||||
Canopy, you will need to comment out any PATH entries for that distribution |
||||
in your .bashrc or .bash_profile. Otherwise, your shell environment may |
||||
pick to wrong Python installation. |
||||
|
||||
If bash is not your default shell type, and the PATH variable has been |
||||
set in .bash_profile by the miniconda installer, try executing |
||||
"bash -l" instead of the "bash" command in step 5. |
||||
|
||||
|
||||
Step 4: Set Up the Conda Environment |
||||
-------------------------------------- |
||||
|
||||
If you are new to the conda package manager, one of the nice features of conda |
||||
is that you can create isolated Python environments that prevent package |
||||
incompatibilities. This is similar to the *virtualenv* package that some |
||||
Python users may be familiar with. However, conda is not compatible with |
||||
virtualenv, so only use conda environments when working with conda. |
||||
|
||||
The name of our conda environment for this tutorial is: **tutorial_2018**. |
||||
|
||||
Follow the instructions below to create the tutorial_2018 environment. |
||||
|
||||
1. Open a command terminal if you haven't done so. |
||||
|
||||
2. [Linux and Mac Users Only] The conda package manager only works with bash, |
||||
so if bash is not your current shell, type:: |
||||
|
||||
bash |
||||
|
||||
3. Add the conda-forge channel to your conda package manager. |
||||
|
||||
Type or copy the command below in to your command terminal. You should |
||||
run this command even if you have already done it in the past. |
||||
This will ensure that conda-forge is set as the highest priority channel. |
||||
|
||||
:: |
||||
|
||||
conda config --add channels conda-forge |
||||
|
||||
.. note:: |
||||
|
||||
Conda-forge is a community driven collection of packages that are |
||||
continually tested to ensure compatibility. We highly recommend using |
||||
conda-forge when working with conda. See https://conda-forge.github.io/ |
||||
for more details on this excellent project. |
||||
|
||||
4. Create the conda environment for the tutorial. |
||||
|
||||
Type or copy this command in to your command terminal:: |
||||
|
||||
conda create -n tutorial_2018 python=2.7 matplotlib cartopy netcdf4 jupyter git ffmpeg wrf-python |
||||
|
||||
Type "y" when prompted. It will take several minutes to install everything. |
||||
|
||||
This command creates an isolated Python environment named *tutorial_2018*, and installs |
||||
the python interpreter, matplotlib, cartopy, netcdf4, jupyter, git, ffmpeg, and wrf-python |
||||
packages. |
||||
|
||||
.. note:: |
||||
|
||||
When the installation completes, your command terminal might post a message similar to: |
||||
|
||||
.. code-block:: none |
||||
|
||||
If this is your first install of dbus, automatically load on login with: |
||||
|
||||
mkdir -p ~/Library/LaunchAgents |
||||
cp /path/to/miniconda2/envs/tutorial_test/org.freedesktop.dbus-session.plist ~/Library/LaunchAgents/ |
||||
launchctl load -w ~/Library/LaunchAgents/org.freedesktop.dbus-session.plist |
||||
|
||||
This is indicating that the dbus package can be set up to automatically load on login. You |
||||
can either ignore this message or type in the commands as indicated on your command terminal. |
||||
The tutorial should work fine in either case. |
||||
|
||||
5. Activate the conda environment. |
||||
|
||||
To activate the tutorial_2018 Python environment, type the following |
||||
in to the command terminal: |
||||
|
||||
For Linux and Mac (using bash):: |
||||
|
||||
source activate tutorial_2018 |
||||
|
||||
For Windows:: |
||||
|
||||
activate tutorial_2018 |
||||
|
||||
You should see (tutorial_2018) on your command prompt. |
||||
|
||||
To deactivate your conda environment, type the following in to the |
||||
command terminal: |
||||
|
||||
For Linux and Mac:: |
||||
|
||||
source deactivate |
||||
|
||||
For Windows:: |
||||
|
||||
deactivate tutorial_2018 |
||||
|
||||
|
||||
Step 5: Download the Student Workbook |
||||
--------------------------------------- |
||||
|
||||
The student workbook for the tutorial is available on GitHub. The tutorial_2018 |
||||
conda environment includes the git application needed to download the repository. |
||||
|
||||
These instructions download the tutorial in to your home directory. If you want |
||||
to place the tutorial in to another directory, we're going to assume you know |
||||
how to do this yourself. |
||||
|
||||
To download the student workbook, follow these instructions: |
||||
|
||||
1. Activate the tutorial_2018 conda environment following the instructions |
||||
in the previous step (*source activate tutorial_2018* or |
||||
*activate tutorial_2018*). |
||||
|
||||
2. Change your working directory to the home directory by typing the |
||||
following command in to the command terminal: |
||||
|
||||
For Linux and Mac:: |
||||
|
||||
cd ~ |
||||
|
||||
For Windows:: |
||||
|
||||
cd %HOMEPATH% |
||||
|
||||
3. Download the git repository for the tutorial by typing the following |
||||
in to the command terminal:: |
||||
|
||||
git clone https://github.com/NCAR/wrf_python_tutorial.git |
||||
|
||||
4. There may be additional changes to the tutorial after you have downloaded |
||||
it. To pull down the latest changes, type the following in to the |
||||
command terminal: |
||||
|
||||
For Linux and Mac:: |
||||
|
||||
source activate tutorial_2018 |
||||
|
||||
cd ~/wrf_python_tutorial/wrf_workshop_2018 |
||||
|
||||
git pull |
||||
|
||||
For Windows:: |
||||
|
||||
activate tutorial_2018 |
||||
|
||||
cd %HOMEPATH%\wrf_python_tutorial\wrf_workshop_2018 |
||||
|
||||
git pull |
||||
|
||||
.. note:: |
||||
|
||||
If you try the "git pull" command and it returns an error indicating |
||||
that you have made changes to the workbook, this is probably because |
||||
you ran the workbook and it contains the cell output. To fix this, |
||||
first do a checkout of the workbook, then do the pull. |
||||
|
||||
.. code-block:: none |
||||
|
||||
git checkout -- . |
||||
git pull |
||||
|
||||
|
||||
Step 6: Verify Your Environment |
||||
---------------------------------- |
||||
|
||||
Verifying that your environment is correct involves importing a few |
||||
packages and checking for errors (you may see some warnings for matplotlib |
||||
or xarray, but you can safely ignore these). |
||||
|
||||
1. Activate the tutorial_2018 conda environment if it isn't already active |
||||
(see instructions above). |
||||
|
||||
2. Open a python terminal by typing the following in to the command |
||||
terminal:: |
||||
|
||||
python |
||||
|
||||
3. Now type the following in to the Python interpreter:: |
||||
|
||||
>>> import netCDF4 |
||||
>>> import matplotlib |
||||
>>> import xarray |
||||
>>> import wrf |
||||
|
||||
4. You can exit the Python interpreter using **CTRL + D** |
||||
|
||||
|
||||
Step 7: Obtain WRF Output Files |
||||
---------------------------------- |
||||
|
||||
For this tutorial, we strongly recommend that you use your own WRF output files. |
||||
The tutorial includes an easy way to point to your own data files. The WRF |
||||
output files should all be from the same WRF run and use the same domain. |
||||
If your files are located on another system (e.g. yellowstone), then copy 2 or |
||||
3 of these files to your local computer prior to the tutorial. |
||||
|
||||
If you do not have any of your own WRF output files, then you can download the |
||||
instructor data files from a link that should have been provided to you in an |
||||
email prior to the tutorial. |
||||
|
||||
If you are using the link provided in the email for your data, you can follow |
||||
the instructions below to place your data in the default location for your |
||||
workbook. |
||||
|
||||
1. The link in the email should take you to a location on an Amazon cloud |
||||
drive. |
||||
|
||||
2. If you hover your mouse over the wrf_tutorial_data.zip file, you'll see |
||||
an empty check box appear next to the file name. Click this check |
||||
box. |
||||
|
||||
3. At the bottom of the screen, you'll see a Download button next to a |
||||
cloud icon. Click this button to start the download. |
||||
|
||||
4. The download was most likely placed in to your ~/Downloads folder |
||||
[%HOMEPATH%\\Downloads for Windows]. Using your preferred method of choice |
||||
for unzipping files, unzip this file in to your home directory. Your data |
||||
should now be in ~/wrf_tutorial_data |
||||
[%HOMEPATH%\\wrf_tutorial_data for Windows]. |
||||
|
||||
5. Verify that you have three WRF output files in that directory. |
||||
|
||||
|
||||
Getting Help |
||||
---------------- |
||||
|
||||
If you experience problems during this installation, please send a question |
||||
to the :ref:`google-group` support mailing list. |
||||
|
||||
|
||||
We look forward to seeing you at the tutorial! |
@ -1,2 +1,2 @@
@@ -1,2 +1,2 @@
|
||||
__version__ = "1.1.2" |
||||
__version__ = "1.1.3" |
||||
|
||||
|
Binary file not shown.
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
from netCDF4 import Dataset |
||||
import matplotlib.pyplot as plt |
||||
from matplotlib.cm import get_cmap |
||||
import cartopy.crs as crs |
||||
from cartopy.feature import NaturalEarthFeature |
||||
|
||||
from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords |
||||
|
||||
# Open the NetCDF file |
||||
ncfile = Dataset("/Users/ladwig/Documents/wrf_files/problem_files/cfrac_bug/wrfout_d02_1987-10-01_00:00:00") |
||||
|
||||
# Get the sea level pressure |
||||
ctt = getvar(ncfile, "ctt") |
||||
|
||||
# Get the latitude and longitude points |
||||
lats, lons = latlon_coords(ctt) |
||||
|
||||
# Get the cartopy mapping object |
||||
cart_proj = get_cartopy(ctt) |
||||
|
||||
# Create a figure |
||||
fig = plt.figure(figsize=(12,9)) |
||||
# Set the GeoAxes to the projection used by WRF |
||||
ax = plt.axes(projection=cart_proj) |
||||
|
||||
# Download and add the states and coastlines |
||||
states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none', |
||||
name='admin_1_states_provinces_shp') |
||||
ax.add_feature(states, linewidth=.5) |
||||
ax.coastlines('50m', linewidth=0.8) |
||||
|
||||
# Make the contour outlines and filled contours for the smoothed sea level pressure. |
||||
plt.contour(to_np(lons), to_np(lats), to_np(ctt), 10, colors="black", |
||||
transform=crs.PlateCarree()) |
||||
plt.contourf(to_np(lons), to_np(lats), to_np(ctt), 10, transform=crs.PlateCarree(), |
||||
cmap=get_cmap("jet")) |
||||
|
||||
# Add a color bar |
||||
plt.colorbar(ax=ax, shrink=.62) |
||||
|
||||
# Set the map limits. Not really necessary, but used for demonstration. |
||||
ax.set_xlim(cartopy_xlim(ctt)) |
||||
ax.set_ylim(cartopy_ylim(ctt)) |
||||
|
||||
# Add the gridlines |
||||
ax.gridlines(color="black", linestyle="dotted") |
||||
|
||||
plt.title("Cloud Top Temperature") |
||||
|
||||
plt.show() |
@ -0,0 +1,214 @@
@@ -0,0 +1,214 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"ctt = getvar(ncfile, \"ctt\", fill_nocloud=False)\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(-80, 20, 5)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(ctt), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Top Temperature (degC)\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"ctt = getvar(ncfile, \"ctt\", fill_nocloud=True, opt_thresh=1.0)\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(-80, 20, 5)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(ctt), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Top Temperature (degC)\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"cfrac = getvar(ncfile, \"cfrac\")[2, :]\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(0, 1.1, .1)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(cfrac), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys_r\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Fraction\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
} |
||||
], |
||||
"metadata": { |
||||
"kernelspec": { |
||||
"display_name": "Python 2", |
||||
"language": "python", |
||||
"name": "python2" |
||||
}, |
||||
"language_info": { |
||||
"codemirror_mode": { |
||||
"name": "ipython", |
||||
"version": 2 |
||||
}, |
||||
"file_extension": ".py", |
||||
"mimetype": "text/x-python", |
||||
"name": "python", |
||||
"nbconvert_exporter": "python", |
||||
"pygments_lexer": "ipython2", |
||||
"version": "2.7.14" |
||||
} |
||||
}, |
||||
"nbformat": 4, |
||||
"nbformat_minor": 2 |
||||
} |
@ -0,0 +1,214 @@
@@ -0,0 +1,214 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"ctt = getvar(ncfile, \"ctt\")\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(-80, 20, 5)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(ctt), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Top Temperature (degC)\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"ctt = getvar(ncfile, \"ctt\")\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(-80, 20, 5)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(ctt), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Top Temperature (degC)\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"%matplotlib inline\n", |
||||
"import numpy as np\n", |
||||
"from netCDF4 import Dataset\n", |
||||
"import matplotlib.pyplot as plt\n", |
||||
"from matplotlib.cm import get_cmap\n", |
||||
"import cartopy.crs as crs\n", |
||||
"from cartopy.feature import NaturalEarthFeature\n", |
||||
"\n", |
||||
"from wrf import to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords\n", |
||||
"\n", |
||||
"# Open the NetCDF file\n", |
||||
"ncfile = Dataset(\"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/wrfout_d01_2005-08-28_12:00:00\")\n", |
||||
"\n", |
||||
"# Get the sea level pressure\n", |
||||
"cfrac = getvar(ncfile, \"cfrac\")[2, :]\n", |
||||
"slp = getvar(ncfile, \"slp\")\n", |
||||
"\n", |
||||
"\n", |
||||
"# Get the latitude and longitude points\n", |
||||
"lats, lons = latlon_coords(ctt)\n", |
||||
"\n", |
||||
"# Get the cartopy mapping object\n", |
||||
"cart_proj = get_cartopy(ctt)\n", |
||||
"\n", |
||||
"# Create a figure\n", |
||||
"fig = plt.figure(figsize=(12,9))\n", |
||||
"# Set the GeoAxes to the projection used by WRF\n", |
||||
"ax = plt.axes(projection=cart_proj)\n", |
||||
"\n", |
||||
"# Download and add the states and coastlines\n", |
||||
"states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", |
||||
" name='admin_1_states_provinces_shp')\n", |
||||
"ax.add_feature(states, linewidth=.5)\n", |
||||
"ax.coastlines('50m', linewidth=0.8)\n", |
||||
"\n", |
||||
"# Make the contour outlines and filled contours for the smoothed sea level pressure.\n", |
||||
"levels = np.arange(0, 1.1, .1)\n", |
||||
"plt.contour(to_np(lons), to_np(lats), to_np(slp), 10, colors=\"black\",\n", |
||||
" transform=crs.PlateCarree())\n", |
||||
"plt.contourf(to_np(lons), to_np(lats), to_np(cfrac), levels=levels, transform=crs.PlateCarree(),\n", |
||||
" cmap=get_cmap(\"Greys_r\"))\n", |
||||
"\n", |
||||
"# Add a color bar\n", |
||||
"plt.colorbar(ax=ax, shrink=.88)\n", |
||||
"\n", |
||||
"# Set the map limits. Not really necessary, but used for demonstration.\n", |
||||
"ax.set_xlim(cartopy_xlim(ctt))\n", |
||||
"ax.set_ylim(cartopy_ylim(ctt))\n", |
||||
"\n", |
||||
"# Add the gridlines\n", |
||||
"ax.gridlines(color=\"black\", linestyle=\"dotted\")\n", |
||||
"\n", |
||||
"plt.title(\"Cloud Fraction\")\n", |
||||
"\n", |
||||
"plt.show()" |
||||
] |
||||
} |
||||
], |
||||
"metadata": { |
||||
"kernelspec": { |
||||
"display_name": "Python 2", |
||||
"language": "python", |
||||
"name": "python2" |
||||
}, |
||||
"language_info": { |
||||
"codemirror_mode": { |
||||
"name": "ipython", |
||||
"version": 2 |
||||
}, |
||||
"file_extension": ".py", |
||||
"mimetype": "text/x-python", |
||||
"name": "python", |
||||
"nbconvert_exporter": "python", |
||||
"pygments_lexer": "ipython2", |
||||
"version": "2.7.14" |
||||
} |
||||
}, |
||||
"nbformat": 4, |
||||
"nbformat_minor": 2 |
||||
} |
@ -0,0 +1,181 @@
@@ -0,0 +1,181 @@
|
||||
import unittest as ut |
||||
import numpy.testing as nt |
||||
import numpy as np |
||||
import numpy.ma as ma |
||||
import os |
||||
import sys |
||||
import subprocess |
||||
|
||||
from wrf import (getvar, interplevel, interpline, vertcross, vinterp, |
||||
disable_xarray, xarray_enabled, to_np, |
||||
xy_to_ll, ll_to_xy, xy_to_ll_proj, ll_to_xy_proj, |
||||
extract_global_attrs, viewitems, CoordPair, ll_points) |
||||
from wrf.util import is_multi_file |
||||
|
||||
IN_DIR = "/Users/ladwig/Documents/wrf_files/wrf_vortex_multi" |
||||
TEST_FILES = [os.path.join(IN_DIR, "wrfout_d02_2005-08-28_00:00:00"), |
||||
os.path.join(IN_DIR, "wrfout_d02_2005-08-28_12:00:00"), |
||||
os.path.join(IN_DIR, "wrfout_d02_2005-08-29_00:00:00")] |
||||
|
||||
def wrfin_gen(wrf_in): |
||||
for x in wrf_in: |
||||
yield x |
||||
|
||||
|
||||
class wrf_in_iter_class(object): |
||||
def __init__(self, wrf_in): |
||||
self._wrf_in = wrf_in |
||||
self._total = len(wrf_in) |
||||
self._i = 0 |
||||
|
||||
def __iter__(self): |
||||
return self |
||||
|
||||
def next(self): |
||||
if self._i >= self._total: |
||||
raise StopIteration |
||||
else: |
||||
result = self._wrf_in[self._i] |
||||
self._i += 1 |
||||
return result |
||||
|
||||
# Python 3 |
||||
def __next__(self): |
||||
return self.next() |
||||
|
||||
|
||||
def make_test(varname, wrf_in): |
||||
def test(self): |
||||
x = getvar(wrf_in, varname, 0) |
||||
xa = getvar(wrf_in, varname, None) |
||||
|
||||
return test |
||||
|
||||
def make_interp_test(varname, wrf_in): |
||||
def test(self): |
||||
|
||||
# Only testing vinterp since other interpolation just use variables |
||||
if (varname == "vinterp"): |
||||
for timeidx in (0, None): |
||||
eth = getvar(wrf_in, "eth", timeidx=timeidx) |
||||
interp_levels = [850,500,5] |
||||
field = vinterp(wrf_in, |
||||
field=eth, |
||||
vert_coord="pressure", |
||||
interp_levels=interp_levels, |
||||
extrapolate=True, |
||||
field_type="theta-e", |
||||
timeidx=timeidx, |
||||
log_p=True) |
||||
else: |
||||
pass |
||||
|
||||
|
||||
return test |
||||
|
||||
|
||||
def make_latlon_test(testid, wrf_in): |
||||
def test(self): |
||||
if testid == "xy_out": |
||||
# Lats/Lons taken from NCL script, just hard-coding for now |
||||
lats = [-55, -60, -65] |
||||
lons = [25, 30, 35] |
||||
xy = ll_to_xy(wrf_in, lats, lons, timeidx=0) |
||||
xy = ll_to_xy(wrf_in, lats, lons, timeidx=None) |
||||
else: |
||||
i_s = np.asarray([10, 100, 150], int) |
||||
j_s = np.asarray([10, 100, 150], int) |
||||
ll = xy_to_ll(wrf_in, i_s, j_s, timeidx=0) |
||||
ll = xy_to_ll(wrf_in, i_s, j_s, timeidx=None) |
||||
|
||||
return test |
||||
|
||||
|
||||
class WRFVarsTest(ut.TestCase): |
||||
longMessage = True |
||||
|
||||
class WRFInterpTest(ut.TestCase): |
||||
longMessage = True |
||||
|
||||
class WRFLatLonTest(ut.TestCase): |
||||
longMessage = True |
||||
|
||||
if __name__ == "__main__": |
||||
from wrf import (omp_set_num_threads, omp_set_schedule, omp_get_schedule, |
||||
omp_set_dynamic, omp_get_num_procs, OMP_SCHED_STATIC) |
||||
omp_set_num_threads(omp_get_num_procs()-1) |
||||
omp_set_schedule(OMP_SCHED_STATIC, 0) |
||||
omp_set_dynamic(False) |
||||
|
||||
ignore_vars = [] # Not testable yet |
||||
wrf_vars = ["avo", "eth", "cape_2d", "cape_3d", "ctt", "dbz", "mdbz", |
||||
"geopt", "helicity", "lat", "lon", "omg", "p", "pressure", |
||||
"pvo", "pw", "rh2", "rh", "slp", "ter", "td2", "td", "tc", |
||||
"theta", "tk", "tv", "twb", "updraft_helicity", "ua", "va", |
||||
"wa", "uvmet10", "uvmet", "z", "cfrac", "zstag", "geopt_stag"] |
||||
interp_methods = ["interplevel", "vertcross", "interpline", "vinterp"] |
||||
latlon_tests = ["xy_out", "ll_out"] |
||||
|
||||
|
||||
for nc_lib in ("netcdf4", "pynio", "scipy"): |
||||
if nc_lib == "netcdf4": |
||||
try: |
||||
from netCDF4 import Dataset |
||||
except ImportError: |
||||
print ("netcdf4-python not installed") |
||||
continue |
||||
else: |
||||
test_in = [Dataset(x) for x in TEST_FILES] |
||||
elif nc_lib == "pynio": |
||||
try: |
||||
from Nio import open_file |
||||
except ImportError: |
||||
print ("PyNIO not installed") |
||||
continue |
||||
else: |
||||
test_in = [open_file(x +".nc", "r") for x in TEST_FILES] |
||||
elif nc_lib == "scipy": |
||||
try: |
||||
from scipy.io.netcdf import netcdf_file |
||||
except ImportError: |
||||
print ("scipy.io.netcdf not installed") |
||||
else: |
||||
test_in = [netcdf_file(x, mmap=False) for x in TEST_FILES] |
||||
|
||||
input0 = test_in[0] |
||||
input1 = test_in |
||||
input2 = tuple(input1) |
||||
input3 = wrfin_gen(test_in) |
||||
input4 = wrf_in_iter_class(test_in) |
||||
input5 = {"A" : input1, |
||||
"B" : input2} |
||||
input6 = {"A" : {"AA" : input1}, |
||||
"B" : {"BB" : input2}} |
||||
|
||||
for i,input in enumerate((input0, input1, input2, |
||||
input3, input4, input5, input6)): |
||||
for var in wrf_vars: |
||||
if var in ignore_vars: |
||||
continue |
||||
test_func1 = make_test(var, input) |
||||
|
||||
setattr(WRFVarsTest, "test_{0}_input{1}_{2}".format(nc_lib, |
||||
i, var), |
||||
test_func1) |
||||
|
||||
|
||||
for method in interp_methods: |
||||
test_interp_func1 = make_interp_test(method, input) |
||||
setattr(WRFInterpTest, |
||||
"test_{0}_input{1}_{2}".format(nc_lib, |
||||
i, method), |
||||
test_interp_func1) |
||||
|
||||
for testid in latlon_tests: |
||||
test_ll_func = make_latlon_test(testid, input) |
||||
test_name = "test_{0}_input{1}_{2}".format(nc_lib, i, testid) |
||||
setattr(WRFLatLonTest, test_name, test_ll_func) |
||||
|
||||
ut.main() |
||||
|
||||
|
Loading…
Reference in new issue