1
我对xarray中的时间变量有误解。我有时间既是无限的维度和变量netcdf4文件这样定义(如坐标变量):python xarray失去时间变量的单位属性
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'