|
1 | 1 | ### This module contains routines to access and retrieve ECCO datasets on the AWS Cloud. |
2 | 2 | ### These functions will only work when called from an AWS EC2 instance running in region us-west-2. |
3 | 3 |
|
4 | | - |
5 | 4 | from .ecco_acc_dates import date_adjustment |
6 | 5 |
|
7 | 6 | ## Initalize Python libraries for module |
|
12 | 11 | import os.path |
13 | 12 | from os.path import basename, isfile, isdir, join, expanduser |
14 | 13 | from pathlib import Path |
| 14 | +from platform import system |
| 15 | +from netrc import netrc |
| 16 | +from urllib import request |
| 17 | +from http.cookiejar import CookieJar |
| 18 | +from getpass import getpass |
| 19 | +import requests |
| 20 | + |
| 21 | + |
| 22 | +def setup_earthdata_login_auth(url: str='urs.earthdata.nasa.gov'): |
| 23 | + """Helper subroutine to log into NASA EarthData""" |
| 24 | + |
| 25 | + # Predict the path of the netrc file depending on os/platform type. |
| 26 | + _netrc = join(expanduser('~'), "_netrc" if system()=="Windows" else ".netrc") |
| 27 | + |
| 28 | + # look for the netrc file and use the login/password |
| 29 | + try: |
| 30 | + username, _, password = netrc(file=_netrc).authenticators(url) |
| 31 | + |
| 32 | + # if the file is not found, prompt the user for the login/password |
| 33 | + except (FileNotFoundError, TypeError): |
| 34 | + print('Please provide Earthdata Login credentials for access.') |
| 35 | + username, password = input('Username: '), getpass('Password: ') |
| 36 | + |
| 37 | + # write credentials to netrc file |
| 38 | + with open(_netrc,'a') as file: |
| 39 | + lines = ["machine urs.earthdata.nasa.gov\n",\ |
| 40 | + " login "+username+"\n",\ |
| 41 | + " password "+password] |
| 42 | + file.writelines(lines) |
| 43 | + file.close() |
| 44 | + |
| 45 | + manager = request.HTTPPasswordMgrWithDefaultRealm() |
| 46 | + manager.add_password(None, url, username, password) |
| 47 | + auth = request.HTTPBasicAuthHandler(manager) |
| 48 | + jar = CookieJar() |
| 49 | + processor = request.HTTPCookieProcessor(jar) |
| 50 | + opener = request.build_opener(auth, processor) |
| 51 | + request.install_opener(opener) |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +###================================================================================================================ |
15 | 56 |
|
16 | 57 |
|
17 | 58 | def ecco_podaac_s3_query(ShortName,StartDate,EndDate): |
@@ -42,41 +83,9 @@ def ecco_podaac_s3_query(ShortName,StartDate,EndDate): |
42 | 83 |
|
43 | 84 | pass |
44 | 85 |
|
45 | | - ## Initalize Python libraries |
46 | | - from platform import system |
47 | | - from netrc import netrc |
48 | | - from os.path import basename, isfile, isdir, join, expanduser |
49 | | - from urllib import request |
50 | | - from http.cookiejar import CookieJar |
51 | | - |
52 | | - |
53 | | - # Predict the path of the netrc file depending on os/platform type. |
54 | | - _netrc = join(expanduser('~'), "_netrc" if system()=="Windows" else ".netrc") |
55 | | - |
56 | 86 |
|
57 | 87 | ## Define Helper Subroutines |
58 | 88 |
|
59 | | - ### Helper subroutine to log into NASA EarthData |
60 | | - |
61 | | - # not pretty but it works |
62 | | - def setup_earthdata_login_auth(url: str='urs.earthdata.nasa.gov'): |
63 | | - # look for the netrc file and use the login/password |
64 | | - try: |
65 | | - username, _, password = netrc(file=_netrc).authenticators(url) |
66 | | - |
67 | | - # if the file is not found, prompt the user for the login/password |
68 | | - except (FileNotFoundError, TypeError): |
69 | | - print('Please provide Earthdata Login credentials for access.') |
70 | | - username, password = input('Username: '), getpass('Password: ') |
71 | | - |
72 | | - manager = request.HTTPPasswordMgrWithDefaultRealm() |
73 | | - manager.add_password(None, url, username, password) |
74 | | - auth = request.HTTPBasicAuthHandler(manager) |
75 | | - jar = CookieJar() |
76 | | - processor = request.HTTPCookieProcessor(jar) |
77 | | - opener = request.build_opener(auth, processor) |
78 | | - request.install_opener(opener) |
79 | | - |
80 | 89 | ### Helper subroutines to make the API calls to search CMR and parse response |
81 | 90 | def set_params(params: dict): |
82 | 91 | params.update({'scroll': "true", 'page_size': 2000}) |
@@ -127,8 +136,6 @@ def get_granules(params: dict, ShortName: str, SingleDay_flag: bool): |
127 | 136 | StartDate,EndDate,CMR_query=True) |
128 | 137 |
|
129 | 138 | ## Log into Earthdata using your username and password |
130 | | - |
131 | | - # actually log in with this command: |
132 | 139 | setup_earthdata_login_auth() |
133 | 140 |
|
134 | 141 | # Query the NASA Common Metadata Repository to find the URL of every granule associated with the desired |
@@ -167,7 +174,6 @@ def init_S3FileSystem(): |
167 | 174 | |
168 | 175 | """ |
169 | 176 |
|
170 | | - import requests |
171 | 177 | import s3fs |
172 | 178 |
|
173 | 179 | creds = requests.get('https://archive.podaac.earthdata.nasa.gov/s3credentials').json() |
@@ -372,30 +378,46 @@ def ecco_podaac_s3_open_fsspec(ShortName,jsons_root_dir): |
372 | 378 |
|
373 | 379 | pass |
374 | 380 |
|
| 381 | + import glob |
375 | 382 | import fsspec |
376 | 383 |
|
377 | 384 |
|
378 | 385 | # identify where json file is found |
379 | 386 | shortname_split = ShortName.split('_') |
380 | | - gridtype = shortname_split[-3] |
381 | 387 | if 'GEOMETRY' in ShortName: |
| 388 | + gridtype = shortname_split[-2] |
382 | 389 | time_res = 'GEOMETRY' |
383 | 390 | elif 'MIX_COEFFS' in ShortName: |
| 391 | + gridtype = shortname_split[-2] |
384 | 392 | time_res = 'MIXING_COEFFS' |
385 | 393 | else: |
| 394 | + gridtype = shortname_split[-3] |
386 | 395 | time_res = shortname_split[-2] |
387 | 396 | json_subdir = join(jsons_root_dir,"_".join(['MZZ',gridtype,time_res])) |
388 | | - json_file = join(json_subdir,ShortName+'.json') |
| 397 | + if (('GEOMETRY' in ShortName) or ('MIX_COEFFS' in ShortName)): |
| 398 | + if 'LLC' in gridtype: |
| 399 | + json_file = glob.glob(join(json_subdir,'*native*.json'))[0] |
| 400 | + elif 'DEG' in gridtype: |
| 401 | + json_file = glob.glob(join(json_subdir,'*latlon*.json'))[0] |
| 402 | + else: |
| 403 | + json_file = join(json_subdir,ShortName+'.json') |
| 404 | + |
| 405 | + |
| 406 | + # get NASA Earthdata credentials for S3 |
| 407 | + creds = requests.get('https://archive.podaac.earthdata.nasa.gov/s3credentials').json() |
389 | 408 |
|
390 | 409 | # generate map object |
391 | 410 | fs = fsspec.filesystem(\ |
392 | 411 | "reference", |
393 | 412 | fo=json_file,\ |
394 | 413 | remote_protocol="s3", |
395 | | - remote_options={"anon":True}, |
| 414 | + remote_options={"anon":False,\ |
| 415 | + "key":creds['accessKeyId'], |
| 416 | + "secret":creds['secretAccessKey'], |
| 417 | + "token":creds['sessionToken']},\ |
396 | 418 | skip_instance_cache=True) |
397 | 419 | fsmap_obj = fs.get_mapper("") |
398 | | - |
| 420 | + |
399 | 421 | return fsmap_obj |
400 | 422 |
|
401 | 423 |
|
|
0 commit comments