如何使用python下载nasa卫星OPeNDAP数据

2024-10-01 07:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试过requests、pydap、urllib和netcdf4,在尝试下载以下NASA数据时,总是会遇到重定向错误或权限错误:

GLDAS NOAH025SUBP_3H:GLDAS Noah陆地表面模型L4 3每小时0.25 x 0.25度,子集V001(http://disc.sci.gsfc.nasa.gov/uui/datasets/GLDAS_NOAH025SUBP_3H_V001/summary?keywords=Hydrology

我正在尝试下载大约50k个文件,这里有一个例子,当粘贴到googlechrome浏览器(如果你有正确的用户名和密码)时,它可以工作:

http://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FGLDAS_V1%2FGLDAS_NOAH025SUBP_3H%2F2016%2F244%2FGLDAS_NOAH025SUBP_3H.A2016244.2100.001.2016256190725.grb&FORMAT=TmV0Q0RGLw&BBOX=-11.95%2C28.86%2C-0.62%2C40.81&LABEL=GLDAS_NOAH025SUBP_3H.A2016244.2100.001.2016286201048.pss.nc&SHORTNAME=GLDAS_NOAH025SUBP_3H&SERVICE=SUBSET_GRIB&VERSION=1.02&LAYERS=AAAB&DATASET_VERSION=001

有没有人有使用python从web上获取OPeNDAP NASA数据的经验?如果需要,我很乐意提供更多信息。在

以下是请求尝试,它给出401错误:

import requests

def httpdownload():
    '''loop through each line in the text file and open url'''
    httpfile = open(pathlist[0]+"NASAdownloadSample.txt", "r")
    for line in httpfile:
        print line 
        outname = line[-134:-122]+".hdf"
        print outname 
        username = ""
        password = "*"
        r = requests.get(line, auth=("username", "password"), stream=True)
        print r.text
        print r.status_code
        with open(pathlist[0]+outname, 'wb') as out:
             out.write(r.content)
        print outname, "finished" # keep track of progress

下面是pydap示例,它给出了重定向错误:

^{pr2}$

Tags: 数据http错误lineopenrequests重定向nasa
1条回答
网友
1楼 · 发布于 2024-10-01 07:48:48

我没有找到使用python的解决方案,但是根据我现在掌握的信息,这应该是可能的。我将wget与.netrc文件和cookie文件一起使用,如下所示(https://disc.gsfc.nasa.gov/information/howto?title=How%20to%20Download%20Data%20Files%20from%20HTTP%20Service%20with%20wget):

#!/bin/bash 

cd # path to output files 
touch .netrc
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> .netrc
chmod 0600 .netrc
touch .urs_cookies
wget  content-disposition  trust-server-names  load-cookies ~/.urs_cookies  save-cookies ~/.urs_cookies  auth-no-challenge=on  keep-session-cookies 
-i <path to text file of url list>

希望它能帮助其他人从这个服务器上处理美国宇航局的数据。在

相关问题 更多 >