gpt4 book ai didi

python - 我想使用 LADS DAAC 站点中提供的 Python 脚本从 HTTP 站点下载 MODIS 数据。但是在运行脚本时出现错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:13 27 4
gpt4 key购买 nike

我想使用 LADS DAAC 站点中提供的 Python 脚本从 HTTP 站点下载 MODIS 数据。但是在运行脚本时出现错误:

error: argument -s/--source is required

虽然我给出了源码路径,以下是原脚本;

#!/usr/bin/env python

# script supports either python2 or python3
#
# Attempts to do HTTP Gets with urllib2(py2) urllib.requets(py3) or subprocess
# if tlsv1.1+ isn't supported by the python ssl module
#
# Will download csv or json depending on which python module is available
#

from __future__ import (division, print_function, absolute_import, unicode_literals)

import argparse
import os
import os.path
import shutil
import sys

try:
from StringIO import StringIO # python2
except ImportError:
from io import StringIO # python3


################################################################################


USERAGENT = 'tis/download.py_1.0--' + sys.version.replace('\n','').replace('\r','')


def geturl(url, token=None, out=None):
headers = { 'user-agent' : USERAGENT }
if not token is None:
headers['Authorization'] = 'Bearer ' + token
try:
import ssl
CTX = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
if sys.version_info.major == 2:
import urllib2
try:
fh = urllib2.urlopen(urllib2.Request(url, headers=headers), context=CTX)
if out is None:
return fh.read()
else:
shutil.copyfileobj(fh, out)
except urllib2.HTTPError as e:
print('HTTP GET error code: %d' % e.code(), file=sys.stderr)
print('HTTP GET error message: %s' % e.message, file=sys.stderr)
except urllib2.URLError as e:
print('Failed to make request: %s' % e.reason, file=sys.stderr)
return None

else:
from urllib.request import urlopen, Request, URLError, HTTPError
try:
fh = urlopen(Request(url, headers=headers), context=CTX)
if out is None:
return fh.read().decode('utf-8')
else:
shutil.copyfileobj(fh, out)
except HTTPError as e:
print('HTTP GET error code: %d' % e.code(), file=sys.stderr)
print('HTTP GET error message: %s' % e.message, file=sys.stderr)
except URLError as e:
print('Failed to make request: %s' % e.reason, file=sys.stderr)
return None

except AttributeError:
# OS X Python 2 and 3 don't support tlsv1.1+ therefore... curl
import subprocess
try:
args = ['curl', '--fail', '-sS', '-L', '--get', url]
for (k,v) in headers.items():
args.extend(['-H', ': '.join([k, v])])
if out is None:
# python3's subprocess.check_output returns stdout as a byte string
result = subprocess.check_output(args)
return result.decode('utf-8') if isinstance(result, bytes) else result
else:
subprocess.call(args, stdout=out)
except subprocess.CalledProcessError as e:
print('curl GET error message: %' + (e.message if hasattr(e, 'message') else e.output), file=sys.stderr)
return None



################################################################################


DESC = "This script will recursively download all files if they don't exist from a LAADS URL and stores them to the specified path"


def sync(src, dest, tok):
'''synchronize src url with dest directory'''
try:
import csv
files = [ f for f in csv.DictReader(StringIO(geturl('%s.csv' % src, tok)), skipinitialspace=True) ]
except ImportError:
import json
files = json.loads(geturl(src + '.json', tok))

# use os.path since python 2/3 both support it while pathlib is 3.4+
for f in files:
# currently we use filesize of 0 to indicate directory
filesize = int(f['size'])
path = os.path.join(dest, f['name'])
url = src + '/' + f['name']
if filesize == 0:
try:
print('creating dir:', path)
os.mkdir(path)
sync(src + '/' + f['name'], path, tok)
except IOError as e:
print("mkdir `%s': %s" % (e.filename, e.strerror), file=sys.stderr)
sys.exit(-1)
else:
try:
if not os.path.exists(path):
print('downloading: ' , path)
with open(path, 'w+b') as fh:
geturl(url, tok, fh)
else:
print('skipping: ', path)
except IOError as e:
print("open `%s': %s" % (e.filename, e.strerror), file=sys.stderr)
sys.exit(-1)
return 0


def _main(argv):
parser = argparse.ArgumentParser(prog=argv[0], description=DESC)
parser.add_argument('-s', '--source', dest='source', metavar='URL', help='Recursively download files at URL', required=True)
parser.add_argument('-d', '--destination', dest='destination', metavar='DIR', help='Store directory structure in DIR', required=True)
parser.add_argument('-t', '--token', dest='token', metavar='TOK', help='Use app token TOK to authenticate', required=True)
args = parser.parse_args(argv[1:])
if not os.path.exists(args.destination):
os.makedirs(args.destination)
return sync(args.source, args.destination, args.token)


if __name__ == '__main__':
try:
sys.exit(_main(sys.argv))
except KeyboardInterrupt:
sys.exit(-1)

我在 _main 函数中给出了源、目标和标记,如下所示

parser.add_argument('-s', '--source', dest='archive/orders/501235044/', metavar='https://ladsweb.modaps.eosdis.nasa.gov/', help='Recursively download files at URL', required=True)
parser.add_argument('-d', '--destination', dest='MODIS_data', metavar='D:\', help='Store directory structure in DIR', required=True)
parser.add_argument('-t', '--token', dest='token', metavar='BA52319C-4DCA-11E8-8D99-C71EAE849760', help='Use app token TOK to authenticate', required=True)

我收到一个错误:argument -s/--source is required

最佳答案

我认为处理此问题的最佳方法是将脚本保存为 modis.py,然后保存在同一目录中,例如:

python modis.py -s https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MCD19A2/2019/240/MCD19A2.A2019240.h10v04.006.2019242041719.hdf -d <destination-path/test.hdf> -t <MY-TOKEN>

我从 here 得到这个 .hdf 作为例子,选择所需的产品,然后浏览顶部的标题(时间、位置、文件)以获取 .hdf 文件。

关于python - 我想使用 LADS DAAC 站点中提供的 Python 脚本从 HTTP 站点下载 MODIS 数据。但是在运行脚本时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com