Skip to content

Commit

Permalink
Update read raster intensity / fraction data
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahan Kropf committed Sep 28, 2023
1 parent 5aeb35e commit fb374d3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
13 changes: 5 additions & 8 deletions climada/entity/exposures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def from_raster(cls, file_name, band=1, src_crs=None, window=None,
--------
Exposures
"""
exp = cls()

meta, value = u_coord.read_raster(file_name, [band], src_crs, window,
geometry, dst_crs, transform, width,
height, resampling)
Expand All @@ -507,14 +507,11 @@ def from_raster(cls, file_name, band=1, src_crs=None, window=None,
lry = uly + meta['height'] * yres
x_grid, y_grid = np.meshgrid(np.arange(ulx + xres / 2, lrx, xres),
np.arange(uly + yres / 2, lry, yres))
return cls(
{'longitude': x_grid.flatten(), 'latitude': y_grid.flatten(), 'value': value.reshape(-1)}

Check warning on line 511 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (101/100)
Raw output
Used when a line is longer than a given number of characters.
, meta=meta, crs= meta['crs']
)

if exp.crs is None:
exp.set_crs()
exp.gdf['longitude'] = x_grid.flatten()
exp.gdf['latitude'] = y_grid.flatten()
exp.gdf['value'] = value.reshape(-1)
exp.meta = meta
return exp

def plot_scatter(self, mask=None, ignore_zero=False, pop_name=True,

Check warning on line 516 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-arguments

LOW: Too many arguments (10/7)
Raw output
Used when a function or method takes too many arguments.

Check warning on line 516 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-locals

LOW: Too many local variables (16/15)
Raw output
Used when a function or method has too many local variables.
buffer=0.0, extend='neither', axis=None, figsize=(9, 13),
Expand Down
83 changes: 73 additions & 10 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,15 @@ def from_raster(cls, files_intensity, files_fraction=None, attrs=None,
if haz_type is not None:
hazard_kwargs["haz_type"] = haz_type

centroids = Centroids.from_raster_file(
centroids, meta = Centroids.from_raster_file(
files_intensity[0], src_crs=src_crs, window=window, geometry=geometry, dst_crs=dst_crs,
transform=transform, width=width, height=height, resampling=resampling)
transform=transform, width=width, height=height, resampling=resampling, return_meta=True)

Check warning on line 345 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (101/100)
Raw output
Used when a line is longer than a given number of characters.

if pool:
chunksize = max(min(len(files_intensity) // pool.ncpus, 1000), 1)
inten_list = pool.map(
centroids.values_from_raster_files,
[[f] for f in files_intensity],
_values_from_raster_files,
[[f] for f in files_intensity], itertools.repeat(meta),
itertools.repeat(band), itertools.repeat(src_crs),
itertools.repeat(window), itertools.repeat(geometry),
itertools.repeat(dst_crs), itertools.repeat(transform),
Expand All @@ -356,22 +357,22 @@ def from_raster(cls, files_intensity, files_fraction=None, attrs=None,
intensity = sparse.vstack(inten_list, format='csr')
if files_fraction is not None:
fract_list = pool.map(
centroids.values_from_raster_files,
[[f] for f in files_fraction],
_values_from_raster_files,
[[f] for f in files_fraction], itertools.repeat(meta),
itertools.repeat(band), itertools.repeat(src_crs),
itertools.repeat(window), itertools.repeat(geometry),
itertools.repeat(dst_crs), itertools.repeat(transform),
itertools.repeat(width), itertools.repeat(height),
itertools.repeat(resampling), chunksize=chunksize)
fraction = sparse.vstack(fract_list, format='csr')
else:
intensity = centroids.values_from_raster_files(
files_intensity, band=band, src_crs=src_crs, window=window, geometry=geometry,
intensity = _values_from_raster_files(
files_intensity, meta=meta, band=band, src_crs=src_crs, window=window, geometry=geometry,

Check warning on line 370 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (105/100)
Raw output
Used when a line is longer than a given number of characters.
dst_crs=dst_crs, transform=transform, width=width, height=height,
resampling=resampling)
if files_fraction is not None:
fraction = centroids.values_from_raster_files(
files_fraction, band=band, src_crs=src_crs, window=window, geometry=geometry,
fraction = _values_from_raster_files(
files_fraction, meta=meta, band=band, src_crs=src_crs, window=window, geometry=geometry,

Check warning on line 375 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (108/100)
Raw output
Used when a line is longer than a given number of characters.
dst_crs=dst_crs, transform=transform, width=width, height=height,
resampling=resampling)

Expand Down Expand Up @@ -2491,3 +2492,65 @@ def _get_fraction(self, cent_idx=None):
if cent_idx is None:
return self.fraction
return self.fraction[:, cent_idx]


#Function to read intensity/fraction values from raster files
#No a method to allow for parallel computing
def _values_from_raster_files(file_names, meta, band=None, src_crs=None, window=None,

Check warning on line 2499 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-arguments

LOW: Too many arguments (11/7)
Raw output
Used when a function or method takes too many arguments.
geometry=None, dst_crs=None, transform=None, width=None,
height=None, resampling=Resampling.nearest):
"""Read raster of bands and set 0 values to the masked ones.

Check warning on line 2502 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 8 spaces, expected 4
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
Each band is an event. Select region using window or geometry. Reproject input by proving
dst_crs and/or (transform, width, height).
Parameters
----------
file_names : str
path of the file
meta : dict
description of the centroids raster
band : list(int), optional
band number to read. Default: [1]
src_crs : crs, optional
source CRS. Provide it if error without it.
window : rasterio.windows.Window, optional
window to read
geometry : list of shapely.geometry, optional
consider pixels only within these shapes
dst_crs : crs, optional
reproject to given crs
transform : rasterio.Affine
affine transformation to apply
wdith : float
number of lons for transform
height : float
number of lats for transform
resampling : rasterio.warp,.Resampling optional
resampling function used for reprojection to dst_crs
Raises
------
ValueError
Returns
-------
inten : scipy.sparse.csr_matrix
Each row is an event.
"""
if band is None:

Check warning on line 2541 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 8 spaces, expected 4
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
band = [1]

Check warning on line 2542 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 12 spaces, expected 8
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.

values = []

Check warning on line 2544 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 8 spaces, expected 4
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
for file_name in file_names:

Check warning on line 2545 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 8 spaces, expected 4
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
tmp_meta, data = u_coord.read_raster(

Check warning on line 2546 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 12 spaces, expected 8
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
file_name, band, src_crs, window, geometry, dst_crs,
transform, width, height, resampling)
if (tmp_meta['crs'] != meta['crs']

Check warning on line 2549 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 12 spaces, expected 8
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
or tmp_meta['transform'] != meta['transform']
or tmp_meta['height'] != meta['height']
or tmp_meta['width'] != meta['width']):
raise ValueError('Raster data is inconsistent with contained raster.')

Check warning on line 2553 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 16 spaces, expected 12
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
values.append(sparse.csr_matrix(data))

Check warning on line 2554 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 12 spaces, expected 8
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.

return sparse.vstack(values, format='csr')

Check warning on line 2556 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

bad-indentation

NORMAL: Bad indentation. Found 8 spaces, expected 4
Raw output
Used when an unexpected number of indentation's tabulations or spaces has beenfound.
8 changes: 4 additions & 4 deletions climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,10 @@ def get_area_pixel(self, min_resol=1.0e-8):
I/O methods
'''


@classmethod
def from_raster_file(cls, file_name, src_crs=None, window=None,

Check warning on line 516 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-arguments

LOW: Too many arguments (11/7)
Raw output
Used when a function or method takes too many arguments.
geometry=None, dst_crs=None, transform=None, width=None,
height=None, resampling=Resampling.nearest):
height=None, resampling=Resampling.nearest, return_meta=False):
"""Create a new Centroids object from a raster file
Select region using window or geometry. Reproject input by providing
Expand Down Expand Up @@ -552,7 +551,9 @@ def from_raster_file(cls, file_name, src_crs=None, window=None,
file_name, [1], src_crs, window, geometry, dst_crs,
transform, width, height, resampling)
lat, lon = _meta_to_lat_lon(meta)
return cls(longitude=lon, latitude=lat, crs=dst_crs)
if return_meta:
return cls(longitude=lon, latitude=lat, crs=meta['crs']), meta
return cls(longitude=lon, latitude=lat, crs=meta['crs'])

@classmethod
def from_meta(cls, meta):
Expand All @@ -572,7 +573,6 @@ def from_meta(cls, meta):
lat, lon = _meta_to_lat_lon(meta)
return cls(longitude=lon, latitude=lat, crs=crs)


@classmethod
def from_vector_file(cls, file_name, dst_crs=None):
"""Create Centroids object from vector file (any format supported by fiona).
Expand Down

0 comments on commit fb374d3

Please sign in to comment.