#!/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 # If you are unsure the name of a section, it is also possible to search for a specific string in the file, to find the name of a specific section. # # In the example below we do an case-insensitive search for the string 'charnock', which occurs at 6 different places in this file. # In[7]: pfs.search("charnock") # The same search can be done at any level of the hierarchy, i.e. to search only within the OUTPUTS section: # In[8]: pfs.FemEngineSW.SPECTRAL_WAVE_MODULE.OUTPUTS.search("charnock") # In[9]: pfs.FemEngineSW.SPECTRAL_WAVE_MODULE.WIND.search("charnock") # 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[10]: pfs.SW.SPECTRAL.number_of_directions # In[11]: pfs.SW.SPECTRAL.maximum_threshold_frequency # Enumerated sections (e.g. [OUTPUT_1], [OUTPUT_2], ...) can be outputted in tabular form (dataframe). # In[12]: 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[13]: pfs.SW.SPECTRAL.number_of_directions = 32 # In[14]: pfs.SW.SPECTRAL # ### Add a new keyword # In[15]: pfs.SW.SPECTRAL["new_keyword"] = "new_value" # In[16]: pfs.SW.SPECTRAL # ### Add a section # # Let's create an additional output, by copying OUTPUT_4 and modifying some parameters. # In[17]: pfs.SW.OUTPUTS.number_of_outputs += 1 # In[18]: new_output = pfs.SW.OUTPUTS.OUTPUT_4.copy() # In[19]: new_output.file_name = 'spectrum_x10km_y40km.dfsu' new_output.POINT_1.x = 10000 new_output.POINT_1.y = 40000 # In[20]: pfs.SW.OUTPUTS["OUTPUT_5"] = new_output # In[21]: 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[22]: pfs.write("lake_modified.pfs") # In[23]: pfs.to_dict() # In[24]: # 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[25]: 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[26]: pfs = mikeio.PfsDocument({"t1_t0": t1_t0}) pfs # In[27]: pfs.write("extract_point.mzt") # ## Clean up # In[28]: import os os.remove("lake_modified.pfs") os.remove('lake_modified.yaml') os.remove("extract_point.mzt")