Skip to content

Get decoded METAR data: Python3: subprocess.run(), fire.Fire(), click

License

Notifications You must be signed in to change notification settings

nick3499/get_metar_nick3499

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get_metar_nick3499

Get decoded METAR data: Python3: subprocess.run(), click

screen capture

Tested Using:

  • Linux 5.4.0-14-generic
  • Ubuntu 20.04 LTS (Focal Fossa)
  • Python 3.8.2
  • GNOME Shell 3.35.91

Methods Used

  • subprocess.run()
  • fire.Fire()
  • click methods

Run get_metar_click.py

To run get_metar_click.py in Bash (Unix shell), save the following into a shell script and execute it:

/bin/python3 $HOME/scripts/get_metar/get_metar_click.py --station $1

Or run the raw script from Github in Bash to get the help doc:

$ curl -s https://raw.githubusercontent.com/nick3499/get_metar_nick3499/master/get_metar_click.py | python3

Run get_metar_fire.py

To run get_metar_fire.py in Bash (Unix shell), save the following into a shell script and execute it:

/bin/python3 $HOME/scripts/get_metar/get_metar_fire.py $1

Or run the raw script from Github in Bash to get the help doc:

$ curl -s https://raw.githubusercontent.com/nick3499/get_metar_nick3499/master/get_metar_fire.py | python3

Shebang Line

#! /bin/python3

The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being 0x23 0x21, the two-character encoding in ASCII of #!. This magic number is detected by the "exec" family of functions, which determine whether a file is a script or an executable binary. The presence of the shebang will result in the execution of the specified executable, usually an interpreter for the script's language.

Shebang_(Unix): Magic number: Wikipedia

Imports (click version)

from subprocess import run
import click
  • subprocess.run() is used to execute command line args from a Python script in the Bash (Unix shell).
  • click is used to generate command line interfaces.

Imports (fire version)

from subprocess import run
from fire import Fire
  • subprocess.run() is used to execute command line args from a Python script in the Bash (Unix shell).
  • fire.Fire() is used to generate command line interfaces.

_get_data

def _get_data(icao_code):
    return run(['curl',
                'ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/' +
                icao_code], check=True)

_get_data uses the subprocess.run() method to run command line args in the Bash (Unix shell). curl is used to get decoded METAR data from a noaa.gov FTP link. icao_code can be one of four ICAO codes depending on which specific airport is selected.

If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they were captured.

subprocess — Subprocess management: Using the subprocess Module

Click Group

@click.group()
@click.option('--station/--no-station', default=False)
def get_decoded_metar(station):

The grouped commands are then bound to get_decoded_metar() using the click.command() method.

Get METAR (click version)

@get_decoded_metar.command()
def mor():
    '''Get decoded METAR data for Morris Municipal Airport.'''
    _get_data('KC09.TXT')

@get_decoded_metar.command()
def dbq():
    '''Get decoded METAR data for Dubuque Regional Airport.'''
    _get_data('KDBQ.TXT')

@get_decoded_metar.command()
def dvn():
    '''Get decoded METAR data for Davenport Municipal Airport.'''
    _get_data('KDVN.TXT')

@get_decoded_metar.command()
def pia():
    '''Get decoded METAR data for Peoria International Airport.'''
    _get_data('KPIA.TXT')

Get METAR (fire version)

def mor():
    '''Get decoded METAR for Morris Municipal Airport.'''
    _get_data('KC09.TXT')

def dbq():
    '''Get decoded METAR for Dubuque Regional Airport.'''
    _get_data('KDBQ.TXT')

def dvn():
    '''Get decoded METAR for Davenport Municipal Airport.'''
    _get_data('KDVN.TXT')

def pia():
    '''Get decoded METAR for Peoria International Airport.'''
    _get_data('KPIA.TXT')

if name == 'main' (click version)

if __name__ == '__main__':
    get_decoded_metar()

If executed as a standalone app, get_decoded_metar() will run automatically. Otherwise, if imported into another module, its name will no longer be main, and dot syntax will be required to start it.

if name == 'main' (fire version)

if __name__ == '__main__':
    Fire()

Other Flags

  • python3 get_metar_fire.py pia -- --help to get help doc for specific command
  • python3 get_metar_fire.py pia -- --trace to trace operations of specific command

ko-fi

Releases

No releases published

Packages

No packages published

Languages