oceanspy icon indicating copy to clipboard operation
oceanspy copied to clipboard

'OceanDataset' object is not subscriptable

Open MaceKuailv opened this issue 2 years ago • 5 comments

  • 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]]

MaceKuailv avatar Aug 21 '22 14:08 MaceKuailv

Any reason to not do this?

ThomasHaine avatar Aug 22 '22 08:08 ThomasHaine

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.

  1. Users familiar with other packages may want to have a more similar interface.
  2. One would not need to always explicitly call out the xarray dataset when using oceanspy, it feels more coherent.

MaceKuailv avatar Aug 22 '22 08:08 MaceKuailv

Sounds fine with me.

ThomasHaine avatar Aug 22 '22 09:08 ThomasHaine

@malmans2 is this a good idea?

ThomasHaine avatar Aug 30 '22 19:08 ThomasHaine

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!

malmans2 avatar Sep 01 '22 08:09 malmans2

@MaceKuailv please explore further and make some test/demo code to show the pros and cons of this change.

ThomasHaine avatar Sep 13 '22 19:09 ThomasHaine

We eventually decided that we do not need this functionality.

MaceKuailv avatar Oct 20 '22 14:10 MaceKuailv