Skip to content

Commit 4faaffe

Browse files
Merge branch 'develop' into feature/bounds_NESW_functions
2 parents fa258fa + 12a9ff4 commit 4faaffe

File tree

11 files changed

+121
-36
lines changed

11 files changed

+121
-36
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ jobs:
7474
needs: build-and-test
7575
with:
7676
core_branch: ${{ github.ref }}
77-
petals_branch: feature/exposures_crs
77+
petals_branch: develop
7878
permissions:
7979
checks: write

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Code freeze date: YYYY-MM-DD
4141
- latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`).
4242
- `Exposures.gdf` has been renamed to `Exposures.data` (it still works though, as it is a property now pointing to the latter)
4343
- the `check` method does not add a default "IMPF_" column to the GeoDataFrame anymore
44+
- Updated IBTrACS version from v4.0 to v4.1 ([#976](https://github.com/CLIMADA-project/climada_python/pull/976)
45+
- Fix xarray future warning in TCTracks for .dims to .sizes
46+
- Fix hazard.concatenate type test for pathos pools
4447

4548
### Fixed
4649

climada/engine/impact.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def __init__(
107107
crs=DEF_CRS,
108108
eai_exp=None,
109109
at_event=None,
110-
tot_value=0,
111-
aai_agg=0,
110+
tot_value=0.,
111+
aai_agg=0.,
112112
unit="",
113113
imp_mat=None,
114114
haz_type="",

climada/entity/exposures/base.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,40 @@ def gdf(self):
132132
@property
133133
def latitude(self):
134134
"""Latitude array of exposures"""
135-
return self.data.geometry.y.values
135+
try:
136+
return self.data.geometry.y.values
137+
except ValueError as valerr:
138+
nonpoints = list(
139+
self.data[
140+
self.data.geometry.type != "Point"
141+
].geometry.type.drop_duplicates()
142+
)
143+
if nonpoints:
144+
raise ValueError(
145+
"Can only calculate latitude from Points."
146+
f" GeoDataFrame contains {', '.join(nonpoints)}."
147+
" Please see the lines_polygons module tutorial."
148+
) from valerr
149+
raise
136150

137151
@property
138152
def longitude(self):
139153
"""Longitude array of exposures"""
140-
return self.data.geometry.x.values
154+
try:
155+
return self.data.geometry.x.values
156+
except ValueError as valerr:
157+
nonpoints = list(
158+
self.data[
159+
self.data.geometry.type != "Point"
160+
].geometry.type.drop_duplicates()
161+
)
162+
if nonpoints:
163+
raise ValueError(
164+
"Can only calculate longitude from Points."
165+
f" GeoDataFrame contains {', '.join(nonpoints)}."
166+
" Please see the lines_polygons module tutorial."
167+
) from valerr
168+
raise
141169

142170
@property
143171
def geometry(self):

climada/entity/exposures/test/test_base.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import rasterio
2828
import scipy as sp
2929
from rasterio.windows import Window
30-
from shapely.geometry import Point
30+
from shapely.geometry import MultiPolygon, Point, Polygon
3131
from sklearn.metrics import DistanceMetric
3232

3333
import climada.util.coordinates as u_coord
@@ -652,6 +652,39 @@ def test_to_crs_epsg_crs(self):
652652
Exposures.to_crs(self, crs="GCS", epsg=26915)
653653
self.assertEqual("one of crs or epsg must be None", str(cm.exception))
654654

655+
def test_latlon_with_polygons(self):
656+
"""Check for proper error message if the data frame contains non-Point shapes"""
657+
poly = Polygon(
658+
[(10.0, 0.0), (10.0, 1.0), (11.0, 1.0), (11.0, 0.0), (10.0, 0.0)]
659+
)
660+
point = Point((1, -1))
661+
multi = MultiPolygon(
662+
[
663+
(
664+
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
665+
[((0.1, 1.1), (0.1, 1.2), (0.2, 1.2), (0.2, 1.1))],
666+
)
667+
]
668+
)
669+
poly = Polygon()
670+
exp = Exposures(geometry=[poly, point, multi, poly])
671+
with self.assertRaises(ValueError) as valer:
672+
exp.latitude
673+
self.assertEqual(
674+
"Can only calculate latitude from Points."
675+
" GeoDataFrame contains Polygon, MultiPolygon."
676+
" Please see the lines_polygons module tutorial.",
677+
str(valer.exception),
678+
)
679+
with self.assertRaises(ValueError) as valer:
680+
exp.longitude
681+
self.assertEqual(
682+
"Can only calculate longitude from Points."
683+
" GeoDataFrame contains Polygon, MultiPolygon."
684+
" Please see the lines_polygons module tutorial.",
685+
str(valer.exception),
686+
)
687+
655688

656689
class TestImpactFunctions(unittest.TestCase):
657690
"""Test impact function handling"""

climada/hazard/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,7 @@ def append(self, *others):
936936
"The hazards are incompatible and cannot be concatenated."
937937
)
938938
self.haz_type = haz_types.pop()
939-
940-
haz_classes = {type(haz) for haz in haz_list}
939+
haz_classes = {haz.__class__.__name__ for haz in haz_list}
941940
if len(haz_classes) > 1:
942941
raise TypeError(
943942
f"The given hazards are of different classes: {haz_classes}. "

climada/hazard/tc_tracks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@
8484
IBTRACS_URL = (
8585
"https://www.ncei.noaa.gov/data/"
8686
"international-best-track-archive-for-climate-stewardship-ibtracs/"
87-
"v04r00/access/netcdf"
87+
"v04r01/access/netcdf"
8888
)
8989
"""Site of IBTrACS netcdf file containing all tracks v4.0,
9090
s. https://www.ncdc.noaa.gov/ibtracs/index.php?name=ib-v4-access"""
9191

92-
IBTRACS_FILE = "IBTrACS.ALL.v04r00.nc"
93-
"""IBTrACS v4.0 file all"""
92+
IBTRACS_FILE = "IBTrACS.ALL.v04r01.nc"
93+
"""IBTrACS v4.1 file all"""
9494

9595
IBTRACS_AGENCIES = [
9696
"usa",
@@ -376,7 +376,7 @@ def from_ibtracs_netcdf(
376376
correct_pres=False,
377377
discard_single_points=True,
378378
additional_variables=None,
379-
file_name="IBTrACS.ALL.v04r00.nc",
379+
file_name=IBTRACS_FILE,
380380
):
381381
"""Create new TCTracks object from IBTrACS databse.
382382
@@ -485,7 +485,7 @@ def from_ibtracs_netcdf(
485485
compatiblity with other functions such as `equal_timesteps`. Default: True.
486486
file_name : str, optional
487487
Name of NetCDF file to be dowloaded or located at climada/data/system.
488-
Default: 'IBTrACS.ALL.v04r00.nc'
488+
Default: 'IBTrACS.ALL.v04r01.nc'
489489
additional_variables : list of str, optional
490490
If specified, additional IBTrACS data variables are extracted, such as "nature" or
491491
"storm_speed". Only variables that are not agency-specific are supported.
@@ -731,7 +731,7 @@ def from_ibtracs_netcdf(
731731
)
732732
ibtracs_ds = ibtracs_ds.sel(storm=valid_storms_mask)
733733

734-
if ibtracs_ds.dims["storm"] == 0:
734+
if ibtracs_ds.sizes["storm"] == 0:
735735
LOGGER.info(
736736
"After discarding IBTrACS events without valid values by the selected "
737737
"reporting agencies, there are no tracks left that match the specified "
@@ -2576,7 +2576,7 @@ def ibtracs_fit_param(explained, explanatory, year_range=(1980, 2019), order=1):
25762576
raise KeyError("Unknown ibtracs variable: %s" % var)
25772577

25782578
# load ibtracs dataset
2579-
fn_nc = SYSTEM_DIR.joinpath("IBTrACS.ALL.v04r00.nc")
2579+
fn_nc = SYSTEM_DIR.joinpath(IBTRACS_FILE)
25802580
with xr.open_dataset(fn_nc) as ibtracs_ds:
25812581
# choose specified year range
25822582
years = ibtracs_ds.sid.str.slice(0, 4).astype(int)

climada/hazard/test/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
def download_ibtracs():
31-
"""This makes sure a IBTrACS.ALL.v04r00.nc file is present in SYSTEM_DIR
31+
"""This makes sure a IBTrACS.ALL.v04r01.nc file is present in SYSTEM_DIR
3232
First, downloading from the original sources is attempted. If that fails an old version
3333
is downloaded from the CLIMADA Data API
3434
"""
@@ -44,9 +44,9 @@ def download_ibtracs():
4444
): # plan b: download an old version of that file from the climada api
4545
client = Client()
4646
dsinfo = client.get_dataset_info(
47-
name="IBTrACS", version="v04r00", status="external"
47+
name="IBTrACS", version="v04r01", status="external"
4848
)
4949
[fileinfo] = [
50-
fi for fi in dsinfo.files if fi.file_name == "IBTrACS.ALL.v04r00.nc"
50+
fi for fi in dsinfo.files if fi.file_name == "IBTrACS.ALL.v04r01.nc"
5151
]
5252
client._download_file(local_path=SYSTEM_DIR, fileinfo=fileinfo)

climada/hazard/test/test_tc_tracks.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,35 @@ def test_penv_rmax_penv_pass(self):
8282
provider="usa", storm_id="1992230N11325"
8383
)
8484
penv_ref = np.ones(97) * 1010
85-
penv_ref[26:36] = [1011, 1012, 1013, 1014, 1015, 1014, 1014, 1014, 1014, 1012]
85+
penv_ref[26:36] = [1011, 1012, 1013, 1014, 1015, 1015, 1014, 1014, 1014, 1012]
86+
rmax_ref = np.zeros(97)
87+
rmax_ref[63:82] = [
88+
10.0,
89+
10.0,
90+
10.625,
91+
11.25,
92+
11.875,
93+
12.5,
94+
13.125,
95+
13.75,
96+
14.375,
97+
15.0,
98+
15.625,
99+
16.25,
100+
16.875,
101+
17.5,
102+
18.125,
103+
18.75,
104+
19.375,
105+
20.0,
106+
20.0,
107+
]
86108

87-
self.assertTrue(
88-
np.allclose(tc_track.get_track()["environmental_pressure"].values, penv_ref)
109+
np.testing.assert_array_almost_equal(
110+
tc_track.get_track()["environmental_pressure"].values, penv_ref, decimal=4
89111
)
90-
self.assertTrue(
91-
np.allclose(tc_track.get_track()["radius_max_wind"].values, np.zeros(97))
112+
np.testing.assert_array_almost_equal(
113+
tc_track.get_track()["radius_max_wind"].values, rmax_ref, decimal=4
92114
)
93115

94116
def test_ibtracs_raw_pass(self):
@@ -278,7 +300,7 @@ def test_ibtracs_correct_pass(self):
278300
tc_try.data[0]["central_pressure"].values[0], 1013, places=0
279301
)
280302
self.assertAlmostEqual(
281-
tc_try.data[0]["central_pressure"].values[5], 1008, places=0
303+
tc_try.data[0]["central_pressure"].values[5], 1007, places=0
282304
)
283305
self.assertAlmostEqual(
284306
tc_try.data[0]["central_pressure"].values[-1], 1012, places=0
@@ -701,14 +723,14 @@ def test_get_extent(self):
701723
tc_track = tc.TCTracks.from_ibtracs_netcdf(
702724
storm_id=storms, provider=["usa", "bom"]
703725
)
704-
bounds = (153.585022, -23.200001, 258.714996, 17.514986)
705-
bounds_buf = (153.485022, -23.300001, 258.814996, 17.614986)
706-
np.testing.assert_array_almost_equal(tc_track.bounds, bounds)
726+
bounds = (153.6, -23.2, 258.7, 17.5)
727+
bounds_buf = (153.5, -23.3, 258.8, 17.6)
728+
np.testing.assert_array_almost_equal(tc_track.bounds, bounds, decimal=4)
707729
np.testing.assert_array_almost_equal(
708-
tc_track.get_bounds(deg_buffer=0.1), bounds_buf
730+
tc_track.get_bounds(deg_buffer=0.1), bounds_buf, decimal=4
709731
)
710732
np.testing.assert_array_almost_equal(
711-
tc_track.extent, u_coord.toggle_extent_bounds(bounds)
733+
tc_track.extent, u_coord.toggle_extent_bounds(bounds), decimal=4
712734
)
713735

714736
def test_generate_centroids(self):
@@ -718,13 +740,13 @@ def test_generate_centroids(self):
718740
storm_id=storms, provider=["usa", "bom"]
719741
)
720742
cen = tc_track.generate_centroids(10, 1)
721-
cen_bounds = (157.585022, -19.200001, 257.585022, 10.799999)
743+
cen_bounds = (157.6, -19.2, 257.6, 10.8)
722744
self.assertEqual(cen.size, 44)
723745
self.assertEqual(np.unique(cen.lat).size, 4)
724746
self.assertEqual(np.unique(cen.lon).size, 11)
725747
np.testing.assert_array_equal(np.diff(np.unique(cen.lat)), 10)
726748
np.testing.assert_array_equal(np.diff(np.unique(cen.lon)), 10)
727-
np.testing.assert_array_almost_equal(cen.total_bounds, cen_bounds)
749+
np.testing.assert_array_almost_equal(cen.total_bounds, cen_bounds, decimal=4)
728750

729751
def test_interp_track_pass(self):
730752
"""Interpolate track to min_time_step. Compare to MATLAB reference."""

climada/util/test/test_finance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def test_gdp_sxm_2010_pass(self):
107107
with self.assertLogs("climada.util.finance", level="INFO") as cm:
108108
gdp_year, gdp_val = gdp("SXM", ref_year)
109109

110-
ref_val = 936089385.47486 # reference GDP value
111-
ref_year = 2011 # nearest year with data available (might change)
110+
ref_val = 892290502.793296 # reference GDP value
111+
ref_year = 2010 # nearest year with data available (might change)
112112
# GDP and years with data available might change if worldbank input
113113
# data changes, check magnitude and adjust ref_val and/or ref_year
114114
# if test fails:
@@ -133,7 +133,7 @@ def test_wb_esp_1950_pass(self):
133133
wb_year, wb_val = world_bank("ESP", ref_year, "NY.GDP.MKTP.CD")
134134

135135
ref_year = 1960
136-
ref_val = 12433394725.2159
136+
ref_val = 12424514013.7604
137137
self.assertEqual(wb_year, ref_year)
138138
self.assertAlmostEqual(wb_val, ref_val)
139139

0 commit comments

Comments
 (0)