tool_utils.py
The tool_utils
library contains a handful of scripts that are useful for code execution.
check_setup
Checks that the isrm_health_calculations local clone is set up properly
- Inputs: None
- Outputs:
valid_setup
: a Boolean indicating if the setup is valid or
- Methodology:
- Gets the programs current working directory.
- Checks that all the script and supporting files exist where they are supposed to.
- Checks that all key data files are saved where they should (not including ISRM)
- Checks that the CA_ISRM is located in the data folder with all necessary objects, but does not consider this an improper setup, as the user may have their own ISRM.
- Reports any missing files or directories.
setup_logging
Sets up the log file capability using the logging
library
- Inputs:
debug_mode
: a Boolean indicating if log statements should be returned in debug mode or not
- Outputs:
tmp_logger
: a filepath string associated with a temporary log file that will be moved as soon as the output directory is created
- Methodology:
- Defines useful variables for the
logging
library. - Creates a temporary log file path (
tmp_logger
) that allows the file to be created before the output directory. - Suppresses all other library warnings and information.
- Sets the formatting system for log statements.
- Defines useful variables for the
verboseprint
Sets up the verbose printing mechanism for global usage
- Inputs:
verbose
: a Boolean indicating if it is in verbose mode or nottext
: a string to be returned if the program is in verbose modedebug_mode
: a Boolean indicating whether or not to output debug statementsframeinfo
: inspect object that provides information about the line making the function call
- Outputs: None
- Methodology:
- Checks if verbose is True.
- If True:
- Checks for debug mode. If it is running in debug, it will add the script name and line number that made the function call to
verboseprint
to the log statement text. - Creates a log statement.
- Checks for debug mode. If it is running in debug, it will add the script name and line number that made the function call to
- If False, does nothing.
report_version
Reports the current working version of the tool
- Inputs: None
- Outputs: None
- Methodology: adds statements to the log file about the tool version
create_output_dir
Creates the output directory for saving files
- Inputs:
batch
: the batch namename
: the run name
- Outputs:
output_dir
: a filepath string for the output directoryf_out
: a string containing the filename pattern to be used in output files
- Methodology:
- Grabs the current working directory of the tool and defines ‘outputs’ as the sub-directory to use.
- Checks to see if the directory already exists. If it does exists, automatically increments by 1 to create a unique directory.
- Creates
f_out
by removing the ‘out’ before theoutput_dir
. - Creates the output directory.
create_shape_out
Creates the output directory for saving shapefiles
- Inputs:
output_dir
: a filepath string for the output directory
- Outputs:
shape_out
: a filepath string for the shapefile output directory
- Methodology:
- Creates a directory within the
output_dir
called ‘shapes’. - Stores this name as
shape_out
.
- Creates a directory within the
get_output_region
Creates the output region geodataframe
- Inputs:
region_of_interest
: the name of the region to be contained in theoutput_region
region_category
: a string containing the region category for the output region, must be one of ‘AB’,’AD’, or ‘C’ for Air Basins, Air Districts, and Countiesoutput_geometry_fps
: a dictionary containing a mapping betweenregion_category
and the filepathsca_fps
: a filepath string containing the link to the California border shapefile
- Outputs
output_region
: a geodataframe containing only the region of interest
- Methodology:
- Checks if the
region_of_interest
is California, in which case, it just reads in the California shapefile. - If California is not the
region_of_interest
:- Gets the filepath of the output region based on the
region_category
from theoutput_geometry_fps
dictionary. - Reads in the file as a geodataframe.
- Clips the geodataframe to the
region_of_interest
.
- Gets the filepath of the output region based on the
- Checks if the
intersect_geometries
Performs geometric intersection between input layer and target geography, calculates area fractions.
- Inputs:
input_layer
: GeoDataFrame containing the source geometries.target_geography
: GeoDataFrame containing the target geometries for intersection.area_column
: Name of the column to store area calculations.id_desc
: Name to give the ID columnverbose
: a Boolean indicating if it is in verbose mode or notdebug_mode
: a Boolean indicating whether or not to output debug statements
- Outputs:
intersect
: GeoDataFrame with intersections and area fractions.input_copy
: Input GeoDataFrame containing the geometries to be intersected.
- Methodology:
- Checks if the CRS of the input layer matches the target geography’s CRS, and projects if necessary.
- Adds an ID field to the input layer.
- Calculates the total area of each input geometry cell.
- Creates an intersect object between the input and target geometries.
- Adds total area and area fraction to the intersect object.
calculate_true_north_angle
Calculates the angle between the positive y-axis and true north.
- Inputs:
center_lon
: Longitude of the center point of the map.center_lat
: Latitude of the center point of the map.crs
: The coordinate reference system of the map.
- Outputs:
angle
: The angle in degrees.
- Methodology:
- Creates a transformer to convert from WGS84 to the given CRS.
- Transforms the center point and a point slightly north.
- Calculates the angle between these points to determine the direction of true north.
add_north_arrow
Adds a simple north arrow to the plot with a specified rotation angle.
- Inputs:
ax
: The axis to add the north arrow to.angle
: The angle to rotate the north arrow.x
: The x-coordinate of the arrow’s tip (in axis coordinates).y
: The y-coordinate of the arrow’s tip (in axis coordinates).arrow_length
: The length of the arrow.
- Outputs:
- None
- Methodology:
- Ensures the angle is a float.
- Creates a rotation transformation.
- Defines coordinates for the arrow.
- Adds the arrow and ‘N’ annotation to the plot.