| 72 | |
| 73 | |
| 74 | class ScipyArrayWrapper(BackendArray): |
| 75 | datastore: ScipyDataStore |
| 76 | variable_name: str |
| 77 | shape: tuple[int, ...] |
| 78 | dtype: np.dtype |
| 79 | |
| 80 | def __init__(self, variable_name: str, datastore: ScipyDataStore) -> None: |
| 81 | self.datastore = datastore |
| 82 | self.variable_name = variable_name |
| 83 | array = self.get_variable().data |
| 84 | self.shape = array.shape |
| 85 | self.dtype = np.dtype(array.dtype.kind + str(array.dtype.itemsize)) |
| 86 | |
| 87 | def get_variable(self, needs_lock: bool = True) -> scipy.io.netcdf_variable: |
| 88 | ds = self.datastore._manager.acquire(needs_lock) |
| 89 | return ds.variables[self.variable_name] |
| 90 | |
| 91 | def _getitem(self, key): |
| 92 | with self.datastore.lock: |
| 93 | data = self.get_variable(needs_lock=False).data |
| 94 | return data[key] |
| 95 | |
| 96 | def __getitem__(self, key): |
| 97 | data = indexing.explicit_indexing_adapter( |
| 98 | key, self.shape, indexing.IndexingSupport.OUTER_1VECTOR, self._getitem |
| 99 | ) |
| 100 | # Copy data if the source file is mmapped. This makes things consistent |
| 101 | # with the netCDF4 library by ensuring we can safely read arrays even |
| 102 | # after closing associated files. |
| 103 | copy: bool | None = self.datastore.ds.use_mmap |
| 104 | |
| 105 | # adapt handling of copy-kwarg to numpy 2.0 |
| 106 | # see https://github.com/numpy/numpy/issues/25916 |
| 107 | # and https://github.com/numpy/numpy/pull/25922 |
| 108 | copy = None if HAS_NUMPY_2_0 and copy is False else copy |
| 109 | |
| 110 | return np.array(data, dtype=self.dtype, copy=copy) |
| 111 | |
| 112 | def __setitem__(self, key, value): |
| 113 | with self.datastore.lock: |
| 114 | data = self.get_variable(needs_lock=False) |
| 115 | try: |
| 116 | data[key] = value |
| 117 | except TypeError: |
| 118 | if key is Ellipsis: |
| 119 | # workaround for GH: scipy/scipy#6880 |
| 120 | data[:] = value |
| 121 | else: |
| 122 | raise |
| 123 | |
| 124 | |
| 125 | # This is a dirty workaround to allow pickling of the flush_only_netcdf_file class. |
no outgoing calls
no test coverage detected
searching dependent graphs…