Commits

Tom Roche committed 253ab91

refactored R utilities from AQMEII_ag_soil/{combine_EDGAR_and_EPIC_emissions.r,compute_EPIC_emissions.r} to regrid_utils: tested from uber_driver::file

  • Participants
  • Parent commits ca9a329

Comments (0)

Files changed (3)

 export GET_AQMEIINA_AREAS_FN='get_output_areas.ncl'
 export GET_AQMEIINA_AREAS_FP="${WORK_DIR}/${GET_AQMEIINA_AREAS_FN}"
 
+export R_UTILS_FN='R_utilities.r'
+export R_UTILS_FP="${WORK_DIR}/${R_UTILS_FN}"
+
 export STATS_FUNCS_FN='netCDF.stats.to.stdout.r'
 export STATS_FUNCS_FP="${WORK_DIR}/${STATS_FUNCS_FN}"
 
     'get_regrid_utils' \
     'get_bash_utils' \
     'get_reunit_vis_EDGAR_global' \
+    'get_R_utils' \
     'get_stats_funcs' \
     'get_vis_funcs' \
     'get_regrid_vis_EDGAR_AQMEII' \
     eval "${CMD}" # comment this out for NOPing, e.g., to `source`
     if [[ $? -ne 0 ]] ; then
       echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-      exit 1
+      exit 2
     fi
   done
 } # end function get_helpers
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 4
       fi
     done
   fi
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 5
       fi
     done
   fi
 
   if [[ ! -r "${REGRID_UTILS_DIR}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot download regrid_utils to '${REGRID_UTILS_DIR}'"
-    exit 4
+    exit 6
   fi  
 } # end function get_regrid_utils
 
 function get_bash_utils {
   if [[ -z "${BASH_UTILS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP not defined"
-    exit 5
+    exit 7
   fi
   if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 8
       fi
     done
   fi
   if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP=='${BASH_UTILS_FP}' not readable"
-    exit 6
+    exit 9
   fi
   # This is bash, so gotta ...
   source "${BASH_UTILS_FP}"
 function get_reunit_vis_EDGAR_global {
   if [[ -z "${REUNIT_VIS_EDGAR_GLOBAL_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: REUNIT_VIS_EDGAR_GLOBAL_FP not defined"
-    exit 1
+    exit 10
   fi
   # is in this repo
 #  if [[ ! -r "${REUNIT_VIS_EDGAR_GLOBAL_FP}" ]] ; then
 #  fi
   if [[ ! -r "${REUNIT_VIS_EDGAR_GLOBAL_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: REUNIT_VIS_EDGAR_GLOBAL_FP=='${REUNIT_VIS_EDGAR_GLOBAL_FP}' not readable"
-    exit 1
+    exit 11
   fi
 } # end function get_reunit_vis_EDGAR_global
 
 function get_vis_funcs {
   if [[ -z "${VIS_FUNCS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: VIS_FUNCS_FP not defined"
-    exit 17
+    exit 12
   fi
   if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 13
       fi
     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
+    exit 14
   fi
 } # end function get_vis_funcs
 
 # isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
+# To override, copy/mod to ${R_UTILS_FP} before running this script.
+function get_R_utils {
+  if [[ -z "${R_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: R_UTILS_FP not defined"
+    exit 15
+  fi
+  if [[ ! -r "${R_UTILS_FP}" ]] ; then
+    # copy from downloaded regrid_utils
+    for CMD in \
+      "cp ${REGRID_UTILS_DIR}/${R_UTILS_FN} ${R_UTILS_FP}" \
+    ; do
+      echo -e "\n$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}\n"
+      eval "${CMD}"
+      if [[ $? -ne 0 ]] ; then
+        echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
+        exit 16
+      fi
+    done
+  fi
+  if [[ ! -r "${R_UTILS_FP}" ]] ; then
+    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: R_UTILS_FP=='${R_UTILS_FP}' not readable"
+    exit 17
+  fi
+} # end function get_R_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
+    exit 18
   fi
   if [[ ! -r "${STATS_FUNCS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 19
       fi
     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
+    exit 20
   fi
 } # end function get_stats_funcs
 
 function get_string_funcs {
   if [[ -z "${STRING_FUNCS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STRING_FUNCS_FP not defined"
-    exit 9
+    exit 21
   fi
   if [[ ! -r "${STRING_FUNCS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 22
       fi
     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
+    exit 23
   fi
 } # end function get_string_funcs
 
 function get_regrid_vis_EDGAR_AQMEII {
   if [[ -z "${REGRID_VIS_EDGAR_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: REGRID_VIS_EDGAR_FP not defined"
-    exit 1
+    exit 24
   fi
   # is in this repo
 #  if [[ ! -r "${REGRID_VIS_EDGAR_FP}" ]] ; then
 #  fi
   if [[ ! -r "${REGRID_VIS_EDGAR_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: REGRID_VIS_EDGAR_FP=='${REGRID_VIS_EDGAR_FP}' not readable"
-    exit 1
+    exit 25
   fi
 } # end function get_regrid_vis
 
 function get_BELD_read {
   if [[ -z "${BELD_READ_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: BELD_READ_FP not defined"
-    exit 1
+    exit 26
   fi
   # is in this repo
 #  if [[ ! -r "${BELD_READ_FP}" ]] ; then
 #  fi
   if [[ ! -r "${BELD_READ_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_READ_FP=='${BELD_READ_FP}' not readable"
-    exit 1
+    exit 27
   fi
 } # end function get_BELD_read
 
 function get_BELD_convert {
   if [[ -z "${BELD_CONVERT_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: BELD_CONVERT_FP not defined"
-    exit 1
+    exit 28
   fi
   # is in this repo
 #  if [[ ! -r "${BELD_CONVERT_FP}" ]] ; then
 #  fi
   if [[ ! -r "${BELD_CONVERT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_CONVERT_FP=='${BELD_CONVERT_FP}' not readable"
-    exit 1
+    exit 29
   fi
 } # end function get_BELD_convert
 
 function get_get_AQMEIINA_areas {
   if [[ -z "${GET_AQMEIINA_AREAS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GET_AQMEIINA_AREAS_FP not defined"
-    exit 9
+    exit 30
   fi
   if [[ ! -r "${GET_AQMEIINA_AREAS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 31
       fi
     done
   fi
   if [[ ! -r "${GET_AQMEIINA_AREAS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GET_AQMEIINA_AREAS_FP=='${GET_AQMEIINA_AREAS_FP}' not readable"
-    exit 10
+    exit 32
   fi
 } # end function get_get_AQMEIINA_areas
 
 function get_time_funcs {
   if [[ -z "${TIME_FUNCS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TIME_FUNCS_FP not defined"
-    exit 9
+    exit 33
   fi
   if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
     # copy from downloaded regrid_utils
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 34
       fi
     done
   fi
   if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TIME_FUNCS_FP=='${TIME_FUNCS_FP}' not readable"
-    exit 10
+    exit 35
   fi
 } # end function get_time_funcs
 
 function get_distill_EPIC {
   if [[ -z "${DISTILL_EPIC_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: DISTILL_EPIC_FP not defined"
-    exit 1
+    exit 36
   fi
   # is in this repo
 #  if [[ ! -r "${DISTILL_EPIC_FP}" ]] ; then
 #  fi
   if [[ ! -r "${DISTILL_EPIC_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: DISTILL_EPIC_FP=='${DISTILL_EPIC_FP}' not readable"
-    exit 1
+    exit 37
   fi
 } # end function get_distill_EPIC
 
 function get_compute_EPIC {
   if [[ -z "${COMPUTE_EPIC_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: COMPUTE_EPIC_FP not defined"
-    exit 1
+    exit 38
   fi
   # is in this repo
 #  if [[ ! -r "${COMPUTE_EPIC_FP}" ]] ; then
 #  fi
   if [[ ! -r "${COMPUTE_EPIC_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMPUTE_EPIC_FP=='${COMPUTE_EPIC_FP}' not readable"
-    exit 1
+    exit 39
   fi
 } # end function get_compute_EPIC
 
 function get_combine_emis {
   if [[ -z "${COMBINE_EMIS_FP}" ]] ; then
     echo -e "${THIS_FN}: ERROR: COMBINE_EMIS_FP not defined"
-    exit 1
+    exit 40
   fi
   # is in this repo
 #  if [[ ! -r "${COMBINE_EMIS_FP}" ]] ; then
 #  fi
   if [[ ! -r "${COMBINE_EMIS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMBINE_EMIS_FP=='${COMBINE_EMIS_FP}' not readable"
-    exit 1
+    exit 41
   fi
 } # end function get_combine_emis
 
     eval "${CMD}" # comment this out for NOPing, e.g., to `source`
     if [[ $? -ne 0 ]] ; then
       echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-      exit 1
+      exit 42
     fi
   done
 } # end function setup_resources
     || -z "${WORK_DIR}"
   ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: undefined dependencies"
-    exit 1
+    exit 43
   fi
 
   if [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_FP}" ]] ; then
         eval "${CMD}"
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 44
         fi
       done
     fi
     ## if we still don't have them, die ...
     if [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_ZIP_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not download EDGAR annual emissions archive='${AQMEII_EDGAR_N2O_GLOBAL_ZIP_FP}'"
-      exit 1
+      exit 45
     elif [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not unzip EDGAR annual emissions='${AQMEII_EDGAR_N2O_GLOBAL_FP}'"
-      exit 1
+      exit 46
     fi
   fi
 } # end function get_EDGAR_N2O
     || -z "${WORK_DIR}"
   ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: undefined dependencies"
-    exit 1
+    exit 47
   fi
 
   if [[ ! -r "${AQMEII_EDGAR_AREAS_GLOBAL_FP}" ]] ; then
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 48
       fi
     done
   fi
   ## if we still don't have them, die ...
   if [[ ! -r "${AQMEII_EDGAR_AREAS_GLOBAL_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not download EDGAR annual emissions='${AQMEII_EDGAR_AREAS_GLOBAL_FP}'"
-    exit 1
+    exit 49
   fi
 } # end function get_EDGAR_areas
 
 function get_EPIC {
   if [[ ! -r "${EPIC_RAW_EMITT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[1]}::${FUNCNAME[0]}: ERROR: cannot find raw EPIC emittivities='${EPIC_RAW_EMITT_FP}' (and it is too big to download)"
-    exit 1
+    exit 50
   fi
 } # end function get_EPIC
 
 function get_template {
   if [[ -z "${TEMPLATE_INPUT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TEMPLATE_INPUT_FP not defined"
-    exit 1
+    exit 51
   fi
   if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
     if [[ ! -r "${TEMPLATE_INPUT_GZ_FP}" ]] ; then
         eval "${CMD}"
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 52
         fi
       done
     fi
         eval "${CMD}"
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 53
         fi
       done
     fi
   fi
   if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: regridding template=='${TEMPLATE_INPUT_FP}' not readable"
-    exit 1
+    exit 54
   fi
 } # end function get_template
 
 function get_BELD_CSV {
   if [[ -z "${BELD_CSV_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_CSV_FP not defined"
-    exit 1
+    exit 55
   fi
   if [[ ! -r "${BELD_CSV_FP}" ]] ; then
     if [[ ! -r "${BELD_CSV_GZ_FP}" ]] ; then
         eval "${CMD}"
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 56
         fi
       done
     fi
         eval "${CMD}"
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 57
         fi
       done
     fi
   fi
   if [[ ! -r "${BELD_CSV_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD coverages as CSV=='${BELD_CSV_FP}' not readable"
-    exit 1
+    exit 58
   fi
 } # end function get_BELD_CSV
 
 function get_MSFs {
   if [[ -z "${AQMEIINA_MSFs_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEIINA_MSFs_FP not defined"
-    exit 1
+    exit 59
   fi
 
   if [[ -r "${AQMEIINA_MSFs_FP}" ]] ; then
     if [[ ! -r "${AQMEIINA_MSFs_FP}" ]] ; then
       if [[ -z "${AQMEIINA_MSFs_GZ_FP}" ]] ; then
 	echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEIINA_MSFs_GZ_FP not defined"
-	exit 1
+	exit 60
       fi
       if [[ ! -r "${AQMEIINA_MSFs_GZ_FP}" ]] ; then
 	# TODO: pipe to gunzip
 	  eval "${CMD}" # comment this out for NOPing, e.g., to `source`
 	  if [[ $? -ne 0 ]] ; then
 	    echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-	    exit 1
+	    exit 61
 	  fi
 	done
       fi
 	  eval "${CMD}" # comment this out for NOPing, e.g., to `source`
 	  if [[ $? -ne 0 ]] ; then
 	    echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-	    exit 1
+	    exit 62
 	  fi
 	done
       fi # if [[ -z "${AQMEIINA_MSFs_GZ_FP}" ]]
 
   if [[ ! -r "${AQMEIINA_MSFs_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot read AQMEIINA_MSFs_FP=='${AQMEIINA_MSFs_FP}'"
-    exit 1
+    exit 63
   fi
 } # end function get_MSFs
 
 # ncl # bail to console, or
   if [[ -z "${AQMEIINA_AREAS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEIINA_AREAS_FP not defined"
-    exit 1
+    exit 64
   fi
 
   if [[ -r "${AQMEIINA_AREAS_FP}" ]] ; then
   else # make it
     if [[ -z "${GET_AQMEIINA_AREAS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: GET_AQMEIINA_AREAS_FP not defined"
-      exit 1
+      exit 65
     fi
     if [[ ! -r "${GET_AQMEIINA_AREAS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: areas-getting helper='${GET_AQMEIINA_AREAS_FP} not readable"
-      exit 1
+      exit 66
     fi
     for CMD in \
       'get_MSFs' \
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 67
       fi
     done
   fi # if [[ -r "${AQMEIINA_AREAS_FP}" ]]
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 68
       fi
     done
   else
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to create AQMEII-NA areas file='${AQMEIINA_AREAS_FP}"
-    exit 1
+    exit 69
   fi
 } # end function get_AQMEIINA_areas
 
 function reunit_vis_EDGAR_global {
   if [[ -z "${AQMEII_EDGAR_N2O_GLOBAL_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_GLOBAL_FP not defined"
-    exit 1
+    exit 70
   elif [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: input file='${AQMEII_EDGAR_N2O_GLOBAL_FP}' not readable"
-    exit 1
+    exit 71
   fi
   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
+    exit 72
   fi
   if [[ -z "${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FP not defined"
-    exit 1
+    exit 73
   fi
 
 # eval "rm ${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}"
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 74
       fi
     done
 
     if [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: output file='${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}' not readable"
-      exit 1
+      exit 75
     elif [[ ! -r "${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: plot file='${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_PDF_FP}' not readable"
-      exit 1
+      exit 76
     else
 #       "ncdump -h ${AQMEII_EDGAR_N2O_GLOBAL_REUNIT_FP}" \ # do this in script
       for CMD in \
         eval "${CMD}" # comment this out for NOPing, e.g., to `source`
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 77
         fi
       done
     fi # checking readability of outputs
 function regrid_vis_EDGAR_AQMEII {
   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
+    exit 78
   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
+    exit 79
   fi
   if [[ -z "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_REGRID_FP not defined"
-    exit 1
+    exit 80
   fi
   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
+    exit 81
   fi
 
 # eval "rm ${AQMEII_EDGAR_N2O_REGRID_FP}"
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 82
       fi
     done
 
     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
+      exit 83
     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
+      exit 84
     else
 #       "ncdump -h ${AQMEII_EDGAR_N2O_REGRID_FP}" \ # do this in script
       for CMD in \
         eval "${CMD}" # comment this out for NOPing, e.g., to `source`
         if [[ $? -ne 0 ]] ; then
           echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-          exit 1
+          exit 85
         fi
       done
     fi # checking readability of outputs
 
   if [[ -z "${BELD_RDS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_RDS_FP not defined"
-    exit 1
+    exit 86
   fi
   if [[ -r "${BELD_RDS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: skipping ${BELD_CONVERT_FN}: output='${BELD_RDS_FP}' is readable"
 
     if [[ -z "${BELD_CSV_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_CSV_FP not defined"
-      exit 1
+      exit 87
     fi
 
     if [[ ! -r "${BELD_CSV_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD coverages as CSV=='${BELD_CSV_FP}' not readable"
-      exit 1
+      exit 88
     fi
 
     if [[ -z "${BELD_READ_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_READ_FP not defined"
-      exit 1
+      exit 89
     fi
 
     if [[ ! -r "${BELD_READ_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_READ_FP=='${BELD_READ_FP}' not readable"
-      exit 1
+      exit 90
     fi
 
     if [[ -z "${BELD_CONVERT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_CONVERT_FP not defined"
-      exit 1
+      exit 91
     fi
 
     if [[ ! -r "${BELD_CONVERT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_CONVERT_FP=='${BELD_CONVERT_FP}' not readable"
-      exit 1
+      exit 92
     fi
 
 #     echo -e "${THIS_FN}::${FUNCNAME[0]}: about to write BELD CSV='${BELD_CSV_FP}' to RDS='${BELD_RDS_FP}'"
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 93
       fi
     done
   fi # end if [[ -r "${BELD_RDS_FP}" ]]
     eval "${CMD}" # comment this out for NOPing, e.g., to `source`
     if [[ $? -ne 0 ]] ; then
       echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-      exit 1
+      exit 94
     fi
   done
 } # end function BELD_CSV_to_RDS
 function distill_EPIC {
   if [[ -z "${AQMEII_EPIC_N2O_EMITT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EPIC_N2O_EMITT_FP not defined"
-    exit 1
+    exit 95
   fi
   if [[ -r "${AQMEII_EPIC_N2O_EMITT_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: skipping ${DISTILL_EPIC_FN}: output='${AQMEII_EPIC_N2O_EMITT_FP}' is readable"
 
     if [[ -z "${EPIC_RAW_EMITT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EPIC_RAW_EMITT_FP not defined"
-      exit 1
+      exit 96
     fi
 
     if [[ ! -r "${EPIC_RAW_EMITT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: raw EPIC emittivities=='${EPIC_RAW_EMITT_FP}' not readable"
-      exit 1
+      exit 97
     fi
 
     if [[ -z "${DISTILL_EPIC_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: DISTILL_EPIC_FP not defined"
-      exit 1
+      exit 98
     fi
 
     if [[ ! -r "${DISTILL_EPIC_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: DISTILL_EPIC_FP=='${DISTILL_EPIC_FP}' not readable"
-      exit 1
+      exit 99
     fi
 
 #     ncl # bail to NCL and copy script lines, or ...
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 100
       fi
     done
   fi # end if [[ -r "${AQMEII_EPIC_N2O_EMITT_FP}" ]] ; then
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 101
       fi
     done
   else
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not create 'distilled' EPIC emittivities=='${AQMEII_EPIC_N2O_EMITT_FP}'"
-    exit 1
+    exit 102
   fi
 
 } # end function distill_EPIC
   ### Have I already named the output?
   if [[ -z "${AQMEII_EPIC_N2O_EMISS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EPIC_N2O_EMISS_FP not defined"
-    exit 1
+    exit 103
   fi
 
   ### Have I already made the output?
     ### input 1 of 2: EPIC emittivities
     if [[ -z "${AQMEII_EPIC_N2O_EMITT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EPIC_N2O_EMITT_FP not defined"
-      exit 1
+      exit 104
     fi
     if [[ ! -r "${AQMEII_EPIC_N2O_EMITT_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: distilled EPIC N2O emittivities=='${AQMEII_EPIC_N2O_EMITT_FP}' not readable"
-      exit 1
+      exit 105
     fi
 
     ### input 2 of 2: BELD crop coverages
     if [[ -z "${BELD_RDS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD_RDS_FP not defined"
-      exit 1
+      exit 106
     fi
     if [[ ! -r "${BELD_RDS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BELD coverages in RDS=='${BELD_RDS_FP}' not readable"
-      exit 1
+      exit 107
     fi
 
     ### helper
     if [[ -z "${COMPUTE_EPIC_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMPUTE_EPIC_FP not defined"
-      exit 1
+      exit 108
     fi
     if [[ ! -r "${COMPUTE_EPIC_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMPUTE_EPIC_FP=='${COMPUTE_EPIC_FP}' not readable"
-      exit 1
+      exit 109
     fi
 
 #     echo -e "${THIS_FN}::${FUNCNAME[0]}: about to compute EPIC emissions='${AQMEII_EPIC_N2O_EMISS_FP}'"
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 110
       fi
     done
   fi # end if [[ -r "${AQMEII_EPIC_N2O_EMISS_FP}" ]]
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 111
       fi
     done
   else
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not create EPIC emissions=='${AQMEII_EPIC_N2O_EMISS_FP}'"
-    exit 1
+    exit 112
   fi
 
 } # end function compute_EPIC
   ### Have I already named the output?
   if [[ -z "${AQMEII_BOTH_N2O_EMISS_FP}" ]] ; then
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_BOTH_N2O_EMISS_FP not defined"
-    exit 1
+    exit 113
   fi
 
   ### Have I already made the output?
     ### input 1 of 2: EPIC emissions over AQMEII
     if [[ -z "${AQMEII_EPIC_N2O_EMISS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EPIC_N2O_EMISS_FP not defined"
-      exit 1
+      exit 114
     fi
     if [[ ! -r "${AQMEII_EPIC_N2O_EMISS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EPIC N2O emissions over AQMEII=='${AQMEII_EPIC_N2O_EMISS_FP}' not readable"
-      exit 1
+      exit 115
     fi
 
     ### input 2 of 2: EDGAR emissions over AQMEII
     if [[ -z "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: AQMEII_EDGAR_N2O_REGRID_FP not defined"
-      exit 1
+      exit 116
     fi
     if [[ ! -r "${AQMEII_EDGAR_N2O_REGRID_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR emissions over AQMEII=='${AQMEII_EDGAR_N2O_REGRID_FP}' not readable"
-      exit 1
+      exit 117
     fi
 
     ### helper
     if [[ -z "${COMBINE_EMIS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMBINE_EMIS_FP not defined"
-      exit 1
+      exit 118
     fi
     if [[ ! -r "${COMBINE_EMIS_FP}" ]] ; then
       echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: COMBINE_EMIS_FP=='${COMBINE_EMIS_FP}' not readable"
-      exit 1
+      exit 119
     fi
 
 #     echo -e "${THIS_FN}::${FUNCNAME[0]}: about to bypass ${COMBINE_EMIS_FP}"
       eval "${CMD}"
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 120
       fi
     done
   fi # end if [[ -r "${AQMEII_BOTH_N2O_EMISS_FP}" ]]
       eval "${CMD}" # comment this out for NOPing, e.g., to `source`
       if [[ $? -ne 0 ]] ; then
         echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-        exit 1
+        exit 121
       fi
     done
   else
     echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: could not create combined emissions=='${AQMEII_BOTH_N2O_EMISS_FP}'"
-    exit 1
+    exit 122
   fi
 
 } # end function combine_emis
     eval "${CMD}" # comment this out for NOPing, e.g., to `source`
     if [[ $? -ne 0 ]] ; then
       echo -e "$ ${THIS_FN}::${FUNCNAME[0]}::${CMD}: ERROR: failed or not found\n"
-      exit 1
+      exit 123
     fi
   done
 } # end function teardown
   eval "${CMD}" # comment this out for NOPing, e.g., to `source`
   if [[ $? -ne 0 ]] ; then
     echo -e "$ ${THIS_FN}::main loop::${CMD}: ERROR: failed or not found\n"
-    exit 1
+    exit 124
   fi
 done
 

combine_EDGAR_and_EPIC_emissions.r

 
 ## helpers
 
+R_utils_fp <- Sys.getenv('R_UTILS_FP', unset=NA)
 stat_funcs_fp <- Sys.getenv('STATS_FUNCS_FP', unset=NA)
 vis_funcs_fp <- Sys.getenv('VIS_FUNCS_FP', unset=NA)
 
 # external functions (referenced)
 # ----------------------------------------------------------------------
 
+source(R_utils_fp)
 source(stat_funcs_fp) # in script, produces errant error=
 #> netCDF.stats.to.stdout.r: no arguments supplied, exiting
 source(vis_funcs_fp)
 # internal functions (defined)
 # ----------------------------------------------------------------------
 
-### TODO: move to regrid_utils: get.ncdf4.prec also used by compute_EPIC_emissions.r
-
-# workaround apparent bug in ncdf4
-get.ncdf4.prec <- function(
-  ncvar4 # object of type=ncvar4
-) {
-  # TODO: test argument
-  ncdf4.prec <- ncvar4$prec
-  if (ncdf4.prec == "int") {
-    return("integer")
-  } else {
-    return(ncdf4.prec)
-  }
-}
-
-### Test if argument is zero within tolerance
-is.zero.tol <- function (val, tol = getOption("tolerance")) {
-  if (is.null(tol)) {
-    tol <- 1e-11
-  }
-  abs(val) <= tol
-}
-
-### Test if argument is NA or zero within tolerance
-is.arr.na.or.zero <- function(arr) {
-# # start debugging-----------------------------------------------------
-#  base::cat(this_fn, '::is.arr.na.or.zero: arr==\n')
-#  print(arr)
-# #   end debugging-----------------------------------------------------
-  # bitwise 'or' !
-  is.na(c(arr)) | is.zero.tol(c(arr))
-} # end is.arr.na.or.zero
-
 # ----------------------------------------------------------------------
 # code
 # ----------------------------------------------------------------------

compute_EPIC_emissions.r

 
 ## helpers
 
+R_utils_fp <- Sys.getenv('R_UTILS_FP', unset=NA)
 stat_funcs_fp <- Sys.getenv('STATS_FUNCS_FP', unset=NA)
 vis_funcs_fp <- Sys.getenv('VIS_FUNCS_FP', unset=NA)
 
 # external functions (referenced)
 # ----------------------------------------------------------------------
 
+source(R_utils_fp)
 source(stat_funcs_fp) # in script, produces errant error=
 #> netCDF.stats.to.stdout.r: no arguments supplied, exiting
 source(vis_funcs_fp)
 # internal functions (defined)
 # ----------------------------------------------------------------------
 
-### TODO: move to regrid_utils: get.ncdf4.prec also used by combine_EDGAR_and_EPIC_emissions.r
-
-# workaround apparent bug in ncdf4
-get.ncdf4.prec <- function(
-  ncvar4 # object of type=ncvar4
-) {
-  # TODO: test argument
-  ncdf4.prec <- ncvar4$prec
-  if (ncdf4.prec == "int") {
-    return("integer")
-  } else {
-    return(ncdf4.prec)
-  }
-}
-
-### Return TRUE if value is NA or 0, else FALSE. FOR USE ONLY IN `if` CONDITIONS (IIRC)!
-### ASSERT: input is numeric (we won't check!)
-is.val.na.or.zero <- function(val) {
-
-# # start debugging-----------------------------------------------------
-#  base::cat(this_fn, '::is.val.na.or.zero: val==\n')
-#  print(val)
-# #   end debugging-----------------------------------------------------
-
-  if (is.na(val)) {
-    TRUE
-  } else {
-    # TODO: add optional tolerance
-    if (val == 0) {
-      TRUE
-    } else {
-      FALSE
-    }
-  }
-} # end is.val.na.or.zero
-
-### FOR USE ONLY IN `if` CONDITIONS, when dealing with data like
-### >  [1] NA NA NA NA  0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
-### > [26] NA NA NA NA NA NA NA NA NA NA NA  0  0 NA NA NA NA
-### ASSERT: input is numeric (we won't check!)
-is.vec.na.or.zero <- function(vec) {
-
-# # start debugging-----------------------------------------------------
-#  base::cat(this_fn, '::is.vec.na.or.zero: vec==\n')
-#  print(vec)
-# #   end debugging-----------------------------------------------------
-
-  if        (!sum(!is.na(vec))) {
-    TRUE
-  } else if (sum(subset(vec, !is.na(vec))) == 0) {
-    TRUE
-  } else {
-    FALSE
-  }
-} # end is.vec.na.or.zero
-
 # Report statistics gathered in function sum.emissions.for.layers
 report.failures <- function(
   gridcells.vec,    # gridcells with product=0