Source

EDGAR-4.2_minus_soil_and_biomass_to_AQMEII-NA / EDGAR_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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#!/usr/bin/env bash

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

# A top-level driver for creating a single, composite inventory of N2O emissions over the AQMEII-NA domain from a number of EDGAR inventories of mostly anthropogenic sources over the globe. It

# * sums several EDGAR global inventories
# * converts the summed inventory from flux rate to mass-rate
# * regrids the reunit-ed global/unprojected data to AQMEII-NA, an LCC-projected subdomain
# * converts the annual data to hourly
# * checks conservation of mass over the conversions

# See https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na

# Requirements include:

# * definitely

# ** bash: required to run this script. Known to work with bash --version==3.2.25

# * potentially initially. If you have not already retrieved regrid_utils (see `get_regrid_utils`), you will need

# ** git: to clone their repo
# ** web access (HTTP currently)

#   These must be available to the script when initially run.

# * ultimately

# ** basename, dirname
# ** cp
# ** curl or wget (latter {preferred, coded} for ability to deal with redirects)
# ** gunzip, unzip
# ** 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
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
# where this is hosted
PROJECT_WEBSITE='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na'
NETCDF_EXT_NCL='nc'   # NCL prefers the "canonical" netCDF extension
NETCDF_EXT_CMAQ='ncf' # CMAQ, as is so often the case, is nonstandard :-(

# 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}"
# dimensions (for R plots--units?) for single-frame plots
export SINGLE_FRAME_PLOT_HEIGHT='10'
export SINGLE_FRAME_PLOT_WIDTH='15'
# dimensions for multi-frame plots
export TWELVE_MONTH_PLOT_HEIGHT='120'
export TWELVE_MONTH_PLOT_WIDTH='20'

## 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=kg N2O"
export CONSERV_REPORT_SUBTITLE="(note (US land area)/(earth land area) ~= 6.15e-02)"

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

### helpers in this repo

export EDGAR_NONAG_REUNIT_HELPER_FN='sum_reunit.r'
export EDGAR_NONAG_REUNIT_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_HELPER_FN}"
export EDGAR_NONAG_REGRID_HELPER_FN='regrid_global_to_AQMEII.r'
export EDGAR_NONAG_REGRID_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_HELPER_FN}"
export EDGAR_NONAG_RETEMP_HELPER_FN='retemp.ncl'
export EDGAR_NONAG_RETEMP_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_RETEMP_HELPER_FN}"
export EDGAR_NONAG_CONSERV_HELPER_FN='check_conservation.ncl'
export EDGAR_NONAG_CONSERV_HELPER_FP="${WORK_DIR}/${EDGAR_NONAG_CONSERV_HELPER_FN}"

### helpers retrieved from elsewhere. TODO: create NCL and R packages

# path to a project, not a .git
REGRID_UTILS_URI='https://bitbucket.org/tlroche/regrid_utils'
REGRID_UTILS_PN="$(basename ${REGRID_UTILS_URI})" # project name
# can also use   'git@bitbucket.org:tlroche/regrid_utils' if supported
# path to a folder, not a file: needed by NCL to get to initial helpers
export REGRID_UTILS_FP="${WORK_DIR}/${REGRID_UTILS_PN}" # folder, not file

export BASH_UTILS_FN='bash_utilities.sh'
# export BASH_UTILS_FP="${REGRID_UTILS_FP}/${BASH_UTILS_FN}"
# no, allow user to override repo code: see `get_bash_utils`
export BASH_UTILS_FP="${WORK_DIR}/${BASH_UTILS_FN}"

export STATS_FUNCS_FN='netCDF.stats.to.stdout.r'
export STATS_FUNCS_FP="${WORK_DIR}/${STATS_FUNCS_FN}"

export STRING_FUNCS_FN='string.ncl'
export STRING_FUNCS_FP="${WORK_DIR}/${STRING_FUNCS_FN}"

export SUMMARIZE_FUNCS_FN='summarize.ncl'
export SUMMARIZE_FUNCS_FP="${WORK_DIR}/${SUMMARIZE_FUNCS_FN}"

export TIME_FUNCS_FN='time.ncl'
export TIME_FUNCS_FP="${WORK_DIR}/${TIME_FUNCS_FN}"

export VIS_FUNCS_FN='visualization.r'
export VIS_FUNCS_FP="${WORK_DIR}/${VIS_FUNCS_FN}"

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

# for name generation
EDGAR_NONAG_FN_ROOT='edgar_non-ag'

### raw input

# Raw input data is global/unprojected EDGAR netCDF, with coordvars=
# lat==(-90,+90)
# lon==(0,+360), hence need to
export ROTATE_INPUT="true"
# after summing

# buncha individual netCDF files (see .tar.gz below) with same schema
# for category mappings, see
# https://github.com/TomRoche/cornbeltN2O/wiki/Simulation-of-N2O-Production-and-Transport-in-the-US-Cornbelt-Compared-to-Tower-Measurements#wiki-EDGAR-4.2-categories
EDGAR_NONAG_COMBUST_FN='v42_N2O_2008_IPCC_1A1_1A2_1B1b.0.1x0.1.nc'
EDGAR_NONAG_NONROAD_FN='v42_N2O_2008_IPCC_1A3a_c_d_e.0.1x0.1.nc'
EDGAR_NONAG_ROAD_FN='v42_N2O_2008_IPCC_1A3b.0.1x0.1.nc'
EDGAR_NONAG_RESID_FN='v42_N2O_2008_IPCC_1A4.0.1x0.1.nc'
EDGAR_NONAG_PETRO_FN='v42_N2O_2008_IPCC_1B2a_c.0.1x0.1.nc'
EDGAR_NONAG_IND_FN='v42_N2O_2008_IPCC_2_3.0.1x0.1.nc'
EDGAR_NONAG_MANURE_FN='v42_N2O_2008_IPCC_4B.0.1x0.1.nc'
EDGAR_NONAG_RUNOFF_FN='v42_N2O_2008_IPCC_4D3.0.1x0.1.nc'
EDGAR_NONAG_WASTE_FN='v42_N2O_2008_IPCC_6.0.1x0.1.nc'
EDGAR_NONAG_FFF_FN='v42_N2O_2008_IPCC_7A.0.1x0.1.nc'
EDGAR_NONAG_TROPO_FN='v42_N2O_2008_IPCC_7B_7C.0.1x0.1.nc'

export EDGAR_NONAG_COMBUST_FP="${WORK_DIR}/${EDGAR_NONAG_COMBUST_FN}"
export EDGAR_NONAG_NONROAD_FP="${WORK_DIR}/${EDGAR_NONAG_NONROAD_FN}"
export EDGAR_NONAG_ROAD_FP="${WORK_DIR}/${EDGAR_NONAG_ROAD_FN}"
export EDGAR_NONAG_RESID_FP="${WORK_DIR}/${EDGAR_NONAG_RESID_FN}"
export EDGAR_NONAG_PETRO_FP="${WORK_DIR}/${EDGAR_NONAG_PETRO_FN}"
export EDGAR_NONAG_IND_FP="${WORK_DIR}/${EDGAR_NONAG_IND_FN}"
export EDGAR_NONAG_MANURE_FP="${WORK_DIR}/${EDGAR_NONAG_MANURE_FN}"
export EDGAR_NONAG_RUNOFF_FP="${WORK_DIR}/${EDGAR_NONAG_RUNOFF_FN}"
export EDGAR_NONAG_WASTE_FP="${WORK_DIR}/${EDGAR_NONAG_WASTE_FN}"
export EDGAR_NONAG_FFF_FP="${WORK_DIR}/${EDGAR_NONAG_FFF_FN}"
export EDGAR_NONAG_TROPO_FP="${WORK_DIR}/${EDGAR_NONAG_TROPO_FN}"

# If we don't have the files, get zip from my repository.
EDGAR_NONAG_GZ_URI='https://bitbucket.org/tlroche/edgar-4.2_minus_soil_and_biomass_to_aqmeii-na/downloads/EDGAR-4.2_N2O_2008_minus_soil_and_biomass.nc.tar.gz'
EDGAR_NONAG_GZ_FN="$(basename ${EDGAR_NONAG_GZ_URI})"
export EDGAR_NONAG_GZ_FP="${WORK_DIR}/${EDGAR_NONAG_GZ_FN}"

# TODO: automate getting this metadata from the netCDF
export EDGAR_NONAG_RAW_DATAVAR_NAME='emi_n2o'
# export EDGAR_NONAG_RAW_DATAVAR_TYPE='float' # in `ncdump -h`
export EDGAR_NONAG_RAW_DATAVAR_TYPE='FLT4S' # == float: see raster.pdf::dataType
# 6 datavar attributes in raw files, one of which is 'total_emi_n2o' in kg/year
export EDGAR_NONAG_RAW_DATAVAR_STANDARD_NAME='tendency_of_atmosphere_mass_content_of_nitrous_oxide_due_to_emission'
export EDGAR_NONAG_RAW_DATAVAR_LONG_NAME='Emissions of N2O - '
export EDGAR_NONAG_RAW_DATAVAR_CELL_METHOD='time: mean (interval: 1 year, 366 days)'
export EDGAR_NONAG_RAW_DATAVAR_COMMENT=' (see http://edgar.jrc.ec.europa.eu/methodology.php#12sou for the definitions of the single sources)'
export EDGAR_NONAG_RAW_DATAVAR_UNITS='kg m-2 s-1'
# export EDGAR_NONAG_RAW_DATAVAR_NA not used--hopefully they have no missing data
export EDGAR_NONAG_RAW_DATAVAR_COORD_X_NAME='lon'
export EDGAR_NONAG_RAW_DATAVAR_COORD_Y_NAME='lat'

### 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/geia_regrid/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_DATAVAR_NAME='emi_n2o'

### global/raw gridcell area data
# Units for the above are flux: convert to mass rate using conveniently provided gridcell areas (thank you, EDGAR!)
EDGAR_NONAG_RAW_AREAS_URI='http://edgar.jrc.ec.europa.eu/dataset/edgarv4_0/info/area_0.1x0.1.nc'
EDGAR_NONAG_RAW_AREAS_FN="$(basename ${EDGAR_NONAG_RAW_AREAS_URI})"
export EDGAR_NONAG_RAW_AREAS_FP="${WORK_DIR}/${EDGAR_NONAG_RAW_AREAS_FN}"
export EDGAR_NONAG_RAW_AREAS_DATAVAR_NAME='cell_area'

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

### Intermediate product: sum of raw inputs, "reunit-ed" from flux rate to molar-mass rate
EDGAR_NONAG_REUNIT_FN_ROOT="${EDGAR_NONAG_FN_ROOT}_summed_massed"

EDGAR_NONAG_REUNIT_FN="${EDGAR_NONAG_REUNIT_FN_ROOT}.${NETCDF_EXT_NCL}"
export EDGAR_NONAG_REUNIT_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_FN}"
export EDGAR_NONAG_REUNIT_DATAVAR_NAME="${EDGAR_NONAG_RAW_DATAVAR_NAME}"
export EDGAR_NONAG_REUNIT_DATAVAR_TYPE="${EDGAR_NONAG_RAW_DATAVAR_TYPE}"
export EDGAR_NONAG_REUNIT_DATAVAR_UNITS='moles/s' # was kg/m^2/s
export EDGAR_NONAG_REUNIT_DATAVAR_LONG_NAME="${EDGAR_NONAG_RAW_DATAVAR_LONG_NAME}"
export EDGAR_NONAG_REUNIT_DATAVAR_COORD_X_NAME="${EDGAR_NONAG_RAW_DATAVAR_COORD_X_NAME}"
export EDGAR_NONAG_REUNIT_DATAVAR_COORD_Y_NAME="${EDGAR_NONAG_RAW_DATAVAR_COORD_Y_NAME}"

## visualize it
EDGAR_NONAG_REUNIT_PDF_FN="${EDGAR_NONAG_REUNIT_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
export EDGAR_NONAG_REUNIT_PDF_FP="${WORK_DIR}/${EDGAR_NONAG_REUNIT_PDF_FN}" # path to PDF output

### Intermediate product: netCDF regridded to AQMEII-NA
EDGAR_NONAG_REGRID_FN_ROOT="${EDGAR_NONAG_REUNIT_FN_ROOT}_regrid"
EDGAR_NONAG_REGRID_FN="${EDGAR_NONAG_REGRID_FN_ROOT}.${NETCDF_EXT_NCL}"
export EDGAR_NONAG_REGRID_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_FN}"

export EDGAR_NONAG_REGRID_DATAVAR_NAME="${EDGAR_NONAG_RAW_DATAVAR_NAME}"
export EDGAR_NONAG_REGRID_X_VAR_NAME='COL'
export EDGAR_NONAG_REGRID_Y_VAR_NAME='ROW'
# export EDGAR_NONAG_REGRID_TIME_VAR_NAME='TSTEP' # more IOAPI-like than 'time'
# export EDGAR_NONAG_REGRID_TIME_VAR_UNIT='month (1=2, 2=Jan, ... 13=Dec, 13=14)'

## visualize it
EDGAR_NONAG_REGRID_PDF_FN="${EDGAR_NONAG_REGRID_FN_ROOT}_$(date +${DATE_FORMAT}).pdf"
export EDGAR_NONAG_REGRID_PDF_FP="${WORK_DIR}/${EDGAR_NONAG_REGRID_PDF_FN}" # path to PDF output

### data container files
### 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
# TODO: driver must ensure output_fp_template does not exist!
# export CMAQ_TARGET_FN_TEMPLATE_STR='######' # works for NCL, not bash
export CMAQ_TARGET_FN_TEMPLATE_STR='@@@@@@'
CMAQ_TARGET_FN_ROOT_TEMPLATE="emis_mole_N2O_${CMAQ_TARGET_FN_TEMPLATE_STR}_12US1_cmaq_cb05_soa_2008ab_08c"
CMAQ_TARGET_FN_TEMPLATE_CMAQ="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${NETCDF_EXT_CMAQ}"
CMAQ_TARGET_FN_TEMPLATE_NCL="${CMAQ_TARGET_FN_ROOT_TEMPLATE}.${NETCDF_EXT_NCL}"
export CMAQ_TARGET_FP_TEMPLATE_CMAQ="${WORK_DIR}/${CMAQ_TARGET_FN_TEMPLATE_CMAQ}"
export CMAQ_TARGET_FP_TEMPLATE_NCL="${WORK_DIR}/${CMAQ_TARGET_FN_TEMPLATE_NCL}"
export CMAQ_TARGET_FP_NCL="${CMAQ_TARGET_FP_TEMPLATE_NCL/${CMAQ_TARGET_FN_TEMPLATE_STR}/${MODEL_YEAR}}"

## global metadata for both {template, "real" monthly/hourly data container} files
export CMAQ_TARGET_FILE_ATTR_HISTORY="see ${PROJECT_WEBSITE}"

### output file, datavar
# replace template string with value
export CMAQ_TARGET_FP_CMAQ="${CMAQ_TARGET_FP_TEMPLATE_CMAQ/${CMAQ_TARGET_FN_TEMPLATE_STR}/${MODEL_YEAR}}"
## metadata for datavar in both {template, "real" monthly/hourly data container} files
export CMAQ_TARGET_DATAVAR_NAME='N2O'
# IOAPI pads varattr=long_name to length=16 with spaces
export CMAQ_TARGET_DATAVAR_ATTR_LONG_NAME="$(printf '%-16s' "${CMAQ_TARGET_DATAVAR_NAME}")"
# IOAPI pads varattr=units to length=16 with spaces
export CMAQ_TARGET_DATAVAR_ATTR_UNITS="$(printf '%-16s' "${EDGAR_NONAG_REUNIT_DATAVAR_UNITS}")"
# IOAPI pads varattr=var_desc to length=80 with spaces
export CMAQ_TARGET_DATAVAR_ATTR_VAR_DESC="$(printf '%-80s' "Model species ${CMAQ_TARGET_DATAVAR_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
} # end function setup

function get_helpers {
  for CMD in \
    'get_regrid_utils' \
    'get_bash_utils' \
    'get_stats_funcs' \
    'get_string_funcs' \
    'get_summarize_funcs' \
    'get_time_funcs' \
    'get_vis_funcs' \
    'get_reunit' \
    'get_regrid' \
    'get_retemp' \
    'get_conserv' \
  ; do
    if [[ -z "${CMD}" ]] ; then
      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
      exit 2
    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

# This gets "package"=regrid_utils from which the remaining helpers are (at least potentially) drawn.
function get_regrid_utils {
  if [[ -z "${REGRID_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: REGRID_UTILS_FP not defined"
    exit 3
  fi
  if [[ ! -r "${REGRID_UTILS_FP}" ]] ; then
#      "rm -fr ${REGRID_UTILS_FP}/.git" \
    for CMD in \
      "git clone ${REGRID_UTILS_URI}.git" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ ! -r "${REGRID_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot download REGRID_UTILS_FP=='${REGRID_UTILS_FP}'"
    exit 4
  fi  
} # end function get_regrid_utils

# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
# To override, copy/mod to ${BASH_UTILS_FP} before running this script.
function get_bash_utils {
  if [[ -z "${BASH_UTILS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: BASH_UTILS_FP not defined"
    exit 5
  fi
  if [[ ! -r "${BASH_UTILS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${BASH_UTILS_FN} ${BASH_UTILS_FP}" \
    ; 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 6
  fi
  # This is bash, so gotta ...
  source "${BASH_UTILS_FP}"
  # ... for its functions to be available later in this script
} # end function get_bash_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
  fi
  if [[ ! -r "${STATS_FUNCS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${STATS_FUNCS_FN} ${STATS_FUNCS_FP}" \
    ; 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 10
  fi
} # end function get_stats_funcs

# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
# To override, copy/mod to ${STRING_FUNCS_FP} before running this script.
function get_string_funcs {
  if [[ -z "${STRING_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: STRING_FUNCS_FP not defined"
    exit 9
  fi
  if [[ ! -r "${STRING_FUNCS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${STRING_FUNCS_FN} ${STRING_FUNCS_FP}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    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
  fi
} # end function get_string_funcs

# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
# To override, copy/mod to ${SUMMARIZE_FUNCS_FP} before running this script.
function get_summarize_funcs {
  if [[ -z "${SUMMARIZE_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: SUMMARIZE_FUNCS_FP not defined"
    exit 13
  fi
  if [[ ! -r "${SUMMARIZE_FUNCS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${SUMMARIZE_FUNCS_FN} ${SUMMARIZE_FUNCS_FP}" \
    ; 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 14
  fi
} # end function get_summarize_funcs

# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
# To override, copy/mod to ${TIME_FUNCS_FP} before running this script.
function get_time_funcs {
  if [[ -z "${TIME_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: TIME_FUNCS_FP not defined"
    exit 15
  fi
  if [[ ! -r "${TIME_FUNCS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${TIME_FUNCS_FN} ${TIME_FUNCS_FP}" \
    ; 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 16
  fi
} # end function get_time_funcs

# isa regrid_utils from https://bitbucket.org/tlroche/regrid_utils
# To override, copy/mod to ${VIS_FUNCS_FP} before running this script.
function get_vis_funcs {
  if [[ -z "${VIS_FUNCS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: VIS_FUNCS_FP not defined"
    exit 17
  fi
  if [[ ! -r "${VIS_FUNCS_FP}" ]] ; then
    # copy from downloaded regrid_utils
    for CMD in \
      "cp ${REGRID_UTILS_FP}/${VIS_FUNCS_FN} ${VIS_FUNCS_FP}" \
    ; 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 18
  fi
} # end function get_vis_funcs

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

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

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

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

function setup_resources {
#     'get_regrid' \
#     'get_MSFs' \
#     'get_target_template' \
  for CMD in \
    'get_raw_input' \
    'get_areas_input' \
    'get_template_input' \
  ; do
    if [[ -z "${CMD}" ]] ; then
      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: '${CMD}' not defined"
      exit 27
    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 (several) raw inputs
function get_raw_input {
  if [[
      -z "${EDGAR_NONAG_COMBUST_FP}" ||
      -z "${EDGAR_NONAG_NONROAD_FP}" ||
      -z "${EDGAR_NONAG_ROAD_FP}" ||
      -z "${EDGAR_NONAG_RESID_FP}" ||
      -z "${EDGAR_NONAG_PETRO_FP}" ||
      -z "${EDGAR_NONAG_IND_FP}" ||
      -z "${EDGAR_NONAG_MANURE_FP}" ||
      -z "${EDGAR_NONAG_RUNOFF_FP}" ||
      -z "${EDGAR_NONAG_WASTE_FP}" ||
      -z "${EDGAR_NONAG_FFF_FP}" ||
      -z "${EDGAR_NONAG_TROPO_FP}"
    ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: not all raw inputs defined"
    exit 1
  fi
  if [[
      ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
      ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
      ! -r "${EDGAR_NONAG_ROAD_FP}" ||
      ! -r "${EDGAR_NONAG_RESID_FP}" ||
      ! -r "${EDGAR_NONAG_PETRO_FP}" ||
      ! -r "${EDGAR_NONAG_IND_FP}" ||
      ! -r "${EDGAR_NONAG_MANURE_FP}" ||
      ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
      ! -r "${EDGAR_NONAG_WASTE_FP}" ||
      ! -r "${EDGAR_NONAG_FFF_FP}" ||
      ! -r "${EDGAR_NONAG_TROPO_FP}"
    ]] ; then
    # download and decompress the files
    for CMD in \
      "${WGET_TO_FILE} - ${EDGAR_NONAG_GZ_URI} | tar xfz -" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[
      ! -r "${EDGAR_NONAG_COMBUST_FP}" ||
      ! -r "${EDGAR_NONAG_NONROAD_FP}" ||
      ! -r "${EDGAR_NONAG_ROAD_FP}" ||
      ! -r "${EDGAR_NONAG_RESID_FP}" ||
      ! -r "${EDGAR_NONAG_PETRO_FP}" ||
      ! -r "${EDGAR_NONAG_IND_FP}" ||
      ! -r "${EDGAR_NONAG_MANURE_FP}" ||
      ! -r "${EDGAR_NONAG_RUNOFF_FP}" ||
      ! -r "${EDGAR_NONAG_WASTE_FP}" ||
      ! -r "${EDGAR_NONAG_FFF_FP}" ||
      ! -r "${EDGAR_NONAG_TROPO_FP}"
    ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: not all raw inputs are readable"
    exit 2
  fi
} # end function get_raw_input

function get_areas_input {
  if [[ -z "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: EDGAR_NONAG_RAW_AREAS_FP not defined"
    exit 3
  fi
  if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
    for CMD in \
      "${WGET_TO_FILE} ${EDGAR_NONAG_RAW_AREAS_FP} ${EDGAR_NONAG_RAW_AREAS_URI}" \
    ; do
      echo -e "$ ${CMD}"
      eval "${CMD}"
    done
  fi
  if [[ ! -r "${EDGAR_NONAG_RAW_AREAS_FP}" ]] ; then
    echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot download EDGAR_NONAG_RAW_AREAS_FP=='${EDGAR_NONAG_RAW_AREAS_FP}'"
    exit 4
  fi
} # end function get_areas_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}::${FUNCNAME[0]}: ERROR: TEMPLATE_INPUT_FP not defined"
    exit 9
  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}::${FUNCNAME[0]}: ERROR: cannot read TEMPLATE_INPUT_FP=='${TEMPLATE_INPUT_FP}'"
    exit 10
  fi
} # end function get_template_input

# ======================================================================

### Sum raw inputs and "reunit" sum from flux rate to molar-mass rate prior to regridding.
### TODO: pass commandline args to R
### punt: just use envvars :-(
function reunit {
#   # ... just start R ...
#   R
#   # ... and maybe source ${EDGAR_NONAG_REUNIT_HELPER_FP}, e.g.,
#   source('....r')

  if [[ -r "${EDGAR_NONAG_REUNIT_FP}" ]] ; then
    echo -e "${THIS_FN}: sum/reunit-ed data=='${EDGAR_NONAG_REUNIT_FP}' exists, skipping"
  else
    for CMD in \
      "Rscript ${EDGAR_NONAG_REUNIT_HELPER_FP}" \
    ; do
    cat <<EOM

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

EOM
      eval "${CMD}"
    done

    # After exiting R, show cwd and display output PDF ... maybe
    if [[ ! -r "${EDGAR_NONAG_REUNIT_FP}" ]] ; then
        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to create sum/reunit-ed output='${CLM_CN_REGRID_PDF_FP}'"
        exit 40
    fi

    if [[ -r "${EDGAR_NONAG_REUNIT_PDF_FP}" ]] ; then
      for CMD in \
          "ls -alht ${EDGAR_NONAG_REUNIT_PDF_FP}" \
          "${PDF_VIEWER} ${EDGAR_NONAG_REUNIT_PDF_FP} &" \
          ; do
          echo -e "$ ${CMD}"
          eval "${CMD}"
      done
    else
      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to plot sum/reunit-ed input='${EDGAR_NONAG_REUNIT_PDF_FP}'"
    fi
  fi # end if [[ -r "${EDGAR_NONAG_REUNIT_FP}" ]]
} # end function reunit

### regrid from global/unprojected to AQMEII/LCC
function regrid {
#   # ... just start R ...
#   R
#   # ... and maybe source ${EDGAR_NONAG_REGRID_HELPER_FP}, e.g.,
#   source('....r')

  if [[ -r "${EDGAR_NONAG_REGRID_FP}" ]] ; then
    echo -e "${THIS_FN}: regridded data=='${EDGAR_NONAG_REGRID_FP}' exists, skipping"
  else
    for CMD in \
      "Rscript ${EDGAR_NONAG_REGRID_HELPER_FP}" \
    ; do
    cat <<EOM

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

EOM
      eval "${CMD}"
    done

    # After exiting R, show cwd and display output PDF ... maybe
    if [[ ! -r "${EDGAR_NONAG_REGRID_FP}" ]] ; then
        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to create regrid output='${EDGAR_NONAG_REGRID_PDF_FP}'"
        exit 40
    fi

    if [[ -r "${EDGAR_NONAG_REGRID_PDF_FP}" ]] ; then
      for CMD in \
          "ls -alht ${EDGAR_NONAG_REGRID_PDF_FP}" \
          "${PDF_VIEWER} ${EDGAR_NONAG_REGRID_PDF_FP} &" \
          ; do
          echo -e "$ ${CMD}"
          eval "${CMD}"
      done
    else
      echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: failed to plot regrid output='${EDGAR_NONAG_REGRID_PDF_FP}'"
    fi
  fi # end if [[ -r "${EDGAR_NONAG_REGRID_FP}" ]]
} # end function regrid

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

  if [[ -r "${CMAQ_TARGET_FP_CMAQ}" ]] ; then
    echo -e "${THIS_FN}: retemporalized data=='${CMAQ_TARGET_FP_CMAQ}' exists, skipping"
  else

    ### retemporalize

    for SCRIPT in \
      "${EDGAR_NONAG_RETEMP_HELPER_FP}" \
      "${EDGAR_NONAG_CONSERV_HELPER_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

    ### cleanup

    if [[ ! -r "${CMAQ_TARGET_FP_CMAQ}" ]] ; then
      if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
        ## copy to the extension CMAQ wants
        for CMD in \
          "mv ${CMAQ_TARGET_FP_NCL} ${CMAQ_TARGET_FP_CMAQ}" \
        ; do
          echo -e "$ ${CMD}"
          eval "${CMD}"
        done
      else
        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot read either CMAQ_TARGET_FP_NCL=='${CMAQ_TARGET_FP_NCL}' or CMAQ_TARGET_FP_CMAQ=='${CMAQ_TARGET_FP_CMAQ}'"
        exit 99
      fi
    fi

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

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

    if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
      for CMD in \
        "rm ${CMAQ_TARGET_FP_NCL}" \
      ; do
        echo -e "$ ${CMD}"
        eval "${CMD}"
      done
      if [[ -r "${CMAQ_TARGET_FP_NCL}" ]] ; then
        echo -e "${THIS_FN}::${FUNCNAME[0]}: ERROR: cannot delete CMAQ_TARGET_FP_NCL=='${CMAQ_TARGET_FP_NCL}'"
        exit 42
      fi
    fi
  fi # end if [[ -r "${CMAQ_TARGET_FP_CMAQ}" ]]
} # end function retemp_conserv

function teardown {
  # remove the *.nc required only for NCL
  # # start debugging-----------------------------------------------------
  # echo -e "${THIS_FN}: CMAQ_TARGET_FP_TEMPLATE_NCL= '${CMAQ_TARGET_FP_TEMPLATE_NCL}'"
  # echo -e "${THIS_FN}: CMAQ_TARGET_FP_NCL=          '${CMAQ_TARGET_FP_NCL}'"
  # #   end debugging-----------------------------------------------------

  ### show files: data and plots
  for CMD in \
    "ls -alht ${WORK_DIR}/*.pdf" \
    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT_CMAQ}" \
    "ls -alht ${WORK_DIR}/*.${NETCDF_EXT_NCL}" \
    "ncdump -h ${CMAQ_TARGET_FP_CMAQ}" \
  ; 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)
#   'setup' \
#   'reunit' \
#   'regrid' \
#   'retemp_conserv' \
#   'teardown' \
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 43
  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.