-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#! /usr/bin/env python3 | ||
|
||
import argparse | ||
import subprocess | ||
import sys | ||
import re | ||
import os | ||
import shutil | ||
|
||
description="Download ATL09 data from NSIDC." +\ | ||
" To use this, you must generate a token, using the setup_token script, which will be saved in a file called NSIDC_token.txt"+\ | ||
"The script will search for NSIDC_token.txt in the current directory (first) and in the directory where the script is located (second)." | ||
|
||
|
||
parser=argparse.ArgumentParser(description=description) | ||
parser.add_argument('-f', dest='full_file', default=False, action='store_true') | ||
parser.add_argument('-o', dest='output_directory', type=str) | ||
parser.add_argument('-d', dest='dry_run', default=False, action='store_true') | ||
parser.add_argument('-t', dest='time_str', required=True, default=None, help="Time range for query. Format is YYYY-MM-DDTHH:MM:SS,YYYY-MM-DDTHH:MM:SS") | ||
args=parser.parse_args() | ||
|
||
# look for a NSIDC_identity.txt file in the current directory | ||
try: | ||
fh=open('NSIDC_token.txt','r') | ||
except(FileNotFoundError): | ||
# also look in the script's directory | ||
fh=open(sys.path[0]+'/NSIDC_token.txt','r') | ||
|
||
token=None | ||
token_re=re.compile('<id>(.*)</id>') | ||
for line in fh: | ||
m=token_re.search(line) | ||
if m is not None: | ||
token=m.group(1) | ||
if token is None: | ||
raise RuntimeError('missing token string') | ||
|
||
token_str='&token=%s' % token | ||
|
||
if 'out_dir' in args: | ||
os.chdir(args.output_directory) | ||
|
||
if args.full_file: | ||
subset_str="&agent=NO" | ||
else: | ||
subset_str="" | ||
|
||
time_str="&time=%s" % args.time_str | ||
|
||
# build the query to submit via curl | ||
cmd_base='curl -O -J --dump-header response-header.txt "https://n5eil02u.ecs.nsidc.org/egi/request?short_name=ATL09&version=200&page_size=1000' | ||
cmd=cmd_base+'%s%s%s"' % (token_str, subset_str, time_str) | ||
|
||
print(cmd) | ||
|
||
if args.dry_run: | ||
# if this is a dry run, exit after reporting the string | ||
exit() | ||
|
||
# run the curl string | ||
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
text=p.stdout.read() | ||
retcode=p.wait() | ||
|
||
# check the response header to get the filename with the output | ||
zip_re=re.compile('filename="(.*.zip)"') | ||
with open('response-header.txt') as ff: | ||
for line in ff: | ||
m=zip_re.search(line) | ||
if m is not None: | ||
zip_file=m.group(1) | ||
zip_str=subprocess.check_output(['unzip',zip_file]) | ||
#print(zip_str) | ||
|
||
# pull the indivicual h5 files into the current directory | ||
cleanup_list=list() | ||
for h5_file in re.compile('(\S+\.h5)').findall(zip_str.decode('utf-8')): | ||
shutil.move(h5_file,'.') | ||
thedir=os.path.dirname(h5_file) | ||
if thedir not in cleanup_list: | ||
cleanup_list.append( thedir) | ||
|
||
# delete the directories that contained the hdf5 files | ||
for entry in cleanup_list: | ||
os.rmdir(entry) | ||
os.remove(zip_file) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#! /usr/bin/env bash | ||
|
||
if [ $# -eq 0]; then | ||
echo "USAGE: setup_token username password ip_address" | ||
fi | ||
|
||
username=$1 | ||
password=$2 | ||
ip_address=$3 | ||
|
||
curl -X POST --header "Content-Type: application/xml" -d "<token><username>$username</username><password>$password</password><client_id>NSIDC_client_id</client_id><user_ip_address>$ip_address</user_ip_address></token>" https://api.echo.nasa.gov/echo-rest/tokens > NSIDC_token | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
<<<<<<< HEAD | ||
Another test | ||
======= | ||
TEST | ||
>>>>>>> d3ba45b... testing fork workflow |