oceanspy
oceanspy copied to clipboard
'OceanDataset' object is not subscriptable
- OceanSpy version: 0.2.0
Description
Most ocean datasets support calls like data['variable']. But the od object is not subscriptable. Instead, we do od._ds['variable'], or od.dataset['variable'].
It may or may not be necessary. There might also be good reasons not to do this as well. But simply adding the following to the oceandataset class will support reading and writing od['variable'].
def __setitem__(self, key, item):
if isinstance(item,xr.DataArray):
self._ds[key] = item
else:
self.__dict__[key] = item
def __getitem__(self, key):
if key in self.__dict__.keys():
return self.__dict__[key]
else:
return self._ds[self.alias[key]]
Any reason to not do this?
od['S'] is less redundant than od.dataset['S'] or od._ds['salt_name_before_alias'].
Really this is more of a user experience thing than practical.
- Users familiar with other packages may want to have a more similar interface.
- One would not need to always explicitly call out the xarray dataset when using oceanspy, it feels more coherent.
Sounds fine with me.
@malmans2 is this a good idea?
Looks like a good idea!
The only downside I can see is that directly indexing the OceanDataset makes a bit harder to understand what's happening under the hood. I.e., users might miss that we are creating xarray objects and that these objects can be used even without OceanSpy.
But feel free to implement this if you think it will improve the usability of OceanSpy!
@MaceKuailv please explore further and make some test/demo code to show the pros and cons of this change.
We eventually decided that we do not need this functionality.