Skip to content

Commit ebb37e7

Browse files
authored
Flood impact fixes and hazard raster scaling (#73)
* Minor flood impact fixes * Add option to apply scaling factor to hazard raster
1 parent 0c82fce commit ebb37e7

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

hazimp/jobs/jobs.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,11 @@ def __init__(self):
633633

634634
def __call__(self, context, attribute_label, file_list,
635635
clip_exposure2all_hazards=False,
636-
file_format=None, variable=None, no_data_value=None):
636+
file_format=None, variable=None,
637+
no_data_value=None, scaling_factor=None):
637638
"""
638639
Load one or more files and get the value for all the
639-
exposure points. All files have to be of the same attribute.
640+
exposure points. All files have to be of the same attribute and unit.
640641
Alternatively a numeric array of the raster data can be passed in.
641642
642643
:param context: The context instance, used to move data around.
@@ -645,6 +646,8 @@ def __call__(self, context, attribute_label, file_list,
645646
clippped to the hazard data, so no hazard values are ignored.
646647
:param file_list: A list of files or a single file to be loaded.
647648
:param no_data_value: Values in the raster that represent no data.
649+
:param scaling_factor: An optional scaling factor to apply to
650+
the raster values.
648651
649652
Context return:
650653
exposure_att: Add the file values into this dictionary.
@@ -673,8 +676,8 @@ def __call__(self, context, attribute_label, file_list,
673676
file_list = misc.mod_file_list(file_list, variable)
674677

675678
file_data, extent = raster_module.files_raster_data_at_points(
676-
context.exposure_long,
677-
context.exposure_lat, file_list)
679+
context.exposure_long, context.exposure_lat,
680+
file_list, scaling_factor)
678681
file_data[file_data == no_data_value] = np.nan
679682

680683
context.exposure_att[attribute_label] = file_data

hazimp/raster.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ def from_file(cls, filename):
9898

9999
return instance
100100

101-
def raster_data_at_points(self, lon, lat):
101+
def raster_data_at_points(self, lon, lat, scaling_factor=None):
102102
"""
103103
Get data at lat lon points of the raster.
104104
105105
:param lon: A 1D array of the longitude of the points.
106106
:param lat: A 1D array of the latitude of the points.
107+
:param scaling_factor: An optional scaling factor to
108+
apply to the values.
107109
:returns: A numpy array, First dimension being the points/sites.
108110
"""
109111

@@ -150,6 +152,9 @@ def read_cell(i, x, y):
150152
values = numpy.where(values == self.no_data_value, numpy.nan,
151153
values)
152154

155+
if scaling_factor:
156+
values *= scaling_factor
157+
153158
return values
154159

155160
def extent(self):
@@ -166,13 +171,14 @@ def extent(self):
166171
return min_long, min_lat, max_long, max_lat
167172

168173

169-
def files_raster_data_at_points(lon, lat, files):
174+
def files_raster_data_at_points(lon, lat, files, scaling_factor=None):
170175
"""
171176
Get data at lat lon points, based on a set of files
172177
173178
:param files: A list of files.
174179
:param lon: A 1D array of the longitude of the points.
175180
:param lat: A 1d array of the latitude of the points.
181+
:param scaling_factor: An optional scaling factor to apply to the values.
176182
:returns: reshaped_data, max_extent
177183
reshaped_data: A numpy array, shape (sites, hazards) or shape (sites),
178184
for one hazard.
@@ -185,7 +191,7 @@ def files_raster_data_at_points(lon, lat, files):
185191
max_extent = None
186192
for filename in files:
187193
a_raster = Raster.from_file(filename)
188-
results = a_raster.raster_data_at_points(lon, lat)
194+
results = a_raster.raster_data_at_points(lon, lat, scaling_factor)
189195
data.append(results)
190196

191197
# Working out the maximum extent

hazimp/templates/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from hazimp.config_build import add_job
2525
from hazimp.templates.earthquake import _earthquake_v1_reader
2626
from hazimp.templates.flood import (_flood_fabric_v2_reader,
27-
_flood_contents_v2_reader)
27+
_flood_contents_v2_reader,
28+
_flood_impact_reader)
2829
from hazimp.templates.wind import (_wind_v4_reader,
2930
_wind_v5_reader,
3031
_wind_nc_reader)
@@ -63,5 +64,6 @@ def _reader2(config: dict) -> list:
6364
EARTHQUAKEV1: _earthquake_v1_reader,
6465
FLOODFABRICV2: _flood_fabric_v2_reader,
6566
FLOODCONTENTSV2: _flood_contents_v2_reader,
66-
SURGENC: _surge_nc_reader
67+
SURGENC: _surge_nc_reader,
68+
FLOODIMPACT: _flood_impact_reader
6769
}

hazimp/templates/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
FLOODFABRICV2 = 'flood_fabric_v2'
1010
FLOODCONTENTSV2 = 'flood_contents_v2'
11+
FLOODIMPACT = 'flood_impact'
1112

1213
SURGENC = 'surge_nc'
1314

hazimp/templates/flood.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def _flood_impact_reader(config: dict) -> list:
6464
add_job(job_insts, LOADCSVEXPOSURE, atts)
6565

6666
atts = find_attributes(config, [HAZARDRASTER])
67+
atts.setdefault('attribute_label', WATER_DEPTH)
6768

68-
atts['attribute_label'] = WATER_DEPTH
6969
add_job(job_insts, LOADRASTER, atts)
7070

7171
vuln_atts = find_attributes(config, VULNFILE)
@@ -79,7 +79,7 @@ def _flood_impact_reader(config: dict) -> list:
7979

8080
atts = {'vul_functions_in_exposure': {
8181
vulnerability_set_id:
82-
'SURGE_VULNERABILITY_FUNCTION_ID'}}
82+
'FLOOD_VULNERABILITY_FUNCTION_ID'}}
8383
add_job(job_insts, SIMPLELINKER, atts)
8484

8585
if VULNMETHOD in vuln_atts:

0 commit comments

Comments
 (0)