10 from urllib.parse
import urlparse
11 from pathlib
import Path, PurePath
13 def retrieveURL(request,localpath='.', uncompress=False, verbose=0,force_download=False, appkey=False, checksum=False):
15 print(
"Retrieving %s" % request.rstrip())
17 server =
"oceandata.sci.gsfc.nasa.gov"
18 parsedRequest = urlparse(request)
19 netpath = parsedRequest.path
21 if parsedRequest.netloc:
22 server = parsedRequest.netloc
24 if not re.match(
".*getfile",netpath):
25 netpath =
'/ob/getfile/' + netpath
28 if (re.match(
".*getfile",netpath))
and appkey:
29 netpath = netpath + joiner +
'appkey=' + appkey
32 if parsedRequest.query:
33 netpath = netpath + joiner + parsedRequest.query
35 status =
httpdl(server, netpath, localpath=localpath, uncompress=uncompress, verbose=verbose,force_download=force_download)
37 if checksum
and not uncompress:
38 cksumURL =
'https://'+server +
'/checkdata/' + parsedRequest.path
39 dnldfile = localpath / parsedRequest.path
41 print(
"The file %s failed checksum test" % parsedRequest.path)
47 if __name__ ==
"__main__":
49 parser = argparse.ArgumentParser(
50 formatter_class=argparse.RawTextHelpFormatter,
51 description=
'Download files archived at the OB.DAAC',
52 epilog=textwrap.dedent(
'''
53 Provide one of either filename, --filelist or --http_manifest.
55 NOTE: For authentication, a valid .netrc file in the user home ($HOME) directory\nor a valid appkey is required.
58 machine urs.earthdata.nasa.gov login USERNAME password PASSWD\n
60 An appkey can be obtained from:
61 https://oceandata.sci.gsfc.nasa.gov/appkey/
64 parser.add_argument(
'-v',
'--verbose', help=
'print status messages',
65 action=
'count',default=0)
66 parser.add_argument(
'filename', nargs=
'?', help=
'name of the file (or the URL of the file) to retreive')
67 parser.add_argument(
'--filelist',
68 help=
'file containing list of filenames to retreive, one per line')
69 parser.add_argument(
'--http_manifest',
70 help=
'URL to http_manifest file for OB.DAAC data order')
71 parser.add_argument(
'--odir',
72 help=
'full path to desired output directory; \ndefaults to current working directory: %s' % Path.cwd(),
74 parser.add_argument(
'--uncompress',action=
"store_true",
75 help=
'uncompress the retrieved files (if compressed)',
77 parser.add_argument(
'--checksum',action=
"store_true",
78 help=
'compare retrieved file checksum; cannot be used with --uncompress',
80 parser.add_argument(
'--failed',help=
'filename to contain list of files that failed to be retrieved')
81 parser.add_argument(
'--appkey',help=
'value of the users application key')
82 parser.add_argument(
'--force',action=
'store_true',
83 help=
'force download even if file already exists locally',
85 args = parser.parse_args()
89 if args.http_manifest:
90 status =
retrieveURL(args.http_manifest,verbose=args.verbose,force_download=
True,appkey=args.appkey)
92 print(
"There was a problem retrieving %s (received status %d)" % (args.http_manifest,status))
93 sys.exit(
"Bailing out...")
95 with open(
'http_manifest.txt')
as flist:
96 for filename
in flist:
97 filelist.append(filename.rstrip())
99 filelist.append(args.filename)
101 with open(os.path.expandvars(args.filelist))
as flist:
102 for filename
in flist:
103 filelist.append(os.path.expandvars(filename.rstrip()))
105 if not len(filelist):
107 sys.exit(
"Please provide a filename (or list file) to retrieve")
109 if args.uncompress
and args.checksum:
111 sys.exit(
"--uncompress is incompatible with --checksum")
113 outpath = Path.resolve(Path.expanduser(Path(os.path.expandvars(args.odir))))
116 print(
"Output directory: %s" % outpath)
120 failed = open(args.failed,
'w')
122 for request
in filelist:
123 status =
retrieveURL(request,localpath=outpath, uncompress=args.uncompress,
124 verbose=args.verbose,force_download=args.force,
125 appkey=args.appkey,checksum=args.checksum)
129 print(
"%s is not newer than local copy, skipping download" % request)
131 print(
"There was a problem retrieving %s (received status %d)" % (request,status))
133 failed.write(request)