8
8
9
9
from .dataset import Dataset
10
10
from .dfs import Dfs0
11
- from .eum import ItemInfo
11
+ from .eum import ItemInfo , EUMUnit , EUMType
12
12
from .spatial import GeometryFM2D
13
13
14
14
@@ -33,33 +33,17 @@ def _extract_track(
33
33
34
34
n_items = len (item_numbers )
35
35
36
- if isinstance (track , str ):
37
- filename = track
38
- path = Path (filename )
39
- if not path .exists ():
40
- raise ValueError (f"{ filename } does not exist" )
41
-
42
- ext = path .suffix .lower ()
43
- if ext == ".dfs0" :
44
- df = Dfs0 (filename ).to_dataframe ()
45
- elif ext == ".csv" :
46
- df = pd .read_csv (filename , index_col = 0 , parse_dates = True )
47
- df .index = pd .DatetimeIndex (df .index )
48
- else :
49
- raise ValueError (f"{ ext } files not supported (dfs0, csv)" )
50
-
51
- times = df .index
52
- coords = df .iloc [:, 0 :2 ].to_numpy (copy = True )
53
-
54
- elif isinstance (track , Dataset ):
55
- times = track .time
56
- coords = np .zeros (shape = (len (times ), 2 ))
57
- coords [:, 0 ] = track [0 ].to_numpy ().copy ()
58
- coords [:, 1 ] = track [1 ].to_numpy ().copy ()
59
- else :
60
- assert isinstance (track , pd .DataFrame )
61
- times = track .index
62
- coords = track .iloc [:, 0 :2 ].to_numpy (copy = True )
36
+ match track :
37
+ case str ():
38
+ times , coords = _get_track_data_from_file (track )
39
+ case Dataset ():
40
+ times , coords = _get_track_data_from_dataset (track )
41
+ case pd .DataFrame ():
42
+ times , coords = _get_track_data_from_dataframe (track )
43
+ case _:
44
+ raise ValueError (
45
+ "track must be a file name, a Dataset or a pandas DataFrame"
46
+ )
63
47
64
48
assert isinstance (
65
49
times , pd .DatetimeIndex
@@ -157,11 +141,72 @@ def is_EOF(step: int) -> bool:
157
141
data_list [item + 2 ][t ] = dati [item ]
158
142
159
143
if geometry .is_geo :
160
- items_out = [ItemInfo ("Longitude" ), ItemInfo ("Latitude" )]
144
+ items_out = [
145
+ ItemInfo ("Longitude" , EUMType .Latitude_longitude , EUMUnit .degree ),
146
+ ItemInfo ("Latitude" , EUMType .Latitude_longitude , EUMUnit .degree ),
147
+ ]
161
148
else :
162
- items_out = [ItemInfo ("x" ), ItemInfo ("y" )]
149
+ items_out = [
150
+ ItemInfo ("x" , EUMType .Geographical_coordinate , EUMUnit .meter ),
151
+ ItemInfo ("y" , EUMType .Geographical_coordinate , EUMUnit .meter ),
152
+ ]
163
153
164
154
for item_info in items :
165
155
items_out .append (item_info )
166
156
167
157
return Dataset (data_list , times , items_out )
158
+
159
+
160
+ def _get_track_data_from_dataset (track : Dataset ) -> tuple [pd .DatetimeIndex , np .ndarray ]:
161
+ times = track .time
162
+ coords = np .zeros (shape = (len (times ), 2 ))
163
+ coords [:, 0 ] = track [0 ].to_numpy ().copy ()
164
+ coords [:, 1 ] = track [1 ].to_numpy ().copy ()
165
+ return times , coords
166
+
167
+
168
+ def _get_track_data_from_dataframe (
169
+ track : pd .DataFrame ,
170
+ ) -> tuple [pd .DatetimeIndex , np .ndarray ]:
171
+ times = track .index
172
+ coords = track .iloc [:, 0 :2 ].to_numpy (copy = True )
173
+ return times , coords
174
+
175
+
176
+ def _get_track_data_from_file (track : str ) -> tuple [pd .DatetimeIndex , np .ndarray ]:
177
+ filename = track
178
+ path = Path (filename )
179
+ if not path .exists ():
180
+ raise FileNotFoundError (f"{ filename } does not exist" )
181
+
182
+ ext = path .suffix .lower ()
183
+ match ext :
184
+ case ".dfs0" :
185
+ df = Dfs0 (filename ).to_dataframe ()
186
+ case ".csv" :
187
+ df = pd .read_csv (filename , index_col = 0 , parse_dates = True )
188
+ df .index = pd .DatetimeIndex (df .index )
189
+ case _:
190
+ raise ValueError (f"{ ext } files not supported (dfs0, csv)" )
191
+
192
+ times = df .index
193
+ coords = df .iloc [:, 0 :2 ].to_numpy (copy = True )
194
+
195
+ return times , coords
196
+
197
+
198
+ def _find_end_index (t_rel : pd .Index , end_time : pd .Timestamp ) -> int :
199
+ # largest idx for which (times - self.end_time)<=0
200
+ tmp = np .where (t_rel <= 0 )[0 ]
201
+ if len (tmp ) == 0 :
202
+ raise ValueError ("No time overlap!" )
203
+ i_end = tmp [- 1 ]
204
+ return i_end
205
+
206
+
207
+ def _find_start_index (t_rel : pd .Index , start_time : pd .Timestamp ) -> int :
208
+ tmp = np .where (t_rel >= 0 )[0 ]
209
+ if len (tmp ) == 0 :
210
+ raise ValueError ("No time overlap!" )
211
+ i_start = tmp [0 ] # smallest idx for which t_rel>=0
212
+ return i_start
0 commit comments