PfsDocument

PfsDocument(self, data, *, encoding='cp1252', names=None, unique_keywords=False)

Create a PfsDocument object for reading, writing and manipulating pfs files.

Parameters

Name Type Description Default
data TextIO | PfsSection | Mapping[str | PfsSection, Any] | str | Path Either a file name (including full path) to the pfs file to be read or dictionary-like structure. required
encoding str How is the pfs file encoded? By default cp1252 'cp1252'
unique_keywords bool Should the keywords in a section be unique? Some tools e.g. the MIKE Plot Composer allows non-unique keywords. If True: warnings will be issued if non-unique keywords are present and the first occurence will be used by default False False

Attributes

Name Description
is_unique Are the target (root) names unique?
n_targets Number of targets (root sections).
names Names of the targets (root sections) as a list.
targets List of targets (root sections).

Methods

Name Description
clear Remove all items from the PfsSection.
copy Return a deep copy of the PfsDocument.
find_replace Update recursively all old_value with new_value.
from_dataframe Create a PfsSection from a DataFrame.
from_text Create a PfsDocument from a string.
get Return the value for key if key is in the PfsSection,
items Return a new view of the PfsDocument’s items ((key, value) pairs).
keys Return a list of the PfsDocument’s keys (target names).
pop If key is in the dictionary, remove it and return its
search Find recursively all keys, sections or parameters
to_dataframe Output enumerated subsections to a DataFrame.
to_dict Convert to (nested) dict (as a copy).
update_recursive Update recursively all matches of key with value.
values Return a list of the PfsDocument’s values (targets).
write Write object to a pfs file.

clear

PfsDocument.clear()

Remove all items from the PfsSection.

copy

PfsDocument.copy()

Return a deep copy of the PfsDocument.

find_replace

PfsDocument.find_replace(old_value, new_value)

Update recursively all old_value with new_value.

from_dataframe

PfsDocument.from_dataframe(df, prefix)

Create a PfsSection from a DataFrame.

Parameters

Name Type Description Default
df pd.DataFrame data required
prefix str section header prefix required

Examples

import pandas as pd
import mikeio
df = pd.DataFrame(dict(station=["Foo", "Bar"],include=[0,1]), index=[1,2])
df
station include
1 Foo 0
2 Bar 1
mikeio.PfsSection.from_dataframe(df,"STATION_")
[STATION_1]
   station = 'Foo'
   include = 0
EndSect  // STATION_1
[STATION_2]
   station = 'Bar'
   include = 1
EndSect  // STATION_2

from_text

PfsDocument.from_text(text)

Create a PfsDocument from a string.

get

PfsDocument.get(key, default=None)

Return the value for key if key is in the PfsSection, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

items

PfsDocument.items()

Return a new view of the PfsDocument’s items ((key, value) pairs).

keys

PfsDocument.keys()

Return a list of the PfsDocument’s keys (target names).

pop

PfsDocument.pop(key, default=None)

If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

search

PfsDocument.search(text=None, *, key=None, section=None, param=None, case=False)

Find recursively all keys, sections or parameters matching a pattern.

NOTE: logical OR between multiple conditions

Parameters

Name Type Description Default
text str Search for text in either key, section or parameter, by default None None
key str text pattern to seach for in keywords, by default None None
section str text pattern to seach for in sections, by default None None
param (str, bool, float, int) text or value in a parameter, by default None None
case bool should the text search be case-sensitive?, by default False False

Returns

Name Type Description
PfsSection Search result as a nested PfsSection

to_dataframe

PfsDocument.to_dataframe(prefix=None)

Output enumerated subsections to a DataFrame.

Parameters

Name Type Description Default
prefix str The prefix of the enumerated sections, e.g. “OUTPUT_”, which can be supplied if it fails without this argument, by default None (will try to “guess” the prefix) None

Returns

Name Type Description
pd.DataFrame The enumerated subsections as a DataFrame

Examples

pfs = mikeio.read_pfs("../data/pfs/lake.sw")
pfs.SW.OUTPUTS.to_dataframe(prefix="OUTPUT_")
Touched include title file_name type format flood_and_dry coordinate_type zone input_file_name ... last_time_step time_step_frequency number_of_points POINT_1 LINE AREA INTEGRAL_WAVE_PARAMETERS INPUT_PARAMETERS MODEL_PARAMETERS SPECTRAL_PARAMETERS
1 1 1 Wave parameters in domain Wave_parameters.dfsu 1 2 2 UTM-32 0 || ... 450 10 1 {'name': 'POINT_1', 'x': 20000.0, 'y': 20000.0} {'npoints': 3, 'x_first': 0.0, 'y_first': 0.0,... {'number_of_points': 4, 'POINT_1': {'x': -400.... {'Touched': 1, 'type_of_spectrum': 1, 'minimum... {'Touched': 1, 'Surface_elevation': 0, 'Water_... {'Touched': 1, 'Wind_friction_speed': 0, 'Roug... {'Touched': 1, 'separation_of_wind_sea_and_swe...
2 1 0 Wave parameters along line Wave_line.dfs1 1 1 2 UTM-32 0 || ... 450 10 1 {'name': 'POINT_1', 'x': 20000.0, 'y': 20000.0} {'npoints': 41, 'x_first': 0.0, 'y_first': 200... {'number_of_points': 4, 'POINT_1': {'x': -400.... {'Touched': 1, 'type_of_spectrum': 1, 'minimum... {'Touched': 1, 'Surface_elevation': 0, 'Water_... {'Touched': 1, 'Wind_friction_speed': 0, 'Roug... {'Touched': 1, 'separation_of_wind_sea_and_swe...
3 1 1 Wave parameters in a point Waves_x20km_y20km.dfs0 1 0 2 UTM-32 0 || ... 450 1 1 {'name': 'POINT_1', 'x': 38000.0, 'y': 20000.0} {'npoints': 3, 'x_first': 0.0, 'y_first': 0.0,... {'number_of_points': 4, 'POINT_1': {'x': -400.... {'Touched': 1, 'type_of_spectrum': 1, 'minimum... {'Touched': 1, 'Surface_elevation': 0, 'Water_... {'Touched': 1, 'Wind_friction_speed': 0, 'Roug... {'Touched': 1, 'separation_of_wind_sea_and_swe...
4 1 1 Spectrum in a point spectrum_x20km_y20km.dfsu 4 0 2 UTM-32 0 || ... 450 10 1 {'name': 'POINT_1', 'x': 38000.0, 'y': 20000.0} {'npoints': 3, 'x_first': 0.0, 'y_first': 0.0,... {'number_of_points': 4, 'POINT_1': {'x': -400.... {'Touched': 1, 'type_of_spectrum': 1, 'minimum... {'Touched': 1, 'Surface_elevation': 0, 'Water_... {'Touched': 1, 'Wind_friction_speed': 0, 'Roug... {'Touched': 1, 'separation_of_wind_sea_and_swe...

4 rows × 24 columns

to_dict

PfsDocument.to_dict()

Convert to (nested) dict (as a copy).

update_recursive

PfsDocument.update_recursive(key, value)

Update recursively all matches of key with value.

values

PfsDocument.values()

Return a list of the PfsDocument’s values (targets).

write

PfsDocument.write(filename)

Write object to a pfs file.

Parameters

Name Type Description Default
filename str Full path and filename of pfs to be created. required

Notes

To return the content as a string, use repr()