pySWAP Model¶
Building, running and parsing the results of a SWAP model run.
When the Model class begun to grow, it was clear that it needed to be refactored into a more modular structure. The functionality to build environment, run and parse result has been abstracted to 3 classes, focusing the main (and exposed to the user) Model class focused on the model components and their interactions. The four classes in this module are:
Classes:
ModelBuilder: Class responsible for building the model components.
ModelRunner: Class responsible for running the model.
ResultReader: Class responsible for parsing the model results.
Model: Main class that runs the SWAP model.
Model
¶
Bases: PySWAPBaseModel
, FileMixin
, SerializableMixin
Main class that runs the SWAP model.
Even though all sections are set to optional, the model will not run if any of the components are missing.
Attributes:
Name | Type | Description |
---|---|---|
metadata |
Subsection
|
Metadata of the model. |
version |
str
|
The version of the model. |
generalsettings |
Subsection
|
Simulation settings. |
meteorology |
Subsection
|
Meteorological data. |
crop |
Subsection
|
Crop data. |
fixedirrigation |
Subsection
|
Fixed irrigation settings. |
soilmoisture |
Subsection
|
Soil moisture data. |
surfaceflow |
Subsection
|
Surface flow data. |
evaporation |
Subsection
|
Evaporation data. |
soilprofile |
Subsection
|
Soil profile data. |
snowandfrost |
Subsection
|
Snow and frost data. |
richards |
Subsection
|
Richards data. |
lateraldrainage |
Subsection
|
Lateral drainage data. |
bottomboundary |
Subsection
|
Bottom boundary data. |
heatflow |
Subsection
|
Heat flow data. |
solutetransport |
Subsection
|
Solute transport data. |
Methods:
Name | Description |
---|---|
write_swp |
Write the .swp input file. |
validate |
Validate the model. |
run |
Run the model. |
Source code in pyswap/model/model.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
|
swp
property
¶
The content of the swp file.
Serialization of Subsection field type has been set in a way that it
will generate SWAP formatted string when model_string()
is called on
the parent class.
get_inputs()
¶
run(path=None, silence_warnings=False)
¶
Run the model using ModelRunner.
Source code in pyswap/model/model.py
to_classic_swap(path)
¶
Prepare all the files for a model run in user's directory.
Source code in pyswap/model/model.py
validate()
¶
Execute the model validation when run()
is called.
This method should probably be refactored. It seems to shadow some validation method from Pydantic.
Source code in pyswap/model/model.py
validate_each_component()
¶
Validate, on run, that all required components are present.
Source code in pyswap/model/model.py
validate_missing_components()
¶
Validate, on run, that all required components are present.
Source code in pyswap/model/model.py
write_swp(path)
¶
Write the .swp input file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
The path to write the file to. |
required |
ModelBuilder
¶
Building model components.
Attributes:
Name | Type | Description |
---|---|---|
model |
Model
|
The model to build. |
tempdir |
str
|
The temporary directory to store the input files. |
Methods:
Name | Description |
---|---|
copy_executable |
Copy the appropriate SWAP executable to the temporary directory. |
write_inputs |
Write the input files to the temporary directory. |
Source code in pyswap/model/model.py
copy_executable()
¶
Copy the appropriate SWAP executable to the temporary directory.
Source code in pyswap/model/model.py
get_inputs()
¶
Get the inpup files in a dictionary.
Source code in pyswap/model/model.py
write_inputs()
¶
Write the input files to the temporary directory.
Source code in pyswap/model/model.py
ModelRunner
¶
Class responsible for running the model.
In the run method, the ResultReader is utilized to abstract the parsing of the model results.
Attributes:
Name | Type | Description |
---|---|---|
model |
Model
|
The model to run. |
Methods:
Name | Description |
---|---|
run_swap |
Run the SWAP executable. |
raise_swap_warning |
Raise a warning. |
run |
Main function that runs the model |
Source code in pyswap/model/model.py
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 |
|
raise_swap_warning(warnings)
¶
Log the warnings form the model run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
warnings
|
list
|
The warnings from the model run parsed with the ResultReaded. |
required |
Source code in pyswap/model/model.py
run(path, silence_warnings=False)
¶
Main function that runs the model.
First ModelBuilder is used to prepare the environment for the model run. Second, the SWAP executable is run and the decoded result passed from the executable is parsed using the ResultReader and used to update the Result object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
The path to the temporary directory. |
required |
silence_warnings
|
bool
|
If True, warnings are not raised. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
The parsed model results. |
Source code in pyswap/model/model.py
run_swap(tempdir)
staticmethod
¶
Run the SWAP executable.
Run the exacutable in the tempdirectory and pass the newline to the stdin when the executable asks for input (upon termination).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tempdir
|
Path
|
The temporary directory where the executable is stored. |
required |
Source code in pyswap/model/model.py
ResultReader
¶
Class responsible for reading the model results.
Attributes:
Name | Type | Description |
---|---|---|
model |
Model
|
The model to read the results from. |
tempdir |
str
|
The temporary directory where the results are stored. |
Methods:
Name | Description |
---|---|
read_csv_output |
Read the csv output. |
read_swap_log |
Read the log files. |
identify_warnings |
Catch warnings from the log file. |
read_ascii_output |
Read all output files that are not in csv format as strings. |
Source code in pyswap/model/model.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
identify_warnings(log)
staticmethod
¶
Catch warnings from the log file.
This is used by the ModelRunner to raise warnings after the model run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log
|
str
|
The log file content. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of warnings. |
Source code in pyswap/model/model.py
read_ascii_output()
¶
Read all output files that are not csv format as strings.
This method is perhaps a bit oversimplified. In the future, we might think about introducing parsers for the different output files. For now, we just read them as strings.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary of the output strings with extension as key. |
Source code in pyswap/model/model.py
read_csv_output(which)
¶
Read the csv output.
There are two types of csv output files: csv and csv_tz. They are both handle in the same method with mode change.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
which
|
str
|
The type of output file to read. |
required |
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The output file as a DataFrame. |
Source code in pyswap/model/model.py
read_swap_log()
¶
Read the log files.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The content of the log file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no log file is found. There should always be a log file. If not, something went wrong. |
FileExistsError
|
If multiple log files are found. Not sure if this is possible or not. If so, it should be handled. |
Source code in pyswap/model/model.py
run_parallel(mls, path=None, silence_warnings=False, **kwargs)
¶
Run multiple models in parallel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mls
|
list[Model]
|
List of models to run. |
required |
path
|
Path | str
|
The path to the temporary directory. |
None
|
silence_warnings
|
bool
|
If True, warnings are not raised. |
False
|
**kwargs
|
dict
|
Keyword arguments for Pool(). |
{}
|
Returns:
Type | Description |
---|---|
list[Result]
|
list[Result]: List of results from the model runs. |
Source code in pyswap/model/model.py
Capturing model results.
After a model is run, the results are stored in a Result
object. The object
stores the log file, output file, and warnings. Output is a dictionary with
the keys being the file extensions and the values being the file contents. There
are also computed properties making the most common output formats easily
accessible.
Classes:
Name | Description |
---|---|
Result |
Result of a model run. |
Result
¶
Bases: BaseModel
Result of a model run.
Attributes:
Name | Type | Description |
---|---|---|
log |
str
|
The log file of the model run. |
output |
DataFrame
|
The output file of the model run. |
warning |
List[str]
|
The warnings of the model run. |
Properties
ascii (dict): The output in ASCII format. csv (DataFrame): The output in CSV format. csv_tz (DataFrame): The output in CSV format with depth. iteration_stats (str): Return the part the iteration statistics from the log. blc_summary (str): The .blc file if it exists. yearly_summary (DataFrame): Yearly sums of all output variables. Will return an error if csv was not included in the output file formats.