Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwsave #34

Merged
merged 7 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ ENV/

# mypy
.mypy_cache/

# cred files
wsmtk.cred.pkl
wsmtk.key.pkl
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],
install_requires=['numpy','gdal>=2','h5py','beautifulsoup4','requests','progress','pandas'],
install_requires=['numpy>=1.15.1','gdal>=2','h5py','beautifulsoup4','requests','progress','pandas', "cryptography"],
python_requires='>=2.7.11, <4',
)
Binary file modified wsmtk/data/ModlandTiles_bbx.pkl
Binary file not shown.
29 changes: 24 additions & 5 deletions wsmtk/downloadMODIS.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pickle
import re
import ogr
from wsmtk.utils import Credentials, pload


try:
range = xrange
Expand All @@ -34,6 +36,7 @@ def main():
parser.add_argument("--username", help='Earthdata username (required for download)',metavar='')
parser.add_argument("--password", help='Earthdata password (required for download)',metavar='')
parser.add_argument("-d","--targetdir", help='Destination directory',default=os.getcwd(),metavar='')
parser.add_argument("--store-credentials", help='Store Earthdata credentials on disk to be used for future downloads (unsecure!)',action='store_true')
#parser.add_argument("-v","--verbose", help='Verbosity',action='store_true')
parser.add_argument("--download", help='Download data',action='store_true')
parser.add_argument("--aria2", help='Use ARIA2 for downloading',action='store_true')
Expand All @@ -45,16 +48,32 @@ def main():

args = parser.parse_args()

credentials = Credentials(args.username, args.password)

# Check for credentials if download is True
if args.download & (not args.username or not args.password):
raise SystemExit('Downloading requires username and password!')
if args.download & (not credentials.complete):

try:

credentials.retrieve()

except:

raise SystemExit('\nError: Earthdata credentials not found!\n')

elif args.store_credentials:

credentials.store()

else:

pass

args.product = [x.upper() for x in args.product]

# Load product table
this_dir, this_filename = os.path.split(__file__)
with open(os.path.join(this_dir, "data", "MODIS_V6_PT.pkl"),'rb') as table_raw:
product_table = pickle.load(table_raw)
product_table = pload(os.path.join(this_dir, "data", "MODIS_V6_PT.pkl"))

for p in args.product:

Expand Down Expand Up @@ -193,7 +212,7 @@ def main():

# If download is True and at least one result, download data
if args.download and res.results > 0:
res.setCredentials(args.username,args.password)
res.setCredentials(credentials.username,credentials.password)
res.download()


Expand Down
116 changes: 116 additions & 0 deletions wsmtk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import multiprocessing.pool
import array
from wsmtk.whittaker import ws2d, ws2d_vc, ws2d_vc_asy
import pickle
import os
from cryptography.fernet import Fernet

# assign xrange to range if py2
try:
Expand Down Expand Up @@ -180,6 +183,119 @@ def getDV(self,nd):
def getDIX(self):
return([self.daily.index(x) for x in self.target])


class Credentials:
'''Credentials helper class'''

def __init__(self, username = None, password = None):
'''Create Credentials instance

Args:
username (str): Earthdata username
password (str): Earthdata passsword
'''


self.username = username
self.password = password

self.complete = not (not self.username or not self.password)

def retrieve(self):
'''Retrieve credentials from disk'''

try:
u, p = pload('wsmtk.cred.pkl')

k = pload('wsmtk.key.pkl')

cipher_suite = Fernet(k)

self.username = cipher_suite.decrypt(u).decode()
self.password = cipher_suite.decrypt(p).decode()

except:

self.destroy()
raise

def store(self):
'''Store credentials on disk'''

try:

k = Fernet.generate_key()

cipher_suite = Fernet(k)

u = cipher_suite.encrypt(self.username.encode())

p = cipher_suite.encrypt(self.password.encode())

pdump((u,p),'wsmtk.cred.pkl')

pdump(k,'wsmtk.key.pkl')

except:

self.destroy()

print('Storing Earthdata credentials failed!')



def destroy(self):
'''Remove all credential files on disk'''

try:
os.remove('wsmtk.cred.pkl')
except FileNotFoundError:
pass

try:
os.remove('wsmtk.key.pkl')
except FileNotFoundError:
pass



def pdump(obj,filename):
'''Pickle dump wrapper

Agrs:
obj: Python object to be pickled
filename: name of target pickle file

Returns:
None
'''

try:
with open(filename,'wb') as pkl:
pickle.dump(obj,pkl)

except FileNotFoundError:
raise

def pload(filename):
'''Pickle load wrapper

Agrs:
filename: name of target pickle file

Returns:
Pickled object
'''


try:
with open(filename,'rb') as pkl:
return(pickle.load(pkl))

except FileNotFoundError:
raise


def dtype_GDNP(dt):
'''GDAL/NP DataType helper.

Expand Down