{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Transformations and QC\n\nBuilt-in transformations using xarray are not\nquality-control aware. This example shows how\na user should apply QC prior to performing transformations.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nfrom arm_test_data import DATASETS\n\nimport act\n\n# Read in some sample MFRSR data and clean up the QC\nfilename_mfrsr = DATASETS.fetch('sgpmfrsr7nchE11.b1.20210329.070000.nc')\nds = act.io.arm.read_arm_netcdf(filename_mfrsr, cleanup_qc=True)\n\n# Let's resample the data to 5 minutes and take the mean\nds_5min = ds.resample(time='5min').mean()\n\nvariable = 'diffuse_hemisp_narrowband_filter4'\n\n# Let's look at a before and after of one of the qc variables\nprint('With no QC applied before transformation')\nprint('Before (10 1-minute samples): ', ds['qc_' + variable].values[0:10])\nprint('After: (2 5-minute averages)', ds_5min['qc_' + variable].values[0:2])\n\n# That new QC variable does not make sense at all and should be an int\n# What needs to happen is that we apply QC as the user see's fit to all\n# variables before the transformations take place.\nprint('\\nAverage of ', variable, ' before and after applying QC')\nprint('Note the change in the second value')\nprint('Before (2 5 - minute averages): ', ds[variable].values[0:2])\n\nds.qcfilter.datafilter(rm_assessments=['Bad', 'Indeterminate'])\nds_5minb = ds.resample(time='5min').mean()\n\n# Print out the corresponding variable values\nprint('After: (2 5 - minute averages)', ds_5minb[variable].values[0:2])\n\n## Plot up the variable and qc block plot\ndisplay = act.plotting.TimeSeriesDisplay(\n {'Original': ds, 'Average': ds_5min, 'Average_QCd': ds_5minb},\n figsize=(15, 10),\n subplot_shape=(2,),\n)\ndisplay.plot(variable, dsname='Original', subplot_index=(0,), day_night_background=True)\ndisplay.plot(\n variable, dsname='Average', subplot_index=(1,), day_night_background=True, label='No QC'\n)\ndisplay.plot(\n variable, dsname='Average_QCd', subplot_index=(1,), day_night_background=True, label='QC'\n)\nplt.legend()\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 }