Source

GEIA_regrid / GEIA_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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#!/usr/bin/env bash

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

# A top-level driver for creating an inventory of marine N2O emissions over the AQMEII-NA domain from the GEIA global inventory.
# Converts GEIA marine N2O emissions in native format, spatiotemporality, and units to netCDF over AQMEII in CMAQ-preferred units.

# See https://bitbucket.org/tlroche/aqmeii_ag_soil

# Expects to run on linux. Utility dependencies include:
# * bash
# * 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 MOLAR_MASS_N2O='44.0128' # grams per mole of N2O, per wolframalpha.com
export MOLAR_MASS_N='14.0067'   # molar mass of N2 (per wolframalpha.com)/2

# 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

# for visualization (generally)
export OUTPUT_SIGNIFICANT_DIGITS='3' # see conservation report below

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

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

### helpers in this repo

export CALL_REFORMAT_FN='reformat_GEIA_to_netCDF.r'
export CALL_REFORMAT_FP="${WORK_DIR}/${CALL_REFORMAT_FN}"
export CALL_REGRID_FN='regrid_global_to_AQMEII.r'
export CALL_REGRID_FP="${WORK_DIR}/${CALL_REGRID_FN}"
export CALL_RETEMP_FN='retemp_reunit.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}"

# PLOT_FUNCS_URI='https://bitbucket.org/tlroche/geia_regrid/raw/cbb6368fb7d9924280095f5f9add6a3c62684be0/plotLayersForTimestep.r'
# PLOT_FUNCS_FN="$(basename ${PLOT_FUNCS_URI%%\?*})"
# in this repo
PLOT_FUNCS_FN='plotLayersForTimestep.r'
export PLOT_FUNCS_FP="${WORK_DIR}/${PLOT_FUNCS_FN}"

## helpers retrieved from elsewhere. TODO: R-package my code

BASH_UTILS_URI='https://bitbucket.org/tlroche/gfed-3.1_global_to_aqmeii-na/raw/95484c5d63502ab146402cedc3612dcdaf629bd7/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/clm_cn_global_to_aqmeii-na/raw/53b5f09469e2426952eb83711f72961566e4932b/netCDF.stats.to.stdout.r'
# get base filename, ignoring query string (if any)
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/618f09df886a86c9bd15880f37bbf1bf9295b638/visualization.r'
VIS_FUNCS_FN="$(basename ${VIZ_SCRIPT_URI})"
export VIS_FUNCS_FP="${WORK_DIR}/${VIS_FUNCS_FN}"

## Raw input data

# 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!
# 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' # no need for another copy
# 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}"
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})"
TEMPLATE_INPUT_FP="${WORK_DIR}/${TEMPLATE_INPUT_FN}"
TEMPLATE_INPUT_ROOT="${TEMPLATE_INPUT_FN%.*}" # everything left of the LAST dot
export TEMPLATE_INPUT_FP="${WORK_DIR}/${TEMPLATE_INPUT_FN}"
# export TEMPLATE_INPUT_BAND='1' # `ncks` makes dim=TSTEP first
export TEMPLATE_DATAVAR_NAME='emi_n2o'

# Intermediate product reformatted (to netCDF) and regridded (to AQMEII-NA)

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_DATAVAR_NAME="${GEIA_REFORMAT_DATAVAR_NAME}"
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'

# ----------------------------------------------------------------------
# retemporalization and re-unit-ing
# ----------------------------------------------------------------------

## create data container files from extents/template file
# problem: NCL currently (version=6.1.2) does not support *.ncf :-( workaround below
# replace a string with values @ runtime
# export GEIA_TARGET_FN_TEMPLATE_STR='######' # works for NCL, not bash
export GEIA_TARGET_FN_TEMPLATE_STR='@@@@@@'
GEIA_TARGET_FN_ROOT_TEMPLATE="emis_mole_N2O_${GEIA_TARGET_FN_TEMPLATE_STR}_12US1_cmaq_cb05_soa_2008ab_08c"
GEIA_TARGET_EXT_CMAQ='ncf'
GEIA_TARGET_EXT_NCL='nc'
GEIA_TARGET_FN_TEMPLATE_CMAQ="${GEIA_TARGET_FN_ROOT_TEMPLATE}.${GEIA_TARGET_EXT_CMAQ}"
GEIA_TARGET_FN_TEMPLATE_NCL="${GEIA_TARGET_FN_ROOT_TEMPLATE}.${GEIA_TARGET_EXT_NCL}"
export GEIA_TARGET_FP_TEMPLATE_CMAQ="${WORK_DIR}/${GEIA_TARGET_FN_TEMPLATE_CMAQ}"
export GEIA_TARGET_FP_TEMPLATE_NCL="${WORK_DIR}/${GEIA_TARGET_FN_TEMPLATE_NCL}"
export GEIA_TARGET_FP_CMAQ="${GEIA_TARGET_FP_TEMPLATE_CMAQ/${GEIA_TARGET_FN_TEMPLATE_STR}/${MODEL_YEAR}}"
export GEIA_TARGET_FP_NCL="${GEIA_TARGET_FP_TEMPLATE_NCL/${GEIA_TARGET_FN_TEMPLATE_STR}/${MODEL_YEAR}}"

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

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

## conservation report constants
# need 5 additional digits for float output, e.g., "4.85e+04"
# bash arithmetic gotcha: allow no spaces around '='!
export OUTPUT_SIGNIFICANT_DIGITS='3' # see conservation report below
# export CONSERV_REPORT_FIELD_WIDTH=$((OUTPUT_SIGNIFICANT_DIGITS + 5))
export CONSERV_REPORT_FIELD_WIDTH='9' # width of 'AQMEII-NA'
export CONSERV_REPORT_FLOAT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}.$((OUTPUT_SIGNIFICANT_DIGITS - 1))e"
# echo -e "${THIS_FN}: CONSERV_REPORT_FLOAT_FORMAT=${CONSERV_REPORT_FLOAT_FORMAT}" # debugging
export CONSERV_REPORT_INT_FORMAT="%${CONSERV_REPORT_FIELD_WIDTH}i"
export CONSERV_REPORT_COLUMN_SEPARATOR="  "
export CONSERV_REPORT_TITLE="Is N2O conserved from input to output? units=ton-N"
# TODO: learn ocean area in AQMEII-NA vs world ocean area (à la CLM-CN checker)
export CONSERV_REPORT_SUBTITLE='(TODO: compare global ocean area to AQMEII-NA ocean area)'

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

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

# ----------------------------------------------------------------------
# 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_plot_funcs' \
    'get_stats_funcs' \
    'get_summarize_funcs' \
    'get_time_funcs' \
    'get_vis_funcs' \
    'get_reformat' \
    'get_regrid' \
    'get_retemp' \
    'get_conserv' \
  ; 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 setup_resources {
  for CMD in \
    'get_map_table' \
    'get_geia_raw' \
    'get_template_input' \
    'put_geia_target_ncl' \
    'put_geia_target_cmaq' \
    'get_geia_reformat' \
    'get_geia_regrid' \
  ; 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

function get_map_table {
  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 [[ ! -r "${MAP_TABLE_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot download MAP_TABLE_FP=='${MAP_TABLE_FP}'"
    exit 2
  fi
} # end function get_map_table

function get_geia_raw {
  if [[ -z "${GEIA_RAW_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: GEIA_RAW_FP not defined"
    exit 11
  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 [[ ! -r "${GEIA_RAW_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot download GEIA_RAW_FP=='${GEIA_RAW_FP}'"
    exit 12
  fi
} # end function get_geia_raw

function get_template_input {
  if [[ -z "${TEMPLATE_INPUT_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: TEMPLATE_INPUT_FP not defined"
    exit 13
  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
  if [[ ! -r "${TEMPLATE_INPUT_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot download TEMPLATE_INPUT_FP=='${TEMPLATE_INPUT_FP}'"
    exit 14
  fi
} # end function get_template_input

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

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

function get_geia_reformat {
  if [[ -z "${GEIA_REFORMAT_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: GEIA_REFORMAT_FP not defined"
    exit 1
  fi
# only for testing. TODO: flag control
#   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
#   if [[ ! -r "${GEIA_REFORMAT_FP}" ]] ; then
#     echo -e "${THIS_FN}: ERROR: cannot download GEIA_REFORMAT_FP=='${GEIA_REFORMAT_FP}'"
#     exit 2
#   fi
} # end function get_geia_reformat

function get_geia_regrid {
  if [[ -z "${GEIA_REGRID_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: GEIA_REGRID_FP not defined"
    exit 1
  fi
# only for testing. TODO: flag control
#   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
#   if [[ ! -r "${GEIA_REGRID_FP}" ]] ; then
#     echo -e "${THIS_FN}: ERROR: cannot download GEIA_REGRID_FP=='${GEIA_REGRID_FP}'"
#     exit 2
#   fi
} # end function get_geia_regrid

function get_bash_utils {
  if [[ -z "${BASH_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: BASH_UTILS_FP not defined"
    exit 1
  fi
  # in this repository
#   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_plot_funcs {
  if [[ -z "${PLOT_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: PLOT_FUNCS_FP not defined"
    exit 1
  fi
  # is in this repo
#  if [[ ! -r "${PLOT_FUNCS_FP}" ]] ; then
#    for CMD in \
#      "${WGET_TO_FILE} ${PLOT_FUNCS_FP} ${PLOT_FUNCS_URI}" \
#    ; do
#      echo -e "$ ${CMD}"
#      eval "${CMD}"
#    done
#  fi
  if [[ ! -r "${PLOT_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: PLOT_FUNCS_FP=='${PLOT_FUNCS_FP}' not readable"
    exit 1
  fi
} # end function get_plot_funcs

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 1
  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}::${FUNCNAME[0]}: ERROR: VIS_FUNCS_FP=='${VIS_FUNCS_FP}' not readable"
    exit 1
  fi
} # end function get_vis_funcs

function get_reformat {
  if [[ -z "${CALL_REFORMAT_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CALL_REFORMAT_FP not defined"
    exit 1
  fi
  # is in this repo
#  if [[ ! -r "${CALL_REFORMAT_FP}" ]] ; then
#    for CMD in \
#      "${WGET_TO_FILE} ${CALL_REFORMAT_FP} ${REFORMAT_URI}" \
#    ; do
#      echo -e "$ ${CMD}"
#      eval "${CMD}"
#    done
#  fi
  if [[ ! -r "${CALL_REFORMAT_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: CALL_REFORMAT_FP=='${CALL_REFORMAT_FP}' not readable"
    exit 1
  fi
} # end function get_reformat

function get_regrid {
  if [[ -z "${CALL_REGRID_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CALL_REGRID_FP not defined"
    exit 1
  fi
  # is in this repo
#  if [[ ! -r "${CALL_REGRID_FP}" ]] ; then
#    for CMD in \
#      "${WGET_TO_FILE} ${CALL_REGRID_FP} ${REGRID_URI}" \
#    ; do
#      echo -e "$ ${CMD}"
#      eval "${CMD}"
#    done
#  fi
  if [[ ! -r "${CALL_REGRID_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: CALL_REGRID_FP=='${CALL_REGRID_FP}' not readable"
    exit 1
  fi
} # end function get_regrid

function get_retemp {
  if [[ -z "${CALL_RETEMP_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CALL_RETEMP_FP not defined"
    exit 1
  fi
  # is in this repo
#  if [[ ! -r "${CALL_RETEMP_FP}" ]] ; then
#    for CMD in \
#      "${WGET_TO_FILE} ${CALL_RETEMP_FP} ${RETEMP_URI}" \
#    ; do
#      echo -e "$ ${CMD}"
#      eval "${CMD}"
#    done
#  fi
  if [[ ! -r "${CALL_RETEMP_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: CALL_RETEMP_FP=='${CALL_RETEMP_FP}' not readable"
    exit 1
  fi
} # end function get_retemp

function get_conserv {
  if [[ -z "${CALL_CONSERV_FP}" ]] ; then
    echo -e "${THIS_FN}: ERROR: CALL_CONSERV_FP not defined"
    exit 1
  fi
  # is in this repo
#  if [[ ! -r "${CALL_CONSERV_FP}" ]] ; then
#    for CMD in \
#      "${WGET_TO_FILE} ${CALL_CONSERV_FP} ${CONSERV_URI}" \
#    ; do
#      echo -e "$ ${CMD}"
#      eval "${CMD}"
#    done
#  fi
  if [[ ! -r "${CALL_CONSERV_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: CALL_CONSERV_FP=='${CALL_CONSERV_FP}' not readable"
    exit 1
  fi
} # end function get_conserv

### reformat from native to netCDF
function reformat {
  # If this fails ...
  for CMD in \
    "Rscript ${CALL_REFORMAT_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_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
} # end function reformat

### regrid from global/lon-lat to AQMEII-NA/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.
  for CMD in \
    "ls -alht ${WORK_DIR}" \
    "${PDF_VIEWER} ${GEIA_REGRID_PDF_FP} &" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
} # end function regrid

### "retemporalize" from annual to hourly (and put in CMAQ-style file)
function retemp {
  # remove any pre-existing output data container files

#   # start debugging-----------------------------------------------------
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_TEMPLATE_CMAQ='${GEIA_TARGET_FP_TEMPLATE_CMAQ}'"
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_TEMPLATE_NCL= '${GEIA_TARGET_FP_TEMPLATE_NCL}'"
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_CMAQ=         '${GEIA_TARGET_FP_CMAQ}'"
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_NCL=          '${GEIA_TARGET_FP_NCL}'"
#   #   end debugging-----------------------------------------------------

  if [[ -r "${GEIA_TARGET_FP_CMAQ}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_CMAQ}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi

  if [[ -r "${GEIA_TARGET_FP_TEMPLATE_CMAQ}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_TEMPLATE_CMAQ}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi

  if [[ -r "${GEIA_TARGET_FP_NCL}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi

  if [[ -r "${GEIA_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_TEMPLATE_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi

#   # start debugging-----------------------------------------------------
#   for CMD in \
#     "ls -alh ${GEIA_TARGET_FP_NCL}" \
#     "ls -alh ${GEIA_TARGET_FP_CMAQ}" \
#   ; do
#     echo -e "$ ${CMD}"
#     eval "${CMD}"
#   done
#   #   end debugging-----------------------------------------------------


  # TODO: pass commandline args to NCL
  # punt: just use envvars :-(
#   ncl bail to NCL and copy script lines, or ...
  # '-n' -> http://www.ncl.ucar.edu/Document/Functions/Built-in/print.shtml
  for CMD in \
    "ncl -n ${CALL_RETEMP_FP}" \
  ; do
  cat <<EOM

About to run command="${CMD}"

EOM
    eval "${CMD}"
  done

  # don't move, copy: NCL won't *read* .ncf either :-(
#     "mv ${GEIA_TARGET_FP_NCL} ${GEIA_TARGET_FP_CMAQ}" \
  for CMD in \
    "cp ${GEIA_TARGET_FP_NCL} ${GEIA_TARGET_FP_CMAQ}" \
    "ls -alht ${WORK_DIR}/*.${GEIA_TARGET_EXT_CMAQ}" \
    "ls -alht ${WORK_DIR}/*.${GEIA_TARGET_EXT_NCL}" \
    "ncdump -h ${GEIA_TARGET_FP_CMAQ}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
} # end function retemp

### check conservation of mass input -> output
### TODO: pass commandline args to NCL
### punt: just use envvars :-(
function check_conserv {
  # ncl bail to NCL and copy script lines, or ...
  for CMD in \
    "ncl -n ${CALL_CONSERV_FP}" \
  ; do
  cat <<EOM

About to run command="${CMD}"

EOM
    eval "${CMD}"
  done
} # end function check_conserv

### cleanup/teardown
function teardown {
  ### remove the *.nc required only for NCL

#   # start debugging-----------------------------------------------------
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_TEMPLATE_NCL= '${GEIA_TARGET_FP_TEMPLATE_NCL}'"
#   echo -e "${THIS_FN}: GEIA_TARGET_FP_NCL=          '${GEIA_TARGET_FP_NCL}'"
#   #   end debugging-----------------------------------------------------

  if [[ -r "${GEIA_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_TEMPLATE_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ -r "${GEIA_TARGET_FP_TEMPLATE_NCL}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot delete GEIA_TARGET_FP_TEMPLATE_NCL=='${GEIA_TARGET_FP_TEMPLATE_NCL}'"
    exit 11
  fi

  if [[ -r "${GEIA_TARGET_FP_NCL}" ]] ; then
    for CMD in \
      "rm ${GEIA_TARGET_FP_NCL}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ -r "${GEIA_TARGET_FP_NCL}" ]] ; then
    echo -e "${THIS_FN}: ERROR: cannot delete GEIA_TARGET_FP_NCL=='${GEIA_TARGET_FP_NCL}'"
    exit 12
  fi

  ### show new annual hourly files
  for CMD in \
    "ls -alht ${WORK_DIR}/*.${GEIA_TARGET_EXT_CMAQ}" \
    "ls -alht ${WORK_DIR}/*.${GEIA_TARGET_EXT_NCL}" \
    "ncdump -h ${GEIA_TARGET_FP_CMAQ}" \
  ; do
    echo -e "$ ${CMD}"
    eval "${CMD}"
  done
} # end function teardown

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

# should always
# * begin with `setup` to do `module add`
# * end with `teardown` for tidy and testing (e.g., plot display)
for CMD in \
  'setup' \
  'reformat' \
  'regrid' \
  'retemp' \
  'check_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
# ----------------------------------------------------------------------