20
20
if TYPE_CHECKING :
21
21
from .write import BoundaryWriter # Abstract class
22
22
23
- # Import default values and auxiliry functions
23
+ # Import default values and aux_funcsiliry functions
24
24
from .. import msg
25
- from ..aux import day_list , create_filename_obj , create_filename_time , create_filename_lonlat , clean_filename , check_if_folder
26
- from ..defaults import dflt_bnd , list_of_placeholders
25
+ from ..aux_funcs import day_list
27
26
28
27
29
28
class Boundary :
@@ -42,18 +41,20 @@ def import_boundary(self, start_time: str, end_time: str, boundary_reader: Bound
42
41
are determined by the point_picker.
43
42
"""
44
43
45
- self .start_time = copy (start_time )
46
- self .end_time = copy (end_time )
47
44
self ._history .append (copy (boundary_reader ))
48
45
49
46
msg .header (boundary_reader , "Reading coordinates of spectra..." )
50
- lon_all , lat_all = boundary_reader .get_coordinates (self . start_time )
47
+ lon_all , lat_all = boundary_reader .get_coordinates (start_time )
51
48
52
49
msg .header (point_picker , "Choosing spectra..." )
53
50
inds = point_picker (self .grid , lon_all , lat_all )
54
51
52
+ if len (inds ) < 1 :
53
+ msg .warning ("PointPicker didn't find any points. Aborting import of boundary." )
54
+ return
55
+
55
56
msg .header (boundary_reader , "Loading boundary spectra..." )
56
- time , freq , dirs , spec , lon , lat , source = boundary_reader (self . start_time , end_time , inds )
57
+ time , freq , dirs , spec , lon , lat , source = boundary_reader (start_time , end_time , inds )
57
58
58
59
self .data = self .compile_to_xr (time , freq , dirs , spec , lon , lat , source )
59
60
self .mask = [True ]* len (self .x ())
@@ -130,71 +131,84 @@ def compile_to_xr(self, time, freq, dirs, spec, lon, lat, source):
130
131
)
131
132
return data
132
133
133
- def slice_data (self , start_time : str = '' , end_time : str = '' , x : List [int ]= None ):
134
- """Slice data in space (x) and time. Returns an xarray dataset."""
134
+ def slice_data (self , start_time : str = '' , end_time : str = '' , x : List [int ]= None ) -> xr .Dataset :
135
+ """Slice data in space (x) and time."""
136
+ if not hasattr (self , 'data' ):
137
+ return None
135
138
136
139
if x is None :
137
140
x = self .x ()
138
-
139
141
if not start_time :
140
142
# This is not a string, but slicing works also with this input
141
143
start_time = self .time ()[0 ]
142
-
143
144
if not end_time :
144
145
# This is not a string, but slicing works also with this input
145
146
end_time = self .time ()[- 1 ]
146
-
147
147
sliced_data = self .data .sel (time = slice (start_time , end_time ), x = x )
148
-
149
148
return sliced_data
150
149
151
- def spec (self , start_time : str = '' , end_time : str = '' , x : List [int ]= None ):
152
- """Slice spectra in space (x) and time. Returns an numpy array."""
153
-
154
- spec = self .slice_data (start_time , end_time , x ).spec .values
155
- return spec
150
+ def spec (self , start_time : str = '' , end_time : str = '' , x : List [int ]= None ) -> np .ndarray :
151
+ """Slice spectra in space (x) and time."""
152
+ spec = self .slice_data (start_time , end_time , x )
153
+ if spec is None :
154
+ return None
155
+ return spec .spec .values
156
156
157
157
def time (self ):
158
+ if not hasattr (self , 'data' ):
159
+ return pd .to_datetime ([])
158
160
return pd .to_datetime (self .data .time .values )
159
161
162
+ def start_time (self ) -> str :
163
+ if len (self .time ()) == 0 :
164
+ return ''
165
+ return self .time ()[0 ].strftime ('%Y-%m-%d %H:%M' )
166
+
167
+ def end_time (self ) -> str :
168
+ if len (self .time ()) == 0 :
169
+ return ''
170
+ return self .time ()[- 1 ].strftime ('%Y-%m-%d %H:%M' )
171
+
160
172
def dt (self ) -> float :
161
173
""" Returns time step of boundary spectra in hours."""
174
+ if len (self .time ()) == 0 :
175
+ return 0
162
176
return self .time ().to_series ().diff ().dt .total_seconds ().values [- 1 ]/ 3600
163
177
164
- def freq (self ):
165
- if hasattr (self , 'data' ):
166
- return copy (self .data .freq .values )
167
- else :
178
+ def freq (self ) -> np .ndarray :
179
+ if not hasattr (self , 'data' ):
168
180
return np .array ([])
181
+ return copy (self .data .freq .values )
169
182
170
- def dirs (self ):
171
- if hasattr (self , 'data' ):
172
- return copy (self .data .dirs .values )
173
- else :
183
+ def dirs (self ) -> np .ndarray :
184
+ if not hasattr (self , 'data' ):
174
185
return np .array ([])
186
+ return copy (self .data .dirs .values )
175
187
176
- def lon (self ):
177
- if hasattr (self , 'data' ):
178
- return copy (self .data .lon .values )
179
- else :
188
+ def lon (self ) -> np .ndarray :
189
+ if not hasattr (self , 'data' ):
180
190
return np .array ([])
191
+ return copy (self .data .lon .values )
181
192
182
- def lat (self ):
183
- if hasattr (self , 'data' ):
184
- return copy (self .data .lat .values )
185
- else :
193
+ def lat (self ) -> np .ndarray :
194
+ if not hasattr (self , 'data' ):
186
195
return np .array ([])
196
+ return copy (self .data .lat .values )
187
197
188
- def x (self ):
189
- if hasattr (self , 'data' ):
190
- return copy (self .data .x .values )
191
- else :
198
+ def x (self ) -> np .ndarray :
199
+ if not hasattr (self , 'data' ):
192
200
return np .array ([])
201
+ return copy (self .data .x .values )
193
202
194
203
def days (self ):
195
204
"""Determins a Pandas data range of all the days in the time span."""
196
- days = day_list (start_time = self .start_time , end_time = self .end_time )
197
- return days
205
+ if len (self .time ()) == 0 :
206
+ return []
207
+ return pd .date_range (start = str (self .time ()[0 ]).split (' ' )[0 ],
208
+ end = str (self .time ()[- 1 ]).split (' ' )[0 ], freq = 'D' )
209
+
210
+ #days = day_list(start_time = self.start_time, end_time = self.end_time)
211
+ #return days
198
212
199
213
def size (self ):
200
214
return (len (self .time ()), len (self .x ()))
@@ -206,33 +220,32 @@ def name(self) -> str:
206
220
207
221
def convention (self ) -> str :
208
222
"""Returns the convention (WW3/Ocean/Met/Math) of the spectra"""
209
- if hasattr (self , '_convention' ):
210
- return copy (self ._convention )
211
- else :
223
+ if not hasattr (self , '_convention' ):
212
224
return None
225
+ return copy (self ._convention )
213
226
214
227
def times_in_day (self , day ):
215
228
"""Determines time stamps of one given day."""
216
229
t0 = day .strftime ('%Y-%m-%d' ) + "T00:00:00"
217
230
t1 = day .strftime ('%Y-%m-%d' ) + "T23:59:59"
218
231
219
- times = self .slice_data (start_time = t0 , end_time = t1 , x = [0 ]).time .values
220
- return times
232
+ times = self .slice_data (start_time = t0 , end_time = t1 , x = [0 ])
233
+ if times is None :
234
+ return []
235
+
236
+ return times .time .values
221
237
222
238
def __str__ (self ) -> str :
223
239
"""Prints status of boundary."""
224
240
225
241
msg .header (self , f"Status of boundary { self .name ()} " )
226
- msg .plain (f"Contains data ({ len (self .x ())} points) for { self .start_time } - { self .end_time } " )
242
+ msg .plain (f"Contains data ({ len (self .x ())} points) for { self .start_time () } - { self .end_time () } " )
227
243
msg .plain (f"Data covers: lon: { min (self .lon ())} - { max (self .lon ())} , lat: { min (self .lat ())} - { max (self .lat ())} " )
228
244
if len (self ._history ) > 0 :
229
245
msg .blank ()
230
246
msg .plain ("Object has the following history:" )
231
247
for obj in self ._history :
232
248
msg .process (f"{ obj .__class__ .__bases__ [0 ].__name__ } : { type (obj ).__name__ } " )
233
- #msg.print_line()
234
- #msg.plain("The Boundary is for the following Grid:")
235
- #print(self.grid)
236
249
237
250
msg .print_line ()
238
251
0 commit comments