【问题标题】:python xarray loses the units attribute for time variablepython xarray 丢失了时间变量的单位属性
【发布时间】:2017-08-05 02:28:47
【问题描述】:

我对 xarray 中的时间变量有误解。我有一个 netcdf4 文件,时间作为无限维度和一个变量(例如坐标变量)定义如下:

   double time(time=59231);
     :_Netcdf4Dimid = 0; // int
     :units = "seconds since 2015-11-12 16:1:48.500000 0:00";

文件中还存储了EPIC约定时间(因为我还在约定测试代码之间来回折腾)

 int EPIC_time(time=59231);
     :units = "True Julian Day";
     :epic_code = 624; // int
     :datum = "Time (UTC) in True Julian Days: 2440000 = 0000 h on May 23, 1968";
     :NOTE = "Decimal Julian day [days] = time [days] + ( time2 [msec] / 86400000 [msec/day] )";
     :_Netcdf4Dimid = 0; // int
     :serial_number = "23881";
     :sensor_type = "TRDI";
     :sensor_depth = 10.367607116699219; // double
     :initial_sensor_height = 1; // int
     :initial_sensor_height_note = "height in meters above bottom:  accurate for tripod mounted instruments";
     :height_depth_units = "m";
     :_FillValue = -1; // int
     :_Unsigned = "true";

还有一个带有仪器标题的变量:

float Hdg_1215(time=59231, lat=1, lon=1);
     :_FillValue = 1.0E35f; // float
     :units = "degrees";
     :name = "Hdg";

要打开文件并使用它,我这样做:

import xarray as xr
import numpy as np
import datetime as dt
vars2omit = {'EPIC_time','EPIC_time2'}
ds = xr.open_dataset(infile,decode_times=True,drop_variables=vars2omit)

我可以用数据绘图和做事。 不过有两个非常奇怪的行为,对其的解释将帮助我理解 xarray 的内部工作原理。

打开文件后,尝试访问时间变量(坐标时间变量)的单位属性会失败。为什么?属性在文件中。

print(ds['Hdg_1215'].attrs['units']) # this works
print(ds['time'].attrs['units']) # this fails, KeyError: 'units'

【问题讨论】:

    标签: python netcdf python-xarray xarray


    【解决方案1】:

    xarray 自动将它理解的时间变量解码为坐标变量datetime64 对象,因此使用units 是不合适的。

    如果您告诉它decode_times=False,它不会创建datetime64 对象并为您留下一个简单的数字数组,需要units 来解释这些值。

    这是一个例子:

    import xarray as xr
    url ='http://geoport.whoi.edu/thredds/dodsC/usgs/data2/emontgomery/stellwagen/CF-1.6/ARGO_MERCHANT/1211P-A.cdf'
    xr.open_dataset(url)
    

    产生:

    <xarray.Dataset>
    Dimensions:                (time: 10748)
    Coordinates:
        latitude               float32 ...
        longitude              float32 ...
      * time                   (time) datetime64[ns] 1977-01-08T11:33:44 ...
        z                      float64 ...
    Data variables:
        feature_type_instance  |S64 ...
        crs                    int32 ...
    ...
    

    同时

     xr.open_dataset(url, decode_times=False)
    

    产生:

    <xarray.Dataset>
    Dimensions:                (time: 10748)
    Coordinates:
        latitude               float32 ...
        longitude              float32 ...
      * time                   (time) float64 2.216e+08 2.216e+08 2.216e+08 ...
        z                      float64 ...
    Data variables:
        feature_type_instance  |S64 ...
        crs                    int32 ...
    

    【讨论】:

    • 这对我来说似乎是一个反特征。他们不能转换为datetime64 并仍然保留 units 属性吗?毕竟这可能是有用的元数据。例如。当您的常规间距为 1 且单位为月或天时,您可能希望在 FacetGrid 中将 colWrap 设置为 12 或 7。另外,我想知道他们在保存回(netCDF)文件时会做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2012-08-16
    相关资源
    最近更新 更多