81
81
Grid3D ,
82
82
]
83
83
84
+ IndexType = Union [int , slice , Sequence [int ], np .ndarray , None ]
85
+
84
86
85
87
class _DataArraySpectrumToHm0 :
86
88
def __init__ (self , da : "DataArray" ) -> None :
@@ -98,12 +100,9 @@ def __call__(self, tail: bool = True) -> "DataArray":
98
100
dims = tuple ([d for d in self .da .dims if d not in ("frequency" , "direction" )])
99
101
item = ItemInfo (EUMType .Significant_wave_height )
100
102
g = self .da .geometry
101
- if isinstance (g , GeometryFMPointSpectrum ):
102
- if g .x is not None and g .y is not None :
103
- geometry : Any = GeometryPoint2D (x = g .x , y = g .y )
104
- else :
105
- geometry = GeometryUndefined ()
106
- elif isinstance (g , GeometryFMLineSpectrum ):
103
+ geometry : Any = GeometryUndefined ()
104
+
105
+ if isinstance (g , GeometryFMLineSpectrum ):
107
106
geometry = Grid1D (
108
107
nx = g .n_nodes ,
109
108
dx = 1.0 ,
@@ -119,8 +118,6 @@ def __call__(self, tail: bool = True) -> "DataArray":
119
118
element_table = g .element_table ,
120
119
element_ids = g .element_ids ,
121
120
)
122
- else :
123
- geometry = GeometryUndefined ()
124
121
125
122
return DataArray (
126
123
data = Hm0 ,
@@ -407,7 +404,11 @@ def type(self) -> EUMType:
407
404
@property
408
405
def unit (self ) -> EUMUnit :
409
406
"""EUMUnit."""
410
- return self .item .unit
407
+ return self .item ._unit
408
+
409
+ @unit .setter
410
+ def unit (self , value : EUMUnit ) -> None :
411
+ self .item .unit = value
411
412
412
413
@property
413
414
def start_time (self ) -> datetime :
@@ -613,9 +614,18 @@ def __setitem__(self, key: Any, value: np.ndarray) -> None:
613
614
614
615
def isel (
615
616
self ,
616
- idx : int | Sequence [int ] | slice | None = None ,
617
+ idx : IndexType = None ,
618
+ * ,
619
+ time : IndexType = None ,
620
+ x : IndexType = None ,
621
+ y : IndexType = None ,
622
+ z : IndexType = None ,
623
+ element : IndexType = None ,
624
+ node : IndexType = None ,
625
+ layer : IndexType = None ,
626
+ frequency : IndexType = None ,
627
+ direction : IndexType = None ,
617
628
axis : int | str = 0 ,
618
- ** kwargs : Any ,
619
629
) -> "DataArray" :
620
630
"""Return a new DataArray whose data is given by
621
631
integer indexing along the specified dimension(s).
@@ -646,11 +656,17 @@ def isel(
646
656
y index, by default None
647
657
z : int, optional
648
658
z index, by default None
659
+ layer: int, optional
660
+ layer index, only used in dfsu 3d
661
+ direction: int, optional
662
+ direction index, only used in sprectra
663
+ frequency: int, optional
664
+ frequencey index, only used in spectra
665
+ node: int, optional
666
+ node index, only used in spectra
649
667
element : int, optional
650
668
Bounding box of coordinates (left lower and right upper)
651
669
to be selected, by default None
652
- **kwargs: Any
653
- Not used
654
670
655
671
Returns
656
672
-------
@@ -683,10 +699,23 @@ def isel(
683
699
```
684
700
685
701
"""
686
- if isinstance (self .geometry , Grid2D ) and ("x" in kwargs and "y" in kwargs ):
687
- idx_x = kwargs ["x" ]
688
- idx_y = kwargs ["y" ]
689
- return self .isel (x = idx_x ).isel (y = idx_y )
702
+ if isinstance (self .geometry , Grid2D ) and (x is not None and y is not None ):
703
+ return self .isel (x = x ).isel (y = y )
704
+ kwargs = {
705
+ k : v
706
+ for k , v in dict (
707
+ time = time ,
708
+ x = x ,
709
+ y = y ,
710
+ z = z ,
711
+ element = element ,
712
+ node = node ,
713
+ layer = layer ,
714
+ frequency = frequency ,
715
+ direction = direction ,
716
+ ).items ()
717
+ if v is not None
718
+ }
690
719
for dim in kwargs :
691
720
if dim in self .dims :
692
721
axis = dim
@@ -727,7 +756,7 @@ def isel(
727
756
spatial_axis = axis - 1 if self .dims [0 ] == "time" else axis
728
757
geometry = self .geometry .isel (idx , axis = spatial_axis )
729
758
730
- # TOOD this is ugly
759
+ # TODO this is ugly
731
760
if isinstance (geometry , _GeometryFMLayered ):
732
761
node_ids , _ = self .geometry ._get_nodes_and_table_for_elements (
733
762
idx , node_layers = "all"
@@ -770,7 +799,12 @@ def sel(
770
799
self ,
771
800
* ,
772
801
time : str | pd .DatetimeIndex | "DataArray" | None = None ,
773
- ** kwargs : Any ,
802
+ x : float | slice | None = None ,
803
+ y : float | slice | None = None ,
804
+ z : float | slice | None = None ,
805
+ coords : np .ndarray | None = None ,
806
+ area : tuple [float , float , float , float ] | None = None ,
807
+ layers : int | str | Sequence [int | str ] | None = None ,
774
808
) -> "DataArray" :
775
809
"""Return a new DataArray whose data is given by
776
810
selecting index labels along the specified dimension(s).
@@ -809,8 +843,6 @@ def sel(
809
843
layer(s) to be selected: "top", "bottom" or layer number
810
844
from bottom 0,1,2,... or from the top -1,-2,... or as
811
845
list of these; only for layered dfsu, by default None
812
- **kwargs: Any
813
- Additional keyword arguments
814
846
815
847
Returns
816
848
-------
@@ -852,24 +884,32 @@ def sel(
852
884
```
853
885
854
886
"""
887
+ # time is not part of kwargs
888
+ kwargs = {
889
+ k : v
890
+ for k , v in dict (
891
+ x = x , y = y , z = z , area = area , coords = coords , layers = layers
892
+ ).items ()
893
+ if v is not None
894
+ }
855
895
if any ([isinstance (v , slice ) for v in kwargs .values ()]):
856
- return self ._sel_with_slice (kwargs )
896
+ return self ._sel_with_slice (kwargs ) # type: ignore
857
897
858
898
da = self
859
899
860
900
# select in space
861
901
if len (kwargs ) > 0 :
862
902
idx = self .geometry .find_index (** kwargs )
903
+
904
+ # TODO this seems fragile
863
905
if isinstance (idx , tuple ):
864
906
# TODO: support for dfs3
865
907
assert len (idx ) == 2
866
- t_ax_offset = 1 if self ._has_time_axis else 0
867
908
ii , jj = idx
868
909
if jj is not None :
869
- da = da .isel (idx = jj , axis = ( 0 + t_ax_offset ) )
910
+ da = da .isel (y = jj )
870
911
if ii is not None :
871
- sp_axis = 0 if (jj is not None and len (jj ) == 1 ) else 1
872
- da = da .isel (idx = ii , axis = (sp_axis + t_ax_offset ))
912
+ da = da .isel (x = ii )
873
913
else :
874
914
da = da .isel (idx , axis = "space" )
875
915
@@ -1895,7 +1935,7 @@ def to_dfs(self, filename: str | Path, **kwargs: Any) -> None:
1895
1935
Dfs0 only: set the dfs data type of the written data
1896
1936
to e.g. np.float64, by default: DfsSimpleType.Float (=np.float32)
1897
1937
**kwargs: Any
1898
- Additional keyword arguments, e.g. dtype for dfs0
1938
+ additional arguments passed to the writing function , e.g. dtype for dfs0
1899
1939
1900
1940
"""
1901
1941
self ._to_dataset ().to_dfs (filename , ** kwargs )
0 commit comments