WRF-CMAQ Preprocessor for InMAP
-------------------------------

Overview:
===========================
These scripts prepare CMAQ concentration and WRF meteorology output to be read into the InMAP preprocessing system. 
Conversions are made to align WRF and CMAQ data spatially along the x, y, and z axes. The MCIP METCRO3D files is used to calculate altitude.
Data slicing is used to obtain and align data temporally.
The resulting netCDF file is read into a modified version of the wrfchem InMAP preprocessing module.
In the interest of leveraging existing libraries and modules this process involves two steps using two different languages.

Requirements:
===========================
- The wrfcmaq2inmap script requires python 3.5+ with modules netCDF4 (1.2.9+), numpy (1.12+), and pandas (0.20.1+).
- InMAP 1.4.1 (this method is untested with later versions)
- CMAQ, WRF, and MCIP METCRO3D daily output files that are on alignable domains (same projection and nestable) with the same 2D resolution and the same layer definition.

An optional but helpful software package is nco, which allows for the combining of WRF output files into single 24 hour days 

Notes:
===========================
The scripts and ancillary files were designed around gridded modeling domains using the Lambert projection and WRF spheroid. Other projections will not work at this time.
Currently only the CB6 and SAPRC chemical mechanisms are supported for conversion.


Installation of wrfcmaq2inmap
===========================
The wrfcmaq2inmap libraries must be installed before the scripts can be used. The installation script checks that the module requirements are met.
To install into your user path run:
python setup.py install --user
Missing modules can be installed using pip. For example, if numpy is missing you can try "pip install numpy>=1.12 --user".

Preprocessing of inputs:
===========================
- WRF output files need to contain the entire 24 hours of the target day within a single file. If the WRF output files contain
  partial days then the entire set of hours 0-23 should be selected from multiple files. An example script used nco is provided at scripts/concat_wrfout.csh
- The CMAQ output concentration files can be preprocessed to create the lumped pollutants and partitioning values or left to 
 be calculated internally by these scripts. Mapping definition files for CB6 and SAPRC are provided in the ancillary directory.
- The WRF layering typically does not match the layer in the MCIP output. A layer mapping file is used to copy the correct WRF layers to the corresponding MCIP layers. Examples for 35 WRF to 25 MCIP and 50 WRF to 28 MCIP layers are provided in the ancillary path. If neither of these files are sufficient for a run a mapping file can be created by aligning the layers by fraction using the ZNW variable in the WRF output file and the VGLVLS attribute in MCIP.

WRF and CMAQ Output Processing:
===========================
The processer, wrfcmaq2inmap, takes WRF meteorological outputs and CMAQ concentration outputs and combines them into a single netCDF file with the same dimensions.
The daily processed netCDFs contain the variables needed to be passed to the InMAP preprocessor.

- An example driver script is provided at scripts/gen_wrfcmaq.csh. This script is setup to read a single day (20180101) of data, calculate the concentrations using SAPRC speciation, and remap the layers using the 50 to 28 layer mapping file.
- All grid subsetting from the WRF to MCIP domain is handled by the script provided that the two domains are properly aligned.

The script has internal dimensional and time step checks that can cause fatal errors when mismatches occur.

InMAP Preprocessing:
===========================
The InMAP Preprocessor is part of the InMAP system. Modified go code, additional scripts, and configuration files are included under the inmap subdirectory.
The go code is a modified version of the WRFCHEM preprocessor from InMAP 1.4.1. These files may not work with more recent versions of InMAP.
The preprocessor reads the daily output from the gen_wrfcmaq.py program and outputs to a single annual file with the necessary variables.

- Two preprocessor go modules are included for the WRFCMAQ model type: wrfcmaq.go.2007 and wrfcmaq.go.nlcd2011. Each version uses a different land cover mapping that matches
  the land cover used in the respective WRF runs. Changes to land cover, even within the same land cover product, may require new mappings. The mappings can be found in the
  NLCDseinfeld and NLCDwesely variables. Modifications requires documentation of the land cover index used in a specific WRF run.
- Two configuration files are also include: inmap/inmap/WRFCmaq_2007.toml and inmap/inmap/WRFCmaq_2011.toml. The only differences are the dates. The configuration files will
  need to be updated if different dates, projections, or modeling domains are used.

Setup and running
1) Select the preprocessor go module and copy to the file wrfcmaq.go in the root inmap path.
2) Copy the configuration files and the preprocessor driver script inmap_preproc.csh to the location where the preprocessor is run.
3) Backup the modules cmd.go and preproc.go under the inmaputil directory in the root inmap path. Copy the replacement modules from this package to that location.
4) Edit the selected configuration file to match the date, domain, input, and output paths for the InMAP preprocessor. 
5) Edit inmap_preproc.csh to match paths and names of the inmap executable and inputs. Set the program to use the selected configuration file. 
6) Run the inmap_preproc.sh script
