将netCDF4.Dataset简单转换为xarray数据集

时间:2017-09-26 18:52:43

标签: python netcdf python-xarray python-siphon

我知道如何手动将netCDF4.Dataset转换为xarray DataArray。但是,我想知道是否有任何简单而优雅的方式,例如使用xarray后端,简单转换以下' netCDF4.Dataset'对象到xarray DataArray对象:

<type 'netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    Originating_or_generating_Center: US National Weather Service, National Centres for Environmental Prediction (NCEP)
    Originating_or_generating_Subcenter: NCEP Ensemble Products
    GRIB_table_version: 2,1
    Type_of_generating_process: Ensemble forecast
    Analysis_or_forecast_generating_process_identifier_defined_by_originating_centre: Global Ensemble Forecast System (GEFS)
    Conventions: CF-1.6
    history: Read using CDM IOSP GribCollection v3
    featureType: GRID
    History: Translated to CF-1.0 Conventions by Netcdf-Java CDM (CFGridWriter2)
Original Dataset = /data/ldm/pub/native/grid/NCEP/GEFS/Global_1p0deg_Ensemble/member/GEFS_Global_1p0deg_Ensemble_20170926_0600.grib2.ncx3#LatLon_181X360-p5S-180p0E; Translation Date = 2017-09-26T17:50:23.259Z
    geospatial_lat_min: 0.0
    geospatial_lat_max: 90.0
    geospatial_lon_min: 0.0
    geospatial_lon_max: 359.0
    dimensions(sizes): time2(2), ens(21), isobaric1(12), lat(91), lon(360)
    variables(dimensions): float32 u-component_of_wind_isobaric_ens(time2,ens,isobaric1,lat,lon), float64 time2(time2), int32 ens(ens), float32 isobaric1(isobaric1), float32 lat(lat), float32 lon(lon), float32 v-component_of_wind_isobaric_ens(time2,ens,isobaric1,lat,lon)
    groups: 

我使用siphon.ncss得到了这个。

1 个答案:

答案 0 :(得分:4)

xarray的下一个版本(0.10)支持这一点,或者至少从netCDF4获取xarray 数据集,正是因为你试图使用它:< / p>

def generator(l, batch_size=2):
    gen = iter(itertools.cycle(l))
    while 1:
        yield [next(gen) for _ in range(batch_size)]

gen = generator(l, batch_size=2)

或者使用import xarray as xr nc = nc4.Dataset('filename.nc', mode='r') # Or from siphon.ncss dataset = xr.open_dataset(xr.backends.NetCDF4DataStore(nc)) ,这看起来像是:

siphon.ncss

在它发布之前,你可以从master安装xarray。否则,唯一的其他解决方案是手动完成所有操作。