我对 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'