USGS批量下载站点日径流数据

1、下载代码 

import requests
import os

def read_site_ids(file_path):
    """
    Reads a file containing site IDs and returns a list of IDs.

    :param file_path: The path to the file containing site IDs.
    :return: A list of site IDs.
    """
    site_ids = []
    with open(file_path, 'r') as file:
        for line in file:
            # Assuming the site IDs are in the second column, separated by whitespace
            parts = line.split()
            if parts and len(parts) > 1:
                try:
                    # Verify it's an 8-digit number before adding
                    site_id = int(parts[1])
                    if len(str(site_id)) == 8:
                        site_ids.append(str(site_id))
                except ValueError:
                    # If it's not a number, move to the next line
                    continue
    return site_ids

def construct_url(site_id):
    """
    Constructs the URL to download data for a given site ID.

    :param site_id: The site ID to construct the URL for.
    :return: The constructed URL.
    """
    base_url = 'https://waterdata.usgs.gov/nwis/dv'
    params = {
        'cb_00060': 'on',
        'format': 'rdb',
        'site_no': site_id,
        'legacy': '',
        'referred_module': 'sw',
        'period': '',
        'begin_date': '1980-01-01',
        'end_date': '2020-12-31'
    }
    # Construct the URL with query parameters
    query_string = '&'.join([f"{key}={value}" for key, value in params.items()])
    url = f"{base_url}?{query_string}"
    return url

def download_txt_file(url, filename, target_folder):
    """
    Downloads a text file from a given URL and saves it to a target folder.

    :param url: The URL to download the file from.
    :param filename: The name to save the file as.
    :param target_folder: The folder to save the file in.
    """
    import requests
    # Get the content from the URL
    response = requests.get(url, allow_redirects=True)
    if response.status_code == 200:
        # Save the content to a file in the target folder
        with open(f"{target_folder}/{filename}", 'wb') as f:
            f.write(response.content)
    else:
        print(f"Failed to download data for site {filename} - Status code: {response.status_code}")

# We will comment out the actual calls to these functions and provide them for execution on the user's local environment
# Assume we're reading from '/mnt/data/Sites_ID_USGS.txt'
file_path = 'E:/Dataset_2024/Sites_ID_USGS.txt'
site_ids = read_site_ids(file_path)
target_folder = 'E:/Dataset_2024/USGS/'  # Assume this is the target folder
# Uncomment the following code to perform the download (to be run by the user locally)

for site_id in site_ids:
    url = construct_url(site_id)
    download_txt_file(url, f"{site_id}.txt", target_folder)

2、所需要的输入 

截图是在USGS网站筛选有径流数据的站点后下载的文件(记得改为txt后缀),见下图:

3、下载好的文件名为站点ID,每个站点一个txt文件。

借鉴了下面的内容:

从usgs上批量下载径流数据(自用)-CSDN博客

 (留个坑更新获取和处理站点对应流域的教程)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值