.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/gridemiss/run_gridemiss_01_perturb.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_gridemiss_run_gridemiss_01_perturb.py: Domain-wide Gridded Emission Scaling ==================================== Apply a constant factor to a subset of emission variables in a file. The basic steps are: 1. Copy a file. 2. Select species to scale NOx species. 3. Scale by a multiplicative factor. 4. Plot the old and new result. *Reminder*: You must have already activated your python environment. .. GENERATED FROM PYTHON SOURCE LINES 18-20 Configuration ''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 20-34 .. code-block:: Python # date to process date = '20160610' # file to copy oldpath = f'../../camx/emiss/camx_area.mobile.{date}.36km.nc' # new file to create newpath = f'outputs/camx_area_2x.mobile.{date}.36km.nc' # species to scale scalekeys = ['NO', 'NO2', 'HONO'] # list of strings or set to 'all' # factor to scale by factor = 2. # path for figure comparing figpath = 'outputs/scaled_gridemiss_scalewholedomain.png' .. GENERATED FROM PYTHON SOURCE LINES 35-37 Imports and File Prep ''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 37-45 .. code-block:: Python import netCDF4 import shutil import os os.makedirs('outputs', exist_ok=True) shutil.copyfile(oldpath, newpath) .. rst-class:: sphx-glr-script-out .. code-block:: none 'outputs/camx_area_2x.mobile.20160610.36km.nc' .. GENERATED FROM PYTHON SOURCE LINES 46-50 Open Files '''''''''' - Open the existing file in read-only mode - Open the new file in editable append mode .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python infile = netCDF4.Dataset(oldpath, mode='r') outfile = netCDF4.Dataset(newpath, mode='a') .. GENERATED FROM PYTHON SOURCE LINES 55-58 Select Emission Variables to Scale '''''''''''''''''''''''''''''''''' - if scalekeys is all, identify all emission variables by unit .. GENERATED FROM PYTHON SOURCE LINES 58-69 .. code-block:: Python if scalekeys == 'all': scalekeys = [ k for k, v in outfile.variables.items() if v.units.strip() in ('mol hr-1', 'g hr-1') ] noscalekeys = sorted(set(outfile.variables).difference(scalekeys)) print('INFO:: no scale', noscalekeys) print('INFO:: to scale', scalekeys) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO:: no scale ['ACET', 'ALD2', 'ALDX', 'BENZ', 'BNZA', 'CH4', 'CO', 'CPRM', 'ECH4', 'ETFLAG', 'ETH', 'ETHA', 'ETHY', 'ETOH', 'FORM', 'FPRM', 'IOLE', 'ISOP', 'ISP', 'IVOA', 'KET', 'MEOH', 'NA', 'NH3', 'NVOL', 'OLE', 'PAL', 'PAR', 'PCA', 'PCL', 'PEC', 'PFE', 'PH2O', 'PK', 'PMG', 'PMN', 'PNH4', 'PNO3', 'POA', 'PRPA', 'PSI', 'PSO4', 'PTI', 'SO2', 'SULF', 'TERP', 'TFLAG', 'TOL', 'TOLA', 'TRP', 'UNK', 'UNR', 'X', 'XYL', 'Y', 'latitude', 'longitude'] INFO:: to scale ['NO', 'NO2', 'HONO'] .. GENERATED FROM PYTHON SOURCE LINES 70-72 Apply Scaling ------------- .. GENERATED FROM PYTHON SOURCE LINES 72-83 .. code-block:: Python for skey in scalekeys: print('INFO:: scaling', skey, 'by', factor) outvar = outfile.variables[skey] invar = infile[skey] outvar[:] = factor * invar[:] outfile.sync() del outfile .. rst-class:: sphx-glr-script-out .. code-block:: none INFO:: scaling NO by 2.0 INFO:: scaling NO2 by 2.0 INFO:: scaling HONO by 2.0 .. GENERATED FROM PYTHON SOURCE LINES 84-86 Plot Comparison ''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 86-102 .. code-block:: Python import pyrsig import pycno import matplotlib.colors as mc oldfile = pyrsig.open_ioapi(oldpath) newfile = pyrsig.open_ioapi(newpath) key = scalekeys[0] compfile = newfile[[]] compfile['New'] = newfile[key].sum('LAY').mean('TSTEP') compfile['Old'] = oldfile[key].sum('LAY').mean('TSTEP') Z = compfile.to_dataarray(dim='version') Z.attrs.update(oldfile[key].attrs) fca = Z.plot(col='version', norm=mc.LogNorm(10, compfile['Old'].max())) pycno.cno(oldfile.crs_proj4).drawstates(ax=fca.axs) fca.fig.savefig(figpath) .. image-sg:: /auto_examples/gridemiss/images/sphx_glr_run_gridemiss_01_perturb_001.png :alt: version = New, version = Old :srcset: /auto_examples/gridemiss/images/sphx_glr_run_gridemiss_01_perturb_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.584 seconds) .. _sphx_glr_download_auto_examples_gridemiss_run_gridemiss_01_perturb.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_gridemiss_01_perturb.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_gridemiss_01_perturb.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: run_gridemiss_01_perturb.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_