Skip to content

Commit

Permalink
Feature/init tc tracks (#559)
Browse files Browse the repository at this point in the history
* update __init__ in tc_tracks.py

* update tc_tracks.py

* update tc_tracks.py

* remove trailing white space

* Update usage of TCTracks.__init__

* Update TCTracks.__init__ docstring

Add information on `data` parameter.

Co-authored-by: emanuel-schmid <schmide@ethz.ch>
Co-authored-by: Lukas Riedel <34276446+peanutfun@users.noreply.github.com>
  • Loading branch information
3 people authored and Simona Meiler committed Nov 22, 2022
1 parent 1188b66 commit c920e23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
59 changes: 24 additions & 35 deletions climada/hazard/tc_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import datetime as dt
import itertools
import logging
from typing import Optional, List
import pathlib
import re
import shutil
Expand All @@ -36,6 +37,7 @@
import cartopy.crs as ccrs
import cftime
import geopandas as gpd
import pathos
import matplotlib.cm as cm_mp
from matplotlib.collections import LineCollection
from matplotlib.colors import BoundaryNorm, ListedColormap
Expand Down Expand Up @@ -188,20 +190,24 @@ class TCTracks():
- on_land (bool for each track position)
- dist_since_lf (in km)
"""
def __init__(self, pool=None):
def __init__(self,
data: Optional[List[xr.Dataset]] = None,
pool: Optional[pathos.multiprocessing.ProcessPool] = None):
"""Create new (empty) TCTracks instance.
Parameters
----------
pool : pathos.pool, optional
data : list of xarray.Dataset, optional
List of tropical cyclone tracks, each stored as single xarray Dataset.
See the Attributes for a full description of the required Dataset variables
and attributes. Defaults to an empty list.
pool : pathos.pools, optional
Pool that will be used for parallel computation when applicable. Default: None
"""
self.data = list()
self.data = data if data is not None else list()
self.pool = pool
if pool:
self.pool = pool
LOGGER.debug('Using %s CPUs.', self.pool.ncpus)
else:
self.pool = None

def append(self, tracks):
"""Append tracks to current.
Expand Down Expand Up @@ -308,8 +314,7 @@ def tracks_in_exp(self, exposure, buffer=1.0):
tc_tracks_lines = self.to_geodataframe().buffer(distance=buffer)
select_tracks = tc_tracks_lines.intersects(exp_buffer)
tracks_in_exp = [track for j, track in enumerate(self.data) if select_tracks[j]]
filtered_tracks = TCTracks()
filtered_tracks.append(tracks_in_exp)
filtered_tracks = TCTracks(tracks_in_exp)

return filtered_tracks

Expand Down Expand Up @@ -705,9 +710,7 @@ def from_ibtracs_netcdf(cls, provider=None, rescale_windspeeds=True, storm_id=No
# If all tracks have been discarded in the loop due to the basin filters:
LOGGER.info('There were no tracks left in the specified basin '
'after discarding invalid track positions.')
tr = cls()
tr.data = all_tracks
return tr
return cls(all_tracks)

def read_processed_ibtracs_csv(self, *args, **kwargs):
"""This function is deprecated, use TCTracks.from_processed_ibtracs_csv instead."""
Expand All @@ -729,9 +732,7 @@ def from_processed_ibtracs_csv(cls, file_names):
tracks : TCTracks
TCTracks with data from the processed ibtracs CSV file.
"""
tr = cls()
tr.data = [_read_ibtracs_csv_single(f) for f in get_file_names(file_names)]
return tr
return cls([_read_ibtracs_csv_single(f) for f in get_file_names(file_names)])

def read_simulations_emanuel(self, *args, **kwargs):
"""This function is deprecated, use TCTracks.from_simulations_emanuel instead."""
Expand All @@ -756,13 +757,11 @@ def from_simulations_emanuel(cls, file_names, hemisphere=None):
tracks : TCTracks
TCTracks with data from Kerry Emanuel's simulations.
"""
tr = cls()
tr.data = []
data = []
for path in get_file_names(file_names):
tr.data.extend(_read_file_emanuel(
path, hemisphere=hemisphere,
rmw_corr=Path(path).name in EMANUEL_RMW_CORR_FILES))
return tr
data.extend(_read_file_emanuel(path, hemisphere=hemisphere,
rmw_corr=Path(path).name in EMANUEL_RMW_CORR_FILES))
return cls(data)

def read_one_gettelman(self, nc_data, i_track):
"""This function is deprecated, use TCTracks.from_gettelman instead."""
Expand All @@ -786,9 +785,7 @@ def from_gettelman(cls, path):
"""
nc_data = nc.Dataset(path)
nstorms = nc_data.dimensions['storm'].size
tr = cls()
tr.data = [_read_one_gettelman(nc_data, i) for i in range(nstorms)]
return tr
return cls([_read_one_gettelman(nc_data, i) for i in range(nstorms)])

def read_simulations_chaz(self, *args, **kwargs):
"""This function is deprecated, use TCTracks.from_simulations_chaz instead."""
Expand Down Expand Up @@ -916,9 +913,7 @@ def from_simulations_chaz(cls, file_names, year_range=None, ensemble_nums=None):
}))
if last_perc != 100:
LOGGER.info("Progress: 100%")
tr = cls()
tr.data = data
return tr
return cls(data)

def read_simulations_storm(self, *args, **kwargs):
"""This function is deprecated, use TCTracks.from_simulations_storm instead."""
Expand Down Expand Up @@ -1042,9 +1037,7 @@ def from_simulations_storm(cls, path, years=None):
}))
if last_perc != 100:
LOGGER.info("Progress: 100%")
tr = cls()
tr.data = data
return tr
return cls(data)

def equal_timestep(self, time_step_h=1, land_params=False, pool=None):
"""Generate interpolated track values to time steps of time_step_h.
Expand Down Expand Up @@ -1292,9 +1285,7 @@ def from_netcdf(cls, folder_name):
del track.attrs['basin']
track['basin'] = ("time", np.full(track.time.size, basin, dtype="<U2"))
data.append(track)
tr = cls()
tr.data = data
return tr
return cls(data)

def write_hdf5(self, file_name, complevel=5):
"""Write TC tracks in NetCDF4-compliant HDF5 format.
Expand Down Expand Up @@ -1346,9 +1337,7 @@ def from_hdf5(cls, file_name):
# when writing '<U2' and reading in again, xarray reads as dtype 'object'. undo this:
track['basin'] = track['basin'].astype('<U2')
data.append(track)
tracks = cls()
tracks.data = data
return tracks
return cls(data)

def to_geodataframe(self, as_points=False, split_lines_antimeridian=True):
"""Transform this TCTracks instance into a GeoDataFrame.
Expand Down
3 changes: 1 addition & 2 deletions climada/hazard/test/test_tc_tracks_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ def test_wrong_decay_pass(self):
track_gen.attrs['orig_event_flag'] = False

cp_ref = np.array([1012., 1012.])
single_track = tc.TCTracks()
single_track.data = [track_gen]
single_track = tc.TCTracks([track_gen])
extent = single_track.get_extent()
land_geom = climada.util.coordinates.get_land_geometry(
extent=extent, resolution=10
Expand Down

0 comments on commit c920e23

Please sign in to comment.