I wanted to name Transcom Regions [1] in my plots by passing their names to the command-line tool, but I only had their region-number and a lookup dictionary in Python. To avoid tampering with the tool, I needed to translate the dictionary to a bash function, and thanks to the case statement [2] it was much simpler than I had expected.
This is the original dictionary:
#: Names of transcom regions transcomregionnames = { 1: "NAM Boreal", 2: "NAM Temperate", 3: "South American tropical", # and so forth }
This is how lookup works in Python:
region = 2 name = transcomregionnames[2]
The solution in bash is a simple mechanic translation:
function regionname () { number="$1" case $number in 1) echo "NAM Boreal";; 2) echo "NAM Temperate";; 3) echo "South American tropical";; # and so forth esac }
And the lookup is easier than anything I hoped for:
region=2 name=$(regionname $region)
This is how it looks in my actual code:
for region in {1..22} ; do ./plotstation.py -c /home/arne/sun-work/ct-tccon/ct-tccon-2015-5x7-use-obspack-no-tccon-nc/ -C "GA: in-situ ground and aircraft" -c /home/arne/sun-work/ct-tccon/ct-tccon-2015-5x7-use-obspack-use-tccon-noassimeu/ -C "TneGA: non-European TCCON and GA" -c /home/arne/sun-work/ct-tccon/ct-tccon-2015-5x7-use-obspack-no-tccon-no-aircraft-doesitbreaktoo/ -C "G: in-situ ground" --regionfluxtimeseries $region --toaverage 5 --exclude-validation --colorscheme paulforabp --linewidth 4 --font-size 36 --start 2009-12-03 --stop 2012-12-02 --title "Effect of assimilating non-EU TCCON, $(regionname ${region})" -o ~/flux-GA-vs-TneGA-vs-G-region-${region}.pdf; done
For your convenience, here’s my entire transcom naming function:
function regionname () { number="$1" case $number in 1) echo "NAM Boreal" ;; 2) echo "NAM Temperate";; 3) echo "South American tropical";; 4) echo "South American temperate";; 5) echo "Northern Africa";; 6) echo "Southern Africa";; 7) echo "Eurasian Boreal";; 8) echo "Eurasian Temperate";; 9) echo "Tropical Asia";; 10) echo "Australia";; 11) echo "Europe";; 12) echo "North Pacific Temperate";; 13) echo "West Pacific Tropics";; 14) echo "East Pacific Tropics";; 15) echo "South Pacific Temperate";; 16) echo "Northern Ocean";; 17) echo "North Atlantic Temperate";; 18) echo "Atlantic Tropics";; 19) echo "South Atlantic Temperate";; 20) echo "Southern Ocean";; 21) echo "Indian Tropical";; 22) echo "South Indian Temperate";; esac }
Happy Hacking!
Links:
[1] http://transcom.project.asu.edu/transcom03_protocol_basisMap.php
[2] http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html