Python | 降水 | 批量下载GPCP日数据

文摘   2024-09-01 12:46   北京  

GPCP daily data

有关其他降水数据集的介绍我之前推文有介绍,可移步至出处查看:

Data | 降水数据集:概述和比较

GPCP的日平均数据(简称1DD)提供 1996 年 10 月至今期间全球 1 天(每日)降水量的 1 度网格估算。是通过最佳合并根据国际降水相关卫星星座观测到的微波、红外和探测数据以及降水计分析计算得出的估计值而产生的。

GPCP使用阈值匹配降水指数(TMPI)来获取每日降水估计,该估计基于40°S-40°N之间的局部月降水数据来确定降水率。在其他地区,则使用经调整的探空基降水率(来源于TOVS和AIRS红外卫星反演数据)。

GPCP 1DD的主要优点是它提供了一个全球范围内、在时间和空间上相对高分辨率的降水格点记录。

这个数据集的主要局限性(如所有合并卫星降水产品一样)在于将稀疏的卫星降水测量转化为高分辨率格点降水估计的间接性和复杂性。地球静止轨道红外数据是一个非常重要的输入组成部分,它感知云层的属性,而不是直接测量降水本身。卫星只能间接测量与地表降水率相关的量:微波和红外卫星测量的亮温,然后间接转换为降水率,而雷达则测量云滴和雨滴在整个柱状深度中反射的能量。然后,这些间接测量(连同陆地上的直接量规测量)被用作输入,通过一个复杂的算法产生定期格网上的地表降水率估计。最后,GPCP使用的卫星数据漏掉了许多轻微降水事件。

这个数据集的研究应用主要包括:

  • 降雨的气候态分布及其频率和强度
  • 极端降水的变化
  • 以及降水系统的特性

当比较不同空间分辨率的降水数据集时,计算和解释中的错误大量存在。降雨率,尤其是特定位置的降雨率分布,很大程度上取决于数据集的空间分辨率。

GPCP 1dd 数据未提供不确定性估计。然而,GPCP 月平均产品的月平均值误差也与该数据集相关。

GPCP

GPCP版本

GPCP目前有多个版本:

  • daily v1.3:此版本的数据可获取到距今三个月的数据,是推荐的每日版本。
  • daily v3.2:这是最新的版本,但目前只包含2000年6月至2021年9月的时间范围。
  • monthly v2.3:这是推荐的每月版本,数据更新至当前日期。
  • monthly v3.2:这是最新版本,但目前只有1983年至2023年的数据。

Python

"""
Created on Mon Oct 31 11:43:46 2022
@author: Skyborn
"""

import os
import requests
import re
from datetime import datetime
from bs4 import BeautifulSoup

def download_file(url, fname):
    '''Download file using requests'''
    r = requests.get(url)
    with open(fname, 'wb'as f:
        f.write(r.content)
    return 

def parse_dir(syr, url, data_dir):
    '''Parse main page for year and download new files.
    
    Find all the links for netcdf files in that year, if file does not
    exists locally then download it. If file exists, compare remote and 
    local last modified dates.
    '''

    r = requests.get(url)
    main_page = BeautifulSoup(r.content,'html.parser')
    for link in main_page.find_all('a',string=re.compile('^%s/' % syr)):
        subdir=link.get('href')
        r2 = requests.get("/".join([url,subdir]))
        year_page = BeautifulSoup(r2.content,'html.parser')
        for flink in year_page.find_all('a',string=re.compile('^gpcp_.*\.nc$')):
            href=flink.get('href')
            local_name="/".join([data_dir,subdir[:4],href])
            if not os.path.exists(local_name):
                download_file("/".join([url,subdir,href]),
                              local_name)
                print(local_name, 'done')
    return


def extra_files(yr, data_dir):
    '''Check if there is more than one file for day in data directory'''
    files = os.listdir(data_dir + f"/{yr}")
    alldates = []
    tocheck = []
    # first check if two consecutive files have same date
    for f in files:
        fdate = f.split("_")[-2]
        if fdate in alldates:
            print(f"Found extra file for {fdate}")
            tocheck.append(fdate)
        alldates.append(fdate)
    if tocheck != []:
        for fdate in tocheck:
            doubles = [f for f in files if fdate in f]
            cr_dates = [f.split("_")[-1for f in doubles]
            print(cr_dates)
            if cr_dates[1] > cr_dates[0]:
                fpath = doubles[0]
            else:
                fpath = doubles[1]
            os.rename(fpath, fpath.replace(f"/{yr}/","/redundant/"))
            print(f"Moved {fpath.split('/')[-1]} to redundant directory")
    return


def DownLoadDataSet(yr, version, tstep, data_dir):
    yr = yr
    tstep = tstep
    version = version
    dataset_dir = data_dir
    # define url for GPCP http server and data_dir for local collection
    today = datetime.today().strftime('%Y-%m-%d')
    root_dir = os.getenv("AUSREFDIR"f"{dataset_dir}")
    if tstep == "daily":
        data_dir = f"{root_dir}/gpcp/data/day/{version}/"
    else:
        data_dir = f"{root_dir}/gpcp/data/mon/{version}/"
      
    if not os.path.exists(data_dir):
        os.makedirs(data_dir)
    # choose url base don version
    url_ncei = "https://www.ncei.noaa.gov/data/global-precipitation-" + f"climatology-project-gpcp-{tstep}/access/"
    if tstep == 'daily':
        tstep2 = 'DAY'
    else
        tstep2 = 'MON'
    url_gesdisc = f"https://measures.gesdisc.eosdis.nasa.gov/data/GPCP/GPCP{tstep2}.3.2/"
    # set up to complete
    url_dict = {'v1-2': url_ncei, 'v2-3': url_ncei, 'v3-2': url_gesdisc}
    url = url_dict[version]
    try:
        os.chdir(data_dir + f"/{yr}")
    except:
        os.mkdir(data_dir + f"/{yr}")
    # download/update the selected year
    print(f"Updated on {today}")
    print(f"Downloading files for {yr}")
    parse_dir(yr, url, data_dir)
    # check if there are more than one file for each date
    print("Checking for redundant files")
    extra_files(yr, data_dir)
    print("Download is complete")

def main(start_year, end_year, version, tstep, data_dir):
    for yr in range(start_year, end_year + 1):
        DownLoadDataSet(yr, version, tstep, data_dir)
    pass

if __name__ == "__main__":
    # v3.2 daily: https://measures.gesdisc.eosdis.nasa.gov/data/GPCP/GPCPDAY.3.2/
    # v3.2 monthly: https://measures.gesdisc.eosdis.nasa.gov/data/GPCP/GPCPMON.3.2/2023/
    '''
    Download GPCP daily and monthly data from the NOAA server
         https://www.ncei.noaa.gov/data/global-precipitation-climatology-project-gpcp-{tstep}/access/
    using requests to download file and BeautifulSoup to find links in webpage.
    '''

    start_year = 2022
    end_year = 2023
    version = "v1-2"
    tstep = "daily"
    data_dir = "your_path"
    main(start_year, end_year, version, tstep, data_dir)

Param

  • start_year:起始年份
  • end_year:结束年份
  • version:GPCP的下载版本,该代码提供了v1-2、v2-3、v3-2三个选择。
  • tstep:分辨率,daily或者monthly。
  • data_dir:数据路径。

以上代码的使用及其简单,使用方法就不介绍了。

往期回顾

·投稿或转载请联系·
小编微信
长按可关注


气python风雨
主要发一些涉及大气科学的Python文章与个人学习备忘录
 最新文章