Skip to content

input_reader

input_reader

AERMOD .inp file reader.

Parses a textual AERMOD control-file back into an :class:AERMODProject so a round-trip is possible:

>>> project = read_aermod_input("facility.inp")
>>> project.write("facility_clone.inp")

Supported pathway keywords (stored on the project model and written back): CO: TITLEONE, TITLETWO, MODELOPT (incl. OLM/PVMRM/ARM2/GRSM/TTRM/TTRM2, ALPHA/BETA/PSDCREDIT, FLAT/ELEV and the FLAT ELEV pair, DRYDPLT/ NODRYDPLT/WETDPLT/NOWETDPLT, any other option kept in extra_model_options), DEBUGOPT, AVERTIME, POLLUTID, RUNORNOT, FLAGPOLE, URBANOPT (one or several areas), LOW_WIND, HALFLIFE, DCAYCOEF, NO2STACK, OZONEVAL, OZONEFIL, O3VALUES, O3SECTOR, OZONUNIT, NOXVALUE, NOX_FILE, NOX_VALS, NOX_UNIT, NOXSECTR, GASDEPDF, GASDEPVD, GDSEASON, GDLANUSE, SAVEFILE, INITFILE, MULTYEAR, EVENTFIL (file and option), ARMRATIO, AWMADWNW, ORD_DWNW, ARCFTOPT SO: LOCATION (POINT/POINTCAP/POINTHOR/SWPOINT/AREA/VOLUME/LINE/RLINE/ RLINEXT/OPENPIT/AREACIRC/AREAPOLY/BUOYLINE), SRCPARAM, AREAVERT, BLPINPUT, BLPGROUP, SRCGROUP, OLMGROUP, PSDGROUP, NO2RATIO, BACKGRND (value forms), BGSECTOR, EMISUNIT, CONCUNIT, DEPOUNIT, GASDEPOS, PARTDIAM, MASSFRAX, PARTDENS, METHOD_2, PLATFORM, ARCFTSRC, HBPSRCID, URBANSRC, BUILDHGT, BUILDWID, BUILDLEN, XBADJ, YBADJ, RBARRIER, RDEPRESS, SBARRIER, VBARRIER, RLEMCONV, ELEVUNIT FEET (as ControlPathway.elevation_units) RE: GRIDCART (XYINC or XPNTS/YPNTS, ELEV/HILL/FLAG rows), GRIDPOLR (ORIG by coordinates or source, DIST list, GDIR num/init/delta or DDIR list, ELEV/HILL/FLAG rows), DISCCART, ELEVUNIT ME: SURFFILE, PROFFILE, SURFDATA, UAIRDATA, PROFBASE, STARTEND (with or without hours), WDROTATE, DAYRANGE, NUMYEARS, WINDCATS, SCIMBYHR, and the turbulence keywords NOTURB, NOTURBST, NOTURBCO, NOSA, NOSW, NOSAST, NOSWST, NOSACO, NOSWCO OU: RECTABLE, MAXTABLE, DAYTABLE, SUMMFILE, MAXIFILE, PLOTFILE, POSTFILE, FILEFORM, MAXDAILY, MXDYBYYR, MAXDCONT, EVENTOUT, NOHEADER, RANKFILE, SEASONHR, EVALFILE, TOXXFILE EV: EVENTPER, EVENTLOC (an EV pathway makes the deck an EVENT run: AERMODProject.event_processing, no RE pathway required)

Every other line -- a keyword with no field above (EMISFACT, HOUREMIS, INCLUDED, BACKUNIT, SO ELEVUNIT METERS, CO ELEVUNIT (E110 in AERMOD), EVALCART, DISCPOLR, SITEDATA, ERRORFIL, NO2EQUIL, ...), a form of a known keyword the model cannot hold (a BACKGRND hourly file, a PLOTFILE with a lower rank or a unit, a second POSTFILE, the definition lines of a source type the reader does not construct, an incomplete receptor network) or a malformed line of a known keyword -- is kept verbatim in :attr:AERMODProject.unparsed_lines (see :mod:pyaermod.unparsed), reported through :mod:logging as one warning per pathway and keyword, and written back into its pathway by :meth:AERMODProject.to_aermod_input unless preserve_unparsed=False. Nothing is dropped silently, so a deck this reader does not fully understand still opens and, rewritten, still carries every line; what it does not do is produce byte-identical text.

Layout follows AERMOD's setup.f: the first line of the deck fixes the pathway column, the keyword occupies the eight columns after it, and a line blank through the keyword columns continues the previous keyword (GRIDPOLR POL1 STA followed by POL1 DIST 100. 1000.). The reader is otherwise permissive about whitespace and, as AERMOD (which upper-cases every line, setup.f LWRUPR), about case: pathway codes, keywords and source and group IDs match whatever their case, while the model keeps each ID as first spelled and file names as written (a modelled file name is stored without the double quotes a name with a blank needs; the writer adds them back). Numbers are read as AERMOD's STODBL reads them (1.0D2 is 100). It is strict about pathway order: each pathway must appear once, inside XX STARTING and XX FINISHED markers.

SandboxViolation dataclass

One path of a sandboxed deck that resolves outside the sandbox root.

field names the project field (meteorology.surface_file) or, for a line kept verbatim, its pathway, keyword and line number (CO ERRORFIL at line 14); path is the path as the deck wrote it and resolved where it would lead.

PathTraversalError

Bases: ValueError

Raised when a sandboxed .inp references a path outside its base dir.

The message names the first such path. :attr:violations lists every one of them in the order they were checked, so a caller can report them all at once rather than one per attempt.

InputFile dataclass

A file AERMOD reads (never writes) that a project names.

keyword says where the deck names it, as AERMOD's user guide does (ME SURFFILE, CO OZONEFIL SECT2, SO HOUREMIS at line 40 for a line kept verbatim); path is the path as written.

quote_path

quote_path(name: str) -> str

name as a runstream field: double-quoted when it holds a blank.

A name that already starts with a double quote is a field as written (setup.f DEFINE) and is returned unchanged, so quoting twice is harmless.

quote_path("My Met/a.sfc"), quote_path('"My Met/a.sfc"'), quote_path("a.sfc") ('"My Met/a.sfc"', '"My Met/a.sfc"', 'a.sfc')

unquote_path

unquote_path(raw: str) -> str

The file name AERMOD opens for a runstream field.

A field that starts with a double quote loses it and its closing quote (setup.f DEFINE); any other field is the name as written.

unquote_path('"My Met/a.sfc"'), unquote_path('x"y') ('My Met/a.sfc', 'x"y')

parse_aermod_input

parse_aermod_input(text: str) -> AERMODProject

Parse the text of an AERMOD .inp file into an AERMODProject.

Every line the pathway parsers cannot represent on the model is returned in :attr:AERMODProject.unparsed_lines (see :mod:pyaermod.unparsed) and summarised in one warning per pathway and keyword through :mod:logging; nothing is dropped silently.

read_aermod_input

read_aermod_input(path: Union[str, Path], *, sandbox: bool = False) -> AERMODProject

Read an AERMOD .inp file from disk and return the project.

Parameters:

Name Type Description Default
path str or Path

Path to the .inp file.

required
sandbox bool

If True, validate that every absolute path referenced inside the .inp (SURFFILE, PROFFILE, OZONEFIL, etc.) and every resolved relative path stays inside the .inp's parent directory. Raises :class:PathTraversalError if any path escapes; its message names the first and its violations list every one. Use this when ingesting untrusted .inp files (third-party permits, forwarded drafts) before passing the project to AERMOD. Lines kept verbatim in unparsed_lines are checked too, field by field as AERMOD splits them: several of them name files AERMOD opens (ERRORFIL, INCLUDED, HOUREMIS, a BACKGRND hourly file, a second POSTFILE, DEBUGOPT files), and they are written back as they stand.

The default (False) preserves prior behavior: paths are stored as-is and AERMOD itself decides what to open at run time.

False

runstream_fields

runstream_fields(text: str) -> List[str]

Split the data part of a runstream line into fields as AERMOD does.

setup.f (subroutine DEFINE) separates fields by blanks; a field that starts with a double quote runs to the next double quote (or the end of the line) and may hold blanks, and the quotes are not part of it. A quote inside an unquoted field is an ordinary character.

runstream_fields('ERRS.OUT "My Files/b.dat" x"y') ['ERRS.OUT', 'My Files/b.dat', 'x"y']

input_files

input_files(project: AERMODProject) -> List[InputFile]

Every file AERMOD reads that project names.

In this order: the met files, the ozone and NOx background files, the file a run starts from (INITFILE, MULTYEAR), the HOUREMIS files of sources.hourly_emissions, and, among the lines kept verbatim, in deck order, INCLUDED, HOUREMIS and hourly BACKGRND files. Files AERMOD writes (outputs, SAVEFILE, ERRORFIL) are not listed.

anchor_input_files

anchor_input_files(project: AERMODProject, deck_dir: Union[str, Path]) -> List[Tuple[InputFile, Path]]

Give each input file named by a relative path its full path, in place.

AERMOD reads a relative path from its working directory; a deck that names ozone.dat means the file beside it. Every input file of :func:input_files whose relative path names an existing file when read from deck_dir gets that file's full path, so the project runs from any working directory. A relative path that names no file there, and every path already absolute, is left as written, as are the files AERMOD writes. Returns (file as the deck named it, full path) for each path changed.