UTILS API Documentation
utils.py
General-purpose utility functions for file management, numerical operations, and geospatial transformations.
Note
This module is intended for internal use within the library, but may be imported elsewhere as needed.
Author: Travis Alongi (talongi@usgs.gov)
convert_dd_catalog_to_csv(input_txt, output_csv, skip_lines=97, header_names_list=None)
Convert a Northern California DD catalog .txt file to a CSV.
Parameters:
input_txt : str Path to the downloaded DD catalog file. output_csv : str Path to output CSV file.
Source code in utils.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
convert_latlon_to_utm(lat, lon, return_crs=False)
Converts latitude and longitude arrays to UTM coordinates.
This function transforms latitude and longitude values to UTM (Universal Transverse Mercator) coordinates using the appropriate UTM projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
array - like
|
Latitude values (in degrees). |
required |
lon
|
array - like
|
Longitude values (in degrees). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple of UTM X and Y coordinates. |
Example
import numpy as np
latitudes = np.array([37.0, 37.1])
longitudes = np.array([-121.9, -121.8])
utm_x, utm_y = convert_latlon_to_utm(latitudes, longitudes)
print(utm_x, utm_y)
Source code in utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
decompress_gz_file(gz_path, dest_path=None, overwrite=False)
Decompress a .gz file to the destination path.
Source code in utils.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
download_file(url, dest_path)
Download a file from a URL if it doesn't already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to download from. |
required |
dest_path
|
str
|
Full path to save the file (including filename). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Path to the downloaded file, or None if failed. |
Source code in utils.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
midpoint(array)
Calculates midpoints between adjacent items in an array.
This function returns the midpoints of adjacent values in the input array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
ndarray
|
A 1D array of numerical values (floats or ints). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
array |
A 1D array of midpoints between adjacent values. |
Example
import numpy as np
values = np.array([0, 10, 20, 30])
midpoints = midpoint(values)
print(midpoints) # Output: [5. 15. 25.]
Source code in utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
remove_temp_files(directory='./input', pattern='*temp*')
Removes temporary files
Source code in utils.py
249 250 251 252 253 254 255 256 257 | |
setup_output_directory(output_dir, delete_existing=False)
Ensure output directory exists and optionally clear existing files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str or Path - like
|
Directory where data will be stored. |
required |
delete_existing
|
bool
|
If True, delete all files in the output directory. Defaults to False. |
False
|
Example
setup_output_directory("results/") # creates the dir if needed, doesn't delete setup_output_directory("results/", delete_existing=True) # deletes files first
Source code in utils.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |