Commit a08b83ed authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

paramin_replace usable

parent 3d2f2e05
...@@ -7,6 +7,7 @@ __version__ = "0.0.1" ...@@ -7,6 +7,7 @@ __version__ = "0.0.1"
__maintainer__ = "Qusai Al Shidi" __maintainer__ = "Qusai Al Shidi"
__email__ = "qusai@umich.edu" __email__ = "qusai@umich.edu"
import logging
import datetime as dt import datetime as dt
import numpy as np import numpy as np
import pandas as pd import pandas as pd
...@@ -223,30 +224,33 @@ def read_gm_log(filename, colnames=None, index_by_time=True): ...@@ -223,30 +224,33 @@ def read_gm_log(filename, colnames=None, index_by_time=True):
return data return data
def replace_paramin_values(param_file, replace, output_file="PARAM.in"): def paramin_replace(input_file, replace, output_file="PARAM.in"):
"""Replace values for the parameters in a PARAM.in file. """Replace values for the parameters in a PARAM.in file.
Note, if you have repeat commands this will replace all the repeats. Note, if you have repeat commands this will replace all the repeats.
Args: Args:
param_file (str): String of PARAM.in file name. input_file (str): String of PARAM.in file name.
replace (dict): Dictionary of strings with format replace (dict): Dictionary of strs with format
replace["#COMMAND"]["parameter"] = "value" replace = {"#COMMAND": ["value", "comments", ...]}
This is case sensitive. This is case sensitive.
output_file (str): (default "PARAM.in") The output file to write to. output_file (str): (default "PARAM.in") The output file to write to.
A value of None will not output a file. A value of None will not output a file.
Returns: Returns:
A list of lines of the PARAM.in file that would be outputted. A list of lines of the PARAM.in file that would be outputted.
Example: Examples:
change["#SOLARWINDFILE"]["UseSolarWindFile"] = "T" ```
change["#SOLARWINDFILE"]["NameSolarWindFile"] = "new_imf.dat" change["#SOLARWINDFILE"] = [["T", "UseSolarWindFile"],
change["#DOAMR"]["DnAmr"] = 200 ["new_imf.dat", "NameSolarWindFile"]]
# This will overwrite PARAM.in # This will overwrite PARAM.in
replace_paramin_option("PARAM.in.template", change) swmfpy.paramin_replace("PARAM.in.template", change)
```
""" """
# TODO This will replace all for repeat commands. # TODO This will replace all for repeat commands.
with open(param_file, 'rt') as paramin: logger = logging.getLogger() # For debugging
# Read and replace paramin file by making a temp list
with open(input_file, 'rt') as paramin:
command = None # Top level #COMMAND command = None # Top level #COMMAND
# Compile lines in a list before editing/writing it # Compile lines in a list before editing/writing it
lines = list(paramin) lines = list(paramin)
...@@ -254,14 +258,14 @@ def replace_paramin_values(param_file, replace, output_file="PARAM.in"): ...@@ -254,14 +258,14 @@ def replace_paramin_values(param_file, replace, output_file="PARAM.in"):
words = line.split() words = line.split()
if words and words[0] in replace.keys(): if words and words[0] in replace.keys():
command = words[0] # Current command command = words[0] # Current command
# Start replace once we're in a specified command for param, value in enumerate(replace[command]):
elif command in replace.keys(): newline = ""
for param, value in replace[command].items(): for text in value:
newline = value newline += text + "\t\t\t"
if param in words: logger.info("Replacing:\n" + line
newline += "\t\t\t" + param + "with:\n" + newline)
print("Replacing:", line, "with:", newline) # Lines will be replaced in order
lines[line_num] = newline + '\n' lines[line_num+param+1] = newline + '\n'
# Write the PARAM.in file # Write the PARAM.in file
if output_file is None: if output_file is None:
return lines # Break if None output_file (not default behaviour) return lines # Break if None output_file (not default behaviour)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment