@@ -2985,6 +2985,7 @@ def _zlib_from_dataarray(data_var: xr.DataArray) -> bool:
29852985def compute_track_density (
29862986 tc_track : TCTracks ,
29872987 res : int = 5 ,
2988+ bounds : tuple = None ,
29882989 genesis : bool = False ,
29892990 norm : str = None ,
29902991 filter_tracks : bool = True ,
@@ -3005,19 +3006,23 @@ def compute_track_density(
30053006 track object containing a list of all tracks
30063007 res: int (optional), default: 5°
30073008 resolution in degrees of the grid bins in which the density will be computed
3009+ bounds: tuple, dafault: None
3010+ (lat_min,lat_max,lon_min,lon_max) latitude and longitude bounds to compute track density.
30083011 genesis: bool, Default = False
30093012 If true the function computes the track density of only the genesis location of tracks
3010- norm: bool (optional), default = False
3011- If False it returns the number of samples in each bin. If True, returns the
3012- specified normalization function at each bin computed as count_bin / grid_area.
3013+ norm: str (optional), default = None
3014+ If None the function returns the number of samples in each bin. If True, it normalize the
3015+ bin count as specified: if norm = area -> normalize by gird cell area. If norm = sum ->
3016+ normalize by the total sum of each bin.
30133017 filter_tracks: bool (optional) default: True
30143018 If True the track density is computed as the number of different tracks crossing a grid
30153019 cell. If False, the track density takes into account how long the track stayed in each
30163020 grid cell. Hence slower tracks increase the density if the parameter is set to False.
30173021 wind_min: float (optional), default: None
3018- Minimum wind speed above which to select tracks.
3022+ Minimum wind speed above which to select tracks (inclusive) .
30193023 wind_max: float (optional), default: None
3020- Maximal wind speed below which to select tracks.
3024+ Maximal wind speed below which to select tracks (exclusive if wind_min is also provided,
3025+ otherwise inclusive).
30213026 Returns:
30223027 -------
30233028 hist_count: np.ndarray
@@ -3048,8 +3053,14 @@ def compute_track_density(
30483053 )
30493054
30503055 # define grid resolution and bounds for density computation
3051- lat_bins : np .ndarray = np .linspace (- 90 , 90 , int (180 / res ))
3052- lon_bins : np .ndarray = np .linspace (- 180 , 180 , int (360 / res ))
3056+ if not bounds :
3057+ lat_min , lat_max , lon_min , lon_max = - 90 , 90 , - 180 , 180
3058+ else :
3059+ lat_min , lat_max , lon_min , lon_max = bounds [0 ], bounds [1 ], bounds [2 ], bounds [3 ]
3060+
3061+ lat_bins : np .ndarray = np .linspace (lat_min , lat_max , int (180 / res ))
3062+ lon_bins : np .ndarray = np .linspace (lon_min , lon_max , int (360 / res ))
3063+
30533064 # compute 2D density
30543065 if genesis :
30553066 hist_count = compute_genesis_density (
@@ -3062,7 +3073,7 @@ def compute_track_density(
30623073 # select according to wind speed
30633074 wind_speed = track .max_sustained_wind .values
30643075 if wind_min and wind_max :
3065- index = np .where ((wind_speed >= wind_min ) & (wind_speed <= wind_max ))[0 ]
3076+ index = np .where ((wind_speed >= wind_min ) & (wind_speed < wind_max ))[0 ]
30663077 elif wind_min and not wind_max :
30673078 index = np .where (wind_speed >= wind_min )[0 ]
30683079 elif wind_max and not wind_min :
@@ -3147,9 +3158,14 @@ def normalize_hist(
31473158 """
31483159
31493160 if norm == "area" :
3150- grid_area , _ = u_coord .compute_grid_cell_area (res = res )
3161+ grid_area = u_coord .compute_grid_cell_area (res = res )
31513162 norm_hist : np .ndarray = hist_count / grid_area
31523163 elif norm == "sum" :
31533164 norm_hist : np .ndarray = hist_count / hist_count .sum ()
3165+ else :
3166+ raise ValueError (
3167+ "Invalid value for input parameter 'norm':\n "
3168+ "it should be either 'area' or 'sum'"
3169+ )
31543170
31553171 return norm_hist
0 commit comments