Skip to content

Commit

Permalink
Filter tracks as they are generated, not once all are done
Browse files Browse the repository at this point in the history
  • Loading branch information
wcarthur committed Nov 3, 2016
1 parent c558c03 commit b5273d6
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions TrackGenerator/TrackGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,45 @@ def generateTracks(self, nTracks, simId, initLon=None, initLat=None,
:rtype :class:`numpy.array`
:return: the tracks generated.
"""
# Define some filter functions

def empty(track):
"""
:return: True if the track is empty. False, otherwise.
"""
return len(track.Longitude) == 0

def diedEarly(track, minAge=12):
"""
:return: True if the track dies before `minAge`. False,
otherwise.
"""
return track.TimeElapsed[-1] < minAge

def insideDomain(track):
"""
:return: True if the track stays inside the domain. False,
otherwise.
"""
inside = [track.Longitude[k] > self.innerGridLimit['xMin'] and
track.Longitude[k] < self.innerGridLimit['xMax'] and
track.Latitude[k] > self.innerGridLimit['yMin'] and
track.Latitude[k] < self.innerGridLimit['yMax']
for k in range(len(track.Longitude))]
return all(inside)

def validPressures(track):
"""
:return: True if a valid pressure. False, otherwise.
"""
return all(np.round(track.CentralPressure, 2) < np.round(track.EnvPressure, 2))

log.debug('Generating %d tropical cyclone tracks', nTracks)
genesisYear = int(uniform(1900, 9998))
results = []
for j in range(1, nTracks + 1):
j = 0
while j < nTracks:
#for j in range(1, nTracks + 1):

if not (initLon and initLat):
log.debug('Cyclone origin not given, sampling a' +
Expand Down Expand Up @@ -614,43 +648,21 @@ def generateTracks(self, nTracks, simId, initLon=None, initLat=None,
data = np.core.records.fromarrays(data, dtype=track_dtype)
track = Track(data)
track.trackId = (j, simId)
log.debug("Completed track {0:03d}-{1:04d}".format(*track.trackId))

results.append(track)

# Define some filter functions

def empty(track):
"""
:return: True if the track is empty. False, otherwise.
"""
return len(track.Longitude) == 0

def diedEarly(track, minAge=12):
"""
:return: True if the track dies before `minAge`. False,
otherwise.
"""
return track.TimeElapsed[-1] < minAge

def insideDomain(track):
"""
:return: True if the track stays inside the domain. False,
otherwise.
"""
inside = [track.Longitude[k] > self.innerGridLimit['xMin'] and
track.Longitude[k] < self.innerGridLimit['xMax'] and
track.Latitude[k] > self.innerGridLimit['yMin'] and
track.Latitude[k] < self.innerGridLimit['yMax']
for k in range(len(track.Longitude))]
return all(inside)

def validPressures(track):
"""
:return: True if a valid pressure. False, otherwise.
"""
return all(np.round(track.CentralPressure, 2) < np.round(track.EnvPressure, 2))


if not (empty(track) or diedEarly(track)) \
and validPressures(track):
if self.innerGridLimit and not insideDomain(track):
log.debug("Track exits inner grid limit - rejecting")
continue
else:
results.append(track)
log.debug("Completed track {0:03d}-{1:04d}".\
format(*track.trackId))
j += 1
else:
log.debug("Eliminated invalid track")

"""
# Filter the generated tracks based on certain criteria
nbefore = len(results)
results = [track for track in results if not empty(track)]
Expand All @@ -671,7 +683,7 @@ def validPressures(track):
results = [track for track in results if insideDomain(track)]
log.debug('Removed %i tracks that do not pass inside' +
' domain.', nbefore - len(results))

"""
# Return the tracks:

return results
Expand Down

0 comments on commit b5273d6

Please sign in to comment.