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
|
output_types
property
¶
The file's output types in AERMOD's order, or None if unknown.
max_concentration
property
¶
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
¶
Return (x, y) coordinates of the maximum of the first output type.
flagpole_heights
property
¶
The distinct flagpole heights (zflag) in the file, ascending.
column_for ¶
Return the name of the column that holds output_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_type
|
str
|
|
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the file does not hold that output type. |
get_timestep ¶
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 |
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: |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Subset of data within tolerance of the requested location. |
get_max_by_receptor ¶
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
( |
to_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 ( |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
parse ¶
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(orMONTHfor 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, aUserWarningasks for averaging_period.PERIODwhen 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)
|
|
None
|
has_deposition
|
bool
|
Shorthand for |
None
|
output_types
|
str or iterable of str
|
The run's output types, as its MODELOPT line ( |
None
|
averaging_period
|
str or int
|
The file's averaging period, as on the POSTFILE keyword: |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
parse ¶
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; A file with more than one output type has one column per type
( |
label_hours ¶
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 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 |
None
|
has_deposition
|
bool
|
Binary files only: True is shorthand for
|
None
|
output_types
|
str or iterable of str
|
The run's output types, as its MODELOPT line
( |
None
|
averaging_period
|
str or int
|
Binary files only: the file's averaging period, as on its POSTFILE
keyword ( |
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. |