Source

GEIA_regrid / GEIA_global_to_AQMEII_netCDF.sh

Full commit
#!/usr/bin/env bash

# Requires R.
# Driver for [GEIA_to_netCDF.r,global_to_AQMEII.r], converting
# GEIA marine N2O emissions from global native format to netCDF over AQMEII.
# Configure as needed for your platform.

# ----------------------------------------------------------------------
# constants with some simple manipulations
# ----------------------------------------------------------------------

THIS_FN="$(basename $0)"
# will do the real work
export CALL_REFORMAT_FN='GEIA_to_netCDF.r'
export CALL_REGRID_FN='global_to_AQMEII.r'

# 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"

# workspace
export WORK_DIR="$(pwd)" # keep it simple for now: same dir as top of repo

# will do the real work
export CALL_REFORMAT_FP="${WORK_DIR}/${CALL_REFORMAT_FN}"
export CALL_REGRID_FP="${WORK_DIR}/${CALL_REGRID_FN}"

# for plotting
PDF_VIEWER='xpdf'  # whatever works on your platform
# temporally disaggregate multiple plots
DATE_FORMAT='%Y%m%d_%H%M'
export PDF_DIR="${WORK_DIR}"

# Raw input data is GEIA marine N2O emissions in native format.
# For details, see http://geiacenter.org/presentData/geiadfrm.html
# Get from my repository (stable location, no unzip)
export GEIA_RAW_SOURCE_FILE="http://geiacenter.org/data/n2ooc90y.1a.zip, 'GEIA Inventory n2ocg90yr1.1a  18 Dec 95'"
export GEIA_RAW_CONVENTIONS='Contact: A.F. Bouwman, RIVM; Box 1, 3720 BA Bilthoven, Netherlands; tel.31-30-2743635; fax.31-30-2744417; email lex.bouwman@rivm.nl'
GEIA_RAW_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/N2OOC90Y.1A'
GEIA_RAW_FN="$(basename ${GEIA_RAW_URI})"
export GEIA_RAW_FP="${WORK_DIR}/${GEIA_RAW_FN}"

export GEIA_RAW_HEADER_N='10'       # header.n lines to skip @ top
# GEIA data is 1° x 1°, so ...
export GEIA_RAW_LAT_DEGREE_START='-90.0'   # 90 S, scalar
export GEIA_RAW_LAT_DEGREE_PER_CELL='1.0'
export GEIA_RAW_LON_DEGREE_START='-180.0'  # 180 W, scalar
export GEIA_RAW_LON_DEGREE_PER_CELL='1.0'

GEIA_RAW_PDF_FN="geia_raw_$(date +${DATE_FORMAT}).pdf"
export GEIA_RAW_PDF_FP="${WORK_DIR}/${GEIA_RAW_PDF_FN}" # path to PDF output

# Intermediate product reformated to netCDF

GEIA_REFORMAT_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/GEIA_N2O_oceanic.nc'
GEIA_REFORMAT_FN="$(basename ${GEIA_REFORMAT_URI})"
export GEIA_REFORMAT_FP="${WORK_DIR}/${GEIA_REFORMAT_FN}"

# TODO: automate getting this metadata
export GEIA_REFORMAT_DATAVAR_NAME='emi_n2o'            # like EDGAR
export GEIA_REFORMAT_DATAVAR_LONGNAME='N2O emissions'  # like CLM-CN
export GEIA_REFORMAT_DATAVAR_UNIT='ton N2O-N/yr'       # value from header(GEIA.emis.txt.fn)
export GEIA_REFORMAT_DATAVAR_NA='-999.0'               # like GFED
export GEIA_REFORMAT_DATAVAR_BAND='3' # index of dim=TIME in emi_n2o(time, lat, lon)
export GEIA_REFORMAT_DATAVAR_TOTAL='3.5959E+06'        # value from header(GEIA.emis.txt.fn)
export GEIA_REFORMAT_TIME_VAR_NAME='time'              # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_TIME_VAR_LONG_NAME='time'         # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_TIME_VAR_UNITS='year'             # for now
export GEIA_REFORMAT_TIME_VAR_CALENDAR='none: emissions attributed to no particular year'
export GEIA_REFORMAT_LAT_VAR_NAME='lat'                # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LAT_VAR_LONG_NAME='latitude'      # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LAT_VAR_UNITS='degrees_north'     # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LAT_VAR_COMMENT='center_of_cell'  # like EDGAR
export GEIA_REFORMAT_LON_VAR_NAME='lon'                # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LON_VAR_LONG_NAME='longitude'     # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LON_VAR_UNITS='degrees_east'      # like CLM-CN, EDGAR, GFED
export GEIA_REFORMAT_LON_VAR_COMMENT='center_of_cell'  # like EDGAR

GEIA_REFORMAT_PDF_FN="geia_reformat_$(date +${DATE_FORMAT}).pdf"
export GEIA_REFORMAT_PDF_FP="${WORK_DIR}/${GEIA_REFORMAT_PDF_FN}"
export GEIA_REFORMAT_PDF_TITLE='GEIA annual oceanic N2O emissions'

# 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!
export TEMPLATE_VAR_NAME='emi_n2o'

TEMPLATE_INPUT_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/emis_mole_all_20080101_12US1_cmaq_cb05_soa_2008ab_08c.EXTENTS_INPUT.nc'
TEMPLATE_INPUT_FN="$(basename ${TEMPLATE_INPUT_URI})"
export TEMPLATE_INPUT_FP="${WORK_DIR}/${TEMPLATE_INPUT_FN}"
export TEMPLATE_INPUT_BAND='1' # `ncks` makes dim=TSTEP first

# Output is regridded netCDF.

GEIA_REGRID_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/GEIA_N2O_oceanic_regrid.nc'
GEIA_REGRID_FN="$(basename ${GEIA_REGRID_URI})"
export GEIA_REGRID_FP="${WORK_DIR}/${GEIA_REGRID_FN}"
# TODO: automate getting this metadata
export GEIA_REGRID_X_VAR_NAME='COL'
export GEIA_REGRID_Y_VAR_NAME='ROW'
export GEIA_REGRID_DATAVAR_UNIT="${GEIA_REFORMAT_DATAVAR_UNIT}"
# export GEIA_REGRID_PDF_TITLE="GEIA annual oceanic N2O emissions regridded to AQMEII-NA\n${GEIA_REGRID_DATAVAR_UNIT}"
export GEIA_REGRID_PDF_TITLE='GEIA annual oceanic N2O emissions regridded to AQMEII-NA'

GEIA_REGRID_PDF_FN="geia_regrid_$(date +${DATE_FORMAT}).pdf"
export GEIA_REGRID_PDF_FP="${WORK_DIR}/${GEIA_REGRID_PDF_FN}"

# map*.dat is used to create a map for image.plot-ing
# use one of following, depending on units

# units=km
# MAP_TABLE_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/map.CMAQkm.world.dat'
# units=m
MAP_TABLE_URI='https://bitbucket.org/tlroche/geia_regrid/downloads/map.CMAQm.world.dat'
MAP_TABLE_FN="$(basename ${MAP_TABLE_URI})"
export MAP_TABLE_FP="${WORK_DIR}/${MAP_TABLE_FN}"

# These helpers should have been cloned into the cwd, but JIC (and for later use). TODO: R-package my code

STAT_SCRIPT_URI='https://bitbucket.org/tlroche/geia_regrid/raw/6ecdb8ae7d6fd661f7c4616a14b817c5aa9c7f90/netCDF.stats.to.stdout.r'
STAT_SCRIPT_FN="$(basename ${STAT_SCRIPT_URI})"
export STAT_SCRIPT_FP="${WORK_DIR}/${STAT_SCRIPT_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
# ----------------------------------------------------------------------

# 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}

if [[ -z "${MAP_TABLE_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: MAP_TABLE_FP not defined"
  exit 1
fi
if [[ ! -r "${MAP_TABLE_FP}" ]] ; then
  for CMD in \
    "wget --no-check-certificate -c -O ${MAP_TABLE_FP} ${MAP_TABLE_URI}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
fi

if [[ -z "${STAT_SCRIPT_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: STAT_SCRIPT_FP not defined"
  exit 2
fi
if [[ ! -r "${STAT_SCRIPT_FP}" ]] ; then
  for CMD in \
    "wget --no-check-certificate -c -O ${STAT_SCRIPT_FP} ${STAT_SCRIPT_URI}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
fi

if [[ -z "${PLOT_SCRIPT_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: PLOT_SCRIPT_FP not defined"
  exit 3
fi
if [[ ! -r "${PLOT_SCRIPT_FP}" ]] ; then
  for CMD in \
    "wget --no-check-certificate -c -O ${PLOT_SCRIPT_FP} ${PLOT_SCRIPT_URI}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
fi

if [[ -z "${GEIA_RAW_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: GEIA_RAW_FP not defined"
  exit 4
fi
if [[ ! -r "${GEIA_RAW_FP}" ]] ; then
  for CMD in \
    "wget --no-check-certificate -c -O ${GEIA_RAW_FP} ${GEIA_RAW_URI}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
fi

if [[ -z "${TEMPLATE_INPUT_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: TEMPLATE_INPUT_FP not defined"
  exit 5
fi
if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
  for CMD in \
    "wget --no-check-certificate -c -O ${TEMPLATE_INPUT_FP} ${TEMPLATE_INPUT_URI}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
fi

# only for testing. TODO: flag control
if [[ -z "${GEIA_REFORMAT_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: GEIA_REFORMAT_FP not defined"
  exit 6
fi
# if [[ ! -r "${GEIA_REFORMAT_FP}" ]] ; then
#   for CMD in \
#     "wget --no-check-certificate -c -O ${GEIA_REFORMAT_FP} ${GEIA_REFORMAT_URI}" \
#   ; do
#     echo -e "$ ${CMD}"
#     eval "${CMD}"
#   done
# fi

# only for testing. TODO: flag control
if [[ -z "${GEIA_REGRID_FP}" ]] ; then
  echo -e "${THIS_FN}: ERROR: GEIA_REGRID_FP not defined"
  exit 7
fi
# if [[ ! -r "${GEIA_REGRID_FP}" ]] ; then
#   for CMD in \
#     "wget --no-check-certificate -c -O ${GEIA_REGRID_FP} ${GEIA_REGRID_URI}" \
#   ; do
#     echo -e "$ ${CMD}"
#     eval "${CMD}"
#   done
# fi

# ----------------------------------------------------------------------
# payload
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# convert from native format to netCDF
# ----------------------------------------------------------------------

# If this fails ...
"${RSCRIPT}" "${CALL_REFORMAT_FP}"
# ... just start R ...
# "${R}"
# ... and source ${CALL_REFORMAT_FP}, e.g.,
# source('....r')

# After exiting R, show cwd and display output PDF.
for CMD in \
  "ls -alht ${WORK_DIR}" \
  "${PDF_VIEWER} ${GEIA_REFORMAT_PDF_FP} &" \
; do
  echo -e "$ ${CMD}"
  eval "${CMD}"
done

# ----------------------------------------------------------------------
# convert from global extents to AQMEII
# ----------------------------------------------------------------------

# If this fails ...
"${RSCRIPT}" "${CALL_REGRID_FP}"
# ... just start R ...
# "${R}"
# ... and source ${CALL_REGRID_FP}, e.g.,
# source('....r')

# After exiting R, show cwd and display output PDF.
for CMD in \
  "ls -alht ${WORK_DIR}" \
  "${PDF_VIEWER} ${GEIA_REGRID_PDF_FP} &" \
; do
  echo -e "$ ${CMD}"
  eval "${CMD}"
done