Skip to content

terrain

terrain

PyAERMOD Terrain Processing Pipeline

Downloads DEM data from USGS, runs AERMAP terrain preprocessor, parses output, and updates receptor/source elevations.

Requires: pip install pyaermod[terrain]

DEMTileInfo dataclass

Metadata for a single DEM tile from USGS.

date is the tile version's date, YYYYMMDD: the one in the file name (USGS_1_n42w074_20230314.tif), else the TNM API's publicationDate.

tile_key property

tile_key: str

The tile's file name without its version date, the same for every dated version of one tile (USGS_1_n42w074.tif).

DEMDownloader

Downloads USGS 3DEP (1/3 arc-second NED) elevation data.

Uses the USGS TNM (The National Map) API to find and download DEM tiles covering a bounding box.

Parameters:

Name Type Description Default
cache_dir Path or str

Directory to cache downloaded tiles. Defaults to ~/.pyaermod/dem_cache.

None
dataset str

USGS dataset name.

'National Elevation Dataset (NED) 1/3 arc-second'

find_tiles

find_tiles(bounds: Tuple[float, float, float, float], newest_only: bool = True, retries: int = 3, backoff: float = 5.0) -> List[DEMTileInfo]

Find DEM tiles covering a bounding box.

Parameters:

Name Type Description Default
bounds tuple

(west, south, east, north) in decimal degrees (WGS84).

required
newest_only bool

Keep only the newest version of each tile (see :func:newest_tiles); False returns every version the API lists.

True
retries int

How many more times to ask after a gateway error (502/503/504, which the TNM API gives under load), a 429/500, or a failed connection.

3
backoff float

Seconds to wait before the first retry; doubled each time.

5.0

Returns:

Type Description
list of DEMTileInfo

download_tile

download_tile(tile: DEMTileInfo, output_dir: Optional[Path] = None, uncompressed: bool = True) -> Path

Download a single DEM tile.

Parameters:

Name Type Description Default
tile DEMTileInfo

Tile to download.

required
output_dir Path

Where to save. Defaults to cache_dir.

None
uncompressed bool

Rewrite the tile uncompressed and striped (:func:write_uncompressed_geotiff), the layout AERMAP reads; USGS's LZW-compressed tiles stop AERMAP 24142 with E455. Needs rasterio; without it the tile is left as downloaded, with a warning.

True

Returns:

Type Description
Path to downloaded file.

download_dem

download_dem(bounds: Tuple[float, float, float, float], output_dir: Optional[Union[str, Path]] = None, uncompressed: bool = True) -> List[Path]

Download the newest version of every DEM tile covering a bounding box.

Parameters:

Name Type Description Default
bounds tuple

(west, south, east, north) in decimal degrees (WGS84).

required
output_dir Path or str

Directory to save tiles. Defaults to cache_dir.

None
uncompressed bool

Rewrite each tile uncompressed for AERMAP (see :meth:download_tile).

True

Returns:

Type Description
list of Path

AERMAPRunResult dataclass

Result from an AERMAP execution.

success is AERMAP's own verdict: exit code 0, the message file <input stem>.out present, its *** AERMAP Finishes Successfully *** line, and no fatal error in its final message summary. AERMAP exits with code 0 after a fatal error, so the exit code alone says nothing. message_file is the path of that .out file, fatal_count and warning_count are AERMAP's own totals, and fatal_errors lists its fatal errors, such as "OU E310 line 29 CHKEXT: Domain Coordinate is NOT Inside a DEM File. Pt.= 1".

AERMAPRunner

Execute AERMAP terrain preprocessor from Python.

Parameters:

Name Type Description Default
executable_path Path or str

Path to AERMAP executable. If None, searches PATH.

None
log_level str

Logging level.

'INFO'

run

run(input_file: Union[str, Path], working_dir: Optional[Union[str, Path]] = None, timeout: int = 3600, capture_output: bool = True) -> AERMAPRunResult

Execute AERMAP with given input file.

Parameters:

Name Type Description Default
input_file Path or str

Path to AERMAP input file.

required
working_dir Path or str

Working directory. Defaults to input file's parent.

None
timeout int

Maximum execution time in seconds.

3600
capture_output bool

Whether to capture stdout/stderr.

True

Returns:

Type Description
AERMAPRunResult

AERMAPOutputParser

Parse AERMAP receptor and source output files.

Output format verified against AERMAP Fortran source code (v24142).

parse_receptor_output staticmethod

parse_receptor_output(filepath: Union[str, Path]) -> pd.DataFrame

Parse AERMAP receptor output to extract elevations and hill heights.

Reads discrete receptors (DISCCART) and every receptor network in the file, Cartesian (GRIDCART, from XYINC or XPNTS/YPNTS) and polar (GRIDPOLR, from ORIG, DIST and GDIR or DDIR), each under its own network ID. AERMAP echoes each network's definition and adds its ELEV and HILL rows: a Cartesian row is one y value across the x values, a polar row one direction across the ring distances.

Parameters:

Name Type Description Default
filepath Path or str

Path to AERMAP receptor output file.

required

Returns:

Type Description
DataFrame

Columns: x, y, zelev, zhill, network, row, col. network is the grid's ID (None for a discrete receptor), and row and col are its 0-based indices (grid_elevations[row][col] of a Cartesian grid, elevations[row][col] of a polar one). A polar network centred on a source ID (ORIG srcid) has no coordinates in the file, so its x and y are NaN.

parse_source_output staticmethod

parse_source_output(filepath: Union[str, Path]) -> pd.DataFrame

Parse AERMAP source output to extract base elevations.

Format: "SO LOCATION srcid(A12) type(A8) x(F12.2) y(F12.2) zelev(F12.2)", with the end point (x2, y2) before zelev for LINE, RLINE and BUOYLINE sources. x and y are the first point.

Parameters:

Name Type Description Default
filepath Path or str

Path to AERMAP source output file.

required

Returns:

Type Description
DataFrame

Columns: source_id, source_type, x, y, zelev

TerrainProcessor

High-level terrain processing pipeline.

Coordinates DEM download, AERMAP input generation, execution, and elevation updates for an AERMOD project.

create_aermap_project_from_aermod

create_aermap_project_from_aermod(aermod_project, dem_files: List[str], utm_zone: int = 16, datum: str = 'NAD83', domain_buffer: Optional[float] = None, nad_grids_dir: Optional[str] = None)

Create an AERMAPProject from an AERMODProject.

The same as AERMAPProject.from_aermod_project: the AERMOD coordinates are read as UTM coordinates in utm_zone, and every source, grid and discrete receptor is written. By default there is no DOMAINXY, so AERMAP searches the whole DEM for hill heights. domain_buffer (metres) limits the search to the project's extent widened by that much, which the DEM files must cover and which must take in every terrain feature that rises above a 10% slope from any receptor, or the hill heights come out too low.

Parameters:

Name Type Description Default
aermod_project AERMODProject
required
dem_files list of str
required
utm_zone int
16
datum str
'NAD83'
domain_buffer float
None
nad_grids_dir str

Directory of the NADCON grid files, needed when datum differs from the DEM files' datum.

None

Returns:

Type Description
AERMAPProject

process

process(project, bounds: Tuple[float, float, float, float], aermap_exe: Optional[Union[str, Path]] = None, working_dir: Optional[Union[str, Path]] = None, utm_zone: int = 16, datum: str = 'NAD83', skip_download: bool = False, dem_files: Optional[List[str]] = None, timeout: int = 3600, domain_buffer: Optional[float] = None, nad_grids_dir: Optional[str] = None)

Run the full terrain processing pipeline.

Steps: 1. Download DEM tiles (or use provided files) 2. Generate AERMAP input from AERMOD project 3. Run AERMAP 4. Parse output and update project elevations

Parameters:

Name Type Description Default
project AERMODProject
required
bounds tuple

(west, south, east, north) in decimal degrees.

required
aermap_exe Path or str
None
working_dir Path or str
None
utm_zone int
16
datum str
'NAD83'
skip_download bool

Skip DEM download (use dem_files instead).

False
dem_files list of str

Pre-existing DEM files. A relative path names a file in working_dir (where AERMAP runs) when one is there, else a file relative to the current directory; either way it is written to the AERMAP deck as an absolute path.

None
timeout int

AERMAP execution timeout in seconds.

3600
domain_buffer float

Metres around the project's extent for AERMAP's DOMAINXY. None (the default) lets AERMAP search the whole DEM for hill heights; see create_aermap_project_from_aermod.

None
nad_grids_dir str

Directory of the NADCON grid files (NADGRIDS).

None

Returns:

Type Description
AERMODProject

Updated project: every source's base elevation, and the elevations and hill heights of every discrete receptor and of every Cartesian and polar grid.

newest_tiles

newest_tiles(tiles: List[DEMTileInfo]) -> List[DEMTileInfo]

The newest version of each tile, in the order the tiles first appear.

The TNM API lists every dated version of a 3DEP tile (USGS_1_n42w074_20220801.tif, ..._20230314.tif); mosaicking them all is slow and gives AERMAP overlapping DEM files. A version with no date counts as older than any dated one.

write_uncompressed_geotiff

write_uncompressed_geotiff(path: Union[str, Path]) -> bool

Rewrite a compressed GeoTIFF in place as an uncompressed, striped one.

USGS ships 3DEP tiles LZW-compressed (TIFF tag 259 = 5) and tiled; AERMAP 24142 reads neither and stops with E455 ("TiffTag 259"). Returns True when the file was rewritten, False when it was already plain or is not a raster rasterio can open. Needs rasterio.

run_aermap

run_aermap(input_file: Union[str, Path], executable_path: Optional[Union[str, Path]] = None, timeout: int = 3600) -> AERMAPRunResult

Quick function to run AERMAP.

Parameters:

Name Type Description Default
input_file Path or str
required
executable_path Path or str
None
timeout int
3600

Returns:

Type Description
AERMAPRunResult