https://github.com/pydata/xarray
https://ftp.ncep.noaa.gov/data/nccf/com/hrrr
https://github.com/ecmwf/cfgrib
conda install -c conda-forge cfgrib
pip install cfgrib
isobaricInhPa 气压坐标系、垂直坐标系,一般是多层高空数据
surface是地面数据
读取会生成.idx文件,读取报错时记得删除.idx文件
ds = xr.open_dataset('hrrr.t18z.wrfnatf00.grib2', engine='cfgrib', backend_kwargs={'filter_by_keys':{'stepType': 'instant', 'typeOfLevel': 'surface'}}) #地面实时数据
ds = cfgrib.open_datasets(r'hrrr.t18z.wrfnatf00.grib2') #所有数据
ds = xr.open_dataset(r'hrrr.t18z.wrfnatf00.grib2', engine='cfgrib',
backend_kwargs={'filter_by_keys': {'stepType': 'max', 'typeOfLevel': 'heightAboveGround',
'paramId': 207}}) #推荐,该读取方式不会报错
instant是实时的
accum是累计
[{'typeOfLevel': 'hybrid'}, {'typeOfLevel': 'depthBelowLandLayer'}, {'typeOfLevel': 'atmosphere'}, {'typeOfLevel': 'cloudTop'}, {'typeOfLevel': 'surface'}, {'typeOfLevel': 'heightAboveGround'}, {'typeOfLevel': 'pressureFromGroundLayer'}, {'typeOfLevel': 'sigmaLayer'}, {'typeOfLevel': 'meanSea'}, {'typeOfLevel': 'isobaricInhPa'}, {'typeOfLevel': 'heightAboveGroundLayer'}, {'typeOfLevel': 'unknown'}, {'typeOfLevel': 'isobaricLayer'}, {'typeOfLevel': 'cloudBase'}, {'typeOfLevel': 'nominalTop'}, {'typeOfLevel': 'adiabaticCondensation'}]
https://www.pyngl.ucar.edu/Nio.shtml
https://github.com/ncar/pynio
conda install pynio
安装失败git下载发布包
python setup.py build
python setup.py install
import numpy as np
import xarray as xr
ds = xr.open_dataset(grid_path, engine='pynio',
backend_kwargs={
'format': 'grib2'}
)
all_keys: List[str] = list(ds.keys())
http://xarray.pydata.org/en/stable/generated/xarray.concat.html
https://programtalk.com/python-examples/xarray.concat/
data = Dataset({'foo': ('x', np.random.randn(10))})
print('data=',data)
objs = [data.isel(x=slice(5)), data.isel(x=slice(5, None))]
print('objs=',objs)
for data_vars in ['minimal', 'different', 'all', [], ['foo']]:
actual = xr.concat(objs, dim='x', data_vars=data_vars) #dim必填,沿X轴拼接
print('actual=',actual)
xr_list = []
for index, row in latest_data.iterrows():
xr_one = get_xr_data(row['file'], row['yb_time'])
xr_list.append(xr_one)
xrData = xr.concat(xr_list, dim='time') #单时间xr合并为多时间xr
#xarray.Dataset (time,level,longitude,latitude)
<class 'tuple'>: (1, 19, 281, 361)
<class 'list'>: [700, 400, 850, 800, 100, 900, 300, 950, 925, 500, 1000, 50, 250, 200, 600, 70, 150, 10, 20]
#level次序混乱,按level排序
var_xr = ds[‘t’].sortby('level')
rect_data.sortby('latitude', 0) #latitude倒序
xrData = xr.DataArray(xr.data,
coords={
'latitude': lat,
'longitude': lon,
'time': [datetime.strptime(file_time, '%Y%m%d%H%M%S')],
'level': levels
},
dims=['time', 'level', 'latitude', 'longitude'])
xrData = xrData.sel(level=[10.200]) #截取level层
xrData = xrData.sel(latitude=slice(maxlat, minlat),
longitude=slice(minlon, maxlon)) #截取经纬度范围
获取固定经纬度点(lon,lat)的所有数据
xrData.sel(longitude=113,latitude=23)
插值到某个经纬度点(lon,lat)的数据
xrData.interp(longitude=113,latitude=23)
1.nearest:最邻近插值法
2.zero:阶梯插值
3.slinear、linear:线性插值
4.quadratic、cubic:2、3阶B样条曲线插值
与4相对
http://xarray.pydata.org/en/stable/generated/xarray.Dataset.drop_sel.html#xarray.Dataset.drop_sel
ds.drop_sel(level=[10.200]) #丢弃高度层10、200
http://xarray.pydata.org/en/stable/generated/xarray.Dataset.filter_by_attrs.html#xarray.Dataset.filter_by_attrs
ds = xr.Dataset(...)
temp_attr = dict(standard_name="air_potential_temperature")
precip_attr = dict(standard_name="convective_precipitation_flux")
ds.filter_by_attrs(standard_name="convective_precipitation_flux")
Data variables:
temperature (x, y, time) float64 22.29 18.32 17.25 ... 13.1 6.688 22.36
precipitation (x, y, time) float64 7.428 6.19 4.359 ... 9.96 1.33 2.406
np.str转np.float32然后*10然后转np.int32(防止int8长度不够)然后转np.str然后取数组
levels = (dat['level'].astype('float32')*10).astype('int32').astype('str').data
沿某些维度取平均值,同np.mean
DataArray.mean('lon') #lon轴消失,沿lon轴取平均值
data_var.assign_coords(level=data_var.level/100)
ds = xr.open_dataset(f'E:\chqqh\wrf\jan_avg_wrf_d04.nc', drop_variables='z')
# 重复的要素z报错,放弃读取