Skip to content

Commit

Permalink
CLI scripts moved to modules with defined entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
PennyHow committed Oct 6, 2023
1 parent d9f0e8f commit 2ee4ce1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 50 deletions.
16 changes: 13 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pypromice",
version="1.3.0",
version="1.3.1",
author="GEUS Glaciology and Climate",
description="PROMICE/GC-Net data processing toolbox",
long_description=long_description,
Expand All @@ -19,7 +19,7 @@
keywords="promice gc-net aws climate glaciology greenland geus",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"Natural Language :: English",
Expand All @@ -31,5 +31,15 @@
packages=setuptools.find_packages(where="src"),
python_requires=">=3.8",
install_requires=['numpy>=1.23.0', 'pandas>=1.5.0', 'xarray>=2022.6.0', 'toml', 'scipy>=1.9.0', 'scikit-learn>=1.1.0', 'Bottleneck', 'netcdf4', 'pyDataverse'],
scripts=['bin/getData', 'bin/getL0tx', 'bin/getL3', 'bin/joinL3', 'bin/getWatsontx', 'bin/getBUFR', 'bin/getMsg'],
entry_points={
'console_scripts': [
'get_promice_data = pypromice.get.get_promice_data:get_promice_data',
'get_l0tx = pypromice.tx.get_l0tx:get_l0tx',
'get_l3 = pypromice.process.get_l3:get_l3',
'join_l3 = pypromice.process.join_l3:join_l3',
'get_watsontx = pypromice.get.get_watsontx:get_watsontx',
'get_bufr = pypromice.postprocess.get_bufr:get_bufr',
'get_msg = pypromice.tx.get_msg:get_msg'
],
},
)
1 change: 1 addition & 0 deletions src/pypromice/get/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pypromice.get.get import *
File renamed without changes.
32 changes: 21 additions & 11 deletions bin/getData → src/pypromice/get/get_promice_data.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#!/usr/bin/env python
from argparse import ArgumentParser
import os
from pypromice.get import aws_names, aws_data
import os, unittest
from pypromice.get.get import aws_data


def parse_arguments():
def parse_arguments_data():
parser = ArgumentParser(description="PROMICE and GC-Net dataset fetcher")
parser.add_argument('-n', '--awsname', default=None, type=str, required=True,
help='AWS name')
parser.add_argument('-f', '--format', default='csv', type=str, required=False,
help='File format to save data as')
parser.add_argument('-o', '--outpath', default=os.getcwd(), type=str, required=False,
help='Directory where file will be written to')
help='Directory where file will be written to')
args = parser.parse_args()
return args


if __name__ == '__main__':
args = parse_arguments()
def get_promice_data():
'''Command line driver for fetching PROMICE and GC-Net datasets'''

args = parse_arguments_data()

# Construct AWS dataset name
# n = aws_names()
Expand All @@ -41,13 +43,21 @@ def parse_arguments():
# Save to file
if f in 'csv':
outfile = os.path.join(args.outpath, args.awsname.lower())
data.to_csv(outfile)
if outfile is not None:
data.to_csv(outfile)
elif f in 'nc':
data.to_netcdf(outfile, mode='w', format='NETCDF4', compute=True)
outfile = os.path.join(args.outpath, args.awsname.lower().split('.csv')[0]+'.nc')
if outfile is not None:
outfile = os.path.join(args.outpath, args.awsname.lower().split('.csv')[0]+'.nc')

print(f'File saved to {outfile}')

else:
"""Executed on import"""
pass

#class get_promice_data_test(unittest.TestCase):
# def get_test(self):
# '''Test get_promice_data'''
# d = os.system('get_promice_data -n KPC_U -f csv -o None')

#if __name__ == "__main__":
# unittest.main()

17 changes: 11 additions & 6 deletions bin/getBUFR → src/pypromice/postprocess/get_bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# from IPython import embed


def parse_arguments():
def parse_arguments_bufr():
parser = argparse.ArgumentParser()

parser.add_argument('--dev',
Expand Down Expand Up @@ -64,10 +64,8 @@ def parse_arguments():
args = parser.parse_args()
return args

if __name__ == '__main__':
"""Executed from the command line"""

args = parse_arguments()
def get_bufr():
args = parse_arguments_bufr()

# Get list of relative file paths
fpaths = glob.glob(args.l3_filepath)
Expand Down Expand Up @@ -287,4 +285,11 @@ def parse_arguments():
print('no_entry_latest_timestamps: {}'.format(no_entry_latest_timestamps))
print('failed_min_data_wx: {}'.format(failed_min_data_wx))
print('failed_min_data_pos: {}'.format(failed_min_data_pos))
print('--------------------------------')
print('--------------------------------')

if __name__ == '__main__':
"""Executed from the command line"""
get_bufr()
else:
"""Executed on import"""
pass
21 changes: 12 additions & 9 deletions bin/getL3 → src/pypromice/process/get_l3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import os
import sys
from argparse import ArgumentParser
from pypromice.process import AWS
from pypromice.process.aws import AWS

def parse_arguments():
def parse_arguments_l3():
parser = ArgumentParser(description="AWS L3 processor")

parser.add_argument('-c', '--config_file', type=str, required=True,
Expand All @@ -21,10 +21,8 @@ def parse_arguments():
args = parser.parse_args()
return args


if __name__ == '__main__':
"""Executed from the command line"""
args = parse_arguments()
def get_l3():
args = parse_arguments_l3()

logging.basicConfig(
format="%(asctime)s; %(levelname)s; %(name)s; %(message)s",
Expand All @@ -40,9 +38,14 @@ def parse_arguments():
else:
aws = AWS(args.config_file, args.inpath, args.variables, args.metadata)

aws.process()
aws.write(args.outpath)

aws.process()

if args.outpath is not None:
aws.write(args.outpath)

if __name__ == '__main__':
"""Executed from the command line"""
get_l3()
else:
"""Executed on import"""
pass
Expand Down
12 changes: 8 additions & 4 deletions bin/joinL3 → src/pypromice/process/join_l3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
roundValues, resampleL3, writeAll
from pypromice.process.L1toL2 import correctPrecip

def parse_arguments():
def parse_arguments_join():
parser = ArgumentParser(description="AWS L3 joiner for merging together two L3 products, for example an L3 RAW and L3 TX data product. An hourly, daily and monthly L3 data product is outputted to the defined output path")
parser.add_argument('-s', '--file1', type=str, required=True,
help='Path to source L3 file, which will be preferenced in merge process')
Expand Down Expand Up @@ -41,9 +41,8 @@ def loadArr(infile):
return ds, name


if __name__ == '__main__':
"""Executed from the command line"""
args = parse_arguments()
def join_l3():
args = parse_arguments_join()

# Check files
if os.path.isfile(args.file1) and os.path.isfile(args.file2):
Expand Down Expand Up @@ -127,6 +126,11 @@ def loadArr(infile):
writeAll(out, name, l3_h, l3_d, l3_m, col_names)
print(f'Files saved to {os.path.join(out, name)}...')



if __name__ == '__main__':
"""Executed from the command line"""
join_l3()
else:
"""Executed on import"""
pass
13 changes: 7 additions & 6 deletions bin/getL0tx → src/pypromice/tx/get_l0tx.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pypromice.tx import getMail, L0tx, sortLines


def parse_arguments():
def parse_arguments_l0tx():
parser = ArgumentParser(description="AWS L0 transmission fetcher")
parser.add_argument('-a', '--account', default=None, type=str, required=True, help='Email account .ini file')
parser.add_argument('-p', '--password', default=None, type=str, required=True, help='Email credentials .ini file')
Expand All @@ -22,10 +22,8 @@ def parse_arguments():
args = parser.parse_args()
return args


if __name__ == '__main__':
"""Executed from the command line"""
args = parse_arguments()
def get_l0tx():
args = parse_arguments_l0tx()
toml_path = os.path.join(args.config, args.name+'.toml')
toml_list = glob(toml_path)

Expand Down Expand Up @@ -173,7 +171,10 @@ def parse_arguments():
print(f'Could not write last uid {uid} to {uid_file}')

print('Finished')


if __name__ == '__main__':
"""Executed from the command line"""
get_l0tx()
else:
"""Executed on import"""
pass
13 changes: 7 additions & 6 deletions bin/getMsg → src/pypromice/tx/get_msg.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pypromice.tx import getMail


def parse_arguments():
def parse_arguments_msg():
parser = ArgumentParser(description="AWS message downloader")
parser.add_argument('-a', '--account', default=None, type=str,
required=True, help='Email account .ini file')
Expand All @@ -21,10 +21,8 @@ def parse_arguments():
args = parser.parse_args()
return args


if __name__ == '__main__':
"""Executed from the command line"""
args = parse_arguments()
def get_msg():
args = parse_arguments_msg()

# Set credential paths
accounts_file = args.account
Expand Down Expand Up @@ -100,7 +98,10 @@ def parse_arguments():


print('Finished')


if __name__ == '__main__':
"""Executed from the command line"""
get_msg()
else:
"""Executed on import"""
pass
12 changes: 7 additions & 5 deletions bin/getWatsontx → src/pypromice/tx/get_watsontx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pypromice.tx import getMail, L0tx, sortLines


def parse_arguments():
def parse_arguments_watson():
parser = ArgumentParser(description="AWS L0 transmission fetcher")
parser.add_argument('-a', '--account', default=None, type=str, required=True, help='Email account .ini file')
parser.add_argument('-p', '--password', default=None, type=str, required=True, help='Email credentials .ini file')
Expand All @@ -30,9 +30,9 @@ def parse_arguments():
return args

#------------------------------------------------------------------------------
if __name__ == '__main__':
def get_watsontx():
"""Executed from the command line"""
args = parse_arguments()
args = parse_arguments_watson()

# Set payload formatter paths
formatter_file = args.formats
Expand Down Expand Up @@ -139,7 +139,9 @@ def parse_arguments():
print(f'Could not write last uid {uid} to {uid_file}')

print('Finished')


if __name__ == '__main__':
get_watsontx()
else:
"""Executed on import"""
pass
pass

0 comments on commit 2ee4ce1

Please sign in to comment.