Skip to content

Commit 7c4f77a

Browse files
committed
rename _calc_space_params and execute it from integration only when necessary
1 parent 4ed0d69 commit 7c4f77a

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

climada/hazard/tc_tracks.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ def retrieve_ibtracs(self, name_ev, date_ev):
115115
"""
116116
raise NotImplementedError
117117

118-
def equal_timestep(self, time_step_h=
119-
CONFIG['trop_cyclone']['time_step_h']):
118+
def equal_timestep(self, time_step_h=CONFIG['trop_cyclone']['time_step_h']):
120119
""" Generate interpolated track values to time steps of min_time_step.
121120
122121
Parameters:
123122
time_step_h (float): time step in hours to which to interpolate
124123
"""
124+
land_param = False
125125
new_list = list()
126126
for track in self.data:
127127
if track.time.size > 3:
@@ -138,15 +138,17 @@ def equal_timestep(self, time_step_h=
138138
track.attrs['category'] = set_category( \
139139
track.max_sustained_wind.values, \
140140
track.max_sustained_wind_unit)
141-
141+
if 'on_land' in track_int or 'dist_since_lf' in track_int:
142+
land_param = True
142143
else:
143144
LOGGER.warning('Track interpolation not done. ' +
144145
'Not enough elements for %s', track.name)
145146
track_int = track
146147
new_list.append(track_int)
147148

148149
self.data = new_list
149-
self._calc_space_params()
150+
if land_param:
151+
self._calc_land_params()
150152

151153
def calc_random_walk(self, ens_size=9, ens_amp0=1.5, max_angle=np.pi/10, \
152154
ens_amp=0.1, seed=CONFIG['trop_cyclone']['random_seed'], decay=True):
@@ -199,7 +201,7 @@ def calc_random_walk(self, ens_size=9, ens_amp0=1.5, max_angle=np.pi/10, \
199201
ens_track.append(i_track)
200202

201203
self.data = ens_track
202-
self._calc_space_params()
204+
self._calc_land_params()
203205
if decay:
204206
v_rel, p_rel = self._calc_land_decay()
205207
self._apply_land_decay(v_rel, p_rel)
@@ -284,7 +286,7 @@ def _calc_land_decay(self, s_rel=True, check_plot=False):
284286
for track in hist_tracks:
285287
_decay_values(s_rel, track, v_lf, p_lf, x_val)
286288
except AttributeError:
287-
LOGGER.error('Execute _calc_space_params() first.')
289+
LOGGER.error('Execute _calc_land_params() first.')
288290
raise ValueError
289291

290292
v_rel, p_rel = _decay_calc_coeff(x_val, v_lf, p_lf)
@@ -320,7 +322,7 @@ def _apply_land_decay(self, v_rel, p_rel, s_rel=True, check_plot=False):
320322
orig_pres.append(np.copy(track.central_pressure.values))
321323
_apply_decay_coeffs(track, v_rel, p_rel, s_rel)
322324
except AttributeError:
323-
LOGGER.error('Execute _calc_space_params() first.')
325+
LOGGER.error('Execute _calc_land_params() first.')
324326
raise ValueError
325327

326328
if check_plot:
@@ -331,7 +333,7 @@ def size(self):
331333
""" Get longitude from coord array """
332334
return len(self.data)
333335

334-
def _calc_space_params(self):
336+
def _calc_land_params(self):
335337
"""Compute tracks attributes dependent on their coordinates:
336338
on_land and dist_since_lf.
337339

climada/hazard/test/test_tc_tracks.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ def test_interp_track_pass(self):
9090
self.assertTrue(np.isnan(tc_track.data[0].basin))
9191
self.assertEqual(tc_track.data[0].id_no, 1951239012334)
9292
self.assertEqual(tc_track.data[0].category, 1)
93-
self.assertTrue(np.all(np.logical_not(tc_track.data[0].on_land.values)))
94-
self.assertTrue(np.all(np.isnan(tc_track.data[0].dist_since_lf.values)))
9593

9694
def test_random_no_landfall_pass(self):
9795
""" Test calc_random_walk with decay and no historical tracks with landfall """
@@ -158,7 +156,7 @@ def test_calc_decay_no_landfall_pass(self):
158156
""" Test _calc_land_decay with no historical tracks with landfall """
159157
tc_track = TCTracks()
160158
tc_track.read_ibtracs_csv(TEST_TRACK_SHORT)
161-
tc_track._calc_space_params()
159+
tc_track._calc_land_params()
162160
with self.assertLogs('climada.hazard.tc_tracks', level='INFO') as cm:
163161
tc_track._calc_land_decay()
164162
self.assertIn('No historical track with landfall.', cm.output[0])
@@ -167,7 +165,7 @@ def test_calc_land_decay_pass(self):
167165
""" Test _calc_land_decay with environmental pressure function."""
168166
tc_track = TCTracks()
169167
tc_track.read_ibtracs_csv(TC_ANDREW_FL)
170-
tc_track._calc_space_params()
168+
tc_track._calc_land_params()
171169
v_rel, p_rel = tc_track._calc_land_decay()
172170

173171
for i, val in enumerate(v_rel.values()):
@@ -281,7 +279,7 @@ def test_apply_decay_no_landfall_pass(self):
281279
""" Test _apply_land_decay with no historical tracks with landfall """
282280
tc_track = TCTracks()
283281
tc_track.read_ibtracs_csv(TEST_TRACK_SHORT)
284-
tc_track._calc_space_params()
282+
tc_track._calc_land_params()
285283
tc_track.data[0]['orig_event_flag']=False
286284
tc_ref = tc_track.data[0].copy()
287285
tc_track._apply_land_decay(dict(), dict())
@@ -312,7 +310,7 @@ def test_apply_decay_pass(self):
312310
tc_track = TCTracks()
313311
tc_track.read_ibtracs_csv(TC_ANDREW_FL)
314312
tc_track.data[0]['orig_event_flag'] = False
315-
tc_track._calc_space_params()
313+
tc_track._calc_land_params()
316314
tc_track._apply_land_decay(v_rel, p_rel, s_rel=True, check_plot=False)
317315

318316
p_ref = np.array([1.010000000000000, 1.009000000000000, 1.008000000000000,

0 commit comments

Comments
 (0)