设置decode_cf=False时Xarray无法解码netCDF的time变量
问题:Xarray中设置
decode_cf=False时无法将time变量解码为np.datetime64类型 问题背景
我有如下结构的netCDF文件:
dimensions: locations = UNLIMITED ; // (37 currently) time = UNLIMITED ; // (1085 currently) variables: int64 locationIndex(time) ; locationIndex:long_name = "which location this observation is for" ; locationIndex:instance_dimension = "locations" ; float lon(locations) ; lon:standard_name = "longitude" ; lon:long_name = "location longitude" ; lon:units = "degrees_east" ; lon:valid_range = -180., 180. ; float lat(locations) ; lat:standard_name = "latitude" ; lat:long_name = "location latitude" ; lat:units = "degrees_north" ; lat:valid_range = -90., 90. ; float alt(locations) ; alt:standard_name = "height" ; alt:long_name = "vertical distance above the surface" ; alt:units = "m" ; alt:positive = "up" ; alt:axis = "Z" ; int64 location_id(locations) ; string location_description(locations) ; double time(time) ; time:standard_name = "time" ; time:long_name = "time of measurement" ; time:units = "days since 1970-01-01T00:00:00" ; int64 sat_id(time) ; sat_id:_FillValue = 127LL ; sat_id:name = "sat_id" ; sat_id:coordinates = "time lat lon alt" ; byte as_des_pass(time) ; as_des_pass:_FillValue = 127b ; as_des_pass:name = "as_des_pass" ; as_des_pass:coordinates = "time lat lon alt" ; ...
使用decode_cf=True打开文件时,多数byte和int变量会被转换为float类型(推测是NaN值导致的问题),因此我选择设置decode_cf=False。但由于不同文件的时间单位存在差异,我希望将time变量单独解码为np.datetime64类型。
预期与实际结果
预期结果
原本以为设置decode_cf=False和decode_times=True后,会得到如下结果:
>>> a = xr.open_dataset(i_file, decode_cf=False, decode_times=True) >>> a <xarray.Dataset> Dimensions: (time: 1040, locations: 37) Coordinates: * time (time) datetime64[ns] 2021-03-01T19:17... Dimensions without coordinates: locations Data variables: (12/34) locationIndex (time) int64 ... lon (locations) float32 ... lat (locations) float32 ... alt (locations) float32 ... location_id (locations) int64 ... location_description (locations) object ... ...
实际结果
但实际得到的time变量仍是相对参考时间的float数值:
>>> a = xr.open_dataset(i_file, decode_cf=False, decode_times=True) >>> a <xarray.Dataset> Dimensions: (time: 1040, locations: 37) Coordinates: * time (time) float64 1.869e+04 ... 1.872e+04 Dimensions without coordinates: locations Data variables: (12/34) locationIndex (time) int64 ... lon (locations) float32 ... lat (locations) float32 ... alt (locations) float32 ... location_id (locations) int64 ... location_description (locations) object ... ...
复现示例
用示例文件执行以下代码可复现该问题:
>>> import xarray as xr >>> file = "ECMWF_ERA-40_subset.nc" >>> ds = xr.open_dataset(file, decode_cf=False, decode_times=True) >>> ds <xarray.Dataset> Dimensions: (longitude: 144, latitude: 73, time: 62) Coordinates: * longitude (longitude) float32 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5 * latitude (latitude) float32 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0 * time (time) int32 898476 898482 898500 898506 ... 899178 899196 899202 Data variables: (12/17) tcw (time, latitude, longitude) int16 ... tcwv (time, latitude, longitude) int16 ... lsp (time, latitude, longitude) int16 ... cp (time, latitude, longitude) int16 ... msl (time, latitude, longitude) int16 ... blh (time, latitude, longitude) int16 ... ... ... e (time, latitude, longitude) int16 ... lcc (time, latitude, longitude) int16 ... mcc (time, latitude, longitude) int16 ... hcc (time, latitude, longitude) int16 ... tco3 (time, latitude, longitude) int16 ... tp (time, latitude, longitude) int16 ... Attributes: Conventions: CF-1.0 history: 2004-09-15 17:04:29 GMT by mars2netcdf-0.92
求助
有没有人遇到过这个问题,或是知道解决方法?
内容的提问来源于stack exchange,提问作者Clay
相关产品推荐
相关产品推荐

