{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot Present Weather Code\n\nPlot the Present Weather Code on Precipitation Accumulation\n\nAuthor: Joe O'Brien\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nfrom arm_test_data import DATASETS\nfrom matplotlib.dates import DateFormatter, num2date\n\nimport act\n\n# Read the MET data into an xarray dataset\nfilename_met = DATASETS.fetch('gucmetM1.b1.20230301.000000.cdf')\nds = act.io.read_arm_netcdf(filename_met)\n\n# Decode the Present Weather Codes\n# Pass it to the function to decode it along with the variable name\nds = act.utils.inst_utils.decode_present_weather(ds, variable='pwd_pw_code_inst')\n\n# Calculate Precipitation Accumulation\npre_accum = act.utils.accumulate_precip(\n ds.where(ds.qc_tbrg_precip_total == 0), \"tbrg_precip_total\"\n).tbrg_precip_total_accumulated.compute()\n\n# Add the Precipitation Accum to the MET DataSet\nds['tbrg_accum'] = pre_accum\n\n# Create a matplotlib figure\nfig, ax = plt.subplots(1, 1, figsize=(10, 10))\n# Adjust subplot width\nfig.subplots_adjust(hspace=0.09)\n\n# Create ACT display\ndisplay = act.plotting.TimeSeriesDisplay(ds)\n\n# Define the Date/Time Format\ndate_form = DateFormatter(\"%H%M UTC\")\n\n# Assign the ACT display object to the matplotlib figure subplot\ndisplay.assign_to_figure_axis(fig, ax)\n# Datastream Names are needed for plotting!\ndisplay.plot('tbrg_accum', label='TBRG Accumualated Precip')\n\n# Add a day/night background\ndisplay.day_night_background()\n\n# Update axe information and formatting!\nax.set_ylabel('Precipitation Accumulation [mm]')\n# Add a title\nax.set_title('MET Tipping Bucket Rain Gauge - Crested Butte, CO')\n# Define the x-axis format\nax.xaxis.set_major_formatter(date_form)\n# Define the x-axis label\nax.set_xlabel('Time [UTC]')\n# Gridlines are helpful\nax.grid(True)\n\n# Grab the X-ticks (and convert to datetime objects) to plot location of PWD codes\nxticks = display.axes[0].get_xticks()\nndates = [num2date(x) for x in xticks]\n\n# Grab the PWD codes associated with those ticks\nncode = [\n ds['pwd_pw_code_inst_decoded'].sel(time=x.replace(tzinfo=None), method='nearest').data.tolist()\n for x in ndates\n]\npwd_code = ['\\n'.join(x.split(' ')) if len(x) > 20 else x for x in ncode]\n\n# Display these select PWD codes as vertical texts along the x-axis\n# Define the minimum y-axis tick mark for plotting\nymin = display.axes[0].get_yticks()[0]\n\n# Plot the PWD code\nfor i, key in enumerate(xticks):\n ax.text(key, ymin, pwd_code[i], rotation=90, va='center')\n\nplt.subplots_adjust(bottom=0.20)\n\nplt.show()" ] } ], "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.14.3" } }, "nbformat": 4, "nbformat_minor": 0 }