Source

CLM_CN_global_to_AQMEII-NA / CLMCN_driver.sh

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env bash

# ----------------------------------------------------------------------
# description
# ----------------------------------------------------------------------

# A top-level driver for creating an inventory of N2O emissions from natural/unmanaged soils over the AQMEII-NA domain from the CLM-CN global inventory:

# * converts global/unprojected CLM-CN data to AQMEII-NA, an LCC-projected subdomain
# * converts monthly to hourly data (over each month)
# * checks conservation of mass over the conversions

# Expects to run on linux. Utility dependencies include:
# * bash with arithmetic support
# * basename, dirname
# * unzip
# * curl or wget
# * ncdump
# * NCL
# * R

# TODO: failing functions should fail this (entire) driver!

# Configure as needed for your platform.

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

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 NOMINAL_GRIDCELL_AREA='1.440e8' # in meter^2, is a float ; stringtofloat(getenv("NOMINAL_GRIDCELL_AREA"))
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
export CMAQ_RADIUS='6370000'       # in meters

# 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' # set this in bash_utilities::setup_apps
# temporally disaggregate multiple plots
DATE_FORMAT='%Y%m%d_%H%M'
export PDF_DIR="${WORK_DIR}"

## conservation report constants
# need 5 additional digits for float output, e.g., "4.85e+04"
# bash arithmetic gotcha: allow no spaces around '='!
# 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=mgN"
export CONSERV_REPORT_SUBTITLE="(note (US land area)/(earth land area) ~= 6.15e-02)"

# ----------------------------------------------------------------------
# helpers
# ----------------------------------------------------------------------

### helpers in this repo

export CALL_REUNIT_FN='reunit.ncl'
export CALL_REUNIT_FP="${WORK_DIR}/${CALL_REUNIT_FN}"
export CALL_REGRID_FN='regrid_global_to_AQMEII.r'
export CALL_REGRID_FP="${WORK_DIR}/${CALL_REGRID_FN}"
export CALL_RETEMP_FN='retemp.ncl'
export CALL_RETEMP_FP="${WORK_DIR}/${CALL_RETEMP_FN}"
export CALL_CONSERV_FN='check_conservation.ncl'
export CALL_CONSERV_FP="${WORK_DIR}/${CALL_CONSERV_FN}"

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

### helpers from elsewhere

BASH_UTILS_URI='https://bitbucket.org/tlroche/geia_regrid/raw/eef59bc6849c2c022415fe9a879310f482c9d8f4/bash_utilities.sh'
export BASH_UTILS_FN="$(basename ${BASH_UTILS_URI})"
BASH_UTILS_FP="${WORK_DIR}/${BASH_UTILS_FN}"

STATS_FUNCS_URI='https://bitbucket.org/tlroche/aqmeii_ag_soil/raw/0fa7d1180b67265540cdf0dd1d82e06d9b4fa789/netCDF.stats.to.stdout.r'
STATS_FUNCS_FN="$(basename ${STATS_FUNCS_URI})"
export STATS_FUNCS_FP="${WORK_DIR}/${STATS_FUNCS_FN}"

SUMMARIZE_FUNCS_URI='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na/raw/618f09df886a86c9bd15880f37bbf1bf9295b638/summarize.ncl'
SUMMARIZE_FUNCS_FN="$(basename ${SUMMARIZE_FUNCS_URI})"
export SUMMARIZE_FUNCS_FP="${WORK_DIR}/${SUMMARIZE_FUNCS_FN}"

TIME_FUNCS_URI='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na/raw/618f09df886a86c9bd15880f37bbf1bf9295b638/time.ncl'
TIME_FUNCS_FN="$(basename ${TIME_FUNCS_URI})"
export TIME_FUNCS_FP="${WORK_DIR}/${TIME_FUNCS_FN}"

VIS_FUNCS_URI='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na/raw/a2a39d8100260726b7825125b835bdd1959aa91f/visualization.r'
VIS_FUNCS_FN="$(basename ${VIS_FUNCS_URI})"
export VIS_FUNCS_FP="${WORK_DIR}/${VIS_FUNCS_FN}"

# ----------------------------------------------------------------------
# inputs
# ----------------------------------------------------------------------

### raw input

# Raw input data is global/unprojected CLM-CN netCDF, with coordvars=
# lat==(-90,+90)
# lon==(0,+360), hence need to
export ROTATE_INPUT="true"
# time: 14 months (not "decimal year" per `time:units`:
#       month[0]== month[1], month[12]== month[13] (per Saikawa)
# Get from my repository.
CLM_CN_RAW_URI='https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na/downloads/2008PTONCLMCNN2O.nc'
CLM_CN_RAW_FN="$(basename ${CLM_CN_RAW_URI})"
CLM_CN_RAW_FN_ROOT="${CLM_CN_RAW_FN%.*}" # everything left of the dot
export CLM_CN_RAW_FP="${WORK_DIR}/${CLM_CN_RAW_FN}"

CLM_CN_RAW_PDF_FN="${CLM_CN_RAW_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
export CLM_CN_RAW_PDF_FP="${WORK_DIR}/${CLM_CN_RAW_PDF_FN}" # path to PDF output

# TODO: automate getting this metadata from the netCDF
export CLM_CN_RAW_DATAVAR_NAME='n2oemissions'
export CLM_CN_RAW_DATAVAR_LONGNAME='N2O emissions'
export CLM_CN_RAW_DATAVAR_UNIT='mgN/m2/month'
# export CLM_CN_RAW_DATAVAR_NA='-999.0' # not used--hopefully they have no missing data
export CLM_CN_RAW_TIME_VAR_NAME='time'

### 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_VAR_NAME='emi_n2o'

# ----------------------------------------------------------------------
# intermediate products
# ----------------------------------------------------------------------

### netCDF regridded to AQMEII-NA

CLM_CN_REGRID_URI='https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na/downloads/2008PTONCLMCNN2O_regrid.nc'
CLM_CN_REGRID_FN="$(basename ${CLM_CN_REGRID_URI})"
CLM_CN_REGRID_FN_ROOT="${CLM_CN_REGRID_FN%.*}" # everything left of the dot
export CLM_CN_REGRID_FP="${WORK_DIR}/${CLM_CN_REGRID_FN}"

export CLM_CN_REGRID_DATAVAR_NAME="${CLM_CN_RAW_DATAVAR_NAME}"
export CLM_CN_REGRID_X_VAR_NAME='COL'
export CLM_CN_REGRID_Y_VAR_NAME='ROW'
export CLM_CN_REGRID_TIME_VAR_NAME='TSTEP' # more IOAPI-like than 'time'
export CLM_CN_REGRID_TIME_VAR_UNIT='month (1=2, 2=Jan, ... 13=Dec, 13=14)'

CLM_CN_REGRID_PDF_FN="${CLM_CN_REGRID_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
export CLM_CN_REGRID_PDF_FP="${WORK_DIR}/${CLM_CN_REGRID_PDF_FN}" # path to PDF output

### map scale factors (MSFs)
# load MSFs from some GRIDCRO2D_<date/> which is pretty large, so gzip it

MSF_GZ_URI='https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na/downloads/GRIDCRO2D_080101.gz'
MSF_GZ_FN="$(basename ${MSF_GZ_URI})"
MSF_FN_ROOT="${MSF_GZ_FN%.*}" # everything left of the dot
MSF_FN="${MSF_FN_ROOT}"       # does not normally have an extension
MSF_GZ_FP="${WORK_DIR}/${MSF_GZ_FN}"
export MSF_FP="${WORK_DIR}/${MSF_FN}"
export MSF_VAR_NAME='MSFX2'

### data container files
### create data container files from extents/template file

##### TODO: driver must ensure output_fp_template does not exist!
export CLM_CN_TARGET_FN_TEMPLATE_STR='######'
# problem: NCL currently (version=6.1.2) does not support *.ncf :-( workaround below
CLM_CN_TARGET_FN_ROOT_TEMPLATE="emis_mole_N2O_${CLM_CN_TARGET_FN_TEMPLATE_STR}_12US1_cmaq_cb05_soa_2008ab_08c"
CLM_CN_TARGET_EXT_CMAQ='ncf'
CLM_CN_TARGET_EXT_NCL='nc'
CLM_CN_TARGET_FN_TEMPLATE_CMAQ="${CLM_CN_TARGET_FN_ROOT_TEMPLATE}.${CLM_CN_TARGET_EXT_CMAQ}"
CLM_CN_TARGET_FN_TEMPLATE_NCL="${CLM_CN_TARGET_FN_ROOT_TEMPLATE}.${CLM_CN_TARGET_EXT_NCL}"
export CLM_CN_TARGET_FP_TEMPLATE_CMAQ="${WORK_DIR}/${CLM_CN_TARGET_FN_TEMPLATE_CMAQ}"
export CLM_CN_TARGET_FP_TEMPLATE_NCL="${WORK_DIR}/${CLM_CN_TARGET_FN_TEMPLATE_NCL}"

## global metadata for both {template, "real" monthly/hourly data container} files
export CLM_CN_TARGET_FILE_ATTR_HISTORY='see https://bitbucket.org/tlroche/clm_cn_global_to_aqmeii-na'

## metadata for datavar in both {template, "real" monthly/hourly data container} files
export CLM_CN_TARGET_VAR_NAME='N2O'
# IOAPI pads varattr=long_name to length=16 with spaces
export CLM_CN_TARGET_VAR_ATTR_LONG_NAME="$(printf '%-16s' ${CLM_CN_TARGET_VAR_NAME})"
# IOAPI pads varattr=units to length=16 with spaces
export CLM_CN_TARGET_VAR_ATTR_UNITS="$(printf '%-16s' 'moles/s')"
# IOAPI pads varattr=var_desc to length=80 with spaces
export CLM_CN_TARGET_VAR_ATTR_VAR_DESC="$(printf '%-80s' 'Model species ${CLM_CN_TARGET_TEMPLATE_VAR_NAME}')"

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

# TODO: test for resources, reuse if available
# {setup_paths, setup_apps} isa bash util, so `get_helpers` first
function setup {
  for CMD in \
    "mkdir -p ${WORK_DIR}" \
    'get_helpers' \
    'setup_paths' \
    'setup_apps' \
    'setup_resources' \
  ; do
    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
#  echo -e "\n$ ${THIS_FN}: PDF_VIEWER='${PDF_VIEWER}'" # debugging
} # end function setup

function get_helpers {
  for CMD in \
    'get_bash_utils' \
    'get_stats_funcs' \
    'get_summarize_funcs' \
    'get_time_funcs' \
    'get_vis_funcs' \
  ; do
    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
} # end function get_helpers

function get_bash_utils {
  if [[ -z "${BASH_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: BASH_UTILS_FP not defined"
    exit 1
  fi
  if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${BASH_UTILS_FP} ${BASH_UTILS_URI}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP=='${BASH_UTILS_FP}' not readable"
    exit 1
  fi
  # This is bash, so gotta ...
  source "${BASH_UTILS_FP}"
} # end function get_bash_utils

function get_stats_funcs {
  if [[ -z "${STATS_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: STATS_FUNCS_FP not defined"
    exit 1
  fi
  if [[ ! -r "${STATS_FUNCS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${STATS_FUNCS_FP} ${STATS_FUNCS_URI}" \
    ; 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 1
  fi
} # end function get_stats_funcs

function get_summarize_funcs {
  if [[ -z "${SUMMARIZE_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: SUMMARIZE_FUNCS_FP not defined"
    exit 1
  fi
  if [[ ! -r "${SUMMARIZE_FUNCS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${SUMMARIZE_FUNCS_FP} ${SUMMARIZE_FUNCS_URI}" \
    ; 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 1
  fi
} # end function get_summarize_funcs

function get_time_funcs {
  if [[ -z "${TIME_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: TIME_FUNCS_FP not defined"
    exit 1
  fi
  if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${TIME_FUNCS_FP} ${TIME_FUNCS_URI}" \
    ; 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 1
  fi
} # end function get_time_funcs

function get_vis_funcs {
  if [[ -z "${VIS_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: VIS_FUNCS_FP not defined"
    exit 11
  fi
  if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${VIS_FUNCS_FP} ${VIS_FUNCS_URI}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot download VIS_FUNCS_FP=='${VIS_FUNCS_FP}'"
    exit 12
  fi
} # end function get_vis_funcs

function setup_resources {
  for CMD in \
    'get_raw_input' \
    'get_template_input' \
    'get_regrid' \
    'get_MSFs' \
    'get_target_template' \
  ; do
    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
} # end function setup_resources

### get the (nearly) raw input
function get_raw_input {
  if [[ -z "${CLM_CN_RAW_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CLM_CN_RAW_FP not defined"
    exit 1
  fi
  if [[ ! -r "${CLM_CN_RAW_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${CLM_CN_RAW_FP} ${CLM_CN_RAW_URI}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ ! -r "${CLM_CN_RAW_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot read CLM_CN_RAW_FP=='${CLM_CN_RAW_FP}'"
    exit 2
  fi
} # end function get_raw_input

### 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}: ERROR: TEMPLATE_INPUT_FP not defined"
    exit 3
  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}: ERROR: cannot read TEMPLATE_INPUT_FP=='${TEMPLATE_INPUT_FP}'"
    exit 4
  fi
} # end function get_template_input

### get the regridded output from the R script? no:
### only for testing. TODO: provide commandline-switch control for this
function get_regrid {
  if [[ -z "${CLM_CN_REGRID_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CLM_CN_REGRID_FP not defined"
    exit 5
  fi
  # if [[ ! -r "${CLM_CN_REGRID_FP}" ]] ; then
  #   for CMD in \
  #     "${WGET_TO_FILE} ${CLM_CN_REGRID_FP} ${CLM_CN_REGRID_URI}" \
  #   ; do
  #     echo -e "$ ${CMD}"
  #     eval "${CMD}"
  #   done
  # fi
  # if [[ ! -r "${CLM_CN_REGRID_FP}" ]] ; then
  #   echo -e "${THIS_FN}: ERROR: cannot read CLM_CN_REGRID_FP=='${CLM_CN_REGRID_FP}'"
  #   exit 6
  # fi
} # function get_regrid

function get_MSFs {
  if [[ -z "${MSF_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: MSF_FP not defined"
    exit 7
  fi
  if [[ ! -r "${MSF_FP}" ]] ; then
    if [[ ! -r "${MSF_GZ_FP}" ]] ; then
      for CMD in \
        "${WGET_TO_FILE} ${MSF_GZ_FP} ${MSF_GZ_URI}" \
      ; do
        echo -e "$ ${CMD}"
        eval "${CMD}"
      done
    fi
    if [[ -r "${MSF_GZ_FP}" ]] ; then
      for CMD in \
        "gunzip ${MSF_GZ_FP}" \
      ; do
        echo -e "$ ${CMD}"
        eval "${CMD}"
      done
    fi
  fi
  if [[ ! -r "${MSF_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot read MSF_FP=='${MSF_FP}'"
    exit 8
  fi
} # end function get_MSFs

function get_target_template {
  if [[ -z "${CLM_CN_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CLM_CN_TARGET_FP_TEMPLATE_NCL not defined"
    exit 9
  fi
  if [[ -r "${CLM_CN_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    # delete: NCL will recreate
    for CMD in \
      "rm ${CLM_CN_TARGET_FP_TEMPLATE_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ -r "${CLM_CN_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot delete CLM_CN_TARGET_FP_TEMPLATE_NCL=='${CLM_CN_TARGET_FP_TEMPLATE_NCL}'"
    exit 10
  fi
} # function get_target_template

### "Reunit" data from flux rate to molar-mass rate prior to regridding.
function reunit {
  # ncl # bail to NCL and copy script lines
  # TODO: pass commandline args to NCL
  # punt: just use envvars :-(

  for SCRIPT in \
      "${CALL_REUNIT_FP}" \
      ; do
      cat <<EOM

About to run NCL script="${SCRIPT}"

EOM
      # '-n' -> http://www.ncl.ucar.edu/Document/Functions/Built-in/print.shtml
      CMD="ncl -n ${SCRIPT}"
      echo -e "$ ${CMD}"
      eval "${CMD}"
  done
} # end function retemp_conserv

### regrid from global/unprojected to AQMEII/LCC
function regrid {
  # If this fails ...
  for CMD in \
    "Rscript ${CALL_REGRID_FP}" \
  ; do
  cat <<EOM

About to run command='${CMD}'. WARNING: may seem to hang while processing!

EOM
    eval "${CMD}"
  done
  # ... just start R ...
#  R
  # ... and source ${CALL_REGRID_FP}, e.g.,
#   source('....r')

  # After exiting R, show cwd and display output PDF.
  if [[ -r "${CLM_CN_RAW_PDF_FP}" ]] ; then
    if [[ -r "${CLM_CN_REGRID_PDF_FP}" ]] ; then
      #      "${PDF_VIEWER} ${CLM_CN_RAW_PDF_FP} &" \
      for CMD in \
          "ls -alht ${WORK_DIR}" \
          "${PDF_VIEWER} ${CLM_CN_REGRID_PDF_FP} &" \
          ; do
          echo -e "$ ${CMD}"
          eval "${CMD}"
      done
    else
      echo -e "${THIS_FN}: ERROR: regrid not plotted: ${CLM_CN_REGRID_PDF_FP}"
    fi
  else
    echo -e "${THIS_FN}: ERROR: input not plotted: ${CLM_CN_RAW_PDF_FP}"
  fi
} # end function regrid

### "Retemporalize" from annual to hourly (and put in CMAQ-style file)
### then check mass conservation
function retemp_conserv {
  # ncl # bail to NCL and copy script lines
  # TODO: pass commandline args to NCL
  # punt: just use envvars :-(

  for SCRIPT in \
      "${CALL_RETEMP_FP}" \
      "${CALL_CONSERV_FP}" \
      ; do
      cat <<EOM

About to run NCL script="${SCRIPT}"

EOM
      # '-n' -> http://www.ncl.ucar.edu/Document/Functions/Built-in/print.shtml
      CMD="ncl -n ${SCRIPT}"
      echo -e "$ ${CMD}"
      eval "${CMD}"
  done
} # end function retemp_conserv

function teardown {
  if [[ -r "${CLM_CN_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    # delete: NCL will recreate
    for CMD in \
      "rm ${CLM_CN_TARGET_FP_TEMPLATE_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ -r "${CLM_CN_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot delete CLM_CN_TARGET_FP_TEMPLATE_NCL=='${CLM_CN_TARGET_FP_TEMPLATE_NCL}'"
    exit 11
  fi

  ### show new monthly/hourly files (i.e., hourly emissions by month)

  for CMD in \
    "ls -alh ${WORK_DIR}/*.ncf" \
    "ls -alht ${WORK_DIR}/*.nc" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
} # end function teardown

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

# should always
# * begin with `setup` setup paths, apps, helpers, resources
# * end with `teardown` for tidy and testing (e.g., plot display)
for CMD in \
  'setup' \
  'reunit' \
  'regrid' \
  'retemp_conserv' \
  'teardown' \
; do
  if [[ -z "${CMD}" ]] ; then
    echo -e "${THIS_FN}::main loop: ERROR: '${CMD}' not defined"
    exit 1
  else
    echo -e "\n$ ${THIS_FN}::main loop: ${CMD}\n"
    eval "${CMD}" # comment this out for NOPing, e.g., to `source`
  fi
done

# ----------------------------------------------------------------------
# debugging
# ----------------------------------------------------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.