Commits

Tom Roche  committed ca5a986

Added regrid_vis.r (et al) to 2D-regrid EDGAR global (README step 2).

regrid_vis.r

* regrids the previously-reunit-ed EDGAR global inventory to AQMEII-NA
* plots the output

Also

* added support in AQMEII_driver.sh and uber_driver.sh
** refactored IOAPI/AQMEII-specific constants in AQMEII_driver.sh
* tweaked comments in reunit_vis_EDGAR_global.r
* updated README.md

Tested on terrae/EMVL from both 'GFED_driver.sh' and 'uber_driver.sh' (latter in fresh terminal).

TODO:
* AQMEII_driver.sh still has lots uncalled cruft from parent=GFED_driver.sh
* all regrids: how to nudge off/onshore as required? e.g., ag-soil emissions should never be offshore.
* all regrid maps: add Caribbean islands (esp Bahamas! for offshore burning), Canadian provinces, Mexican states
* test on
** tlrPanP5 (which now has R package=ncdf4, but readAsciiTable of input .txt's is very slow compared to terrae)
** HPCC (once problem with ncdf4 on amad1 is debugged: in process with JOB and KMF)

  • Participants
  • Parent commits cd8f7c6

Comments (0)

Files changed (5)

File AQMEII_driver.sh

 
 export TIME_FUNCS_FN='time.ncl'
 TIME_FUNCS_FP="${WORK_DIR}/${TIME_FUNCS_FN}"
-export VIS_REGRID_VIS_FN='vis_regrid_vis.r'
-VIS_REGRID_VIS_FP="${WORK_DIR}/${VIS_REGRID_VIS_FN}"
+export REGRID_VIS_FN='regrid_vis.r'
+REGRID_VIS_FP="${WORK_DIR}/${REGRID_VIS_FN}"
 export MAKE_HOURLIES_FN='make_hourlies.ncl'
 MAKE_HOURLIES_FP="${WORK_DIR}/${MAKE_HOURLIES_FN}"
 export STRING_FUNCS_FN='string.ncl'
 ## PROJ.4 string for unprojected data
 export GLOBAL_PROJ4='+proj=longlat +ellps=WGS84'
 
+### AQMEII-NA constants
+
+## file/global attributes
+AQMEII_NA_HISTORY="see ${PROJECT_WEBSITE}"
+# AQMEII_NA_SOURCE_FILE # varies
+# AQMEII_NA_TITLE # varies
+
+## datavar dims and attributes
+# TODO: get from template file or from EPIC
+AQMEII_NA_DV_NAME='N2O'
+# IOAPI pads varattr=long_name to length=16 with trailing spaces
+AQMEII_NA_DV_LONG_NAME="$(printf '%-16s' ${AQMEII_NA_DV_NAME})"
+# IOAPI pads varattr=units to length=16 with trailing spaces
+AQMEII_NA_DV_UNITS="$(printf '%-16s' 'moles/s')"
+# IOAPI pads varattr=var_desc to length=80 with trailing spaces
+AQMEII_NA_DV_VAR_DESC="$(printf '%-80s' 'Model species ${AQMEII_NA_DV_NAME}')"
+
+AQMEII_NA_DIM_LAYER_NAME='LAY'
+AQMEII_NA_DIM_LAYER_LONG_NAME='index of layers above surface'
+AQMEII_NA_DIM_LAYER_UNITS='unitless'
+
+AQMEII_NA_DIM_TIME_NAME='TSTEP'
+# AQMEII_NA_DIM_TIME_UNITS # vary
+# AQMEII_NA_DIM_TIME_LONG_NAME='timestep'
+
+AQMEII_NA_DIM_X_N='459'
+AQMEII_NA_DIM_X_NAME='COL'
+AQMEII_NA_DIM_X_UNITS='unitless' # my invention
+AQMEII_NA_DIM_X_LONG_NAME='grid column index from left' # my invention (TODO: CHECK it's not from right)
+
+AQMEII_NA_DIM_Y_N='299'
+AQMEII_NA_DIM_Y_NAME='ROW'
+AQMEII_NA_DIM_Y_UNITS='unitless' # my invention
+AQMEII_NA_DIM_Y_LONG_NAME='grid row index from top' # my invention (TODO: CHECK it's not from bottom)
+
 ### artifact visualization
 
 ## for visualization (generally)
 
 ### input: EDGAR N2O emissions
 
+AQMEII_EDGAR_N2O_GLOBAL_ZIP_HOMEPAGE='http://edgar.jrc.ec.europa.eu/datasets_grid_list.php?v=42&edgar_compound=N2O' # unfortunately require GUI manipulation, so instead use ...
 AQMEII_EDGAR_N2O_GLOBAL_ZIP_URI='https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1.zip'
 AQMEII_EDGAR_N2O_GLOBAL_ZIP_FN="$(basename ${AQMEII_EDGAR_N2O_GLOBAL_ZIP_URI})"
 AQMEII_EDGAR_N2O_GLOBAL_FN_ROOT="${AQMEII_EDGAR_N2O_GLOBAL_ZIP_FN%.*}" # everything left of the LAST dot
 
 ### intermediate product: reunit-ed global EDGAR N2O
 
-AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN="${AQMEII_EDGAR_N2O_GLOBAL_FN_ROOT}_reunit.${NETCDF_EXT}"
+AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN_ROOT="${AQMEII_EDGAR_N2O_GLOBAL_FN_ROOT}_reunit"
+AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN="${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN_ROOT}.${NETCDF_EXT}"
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP="${WORK_DIR}/${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN}"
+
+## file/global attributes
+export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_HISTORY="${AQMEII_NA_HISTORY}"
+export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_SOURCE_FILE="EDGAR N2O emissions from agricultural soils for ${MODEL_YEAR} from ${AQMEII_EDGAR_N2O_GLOBAL_ZIP_HOMEPAGE}"
+export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_TITLE="global EDGAR N2O emissions from agricultural soils for ${MODEL_YEAR} in ${AQMEII_NA_DV_UNITS}"
+
+## datavar and attributes
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_NAME="${AQMEII_EDGAR_N2O_GLOBAL_DV_NAME}"
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_TYPE="${AQMEII_EDGAR_N2O_GLOBAL_DV_TYPE}"
-export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_UNITS='mol/s' # hopefully :-)
+export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_UNITS="${AQMEII_NA_DV_UNITS}" # hopefully :-)
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_LONG_NAME="${AQMEII_EDGAR_N2O_GLOBAL_DV_LONG_NAME}"
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_COORD_X_NAME="${AQMEII_EDGAR_N2O_GLOBAL_DV_COORD_X_NAME}"
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_COORD_Y_NAME="${AQMEII_EDGAR_N2O_GLOBAL_DV_COORD_Y_NAME}"
+# do I want to rotate its longitudes? i.e. from 0-360 -> -180-+180
+export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_ROTATE_LONS='FALSE'
 
 ### intermediate product: plot of reunit-ed global EDGAR N2O
 
 AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FN="${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FN_ROOT}.pdf"
 export AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FP="${WORK_DIR}/${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FN}"
 
+### intermediate product: global->AQMEII regridding of re-united EDGAR emissions
+
+AQMEII_EDGAR_N2O_REGRID_FN_ROOT="${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FN_ROOT}_regrid"
+AQMEII_EDGAR_N2O_REGRID_FN="${AQMEII_EDGAR_N2O_REGRID_FN_ROOT}.${NETCDF_EXT}"
+export AQMEII_EDGAR_N2O_REGRID_FP="${WORK_DIR}/${AQMEII_EDGAR_N2O_REGRID_FN}"
+
+## file/global attributes
+export AQMEII_EDGAR_N2O_REGRID_HISTORY="${AQMEII_NA_HISTORY}"
+export AQMEII_EDGAR_N2O_REGRID_SOURCE_FILE="${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}"
+export AQMEII_EDGAR_N2O_REGRID_TITLE="EDGAR N2O emissions from agricultural soils for ${MODEL_YEAR} in ${AQMEII_NA_DV_UNITS} 2D-regridded to AQMEII-NA"
+
+## datavar and attributes
+
+# datavar attributes
+export AQMEII_EDGAR_N2O_REGRID_DV_NAME="${AQMEII_NA_DV_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DV_UNITS="${AQMEII_NA_DV_UNITS}"
+export AQMEII_EDGAR_N2O_REGRID_DV_LONG_NAME="${AQMEII_NA_DV_LONG_NAME}"
+
+# datavar dims=(TSTEP, LAY, ROW, COL)
+
+export AQMEII_EDGAR_N2O_REGRID_DIM_LAYER_NAME="${AQMEII_NA_DIM_LAYER_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_LAYER_UNITS="${AQMEII_NA_DIM_LAYER_UNITS}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_LAYER_LONG_NAME="${AQMEII_NA_DIM_LAYER_LONG_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_LAYER_N='1'
+
+export AQMEII_EDGAR_N2O_REGRID_DIM_TIME_NAME="${AQMEII_NA_DIM_TIME_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_TIME_UNITS="${AQMEII_NA_DIM_TIME_UNITS}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_TIME_LONG_NAME="${AQMEII_NA_DIM_TIME_LONG_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_TIME_N='1'
+
+export AQMEII_EDGAR_N2O_REGRID_DIM_X_NAME="${AQMEII_NA_DIM_X_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_X_UNITS="${AQMEII_NA_DIM_X_UNITS}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_X_LONG_NAME="${AQMEII_NA_DIM_X_LONG_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_X_N="${AQMEII_NA_DIM_X_N}"
+
+export AQMEII_EDGAR_N2O_REGRID_DIM_Y_NAME="${AQMEII_NA_DIM_Y_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_Y_UNITS="${AQMEII_NA_DIM_Y_UNITS}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_Y_LONG_NAME="${AQMEII_NA_DIM_Y_LONG_NAME}"
+export AQMEII_EDGAR_N2O_REGRID_DIM_Y_N="${AQMEII_NA_DIM_Y_N}"
+
+### intermediate product: plot of regridded EDGAR N2O
+
+AQMEII_EDGAR_N2O_REGRID_PDF_FN_ROOT="${AQMEII_EDGAR_N2O_REGRID_FN%.*}" # everything left of the LAST dot
+AQMEII_EDGAR_N2O_REGRID_PDF_FN="${AQMEII_EDGAR_N2O_REGRID_PDF_FN_ROOT}.pdf"
+export AQMEII_EDGAR_N2O_REGRID_PDF_FP="${WORK_DIR}/${AQMEII_EDGAR_N2O_REGRID_PDF_FN}"
+
 # ======================================================================
 
 ### intermediate product: output/AQMEII-NA gridcell areas
 export GFED_AREAS_AQMEIINA_DIM_COL_NAME='COL' # long_name, units like input
 export GFED_AREAS_AQMEIINA_HISTORY="see ${PROJECT_WEBSITE}"
 
-### intermediate product: netCDFed aggregation of molarized GFED global monthly emissions
-GFED_GLOBAL_MONTHLY_CONV_FN_ROOT="${GFED_OUTPUT_VERSION}_${MODEL_YEAR}_N2O_monthly_emissions"
-GFED_GLOBAL_MONTHLY_CONV_FN="${GFED_GLOBAL_MONTHLY_CONV_FN_ROOT}.${NETCDF_EXT}"
-export GFED_GLOBAL_MONTHLY_CONV_FP="${WORK_DIR}/${GFED_GLOBAL_MONTHLY_CONV_FN}"
-## file/global attributes
-export GFED_GLOBAL_MONTHLY_CONV_HISTORY="see ${PROJECT_WEBSITE}"
-export GFED_GLOBAL_MONTHLY_CONV_SOURCE_FILE="assembled from data from ${GFED_GLOBAL_MONTHLY_ZIP_URI}"
-export GFED_GLOBAL_MONTHLY_CONV_TITLE="monthly GFED N2O emissions for ${MODEL_YEAR}"
-
-## datavar
-export GFED_GLOBAL_MONTHLY_CONV_DATAVAR_NAME='N2O'
-export GFED_GLOBAL_MONTHLY_CONV_DATAVAR_UNITS='molN20/mo'
-export GFED_GLOBAL_MONTHLY_CONV_DATAVAR_LONG_NAME='N2O emissions'
-# datavar dims=(TSTEP, LAY, ROW, COL)
-export GFED_GLOBAL_MONTHLY_CONV_N_TSTEP="${MONTHS_PER_YEAR}"
-export GFED_GLOBAL_MONTHLY_CONV_N_LAYER='1' # one species in file
-# still global -> same rows, cols as input .txt
-export GFED_GLOBAL_MONTHLY_CONV_N_ROW="${GFED_GLOBAL_MONTHLY_TXT_ROWS}"
-export GFED_GLOBAL_MONTHLY_CONV_N_COL="${GFED_GLOBAL_MONTHLY_TXT_COLS}"
-
-# datavar dims and attributes
-export GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_NAME='TSTEP'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_UNITS='month'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_LONG_NAME="index of month in ${MODEL_YEAR}"
-export GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_NAME='LAY'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_LONG_NAME='index of vertical layers'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_UNITS='unitless'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_X_NAME='COL'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_X_UNITS='longitude'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_X_LONG_NAME='degrees_east'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_Y_NAME='ROW'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_Y_UNITS='degrees_north'
-export GFED_GLOBAL_MONTHLY_CONV_DIM_Y_LONG_NAME='latitude'
-
-### intermediate product: aggregation of GFED global daily fractions
-GFED_GLOBAL_DAILY_FRAC_CONV_FN_ROOT="${GFED_OUTPUT_VERSION}_${MODEL_YEAR}_N2O_daily_fractions"
-GFED_GLOBAL_DAILY_FRAC_CONV_FN="${GFED_GLOBAL_DAILY_FRAC_CONV_FN_ROOT}.${NETCDF_EXT}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_FP="${WORK_DIR}/${GFED_GLOBAL_DAILY_FRAC_CONV_FN}"
-## file/global attributes
-export GFED_GLOBAL_DAILY_FRAC_CONV_HISTORY="see ${PROJECT_WEBSITE}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_SOURCE_FILE="assembled from data from ${GFED_GLOBAL_DAILY_FRAC_GZ_URI}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_TITLE="daily GFED N2O fractions for ${MODEL_YEAR}"
-
-## datavar
-export GFED_GLOBAL_DAILY_FRAC_CONV_DATAVAR_NAME="${GFED_GLOBAL_DAILY_FRAC_DATAVAR_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DATAVAR_UNITS='unitless'
-export GFED_GLOBAL_DAILY_FRAC_CONV_DATAVAR_LONG_NAME='daily fraction of monthly N2O emission'
-# datavar dims=(TSTEP, LAY, ROW, COL)
-# export GFED_GLOBAL_DAILY_FRAC_CONV_N_TSTEP="${DAYS_PER_YEAR}" # calculate
-export GFED_GLOBAL_DAILY_FRAC_CONV_N_LAYER='1' # one species in file
-# still global -> same rows, cols as input .txt
-export GFED_GLOBAL_DAILY_FRAC_CONV_N_ROW="${GFED_GLOBAL_MONTHLY_TXT_ROWS}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_N_COL="${GFED_GLOBAL_MONTHLY_TXT_COLS}"
-
-# datavar dims and attributes
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_TIME_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_TIME_UNITS='day'
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_TIME_LONG_NAME="index of day in ${MODEL_YEAR}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_LAYER_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_LAYER_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_LONG_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_LAYER_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_UNITS}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_X_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_X_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_UNITS}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_X_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_LONG_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_Y_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_NAME}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_Y_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_UNITS}"
-export GFED_GLOBAL_DAILY_FRAC_CONV_DIM_Y_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_LONG_NAME}"
-
-### intermediate product: aggregation of GFED global 3hourly fractions
-GFED_GLOBAL_3HOURLY_FRAC_CONV_FN_ROOT="${GFED_OUTPUT_VERSION}_${MODEL_YEAR}_N2O_3hourly_fractions"
-GFED_GLOBAL_3HOURLY_FRAC_CONV_FN="${GFED_GLOBAL_3HOURLY_FRAC_CONV_FN_ROOT}.${NETCDF_EXT}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_FP="${WORK_DIR}/${GFED_GLOBAL_3HOURLY_FRAC_CONV_FN}"
-## file/global attributes
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_HISTORY="see ${PROJECT_WEBSITE}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_SOURCE_FILE="assembled from data from ${GFED_GLOBAL_3HOURLY_FRAC_GZ_URI}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_TITLE="3hourly GFED N2O fractions for ${MODEL_YEAR}"
-
-## TODO: figure out how to make `raster` handle/regrid 4D datavars.
-## Meanwhile, tell it how to find dim=TSTEP. Note
-## * R uses Fortran-style indexing
-## * netCDF reverses the dimensions under the hood
-## , so `float Fraction_of_Emissions(TSTEP, LAY, ROW, COL)` -> position in the dimension array(TSTEP)==4
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_TSTEP_POSN='4'
-
-## datavar
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DATAVAR_NAME="${GFED_GLOBAL_3HOURLY_FRAC_DATAVAR_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DATAVAR_UNITS='unitless'
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DATAVAR_LONG_NAME='3-hourly fraction of daily N2O emission'
-# datavar dims=(TSTEP, LAY, ROW, COL)
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_N_TSTEP="${MONTHS_PER_YEAR}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_N_LAYER='8' # 8 3-hour periods per file
-# still global -> same rows, cols as input .txt
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_N_ROW="${GFED_GLOBAL_MONTHLY_TXT_ROWS}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_N_COL="${GFED_GLOBAL_MONTHLY_TXT_COLS}"
-
-# datavar dims and attributes
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_TIME_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_TIME_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_UNITS}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_TIME_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_LONG_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_LAYER_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_LAYER_LONG_NAME='hour of day starting 3-hour-long period'
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_LAYER_UNITS='hour of day'
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_X_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_X_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_UNITS}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_X_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_LONG_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_Y_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_NAME}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_Y_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_UNITS}"
-export GFED_GLOBAL_3HOURLY_FRAC_CONV_DIM_Y_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_LONG_NAME}"
-
-### regrid constants
-export GFED_REGRID_N_ROW='299'
-export GFED_REGRID_N_COL='459'
-
-### intermediate product: global->AQMEII regridding of aggregated, molarized monthly emissions
-GFED_REGRID_MONTHLY_FN_ROOT="${GFED_GLOBAL_MONTHLY_CONV_FN_ROOT}_regrid"
-GFED_REGRID_MONTHLY_FN="${GFED_REGRID_MONTHLY_FN_ROOT}.${NETCDF_EXT}"
-export GFED_REGRID_MONTHLY_FP="${WORK_DIR}/${GFED_REGRID_MONTHLY_FN}"
-## file/global attributes
-export GFED_REGRID_MONTHLY_HISTORY="${GFED_GLOBAL_MONTHLY_CONV_HISTORY}"
-export GFED_REGRID_MONTHLY_SOURCE_FILE="${GFED_GLOBAL_MONTHLY_CONV_SOURCE_FILE}"
-export GFED_REGRID_MONTHLY_TITLE="${GFED_GLOBAL_MONTHLY_CONV_TITLE}"
-
-## datavar
-export GFED_REGRID_MONTHLY_DATAVAR_NAME="${GFED_GLOBAL_MONTHLY_CONV_DATAVAR_NAME}"
-export GFED_REGRID_MONTHLY_DATAVAR_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DATAVAR_UNITS}"
-export GFED_REGRID_MONTHLY_DATAVAR_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DATAVAR_LONG_NAME}"
-# datavar dims=(TSTEP, LAY, ROW, COL)
-export GFED_REGRID_MONTHLY_N_TSTEP="${GFED_GLOBAL_MONTHLY_CONV_N_TSTEP}"
-export GFED_REGRID_MONTHLY_N_LAYER="${GFED_GLOBAL_MONTHLY_CONV_N_LAYER}"
-# no longer global
-# export GFED_REGRID_MONTHLY_N_ROW=""
-# export GFED_REGRID_MONTHLY_N_COL=""
-
-# datavar dims and attributes
-export GFED_REGRID_MONTHLY_DIM_TIME_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_NAME}"
-export GFED_REGRID_MONTHLY_DIM_TIME_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_UNITS}"
-export GFED_REGRID_MONTHLY_DIM_TIME_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_TIME_LONG_NAME}"
-export GFED_REGRID_MONTHLY_DIM_LAYER_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_NAME}"
-export GFED_REGRID_MONTHLY_DIM_LAYER_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_LONG_NAME}"
-export GFED_REGRID_MONTHLY_DIM_LAYER_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_LAYER_UNITS}"
-export GFED_REGRID_MONTHLY_DIM_X_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_NAME}"
-export GFED_REGRID_MONTHLY_DIM_X_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_UNITS}"
-export GFED_REGRID_MONTHLY_DIM_X_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_X_LONG_NAME}"
-export GFED_REGRID_MONTHLY_DIM_Y_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_NAME}"
-export GFED_REGRID_MONTHLY_DIM_Y_UNITS="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_UNITS}"
-export GFED_REGRID_MONTHLY_DIM_Y_LONG_NAME="${GFED_GLOBAL_MONTHLY_CONV_DIM_Y_LONG_NAME}"
-
 ### intermediate product: global->AQMEII regridding of aggregated daily fractions
 GFED_REGRID_DAILY_FRAC_FN_ROOT="${GFED_GLOBAL_DAILY_FRAC_CONV_FN_ROOT}_regrid"
 GFED_REGRID_DAILY_FRAC_FN="${GFED_REGRID_DAILY_FRAC_FN_ROOT}.${NETCDF_EXT}"
 #  echo -e "\n$ ${THIS_FN}: PDF_VIEWER='${PDF_VIEWER}'" # debugging
 } # end function setup
 
-#     'get_vis_regrid_vis' \
 #     'get_check_conservation' \
 #     'get_process_EPIC' \
 #     'get_overlay_EPIC' \
     'get_reunit_vis_EDGAR_global' \
     'get_stats_funcs' \
     'get_vis_funcs' \
+    'get_regrid_vis' \
   ; do
     if [[ -z "${CMD}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
   fi
 } # end function get_reunit_vis_EDGAR_global
 
+function get_regrid_vis {
+  if [[ -z "${REGRID_VIS_FP}" ]] ; then
+    echo -e "${THIS_FN}: ERROR: REGRID_VIS_FP not defined"
+    exit 1
+  fi
+  # is in this repo
+#  if [[ ! -r "${REGRID_VIS_FP}" ]] ; then
+#    for CMD in \
+#      "${WGET_TO_FILE} ${REGRID_VIS_FP} ${REGRID_VIS_URI}" \
+#    ; do
+#      echo -e "$ ${CMD}"
+#      eval "${CMD}"
+#    done
+#  fi
+  if [[ ! -r "${REGRID_VIS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: REGRID_VIS_FP=='${REGRID_VIS_FP}' not readable"
+    exit 1
+  fi
+} # end function get_regrid_vis
+
 function get_make_hourlies {
   if [[ -z "${MAKE_HOURLIES_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: MAKE_HOURLIES_FP not defined"
   fi
 } # end function get_template
 
-#     'get_template' \
 #     'get_output_areas' \
 function download_inputs {
   for CMD in \
     'download_EDGAR_N2O' \
     'download_EDGAR_areas' \
     'download_EPIC' \
+    'get_template' \
   ; do
     if [[ -z "${CMD}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
     ; do
     cat <<EOM
 
-About to run command='${CMD}'
+About to run command='${CMD}'. WARNING: may seem to hang while processing!
 
 EOM
       eval "${CMD}"
   fi # checking readability of input
 } # end function reunit_vis_EDGAR_global
 
-### Depends on function get_vis_regrid_vis above.
+### Depends on function get_regrid_vis above.
 ### Assumes `Rscript` is in PATH: see bash_utilities.sh
 ### TODO: pass commandline args to R
 ### for now: just use envvars :-(
-function vis_regrid_vis {
-  if [[ -z "${GFED_REGRID_MONTHLY_FP}" ]] ; then
-    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GFED_REGRID_MONTHLY_FP not defined"
+function regrid_vis {
+  if [[ -z "${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP not defined"
+    exit 1
+  elif [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: input file='${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}' not readable"
     exit 1
   fi
-  if [[ -z "${GFED_REGRID_DAILY_FRAC_FP}" ]] ; then
-    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GFED_REGRID_DAILY_FRAC_FP not defined"
+  if [[ -z "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_REGRID_FP not defined"
     exit 1
   fi
-  if [[ -z "${GFED_REGRID_3HOURLY_FRAC_FP}" ]] ; then
-    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GFED_REGRID_3HOURLY_FRAC_FP not defined"
+  if [[ -z "${AQMEII_EDGAR_N2O_REGRID_PDF_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_REGRID_PDF_FP not defined"
     exit 1
   fi
 
-# eval "rm ${GFED_REGRID_MONTHLY_FP}"
+# eval "rm ${AQMEII_EDGAR_N2O_REGRID_FP}"
 # R # bail to R console and copy script lines, or ...
-  if [[ -r "${GFED_REGRID_MONTHLY_FP}" &&
-        -r "${GFED_REGRID_DAILY_FRAC_FP}" &&
-        -r "${GFED_REGRID_3HOURLY_FRAC_FP}" ]] ; then
-    echo -e "${THIS_FN}::${FUNCNAME[0]}: skipping regrid: monthly, daily, and 3-hourly regrid files all readable"
-  else # make them: but since R::raster can't seem to regrid 4D,
-    # use NCL to reassemble the separately-regridded 3hourlies
-# ncl
+  if [[ -r "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: skipping ${REGRID_VIS_FN}: output='${AQMEII_EDGAR_N2O_REGRID_FP}' is readable"
+  else # make it
     for CMD in \
-      "Rscript ${VIS_REGRID_VIS_FP}" \
-      "ncl -n ${FIX_3HOURLIES_FP}" \
+      "Rscript ${REGRID_VIS_FP}" \
     ; do
     cat <<EOM
 
 EOM
       eval "${CMD}"
     done
-    for CMD in \
-      "${PDF_VIEWER} ${AQMEII_EDGAR_N2O_GLOBAL_CONV_PDF_FP} &" \
-      "ncdump -h ${GFED_REGRID_MONTHLY_FP}" \
-      "${PDF_VIEWER} ${GFED_REGRID_MONTHLY_PDF_FP} &" \
-      "ncdump -h ${GFED_REGRID_DAILY_FRAC_FP}" \
-      "ncdump -h ${GFED_REGRID_3HOURLY_FRAC_FP}" \
-    ; do
-      echo -e "$ ${CMD}"
-      eval "${CMD}"
-    done
-  fi
-} # end function vis_regrid_vis
+
+    if [[ ! -r "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: output file='${AQMEII_EDGAR_N2O_REGRID_FP}' not readable"
+      exit 1
+    elif [[ ! -r "${AQMEII_EDGAR_N2O_REGRID_PDF_FP}" ]] ; then
+      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: plot file='${AQMEII_EDGAR_N2O_REGRID_PDF_FP}' not readable"
+      exit 1
+    else
+#	"ncdump -h ${AQMEII_EDGAR_N2O_REGRID_FP}" \ # do this in script
+      for CMD in \
+	"${PDF_VIEWER} ${AQMEII_EDGAR_N2O_REGRID_PDF_FP} &" \
+      ; do
+	echo -e "\n$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}\n"
+	eval "${CMD}" # comment this out for NOPing, e.g., to `source`
+      done
+    fi # checking readability of outputs
+  fi # checking readability of input
+} # end function regrid_vis
 
 ### Call NCL helper to check the presence and orientation of coordinate variables in the outputs of
 
 ### * conv_monthlies
 ### * conv_dailies
 ### * conv_3hourlies
-### * vis_regrid_vis
+### * regrid_vis
 ### * get_template
 
 ### Depends on function get_check_coord_vars above.
 #  'setup' \                 # paths, helpers, etc
 #  'download_inputs' \       # get raw inputs from the relevant (or achievable) repository
 #  'reunit_vis_EDGAR_global' # reunit flux rate -> mol rate, visualize after
-#  'vis_regrid_vis' \        # regrid global -> AQMEII, visualize after
+#  'regrid_vis' \            # regrid global -> AQMEII, visualize after
 #  'check_conservation' \    # of mass from inputs to outputs
 #  'process_EPIC' \          # EPIC values replace EDGAR where both exist
 #  'overlay_EPIC' \          # EPIC values replace EDGAR where both exist
   'setup' \
   'download_inputs' \
   'reunit_vis_EDGAR_global' \
+  'regrid_vis' \
 ; do
   if [[ -z "${CMD}" ]] ; then
     echo -e "${THIS_FN}::main loop: ERROR: '${CMD}' not defined"
 
 1. *(completed)* "reunit" an [EDGAR-4.2][EDGAR 4.2 overview] N2O emission inventory (in [netCDF][] format) for agricultural soils from (per-gridcell) flux rate (`kg/m^2/s`) to molar-mass rate (`mol/s`, which [CMAQ][CMAQ @ CMAS] wants anyway) prior to regridding.
     1. [... and visualize it][visualization of global EDGAR].
-1. *(in process)* 2D-regrid the EDGAR inventory from global/unprojected to a projected subdomain ([AQMEII-NA][]).
+1. *(completed)* 2D-regrid the EDGAR inventory from global/unprojected to a projected subdomain ([AQMEII-NA][]).
     1. [... and visualize it][visualization of AQMEII-NA EDGAR].
-1. *(planned)* Overlay the EDGAR data with [EPIC][EPIC overview] data on CONUS ag soils.
+1. *(in process)* Overlay the EDGAR data with [EPIC][EPIC overview] data on CONUS ag soils.
     1. Process the raw EPIC data.
     1. For each gridcell in AQMEII-NA: if it has values for both EDGAR and EPIC, use only the EPIC value. 
     1. [Visualize the combined AQMEII-NA data][visualization of AQMEII-NA combined].
 [AQMEII-NA]: https://github.com/TomRoche/cornbeltN2O/wiki/AQMEII-North-American-domain
 [EPIC overview]: TODO!
 [visualization of global EDGAR]: https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit.pdf
-[visualization of AQMEII-NA EDGAR]: TODO!
+[visualization of AQMEII-NA EDGAR]: https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit_regrid.pdf
+[visualization of AQMEII-NA EPIC]: TODO!
 [visualization of AQMEII-NA combined]: TODO!
 [CMAQ @ CMAS]: http://www.cmaq-model.org/
 [hourly emissions for 1 Jan 2008]: TODO!
 1. Run the driver:
     `$ ./AQMEII_driver.sh`
     This will download input, then run ...
-    1. [reunit_vis_EDGAR_global.r][], which will prepare the [raw EDGAR input][global ag soil N2O from EDGAR] (unfortunately not directly downloadable from EDGAR) for regridding, and [plot it][visualization of global EDGAR].
+    1. [reunit_vis_EDGAR_global.r][], which will prepare the [raw EDGAR input][global ag soil N2O from EDGAR in flux-rate units] (unfortunately not directly downloadable from EDGAR) for regridding, and [plot it][visualization of global EDGAR].
+    1. [regrid_vis.r][], which will 2D-regrid the [reunit-ed global EDGAR][] (from the previous step) to [AQMEII-NA], and [plot it][visualization of AQMEII-NA EDGAR].
 
 [uber_driver.sh]: https://bitbucket.org/tlroche/aqmeii_ag_soil/src/4b49b006625c9019b6861d7a264453a512dd3ada/uber_driver.sh?at=master
 [AQMEII_driver.sh]: https://bitbucket.org/tlroche/aqmeii_ag_soil/src/4b49b006625c9019b6861d7a264453a512dd3ada/AQMEII_driver.sh?at=master
 [reunit_vis_EDGAR_global.r]: https://bitbucket.org/tlroche/aqmeii_ag_soil/src/8456b5f881906fccf287d8dd60b726a3c496c254/reunit_vis_EDGAR_global.r?at=master
-[global ag soil N2O from EDGAR]: https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1.zip
+[global ag soil N2O from EDGAR in flux-rate units]: https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1.zip
+[regrid_vis.r]: TODO!
+[global ag soil N2O from EDGAR in mole-rate units]: https://bitbucket.org/tlroche/aqmeii_ag_soil/downloads/v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit.nc.gz

File regrid_vis.r

+# R code to
+# * 2D-regrid an EDGAR global/unprojected inventory to AQMEII-NA, an LCC-projected subdomain
+# * visualize it
+
+# If running manually in R console, remember to run setup actions: `source ./AQMEII_driver.sh`
+
+# Code closely resembles ./reunit_vis_EDGAR_global.r, GFED-3.1_global_to_AQMEII-NA/vis_regrid_vis.r
+# TODO: refactor, package
+
+# ----------------------------------------------------------------------
+# constants
+# ----------------------------------------------------------------------
+
+# all the following env vars must be set and exported in driver script
+# TODO: pass as args!
+
+### miscellany
+
+## kludge for my clumsy namespacing
+my_this_fn <- Sys.getenv('REGRID_VIS_FN')
+this_fn <- my_this_fn
+
+work_dir <- Sys.getenv('WORK_DIR')
+
+## projection
+global_proj4 <- Sys.getenv('GLOBAL_PROJ4')
+
+## visualization
+sigdigs <- as.numeric(Sys.getenv('OUTPUT_SIGNIFICANT_DIGITS'))
+
+# helpers
+stats_funcs_fp <- Sys.getenv('STATS_FUNCS_FP')
+vis_funcs_fp <- Sys.getenv('VIS_FUNCS_FP')
+
+# plotting
+pdf_er <- Sys.getenv('PDF_VIEWER')
+pdf_height <- as.numeric(Sys.getenv('SINGLE_FRAME_PLOT_HEIGHT'))
+pdf_width <- as.numeric(Sys.getenv('SINGLE_FRAME_PLOT_WIDTH'))
+
+### input: reunit-ed EDGAR global data
+
+in_fp <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP')
+in_datavar_name <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_NAME')
+in_datavar_type <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_TYPE')
+in_datavar_units <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_UNITS')
+in_datavar_long_name <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_LONG_NAME')
+in_datavar_coord_x_name <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_COORD_X_NAME')
+in_datavar_coord_y_name <- Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_DV_COORD_Y_NAME')
+in_raster_rotate <- as.logical( Sys.getenv('AQMEII_EDGAR_N2O_GLOBAL_REUNIT_ROTATE_LONS'))
+
+### input: CMAQ AQMEII-NA template file
+
+template_input_fp <- Sys.getenv('TEMPLATE_INPUT_FP')
+template_datavar_name <- Sys.getenv('TEMPLATE_DATAVAR_NAME')
+
+### output: EDGAR data regridded from global/unprojected to AQMEII-NA, an LCC-projected subdomain
+
+out_fp <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_FP')
+out_datavar_name <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DV_NAME')
+out_datavar_type <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DV_TYPE')
+out_datavar_units <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DV_UNITS')
+out_datavar_long_name <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DV_LONG_NAME')
+out_datavar_dim_x_name <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DIM_X_NAME')
+out_datavar_dim_y_name <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DIM_Y_NAME')
+out_datavar_dim_z_name <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DIM_TIME_NAME')
+out_datavar_dim_z_units <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_DIM_TIME_UNITS')
+
+### plotting regridded EDGAR data
+
+out_pdf_fp <- Sys.getenv('AQMEII_EDGAR_N2O_REGRID_PDF_FP')
+
+# ----------------------------------------------------------------------
+# functions
+# ----------------------------------------------------------------------
+
+# start debugging-------------------------------------------------------
+cat(sprintf('%s: about to load stats script=%s\n', this_fn, stats_funcs_fp))
+cat(sprintf('%s: about to load vis script=%s\n', this_fn, vis_funcs_fp))
+#   end debugging-------------------------------------------------------
+
+source(stats_funcs_fp) # in script, produces errant error=
+#> netCDF.stats.to.stdout.r: no arguments supplied, exiting
+source(vis_funcs_fp)
+
+# ----------------------------------------------------------------------
+# code
+# ----------------------------------------------------------------------
+
+# ----------------------------------------------------------------------
+# setup
+# ----------------------------------------------------------------------
+
+# accelerate R graphics over SSH, per Adam Wilson
+# http://planetflux.adamwilson.us/2012/03/r-graphics-via-ssh.html
+X11.options(type="Xlib")
+
+### visualization
+
+### input visualization
+global.crs <- sp::CRS(global_proj4)
+
+### output visualization
+
+# output coordinate reference system:
+# use package=M3 to get CRS from template file
+library(M3)
+out.proj4 <- M3::get.proj.info.M3(template_input_fp)
+# cat(sprintf('out.proj4=%s\n', out.proj4)) # debugging
+# out.proj4=+proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +a=6370000 +b=6370000
+out.crs <- sp::CRS(out.proj4)
+
+# ----------------------------------------------------------------------
+# payload
+# ----------------------------------------------------------------------
+
+# ----------------------------------------------------------------------
+# load input: reunit-ed global EDGAR
+# ----------------------------------------------------------------------
+
+cat(sprintf('%s: creating input raster\n', this_fn)) # debugging
+library(raster)
+in.raster <- raster::raster(in_fp, varname=in_datavar_name)
+
+if (in_raster_rotate) {
+  in.raster <- rotate(
+    in.raster,      # )
+    overwrite=TRUE) # else levelplot does one layer per page?
+}
+
+# # start debugging-----------------------------------------------------
+# in.raster
+# # class       : RasterLayer 
+# # dimensions  : 1800, 3600, 6480000  (nrow, ncol, ncell)
+# # resolution  : 0.1, 0.1  (x, y)
+# # extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
+# # coord. ref. : +proj=longlat +datum=WGS84 
+# # data source : .../v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit.nc 
+# # names       : Emissions.of.N2O.. 
+# # zvar        : emi_n2o 
+
+# in.data <- values(in.raster)
+# in.data.n <- length(in.data)
+# in.data.obs <- c(in.data[!is.na(in.data)])
+# in.data.obs.n <- length(in.data.obs)
+
+# stats.to.stdout(
+#   data.nonNA=in.data.obs,
+#   data.nonNA.n=in.data.obs.n,
+#   data.raw.n=in.data.n,
+#   sig.digs=sigdigs,
+#   title='in.raster data:')
+# # in.raster data:
+# #     cells=6480000
+# #     obs=6480000
+# #     min=0
+# #     q1=0
+# #     med=0
+# #     mean=0.000518
+# #     q3=0
+# #     max=1
+# #     sum=3.35e+03
+# #   end debugging-----------------------------------------------------
+
+# ----------------------------------------------------------------------
+# load regridding template
+# ----------------------------------------------------------------------
+
+# # start debugging
+# # long IOAPI schema and I don't care about its layers
+# cmd <- sprintf('ncdump -h %s | head -n 13', template_input_fp)
+# cat(sprintf('\n: template file: %s==\n', cmd))
+# system(cmd)
+# # netcdf emis_mole_all_20080101_12US1_cmaq_cb05_soa_2008ab_08c.EXTENTS_INPUT {
+# # dimensions:
+# #       TSTEP = UNLIMITED ; // (25 currently)
+# #       LAY = 1 ;
+# #       ROW = 299 ;
+# #       COL = 459 ;
+# # variables:
+# #       float emi_n2o(TSTEP, LAY, ROW, COL) ;
+# #               emi_n2o:long_name = "XYL             " ;
+# #               emi_n2o:units = "moles/s         " ;
+# #               emi_n2o:var_desc = "Model species XYL                                                               " ;
+# # 
+# # // global attributes:
+# #   end debugging
+
+# use package=M3 to get extents from template file (thanks CGN!)
+extents.info <- M3::get.grid.info.M3(template_input_fp)
+extents.xmin <- extents.info$x.orig
+extents.xmax <- max(
+  M3::get.coord.for.dimension(
+    file=template_input_fp, dimension="col", position="upper", units="m")$coords)
+extents.ymin <- extents.info$y.orig
+extents.ymax <- max(
+  M3::get.coord.for.dimension(
+    file=template_input_fp, dimension="row", position="upper", units="m")$coords)
+grid.res <- c(extents.info$x.cell.width, extents.info$y.cell.width) # units=m
+
+template.extents <-
+  raster::extent(extents.xmin, extents.xmax, extents.ymin, extents.ymax)
+# template.extents # debugging
+# # class       : Extent 
+# # xmin        : -2556000 
+# # xmax        : 2952000 
+# # ymin        : -1728000 
+# # ymax        : 1860000 
+
+template.in.raster <-
+  raster::raster(template_input_fp, varname=template_datavar_name)
+template.raster <- raster::projectExtent(template.in.raster, crs=out.crs)
+#> Warning message:
+#> In projectExtent(template.in.raster, out.proj4) :
+#>   158 projected point(s) not finite
+# is that "projected point(s) not finite" warning important? Probably not, per Hijmans
+
+# without this, extents aren't correct!
+template.raster@extent <- template.extents
+# should resemble the domain specification @
+# https://github.com/TomRoche/cornbeltN2O/wiki/AQMEII-North-American-domain#wiki-EPA
+
+# # start debugging
+# cat('\ntemplate.raster==\n')
+# template.raster
+# # class       : RasterLayer 
+# # dimensions  : 299, 459, 137241  (nrow, ncol, ncell)
+# # resolution  : 12000, 12000  (x, y)
+# # extent      : -2556000, 2952000, -1728000, 1860000  (xmin, xmax, ymin, ymax)
+# # coord. ref. : +proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +a=6370000 +b=6370000 
+# #   end debugging
+
+# ----------------------------------------------------------------------
+# regrid
+# ----------------------------------------------------------------------
+
+cat(sprintf(  # debugging
+  '\n%s: regridding global -> AQMEII-NA (may take awhile)\n', this_fn))
+out.raster <-
+  raster::projectRaster(
+    # give a template with extents--fast, but gotta calculate extents
+    from=in.raster, to=template.raster, crs=out.crs,
+    # give a resolution instead of a template? no, that hangs
+#    from=in.raster, res=grid.res, crs=out.proj4,
+    method='bilinear', overwrite=TRUE, format='CDF',
+    # args from writeRaster
+#    NAflag=regrid.datavar.na,
+    varname=out_datavar_name, 
+    varunit=out_datavar_units,
+    longname=out_datavar_long_name,
+    xname=out_datavar_dim_x_name,
+    yname=out_datavar_dim_y_name,
+    # gotta have these, else `ncdump -h` ->
+# dimensions:
+#         COL = 459 ;
+#         ROW = 299 ;
+#         value = UNLIMITED ; // (12 currently)
+# variables:
+# ...
+#         int value(value) ;
+#                 value:units = "unknown" ;
+#                 value:long_name = "value" ;
+    zname=out_datavar_dim_z_name,
+    zunit=out_datavar_dim_z_units,
+    filename=out_fp)
+
+# # start debugging-----------------------------------------------------
+# out.raster
+# # class       : RasterLayer 
+# # dimensions  : 299, 459, 137241  (nrow, ncol, ncell)
+# # resolution  : 12000, 12000  (x, y)
+# # extent      : -2556000, 2952000, -1728000, 1860000  (xmin, xmax, ymin, ymax)
+# # coord. ref. : +proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +a=6370000 +b=6370000 
+# # data source : .../v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit_regrid.nc 
+# # names       : N2O 
+# # zvar        : N2O 
+
+# netCDF.stats.to.stdout(
+#   netcdf.fp=out_fp,
+#   data.var.name=out_datavar_name,
+#   stats.precision=sigdigs
+# )
+# # From ./v42_N2O_2008_IPCC_4C_4D.0.1x0.1_reunit_regrid.nc datavar=N2O:
+# # 	cells=137241
+# # 	obs=137241
+# # 	min=0
+# # 	q1=0
+# # 	med=0.000157
+# # 	mean=0.00189
+# # 	q3=0.00312
+# # 	max=0.0861
+# # 	sum=259
+# #   end debugging-----------------------------------------------------
+
+# ----------------------------------------------------------------------
+# visualize output/AQMEII monthly emissions
+# ----------------------------------------------------------------------
+
+cat(sprintf('%s: plotting output=%s to %s...\n', # debugging
+  this_fn, out_fp, out_pdf_fp))
+
+## get projected North American map
+NorAm.shp <- project.NorAm.boundaries.for.CMAQ(
+  units='m',
+  extents.fp=template_input_fp,
+  extents=template.extents,
+  LCC.parallels=c(33,45),
+  CRS=out.crs)
+# # start debugging
+# class(NorAm.shp)
+# bbox(NorAm.shp)
+# # compare bbox to (above)
+# # > template.extents
+# # class       : Extent 
+# # xmin        : -2556000 
+# # xmax        : 2952000 
+# # ymin        : -1728000 
+# # ymax        : 1860000 
+# #   end debugging
+
+# Why does '=' fail and '<-' succeed in the arg list?
+visualize.layer(
+  nc.fp=out_fp,
+  datavar.name=out_datavar_name,
+  sigdigs=sigdigs,
+  layer=out.raster,
+#  map.list=list(NorAm.shp),
+  map.list <- list(NorAm.shp),
+  pdf.fp=out_pdf_fp,
+  pdf.height=pdf_height,
+  pdf.width=pdf_width
+)

File reunit_vis_EDGAR_global.r

 # R code to
-# * reunit an EDGAR global/unprojected inventory from %%%
+# * reunit an EDGAR global/unprojected inventory from EDGAR (via this project, since there's no direct URI)
 # * visualize it
 
-# If running manually in R console, remember to run setup actions: `source ./sum_reunit_regrid_retemp.sh`
+# If running manually in R console, remember to run setup actions: `source ./AQMEII_driver.sh`
 
 # Code slightly edited from EDGAR-4.2_minus_soil_and_biomass/sum_reunit.r
 # TODO: refactor, package

File uber_driver.sh

 # > module add netcdf-4.1.2 # on terrae (a cluster on which I work)
 
 # TODO: get from commandline
-# ACCESS='file'       # use uncommitted code in current directory
+ACCESS='file'       # use uncommitted code in current directory
 # ACCESS='git/ssh'    # outgoing ssh works (preferred if available)
 # ACCESS='http+cert'  # outgoing ssh blocked, but you have certificates
-ACCESS='http-cert'  # (terrae) outgoing ssh blocked, no certs installed
+# ACCESS='http-cert'  # (terrae) outgoing ssh blocked, no certs installed
 
 USERNAME='tlroche'             # TODO: get from commandline
 REPO_ROOT='/tmp' # or location of your choice: repo/workspace created in subdir
            ./area_0.1x0.1.nc \
            ${EPIC_INPUT_FP} \
            ./netCDF.stats.to.stdout.r \
-           ./reunit_vis_EDGAR_global.r"
+           ./reunit_vis_EDGAR_global.r \
+           ./regrid_vis.r"
 # note: must use double quotes?
 # TODO: get members(${FILE_LIST}) from ${DRIVER_FN}