|
75 | 75 | UNSET,
|
76 | 76 | WithMemoization,
|
77 | 77 | _add_future_warning_tag,
|
| 78 | + _as_coord_vals, |
78 | 79 | get_transformed_name,
|
79 | 80 | get_value_vars_from_user_vars,
|
80 | 81 | get_var_name,
|
@@ -986,8 +987,19 @@ def RV_dims(self) -> Dict[str, Tuple[Union[str, None], ...]]:
|
986 | 987 |
|
987 | 988 | @property
|
988 | 989 | def coords(self) -> Dict[str, Union[Tuple, None]]:
|
989 |
| - """Coordinate values for model dimensions.""" |
990 |
| - return self._coords |
| 990 | + """Coordinate values (tuple) for model dimensions.""" |
| 991 | + return { |
| 992 | + cname: tuple(cvals) if cvals is not None else None |
| 993 | + for cname, cvals in self._coords.items() |
| 994 | + } |
| 995 | + |
| 996 | + @property |
| 997 | + def coords_typed(self) -> Dict[str, Union[np.ndarray, None]]: |
| 998 | + """Coordinate values (numpy array) for model dimensions.""" |
| 999 | + return { |
| 1000 | + cname: cvals.copy() if cvals is not None else None |
| 1001 | + for cname, cvals in self._coords.items() |
| 1002 | + } |
991 | 1003 |
|
992 | 1004 | @property
|
993 | 1005 | def dim_lengths(self) -> Dict[str, Variable]:
|
@@ -1047,9 +1059,8 @@ def add_coord(
|
1047 | 1059 | f"Either `values` or `length` must be specified for the '{name}' dimension."
|
1048 | 1060 | )
|
1049 | 1061 | if values is not None:
|
1050 |
| - # Conversion to a tuple ensures that the coordinate values are immutable. |
1051 |
| - # Also unlike numpy arrays the's tuple.index(...) which is handy to work with. |
1052 |
| - values = tuple(values) |
| 1062 | + # Conversion to numpy array to ensure coord vals are 1-dim |
| 1063 | + values = _as_coord_vals(values) |
1053 | 1064 | if name in self.coords:
|
1054 | 1065 | if not np.array_equal(values, self.coords[name]):
|
1055 | 1066 | raise ValueError(f"Duplicate and incompatible coordinate: {name}.")
|
@@ -1107,7 +1118,7 @@ def set_dim(self, name: str, new_length: int, coord_values: Optional[Sequence] =
|
1107 | 1118 | actual=len_cvals,
|
1108 | 1119 | expected=new_length,
|
1109 | 1120 | )
|
1110 |
| - self._coords[name] = tuple(coord_values) |
| 1121 | + self._coords[name] = _as_coord_vals(coord_values) |
1111 | 1122 | self.dim_lengths[name].set_value(new_length)
|
1112 | 1123 | return
|
1113 | 1124 |
|
@@ -1278,8 +1289,7 @@ def set_data(
|
1278 | 1289 | actual=len(new_coords),
|
1279 | 1290 | expected=new_length,
|
1280 | 1291 | )
|
1281 |
| - # store it as tuple for immutability as in add_coord |
1282 |
| - self._coords[dname] = tuple(new_coords) |
| 1292 | + self._coords[dname] = _as_coord_vals(new_coords) |
1283 | 1293 |
|
1284 | 1294 | shared_object.set_value(values)
|
1285 | 1295 |
|
|
0 commit comments