#!/usr/bin/env python # coding: utf-8 # # PFS # # The support for PFS files have been extended with MIKE IO release 1.2. It was previously only possible to *read* PFS files. It is now also possible to *modify* and *create* new PFS files. # In[1]: import mikeio # ## Read # In[2]: pfs = mikeio.read_pfs("../tests/testdata/pfs/lake.sw") pfs # The "target" (root section) is in this case called FemEngineSW. `pfs.FemEngineSW` is a PfsSection object that contains other PfsSection objects. Let's print the names of it's subsections: # In[3]: pfs.FemEngineSW.keys() # It is possible to navigate to each section and keyword in the pfs file: # In[4]: pfs.FemEngineSW.DOMAIN.file_name # In[5]: pfs.FemEngineSW.MODULE_SELECTION # In[6]: pfs.FemEngineSW.MODULE_SELECTION.mode_of_spectral_wave_module # MIKE FM PFS files has a specific structure and active FM modules can be accessed by an alias on the Pfs object. In this case, `pfs.SW` can be used as a short-hand for `pfs.FemEngineSW.SPECTRAL_WAVE_MODULE`. # In[7]: pfs.SW.SPECTRAL.number_of_directions # In[8]: pfs.SW.SPECTRAL.maximum_threshold_frequency # Enumerated sections (e.g. [OUTPUT_1], [OUTPUT_2], ...) can be outputted in tabular form (dataframe). # In[9]: df = pfs.SW.OUTPUTS.to_dataframe() df # ## Modify # # The PfsSection object can be modified. Existing values can be changes, new key-value pairs can be added, subsections can added or removed. # In[10]: pfs.SW.SPECTRAL.number_of_directions = 32 # In[11]: pfs.SW.SPECTRAL # ### Add a new keyword # In[12]: pfs.SW.SPECTRAL["new_keyword"] = "new_value" # In[13]: pfs.SW.SPECTRAL # ### Add a section # # Let's create an additional output, by copying OUTPUT_4 and modifying some parameters. # In[14]: pfs.SW.OUTPUTS.number_of_outputs += 1 # In[15]: new_output = pfs.SW.OUTPUTS.OUTPUT_4.copy() # In[16]: new_output.file_name = 'spectrum_x10km_y40km.dfsu' new_output.POINT_1.x = 10000 new_output.POINT_1.y = 40000 # In[17]: pfs.SW.OUTPUTS["OUTPUT_5"] = new_output # In[18]: pfs.SW.OUTPUTS.keys() # ## Output # # The Pfs object can be written to pfs file, but can also be exported to a dictionary (which in turn can be written to a yaml or json file). # In[19]: pfs.write("lake_modified.pfs") # In[20]: pfs.to_dict() # In[21]: # write to yaml file import yaml yaml.dump(pfs.to_dict(), open('lake_modified.yaml', 'w+')) # ## Create # # A PFS file can also be created from a dictionary, like this: # In[22]: setup = { "Name": "Extract that", "InputFileName": "|random.dfs1|", "FirstTimeStep": 0, "LastTimeStep": 99, "X": 2, "OutputFileName": "|.\\out2.dfs0|", } t1_t0 = {"CLSID": "t1_t0.dll", "TypeName": "t1_t0", "Setup": setup} t1_t0 # In[23]: section = mikeio.PfsSection(t1_t0) section # In[24]: pfs = section.to_Pfs(name="t1_t0") pfs # In[25]: pfs.write("extract_point.mzt") # ## Clean up # In[26]: import os os.remove("lake_modified.pfs") os.remove('lake_modified.yaml') os.remove("extract_point.mzt")