utils
This module contains general utility functions.
create_logger #
create_logger(logger_name: str) -> Logger
Creates a logger object using input name parameter that outputs to stdout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger_name
|
str
|
Name of logger |
required |
Returns:
Source code in geospatial_tools/utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
get_yaml_config #
get_yaml_config(yaml_config_file: str, logger: Logger = LOGGER) -> dict
This function takes in the path, or name of the file if it can be found in the config/ folder, with of without the extension, and returns the values of the file in a dictionary format.
Ex. For a file named app_config.yml (or app_config.yaml), directly in the config/ folder,
the function could be called like so : params = get_yaml_config('app_config')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_config_file
|
str
|
Path to yaml config file. If config file is in the config folder, you can use the file's name without the extension. |
required |
logger
|
Logger
|
Logger to handle messaging, by default LOGGER |
LOGGER
|
Returns:
Source code in geospatial_tools/utils.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
get_json_config #
get_json_config(json_config_file: str, logger=LOGGER) -> dict
This function takes in the path, or name of the file if it can be found in the config/ folder, with of without the extension, and returns the values of the file in a dictionary format.
Ex. For a file named app_config.json, directly in the config/ folder,
the function could be called like so : params = get_json_config('app_config')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_config_file
|
str
|
Path to JSON config file. If config file is in the config folder, |
required |
logger
|
Logger to handle messaging |
LOGGER
|
Returns:
Source code in geospatial_tools/utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
create_crs #
create_crs(dataset_crs: str | int, logger=LOGGER)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_crs
|
str | int
|
EPSG code in string or int format. Can be given in the following ways: 5070 | "5070" | "EPSG:5070" |
required |
logger
|
Logger instance (Default value = LOGGER) |
LOGGER
|
Returns:
Source code in geospatial_tools/utils.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
download_url #
download_url(
url: str, filename: str | Path, overwrite: bool = False, logger=LOGGER
) -> Path | None
This function downloads a file from a given URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
Url to download |
required |
filename
|
str | Path
|
Filename (or full path) to save the downloaded file |
required |
overwrite
|
bool
|
If True, overwrite existing file |
False
|
logger
|
Logger instance |
LOGGER
|
Returns:
Source code in geospatial_tools/utils.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
unzip_file #
unzip_file(
zip_path: str | Path, extract_to: str | Path, logger: Logger = LOGGER
) -> list[str | Path]
This function unzips an archive to a specific directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
zip_path
|
str | Path
|
Path to zip file |
required |
extract_to
|
str | Path
|
Path of directory to extract the zip file |
required |
logger
|
Logger
|
Logger instance |
LOGGER
|
Returns:
Source code in geospatial_tools/utils.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
create_date_range_for_specific_period #
create_date_range_for_specific_period(
start_year: int, end_year: int, start_month_range: int, end_month_range: int
) -> list[str]
This function create a list of date ranges.
For example, I want to create date ranges for 2020 and 2021, but only for the months from March to May. I therefore expect to have 2 ranges: [2020-03-01 to 2020-05-30, 2021-03-01 to 2021-05-30].
Handles the automatic definition of the last day for the end month, as well as periods that cross over years
For example, I want to create date ranges for 2020 and 2022, but only for the months from November to January. I therefore expect to have 2 ranges: [2020-11-01 to 2021-01-31, 2021-11-01 to 2022-01-31].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_year
|
int
|
Start year for ranges |
required |
end_year
|
int
|
End year for ranges |
required |
start_month_range
|
int
|
Starting month for each period |
required |
end_month_range
|
int
|
End month for each period (inclusively) |
required |
Returns:
Source code in geospatial_tools/utils.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|