Source

EDGAR-4.2_minus_soil_and_biomass_to_AQMEII-NA / EDGAR_driver.sh

Diff from to

EDGAR_driver.sh

 #!/usr/bin/env bash
 
-# Requires NCL and R.
-# Driver for [,retemp_reunit.ncl,check_conservation.ncl], which
-# * sum and convert to mass several EDGAR global inventories
-# * regrid the global/unprojected EDGAR data to AQMEII-NA, an LCC-projected subdomain
-# * convert annual to hourly data
-# * check conservation of mass
+# ----------------------------------------------------------------------
+# description
+# ----------------------------------------------------------------------
+
+# A top-level driver for creating a single, composite inventory of N2O emissions over the AQMEII-NA domain from a number of EDGAR inventories of mostly anthropogenic sources over the globe. It
+
+# * sums several EDGAR global inventories
+# * converts the summed inventory from flux rate to mass-rate
+# * regrids the reunit-ed global/unprojected data to AQMEII-NA, an LCC-projected subdomain
+# * converts the annual data to hourly
+# * checks conservation of mass over the conversions
+
+# See https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na
+
+# Requirements include:
+
+# * definitely
+
+# ** bash: required to run this script. Known to work with bash --version==3.2.25
+
+# * potentially initially. If you have not already retrieved regrid_utils (see `get_regrid_utils`), you will need
+
+# ** git: to clone their repo
+# ** web access (HTTP currently)
+
+#   These must be available to the script when initially run.
+
+# * ultimately
+
+# ** basename, dirname
+# ** cp
+# ** curl or wget (latter {preferred, coded} for ability to deal with redirects)
+# ** gunzip, unzip
+# ** ncdump
+# ** NCL
+# ** R
+
+# TODO: failing functions should fail this (entire) driver!
+
 # Configure as needed for your platform.
 
 # ----------------------------------------------------------------------
 # constants with some simple manipulations
 # ----------------------------------------------------------------------
 
-THIS_FN="$(basename $0)"
-# will do the real work
-export CALL_REUNIT_FN='sum_reunit.r'
-export CALL_REGRID_FN='regrid_global_to_AQMEII.r'
-export CALL_RETEMP_FN='retemp.ncl'
-export CALL_CONSERV_FN='check_conservation.ncl'
+THIS="$0"
+THIS_FN="$(basename ${THIS})"
+THIS_DIR="$(dirname ${THIS})"
+
+# workspace
+export WORK_DIR="$(pwd)" # keep it simple for now: same dir as top of repo
+
+### downloading
+WGET_TO_FILE='wget --no-check-certificate -c -O'
+CURL_TO_FILE='curl -C - -o'
+CURL_TO_STREAM='curl'
 
 # model/inventory constants
 export MODEL_YEAR='2008'
 export MOLAR_MASS_N2O='44.0128'    # grams per mole of N2O, per wolframalpha.com
 export NITROGEN_MASS_N2O='28.0134' # grams per mole of N2,  per wolframalpha.com
 # export SECONDS_PER_HOUR='3600'     # 60 * 60
-
-# for R on EMVL: don't ask :-(
-R_DIR='/usr/local/apps/R-2.15.2/intel-13.0/bin'
-R="${R_DIR}/R"
-RSCRIPT="${R_DIR}/Rscript"
-
-# Ensure $NCARG_ROOT is set for NCL--it should be in your dotfiles, but, JIC ...
-# tlrPanP5
-#NCL_VERSION='ncl_ncarg-6.1.0.Linux_Debian_x86_64_nodap_gcc445'
-#export NCARG_ROOT="${HOME}/bin/${NCL_VERSION}"
-## Only change default if absolutely necessary!
-#NCL_EXEC='ncl'
-# terrae (temporary: they gotta upgrade RHEL, meanwhile use Herwehe's)
-# NCL_VERSION='ncl_ncarg-6.1.0.Linux_RedHat_x86_64_gcc412'
-NCL_VERSION='ncl_ncarg-6.1.2.Linux_RHEL5.6_x86_64_gcc412'
-export NCARG_ROOT="/home/hhg/ncl_ncarg/${NCL_VERSION}"
-NCL_EXEC="${NCARG_ROOT}/bin/ncl"
-
-# workspace
-export WORK_DIR="$(pwd)" # keep it simple for now: same dir as top of repo
-NETCDF_EXT='nc' # use this standard extension for netCDF files ...
-CMAQ_EXT='ncf'  # ... except when CMAQ expects something else
-
-# will do the real work
-export CALL_REUNIT_FP="${WORK_DIR}/${CALL_REUNIT_FN}"
-export CALL_REGRID_FP="${WORK_DIR}/${CALL_REGRID_FN}"
-export CALL_RETEMP_FP="${WORK_DIR}/${CALL_RETEMP_FN}"
-export CALL_CONSERV_FP="${WORK_DIR}/${CALL_CONSERV_FN}"
+# where this is hosted
+PROJECT_WEBSITE='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na'
+NETCDF_EXT_NCL='nc'   # NCL prefers the "canonical" netCDF extension
+NETCDF_EXT_CMAQ='ncf' # CMAQ, as is so often the case, is nonstandard :-(
 
 # for visualization (generally)
 export OUTPUT_SIGNIFICANT_DIGITS='3' # see conservation report below
+# PROJ.4 string for unprojected data
+export GLOBAL_PROJ4='+proj=longlat +ellps=WGS84'
 
 # for plotting (specifically)
-PDF_VIEWER='xpdf'  # whatever works on your platform
+# PDF_VIEWER='xpdf' # set this in bash_utilities::setup_apps
 # temporally disaggregate multiple plots
 DATE_FORMAT='%Y%m%d_%H%M'
 export PDF_DIR="${WORK_DIR}"
-# PROJ.4 string for unprojected data
-export GLOBAL_PROJ4='+proj=longlat +ellps=WGS84'
+# dimensions (for R plots--units?) for single-frame plots
+export SINGLE_FRAME_PLOT_HEIGHT='10'
+export SINGLE_FRAME_PLOT_WIDTH='15'
+# dimensions for multi-frame plots
+export TWELVE_MONTH_PLOT_HEIGHT='120'
+export TWELVE_MONTH_PLOT_WIDTH='20'
+
+## conservation report constants
+# need 5 additional digits for float output, e.g., "4.85e+04"
+# bash arithmetic gotcha: allow no spaces around '='!
+export OUTPUT_SIGNIFICANT_DIGITS='3' # see conservation report below
+# export CONSERV_REPORT_FIELD_WIDTH=$((OUTPUT_SIGNIFICANT_DIGITS + 5))
+export CONSERV_REPORT_FIELD_WIDTH='9' # width of 'AQMEII-NA'
+export CONSERV_REPORT_FLOAT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}.$((OUTPUT_SIGNIFICANT_DIGITS - 1))e"
+# echo -e "${THIS_FN}: CONSERV_REPORT_FLOAT_FORMAT=${CONSERV_REPORT_FLOAT_FORMAT}" # debugging
+export CONSERV_REPORT_INT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}i"
+export CONSERV_REPORT_COLUMN_SEPARATOR="  "
+export CONSERV_REPORT_TITLE="Is N2O conserved from input to output? units=kg N2O"
+export CONSERV_REPORT_SUBTITLE="(note (US land area)/(earth land area) ~= 6.15e-02)"
+
+# ----------------------------------------------------------------------
+# helpers
+# ----------------------------------------------------------------------
+
+### helpers in this repo
+
+export EDGAR_NONAG_REUNIT_HELPER_FN='sum_reunit.r'
+export EDGAR_NONAG_REUNIT_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_HELPER_FN}"
+export EDGAR_NONAG_REGRID_HELPER_FN='regrid_global_to_AQMEII.r'
+export EDGAR_NONAG_REGRID_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_HELPER_FN}"
+export EDGAR_NONAG_RETEMP_HELPER_FN='retemp.ncl'
+export EDGAR_NONAG_RETEMP_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_RETEMP_HELPER_FN}"
+export EDGAR_NONAG_CONSERV_HELPER_FN='check_conservation.ncl'
+export EDGAR_NONAG_CONSERV_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_CONSERV_HELPER_FN}"
+
+### helpers retrieved from elsewhere. TODO: create NCL and R packages
+
+# path to a project, not a .git
+REGRID_UTILS_URI='https://bitbucket.org/tlroche/regrid_utils'
+REGRID_UTILS_PN="$(basename ${REGRID_UTILS_URI})" # project name
+# can also use   'git@bitbucket.org:tlroche/regrid_utils' if supported
+# path to a folder, not a file: needed by NCL to get to initial helpers
+export REGRID_UTILS_FP="${WORK_DIR}/${REGRID_UTILS_PN}" # folder, not file
+
+export BASH_UTILS_FN='bash_utilities.sh'
+# export BASH_UTILS_FP="${REGRID_UTILS_FP}/${BASH_UTILS_FN}"
+# no, allow user to override repo code: see `get_bash_utils`
+export BASH_UTILS_FP="${WORK_DIR}/${BASH_UTILS_FN}"
+
+export STATS_FUNCS_FN='netCDF.stats.to.stdout.r'
+export STATS_FUNCS_FP="${WORK_DIR}/${STATS_FUNCS_FN}"
+
+export STRING_FUNCS_FN='string.ncl'
+export STRING_FUNCS_FP="${WORK_DIR}/${STRING_FUNCS_FN}"
+
+export SUMMARIZE_FUNCS_FN='summarize.ncl'
+export SUMMARIZE_FUNCS_FP="${WORK_DIR}/${SUMMARIZE_FUNCS_FN}"
+
+export TIME_FUNCS_FN='time.ncl'
+export TIME_FUNCS_FP="${WORK_DIR}/${TIME_FUNCS_FN}"
+
+export VIS_FUNCS_FN='visualization.r'
+export VIS_FUNCS_FP="${WORK_DIR}/${VIS_FUNCS_FN}"
+
+# ----------------------------------------------------------------------
+# inputs
+# ----------------------------------------------------------------------
 
 # for name generation
 EDGAR_NONAG_FN_ROOT='edgar_non-ag'
 
-## Raw input data
+### raw input
 
-# buncha individual netCDF files (see .tar.gz below)
+# Raw input data is global/unprojected EDGAR netCDF, with coordvars=
+# lat==(-90,+90)
+# lon==(0,+360), hence need to
+export ROTATE_INPUT="true"
+# after summing
+
+# buncha individual netCDF files (see .tar.gz below) with same schema
 # for category mappings, see
 # https://github.com/TomRoche/cornbeltN2O/wiki/Simulation-of-N2O-Production-and-Transport-in-the-US-Cornbelt-Compared-to-Tower-Measurements#wiki-EDGAR-4.2-categories
 EDGAR_NONAG_COMBUST_FN='v42_N2O_2008_IPCC_1A1_1A2_1B1b.0.1x0.1.nc'
 EDGAR_NONAG_GZ_FN="$(basename ${EDGAR_NONAG_GZ_URI})"
 export EDGAR_NONAG_GZ_FP="${WORK_DIR}/${EDGAR_NONAG_GZ_FN}"
 
-# Raw input data is global/unprojected EDGAR netCDF, with coordvars=
-# lat==(-90,+90)
-# lon==(0,+360), hence need to
-export ROTATE_INPUT="true"
-# after summing
-
 # TODO: automate getting this metadata from the netCDF
 export EDGAR_NONAG_RAW_DATAVAR_NAME='emi_n2o'
 # export EDGAR_NONAG_RAW_DATAVAR_TYPE='float' # in `ncdump -h`
 export EDGAR_NONAG_RAW_DATAVAR_COORD_X_NAME='lon'
 export EDGAR_NONAG_RAW_DATAVAR_COORD_Y_NAME='lat'
 
-## global/raw gridcell area data
-# Units for the above are flux: convert to mass rate using conveniently provided gridcell areas
+### Template for `raster` regridding.
+
+# The template input is a copy of some meteorological data with 52 "real" datavars.
+# That data is in the IOAPI format, which here is basically a wrapper around netCDF.
+# (IOAPI can be used with other data, and similar wrappers exist for other models.)
+# I removed all but one of the datavars (with NCO 'ncks'). TODO: script that!
+TEMPLATE_INPUT_GZ_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/emis_mole_all_20080101_12US1_cmaq_cb05_soa_2008ab_08c.EXTENTS_INPUT.nc.gz'
+TEMPLATE_INPUT_GZ_FN="$(basename ${TEMPLATE_INPUT_GZ_URI})"
+TEMPLATE_INPUT_GZ_FP="${WORK_DIR}/${TEMPLATE_INPUT_GZ_FN}"
+TEMPLATE_INPUT_ROOT="${TEMPLATE_INPUT_GZ_FN%.*}" # everything left of the LAST dot
+TEMPLATE_INPUT_FN="${TEMPLATE_INPUT_ROOT}"
+export TEMPLATE_INPUT_FP="${WORK_DIR}/${TEMPLATE_INPUT_FN}"
+# export TEMPLATE_INPUT_BAND='1' # `ncks` makes dim=TSTEP first
+export TEMPLATE_DATAVAR_NAME='emi_n2o'
+
+### global/raw gridcell area data
+# Units for the above are flux: convert to mass rate using conveniently provided gridcell areas (thank you, EDGAR!)
 EDGAR_NONAG_RAW_AREAS_URI='http://edgar.jrc.ec.europa.eu/dataset/edgarv4_0/info/area_0.1x0.1.nc'
 EDGAR_NONAG_RAW_AREAS_FN="$(basename ${EDGAR_NONAG_RAW_AREAS_URI})"
 export EDGAR_NONAG_RAW_AREAS_FP="${WORK_DIR}/${EDGAR_NONAG_RAW_AREAS_FN}"
 export EDGAR_NONAG_RAW_AREAS_DATAVAR_NAME='cell_area'
 
-## Intermediate product: sum of raw inputs, "reunit-ed" from flux rate to molar-mass rate
+# ----------------------------------------------------------------------
+# intermediate products
+# ----------------------------------------------------------------------
+
+### Intermediate product: sum of raw inputs, "reunit-ed" from flux rate to molar-mass rate
 EDGAR_NONAG_REUNIT_FN_ROOT="${EDGAR_NONAG_FN_ROOT}_summed_massed"
 
-EDGAR_NONAG_REUNIT_FN="${EDGAR_NONAG_REUNIT_FN_ROOT}.${NETCDF_EXT}"
+EDGAR_NONAG_REUNIT_FN="${EDGAR_NONAG_REUNIT_FN_ROOT}.${NETCDF_EXT_NCL}"
 export EDGAR_NONAG_REUNIT_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_FN}"
 export EDGAR_NONAG_REUNIT_DATAVAR_NAME="${EDGAR_NONAG_RAW_DATAVAR_NAME}"
 export EDGAR_NONAG_REUNIT_DATAVAR_TYPE="${EDGAR_NONAG_RAW_DATAVAR_TYPE}"
 export EDGAR_NONAG_REUNIT_DATAVAR_COORD_X_NAME="${EDGAR_NONAG_RAW_DATAVAR_COORD_X_NAME}"
 export EDGAR_NONAG_REUNIT_DATAVAR_COORD_Y_NAME="${EDGAR_NONAG_RAW_DATAVAR_COORD_Y_NAME}"
 
-# visualize it
+## visualize it
 EDGAR_NONAG_REUNIT_PDF_FN="${EDGAR_NONAG_REUNIT_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
 export EDGAR_NONAG_REUNIT_PDF_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_PDF_FN}" # path to PDF output
 
-## Template for `raster` regridding.
-
-# The template input is a copy of some meteorological data with 52 "real" datavars.
-# That data is in the IOAPI format, which here is basically a wrapper around netCDF.
-# (IOAPI can be used with other data, and similar wrappers exist for other models.)
-# I removed all but one of the datavars (with NCO 'ncks'). TODO: script that!
-TEMPLATE_INPUT_GZ_URI='https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na/downloads/emis_mole_all_20080101_12US1_cmaq_cb05_soa_2008ab_08c.EXTENTS_INPUT.nc.gz'
-TEMPLATE_INPUT_GZ_FN="$(basename ${TEMPLATE_INPUT_GZ_URI})"
-TEMPLATE_INPUT_GZ_FP="${WORK_DIR}/${TEMPLATE_INPUT_GZ_FN}"
-TEMPLATE_INPUT_ROOT="${TEMPLATE_INPUT_GZ_FN%.*}" # everything left of the LAST dot
-TEMPLATE_INPUT_FN="${TEMPLATE_INPUT_ROOT}"
-export TEMPLATE_INPUT_FP="${WORK_DIR}/${TEMPLATE_INPUT_FN}"
-# export TEMPLATE_INPUT_BAND='1' # `ncks` makes dim=TSTEP first
-export TEMPLATE_DATAVAR_NAME='emi_n2o'
-
-## Intermediate product: netCDF regridded to AQMEII-NA
+### Intermediate product: netCDF regridded to AQMEII-NA
 EDGAR_NONAG_REGRID_FN_ROOT="${EDGAR_NONAG_REUNIT_FN_ROOT}_regrid"
-EDGAR_NONAG_REGRID_FN="${EDGAR_NONAG_REGRID_FN_ROOT}.${NETCDF_EXT}"
-# EDGAR_NONAG_REGRID_URI='2008PTONCLMCNN2O_regrid.nc'
-# EDGAR_NONAG_REGRID_FN="$(basename ${EDGAR_NONAG_REGRID_URI})"
-# EDGAR_NONAG_REGRID_FN_ROOT="${EDGAR_NONAG_REGRID_FN%.*}" # everything left of the dot
+EDGAR_NONAG_REGRID_FN="${EDGAR_NONAG_REGRID_FN_ROOT}.${NETCDF_EXT_NCL}"
 export EDGAR_NONAG_REGRID_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_FN}"
 
 export EDGAR_NONAG_REGRID_DATAVAR_NAME="${EDGAR_NONAG_RAW_DATAVAR_NAME}"
 # export EDGAR_NONAG_REGRID_TIME_VAR_NAME='TSTEP' # more IOAPI-like than 'time'
 # export EDGAR_NONAG_REGRID_TIME_VAR_UNIT='month (1=2, 2=Jan, ... 13=Dec, 13=14)'
 
+## visualize it
 EDGAR_NONAG_REGRID_PDF_FN="${EDGAR_NONAG_REGRID_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
 export EDGAR_NONAG_REGRID_PDF_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_PDF_FN}" # path to PDF output
 
-# ----------------------------------------------------------------------
+### data container files
+### create data container files from extents/template file
 
-## create data container files from extents/template file
 # problem: NCL currently (version=6.1.2) does not support *.ncf :-( workaround below
 # replace a string with values @ runtime
+# TODO: driver must ensure output_fp_template does not exist!
 # export CMAQ_TARGET_FN_TEMPLATE_STR='######' # works for NCL, not bash
 export CMAQ_TARGET_FN_TEMPLATE_STR='@@@@@@'
 CMAQ_TARGET_FN_ROOT_TEMPLATE="emis_mole_N2O_${CMAQ_TARGET_FN_TEMPLATE_STR}_12US1_cmaq_cb05_soa_2008ab_08c"
-CMAQ_TARGET_EXT_CMAQ='ncf'
-CMAQ_TARGET_EXT_NCL='nc'
-CMAQ_TARGET_FN_TEMPLATE_CMAQ="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${CMAQ_TARGET_EXT_CMAQ}"
-CMAQ_TARGET_FN_TEMPLATE_NCL="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${CMAQ_TARGET_EXT_NCL}"
+CMAQ_TARGET_FN_TEMPLATE_CMAQ="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${NETCDF_EXT_CMAQ}"
+CMAQ_TARGET_FN_TEMPLATE_NCL="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${NETCDF_EXT_NCL}"
 export CMAQ_TARGET_FP_TEMPLATE_CMAQ="${WORK_DIR}/${CMAQ_TARGET_FN_TEMPLATE_CMAQ}"
 export CMAQ_TARGET_FP_TEMPLATE_NCL="${WORK_DIR}/${CMAQ_TARGET_FN_TEMPLATE_NCL}"
 export CMAQ_TARGET_FP_NCL="${CMAQ_TARGET_FP_TEMPLATE_NCL/${CMAQ_TARGET_FN_TEMPLATE_STR}/${MODEL_YEAR}}"
 
 ## global metadata for both {template, "real" monthly/hourly data container} files
-export CMAQ_TARGET_FILE_ATTR_HISTORY='see https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na'
+export CMAQ_TARGET_FILE_ATTR_HISTORY="see ${PROJECT_WEBSITE}"
 
 ### output file, datavar
 # replace template string with value
 ## metadata for datavar in both {template, "real" monthly/hourly data container} files
 export CMAQ_TARGET_DATAVAR_NAME='N2O'
 # IOAPI pads varattr=long_name to length=16 with spaces
-export CMAQ_TARGET_DATAVAR_ATTR_LONG_NAME="$(printf '%-16s' ${CMAQ_TARGET_DATAVAR_NAME})"
+export CMAQ_TARGET_DATAVAR_ATTR_LONG_NAME="$(printf '%-16s' "${CMAQ_TARGET_DATAVAR_NAME}")"
 # IOAPI pads varattr=units to length=16 with spaces
-export CMAQ_TARGET_DATAVAR_ATTR_UNITS="$(printf '%-16s' ${EDGAR_NONAG_REUNIT_DATAVAR_UNITS})"
+export CMAQ_TARGET_DATAVAR_ATTR_UNITS="$(printf '%-16s' "${EDGAR_NONAG_REUNIT_DATAVAR_UNITS}")"
 # IOAPI pads varattr=var_desc to length=80 with spaces
 export CMAQ_TARGET_DATAVAR_ATTR_VAR_DESC="$(printf '%-80s' "Model species ${CMAQ_TARGET_DATAVAR_NAME}")"
 
-## conservation report constants
-# need 5 additional digits for float output, e.g., "4.85e+04"
-# bash arithmetic gotcha: allow no spaces around '='!
-export OUTPUT_SIGNIFICANT_DIGITS='3' # see conservation report below
-# export CONSERV_REPORT_FIELD_WIDTH=$((OUTPUT_SIGNIFICANT_DIGITS + 5))
-export CONSERV_REPORT_FIELD_WIDTH='9' # width of 'AQMEII-NA'
-export CONSERV_REPORT_FLOAT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}.$((OUTPUT_SIGNIFICANT_DIGITS - 1))e"
-# echo -e "${THIS_FN}: CONSERV_REPORT_FLOAT_FORMAT=${CONSERV_REPORT_FLOAT_FORMAT}" # debugging
-export CONSERV_REPORT_INT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}i"
-export CONSERV_REPORT_COLUMN_SEPARATOR="  "
-export CONSERV_REPORT_TITLE="Is N2O conserved from input to output? units=kg N2O"
-export CONSERV_REPORT_SUBTITLE="(note (US land area)/(earth land area) ~= 6.15e-02)"
-
-# These helpers should have been cloned into the cwd, but JIC (and for later use). TODO: R-package my code
-
-STAT_FUNCS_URI='https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na/raw/53b5f09469e2426952eb83711f72961566e4932b/netCDF.stats.to.stdout.r'
-STAT_FUNCS_FN="$(basename ${STAT_FUNCS_URI})"
-export STAT_FUNCS_FP="${WORK_DIR}/${STAT_FUNCS_FN}"
-
-# currently in this repository
-VIZ_FUNCS_FN='visualization.r'
-export VIZ_FUNCS_FP="${WORK_DIR}/${VIZ_FUNCS_FN}"
-
-# currently in this repository
-SUMM_FUNCS_FN='summarize.ncl'
-export SUMM_FUNCS_FP="${WORK_DIR}/${SUMM_FUNCS_FN}"
-
-# PLOT_SCRIPT_URI='https://bitbucket.org/tlroche/geia_regrid/raw/6ecdb8ae7d6fd661f7c4616a14b817c5aa9c7f90/plotLayersForTimestep.r'
-# PLOT_SCRIPT_FN="$(basename ${PLOT_SCRIPT_URI})"
-# export PLOT_SCRIPT_FP="${WORK_DIR}/${PLOT_SCRIPT_FN}"
-
 # ----------------------------------------------------------------------
-# setup
+# functions
 # ----------------------------------------------------------------------
 
-# for netcdf4 (ncdf4.so, libnetcdf.so.7) on EMVL:
-# I may need to run this from CLI, not here, and
-# you may not need this at all!
-module add netcdf-4.1.2
-
-mkdir -p ${WORK_DIR}
-
-# get the raw inputs
-if [[
-    -z "${EDGAR_NONAG_COMBUST_FP}" ||
-    -z "${EDGAR_NONAG_NONROAD_FP}" ||
-    -z "${EDGAR_NONAG_ROAD_FP}" ||
-    -z "${EDGAR_NONAG_RESID_FP}" ||
-    -z "${EDGAR_NONAG_PETRO_FP}" ||
-    -z "${EDGAR_NONAG_IND_FP}" ||
-    -z "${EDGAR_NONAG_MANURE_FP}" ||
-    -z "${EDGAR_NONAG_RUNOFF_FP}" ||
-    -z "${EDGAR_NONAG_WASTE_FP}" ||
-    -z "${EDGAR_NONAG_FFF_FP}" ||
-    -z "${EDGAR_NONAG_TROPO_FP}"
-  ]] ; then
-  echo -e "${THIS_FN}: ERROR: not all raw inputs defined"
-  exit 1
-fi
-if [[
-    ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
-    ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
-    ! -r "${EDGAR_NONAG_ROAD_FP}" ||
-    ! -r "${EDGAR_NONAG_RESID_FP}" ||
-    ! -r "${EDGAR_NONAG_PETRO_FP}" ||
-    ! -r "${EDGAR_NONAG_IND_FP}" ||
-    ! -r "${EDGAR_NONAG_MANURE_FP}" ||
-    ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
-    ! -r "${EDGAR_NONAG_WASTE_FP}" ||
-    ! -r "${EDGAR_NONAG_FFF_FP}" ||
-    ! -r "${EDGAR_NONAG_TROPO_FP}"
-  ]] ; then
-  # download and decompress the files
+# TODO: test for resources, reuse if available
+# {setup_paths, setup_apps} isa bash util, so `get_helpers` first
+function setup {
   for CMD in \
-    "wget  --no-check-certificate -c -O - ${EDGAR_NONAG_GZ_URI} | tar xfz -" \
+    "mkdir -p ${WORK_DIR}" \
+    'get_helpers' \
+    'setup_paths' \
+    'setup_apps' \
+    'setup_resources' \
   ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
+    if [[ -z "${CMD}" ]] ; then
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
+      exit 1
+    else
+      echo -e "\n$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}\n"
+      eval "${CMD}" # comment this out for NOPing, e.g., to `source`
+    fi
   done
-fi
-if [[
-    ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
-    ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
-    ! -r "${EDGAR_NONAG_ROAD_FP}" ||
-    ! -r "${EDGAR_NONAG_RESID_FP}" ||
-    ! -r "${EDGAR_NONAG_PETRO_FP}" ||
-    ! -r "${EDGAR_NONAG_IND_FP}" ||
-    ! -r "${EDGAR_NONAG_MANURE_FP}" ||
-    ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
-    ! -r "${EDGAR_NONAG_WASTE_FP}" ||
-    ! -r "${EDGAR_NONAG_FFF_FP}" ||
-    ! -r "${EDGAR_NONAG_TROPO_FP}"
-  ]] ; then
-  echo -e "${THIS_FN}: ERROR: not all raw inputs are readable"
-  exit 2
-fi
-
-if [[ -z "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: EDGAR_NONAG_RAW_AREAS_FP not defined"
-  exit 3
-fi
-if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
-  for CMD in \
-    "wget --no-check-certificate -c -O ${EDGAR_NONAG_RAW_AREAS_FP} ${EDGAR_NONAG_RAW_AREAS_URI}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
-if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot download EDGAR_NONAG_RAW_AREAS_FP=='${EDGAR_NONAG_RAW_AREAS_FP}'"
-  exit 4
-fi
-
-if [[ -z "${STAT_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: STAT_FUNCS_FP not defined"
-  exit 5
-fi
-if [[ ! -r "${STAT_FUNCS_FP}" ]] ; then
+} # end function setup
+
+function get_helpers {
   for CMD in \
-    "wget --no-check-certificate -c -O ${STAT_FUNCS_FP} ${STAT_FUNCS_URI}" \
+    'get_regrid_utils' \
+    'get_bash_utils' \
+    'get_stats_funcs' \
+    'get_string_funcs' \
+    'get_summarize_funcs' \
+    'get_time_funcs' \
+    'get_vis_funcs' \
+    'get_reunit' \
+    'get_regrid' \
+    'get_retemp' \
+    'get_conserv' \
   ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
+    if [[ -z "${CMD}" ]] ; then
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
+      exit 2
+    else
+      echo -e "\n$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}\n"
+      eval "${CMD}" # comment this out for NOPing, e.g., to `source`
+    fi
   done
-fi
-if [[ ! -r "${STAT_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot download STAT_FUNCS_FP=='${STAT_FUNCS_FP}'"
-  exit 6
-fi
-
-if [[ -z "${VIZ_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: VIZ_FUNCS_FP not defined"
-  exit 5
-fi
-# currently in this repo
-# if [[ ! -r "${VIZ_FUNCS_FP}" ]] ; then
-#   for CMD in \
-#     "wget --no-check-certificate -c -O ${VIZ_FUNCS_FP} ${VIZ_FUNCS_URI}" \
-#   ; do
-#     echo -e "$ ${CMD}"
-#     eval "${CMD}"
-#   done
-# fi
-if [[ ! -r "${VIZ_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot download VIZ_FUNCS_FP=='${VIZ_FUNCS_FP}'"
-  exit 6
-fi
-
-if [[ -z "${SUMM_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: SUMM_FUNCS_FP not defined"
-  exit 7
-fi
-# currently in this repo
-# if [[ ! -r "${SUMM_FUNCS_FP}" ]] ; then
-#   for CMD in \
-#     "wget --no-check-certificate -c -O ${SUMM_FUNCS_FP} ${SUMM_FUNCS_URI}" \
-#   ; do
-#     echo -e "$ ${CMD}"
-#     eval "${CMD}"
-#   done
-# fi
-if [[ ! -r "${SUMM_FUNCS_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot download SUMM_FUNCS_FP=='${SUMM_FUNCS_FP}'"
-  exit 8
-fi
+} # end function get_helpers
 
-# get the "template" file, used for extent-setting and emissions-file creation
-if [[ -z "${TEMPLATE_INPUT_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: TEMPLATE_INPUT_FP not defined"
-  exit 9
-fi
-if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
-  if [[ ! -r "${TEMPLATE_INPUT_GZ_FP}" ]] ; then
+# This gets "package"=regrid_utils from which the remaining helpers are (at least potentially) drawn.
+function get_regrid_utils {
+  if [[ -z "${REGRID_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: REGRID_UTILS_FP not defined"
+    exit 3
+  fi
+  if [[ ! -r "${REGRID_UTILS_FP}" ]] ; then
+#      "rm -fr ${REGRID_UTILS_FP}/.git" \
     for CMD in \
-      "wget --no-check-certificate -c -O ${TEMPLATE_INPUT_GZ_FP} ${TEMPLATE_INPUT_GZ_URI}" \
+      "git clone ${REGRID_UTILS_URI}.git" \
     ; do
       echo -e "$ ${CMD}"
       eval "${CMD}"
     done
   fi
-  if [[ -r "${TEMPLATE_INPUT_GZ_FP}" ]] ; then
+  if [[ ! -r "${REGRID_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot download REGRID_UTILS_FP=='${REGRID_UTILS_FP}'"
+    exit 4
+  fi  
+} # end function get_regrid_utils
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${BASH_UTILS_FP} before running this script.
+function get_bash_utils {
+  if [[ -z "${BASH_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP not defined"
+    exit 5
+  fi
+  if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
     for CMD in \
-      "gunzip ${TEMPLATE_INPUT_GZ_FP}" \
+      "cp ${REGRID_UTILS_FP}/${BASH_UTILS_FN} ${BASH_UTILS_FP}" \
     ; do
       echo -e "$ ${CMD}"
       eval "${CMD}"
     done
   fi
-fi
-if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot read TEMPLATE_INPUT_FP=='${TEMPLATE_INPUT_FP}'"
-  exit 10
-fi
-
-# ----------------------------------------------------------------------
-# payload
-# ----------------------------------------------------------------------
-
-# ----------------------------------------------------------------------
-# sum raw inputs and convert units
-# ----------------------------------------------------------------------
-
-cat <<EOM
+  if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP=='${BASH_UTILS_FP}' not readable"
+    exit 6
+  fi
+  # This is bash, so gotta ...
+  source "${BASH_UTILS_FP}"
+  # ... for its functions to be available later in this script
+} # end function get_bash_utils
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${STATS_FUNCS_FP} before running this script.
+function get_stats_funcs {
+  if [[ -z "${STATS_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STATS_FUNCS_FP not defined"
+    exit 9
+  fi
+  if [[ ! -r "${STATS_FUNCS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_FP}/${STATS_FUNCS_FN} ${STATS_FUNCS_FP}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${STATS_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STATS_FUNCS_FP=='${STATS_FUNCS_FP}' not readable"
+    exit 10
+  fi
+} # end function get_stats_funcs
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${STRING_FUNCS_FP} before running this script.
+function get_string_funcs {
+  if [[ -z "${STRING_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STRING_FUNCS_FP not defined"
+    exit 9
+  fi
+  if [[ ! -r "${STRING_FUNCS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_FP}/${STRING_FUNCS_FN} ${STRING_FUNCS_FP}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${STRING_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STRING_FUNCS_FP=='${STRING_FUNCS_FP}' not readable"
+    exit 10
+  fi
+} # end function get_string_funcs
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${SUMMARIZE_FUNCS_FP} before running this script.
+function get_summarize_funcs {
+  if [[ -z "${SUMMARIZE_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: SUMMARIZE_FUNCS_FP not defined"
+    exit 13
+  fi
+  if [[ ! -r "${SUMMARIZE_FUNCS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_FP}/${SUMMARIZE_FUNCS_FN} ${SUMMARIZE_FUNCS_FP}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${SUMMARIZE_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: SUMMARIZE_FUNCS_FP=='${SUMMARIZE_FUNCS_FP}' not readable"
+    exit 14
+  fi
+} # end function get_summarize_funcs
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${TIME_FUNCS_FP} before running this script.
+function get_time_funcs {
+  if [[ -z "${TIME_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TIME_FUNCS_FP not defined"
+    exit 15
+  fi
+  if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_FP}/${TIME_FUNCS_FN} ${TIME_FUNCS_FP}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TIME_FUNCS_FP=='${TIME_FUNCS_FP}' not readable"
+    exit 16
+  fi
+} # end function get_time_funcs
+
+# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${VIS_FUNCS_FP} before running this script.
+function get_vis_funcs {
+  if [[ -z "${VIS_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: VIS_FUNCS_FP not defined"
+    exit 17
+  fi
+  if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_FP}/${VIS_FUNCS_FN} ${VIS_FUNCS_FP}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: VIS_FUNCS_FP=='${VIS_FUNCS_FP}' not readable"
+    exit 18
+  fi
+} # end function get_vis_funcs
 
-About to run R script="${CALL_REUNIT_FP}"
+function get_reunit {
+  if [[ -z "${EDGAR_NONAG_REUNIT_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_REUNIT_HELPER_FP not defined"
+    exit 19
+  fi
+  # is in this repo
+#  if [[ ! -r "${EDGAR_NONAG_REUNIT_HELPER_FP}" ]] ; then
+#    for CMD in \
+#      "${WGET_TO_FILE} ${EDGAR_NONAG_REUNIT_HELPER_FP} ${REUNIT_URI}" \
+#    ; do
+#      echo -e "$ ${CMD}"
+#      eval "${CMD}"
+#    done
+#  fi
+  if [[ ! -r "${EDGAR_NONAG_REUNIT_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_REUNIT_HELPER_FP=='${EDGAR_NONAG_REUNIT_HELPER_FP}' not readable"
+    exit 20
+  fi
+} # end function get_reunit
 
-EOM
+function get_regrid {
+  if [[ -z "${EDGAR_NONAG_REGRID_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_REGRID_HELPER_FP not defined"
+    exit 21
+  fi
+  # is in this repo
+#  if [[ ! -r "${EDGAR_NONAG_REGRID_HELPER_FP}" ]] ; then
+#    for CMD in \
+#      "${WGET_TO_FILE} ${EDGAR_NONAG_REGRID_HELPER_FP} ${REGRID_URI}" \
+#    ; do
+#      echo -e "$ ${CMD}"
+#      eval "${CMD}"
+#    done
+#  fi
+  if [[ ! -r "${EDGAR_NONAG_REGRID_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_REGRID_HELPER_FP=='${EDGAR_NONAG_REGRID_HELPER_FP}' not readable"
+    exit 22
+  fi
+} # end function get_regrid
 
-# If this fails ...
-"${RSCRIPT}" "${CALL_REUNIT_FP}"
-# ... just start R ...
-# "${R}"
-# ... and source ${CALL_REUNIT_FP}, e.g.,
-# source('....r')
+function get_retemp {
+  if [[ -z "${EDGAR_NONAG_RETEMP_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_RETEMP_HELPER_FP not defined"
+    exit 23
+  fi
+  # is in this repo
+#  if [[ ! -r "${EDGAR_NONAG_RETEMP_HELPER_FP}" ]] ; then
+#    for CMD in \
+#      "${WGET_TO_FILE} ${EDGAR_NONAG_RETEMP_HELPER_FP} ${RETEMP_URI}" \
+#    ; do
+#      echo -e "$ ${CMD}"
+#      eval "${CMD}"
+#    done
+#  fi
+  if [[ ! -r "${EDGAR_NONAG_RETEMP_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_RETEMP_HELPER_FP=='${EDGAR_NONAG_RETEMP_HELPER_FP}' not readable"
+    exit 24
+  fi
+} # end function get_retemp
 
-# ----------------------------------------------------------------------
-# display netCDFs and PDFs (if plotted)
-# ----------------------------------------------------------------------
+function get_conserv {
+  if [[ -z "${EDGAR_NONAG_CONSERV_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_CONSERV_HELPER_FP not defined"
+    exit 25
+  fi
+  # is in this repo
+#  if [[ ! -r "${EDGAR_NONAG_CONSERV_HELPER_FP}" ]] ; then
+#    for CMD in \
+#      "${WGET_TO_FILE} ${EDGAR_NONAG_CONSERV_HELPER_FP} ${CONSERV_URI}" \
+#    ; do
+#      echo -e "$ ${CMD}"
+#      eval "${CMD}"
+#    done
+#  fi
+  if [[ ! -r "${EDGAR_NONAG_CONSERV_HELPER_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_CONSERV_HELPER_FP=='${EDGAR_NONAG_CONSERV_HELPER_FP}' not readable"
+    exit 25
+  fi
+} # end function get_conserv
 
-if [[ -r "${EDGAR_NONAG_REUNIT_PDF_FP}" ]] ; then
+function setup_resources {
+#     'get_regrid' \
+#     'get_MSFs' \
+#     'get_target_template' \
   for CMD in \
-    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT}" \
-    "ls -alht ${WORK_DIR}/*.pdf" \
-    "${PDF_VIEWER} ${EDGAR_NONAG_REUNIT_PDF_FP} &" \
+    'get_raw_input' \
+    'get_areas_input' \
+    'get_template_input' \
   ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
+    if [[ -z "${CMD}" ]] ; then
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
+      exit 27
+    else
+      echo -e "\n$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}\n"
+      eval "${CMD}" # comment this out for NOPing, e.g., to `source`
+    fi
   done
-else
-  echo -e "${THIS_FN}: ERROR: sum/reunit not plotted: ${EDGAR_NONAG_REUNIT_PDF_FP}"
-fi
+} # end function setup_resources
+
+### get the (several) raw inputs
+function get_raw_input {
+  if [[
+      -z "${EDGAR_NONAG_COMBUST_FP}" ||
+      -z "${EDGAR_NONAG_NONROAD_FP}" ||
+      -z "${EDGAR_NONAG_ROAD_FP}" ||
+      -z "${EDGAR_NONAG_RESID_FP}" ||
+      -z "${EDGAR_NONAG_PETRO_FP}" ||
+      -z "${EDGAR_NONAG_IND_FP}" ||
+      -z "${EDGAR_NONAG_MANURE_FP}" ||
+      -z "${EDGAR_NONAG_RUNOFF_FP}" ||
+      -z "${EDGAR_NONAG_WASTE_FP}" ||
+      -z "${EDGAR_NONAG_FFF_FP}" ||
+      -z "${EDGAR_NONAG_TROPO_FP}"
+    ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: not all raw inputs defined"
+    exit 1
+  fi
+  if [[
+      ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
+      ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
+      ! -r "${EDGAR_NONAG_ROAD_FP}" ||
+      ! -r "${EDGAR_NONAG_RESID_FP}" ||
+      ! -r "${EDGAR_NONAG_PETRO_FP}" ||
+      ! -r "${EDGAR_NONAG_IND_FP}" ||
+      ! -r "${EDGAR_NONAG_MANURE_FP}" ||
+      ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
+      ! -r "${EDGAR_NONAG_WASTE_FP}" ||
+      ! -r "${EDGAR_NONAG_FFF_FP}" ||
+      ! -r "${EDGAR_NONAG_TROPO_FP}"
+    ]] ; then
+    # download and decompress the files
+    for CMD in \
+      "${WGET_TO_FILE} - ${EDGAR_NONAG_GZ_URI} | tar xfz -" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[
+      ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
+      ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
+      ! -r "${EDGAR_NONAG_ROAD_FP}" ||
+      ! -r "${EDGAR_NONAG_RESID_FP}" ||
+      ! -r "${EDGAR_NONAG_PETRO_FP}" ||
+      ! -r "${EDGAR_NONAG_IND_FP}" ||
+      ! -r "${EDGAR_NONAG_MANURE_FP}" ||
+      ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
+      ! -r "${EDGAR_NONAG_WASTE_FP}" ||
+      ! -r "${EDGAR_NONAG_FFF_FP}" ||
+      ! -r "${EDGAR_NONAG_TROPO_FP}"
+    ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: not all raw inputs are readable"
+    exit 2
+  fi
+} # end function get_raw_input
 
-# ----------------------------------------------------------------------
-# regrid from global/unprojected to AQMEII/LCC
-# ----------------------------------------------------------------------
+function get_areas_input {
+  if [[ -z "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_RAW_AREAS_FP not defined"
+    exit 3
+  fi
+  if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
+    for CMD in \
+      "${WGET_TO_FILE} ${EDGAR_NONAG_RAW_AREAS_FP} ${EDGAR_NONAG_RAW_AREAS_URI}" \
+    ; do
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
+  fi
+  if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot download EDGAR_NONAG_RAW_AREAS_FP=='${EDGAR_NONAG_RAW_AREAS_FP}'"
+    exit 4
+  fi
+} # end function get_areas_input
 
-cat <<EOM
+# get the "template" file, used for extent-setting and emissions-file creation
+function get_template_input {
+  if [[ -z "${TEMPLATE_INPUT_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TEMPLATE_INPUT_FP not defined"
+    exit 9
+  fi
+  if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
+    if [[ ! -r "${TEMPLATE_INPUT_GZ_FP}" ]] ; then
+      for CMD in \
+        "${WGET_TO_FILE} ${TEMPLATE_INPUT_GZ_FP} ${TEMPLATE_INPUT_GZ_URI}" \
+      ; do
+        echo -e "$ ${CMD}"
+        eval "${CMD}"
+      done
+    fi
+    if [[ -r "${TEMPLATE_INPUT_GZ_FP}" ]] ; then
+      for CMD in \
+        "gunzip ${TEMPLATE_INPUT_GZ_FP}" \
+      ; do
+        echo -e "$ ${CMD}"
+        eval "${CMD}"
+      done
+    fi
+  fi
+  if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot read TEMPLATE_INPUT_FP=='${TEMPLATE_INPUT_FP}'"
+    exit 10
+  fi
+} # end function get_template_input
+
+# ======================================================================
+
+### Sum raw inputs and "reunit" sum from flux rate to molar-mass rate prior to regridding.
+### TODO: pass commandline args to R
+### punt: just use envvars :-(
+function reunit {
+#   # ... just start R ...
+#   R
+#   # ... and maybe source ${EDGAR_NONAG_REUNIT_HELPER_FP}, e.g.,
+#   source('....r')
+
+  if [[ -r "${EDGAR_NONAG_REUNIT_FP}" ]] ; then
+    echo -e "${THIS_FN}: sum/reunit-ed data=='${EDGAR_NONAG_REUNIT_FP}' exists, skipping"
+  else
+    for CMD in \
+      "Rscript ${EDGAR_NONAG_REUNIT_HELPER_FP}" \
+    ; do
+    cat <<EOM
 
-About to run R script="${CALL_REGRID_FP}"
+About to run command='${CMD}'. WARNING: may seem to hang while processing!
 
 EOM
+      eval "${CMD}"
+    done
 
-# If this fails ...
-"${RSCRIPT}" "${CALL_REGRID_FP}"
-# ... just start R ...
-#"${R}"
-# ... and source ${CALL_REGRID_FP}, e.g.,
-# source('....r')
-
-# ----------------------------------------------------------------------
-# display cwd and PDFs (if plotted)
-# ----------------------------------------------------------------------
-
-if [[ -r "${EDGAR_NONAG_REGRID_PDF_FP}" ]] ; then
-  for CMD in \
-    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT}" \
-    "ls -alht ${WORK_DIR}/*.pdf" \
-    "${PDF_VIEWER} ${EDGAR_NONAG_REGRID_PDF_FP} &" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-else
-  echo -e "${THIS_FN}: ERROR: regrid not plotted: ${EDGAR_NONAG_REGRID_PDF_FP}"
-fi
-
-# ----------------------------------------------------------------------
-# "retemporalize" from annual to hourly (and put in CMAQ-style file)
-# ----------------------------------------------------------------------
+    # After exiting R, show cwd and display output PDF ... maybe
+    if [[ ! -r "${EDGAR_NONAG_REUNIT_FP}" ]] ; then
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to create sum/reunit-ed output='${CLM_CN_REGRID_PDF_FP}'"
+        exit 40
+    fi
+
+    if [[ -r "${EDGAR_NONAG_REUNIT_PDF_FP}" ]] ; then
+      for CMD in \
+          "ls -alht ${EDGAR_NONAG_REUNIT_PDF_FP}" \
+          "${PDF_VIEWER} ${EDGAR_NONAG_REUNIT_PDF_FP} &" \
+          ; do
+          echo -e "$ ${CMD}"
+          eval "${CMD}"
+      done
+    else
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to plot sum/reunit-ed input='${EDGAR_NONAG_REUNIT_PDF_FP}'"
+    fi
+  fi # end if [[ -r "${EDGAR_NONAG_REUNIT_FP}" ]]
+} # end function reunit
+
+### regrid from global/unprojected to AQMEII/LCC
+function regrid {
+#   # ... just start R ...
+#   R
+#   # ... and maybe source ${EDGAR_NONAG_REGRID_HELPER_FP}, e.g.,
+#   source('....r')
+
+  if [[ -r "${EDGAR_NONAG_REGRID_FP}" ]] ; then
+    echo -e "${THIS_FN}: regridded data=='${EDGAR_NONAG_REGRID_FP}' exists, skipping"
+  else
+    for CMD in \
+      "Rscript ${EDGAR_NONAG_REGRID_HELPER_FP}" \
+    ; do
+    cat <<EOM
 
-# remove any pre-existing output data container files
+About to run command='${CMD}'. WARNING: may seem to hang while processing!
 
-# # start debugging-----------------------------------------------------
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_TEMPLATE_CMAQ='${CMAQ_TARGET_FP_TEMPLATE_CMAQ}'"
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_TEMPLATE_NCL= '${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_CMAQ=         '${CMAQ_TARGET_FP_CMAQ}'"
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_NCL=          '${CMAQ_TARGET_FP_NCL}'"
-# #   end debugging-----------------------------------------------------
+EOM
+      eval "${CMD}"
+    done
 
-if [[ -r "${CMAQ_TARGET_FP_CMAQ}" ]] ; then
-  for CMD in \
-    "rm ${CMAQ_TARGET_FP_CMAQ}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
+    # After exiting R, show cwd and display output PDF ... maybe
+    if [[ ! -r "${EDGAR_NONAG_REGRID_FP}" ]] ; then
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to create regrid output='${EDGAR_NONAG_REGRID_PDF_FP}'"
+        exit 40
+    fi
+
+    if [[ -r "${EDGAR_NONAG_REGRID_PDF_FP}" ]] ; then
+      for CMD in \
+          "ls -alht ${EDGAR_NONAG_REGRID_PDF_FP}" \
+          "${PDF_VIEWER} ${EDGAR_NONAG_REGRID_PDF_FP} &" \
+          ; do
+          echo -e "$ ${CMD}"
+          eval "${CMD}"
+      done
+    else
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to plot regrid output='${EDGAR_NONAG_REGRID_PDF_FP}'"
+    fi
+  fi # end if [[ -r "${EDGAR_NONAG_REGRID_FP}" ]]
+} # end function regrid
+
+### "Retemporalize" from annual to hourly (and put in CMAQ-style file), then check mass conservation.
+### TODO: pass commandline args to NCL
+### punt: just use envvars :-(
+function retemp_conserv {
+#   eval "rm ${CMAQ_TARGET_FP_TEMPLATE_NCL} ${CMAQ_TARGET_FP_NCL}"
+#   ncl # bail to NCL and copy script lines
+
+  if [[ -r "${CMAQ_TARGET_FP_CMAQ}" ]] ; then
+    echo -e "${THIS_FN}: retemporalized data=='${CMAQ_TARGET_FP_CMAQ}' exists, skipping"
+  else
+
+    ### retemporalize
+
+    for SCRIPT in \
+      "${EDGAR_NONAG_RETEMP_HELPER_FP}" \
+      "${EDGAR_NONAG_CONSERV_HELPER_FP}" \
+    ; do
+      cat <<EOM
 
-if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_CMAQ}" ]] ; then
-  for CMD in \
-    "rm ${CMAQ_TARGET_FP_TEMPLATE_CMAQ}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
+About to run NCL script="${SCRIPT}"
 
-if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
-  for CMD in \
-    "rm ${CMAQ_TARGET_FP_NCL}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
+EOM
+      # '-n' -> http://www.ncl.ucar.edu/Document/Functions/Built-in/print.shtml
+      CMD="ncl -n ${SCRIPT}"
+      echo -e "$ ${CMD}"
+      eval "${CMD}"
+    done
 
-if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_NCL}" ]] ; then
+    ### cleanup
+
+    if [[ ! -r "${CMAQ_TARGET_FP_CMAQ}" ]] ; then
+      if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
+        ## copy to the extension CMAQ wants
+        for CMD in \
+          "mv ${CMAQ_TARGET_FP_NCL} ${CMAQ_TARGET_FP_CMAQ}" \
+        ; do
+          echo -e "$ ${CMD}"
+          eval "${CMD}"
+        done
+      else
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot read either CMAQ_TARGET_FP_NCL=='${CMAQ_TARGET_FP_NCL}' or CMAQ_TARGET_FP_CMAQ=='${CMAQ_TARGET_FP_CMAQ}'"
+        exit 99
+      fi
+    fi
+
+    if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_CMAQ}" ]] ; then
+      for CMD in \
+        "rm ${CMAQ_TARGET_FP_TEMPLATE_CMAQ}" \
+      ; do
+        echo -e "$ ${CMD}"
+        eval "${CMD}"
+      done
+      if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_CMAQ}" ]] ; then
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot delete CMAQ_TARGET_FP_TEMPLATE_CMAQ=='${CMAQ_TARGET_FP_TEMPLATE_CMAQ}'"
+        exit 41
+      fi
+    fi
+
+    if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_NCL}" ]] ; then
+      for CMD in \
+        "rm ${CMAQ_TARGET_FP_TEMPLATE_NCL}" \
+      ; do
+        echo -e "$ ${CMD}"
+        eval "${CMD}"
+      done
+      if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_NCL}" ]] ; then
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot delete CMAQ_TARGET_FP_TEMPLATE_NCL=='${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
+        exit 42
+      fi
+    fi
+
+    if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
+      for CMD in \
+        "rm ${CMAQ_TARGET_FP_NCL}" \
+      ; do
+        echo -e "$ ${CMD}"
+        eval "${CMD}"
+      done
+      if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
+        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot delete CMAQ_TARGET_FP_NCL=='${CMAQ_TARGET_FP_NCL}'"
+        exit 42
+      fi
+    fi
+  fi # end if [[ -r "${CMAQ_TARGET_FP_CMAQ}" ]]
+} # end function retemp_conserv
+
+function teardown {
+  # remove the *.nc required only for NCL
+  # # start debugging-----------------------------------------------------
+  # echo -e "${THIS_FN}: CMAQ_TARGET_FP_TEMPLATE_NCL= '${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
+  # echo -e "${THIS_FN}: CMAQ_TARGET_FP_NCL=          '${CMAQ_TARGET_FP_NCL}'"
+  # #   end debugging-----------------------------------------------------
+
+  ### show files: data and plots
   for CMD in \
-    "rm ${CMAQ_TARGET_FP_TEMPLATE_NCL}" \
+    "ls -alht ${WORK_DIR}/*.pdf" \
+    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT_CMAQ}" \
+    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT_NCL}" \
+    "ncdump -h ${CMAQ_TARGET_FP_CMAQ}" \
   ; do
     echo -e "$ ${CMD}"
     eval "${CMD}"
   done
-fi
-
-# TODO: pass commandline args to NCL
-# punt: just use envvars :-(
-
-# ${NCL_EXEC} bail to NCL and copy script lines, or ...
-# '-n' -> http://www.ncl.ucar.edu/Document/Functions/Built-in/print.shtml
-for CMD in \
-  "${NCL_EXEC} -n ${CALL_RETEMP_FP}" \
-; do
-cat <<EOM
-
-About to run command="${CMD}"
-
-EOM
-  eval "${CMD}"
-done
-
-# don't move, copy: NCL won't *read* .ncf either :-(
-#   "mv ${CMAQ_TARGET_FP_NCL} ${CMAQ_TARGET_FP_CMAQ}" \
-for CMD in \
-  "cp ${CMAQ_TARGET_FP_NCL} ${CMAQ_TARGET_FP_CMAQ}" \
-  "ls -alht ${WORK_DIR}/*.${CMAQ_TARGET_EXT_CMAQ}" \
-  "ls -alht ${WORK_DIR}/*.${CMAQ_TARGET_EXT_NCL}" \
-  "ncdump -h ${CMAQ_TARGET_FP_CMAQ}" \
-; do
-  echo -e "$ ${CMD}"
-  eval "${CMD}"
-done
+} # end function teardown
 
 # ----------------------------------------------------------------------
-# check conservation of mass input -> output
+# payload
 # ----------------------------------------------------------------------
 
-# TODO: pass commandline args to NCL
-# punt: just use envvars :-(
-
-# ${NCL_EXEC} bail to NCL and copy script lines, or ...
+# should always
+# * begin with `setup` setup paths, apps, helpers, resources
+# * end with `teardown` for tidy and testing (e.g., plot display)
+#   'setup' \
+#   'reunit' \
+#   'regrid' \
+#   'retemp_conserv' \
+#   'teardown' \
 for CMD in \
-  "${NCL_EXEC} -n ${CALL_CONSERV_FP}" \
+  'setup' \
+  'reunit' \
+  'regrid' \
+  'retemp_conserv' \
+  'teardown' \
 ; do
-cat <<EOM
-
-About to run command="${CMD}"
-
-EOM
-  eval "${CMD}"
+  if [[ -z "${CMD}" ]] ; then
+    echo -e "${THIS_FN}::main loop: ERROR: '${CMD}' not defined"
+    exit 43
+  else
+    echo -e "\n$ ${THIS_FN}::main loop: ${CMD}\n"
+    eval "${CMD}" # comment this out for NOPing, e.g., to `source`
+  fi
 done
 
 # ----------------------------------------------------------------------
-# cleanup/teardown
+# debugging
 # ----------------------------------------------------------------------
-
-# remove the *.nc required only for NCL
-
-# # start debugging-----------------------------------------------------
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_TEMPLATE_NCL= '${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
-# echo -e "${THIS_FN}: CMAQ_TARGET_FP_NCL=          '${CMAQ_TARGET_FP_NCL}'"
-# #   end debugging-----------------------------------------------------
-
-if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_NCL}" ]] ; then
-  for CMD in \
-    "rm ${CMAQ_TARGET_FP_TEMPLATE_NCL}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
-if [[ -r "${CMAQ_TARGET_FP_TEMPLATE_NCL}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot delete CMAQ_TARGET_FP_TEMPLATE_NCL=='${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
-  exit 11
-fi
-
-if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
-  for CMD in \
-    "rm ${CMAQ_TARGET_FP_NCL}" \
-  ; do
-    echo -e "$ ${CMD}"
-    eval "${CMD}"
-  done
-fi
-if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
-  echo -e "${THIS_FN}: ERROR: cannot delete CMAQ_TARGET_FP_NCL=='${CMAQ_TARGET_FP_NCL}'"
-  exit 12
-fi
-
-# ----------------------------------------------------------------------
-# show new annual hourly files
-# ----------------------------------------------------------------------
-
-for CMD in \
-  "ls -alht ${WORK_DIR}/*.${CMAQ_TARGET_EXT_CMAQ}" \
-  "ls -alht ${WORK_DIR}/*.${CMAQ_TARGET_EXT_NCL}" \
-  "ncdump -h ${CMAQ_TARGET_FP_CMAQ}" \
-; do
-  echo -e "$ ${CMD}"
-  eval "${CMD}"
-done