气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)

数据访问服务:

HTTP下载。
网络地图/覆盖服务(WMS/WCS)。
简化
NetCDF子集服务
CDMRemote

1、总数据目录

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第1张图片
通过图片发现数据目录主要包括两大类:实时数据和其他数据。实时数据包括模型预测数据、模型产品和分析数据、观测数据、雷达数据和卫星数据。

2、NCEP模型预测数据目录

主要包括全球集合预报系统、全球天气预报系统(四分之一度、半度和一度)等
气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第2张图片

3、下载全球天气预报系统(四分之一度)

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第3张图片
获取想要下载的数据的网址
气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第4张图片

4、Siphon库

Siphon官方文档

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第5张图片
添加链接描述

1、from siphon.catalog import TDSCatalog#TDS数据服务器
2、from siphon.simplewebservice.wyoming import WyomingUpperAir#来自怀俄明大学的探空数据

1、NCS的GFS数据下载

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第6张图片

2、浮标数据下载

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第7张图片

3、探空数据下载

气象数据GFS、雷达、探空和浮标数据访问下载( THREDDS Catalog)_第8张图片

5、下载三级雷达数据

# Copyright (c) 2013-2015 Siphon Contributors.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""
=======================
TDS Radar Query Service
=======================

Use Siphon to get NEXRAD Level 3 data from a TDS.
"""
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from siphon.cdmr import Dataset
from siphon.radarserver import get_radarserver_datasets, RadarServer
import os
os.chdir(r"D:\Pythonbase\Test\QHTJ\DownData")
###########################################
# First, point to the top-level thredds radar server accessor to find what datasets are
# available.#首先,指向顶级线程雷达服务器访问器,以查找哪些数据集是可用的
ds = get_radarserver_datasets('http://thredds.ucar.edu/thredds/')
print(list(ds))

###########################################
# Now create an instance of RadarServer to point to the appropriate
# radar server access URL. This is pulled from the catalog reference url.
url = ds['NEXRAD Level III Radar from IDD'].follow().catalog_url #获取IDD三级雷达数据源XML文件
print("url",url)
rs = RadarServer(url)

###########################################
# Look at the variables available in this dataset,查看该数据集中可用的变量
# print(rs.variables)

###########################################
# Create a new query object to help request the data. Using the chaining
# methods, ask for data from radar FTG (Denver) for now for the product
# N0Q, which is reflectivity data for the lowest tilt. We see that when the query
# is represented as a string, it shows the encoded URL.
query = rs.query()#ncss访问数据
query.stations('FTG').time(datetime.utcnow()).variables('N0Q')
#或者使用这种方法
# time = datetime.strptime('2021-08-10T00:00:00Z','%Y-%m-%dT%H:%M:%Sz')#时间
# query.lonlat_box(north=55, south=0, east=140, west=70).time(time)##时间和空间
# query.variables('N0Q')#最低倾斜的反射率数据

###########################################
# We can use the RadarServer instance to check our query, to make
# sure we have required parameters and that we have chosen valid
# station(s) and variable(s)
# print(rs.validate_query(query))#确保输入了正确的请求参数

###########################################
# Make the request, which returns an instance of TDSCatalog. This
# handles parsing the catalog
catalog = rs.get_catalog(query)

###########################################
# We can look at the datasets on the catalog to see what data we found by the query. We
# find one NIDS file in the return.
# print(catalog.datasets)#返回查询到的数据列表

###########################################
# We can pull that dataset out of the dictionary and look at the available access URLs.
# We see URLs for OPeNDAP, CDMRemote, and HTTPServer (direct download).
ds = list(catalog.datasets.values())[0]
print(ds.access_urls)#返回三种数据下载方式

###########################################
# We'll use the CDMRemote reader in Siphon and pass it the appropriate access URL.
# data = Dataset(ds.access_urls['CdmRemote'])
# data = Dataset(ds.access_urls['OPENDAP'])#利用open的方式访问数据
data = xr.open_dataset(ds.access_urls['OPENDAP'])
# data.to_netcdf('FTG_N0Q_20210811_0318.nc','w')#数据下载



###########################################
# The CDMRemote reader provides an interface that is almost identical to the usual python
# NetCDF interface. We pull out the variables we need for azimuth and range, as well as
# the data itself.
rng = data.variables['gate'][:] / 1000. #距离数据
az = data.variables['azimuth'][:] #方位角数据
ref = data.variables['BaseReflectivityDR'][:] #反射率数据

###########################################
# Then convert the polar coordinates to Cartesian,将极坐标转化为笛卡尔坐标
print("az",az)
x = rng * np.sin(np.deg2rad(az))
y = rng * np.cos(np.deg2rad(az))
ref = np.ma.array(ref, mask=np.isnan(ref))#掩膜空值
print("x.shape",x.shape)
print("y.shape",y.shape)
print("ref.shape",ref.shape)

###########################################
# Finally, we plot them up using matplotlib.
fig, ax = plt.subplots(1, 1, figsize=(9, 8))
ax.pcolormesh(x, y, ref.T)
ax.set_aspect('equal', 'datalim')
ax.set_xlim(-460, 460)
ax.set_ylim(-460, 460)
plt.show()

6、GFS数据的NCSS和OPEN下载方式

'''
Author: your name
Date: 2021-08-05 22:28:46
LastEditTime: 2021-08-11 14:14:54
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \Test\QHTJ\Download data online\Download_GFS.py
'''
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 14 09:09:29 2021

@author: User
"""
'''
目的和意义:如何引入网格化数据之外的其他信息,将站点数据和网格化数据结合在一起,获取更高精度、更高分辨率和更高准确度的气象要素分布图。
实用性:格点化的天气预报数据往往数个小时前就已经获得,而再分析资料现在暂未获得。我们有的只有十几分钟前获得的站点观测数据,而我们需要的是一个较为准确的区域的当前网格化观测数据。
主要步骤:
下载GFS数值模式的预报数据和地面自动气象站观测数据
读取GFS预报数据的地表温度和露点温度变量
基于地表温度和露点温度,计算网格化地表相对湿度
使用cressman插值,以站点经纬度为第一猜测场,引入站点相对湿度,获得一个新的相对湿度
将GFS预报的相对湿度由经纬度网格处理为WRF的兰伯特投影
需要提前安装模块:siphon、metpy、cmaps、cartopy
'''
from siphon.catalog import TDSCatalog#TDS数据服务器
from datetime import datetime, timedelta
from xarray.backends import NetCDF4DataStore
import xarray as xr
import shutil
import metpy
import xarray as xr
import numpy as np
import os
import metpy.calc
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cmaps
import pandas as pd
import cartopy.io.shapereader as shpreader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from math import acos, asin, cos, pi, radians, sin, sqrt
from numba import *
import multiprocessing
import warnings
from netCDF4 import num2date
warnings.filterwarnings('ignore')
plt.rc('font',family='Times New Roman') #设置字体
os.chdir(r"D:\Pythonbase\Test\QHTJ\DownData")

# 一、下载GFS预报数据
def ncc_gfsdata1(path):
    best_gfs = TDSCatalog(path)
    #1、ncss访问数据
    # best_ds = list(best_gfs.datasets.values())[0]
    best_ds = best_gfs.datasets[0]
    # print(best_ds)
    #We pull out this dataset and get the NCSS access point
    ncss = best_ds.subset()
    query = ncss.query()
    #查询条件1
    mytime = datetime.strptime('2021-08-10T00:00:00Z','%Y-%m-%dT%H:%M:%Sz')#时间
    query.lonlat_box(north=55, south=0, east=140, west=70).time(mytime)##时间和空间赛选
    query.variables('Temperature_surface','Dewpoint_temperature_height_above_ground',
                'Relative_humidity_height_above_ground')#表面温度','地面上露点温度高度' '地面上相对湿度高度
    query.accept('netcdf4')#获取的数据格式
    nc = ncss.get_data(query)
    print(list(nc.variables))
    data = xr.open_dataset(NetCDF4DataStore(nc))
    ds = xr.open_dataset(NetCDF4DataStore(nc)).metpy.parse_cf()#获取变量的坐标值数组

    return data

# 一、下载GFS预报数据
def ncc_gfsdata2(path):
    best_gfs = TDSCatalog(path)
    #1、ncss访问数据
    best_ds = best_gfs.datasets[0]
    # print(best_ds)
    #We pull out this dataset and get the NCSS access point
    ncss = best_ds.subset()
    query = ncss.query()
    # #查询条件1
    # mytime = datetime.strptime('2021-08-10T00:00:00Z','%Y-%m-%dT%H:%M:%Sz')#时间
    # query.lonlat_box(north=55, south=0, east=140, west=70).time(mytime)##时间和空间赛选
    # query.variables('Temperature_surface','Dewpoint_temperature_height_above_ground',
    #             'Relative_humidity_height_above_ground')#表面温度','地面上露点温度高度' '地面上相对湿度高度
    # query.accept('netcdf4')#获取的数据格式
    #查询条件二
    now = datetime.utcnow()
    query.lonlat_point(-105, 40).vertical_level(100000).time_range(now, now + timedelta(days=7))
    query.variables('Temperature_isobaric').accept('netcdf')
    nc = ncss.get_data(query)
    print(list(nc.variables))
    time = nc.variables['time'].metpy.time.values
    print(time[:5])
    temp = nc.variables['Temperature_isobaric']
    time_vals = num2date(time[:].squeeze(), time.units)#转化时间日期
    print(time_vals[:5])
    
    fig, ax = plt.subplots(1, 1, figsize=(9, 8))
    ax.plot(time_vals, temp[:].squeeze(), 'r', linewidth=2)
    ax.set_ylabel('{} ({})'.format(temp.standard_name, temp.units))
    ax.set_xlabel('Forecast Time (UTC)')
    ax.grid(True)
    plt.show()

# 一、OPENDAP下载GFS预报数据
def open_gfsdata(path):
    # OPENDAP来访问数据
    best_gfs = TDSCatalog(path)
    ncss = best_gfs.datasets[0].subset()
    query = ncss.query()
    
    now = datetime.utcnow()
    query.lonlat_point(-105, 40).vertical_level(100000).time_range(now, now + timedelta(days=7))
    query.variables('Temperature_isobaric').accept('netcdf')
    
    nc = ncss.get_data(query)
    opendap_url = nc.access_urls['OPENDAP']#利用open的方式访问数据
    ds = xr.open_dataset(opendap_url)
    time = ds['Absolute_vorticity_isobaric'].metpy.time.values
    vertical = ds['Absolute_vorticity_isobaric'].metpy.vertical.values
    print(time)
    print(vertical)
    return ds

def Raddata(Radpath,date):
    cat = TDSCatalog(Radpath)
    request_time = date.replace(hour=12, minute=0, second=0, microsecond=0)#设置时间
    datasets = cat.datasets.filter_time_range(request_time, request_time + timedelta(hours=6))#时间过滤
    ds = datasets[0]
    print(ds)
    # ds.download()
    # fobj = ds.remote_open()#打开数据
    # data = fobj.read()#数据读取
    # nc = ds.remote_access()#获取数据
    # print(list(nc.variables))#打印变量
    return ds
   
def read_Rad(ds):
    fobj = ds.remote_open()#打开数据
    data = fobj.read()#数据读取
    print(len(data))
    nc = ds.remote_access()#获取数据
    print(list(nc.variables))#打印变量

def save_gfs(data):
    data.to_netcdf('GFS_2021071000.nc','w')#数据写入到文件
    shutil.move('GFS_2021071000.nc', './data')#文件移动到文件夹

if __name__ == '__main__':
    #数据集:GFS季度度预测/最佳GFS季度度预测时间序列
    gfspath='http://thredds.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg/catalog.xml?dataset=grib/NCEP/GFS/Global_0p25deg/Best'
    #1、ncss访问数据
    # gfsdata=ncc_gfsdata1(gfspath)
    ncc_gfsdata2(gfspath)
    # save_gfs(gfsdata)
     #1、open访问数据
    open_gfsdata(gfspath)
    
    # date = datetime.utcnow() - timedelta(days=1)
    # print(date)
    # Radpath=f'https://thredds.ucar.edu/thredds/catalog/nexrad/level2/KINX/{date:%Y%m%d}/catalog.xml'
    # ds=Raddata(Radpath,date)

7、探空数据下载

你可能感兴趣的:(机器学习在气象的应用)