Translating a lookup-dictionary to bash: Much simpler than I thought

I wanted to name Transcom Regions 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 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!

Use Node:

⚙ Babcom is trying to load the comments ⚙

This textbox will disappear when the comments have been loaded.

If the box below shows an error-page, you need to install Freenet with the Sone-Plugin or set the node-path to your freenet node and click the Reload Comments button (or return).

If you see something like Invalid key: java.net.MalformedURLException: There is no @ in that URI! (Sone/search.html), you need to setup Sone and the Web of Trust

If you had Javascript enabled, you would see comments for this page instead of the Sone page of the sites author.

Note: To make a comment which isn’t a reply visible to others here, include a link to this site somewhere in the text of your comment. It will then show up here. To ensure that I get notified of your comment, also include my Sone-ID.

Link to this site and my Sone ID: sone://6~ZDYdvAgMoUfG6M5Kwi7SQqyS-gTcyFeaNN1Pf3FvY

This spam-resistant comment-field is made with babcom.

Inhalt abgleichen
Willkommen im Weltenwald!
((λ()'Dr.ArneBab))



Beliebte Inhalte

Draketo neu: Beiträge

Ein Würfel System

sn.1w6.org news