Skip to content

Commit

Permalink
Flood impact fixes and hazard raster scaling (#73)
Browse files Browse the repository at this point in the history
* Minor flood impact fixes

* Add option to apply scaling factor to hazard raster
  • Loading branch information
c-mckenna authored Oct 20, 2024
1 parent 0c82fce commit ebb37e7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
11 changes: 7 additions & 4 deletions hazimp/jobs/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,11 @@ def __init__(self):

def __call__(self, context, attribute_label, file_list,
clip_exposure2all_hazards=False,
file_format=None, variable=None, no_data_value=None):
file_format=None, variable=None,
no_data_value=None, scaling_factor=None):
"""
Load one or more files and get the value for all the
exposure points. All files have to be of the same attribute.
exposure points. All files have to be of the same attribute and unit.
Alternatively a numeric array of the raster data can be passed in.
:param context: The context instance, used to move data around.
Expand All @@ -645,6 +646,8 @@ def __call__(self, context, attribute_label, file_list,
clippped to the hazard data, so no hazard values are ignored.
:param file_list: A list of files or a single file to be loaded.
:param no_data_value: Values in the raster that represent no data.
:param scaling_factor: An optional scaling factor to apply to
the raster values.
Context return:
exposure_att: Add the file values into this dictionary.
Expand Down Expand Up @@ -673,8 +676,8 @@ def __call__(self, context, attribute_label, file_list,
file_list = misc.mod_file_list(file_list, variable)

file_data, extent = raster_module.files_raster_data_at_points(
context.exposure_long,
context.exposure_lat, file_list)
context.exposure_long, context.exposure_lat,
file_list, scaling_factor)
file_data[file_data == no_data_value] = np.nan

context.exposure_att[attribute_label] = file_data
Expand Down
12 changes: 9 additions & 3 deletions hazimp/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def from_file(cls, filename):

return instance

def raster_data_at_points(self, lon, lat):
def raster_data_at_points(self, lon, lat, scaling_factor=None):
"""
Get data at lat lon points of the raster.
:param lon: A 1D array of the longitude of the points.
:param lat: A 1D array of the latitude of the points.
:param scaling_factor: An optional scaling factor to
apply to the values.
:returns: A numpy array, First dimension being the points/sites.
"""

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

if scaling_factor:
values *= scaling_factor

return values

def extent(self):
Expand All @@ -166,13 +171,14 @@ def extent(self):
return min_long, min_lat, max_long, max_lat


def files_raster_data_at_points(lon, lat, files):
def files_raster_data_at_points(lon, lat, files, scaling_factor=None):
"""
Get data at lat lon points, based on a set of files
:param files: A list of files.
:param lon: A 1D array of the longitude of the points.
:param lat: A 1d array of the latitude of the points.
:param scaling_factor: An optional scaling factor to apply to the values.
:returns: reshaped_data, max_extent
reshaped_data: A numpy array, shape (sites, hazards) or shape (sites),
for one hazard.
Expand All @@ -185,7 +191,7 @@ def files_raster_data_at_points(lon, lat, files):
max_extent = None
for filename in files:
a_raster = Raster.from_file(filename)
results = a_raster.raster_data_at_points(lon, lat)
results = a_raster.raster_data_at_points(lon, lat, scaling_factor)
data.append(results)

# Working out the maximum extent
Expand Down
6 changes: 4 additions & 2 deletions hazimp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from hazimp.config_build import add_job
from hazimp.templates.earthquake import _earthquake_v1_reader
from hazimp.templates.flood import (_flood_fabric_v2_reader,
_flood_contents_v2_reader)
_flood_contents_v2_reader,
_flood_impact_reader)
from hazimp.templates.wind import (_wind_v4_reader,
_wind_v5_reader,
_wind_nc_reader)
Expand Down Expand Up @@ -63,5 +64,6 @@ def _reader2(config: dict) -> list:
EARTHQUAKEV1: _earthquake_v1_reader,
FLOODFABRICV2: _flood_fabric_v2_reader,
FLOODCONTENTSV2: _flood_contents_v2_reader,
SURGENC: _surge_nc_reader
SURGENC: _surge_nc_reader,
FLOODIMPACT: _flood_impact_reader
}
1 change: 1 addition & 0 deletions hazimp/templates/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

FLOODFABRICV2 = 'flood_fabric_v2'
FLOODCONTENTSV2 = 'flood_contents_v2'
FLOODIMPACT = 'flood_impact'

SURGENC = 'surge_nc'

Expand Down
4 changes: 2 additions & 2 deletions hazimp/templates/flood.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def _flood_impact_reader(config: dict) -> list:
add_job(job_insts, LOADCSVEXPOSURE, atts)

atts = find_attributes(config, [HAZARDRASTER])
atts.setdefault('attribute_label', WATER_DEPTH)

atts['attribute_label'] = WATER_DEPTH
add_job(job_insts, LOADRASTER, atts)

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

atts = {'vul_functions_in_exposure': {
vulnerability_set_id:
'SURGE_VULNERABILITY_FUNCTION_ID'}}
'FLOOD_VULNERABILITY_FUNCTION_ID'}}
add_job(job_insts, SIMPLELINKER, atts)

if VULNMETHOD in vuln_atts:
Expand Down
Loading

0 comments on commit ebb37e7

Please sign in to comment.