I have a misunderstanding of time variables in xarray. I have a netcdf4 file with time as both the unlimited dimension and a variable (e.g. coordinate variable) defined thus:
double time(time=59231);
:_Netcdf4Dimid = 0; // int
:units = "seconds since 2015-11-12 16:1:48.500000 0:00";
There is also EPIC convention time stored in the file (because I'm still going back and forth here between conventions testing code)
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";
And there's a variable with instrument heading in there too:
float Hdg_1215(time=59231, lat=1, lon=1);
:_FillValue = 1.0E35f; // float
:units = "degrees";
:name = "Hdg";
To open the file and work with it I do this:
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)
I can plot and do things with the data. There are two very odd behaviors though, the explanation of which will help me understand the inner workings of xarray.
Once the file is open, trying to access the units attribute for the time variable (coordinate time variable) fails. Why? The attribute is in the file.
print(ds['Hdg_1215'].attrs['units']) # this works
print(ds['time'].attrs['units']) # this fails, KeyError: 'units'