.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/mpe/run_mpe_01_no2.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_mpe_run_mpe_01_no2.py: Compare Model to Hourly Observations ==================================== Evaluate CAMx by comparing to AirNow or AQS hourly observations. The basic steps are: 1. Open CAMx file and identify space/time domain, 2. Download hourly observations for that domain to a CSV file. 3. Add CAMx hourly model data to the observations CSV file.. 4. Plot a time series *Reminder*: You must have already activated your python environment. .. GENERATED FROM PYTHON SOURCE LINES 18-20 Configuration ''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 20-37 .. code-block:: Python # Define Analysis obssrc = 'airnow' # or aqs obsspc = 'no2' # or ozone, co, pm25, ... modsrc = 'CAMx' # Or CMAQ modspc = 'NO2' # or O3, CO, PM25, ... # Set input files dates = ['20160610', '20160611'] avrgtmpl = '../../camx/outputs/CAMx.v7.32.36.12.{}.avrg.grd02.nc' # placeholder {} for date # Outputs outstem = f'outputs/{obssrc}.{obsspc}_and_CAMx.v7.32.36.12.avrg.grd02' pairedpath = outstem + '.csv' statspath = outstem + '_stats.csv' figpath = outstem + '_ts.png' .. GENERATED FROM PYTHON SOURCE LINES 38-40 Imports and Folders ''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. code-block:: Python import pandas as pd import os import pyrsig os.makedirs('outputs', exist_ok=True) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Query Observations for each model file '''''''''''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 50-64 .. code-block:: Python obskey = f'{obssrc}.{obsspc}' # must exist in RSIG modkey = f'{modsrc}{modspc}' dfs = [] for datestr in dates: ds = pyrsig.open_ioapi(avrgtmpl.format(datestr)) df = pyrsig.cmaq.pair_rsigcmaq(ds, modspc, obskey, prefix=modsrc, workdir='outputs') df[modkey] *= 1000 df.rename(columns={obsspc: obskey}, inplace=True) dfs.append(df) df = pd.concat(dfs) df.to_csv(pairedpath) .. rst-class:: sphx-glr-script-out .. code-block:: none Using cached: outputs/airnow.no2_2016-06-10T000000Z_2016-06-10T235959Z.csv.gz Using cached: outputs/airnow.no2_2016-06-11T000000Z_2016-06-11T235959Z.csv.gz .. GENERATED FROM PYTHON SOURCE LINES 65-67 Calculate Statistics '''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 67-75 .. code-block:: Python keys = [obskey, modkey] statsdf = pyrsig.utils.quickstats(df[keys], obskey) # Print them for the user to review. print(statsdf) # Save stats to disk statsdf.to_csv(statspath) .. rst-class:: sphx-glr-script-out .. code-block:: none airnow.no2 CAMxNO2 count 3357.000000 3357.000000 mean 8.901991 5.682308 std 9.040611 7.251954 min 0.000000 0.018669 25% 2.600000 1.458120 50% 5.800000 3.107684 75% 12.000000 6.906990 max 55.700000 55.065445 r 1.000000 0.567699 mb 0.000000 -3.219683 nmb 0.000000 -0.361681 fmb 0.000000 -0.441527 ioa 1.000000 0.715514 .. GENERATED FROM PYTHON SOURCE LINES 76-78 Make a Plot ''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 78-85 .. code-block:: Python gb = df.groupby('time')[keys] ax = gb.median().plot(color=['k', 'r'], linewidth=2, zorder=2) gb.quantile(.75).plot(ax=ax, color=['k', 'r'], linestyle='--', legend=False, zorder=1) gb.quantile(.25).plot(ax=ax, color=['k', 'r'], linestyle='--', legend=False, zorder=1) ax.figure.savefig(figpath) .. image-sg:: /auto_examples/mpe/images/sphx_glr_run_mpe_01_no2_001.png :alt: run mpe 01 no2 :srcset: /auto_examples/mpe/images/sphx_glr_run_mpe_01_no2_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-90 Extra Credit '''''''''''' 1. AirNow uses the "airnow" prefix and AQS uses "aqs". Can you change the script to evaluate no2 from AQS? Modify 2. Instead of no2, can you change the script to evaluate carbon monoxide? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.537 seconds) .. _sphx_glr_download_auto_examples_mpe_run_mpe_01_no2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_mpe_01_no2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_mpe_01_no2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: run_mpe_01_no2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_