Skip to content

postfile

postfile

PyAERMOD POSTFILE Parser

Parses AERMOD POSTFILE output files containing concentration grids for each averaging period and source group.

Supports both formatted (PLOT) and unformatted (UNFORM/binary) POSTFILE output.

AERMOD formatted POSTFILE (PLOT): - Header lines start with '*' and contain metadata such as AERMOD version, AVERTIME, POLLUTID, and SRCGROUP. - Data lines contain columns: X, Y, one value per output type, ZELEV, ZHILL, ZFLAG, AVE, GRP, DATE (YYMMDDHH), NET ID. - Concentrations may use scientific notation (e.g. 1.23456E+01).

AERMOD unformatted POSTFILE (UNFORM): - Fortran unformatted sequential records. - Each record contains: KURDAT (int32), IANHRS (int32), GRPID (char*8), then one block of num_receptors float64 values per output type. - IANHRS is the hours in a short-term record's averaging period, the number of hours in the run for a PERIOD record and the number of years (NUMYRS) for an ANNUAL one, so it does not always say which kind of record it is (see :class:UnformattedPostfileParser). - Receptor coordinates are NOT stored in the binary file; they must be supplied externally or default to index-based values.

Output types. A run writes one value per receptor for each output type on its MODELOPT line (CONC, DEPOS, DDEP, WDEP; NUMTYP of them). AERMOD always orders them CONC, DEPOS, DDEP, WDEP, whatever the keyword order on MODELOPT (MODOPT in coset.f), and writes them in that order in every POSTFILE and PLOTFILE row (POSTFL in calc2.f, PSTANN and PLOTFL in output.f). A text file names them in its column-label header line (AVERAGE CONC, TOTAL DEPO, DRY DEPO, WET DEPO); a binary file does not, so a binary file from a run with more than one output type needs the types from the caller.

Based on AERMOD version 26135 POSTFILE specifications (validated against 26135 and 24142; see :mod:pyaermod.versions).

PostfileHeader dataclass

Metadata parsed from POSTFILE header lines.

Each field corresponds to a '*'-prefixed header line in the POSTFILE. output_types is the file's output types in AERMOD's order (for example ("CONC", "DDEP")), read from the column labels of a text file or given by the caller for a binary one; it is None when neither says.

PostfileResult dataclass

Parsed data for a single POSTFILE (one source group / averaging period).

Attributes:

Name Type Description
header PostfileHeader

Metadata extracted from the file header.

data DataFrame

One row per receptor and time step, with columns x, y, the values, zelev, zhill, zflag, ave, grp, date (plus rank in a PLOTFILE of high values). A file with one output type holds its values in concentration, whatever the type. A file with several holds one column per type: concentration (CONC), total_depo (DEPOS), dry_depo (DDEP) and wet_depo (WDEP). :meth:column_for names the column of a given type.

output_types property

output_types: Optional[Tuple[str, ...]]

The file's output types in AERMOD's order, or None if unknown.

max_concentration property

max_concentration: float

Return the maximum value of the file's first output type.

That is the concentration whenever the file holds CONC; for a file without CONC it is the first of its deposition types.

max_location property

max_location: Tuple[float, float]

Return (x, y) coordinates of the maximum of the first output type.

flagpole_heights property

flagpole_heights: List[float]

The distinct flagpole heights (zflag) in the file, ascending.

column_for

column_for(output_type: str) -> str

Return the name of the column that holds output_type.

Parameters:

Name Type Description Default
output_type str

"CONC", "DEPOS", "DDEP" or "WDEP".

required

Raises:

Type Description
KeyError

If the file does not hold that output type.

get_timestep

get_timestep(date: str) -> pd.DataFrame

Get all data rows for a specific date/time.

Parameters:

Name Type Description Default
date str

Date string in YYMMDDHH format (e.g. '26010101').

required

Returns:

Type Description
DataFrame

Subset of data matching the requested date.

get_receptor

get_receptor(x: float, y: float, tolerance: float = 1.0, zflag: Optional[float] = None) -> pd.DataFrame

Get all data rows for a specific receptor location.

A flagpole run (CO FLAGPOLE with one network per height) has several receptors at the same (x, y), one per zflag. Pass zflag to pick one height.

Parameters:

Name Type Description Default
x float

X coordinate of the receptor.

required
y float

Y coordinate of the receptor.

required
tolerance float

Distance tolerance for matching receptor coordinates, and for matching zflag when it is given.

1.0
zflag float

Flagpole height of the receptor. When it is None and the rows found span several heights, all of them are returned, as before, with a :class:UserWarning naming the heights.

None

Returns:

Type Description
DataFrame

Subset of data within tolerance of the requested location.

get_max_by_receptor

get_max_by_receptor() -> pd.DataFrame

Get the maximum of the first output type at each receptor.

A receptor is an (x, y) location, or an (x, y, zflag) location when the file holds more than one flagpole height (CO FLAGPOLE with a network per height): receptors at the same (x, y) but different heights stay separate rows.

Returns:

Type Description
DataFrame

DataFrame with columns x, y and the first output type's column (concentration whenever the file holds CONC). With several flagpole heights it also has zflag, zelev and zhill.

to_dataframe

to_dataframe() -> pd.DataFrame

Return a copy of the full data DataFrame.

Returns:

Type Description
DataFrame

Copy of the concentration data.

PostfileParser

Parser for AERMOD POSTFILE output files.

Also reads AERMOD's text PLOTFILEs, whose rows have the same layout plus a RANK column.

Parameters:

Name Type Description Default
filepath str or Path

Path to the POSTFILE to parse.

required
output_types str or iterable of str

The run's output types, as a MODELOPT line ("CONC DDEP FLAT") or as names (["CONC", "DDEP"]). Needed only for a file whose header does not name its columns, such as one written with OU NOHEADER that has two or three value columns; when the header does, the header is used and a different output_types raises ValueError.

None

Raises:

Type Description
FileNotFoundError

If the specified file does not exist.

parse

parse() -> PostfileResult

Read and parse the POSTFILE.

Returns:

Type Description
PostfileResult

Parsed header metadata and concentration data.

Raises:

Type Description
ValueError

If the header, or the rows of a file without one, disagree with the caller's output_types, or if the file has two or three value columns and neither the header nor the caller says which output types they are.

UnformattedPostfileParser

Parser for AERMOD unformatted (binary) POSTFILE output files.

AERMOD writes unformatted POSTFILE records using Fortran sequential unformatted I/O. Each record has the layout::

[4-byte record-length marker]
KURDAT    — int32    (date in YYMMDDHH format)
IANHRS    — int32    (hours in averaging period, or NUMYRS for ANNUAL)
GRPID     — char*8   (source group ID, space-padded)
AVEVAL()  — float64 × num_receptors × NUMTYP
[4-byte record-length marker]

A short-term record (POSTFL in calc2.f) holds the hour its block average ends and KAVE, the hours in its averaging period (1, 2, 3, 4, 6, 8, 12 or 24, or 720 for MONTH). A PERIOD or ANNUAL record (PSTANN in output.f) holds the run's last hour and, in the same field, the number of hours in the run (PERIOD) or of years (ANNUAL, NUMYRS). The text POSTFILE labels these rows PERIOD or ANNUAL and writes the count where the date would be (I8.8, so five years read 00000005); this parser gives a binary file the same ave and date.

The record does not say which kind it is, so averaging_period names it. Without it each record is read as:

  • N-HR (or MONTH for 720) when IANHRS is a short-term period and KURDAT is an hour that period's blocks end on (a 3-HR block ends at hour 3, 6, ... 24; a MONTH at hour 24). An ANNUAL file over 1, 2, 3, 4, 6, 8, 12 or 24 years, or a PERIOD file of that many hours ending on such an hour, reads this way too, so when every source group has just one record, as in an ANNUAL or PERIOD file, a UserWarning asks for averaging_period.
  • PERIOD when IANHRS is 100 or more: no run has that many years.
  • Otherwise ValueError: the record is ANNUAL or PERIOD, and only the caller knows which.

The values come in one block of num_receptors per output type, in AERMOD's order CONC, DEPOS, DDEP, WDEP (POSTFL in calc2.f writes ((AVEVAL(IREC,IGRP,IAVE,ITYP),IREC=1,NUMREC),ITYP=1,NUMTYP)). The file says neither how many receptors nor which types, so a file from a run with more than one output type needs output_types (or, for the unambiguous cases, num_receptors).

Parameters:

Name Type Description Default
filepath str or Path

Path to the unformatted POSTFILE.

required
num_receptors int

Number of receptors. If None, inferred from the first record size and the number of output types.

None
receptor_coords list of (float, float)

(x, y) coordinate pairs, one per receptor. Its length is the receptor count when num_receptors is not given, and must equal num_receptors when it is. If None, receptors are assigned index-based coordinates (i, 0).

None
has_deposition bool

Shorthand for output_types=("CONC", "DDEP", "WDEP"): True reads three blocks as concentration, dry and wet deposition. If None (default) and output_types is not given, the types are inferred from num_receptors where the record size allows only one reading (see output_types). If False, deposition columns are never produced.

None
output_types str or iterable of str

The run's output types, as its MODELOPT line ("CONC DDEP FLAT") or as names (["CONC", "DDEP"]); order does not matter. Without it, a record holding one block of values per receptor is read as concentration and four blocks as all four types; two or three blocks could be several sets of types and raise ValueError (pass has_deposition=True for CONC DDEP WDEP). The receptor count comes from num_receptors or receptor_coords; with neither, every value is read as a concentration at its own receptor, which is wrong for a run with more than one output type. With output_types, a receptor count that disagrees with the record catches a wrong set of types that still divides the record evenly.

None
averaging_period str or int

The file's averaging period, as on the POSTFILE keyword: "1" ... "24" (or "1-HR" ... "24-HR"), "MONTH", "PERIOD" or "ANNUAL". "PERIOD" and "ANNUAL" label every record so and put its IANHRS (hours, or NUMYRS) in date as the text POSTFILE does; a short-term period or MONTH must match every record's IANHRS. Without it the period is read from each record as described above.

None

Raises:

Type Description
FileNotFoundError

If the specified file does not exist.

parse

parse() -> PostfileResult

Read all records from the unformatted POSTFILE.

Returns:

Type Description
PostfileResult

Parsed header metadata and concentration data. Header fields are populated from the first record's source group and averaging info; version, title, model_options, and pollutant_id are set to None (not present in binary format). output_types is set when the caller gave the types or the record size settled them. ave and date are as the text POSTFILE writes them (see the class notes on PERIOD and ANNUAL records); header.averaging_period is the first record's ave.

A file with more than one output type has one column per type (concentration, total_depo, dry_depo, wet_depo); a file with one keeps it in concentration.

label_hours

label_hours(label: str) -> Optional[int]

N of a POSTFILE AVE label "N-HR" (upper case, any N), else None.

The one reader of the label: whether N is an averaging period AERMOD has (:data:SHORT_TERM_HOURS) is the caller's to judge.

warn_hourly_mean_limitation

warn_hourly_mean_limitation(what: str, stacklevel: int = 3) -> None

Warn that what is a mean computed from an hourly POSTFILE.

For the functions that average hourly POSTFILE rows (24-hour, PERIOD or ANNUAL means): the file cannot say which hours were calm or missing (HOURLY_MEAN_LIMITATION), so the mean can differ from the one AERMOD prints. No number is changed. stacklevel counts from the function that calls this one (3 names that function's caller).

read_postfile

read_postfile(filepath: Union[str, Path], *, num_receptors: Optional[int] = None, receptor_coords: Optional[List[Tuple[float, float]]] = None, has_deposition: Optional[bool] = None, output_types: OutputTypesArg = None, averaging_period: Union[str, int, None] = None) -> PostfileResult

Parse an AERMOD POSTFILE and return the result.

Automatically detects whether the file is in formatted (PLOT/text) or unformatted (UNFORM/binary) format. Also reads text PLOTFILEs, and text files written without a header (OU NOHEADER).

Parameters:

Name Type Description Default
filepath str or Path

Path to the POSTFILE.

required
num_receptors int

Number of receptors (binary files only). Ignored for text files. If None, taken from receptor_coords, else inferred from the first record.

None
receptor_coords list of (float, float)

Receptor (x, y) coordinates, one pair per receptor (binary files only). Ignored for text files.

None
has_deposition bool

Binary files only: True is shorthand for output_types=("CONC", "DDEP", "WDEP"). Ignored for text files.

None
output_types str or iterable of str

The run's output types, as its MODELOPT line ("CONC DDEP WDEP FLAT") or as names (["CONC", "DDEP"]). A binary file from a run with more than one output type needs it (except for all four types with a known receptor count), and so does a text file written without a header that has two or three value columns. A text file with a header names its types there, and a different output_types raises ValueError.

None
averaging_period str or int

Binary files only: the file's averaging period, as on its POSTFILE keyword ("1" ... "24", "MONTH", "PERIOD", "ANNUAL"). A binary record holds the hours of a short-term period, the hours of a PERIOD run or the years of an ANNUAL one in the same field, so an ANNUAL or PERIOD file needs it to be labelled as its text POSTFILE is (see :class:UnformattedPostfileParser). Ignored for text files, which name the period in every row.

None
Notes

An hourly POSTFILE has a row for every hour, calm and missing hours included, and nothing in a row says which hours those were. A 24-hour or PERIOD/ANNUAL mean computed from its rows therefore divides by the number of rows, where AERMOD divides by the hours that were neither calm nor missing; the two differ for any period that has such hours. Have AERMOD write the averaged value (POSTFILE 24, PERIOD) when it is the number that matters.

Returns:

Type Description
PostfileResult

Parsed header metadata and data. result.output_types gives the file's output types and result.column_for("DDEP") the column that holds one of them.