2017-09-26 270 views
2

我知道如何手動將netCDF4.Dataset轉換爲xarray DataArray。不過,我想知道是否有簡單而優雅的方式,例如使用xarray後端,以下「netCDF4.Dataset」對象的簡單轉換爲xarray於DataArray對象:netCDF4.Dataset簡單轉換爲xarray數據集

<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

+0

是一個更好的標題爲您的問題:「簡單的netCDF4.Dataset轉換爲xarray.Dataset」?如果是這樣,@DopplerShift有正確的答案。 – jhamman

+0

由於netCDF4.Dataset可以包含多個變量,因此無法將其轉換爲DataArray ... – DopplerShift

回答

4

xarray的下一個版本(0.10)有支持這事,或者至少得到一個xarray 數據集從netCDF4之一,正是原因你要使用它:

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,這看起來像:

from datetime import datetime 
from siphon.catalog import TDSCatalog 
import xarray as xr 

gfs_cat = TDSCatalog('http://thredds.ucar.edu/thredds/catalog' 
        '/grib/NCEP/GFS/Global_0p5deg/catalog.xml') 
latest = gfs_cat.latest 
ncss = latest.subset() 
query = ncss.query().variables('Temperature_isobaric') 
query.time(datetime.utcnow()).accept('netCDF4') 
nc = ncss.get_data(query) 
dataset = xr.open_dataset(xr.backends.NetCDF4DataStore(nc)) 

,直到它的發佈,你可以從主安裝xarray。否則,唯一的其他解決方案是手動完成所有工作。

+0

是的,當我使用xarray'0.9.6'和上面的代碼時,我收到** TypeError **: '預期的字符串或Unicode對象,找到netCDF4.Dataset'。但是,當我使用xarray'0.9.6-51-g25d1855'時,在'siphon.ncss中調用了數據集(tmp_file.name,'r')'時,我收到** RuntimeError **:'NetCDF:HDF error' '。你能重現嗎? – Ales

+0

我更新了答案,以明確顯示如何使用'siphon.ncss'。 – DopplerShift

+0

鏈接到筆記本電腦不適合我。 – DopplerShift