climada.util package¶
climada.util.checker module¶
-
climada.util.checker.
size
(exp_len, var, var_name)[source]¶ Check if the length of a variable is the expected one.
- Raises
ValueError –
-
climada.util.checker.
shape
(exp_row, exp_col, var, var_name)[source]¶ Check if the length of a variable is the expected one.
- Raises
ValueError –
-
climada.util.checker.
array_optional
(exp_len, var, var_name)[source]¶ Check if array has right size. Warn if array empty. Call check_size.
- Parameters
exp_len (str) – expected array size
var (np.array) – numpy array to check
var_name (str) – name of the variable. Used in error/warning msg
- Raises
ValueError –
-
climada.util.checker.
array_default
(exp_len, var, var_name, def_val)[source]¶ Check array has right size. Set default value if empty. Call check_size.
- Parameters
exp_len (str) – expected array size
var (np.array) – numpy array to check
var_name (str) – name of the variable. Used in error/warning msg
def_val (np.array) – nump array used as default value
- Raises
ValueError –
- Returns
Filled array
climada.util.config module¶
-
climada.util.config.
CONFIG
= {'global': {'log_level': 'DEBUG', 'max_matrix_size': 1000000000.0}, 'local_data': {'save_dir': '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/doc/results'}, 'trop_cyclone': {'random_seed': 54}}¶
climada.util.constants module¶
-
climada.util.constants.
SOURCE_DIR
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/climada'¶ climada directory
-
climada.util.constants.
DATA_DIR
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data'¶ Folder containing the data
-
climada.util.constants.
SYSTEM_DIR
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/system'¶ Folder containing the data used internally
-
climada.util.constants.
GLB_CENTROIDS_NC
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/system/NatID_grid_0150as.nc'¶ Global centroids nc.
-
climada.util.constants.
GLB_CENTROIDS_MAT
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/system/GLB_NatID_grid_0360as_adv_2.mat'¶ Global centroids.
-
climada.util.constants.
ENT_TEMPLATE_XLS
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/system/entity_template.xlsx'¶ Entity template in xls format.
-
climada.util.constants.
NAT_REG_ID
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/system/NatRegIDs.csv'¶ Look-up table ISO3 codes
-
climada.util.constants.
HAZ_DEMO_FLDDPH
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/flddph_WaterGAP2_miroc5_historical_flopros_gev_picontrol_2000_0.1.nc'¶ NetCDF4 Flood depth from isimip simulations
-
climada.util.constants.
HAZ_DEMO_FLDFRC
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/fldfrc_WaterGAP2_miroc5_historical_flopros_gev_picontrol_2000_0.1.nc'¶ NetCDF4 Flood fraction from isimip simulations
-
climada.util.constants.
HAZ_DEMO_MAT
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/atl_prob.mat'¶ hurricanes from 1851 to 2011 over Florida with 100 centroids.
- Type
Hazard demo from climada in MATLAB
-
climada.util.constants.
HAZ_DEMO_H5
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/tc_fl_1975_2011.h5'¶ ibtracs from 1975 to 2011 over Florida with 2500 centroids.
- Type
Hazard demo in h5 format
-
climada.util.constants.
DEMO_GDP2ASSET
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/gdp2asset_demo_exposure.nc'¶ Exposure demo file for GDP2Asset
-
climada.util.constants.
WS_DEMO_NC
= ['/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/fp_lothar_crop-test.nc', '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/fp_xynthia_crop-test.nc']¶ Winter storm in Europe files. These test files have been generated using the netCDF kitchen sink: ncks -d latitude,50.5,54.0 -d longitude,3.0,7.5 ./file_in.nc ./file_out.nc
-
climada.util.constants.
EXP_DEMO_H5
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/exp_demo_today.h5'¶ Exposures over Florida
-
climada.util.constants.
TC_ANDREW_FL
= '/home/docs/checkouts/readthedocs.org/user_builds/climada-python/checkouts/v1.2.5/data/demo/ibtracs_global_intp-None_1992230N11325.csv'¶ Tropical cyclone Andrew in Florida
-
climada.util.constants.
ONE_LAT_KM
= 111.12¶ Mean one latitude (in degrees) to km
-
climada.util.constants.
EARTH_RADIUS_KM
= 6371¶ Earth radius in km
climada.util.coordinates module¶
-
climada.util.coordinates.
LOGGER
= <Logger climada.util.coordinates (DEBUG)>¶
-
climada.util.coordinates.
NE_EPSG
= 4326¶ Natural Earth CRS EPSG
-
climada.util.coordinates.
NE_CRS
= {'init': 'epsg:4326', 'no_defs': True}¶ Natural Earth CRS
-
climada.util.coordinates.
grid_is_regular
(coord)[source]¶ Return True if grid is regular.
- Parameters
coord (np.array)
-
climada.util.coordinates.
get_coastlines
(extent=None, resolution=110)[source]¶ Get latitudes and longitudes of the coast lines inside extent. All earth if no extent.
- Parameters
extent (tuple, optional) – (min_lon, max_lon, min_lat, max_lat)
resolution (float, optional) – 10, 50 or 110. Resolution in m. Default: 110m, i.e. 1:110.000.000
- Returns
np.array (lat, lon coastlines)
-
climada.util.coordinates.
dist_to_coast
(coord_lat, lon=None)[source]¶ Comput distance to coast from input points in meters.
- Parameters
coord_lat (np.array or tuple or float) –
- np.array with two columns, first for latitude of each point and
second with longitude.
np.array with one dimension containing latitudes
tuple with first value latitude, second longitude
float with a latitude value
lon (np.array or float, optional) –
np.array with one dimension containing longitudes
float with a longitude value
- Returns
np.array
-
climada.util.coordinates.
get_land_geometry
(country_names=None, extent=None, resolution=10)[source]¶ Get union of all the countries or the provided ones or the points inside the extent.
- Parameters
country_names (list, optional) – list with ISO3 names of countries, e.g [‘ZWE’, ‘GBR’, ‘VNM’, ‘UZB’]
extent (tuple, optional) – (min_lon, max_lon, min_lat, max_lat)
resolution (float, optional) – 10, 50 or 110. Resolution in m. Default: 10m, i.e. 1:10.000.000
- Returns
shapely.geometry.multipolygon.MultiPolygon
-
climada.util.coordinates.
coord_on_land
(lat, lon, land_geom=None)[source]¶ Check if point is on land (True) or water (False) of provided coordinates. All globe considered if no input countries.
- Parameters
lat (np.array) – latitude of points
lon (np.array) – longitude of points
land_geom (shapely.geometry.multipolygon.MultiPolygon, optional) – profiles of land.
- Returns
np.array(bool)
-
climada.util.coordinates.
nat_earth_resolution
(resolution)[source]¶ Check if resolution is available in Natural Earth. Build string.
- Parameters
resolution (int) – resolution in millions, 110 == 1:110.000.000.
- Returns
str
- Raises
ValueError –
-
climada.util.coordinates.
get_country_geometries
(country_names=None, extent=None, resolution=10)[source]¶ Returns a gpd GeoSeries of natural earth multipolygons of the specified countries, resp. the countries that lie within the specified extent. If no arguments are given, simply returns the whole natural earth dataset. Take heed: we assume WGS84 as the CRS unless the Natural Earth download utility from cartopy starts including the projection information. (They are saving a whopping 147 bytes by omitting it.) Same goes for UTF.
- Parameters
country_names (list, optional) – list with ISO3 names of countries, e.g [‘ZWE’, ‘GBR’, ‘VNM’, ‘UZB’]
extent (tuple, optional) – (min_lon, max_lon, min_lat, max_lat) assumed to be in the same CRS as the natural earth data.
resolution (float, optional) – 10, 50 or 110. Resolution in m. Default: 10m
- Returns
GeoDataFrame
-
climada.util.coordinates.
get_resolution
(lat, lon)[source]¶ Compute resolution of points in lat and lon
- Parameters
lat (np.array) – latitude of points
lon (np.array) – longitude of points
- Returns
float
-
climada.util.coordinates.
points_to_raster
(points_bounds, res)[source]¶ ” Transform vector data coordinates to raster. Returns number of rows, columns and affine transformation
- Parameters
points_bounds (tuple) – points total bounds (xmin, ymin, xmax, ymax)
res (float) – resolution of output raster
- Returns
int, int, affine.Affine
-
climada.util.coordinates.
equal_crs
(crs_one, crs_two)[source]¶ Compare two crs
- Parameters
crs_one (dict or string or wkt) – user crs
crs_two (dict or string or wkt) – user crs
- Returns
bool
-
climada.util.coordinates.
read_raster
(file_name, band=[1], src_crs=None, window=False, geometry=False, dst_crs=False, transform=None, width=None, height=None, resampling=<Resampling.nearest: 0>)[source]¶ Read raster of bands and set 0 values to the masked ones. Each band is an event. Select region using window or geometry. Reproject input by proving dst_crs and/or (transform, width, height). Returns matrix in 2d: band x coordinates in 1d (evtl. reshape to band x height x width)
- Parameters
file_name (str) – name of the file
band (list(int), optional) – band number to read. Default: 1
window (rasterio.windows.Window, optional) – window to read
geometry (shapely.geometry, optional) – consider pixels only in shape
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
- Returns
dict (meta), np.array (band x coordinates_in_1d)
-
climada.util.coordinates.
read_vector
(file_name, field_name, dst_crs=None)[source]¶ Read vector file format supported by fiona. Each field_name name is considered an event.
- Parameters
file_name (str) – vector file with format supported by fiona and ‘geometry’ field.
field_name (list(str)) – list of names of the columns with values.
dst_crs (crs, optional) – reproject to given crs
- Returns
np.array (lat), np.array (lon), geometry (GeiSeries), np.array (value)
-
climada.util.coordinates.
write_raster
(file_name, data_matrix, meta)[source]¶ Write raster in GeoTiff format
- Parameters
fle_name (str) – file name to write
data_matrix (np.array) – 2d raster data. Either containing one band, or every row is a band and the column represents the grid in 1d.
meta (dict) – rasterio meta dictionary containing raster properties: width, height, crs and transform must be present at least (transform needs to contain upper left corner!)
climada.util.files_handler module¶
-
climada.util.files_handler.
to_list
(num_exp, values, val_name)[source]¶ Check size and transform to list if necessary. If size is one, build a list with num_exp repeated values.
- Parameters
num_exp (int) – number of expect list elements
values (object or list(object)) – values to check and transform
val_name (str) – name of the variable values
- Returns
list
-
climada.util.files_handler.
get_file_names
(file_name)[source]¶ Return list of files contained. Supports globbing.
- Parameters
file_name (str or list(str)) – Either a single string or a list of strings that are either
a file path
or the path of the folder containing the files
or a globbing pattern.
- Returns
list
climada.util.hdf5_handler module¶
-
climada.util.hdf5_handler.
read
(file_name, with_refs=False)[source]¶ Load a hdf5 data structure from a file.
- Parameters
file_name – file to load
with_refs – enable loading of the references. Default is unset, since it increments the execution time considerably.
- Returns
dictionary structure containing all the variables.
- Return type
contents
Examples
Contents contains the Matlab data in a dictionary.
>>> contents = read("/pathto/dummy.mat")
Contents contains the Matlab data and its reference in a dictionary.
>>> contents = read("/pathto/dummy.mat", True)
- Raises
Exception while reading –
-
climada.util.hdf5_handler.
get_string
(array)[source]¶ Form string from input array of unisgned integers.
- Parameters
array – array of integers
- Returns
string
-
climada.util.hdf5_handler.
get_str_from_ref
(file_name, var)[source]¶ Form string from a reference HDF5 variable of the given file.
- Parameters
file_name – matlab file name
var – HDF5 reference variable
- Returns
string
climada.util.interpolation module¶
-
climada.util.interpolation.
DIST_DEF
= ['approx', 'haversine']¶ Distances
-
climada.util.interpolation.
METHOD
= ['NN']¶ Interpolation methods
-
climada.util.interpolation.
dist_sqr_approx
[source]¶ Compute squared equirectangular approximation distance. Values need to be sqrt and multiplicated by ONE_LAT_KM to obtain distance in km.
-
climada.util.interpolation.
interpol_index
(centroids, coordinates, method='NN', distance='haversine', threshold=100)[source]¶ Returns for each coordinate the centroids indexes used for interpolation.
- Parameters
centroids (2d array) – First column contains latitude, second column contains longitude. Each row is a geographic point
coordinates (2d array) – First column contains latitude, second column contains longitude. Each row is a geographic point
method (str, optional) – interpolation method to use. NN default.
distance (str, optional) – distance to use. Haversine default
threshold (float) – distance threshold in km over which no neighbor will be found. Those are assigned with a -1 index
- Returns
- numpy array with so many rows as coordinates containing the
centroids indexes
climada.util.plot module¶
-
climada.util.plot.
geo_bin_from_array
(array_sub, geo_coord, var_name, title, pop_name=True, buffer=1.0, extend='neither', proj=<cartopy.crs.PlateCarree object>, **kwargs)[source]¶ Plot array values binned over input coordinates.
- Parameters
array_sub (np.array(1d or 2d) or list(np.array)) – Each array (in a row or in the list) are values at each point in corresponding geo_coord that are binned in one subplot.
geo_coord (2d np.array or list(2d np.array)) – (lat, lon) for each point in a row. If one provided, the same grid is used for all subplots. Otherwise provide as many as subplots in array_sub.
var_name (str or list(str)) – label to be shown in the colorbar. If one provided, the same is used for all subplots. Otherwise provide as many as subplots in array_sub.
title (str or list(str)) – subplot title. If one provided, the same is used for all subplots. Otherwise provide as many as subplots in array_sub.
pop_name (bool, optional) – add names of the populated places.
buffer (float, optional) – border to add to coordinates
extend (str, optional) – extend border colorbar with arrows. [ ‘neither’ | ‘both’ | ‘min’ | ‘max’ ]
proj (ccrs) – coordinate reference system used in coordinates
kwargs (optional) – arguments for hexbin matplotlib function
- Returns
matplotlib.figure.Figure, cartopy.mpl.geoaxes.GeoAxesSubplot
- Raises
ValueError –
-
climada.util.plot.
geo_im_from_array
(array_sub, geo_coord, var_name, title, proj=<cartopy.crs.PlateCarree object>, **kwargs)[source]¶ Image(s) plot defined in array(s) over input coordinates.
- Parameters
array_sub (np.array(1d or 2d) or list(np.array)) – Each array (in a row or in the list) are values at each point in corresponding geo_coord that are ploted in one subplot.
geo_coord (2d np.array or list(2d np.array)) – (lat, lon) for each point in a row. If one provided, the same grid is used for all subplots. Otherwise provide as many as subplots in array_sub.
var_name (str or list(str)) – label to be shown in the colorbar. If one provided, the same is used for all subplots. Otherwise provide as many as subplots in array_sub.
title (str or list(str)) – subplot title. If one provided, the same is used for all subplots. Otherwise provide as many as subplots in array_sub.
proj (ccrs) – coordinate reference system used in coordinates
kwargs (optional) – arguments for pcolormesh matplotlib function
- Returns
matplotlib.figure.Figure, cartopy.mpl.geoaxes.GeoAxesSubplot
- Raises
ValueError –
-
class
climada.util.plot.
Graph2D
(title='', num_subplots=1, num_row=None, num_col=None)[source]¶ Bases:
object
2D graph object. Handles various subplots and curves.
-
__init__
(title='', num_subplots=1, num_row=None, num_col=None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
add_curve
(var_x, var_y, fmt=None, ax_num=None, **kwargs)[source]¶ Add (x, y) curve to current subplot.
- Parameters
var_x (array) – abcissa values
var_y (array) – ordinate values
fmt (str, optional) – format e.g ‘k–’
ax_num (int, optional) – number of axis to plot. Current if None.
kwargs (optional) – arguments for plot matplotlib function
-
set_x_lim
(var_x, ax_num=None)[source]¶ Set x axis limits from minimum and maximum provided values.
- Parameters
var_x (array) – abcissa values
ax_num (int, optional) – number of axis to plot. Current if None.
-
-
climada.util.plot.
make_map
(num_sub=1, proj=<cartopy.crs.PlateCarree object>)[source]¶ Create map figure with cartopy.
- Parameters
num_sub (int) – number of subplots in figure.
proj (cartopy.crs projection, optional) – geographical projection, PlateCarree default.
- Returns
matplotlib.figure.Figure, np.array(cartopy.mpl.geoaxes.GeoAxesSubplot)
-
climada.util.plot.
add_shapes
(axis)[source]¶ Overlay Earth’s countries coastlines to matplotlib.pyplot axis.
- Parameters
axis (cartopy.mpl.geoaxes.GeoAxesSubplot) – cartopy axis.
projection (cartopy.crs projection, optional) – geographical projection, PlateCarree default.
-
climada.util.plot.
add_populated_places
(axis, extent, proj=<cartopy.crs.PlateCarree object>)[source]¶ Add city names.
- Parameters
axis (cartopy.mpl.geoaxes.GeoAxesSubplot) – cartopy axis.
extent (list) – geographical limits [min_lon, max_lon, min_lat, max_lat]
proj (cartopy.crs projection, optional) – geographical projection, PlateCarree default.
-
climada.util.plot.
add_cntry_names
(axis, extent, proj=<cartopy.crs.PlateCarree object>)[source]¶ Add country names.
- Parameters
axis (cartopy.mpl.geoaxes.GeoAxesSubplot) – cartopy axis.
extent (list) – geographical limits [min_lon, max_lon, min_lat, max_lat]
proj (cartopy.crs projection, optional) – geographical projection, PlateCarree default.
-
climada.util.plot.
add_basemap
(axis, zoom, url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png', flip=False)[source]¶ Add image to given axis. Coordinates need to be in epsg=3857.
- Parameters
(cartopy.mpl.geoaxes.GeoAxesSubplot) – plot axis
zoom (int, optional) – zoom coefficient used in the satellite image
url (str, optional) – image source, e.g. ctx.sources.OSM_C